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/l2/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::l2 {
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(uint16_t psm)
40 : SocketAddress() {
41 setPsm(psm);
42 }
43
44 SocketAddress::SocketAddress(const std::string& btAddress, uint16_t psm)
45 : SocketAddress() {
46 setBtAddress(btAddress);
47 setPsm(psm);
48 }
49
50 SocketAddress::SocketAddress(const SockAddr& sockAddr, SockLen sockAddrLen)
52 psm = btohs(sockAddr.l2_psm);
53
54 char btAddressC[15];
55 ba2str(&sockAddr.l2_bdaddr, btAddressC);
56 btAddress = btAddressC;
57 }
58
59 void SocketAddress::init() {
60 sockAddr.l2_psm = htobs(psm);
61 str2ba(btAddress.c_str(), &sockAddr.l2_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::setPsm(uint16_t psm) {
75 this->psm = psm;
76
77 return *this;
78 }
79
80 uint16_t SocketAddress::getPsm() const {
81 return psm;
82 }
83
84 std::string SocketAddress::toString([[maybe_unused]] bool expanded) const {
85 return std::string(btAddress).append(expanded ? std::string(":").append(std::to_string(psm)) : "");
86 }
87
88} // namespace net::l2
89
90template class net::SocketAddress<sockaddr_l2>;
uint16_t getPsm() const
SocketAddress(const std::string &btAddress, uint16_t psm)
SocketAddress(const std::string &btAddress)
SocketAddress & setPsm(uint16_t psm)
std::string getBtAddress() const
SocketAddress(uint16_t psm)
SocketAddress(const SockAddr &sockAddr, SockLen sockAddrLen)
SocketAddress & setBtAddress(const std::string &btAddress)
std::string toString(bool expanded=true) const override