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, uint16_t keepAlive, const std::string& sessionStoreFileName)
79 : Super(connectionName, clientId)
80 , keepAlive(keepAlive)
81 , sessionStoreFileName(sessionStoreFileName) { // NOLINT
82 if (!sessionStoreFileName.empty()) {
83 std::ifstream sessionStoreFile(sessionStoreFileName);
84
85 if (sessionStoreFile.is_open()) {
86 try {
87 nlohmann::json sessionStoreJson;
88
89 sessionStoreFile >> sessionStoreJson;
90
91 session.fromJson(sessionStoreJson);
92
93 LOG(DEBUG) << connectionName << " MQTT Client: ... Persistent session data loaded successful";
94 } catch (const nlohmann::json::exception&) {
95 LOG(DEBUG) << connectionName << " MQTT Client: ... Starting with empty session: Session store '"
96 << sessionStoreFileName << "' empty or corrupted";
97
99 }
100
101 sessionStoreFile.close();
102 std::remove(sessionStoreFileName.data()); // NOLINT
103
104 LOG(INFO) << connectionName << " MQTT Client: Restoring saved session done";
105 } else {
106 PLOG(WARNING) << connectionName << " MQTT Client: ... Could not read session store '" << sessionStoreFileName << "'";
107 }
108 } else {
109 LOG(INFO) << connectionName << " MQTT Client: Session not reloaded: Session store filename empty";
110 }
111 }
112
113 Mqtt::~Mqtt() {
114 if (!sessionStoreFileName.empty()) {
115 const nlohmann::json sessionJson = session.toJson();
116
117 std::ofstream sessionStoreFile(sessionStoreFileName);
118
119 if (sessionStoreFile.is_open()) {
120 if (!sessionJson.empty()) {
121 sessionStoreFile << sessionJson;
122 }
123
124 sessionStoreFile.close();
125 } else {
126 PLOG(DEBUG) << connectionName << " MQTT Client: Could not write session store '" << sessionStoreFileName << "'";
127 }
128 } else {
129 LOG(INFO) << connectionName << " MQTT Client: Session not saved: Session store filename empty";
130 }
131
133 }
134
136 iot::mqtt::ControlPacketDeserializer* currentPacket = nullptr;
137
138 switch (fixedHeader.getType()) {
139 case MQTT_CONNACK:
140 currentPacket = new iot::mqtt::client::packets::Connack(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
141 break;
142 case MQTT_PUBLISH:
143 currentPacket = new iot::mqtt::client::packets::Publish(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
144 break;
145 case MQTT_PUBACK:
146 currentPacket = new iot::mqtt::client::packets::Puback(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
147 break;
148 case MQTT_PUBREC:
149 currentPacket = new iot::mqtt::client::packets::Pubrec(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
150 break;
151 case MQTT_PUBREL:
152 currentPacket = new iot::mqtt::client::packets::Pubrel(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
153 break;
154 case MQTT_PUBCOMP:
155 currentPacket = new iot::mqtt::client::packets::Pubcomp(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
156 break;
157 case MQTT_SUBACK:
158 currentPacket = new iot::mqtt::client::packets::Suback(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
159 break;
160 case MQTT_UNSUBACK:
161 currentPacket = new iot::mqtt::client::packets::Unsuback(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
162 break;
163 case MQTT_PINGRESP:
164 currentPacket = new iot::mqtt::client::packets::Pingresp(fixedHeader.getRemainingLength(), fixedHeader.getFlags());
165 break;
166 default:
167 currentPacket = nullptr;
168 break;
169 }
170
171 return currentPacket;
172 }
173
174 void Mqtt::deliverPacket(iot::mqtt::ControlPacketDeserializer* controlPacketDeserializer) {
175 static_cast<iot::mqtt::client::ControlPacketDeserializer*>(controlPacketDeserializer)->deliverPacket(this); // NOLINT
176 }
177
178 bool Mqtt::onSignal([[maybe_unused]] int sig) {
179 return false;
180 }
181
182 void Mqtt::onConnack([[maybe_unused]] const mqtt::packets::Connack& connack) {
183 }
184
185 void Mqtt::onSuback([[maybe_unused]] const mqtt::packets::Suback& suback) {
186 }
187
188 void Mqtt::onUnsuback([[maybe_unused]] const mqtt::packets::Unsuback& unsuback) {
189 }
190
191 void Mqtt::onPingresp([[maybe_unused]] const mqtt::packets::Pingresp& pingresp) {
192 }
193
194 void Mqtt::_onConnack(const iot::mqtt::client::packets::Connack& connack) {
195 LOG(INFO) << connectionName << " MQTT Client: Acknowledge Flag: " << static_cast<int>(connack.getAcknowledgeFlags());
196 LOG(INFO) << connectionName << " MQTT Client: Return code: " << static_cast<int>(connack.getReturnCode());
197 LOG(INFO) << connectionName << " MQTT Client: Session present: " << connack.getSessionPresent();
198
200 LOG(ERROR) << connectionName << " MQTT Client: Negative ack received";
201 } else {
203
205 [this]() {
207 },
209
210 onConnack(connack);
211 }
212 }
213
214 void Mqtt::_onPublish(const iot::mqtt::client::packets::Publish& publish) {
215 if (Super::_onPublish(publish)) {
216 onPublish(publish);
217 }
218 }
219
220 void Mqtt::_onSuback(const iot::mqtt::client::packets::Suback& suback) {
221 if (suback.getPacketIdentifier() == 0) {
222 LOG(ERROR) << connectionName << " MQTT Client: PackageIdentifier missing";
224 } else {
225 LOG(DEBUG) << connectionName << " MQTT Client: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
226 << suback.getPacketIdentifier() << std::dec;
227
228 std::stringstream ss;
229 std::list<uint8_t>::size_type i = 0;
230
231 for (const uint8_t returnCode : suback.getReturnCodes()) {
232 if (i != 0 && i % 8 == 0 && i != suback.getReturnCodes().size()) {
233 ss << std::endl;
234 ss << " ";
235 }
236 ++i;
237 ss << "0x" << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint16_t>(returnCode) << " "; // << " | ";
238 }
239
240 LOG(DEBUG) << connectionName << " MQTT Client: Return codes: " << ss.str();
241
242 onSuback(suback);
243 }
244 }
245
246 void Mqtt::_onUnsuback(const iot::mqtt::client::packets::Unsuback& unsuback) {
247 if (unsuback.getPacketIdentifier() == 0) {
248 LOG(ERROR) << connectionName << " MQTT Client: PacketIdentifier missing";
250 } else {
251 LOG(DEBUG) << connectionName << " MQTT Client: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
252 << unsuback.getPacketIdentifier() << std::dec;
253
254 onUnsuback(unsuback);
255 }
256 }
257
258 void Mqtt::_onPingresp(const iot::mqtt::client::packets::Pingresp& pingresp) {
259 onPingresp(pingresp);
260 }
261
262 void Mqtt::sendConnect(bool cleanSession,
263 const std::string& willTopic,
264 const std::string& willMessage,
265 uint8_t willQoS,
266 bool willRetain,
267 const std::string& username,
268 const std::string& password,
269 bool loopPrevention) const { // Client
270 LOG(INFO) << connectionName << " MQTT Client: CONNECT send: " << clientId;
271
273 clientId, keepAlive, cleanSession, willTopic, willMessage, willQoS, willRetain, username, password, loopPrevention));
274 }
275
276 void Mqtt::sendSubscribe(const std::list<iot::mqtt::Topic>& topics) const { // Client
277 if (!topics.empty()) {
279 }
280 }
281
282 void Mqtt::sendUnsubscribe(const std::list<std::string>& topics) const { // Client
283 if (!topics.empty()) {
285 }
286 }
287
288 void Mqtt::sendPingreq() const { // Client
290 }
291
292 void Mqtt::sendDisconnect() const { // Client
294
296 }
297
298} // 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
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
uint16_t getPacketIdentifier() const
Definition Mqtt.cpp:399
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
Mqtt(const std::string &connectionName, const std::string &clientId, uint16_t keepAlive, const std::string &sessionStoreFileName)
Definition Mqtt.cpp:78
void _onPublish(const iot::mqtt::client::packets::Publish &publish)
Definition Mqtt.cpp:214
void sendConnect(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) const
Definition Mqtt.cpp:262
iot::mqtt::Session session
Definition Mqtt.h:143
std::string sessionStoreFileName
Definition Mqtt.h:142
void sendUnsubscribe(const std::list< std::string > &topics) const
Definition Mqtt.cpp:282
void _onPingresp(const iot::mqtt::client::packets::Pingresp &pingresp)
Definition Mqtt.cpp:258
virtual void onConnack(const iot::mqtt::packets::Connack &connack)
Definition Mqtt.cpp:182
virtual void onPingresp(const iot::mqtt::packets::Pingresp &pingresp)
Definition Mqtt.cpp:191
void sendSubscribe(const std::list< Topic > &topics) const
Definition Mqtt.cpp:276
iot::mqtt::ControlPacketDeserializer * createControlPacketDeserializer(iot::mqtt::FixedHeader &fixedHeader) final
Definition Mqtt.cpp:135
void _onSuback(const iot::mqtt::client::packets::Suback &suback)
Definition Mqtt.cpp:220
virtual void onUnsuback(const iot::mqtt::packets::Unsuback &unsuback)
Definition Mqtt.cpp:188
void _onConnack(const iot::mqtt::client::packets::Connack &connack)
Definition Mqtt.cpp:194
void _onUnsuback(const iot::mqtt::client::packets::Unsuback &unsuback)
Definition Mqtt.cpp:246
bool onSignal(int sig) override
Definition Mqtt.cpp:178
virtual void onSuback(const iot::mqtt::packets::Suback &suback)
Definition Mqtt.cpp:185
void deliverPacket(iot::mqtt::ControlPacketDeserializer *controlPacketDeserializer) override
Definition Mqtt.cpp:174
void sendPingreq() const
Definition Mqtt.cpp:288
void sendDisconnect() const
Definition Mqtt.cpp:292
core::timer::Timer pingTimer
Definition Mqtt.h:145
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