SNode.C
Loading...
Searching...
No Matches
Connect.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/*
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 "iot/mqtt/packets/Connect.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include <vector>
47
48#endif // DOXYGEN_SHOULD_SKIP_THIS
49
50namespace iot::mqtt::packets {
51
53 : iot::mqtt::ControlPacket(MQTT_CONNECT, "CONNECT") {
54 }
55
56 Connect::Connect(const std::string& clientId,
57 uint16_t keepAlive,
58 bool cleanSession,
59 const std::string& willTopic,
60 const std::string& willMessage,
61 uint8_t willQoS,
62 bool willRetain,
63 const std::string& username,
64 const std::string& password,
65 bool loopPrevention)
66 : Connect() {
67 this->protocol = "MQTT";
68 this->level =
69 MQTT_VERSION_3_1_1 | (loopPrevention ? 0x80 : 0x00); // msb 1 -> do not reflect messages to origin (try_private in mosquitto)
70 this->keepAlive = keepAlive;
71 this->clientId = clientId;
72 this->willTopic = willTopic;
73 this->willMessage = willMessage;
74 this->willQoS = willQoS;
75 this->willRetain = willRetain;
76 this->willFlag = !willMessage.empty();
77 this->username = username;
78 this->usernameFlag = !username.empty();
79 this->password = password;
80 this->passwordFlag = !password.empty();
81
82 this->connectFlags = static_cast<uint8_t>((!usernameFlag ? 0 : 0x80) | (!passwordFlag ? 0 : 0x40) | (!willRetain ? 0 : 0x20) |
83 ((willQoS << 3) & 0x18) | (!willFlag ? 0 : 0x04) | (!cleanSession ? 0 : 0x02));
84 }
85
86 std::vector<char> Connect::serializeVP() const {
87 std::vector<char> packet(protocol.serialize());
88
89 std::vector<char> tmpVector = level.serialize();
90 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
91
92 tmpVector = connectFlags.serialize();
93 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
94
95 tmpVector = keepAlive.serialize();
96 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
97
98 tmpVector = clientId.serialize();
99 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
100
101 if (willFlag) {
102 tmpVector = willTopic.serialize();
103 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
104
105 tmpVector = willMessage.serialize();
106 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
107 }
108 if (usernameFlag) {
109 tmpVector = username.serialize();
110 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
111 }
112 if (passwordFlag) {
113 tmpVector = password.serialize();
114 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
115 }
116
117 return packet;
118 }
119
120 std::string Connect::getProtocol() const {
121 return protocol;
122 }
123
124 uint8_t Connect::getLevel() const {
125 return level;
126 }
127
128 uint8_t Connect::getConnectFlags() const {
129 return connectFlags;
130 }
131
132 uint16_t Connect::getKeepAlive() const {
133 return keepAlive;
134 }
135
136 std::string Connect::getClientId() const {
137 return clientId;
138 }
139
140 bool Connect::getUsernameFlag() const {
141 return usernameFlag;
142 }
143
144 bool Connect::getPasswordFlag() const {
145 return passwordFlag;
146 }
147
148 bool Connect::getWillRetain() const {
149 return willRetain;
150 }
151
152 uint8_t Connect::getWillQoS() const {
153 return willQoS;
154 }
155
156 bool Connect::getWillFlag() const {
157 return willFlag;
158 }
159
160 bool Connect::getCleanSession() const {
161 return cleanSession;
162 }
163
164 bool Connect::getReflect() const {
165 return reflect;
166 }
167
168 std::string Connect::getWillTopic() const {
169 return willTopic;
170 }
171
172 std::string Connect::getWillMessage() const {
173 return willMessage;
174 }
175
176 std::string Connect::getUsername() const {
177 return username;
178 }
179
180 std::string Connect::getPassword() const {
181 return password;
182 }
183
184} // namespace iot::mqtt::packets
ControlPacket(uint8_t type, const std::string &name)
iot::mqtt::types::String willMessage
Definition Connect.h:108
uint8_t getLevel() const
Definition Connect.cpp:124
iot::mqtt::types::String protocol
Definition Connect.h:102
Connect(const std::string &clientId, uint16_t keepAlive, bool cleanSession, const std::string &willTopic, const std::string &willMessage, uint8_t willQoS, bool willRetain, const std::string &username, const std::string &password, bool loopPrevention)
Definition Connect.cpp:56
uint8_t getWillQoS() const
Definition Connect.cpp:152
std::string getClientId() const
Definition Connect.cpp:136
std::string getWillTopic() const
Definition Connect.cpp:168
uint16_t getKeepAlive() const
Definition Connect.cpp:132
iot::mqtt::types::String willTopic
Definition Connect.h:107
std::vector< char > serializeVP() const override
Definition Connect.cpp:86
std::string getUsername() const
Definition Connect.cpp:176
uint8_t getConnectFlags() const
Definition Connect.cpp:128
iot::mqtt::types::UInt8 connectFlags
Definition Connect.h:104
std::string getPassword() const
Definition Connect.cpp:180
iot::mqtt::types::String username
Definition Connect.h:109
iot::mqtt::types::UInt8 level
Definition Connect.h:103
std::string getWillMessage() const
Definition Connect.cpp:172
iot::mqtt::types::String clientId
Definition Connect.h:106
iot::mqtt::types::String password
Definition Connect.h:110
std::string getProtocol() const
Definition Connect.cpp:120
iot::mqtt::types::UInt16 keepAlive
Definition Connect.h:105
std::vector< char > serialize() const override
Definition String.cpp:97
String & operator=(const std::string &newValue)
Definition String.cpp:104
virtual std::vector< char > serialize() const
Definition TypeBase.hpp:83
UInt16 & operator=(const uint16_t &newValue)
Definition UInt16.cpp:58
UInt8 & operator=(const uint8_t &newValue)
Definition UInt8.cpp:57
#define MQTT_CONNECT
Definition Connect.h:58
#define MQTT_VERSION_3_1_1
Definition Connect.h:60