SNode.C
Loading...
Searching...
No Matches
Mqtt.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/client/Mqtt.h"
43
44#include "iot/mqtt/MqttContext.h"
45#include "iot/mqtt/client/packets/Connack.h"
46#include "iot/mqtt/client/packets/Pingresp.h"
47#include "iot/mqtt/client/packets/Puback.h"
48#include "iot/mqtt/client/packets/Pubcomp.h"
49#include "iot/mqtt/client/packets/Publish.h"
50#include "iot/mqtt/client/packets/Pubrec.h"
51#include "iot/mqtt/client/packets/Pubrel.h"
52#include "iot/mqtt/client/packets/Suback.h"
53#include "iot/mqtt/client/packets/Unsuback.h"
54#include "iot/mqtt/packets/Connect.h"
55#include "iot/mqtt/packets/Disconnect.h"
56#include "iot/mqtt/packets/Pingreq.h"
57#include "iot/mqtt/packets/Subscribe.h"
58#include "iot/mqtt/packets/Unsubscribe.h"
59
60#ifndef DOXYGEN_SHOULD_SKIP_THIS
61
62#include "log/Logger.h"
63
64#include <cstdio>
65#include <fstream>
66#include <functional>
67#include <iomanip>
68#include <map>
69#include <nlohmann/json.hpp>
70#include <sstream>
71
72// IWYU pragma: no_include <nlohmann/json_fwd.hpp>
73
74#endif // DOXYGEN_SHOULD_SKIP_THIS
75
76namespace iot::mqtt::client {
77
78 Mqtt::Mqtt(const std::string& connectionName, const std::string& clientId, const std::string& sessionStoreFileName)
79 : Super(connectionName, clientId)
80 , sessionStoreFileName(sessionStoreFileName) { // NOLINT
81 if (!sessionStoreFileName.empty()) {
82 std::ifstream sessionStoreFile(sessionStoreFileName);
83
84 if (sessionStoreFile.is_open()) {
85 try {
86 nlohmann::json sessionStoreJson;
87
88 sessionStoreFile >> sessionStoreJson;
89
90 session.fromJson(sessionStoreJson);
91
92 LOG(DEBUG) << connectionName << " MQTT Client: ... Persistent session data loaded successful";
93 } catch (const nlohmann::json::exception&) {
94 LOG(DEBUG) << connectionName << " MQTT Client: ... Starting with empty session: Session store '"
95 << sessionStoreFileName << "' empty or corrupted";
96
98 }
99
100 sessionStoreFile.close();
101 std::remove(sessionStoreFileName.data()); // NOLINT
102
103 LOG(INFO) << connectionName << " MQTT Client: Restoring saved session done";
104 } else {
105 PLOG(WARNING) << connectionName << " MQTT Client: ... Could not read session store '" << sessionStoreFileName << "'";
106 }
107 } else {
108 LOG(INFO) << connectionName << " MQTT Client: Session not reloaded: Session store filename empty";
109 }
110 }
111
112 Mqtt::~Mqtt() {
113 if (!sessionStoreFileName.empty()) {
114 const nlohmann::json sessionJson = session.toJson();
115
116 std::ofstream sessionStoreFile(sessionStoreFileName);
117
118 if (sessionStoreFile.is_open()) {
119 if (!sessionJson.empty()) {
120 sessionStoreFile << sessionJson;
121 }
122
123 sessionStoreFile.close();
124 } else {
125 PLOG(DEBUG) << connectionName << " MQTT Client: Could not write session store '" << sessionStoreFileName << "'";
126 }
127 } else {
128 LOG(INFO) << connectionName << " MQTT Client: Session not saved: Session store filename empty";
129 }
130
132 }
133
135 iot::mqtt::ControlPacketDeserializer* currentPacket = nullptr;
136
137 switch (fixedHeader.getType()) {
138 case MQTT_CONNACK:
139 currentPacket = new iot::mqtt::client::packets::Connack(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
140 break;
141 case MQTT_PUBLISH:
142 currentPacket = new iot::mqtt::client::packets::Publish(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
143 break;
144 case MQTT_PUBACK:
145 currentPacket = new iot::mqtt::client::packets::Puback(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
146 break;
147 case MQTT_PUBREC:
148 currentPacket = new iot::mqtt::client::packets::Pubrec(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
149 break;
150 case MQTT_PUBREL:
151 currentPacket = new iot::mqtt::client::packets::Pubrel(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
152 break;
153 case MQTT_PUBCOMP:
154 currentPacket = new iot::mqtt::client::packets::Pubcomp(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
155 break;
156 case MQTT_SUBACK:
157 currentPacket = new iot::mqtt::client::packets::Suback(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
158 break;
159 case MQTT_UNSUBACK:
160 currentPacket = new iot::mqtt::client::packets::Unsuback(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
161 break;
162 case MQTT_PINGRESP:
163 currentPacket = new iot::mqtt::client::packets::Pingresp(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
164 break;
165 default:
166 currentPacket = nullptr;
167 break;
168 }
169
170 return currentPacket;
171 }
172
173 void Mqtt::deliverPacket(iot::mqtt::ControlPacketDeserializer* controlPacketDeserializer) {
174 static_cast<iot::mqtt::client::ControlPacketDeserializer*>(controlPacketDeserializer)->deliverPacket(this); // NOLINT
175 }
176
177 bool Mqtt::onSignal([[maybe_unused]] int sig) {
178 return false;
179 }
180
181 void Mqtt::onConnack([[maybe_unused]] const mqtt::packets::Connack& connack) {
182 }
183
184 void Mqtt::onSuback([[maybe_unused]] const mqtt::packets::Suback& suback) {
185 }
186
187 void Mqtt::onUnsuback([[maybe_unused]] const mqtt::packets::Unsuback& unsuback) {
188 }
189
190 void Mqtt::onPingresp([[maybe_unused]] const mqtt::packets::Pingresp& pingresp) {
191 }
192
193 void Mqtt::_onConnack(const iot::mqtt::client::packets::Connack& connack) {
194 LOG(INFO) << connectionName << " MQTT Client: Acknowledge Flag: " << static_cast<int>(connack.getAcknowledgeFlags());
195 LOG(INFO) << connectionName << " MQTT Client: Return code: " << static_cast<int>(connack.getReturnCode());
196 LOG(INFO) << connectionName << " MQTT Client: Session present: " << connack.getSessionPresent();
197
199 LOG(ERROR) << connectionName << " MQTT Client: Negative ack received";
200 } else {
202
204 [this]() {
206 },
208
209 onConnack(connack);
210 }
211 }
212
213 void Mqtt::_onPublish(const iot::mqtt::client::packets::Publish& publish) {
214 if (Super::_onPublish(publish)) {
215 onPublish(publish);
216 }
217 }
218
219 void Mqtt::_onSuback(const iot::mqtt::client::packets::Suback& suback) {
220 if (suback.getPacketIdentifier() == 0) {
221 LOG(ERROR) << connectionName << " MQTT Client: PackageIdentifier missing";
223 } else {
224 LOG(DEBUG) << connectionName << " MQTT Client: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
225 << suback.getPacketIdentifier();
226
227 std::stringstream ss;
228 std::list<uint8_t>::size_type i = 0;
229
230 for (const uint8_t returnCode : suback.getReturnCodes()) {
231 if (i != 0 && i % 8 == 0 && i != suback.getReturnCodes().size()) {
232 ss << std::endl;
233 ss << " ";
234 }
235 ++i;
236 ss << "0x" << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint16_t>(returnCode) << " "; // << " | ";
237 }
238
239 LOG(DEBUG) << connectionName << " MQTT Client: Return codes: " << ss.str();
240
241 onSuback(suback);
242 }
243 }
244
245 void Mqtt::_onUnsuback(const iot::mqtt::client::packets::Unsuback& unsuback) {
246 if (unsuback.getPacketIdentifier() == 0) {
247 LOG(ERROR) << connectionName << " MQTT Client: PacketIdentifier missing";
249 } else {
250 LOG(DEBUG) << connectionName << " MQTT Client: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
251 << unsuback.getPacketIdentifier();
252
253 onUnsuback(unsuback);
254 }
255 }
256
257 void Mqtt::_onPingresp(const iot::mqtt::client::packets::Pingresp& pingresp) {
258 onPingresp(pingresp);
259 }
260
261 void Mqtt::sendConnect(uint16_t keepAlive,
262 const std::string& clientId,
263 bool cleanSession,
264 const std::string& willTopic,
265 const std::string& willMessage,
266 uint8_t willQoS,
267 bool willRetain,
268 const std::string& username,
269 const std::string& password,
270 bool loopPrevention) { // Client
271 this->clientId = clientId;
272
273 LOG(INFO) << connectionName << " MQTT Client: CONNECT send: " << clientId;
274
276 clientId, keepAlive, cleanSession, willTopic, willMessage, willQoS, willRetain, username, password, loopPrevention));
277
278 this->keepAlive = keepAlive;
279 }
280
281 void Mqtt::sendSubscribe(const std::list<iot::mqtt::Topic>& topics) { // Client
283 }
284
285 void Mqtt::sendUnsubscribe(const std::list<std::string>& topics) { // Client
287 }
288
289 void Mqtt::sendPingreq() const { // Client
291 }
292
293 void Mqtt::sendDisconnect() const { // Client
295
297 }
298
299} // namespace iot::mqtt::client
void cancel()
Definition Timer.cpp:84
Timer & operator=(Timer &&timer) noexcept=default
static Timer intervalTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:65
uint8_t getType() const
uint32_t getRemainingLength() const
uint8_t getFlags() const
virtual void end(bool fatal=false)=0
MqttContext * mqttContext
Definition Mqtt.h:158
virtual void onPublish(const iot::mqtt::packets::Publish &publish)
Definition Mqtt.cpp:253
std::string connectionName
Definition Mqtt.h:142
Mqtt(const std::string &connectionName, const std::string &clientId)
Definition Mqtt.cpp:73
uint16_t getPacketIdentifier()
Definition Mqtt.cpp:398
bool _onPublish(const iot::mqtt::packets::Publish &publish)
Definition Mqtt.cpp:268
void send(const iot::mqtt::ControlPacket &controlPacket) const
Definition Mqtt.cpp:205
std::string clientId
Definition Mqtt.h:143
void initSession(Session *session, utils::Timeval keepAlive)
Definition Mqtt.cpp:174
virtual nlohmann::json toJson() const
Definition Session.cpp:59
void fromJson(const nlohmann::json &json)
Definition Session.cpp:89
virtual void deliverPacket(iot::mqtt::client::Mqtt *mqtt)=0
void _onPublish(const iot::mqtt::client::packets::Publish &publish)
Definition Mqtt.cpp:213
void sendConnect(uint16_t keepAlive, const std::string &clientId, 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=false)
Definition Mqtt.cpp:261
iot::mqtt::Session session
Definition Mqtt.h:141
std::string sessionStoreFileName
Definition Mqtt.h:140
Mqtt(const std::string &connectionName, const std::string &clientId, const std::string &sessionStoreFileName)
Definition Mqtt.cpp:78
void _onPingresp(const iot::mqtt::client::packets::Pingresp &pingresp)
Definition Mqtt.cpp:257
virtual void onConnack(const iot::mqtt::packets::Connack &connack)
Definition Mqtt.cpp:181
virtual void onPingresp(const iot::mqtt::packets::Pingresp &pingresp)
Definition Mqtt.cpp:190
void sendSubscribe(const std::list< Topic > &topics)
Definition Mqtt.cpp:281
iot::mqtt::ControlPacketDeserializer * createControlPacketDeserializer(iot::mqtt::FixedHeader &fixedHeader) final
Definition Mqtt.cpp:134
void _onSuback(const iot::mqtt::client::packets::Suback &suback)
Definition Mqtt.cpp:219
virtual void onUnsuback(const iot::mqtt::packets::Unsuback &unsuback)
Definition Mqtt.cpp:187
void _onConnack(const iot::mqtt::client::packets::Connack &connack)
Definition Mqtt.cpp:193
void _onUnsuback(const iot::mqtt::client::packets::Unsuback &unsuback)
Definition Mqtt.cpp:245
void sendUnsubscribe(const std::list< std::string > &topics)
Definition Mqtt.cpp:285
bool onSignal(int sig) override
Definition Mqtt.cpp:177
virtual void onSuback(const iot::mqtt::packets::Suback &suback)
Definition Mqtt.cpp:184
void deliverPacket(iot::mqtt::ControlPacketDeserializer *controlPacketDeserializer) override
Definition Mqtt.cpp:173
void sendPingreq() const
Definition Mqtt.cpp:289
void sendDisconnect() const
Definition Mqtt.cpp:293
core::timer::Timer pingTimer
Definition Mqtt.h:143
Connack(uint32_t remainingLength, uint8_t flags)
Definition Connack.cpp:52
Pingresp(uint32_t remainingLength, uint8_t flags)
Definition Pingresp.cpp:52
Puback(uint32_t remainingLength, uint8_t flags)
Definition Puback.cpp:52
Pubcomp(uint32_t remainingLength, uint8_t flags)
Definition Pubcomp.cpp:52
Publish(uint32_t remainingLength, uint8_t flags)
Definition Publish.cpp:52
Pubrec(uint32_t remainingLength, uint8_t flags)
Definition Pubrec.cpp:52
Pubrel(uint32_t remainingLength, uint8_t flags)
Definition Pubrel.cpp:52
Suback(uint32_t remainingLength, uint8_t flags)
Definition Suback.cpp:54
Unsuback(uint32_t remainingLength, uint8_t flags)
Definition Unsuback.cpp:52
uint8_t getReturnCode() const
Definition Connack.cpp:76
bool getSessionPresent() const
Definition Connack.cpp:80
uint8_t getAcknowledgeFlags() const
Definition Connack.cpp:72
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
uint16_t getPacketIdentifier() const
Definition Suback.cpp:71
const std::list< uint8_t > & getReturnCodes() const
Definition Suback.cpp:75
Subscribe(uint16_t packetIdentifier, const std::list< iot::mqtt::Topic > &topics)
Definition Subscribe.cpp:60
uint16_t getPacketIdentifier() const
Definition Unsuback.cpp:66
Unsubscribe(uint16_t packetIdentifier, const std::list< std::string > &topics)
#define MQTT_CONNACK
Definition Connack.h:57
#define MQTT_CONNACK_ACCEPT
Definition Connack.h:59
#define MQTT_PINGRESP
Definition Pingresp.h:55
#define MQTT_PUBACK
Definition Puback.h:57
#define MQTT_PUBCOMP
Definition Pubcomp.h:57
#define MQTT_PUBLISH
Definition Publish.h:58
#define MQTT_PUBREC
Definition Pubrec.h:57
#define MQTT_PUBREL
Definition Pubrel.h:57
#define MQTT_SUBACK
Definition Suback.h:58
#define MQTT_UNSUBACK
Definition Unsuback.h:57
Definition Timer.h:59