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/l2/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 <limits>
55#include <regex>
56
57#endif /* DOXYGEN_SHOULD_SKIP_THIS */
58
59namespace net::l2::config {
60
61 template <template <typename SocketAddress> typename ConfigAddressType>
63 [[maybe_unused]] const std::string& addressOptionName,
64 [[maybe_unused]] const std::string& addressOptionDescription)
65 : ConfigSection(instance, this)
66 , ConfigAddressType<net::l2::SocketAddress>(this) {
67 }
68
69 template <template <typename SocketAddress> typename ConfigAddressType>
70 ConfigAddress<ConfigAddressType>::ConfigAddress(net::config::ConfigInstance* instance,
71 [[maybe_unused]] const std::string& addressOptionName,
72 [[maybe_unused]] const std::string& addressOptionDescription)
73 : ConfigSection(instance, this)
74 , ConfigAddressType<net::l2::SocketAddress>(this) {
75 btAddressOpt = addOption( //
76 "--host",
77 "Bluetooth address (format 01:23:45:67:89:AB)",
78 "address",
79 "00:00:00:00:00:00",
80 CLI::Validator(
81 [](std::string& s) -> std::string {
82 // Classic 48-bit Bluetooth/MAC style: "01:23:45:67:89:AB"
83 static const std::regex re(R"(^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$)");
85 if (std::regex_match(s, re)) {
86 return {}; // OK
87 }
88
89 return "Invalid Bluetooth address. Expected format like \"01:23:45:67:89:AB\" "
90 "(6 hex octets separated by ':').";
91 },
92 "BT_ADDR")
93 .name("BT_ADDR"));
95 psmOpt = addOption( //
96 "--psm",
97 "Protocol service multiplexer",
98 "psm",
99 "0",
100 CLI::Range(std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max()));
101 }
102
103 template <template <typename SocketAddress> typename ConfigAddressType>
104 SocketAddress* ConfigAddress<ConfigAddressType>::init() {
105 SocketAddress* socketAddress = new SocketAddress(btAddressOpt->as<std::string>(), psmOpt->as<uint16_t>());
106 socketAddress->init();
107
108 return socketAddress;
109 }
110
111 template <template <typename SocketAddress> typename ConfigAddressType>
112 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setSocketAddress(const SocketAddress& socketAddress) {
114 setPsm(socketAddress.getPsm());
115
116 return *this;
117 }
118
119 template <template <typename SocketAddress> typename ConfigAddressType>
120 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setBtAddress(const std::string& btAddress) {
121 const utils::PreserveErrno preserveErrno;
122
123 setDefaultValue(btAddressOpt, btAddress);
124 required(btAddressOpt, false);
125
126 return *this;
127 }
128
129 template <template <typename SocketAddress> typename ConfigAddressType>
130 std::string ConfigAddress<ConfigAddressType>::getBtAddress() const {
131 return btAddressOpt->as<std::string>();
132 }
133
134 template <template <typename SocketAddress> typename ConfigAddressType>
135 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setPsm(uint16_t psm) {
136 const utils::PreserveErrno preserveErrno;
137
138 setDefaultValue(psmOpt, psm);
139 required(psmOpt, false);
140
141 return *this;
142 }
143
144 template <template <typename SocketAddress> typename ConfigAddressType>
145 uint16_t ConfigAddress<ConfigAddressType>::getPsm() const {
146 return psmOpt->as<uint16_t>();
147 }
148
149 template <template <typename SocketAddressT> typename ConfigAddressTypeT>
150 void ConfigAddress<ConfigAddressTypeT>::configurable(bool configurable) {
151 this->setConfigurable(btAddressOpt, configurable);
152 this->setConfigurable(psmOpt, configurable);
153 }
154
155 template <template <typename SocketAddress> typename ConfigAddressType>
156 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setBtAddressRequired(bool required) {
157 this->required(btAddressOpt, required);
158
159 return *this;
160 }
161
162 template <template <typename SocketAddress> typename ConfigAddressType>
163 ConfigAddress<ConfigAddressType>& ConfigAddress<ConfigAddressType>::setPsmRequired(bool required) {
164 this->required(psmOpt, required);
165
166 return *this;
167 }
168
169} // namespace net::l2::config
170
171template class net::config::ConfigAddress<net::l2::SocketAddress>;
172template class net::config::ConfigAddressLocal<net::l2::SocketAddress>;
173template class net::config::ConfigAddressRemote<net::l2::SocketAddress>;
174template class net::config::ConfigAddressReverse<net::l2::SocketAddress>;
175template class net::config::ConfigAddressBase<net::l2::SocketAddress>;
176template class net::l2::config::ConfigAddress<net::config::ConfigAddressLocal>;
177template class net::l2::config::ConfigAddress<net::config::ConfigAddressRemote>;
178template class net::l2::config::ConfigAddressReverse<net::config::ConfigAddressReverse>;
uint16_t getPsm() const
SocketAddress(const std::string &btAddress, uint16_t psm)
std::string getBtAddress() const
ConfigAddressReverse(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)
ConfigAddress & setPsm(uint16_t psm)
ConfigAddress & setSocketAddress(const SocketAddress &socketAddress)
ConfigAddress & setBtAddress(const std::string &btAddress)
void configurable(bool configurable=true) final
ConfigAddress & setPsmRequired(bool required=true)
ConfigAddress & setBtAddressRequired(bool required=true)
SocketAddress * init() final
ConfigAddress(net::config::ConfigInstance *instance, const std::string &addressOptionName, const std::string &addressOptionDescription)