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