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#include "iot/mqtt/packets/Connect.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include <vector>
25
26#endif // DOXYGEN_SHOULD_SKIP_THIS
27
28namespace iot::mqtt::packets {
29
31 : iot::mqtt::ControlPacket(MQTT_CONNECT, "CONNECT") {
32 }
33
34 Connect::Connect(const std::string& clientId,
35 uint16_t keepAlive,
36 bool cleanSession,
37 const std::string& willTopic,
38 const std::string& willMessage,
39 uint8_t willQoS,
40 bool willRetain,
41 const std::string& username,
42 const std::string& password,
43 bool loopPrevention)
44 : Connect() {
45 this->protocol = "MQTT";
46 this->level =
47 MQTT_VERSION_3_1_1 | (loopPrevention ? 0x80 : 0x00); // msb 1 -> do not reflect messages to origin (try_private in mosquitto)
48 this->keepAlive = keepAlive;
49 this->clientId = clientId;
50 this->willTopic = willTopic;
51 this->willMessage = willMessage;
52 this->willQoS = willQoS;
53 this->willRetain = willRetain;
54 this->willFlag = !willMessage.empty();
55 this->username = username;
56 this->usernameFlag = !username.empty();
57 this->password = password;
58 this->passwordFlag = !password.empty();
59
60 this->connectFlags = static_cast<uint8_t>((!usernameFlag ? 0 : 0x80) | (!passwordFlag ? 0 : 0x40) | (!willRetain ? 0 : 0x20) |
61 ((willQoS << 3) & 0x18) | (!willFlag ? 0 : 0x04) | (!cleanSession ? 0 : 0x02));
62 }
63
64 std::vector<char> Connect::serializeVP() const {
65 std::vector<char> packet(protocol.serialize());
66
67 std::vector<char> tmpVector = level.serialize();
68 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
69
70 tmpVector = connectFlags.serialize();
71 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
72
73 tmpVector = keepAlive.serialize();
74 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
75
76 tmpVector = clientId.serialize();
77 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
78
79 if (willFlag) {
80 tmpVector = willTopic.serialize();
81 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
82
83 tmpVector = willMessage.serialize();
84 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
85 }
86 if (usernameFlag) {
87 tmpVector = username.serialize();
88 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
89 }
90 if (passwordFlag) {
91 tmpVector = password.serialize();
92 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
93 }
94
95 return packet;
96 }
97
98 std::string Connect::getProtocol() const {
99 return protocol;
100 }
101
102 uint8_t Connect::getLevel() const {
103 return level;
104 }
105
106 uint8_t Connect::getConnectFlags() const {
107 return connectFlags;
108 }
109
110 uint16_t Connect::getKeepAlive() const {
111 return keepAlive;
112 }
113
114 std::string Connect::getClientId() const {
115 return clientId;
116 }
117
118 bool Connect::getUsernameFlag() const {
119 return usernameFlag;
120 }
121
122 bool Connect::getPasswordFlag() const {
123 return passwordFlag;
124 }
125
126 bool Connect::getWillRetain() const {
127 return willRetain;
128 }
129
130 uint8_t Connect::getWillQoS() const {
131 return willQoS;
132 }
133
134 bool Connect::getWillFlag() const {
135 return willFlag;
136 }
137
138 bool Connect::getCleanSession() const {
139 return cleanSession;
140 }
141
142 bool Connect::getReflect() const {
143 return reflect;
144 }
145
146 std::string Connect::getWillTopic() const {
147 return willTopic;
148 }
149
150 std::string Connect::getWillMessage() const {
151 return willMessage;
152 }
153
154 std::string Connect::getUsername() const {
155 return username;
156 }
157
158 std::string Connect::getPassword() const {
159 return password;
160 }
161
162} // namespace iot::mqtt::packets
uint8_t getLevel() const
Definition Connect.cpp: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:34
uint8_t getWillQoS() const
Definition Connect.cpp:130
std::string getClientId() const
Definition Connect.cpp:114
std::string getWillTopic() const
Definition Connect.cpp:146
uint16_t getKeepAlive() const
Definition Connect.cpp:110
std::vector< char > serializeVP() const override
Definition Connect.cpp:64
std::string getUsername() const
Definition Connect.cpp:154
uint8_t getConnectFlags() const
Definition Connect.cpp:106
std::string getPassword() const
Definition Connect.cpp:158
std::string getWillMessage() const
Definition Connect.cpp:150
std::string getProtocol() const
Definition Connect.cpp:98
#define MQTT_CONNECT
Definition Connect.h:36
#define MQTT_VERSION_3_1_1
Definition Connect.h:38