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, uint16_t keepAlive, const std::string &sessionStoreFileName)
 
 ~Mqtt () override
 
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
 
void sendSubscribe (const std::list< Topic > &topics) const
 
void sendUnsubscribe (const std::list< std::string > &topics) const
 
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) const
 
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 () const
 
void send (const iot::mqtt::ControlPacket &controlPacket) const
 
void printVP (const iot::mqtt::ControlPacket &packet) const
 
void printFixedHeader (const iot::mqtt::FixedHeader &fixedHeader) const
 

Protected Attributes

uint16_t keepAlive = 0
 
- Protected Attributes inherited from iot::mqtt::Mqtt
std::string connectionName
 
std::string clientId
 
MqttContextmqttContext = nullptr
 

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

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)
 

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,
uint16_t  keepAlive,
const std::string &  sessionStoreFileName 
)
explicit

Definition at line 78 of file Mqtt.cpp.

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
98 session.clear();
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 }
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:143
std::string sessionStoreFileName
Definition Mqtt.h:142
iot::mqtt::Mqtt Super
Definition Mqtt.h:86

References iot::mqtt::Session::clear(), iot::mqtt::Session::fromJson(), keepAlive, 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 113 of file Mqtt.cpp.

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

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 194 of file Mqtt.cpp.

194 {
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
199 if (connack.getReturnCode() != MQTT_CONNACK_ACCEPT) {
200 LOG(ERROR) << connectionName << " MQTT Client: Negative ack received";
201 } else {
203
205 [this]() {
206 sendPingreq();
207 },
208 keepAlive);
209
210 onConnack(connack);
211 }
212 }
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:182
void sendPingreq() const
Definition Mqtt.cpp:288
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 258 of file Mqtt.cpp.

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

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 214 of file Mqtt.cpp.

214 {
215 if (Super::_onPublish(publish)) {
216 onPublish(publish);
217 }
218 }
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 220 of file Mqtt.cpp.

220 {
221 if (suback.getPacketIdentifier() == 0) {
222 LOG(ERROR) << connectionName << " MQTT Client: PackageIdentifier missing";
223 mqttContext->end(true);
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 }
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:185
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 246 of file Mqtt.cpp.

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

135 {
136 iot::mqtt::ControlPacketDeserializer* currentPacket = nullptr;
137
138 switch (fixedHeader.getType()) {
139 case MQTT_CONNACK:
141 break;
142 case MQTT_PUBLISH:
144 break;
145 case MQTT_PUBACK:
147 break;
148 case MQTT_PUBREC:
150 break;
151 case MQTT_PUBREL:
153 break;
154 case MQTT_PUBCOMP:
156 break;
157 case MQTT_SUBACK:
159 break;
160 case MQTT_UNSUBACK:
162 break;
163 case MQTT_PINGRESP:
165 break;
166 default:
167 currentPacket = nullptr;
168 break;
169 }
170
171 return currentPacket;
172 }
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 174 of file Mqtt.cpp.

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

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 182 of file Mqtt.cpp.

182 {
183 }

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 191 of file Mqtt.cpp.

191 {
192 }

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 178 of file Mqtt.cpp.

178 {
179 return false;
180 }

◆ onSuback()

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

Definition at line 185 of file Mqtt.cpp.

185 {
186 }

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 188 of file Mqtt.cpp.

188 {
189 }

Referenced by _onUnsuback().

Here is the caller graph for this function:

◆ sendConnect()

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

269 { // Client
270 LOG(INFO) << connectionName << " MQTT Client: CONNECT send: " << clientId;
271
273 clientId, keepAlive, cleanSession, willTopic, willMessage, willQoS, willRetain, username, password, loopPrevention));
274 }
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 292 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 288 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) const

Definition at line 276 of file Mqtt.cpp.

276 { // Client
277 if (!topics.empty()) {
279 }
280 }
uint16_t getPacketIdentifier() const
Definition Mqtt.cpp:399

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) const

Definition at line 282 of file Mqtt.cpp.

282 { // Client
283 if (!topics.empty()) {
285 }
286 }

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 128 of file Mqtt.h.

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

Definition at line 131 of file Mqtt.h.

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

Definition at line 135 of file Mqtt.h.

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

Definition at line 133 of file Mqtt.h.

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

Definition at line 132 of file Mqtt.h.

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

Definition at line 134 of file Mqtt.h.

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

Definition at line 136 of file Mqtt.h.

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

Definition at line 129 of file Mqtt.h.

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

Definition at line 130 of file Mqtt.h.

Member Data Documentation

◆ keepAlive

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

Definition at line 139 of file Mqtt.h.

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

◆ pingTimer

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

Definition at line 145 of file Mqtt.h.

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

◆ session

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

Definition at line 143 of file Mqtt.h.

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

◆ sessionStoreFileName

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

Definition at line 142 of file Mqtt.h.

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


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