SNode.C
Loading...
Searching...
No Matches
ConfigAddress.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "net/rc/config/ConfigAddress.h"
43
44#include "net/config/ConfigAddressBase.hpp" // IWYU pragma: keep
45#include "net/config/ConfigAddressLocal.hpp" // IWYU pragma: keep
46#include "net/config/ConfigAddressRemote.hpp" // IWYU pragma: keep
47#include "net/config/ConfigAddressReverse.hpp" // IWYU pragma: keep
48#include "net/config/ConfigSection.hpp"
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52#include "utils/PreserveErrno.h"
53
54#include <regex>
55
56#endif /* DOXYGEN_SHOULD_SKIP_THIS */
57
58namespace net::rc::config {
59
60 template <template <typename SocketAddress> typename ConfigAddressType>
62 [[maybe_unused]] const std::string& addressOptionName,
63 [[maybe_unused]] const std::string& addressOptionDescription)
64 : ConfigSection(instance, this)
65 , ConfigAddressType<net::rc::SocketAddress>(this) {
66 }
67
68 template <template <typename SocketAddress> typename ConfigAddressType>
69 ConfigAddress<ConfigAddressType>::ConfigAddress(net::config::ConfigInstance* instance,
70 [[maybe_unused]] const std::string& addressOptionName,
71 [[maybe_unused]] const std::string& addressOptionDescription)
72 : ConfigSection(instance, this)
73 , ConfigAddressType<net::rc::SocketAddress>(this) {
74 btAddressOpt = addOption( //
75 "--host",
76 "Bluetooth address (format 01:23:45:67:89:AB)",
77 "address",
78 "00:00:00:00:00:00",
79 CLI::Validator(
80 [](std::string& s) -> std::string {
81 // Classic 48-bit Bluetooth/MAC style: "01:23:45:67:89:AB"
82 static const std::regex re(R"(^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$)");
83
84 if (std::regex_match(s, re)) {
85 return {}; // OK
86 }
87
88 return "Invalid Bluetooth address. Expected format like \"01:23:45:67:89:AB\" "
89 "(6 hex octets separated by ':').";
90 },
91 "BT_ADDR")
92 .name("BT_ADDR"));
93
94 channelOpt = addOption( //
95 "--channel",
96 "Channel number",
97 "channel",
98 "1",
99 CLI::Range(static_cast<uint16_t>(1), static_cast<uint16_t>(30)));
101
102 template <template <typename SocketAddress> typename ConfigAddressType>
103 SocketAddress* ConfigAddress<ConfigAddressType>::init() {
104 SocketAddress* socketAddress = new SocketAddress(btAddressOpt->as<std::string>(), channelOpt->as<uint8_t>());
105 socketAddress->init();
106
107 return socketAddress;
108 }
109
110 template <template <typename SocketAddress> typename ConfigAddressType>
111 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setSocketAddress(const SocketAddress& socketAddress) {
113 setChannel(socketAddress.getChannel());
114
115 return *this;
116 }
117
118 template <template <typename SocketAddress> typename ConfigAddressType>
119 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setBtAddress(const std::string& btAddress) {
120 const utils::PreserveErrno preserveErrno;
121
122 setDefaultValue(btAddressOpt, btAddress);
123 required(btAddressOpt, false);
124
125 return *this;
126 }
127
128 template <template <typename SocketAddress> typename ConfigAddressType>
129 std::string ConfigAddress<ConfigAddressType>::getBtAddress() const {
130 return btAddressOpt->as<std::string>();
131 }
132
133 template <template <typename SocketAddress> typename ConfigAddressType>
134 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setChannel(uint8_t channel) {
135 const utils::PreserveErrno preserveErrno;
136
137 setDefaultValue<int>(channelOpt, channel);
138 required(channelOpt, false);
139
140 return *this;
141 }
142
143 template <template <typename SocketAddress> typename ConfigAddressType>
144 uint8_t ConfigAddress<ConfigAddressType>::getChannel() const {
145 return channelOpt->as<uint8_t>();
146 }
147
148 template <template <typename SocketAddressT> typename ConfigAddressTypeT>
149 void ConfigAddress<ConfigAddressTypeT>::configurable(bool configurable) {
150 this->setConfigurable(btAddressOpt, configurable);
151 this->setConfigurable(channelOpt, configurable);
152 }
153
154 template <template <typename SocketAddress> typename ConfigAddressType>
155 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setBtAddressRequired(bool required) {
156 this->required(btAddressOpt, required);
157
158 return *this;
159 }
160
161 template <template <typename SocketAddress> typename ConfigAddressType>
162 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setChannelRequired(bool required) {
163 this->required(channelOpt, required);
164
165 return *this;
166 }
167
168} // namespace net::rc::config
169
170template class net::config::ConfigAddress<net::rc::SocketAddress>;
171template class net::config::ConfigAddressLocal<net::rc::SocketAddress>;
172template class net::config::ConfigAddressRemote<net::rc::SocketAddress>;
173template class net::config::ConfigAddressReverse<net::rc::SocketAddress>;
174template class net::config::ConfigAddressBase<net::rc::SocketAddress>;
175template class net::rc::config::ConfigAddress<net::config::ConfigAddressLocal>;
176template class net::rc::config::ConfigAddress<net::config::ConfigAddressRemote>;
177template class net::rc::config::ConfigAddressReverse<net::config::ConfigAddressReverse>;
uint8_t getChannel() const
SocketAddress(const std::string &btAddress, uint8_t channel)
std::string getBtAddress() const
ConfigAddressReverse(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)
SocketAddress * init() final
ConfigAddress(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)
ConfigAddress & setChannel(uint8_t channel)
void configurable(bool configurable=true) final
ConfigAddress & setBtAddress(const std::string &btAddress)
ConfigAddress & setSocketAddress(const SocketAddress &socketAddress)
ConfigAddress & setChannelRequired(bool required=true)
ConfigAddress & setBtAddressRequired(bool required=true)