SNode.C
Loading...
Searching...
No Matches
SocketAddress.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
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#include "net/rc/SocketAddress.h"
21
22#include "net/SocketAddress.hpp"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#endif // DOXYGEN_SHOULD_SKIP_THIS
27
28namespace net::rc {
29
30 SocketAddress::SocketAddress()
31 : Super(AF_BLUETOOTH) {
32 }
33
34 SocketAddress::SocketAddress(const std::string& btAddress)
35 : SocketAddress() {
36 setBtAddress(btAddress);
37 }
38
39 SocketAddress::SocketAddress(uint8_t channel)
40 : SocketAddress() {
41 setChannel(channel);
42 }
43
44 SocketAddress::SocketAddress(const std::string& btAddress, uint8_t channel)
45 : SocketAddress() {
46 setBtAddress(btAddress);
47 setChannel(channel);
48 }
49
50 SocketAddress::SocketAddress(const SockAddr& sockAddr, SockLen sockAddrLen)
52 channel = sockAddr.rc_channel;
53
54 char btAddressC[15];
55 ba2str(&sockAddr.rc_bdaddr, btAddressC);
56 btAddress = btAddressC;
57 }
58
59 void SocketAddress::init() {
60 sockAddr.rc_channel = channel;
61 str2ba(btAddress.c_str(), &sockAddr.rc_bdaddr);
62 }
63
64 SocketAddress& SocketAddress::setBtAddress(const std::string& btAddress) {
65 this->btAddress = btAddress;
66
67 return *this;
68 }
69
70 std::string SocketAddress::getBtAddress() const {
71 return btAddress;
72 }
73
74 SocketAddress& SocketAddress::setChannel(uint8_t channel) {
75 this->channel = channel;
76
77 return *this;
78 }
79
80 uint8_t SocketAddress::getChannel() const {
81 return channel;
82 }
83
84 std::string SocketAddress::toString(bool expanded) const {
85 return std::string(btAddress).append(expanded ? std::string(":").append(std::to_string(channel)) : "");
86 }
87
88} // namespace net::rc
89
90template class net::SocketAddress<sockaddr_rc>;
uint8_t getChannel() const
SocketAddress & setChannel(uint8_t channel)
SocketAddress(const std::string &btAddress, uint8_t channel)
std::string toString(bool expanded=true) const override
SocketAddress(const std::string &btAddress)
SocketAddress & setBtAddress(const std::string &btAddress)
SocketAddress(const SockAddr &sockAddr, SockLen sockAddrLen)
std::string getBtAddress() const
SocketAddress(uint8_t channel)