SNode.C
Loading...
Searching...
No Matches
iot::mqtt::client::Mqtt Class Reference

#include <Mqtt.h>

Inheritance diagram for iot::mqtt::client::Mqtt:
Collaboration diagram for iot::mqtt::client::Mqtt:

Public Types

using Super = iot::mqtt::Mqtt
 

Public Member Functions

 Mqtt (const std::string &connectionName, const std::string &clientId, const std::string &sessionStoreFileName)
 
 ~Mqtt () override
 
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)
 
void sendSubscribe (const std::list< Topic > &topics)
 
void sendUnsubscribe (const std::list< std::string > &topics)
 
void sendPingreq () const
 
void sendDisconnect () const
 
- Public Member Functions inherited from iot::mqtt::Mqtt
 Mqtt (const std::string &connectionName)
 
 Mqtt (const std::string &connectionName, const std::string &clientId)
 
 Mqtt (Mqtt &&)=delete
 
 Mqtt (const Mqtt &)=delete
 
Mqttoperator= (Mqtt &&)=delete
 
Mqttoperator= (const Mqtt &)=delete
 
virtual void onConnected ()
 
virtual void onDisconnected ()
 
const std::string & getConnectionName () const
 
const MqttContextgetMqttContext () const
 
void sendPublish (const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
 
void sendPuback (uint16_t packetIdentifier) const
 
void sendPubrec (uint16_t packetIdentifier) const
 
void sendPubrel (uint16_t packetIdentifier) const
 
void sendPubcomp (uint16_t packetIdentifier) const
 

Protected Member Functions

bool onSignal (int sig) override
 
- Protected Member Functions inherited from iot::mqtt::Mqtt
void initSession (Session *session, utils::Timeval keepAlive)
 
virtual void onPublish (const iot::mqtt::packets::Publish &publish)
 
virtual void onPuback (const iot::mqtt::packets::Puback &puback)
 
virtual void onPubrec (const iot::mqtt::packets::Pubrec &pubrec)
 
virtual void onPubrel (const iot::mqtt::packets::Pubrel &pubrel)
 
virtual void onPubcomp (const iot::mqtt::packets::Pubcomp &pubcomp)
 
bool _onPublish (const iot::mqtt::packets::Publish &publish)
 
void _onPuback (const iot::mqtt::packets::Puback &puback)
 
void _onPubrec (const iot::mqtt::packets::Pubrec &pubrec)
 
void _onPubrel (const iot::mqtt::packets::Pubrel &pubrel)
 
void _onPubcomp (const iot::mqtt::packets::Pubcomp &pubcomp)
 
uint16_t getPacketIdentifier ()
 
void send (const iot::mqtt::ControlPacket &controlPacket) const
 
void printVP (const iot::mqtt::ControlPacket &packet) const
 
void printFixedHeader (const iot::mqtt::FixedHeader &fixedHeader) const
 

Private Member Functions

iot::mqtt::ControlPacketDeserializercreateControlPacketDeserializer (iot::mqtt::FixedHeader &fixedHeader) final
 
void deliverPacket (iot::mqtt::ControlPacketDeserializer *controlPacketDeserializer) override
 
virtual void onConnack (const iot::mqtt::packets::Connack &connack)
 
virtual void onSuback (const iot::mqtt::packets::Suback &suback)
 
virtual void onUnsuback (const iot::mqtt::packets::Unsuback &unsuback)
 
virtual void onPingresp (const iot::mqtt::packets::Pingresp &pingresp)
 
void _onConnack (const iot::mqtt::client::packets::Connack &connack)
 
void _onPublish (const iot::mqtt::client::packets::Publish &publish)
 
void _onSuback (const iot::mqtt::client::packets::Suback &suback)
 
void _onUnsuback (const iot::mqtt::client::packets::Unsuback &unsuback)
 
void _onPingresp (const iot::mqtt::client::packets::Pingresp &pingresp)
 

Private Attributes

uint16_t keepAlive = 0
 
std::string sessionStoreFileName
 
iot::mqtt::Session session
 
core::timer::Timer pingTimer
 

Friends

class iot::mqtt::client::packets::Connack
 
class iot::mqtt::client::packets::Suback
 
class iot::mqtt::client::packets::Unsuback
 
class iot::mqtt::client::packets::Pingresp
 
class iot::mqtt::client::packets::Publish
 
class iot::mqtt::client::packets::Pubcomp
 
class iot::mqtt::client::packets::Pubrec
 
class iot::mqtt::client::packets::Puback
 
class iot::mqtt::client::packets::Pubrel
 

Additional Inherited Members

- Static Public Member Functions inherited from iot::mqtt::Mqtt
static std::string toHexString (const std::vector< char > &data)
 
static std::string toHexString (const std::string &data)
 
- Protected Attributes inherited from iot::mqtt::Mqtt
std::string connectionName
 
std::string clientId
 
MqttContextmqttContext = nullptr
 

Detailed Description

Definition at line 84 of file Mqtt.h.

Member Typedef Documentation

◆ Super

Definition at line 86 of file Mqtt.h.

Constructor & Destructor Documentation

◆ Mqtt()

iot::mqtt::client::Mqtt::Mqtt ( const std::string &  connectionName,
const std::string &  clientId,
const std::string &  sessionStoreFileName 
)
explicit

Definition at line 78 of file Mqtt.cpp.

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
97 session.clear();
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 }
std::string connectionName
Definition Mqtt.h:142
std::string clientId
Definition Mqtt.h:143
void fromJson(const nlohmann::json &json)
Definition Session.cpp:89
iot::mqtt::Session session
Definition Mqtt.h:141
std::string sessionStoreFileName
Definition Mqtt.h:140
iot::mqtt::Mqtt Super
Definition Mqtt.h:86

References iot::mqtt::Session::clear(), iot::mqtt::Session::fromJson(), iot::mqtt::Mqtt::Mqtt(), session, and sessionStoreFileName.

Here is the call graph for this function:

◆ ~Mqtt()

iot::mqtt::client::Mqtt::~Mqtt ( )
overridevirtual

Reimplemented from iot::mqtt::Mqtt.

Definition at line 112 of file Mqtt.cpp.

112 {
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 }
void cancel()
Definition Timer.cpp:84
virtual nlohmann::json toJson() const
Definition Session.cpp:59
core::timer::Timer pingTimer
Definition Mqtt.h:143

References core::Timer::cancel(), iot::mqtt::Mqtt::connectionName, pingTimer, session, sessionStoreFileName, and iot::mqtt::Session::toJson().

Here is the call graph for this function:

Member Function Documentation

◆ _onConnack()

void iot::mqtt::client::Mqtt::_onConnack ( const iot::mqtt::client::packets::Connack connack)
private

Definition at line 193 of file Mqtt.cpp.

193 {
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
198 if (connack.getReturnCode() != MQTT_CONNACK_ACCEPT) {
199 LOG(ERROR) << connectionName << " MQTT Client: Negative ack received";
200 } else {
202
204 [this]() {
205 sendPingreq();
206 },
207 keepAlive);
208
209 onConnack(connack);
210 }
211 }
static Timer intervalTimer(const std::function< void(const std::function< void()> &)> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:61
void initSession(Session *session, utils::Timeval keepAlive)
Definition Mqtt.cpp:174
virtual void onConnack(const iot::mqtt::packets::Connack &connack)
Definition Mqtt.cpp:181
void sendPingreq() const
Definition Mqtt.cpp:289
uint8_t getReturnCode() const
Definition Connack.cpp:76
bool getSessionPresent() const
Definition Connack.cpp:80
uint8_t getAcknowledgeFlags() const
Definition Connack.cpp:72
#define MQTT_CONNACK_ACCEPT
Definition Connack.h:59

References iot::mqtt::Mqtt::connectionName, iot::mqtt::packets::Connack::getAcknowledgeFlags(), iot::mqtt::packets::Connack::getReturnCode(), iot::mqtt::packets::Connack::getSessionPresent(), iot::mqtt::Mqtt::initSession(), core::timer::Timer::intervalTimer(), keepAlive, onConnack(), core::timer::Timer::operator=(), pingTimer, sendPingreq(), and session.

Referenced by iot::mqtt::client::packets::Connack::deliverPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _onPingresp()

void iot::mqtt::client::Mqtt::_onPingresp ( const iot::mqtt::client::packets::Pingresp pingresp)
private

Definition at line 257 of file Mqtt.cpp.

257 {
258 onPingresp(pingresp);
259 }
virtual void onPingresp(const iot::mqtt::packets::Pingresp &pingresp)
Definition Mqtt.cpp:190

References onPingresp().

Referenced by iot::mqtt::client::packets::Pingresp::deliverPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _onPublish()

void iot::mqtt::client::Mqtt::_onPublish ( const iot::mqtt::client::packets::Publish publish)
private

Definition at line 213 of file Mqtt.cpp.

213 {
214 if (Super::_onPublish(publish)) {
215 onPublish(publish);
216 }
217 }
virtual void onPublish(const iot::mqtt::packets::Publish &publish)
Definition Mqtt.cpp:253
bool _onPublish(const iot::mqtt::packets::Publish &publish)
Definition Mqtt.cpp:268

References iot::mqtt::Mqtt::_onPublish(), and iot::mqtt::Mqtt::onPublish().

Referenced by iot::mqtt::client::packets::Publish::deliverPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _onSuback()

void iot::mqtt::client::Mqtt::_onSuback ( const iot::mqtt::client::packets::Suback suback)
private

Definition at line 219 of file Mqtt.cpp.

219 {
220 if (suback.getPacketIdentifier() == 0) {
221 LOG(ERROR) << connectionName << " MQTT Client: PackageIdentifier missing";
222 mqttContext->end(true);
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 }
virtual void end(bool fatal=false)=0
MqttContext * mqttContext
Definition Mqtt.h:158
virtual void onSuback(const iot::mqtt::packets::Suback &suback)
Definition Mqtt.cpp:184
uint16_t getPacketIdentifier() const
Definition Suback.cpp:71
const std::list< uint8_t > & getReturnCodes() const
Definition Suback.cpp:75

References iot::mqtt::Mqtt::connectionName, iot::mqtt::MqttContext::end(), iot::mqtt::packets::Suback::getPacketIdentifier(), iot::mqtt::packets::Suback::getReturnCodes(), iot::mqtt::Mqtt::mqttContext, and onSuback().

Referenced by iot::mqtt::client::packets::Suback::deliverPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _onUnsuback()

void iot::mqtt::client::Mqtt::_onUnsuback ( const iot::mqtt::client::packets::Unsuback unsuback)
private

Definition at line 245 of file Mqtt.cpp.

245 {
246 if (unsuback.getPacketIdentifier() == 0) {
247 LOG(ERROR) << connectionName << " MQTT Client: PacketIdentifier missing";
248 mqttContext->end(true);
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 }
virtual void onUnsuback(const iot::mqtt::packets::Unsuback &unsuback)
Definition Mqtt.cpp:187
uint16_t getPacketIdentifier() const
Definition Unsuback.cpp:66

References iot::mqtt::Mqtt::connectionName, iot::mqtt::MqttContext::end(), iot::mqtt::packets::Unsuback::getPacketIdentifier(), iot::mqtt::Mqtt::mqttContext, and onUnsuback().

Referenced by iot::mqtt::client::packets::Unsuback::deliverPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createControlPacketDeserializer()

iot::mqtt::ControlPacketDeserializer * iot::mqtt::client::Mqtt::createControlPacketDeserializer ( iot::mqtt::FixedHeader fixedHeader)
finalprivatevirtual

Implements iot::mqtt::Mqtt.

Definition at line 134 of file Mqtt.cpp.

134 {
135 iot::mqtt::ControlPacketDeserializer* currentPacket = nullptr;
136
137 switch (fixedHeader.getType()) {
138 case MQTT_CONNACK:
140 break;
141 case MQTT_PUBLISH:
143 break;
144 case MQTT_PUBACK:
146 break;
147 case MQTT_PUBREC:
149 break;
150 case MQTT_PUBREL:
152 break;
153 case MQTT_PUBCOMP:
155 break;
156 case MQTT_SUBACK:
158 break;
159 case MQTT_UNSUBACK:
161 break;
162 case MQTT_PINGRESP:
164 break;
165 default:
166 currentPacket = nullptr;
167 break;
168 }
169
170 return currentPacket;
171 }
uint8_t getType() const
uint32_t getRemainingLength() const
uint8_t getFlags() const
iot::mqtt::FixedHeader fixedHeader
Definition Mqtt.h:146
#define MQTT_CONNACK
Definition Connack.h:57
#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

References iot::mqtt::client::packets::Connack::Connack(), iot::mqtt::FixedHeader::getFlags(), iot::mqtt::FixedHeader::getRemainingLength(), iot::mqtt::FixedHeader::getType(), iot::mqtt::client::packets::Pingresp::Pingresp(), iot::mqtt::client::packets::Puback::Puback(), iot::mqtt::client::packets::Pubcomp::Pubcomp(), iot::mqtt::client::packets::Publish::Publish(), iot::mqtt::client::packets::Pubrec::Pubrec(), iot::mqtt::client::packets::Pubrel::Pubrel(), iot::mqtt::client::packets::Suback::Suback(), and iot::mqtt::client::packets::Unsuback::Unsuback().

Here is the call graph for this function:

◆ deliverPacket()

void iot::mqtt::client::Mqtt::deliverPacket ( iot::mqtt::ControlPacketDeserializer controlPacketDeserializer)
overrideprivatevirtual

Implements iot::mqtt::Mqtt.

Definition at line 173 of file Mqtt.cpp.

173 {
175 }
iot::mqtt::ControlPacketDeserializer * controlPacketDeserializer
Definition Mqtt.h:147
void deliverPacket(iot::mqtt::ControlPacketDeserializer *controlPacketDeserializer) override
Definition Mqtt.cpp:173

References iot::mqtt::client::ControlPacketDeserializer::deliverPacket().

Here is the call graph for this function:

◆ onConnack()

void iot::mqtt::client::Mqtt::onConnack ( const iot::mqtt::packets::Connack connack)
privatevirtual

Definition at line 181 of file Mqtt.cpp.

181 {
182 }

Referenced by _onConnack().

Here is the caller graph for this function:

◆ onPingresp()

void iot::mqtt::client::Mqtt::onPingresp ( const iot::mqtt::packets::Pingresp pingresp)
privatevirtual

Definition at line 190 of file Mqtt.cpp.

190 {
191 }

Referenced by _onPingresp().

Here is the caller graph for this function:

◆ onSignal()

bool iot::mqtt::client::Mqtt::onSignal ( int  sig)
overrideprotectedvirtual

Implements iot::mqtt::Mqtt.

Definition at line 177 of file Mqtt.cpp.

177 {
178 return false;
179 }

◆ onSuback()

void iot::mqtt::client::Mqtt::onSuback ( const iot::mqtt::packets::Suback suback)
privatevirtual

Definition at line 184 of file Mqtt.cpp.

184 {
185 }

Referenced by _onSuback().

Here is the caller graph for this function:

◆ onUnsuback()

void iot::mqtt::client::Mqtt::onUnsuback ( const iot::mqtt::packets::Unsuback unsuback)
privatevirtual

Definition at line 187 of file Mqtt.cpp.

187 {
188 }

Referenced by _onUnsuback().

Here is the caller graph for this function:

◆ sendConnect()

void iot::mqtt::client::Mqtt::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 at line 261 of file Mqtt.cpp.

270 { // 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 }
void send(const iot::mqtt::ControlPacket &controlPacket) const
Definition Mqtt.cpp:205

References iot::mqtt::Mqtt::clientId, iot::mqtt::packets::Connect::Connect(), iot::mqtt::Mqtt::connectionName, keepAlive, and iot::mqtt::Mqtt::send().

Here is the call graph for this function:

◆ sendDisconnect()

void iot::mqtt::client::Mqtt::sendDisconnect ( ) const

Definition at line 293 of file Mqtt.cpp.

References iot::mqtt::packets::Disconnect::Disconnect(), iot::mqtt::MqttContext::end(), iot::mqtt::Mqtt::mqttContext, and iot::mqtt::Mqtt::send().

Here is the call graph for this function:

◆ sendPingreq()

void iot::mqtt::client::Mqtt::sendPingreq ( ) const

Definition at line 289 of file Mqtt.cpp.

References iot::mqtt::packets::Pingreq::Pingreq(), and iot::mqtt::Mqtt::send().

Referenced by _onConnack().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendSubscribe()

void iot::mqtt::client::Mqtt::sendSubscribe ( const std::list< Topic > &  topics)

Definition at line 281 of file Mqtt.cpp.

281 { // Client
283 }
uint16_t getPacketIdentifier()
Definition Mqtt.cpp:398

References iot::mqtt::Mqtt::getPacketIdentifier(), iot::mqtt::Mqtt::send(), and iot::mqtt::packets::Subscribe::Subscribe().

Here is the call graph for this function:

◆ sendUnsubscribe()

void iot::mqtt::client::Mqtt::sendUnsubscribe ( const std::list< std::string > &  topics)

Definition at line 285 of file Mqtt.cpp.

References iot::mqtt::Mqtt::getPacketIdentifier(), iot::mqtt::Mqtt::send(), and iot::mqtt::packets::Unsubscribe::Unsubscribe().

Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ iot::mqtt::client::packets::Connack

Definition at line 127 of file Mqtt.h.

◆ iot::mqtt::client::packets::Pingresp

Definition at line 130 of file Mqtt.h.

◆ iot::mqtt::client::packets::Puback

Definition at line 134 of file Mqtt.h.

◆ iot::mqtt::client::packets::Pubcomp

Definition at line 132 of file Mqtt.h.

◆ iot::mqtt::client::packets::Publish

Definition at line 131 of file Mqtt.h.

◆ iot::mqtt::client::packets::Pubrec

Definition at line 133 of file Mqtt.h.

◆ iot::mqtt::client::packets::Pubrel

Definition at line 135 of file Mqtt.h.

◆ iot::mqtt::client::packets::Suback

Definition at line 128 of file Mqtt.h.

◆ iot::mqtt::client::packets::Unsuback

Definition at line 129 of file Mqtt.h.

Member Data Documentation

◆ keepAlive

uint16_t iot::mqtt::client::Mqtt::keepAlive = 0
private

Definition at line 138 of file Mqtt.h.

Referenced by _onConnack(), and sendConnect().

◆ pingTimer

core::timer::Timer iot::mqtt::client::Mqtt::pingTimer
private

Definition at line 143 of file Mqtt.h.

Referenced by _onConnack(), and ~Mqtt().

◆ session

iot::mqtt::Session iot::mqtt::client::Mqtt::session
private

Definition at line 141 of file Mqtt.h.

Referenced by _onConnack(), Mqtt(), and ~Mqtt().

◆ sessionStoreFileName

std::string iot::mqtt::client::Mqtt::sessionStoreFileName
private

Definition at line 140 of file Mqtt.h.

Referenced by Mqtt(), and ~Mqtt().


The documentation for this class was generated from the following files: