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

#include <Mqtt.h>

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

Public Member Functions

 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 ~Mqtt ()
virtual void onConnected ()
virtual void onDisconnected ()
virtual bool onSignal (int sig)=0
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

Static Public Member Functions

static std::string toHexString (const std::vector< char > &data)
static std::string toHexString (const std::string &data)

Protected Member Functions

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
virtual void deliverPublish (const iot::mqtt::packets::Publish &publish)=0
void printVP (const iot::mqtt::ControlPacket &packet) const
void printFixedHeader (const iot::mqtt::FixedHeader &fixedHeader) const

Protected Attributes

std::string connectionName
std::string clientId
MqttContextmqttContext = nullptr

Private Member Functions

std::size_t onReceivedFromPeer ()
void setMqttContext (MqttContext *mqttContext)
virtual iot::mqtt::ControlPacketDeserializercreateControlPacketDeserializer (iot::mqtt::FixedHeader &staticHeader) const =0
virtual void deliverPacket (iot::mqtt::ControlPacketDeserializer *controlPacketDeserializer)=0
void send (const std::vector< char > &data) const

Private Attributes

iot::mqtt::FixedHeader fixedHeader
iot::mqtt::ControlPacketDeserializercontrolPacketDeserializer = nullptr
uint16_t _packetIdentifier = 0
core::timer::Timer keepAliveTimer
int state = 0
Sessionsession = nullptr

Friends

class MqttContext

Detailed Description

Definition at line 74 of file Mqtt.h.

Constructor & Destructor Documentation

◆ Mqtt() [1/4]

iot::mqtt::Mqtt::Mqtt ( const std::string & connectionName)

Definition at line 69 of file Mqtt.cpp.

71 }
std::string connectionName
Definition Mqtt.h:144

References connectionName.

Referenced by iot::mqtt::server::Mqtt::Mqtt().

Here is the caller graph for this function:

◆ Mqtt() [2/4]

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

Definition at line 73 of file Mqtt.cpp.

76 }
std::string clientId
Definition Mqtt.h:145

References clientId, and connectionName.

Referenced by iot::mqtt::client::Mqtt::Mqtt().

Here is the caller graph for this function:

◆ Mqtt() [3/4]

iot::mqtt::Mqtt::Mqtt ( Mqtt && )
delete

◆ Mqtt() [4/4]

iot::mqtt::Mqtt::Mqtt ( const Mqtt & )
delete

◆ ~Mqtt()

iot::mqtt::Mqtt::~Mqtt ( )
virtual

Reimplemented in iot::mqtt::client::Mqtt, and iot::mqtt::server::Mqtt.

Definition at line 78 of file Mqtt.cpp.

78 {
79 if (controlPacketDeserializer != nullptr) {
82 }
83
84 keepAliveTimer.cancel();
85 }
core::timer::Timer keepAliveTimer
Definition Mqtt.h:153
iot::mqtt::ControlPacketDeserializer * controlPacketDeserializer
Definition Mqtt.h:149

References core::Timer::cancel(), controlPacketDeserializer, and keepAliveTimer.

Here is the call graph for this function:

Member Function Documentation

◆ _onPuback()

void iot::mqtt::Mqtt::_onPuback ( const iot::mqtt::packets::Puback & puback)
protected

Definition at line 323 of file Mqtt.cpp.

323 {
324 if (puback.getPacketIdentifier() == 0) {
325 LOG(ERROR) << connectionName << " MQTT: PackageIdentifier missing";
326 mqttContext->close();
327 } else {
328 LOG(DEBUG) << connectionName << " MQTT: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
329 << puback.getPacketIdentifier() << std::dec;
330
331 session->outgoingPublishMap.erase(puback.getPacketIdentifier());
332 }
333
334 onPuback(puback);
335 }
MqttContext * mqttContext
Definition Mqtt.h:160
virtual void onPuback(const iot::mqtt::packets::Puback &puback)
Definition Mqtt.cpp:254
Session * session
Definition Mqtt.h:157
uint16_t getPacketIdentifier() const
Definition Puback.cpp:66

References iot::mqtt::MqttContext::close(), connectionName, iot::mqtt::packets::Puback::getPacketIdentifier(), mqttContext, onPuback(), iot::mqtt::Session::outgoingPublishMap, and session.

Referenced by iot::mqtt::client::packets::Puback::deliverPacket(), and iot::mqtt::server::packets::Puback::deliverPacket().

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

◆ _onPubcomp()

void iot::mqtt::Mqtt::_onPubcomp ( const iot::mqtt::packets::Pubcomp & pubcomp)
protected

Definition at line 385 of file Mqtt.cpp.

385 {
386 if (pubcomp.getPacketIdentifier() == 0) {
387 LOG(ERROR) << connectionName << " MQTT: PackageIdentifier missing";
388 mqttContext->close();
389 } else {
390 LOG(DEBUG) << connectionName << " MQTT: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
391 << pubcomp.getPacketIdentifier() << std::dec;
392
393 session->outgoingPublishMap.erase(pubcomp.getPacketIdentifier());
394 session->pubrelPacketIdentifierSet.erase(pubcomp.getPacketIdentifier());
395 }
396
397 onPubcomp(pubcomp);
398 }
virtual void onPubcomp(const iot::mqtt::packets::Pubcomp &pubcomp)
Definition Mqtt.cpp:263
uint16_t getPacketIdentifier() const
Definition Pubcomp.cpp:66

References iot::mqtt::MqttContext::close(), connectionName, iot::mqtt::packets::Pubcomp::getPacketIdentifier(), mqttContext, onPubcomp(), iot::mqtt::Session::outgoingPublishMap, iot::mqtt::Session::pubrelPacketIdentifierSet, and session.

Referenced by iot::mqtt::client::packets::Pubcomp::deliverPacket(), and iot::mqtt::server::packets::Pubcomp::deliverPacket().

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

◆ _onPublish()

bool iot::mqtt::Mqtt::_onPublish ( const iot::mqtt::packets::Publish & publish)
protected

Definition at line 266 of file Mqtt.cpp.

266 {
267 bool deliver = true;
268
269 LOG(INFO) << connectionName << " MQTT: Topic: " << publish.getTopic();
270 LOG(INFO) << connectionName << " MQTT: Message:\n" << toHexString(publish.getMessage());
271 LOG(DEBUG) << connectionName << " MQTT: QoS: " << static_cast<uint16_t>(publish.getQoS());
272 LOG(DEBUG) << connectionName << " MQTT: PacketIdentifier: " << publish.getPacketIdentifier();
273 LOG(DEBUG) << connectionName << " MQTT: DUP: " << publish.getDup();
274 LOG(DEBUG) << connectionName << " MQTT: Retain: " << publish.getRetain();
275
276 if (publish.getQoS() > 2) {
277 LOG(ERROR) << connectionName << " MQTT: Received invalid QoS: " << publish.getQoS();
278 mqttContext->close();
279 deliver = false;
280 } else if (publish.getPacketIdentifier() == 0 && publish.getQoS() > 0) {
281 LOG(ERROR) << connectionName << " MQTT: Received QoS > 0 but no PackageIdentifier present";
282 mqttContext->close();
283 deliver = false;
284 } else if (publish.getQoS() == 0 && publish.getDup()) {
285 LOG(ERROR) << connectionName << " MQTT: Received QoS == 0 but dup is set";
286 mqttContext->close();
287 deliver = false;
288 } else {
289 switch (publish.getQoS()) {
290 case 1:
292
293 break;
294 case 2:
296
297 deliver = false;
298
299 const uint16_t pid = publish.getPacketIdentifier();
300
301 if (session->pubcompPacketIdentifierSet.contains(pid)) {
302 if (!publish.getDup()) {
303 session->pubcompPacketIdentifierSet.erase(pid);
304 } else {
305 LOG(WARNING) << connectionName << " MQTT: Duplicate QoS2 PUBLISH after PUBCOMP for PacketIdentifier: " << pid;
306 break;
307 }
308 }
309
310 if (session->incomingPublishMap.contains(pid)) {
311 LOG(INFO) << connectionName << " MQTT: Duplicate QoS2 PUBLISH suppressed for PacketIdentifier: " << pid;
312 } else {
313 session->incomingPublishMap.emplace(pid, publish);
314 }
315
316 break;
317 }
318 }
319
320 return deliver;
321 }
static std::string toHexString(const std::vector< char > &data)
Definition Mqtt.cpp:422
void sendPuback(uint16_t packetIdentifier) const
Definition Mqtt.cpp:235
void sendPubrec(uint16_t packetIdentifier) const
Definition Mqtt.cpp:239
uint8_t getQoS() const
Definition Publish.cpp:85
std::string getTopic() const
Definition Publish.cpp:93
std::string getMessage() const
Definition Publish.cpp:97
uint16_t getPacketIdentifier() const
Definition Publish.cpp:89

References iot::mqtt::MqttContext::close(), connectionName, iot::mqtt::packets::Publish::getDup(), iot::mqtt::packets::Publish::getMessage(), iot::mqtt::packets::Publish::getPacketIdentifier(), iot::mqtt::packets::Publish::getQoS(), iot::mqtt::packets::Publish::getRetain(), iot::mqtt::packets::Publish::getTopic(), iot::mqtt::Session::incomingPublishMap, mqttContext, iot::mqtt::Session::pubcompPacketIdentifierSet, sendPuback(), sendPubrec(), session, and toHexString().

Referenced by iot::mqtt::client::Mqtt::_onPublish(), and iot::mqtt::server::Mqtt::_onPublish().

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

◆ _onPubrec()

void iot::mqtt::Mqtt::_onPubrec ( const iot::mqtt::packets::Pubrec & pubrec)
protected

Definition at line 337 of file Mqtt.cpp.

337 {
338 if (pubrec.getPacketIdentifier() == 0) {
339 LOG(ERROR) << connectionName << " MQTT: PackageIdentifier missing";
340 mqttContext->close();
341 } else {
342 LOG(DEBUG) << connectionName << " MQTT: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
343 << pubrec.getPacketIdentifier() << std::dec;
344
345 session->outgoingPublishMap.erase(pubrec.getPacketIdentifier());
346 session->pubrelPacketIdentifierSet.insert(pubrec.getPacketIdentifier());
347
349 }
350
351 onPubrec(pubrec);
352 }
void sendPubrel(uint16_t packetIdentifier) const
Definition Mqtt.cpp:243
virtual void onPubrec(const iot::mqtt::packets::Pubrec &pubrec)
Definition Mqtt.cpp:257
uint16_t getPacketIdentifier() const
Definition Pubrec.cpp:66

References iot::mqtt::MqttContext::close(), connectionName, iot::mqtt::packets::Pubrec::getPacketIdentifier(), mqttContext, onPubrec(), iot::mqtt::Session::outgoingPublishMap, iot::mqtt::Session::pubrelPacketIdentifierSet, sendPubrel(), and session.

Referenced by iot::mqtt::client::packets::Pubrec::deliverPacket(), and iot::mqtt::server::packets::Pubrec::deliverPacket().

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

◆ _onPubrel()

void iot::mqtt::Mqtt::_onPubrel ( const iot::mqtt::packets::Pubrel & pubrel)
protected

Definition at line 354 of file Mqtt.cpp.

354 {
355 if (pubrel.getPacketIdentifier() == 0) {
356 LOG(ERROR) << connectionName << " MQTT: PackageIdentifier missing";
357 mqttContext->close();
358 } else {
359 LOG(DEBUG) << connectionName << " MQTT: PacketIdentifier: 0x" << std::hex << std::setfill('0') << std::setw(4)
360 << pubrel.getPacketIdentifier() << std::dec;
361
362 const uint16_t pid = pubrel.getPacketIdentifier();
363
364 if (session->incomingPublishMap.contains(pid)) {
365 LOG(INFO) << connectionName << " MQTT: QoS2 PUBREL received. Deliver publish: " << pid;
366
367 deliverPublish(session->incomingPublishMap[pid]);
368
369 session->incomingPublishMap.erase(pid);
370 session->pubcompPacketIdentifierSet.insert(pid);
371 } else if (session->pubcompPacketIdentifierSet.contains(pid)) {
372 LOG(INFO) << connectionName << " MQTT: Duplicate QoS2 PUBREL for completed PacketIdentifier: " << pid;
373 } else {
374 LOG(WARNING) << connectionName << " MQTT: QoS2 PUBREL received for unknown PacketIdentifier: " << pid;
375
376 session->pubcompPacketIdentifierSet.insert(pid);
377 }
378
379 sendPubcomp(pid);
380 }
381
382 onPubrel(pubrel);
383 }
virtual void deliverPublish(const iot::mqtt::packets::Publish &publish)=0
virtual void onPubrel(const iot::mqtt::packets::Pubrel &pubrel)
Definition Mqtt.cpp:260
void sendPubcomp(uint16_t packetIdentifier) const
Definition Mqtt.cpp:247
uint16_t getPacketIdentifier() const
Definition Pubrel.cpp:66

References iot::mqtt::MqttContext::close(), connectionName, deliverPublish(), iot::mqtt::packets::Pubrel::getPacketIdentifier(), iot::mqtt::Session::incomingPublishMap, mqttContext, onPubrel(), iot::mqtt::Session::pubcompPacketIdentifierSet, sendPubcomp(), and session.

Referenced by iot::mqtt::client::packets::Pubrel::deliverPacket(), and iot::mqtt::server::packets::Pubrel::deliverPacket().

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

◆ createControlPacketDeserializer()

virtual iot::mqtt::ControlPacketDeserializer * iot::mqtt::Mqtt::createControlPacketDeserializer ( iot::mqtt::FixedHeader & staticHeader) const
privatepure virtual

◆ deliverPacket()

virtual void iot::mqtt::Mqtt::deliverPacket ( iot::mqtt::ControlPacketDeserializer * controlPacketDeserializer)
privatepure virtual

◆ deliverPublish()

virtual void iot::mqtt::Mqtt::deliverPublish ( const iot::mqtt::packets::Publish & publish)
protectedpure virtual

Implemented in iot::mqtt::client::Mqtt, and iot::mqtt::server::Mqtt.

Referenced by _onPubrel().

Here is the caller graph for this function:

◆ getConnectionName()

const std::string & iot::mqtt::Mqtt::getConnectionName ( ) const

Definition at line 170 of file Mqtt.cpp.

170 {
171 return connectionName;
172 }

References connectionName.

◆ getMqttContext()

const MqttContext * iot::mqtt::Mqtt::getMqttContext ( ) const

Definition at line 91 of file Mqtt.cpp.

91 {
92 return mqttContext;
93 }

References mqttContext.

◆ getPacketIdentifier()

uint16_t iot::mqtt::Mqtt::getPacketIdentifier ( ) const
protected

Definition at line 431 of file Mqtt.cpp.

431 {
432 do {
434 if (_packetIdentifier == 0) {
436 }
437 } while (session->outgoingPublishMap.contains(_packetIdentifier) || session->pubrelPacketIdentifierSet.contains(_packetIdentifier));
438
439 return _packetIdentifier;
440 }
uint16_t _packetIdentifier
Definition Mqtt.h:151

References _packetIdentifier, iot::mqtt::Session::outgoingPublishMap, iot::mqtt::Session::pubrelPacketIdentifierSet, and session.

Referenced by sendPublish(), iot::mqtt::client::Mqtt::sendSubscribe(), and iot::mqtt::client::Mqtt::sendUnsubscribe().

Here is the caller graph for this function:

◆ initSession()

void iot::mqtt::Mqtt::initSession ( Session * session,
utils::Timeval keepAlive )
protected

Definition at line 174 of file Mqtt.cpp.

174 {
175 this->session = session;
176
177 for (const auto& [packetIdentifier, publish] : session->outgoingPublishMap) {
178 LOG(INFO) << connectionName << " MQTT: PUBLISH Resend";
179
180 send(publish);
181 }
182
183 for (const uint16_t packetIdentifier : session->pubrelPacketIdentifierSet) {
184 LOG(INFO) << connectionName << " MQTT: PUBREL Resend";
185
186 sendPubrel(packetIdentifier);
187 }
188
189 if (keepAlive > 0) {
190 keepAlive *= 1.5;
191
192 LOG(INFO) << connectionName << " MQTT: Keep alive initialized with: " << keepAlive;
193
195 [this, keepAlive]() {
196 LOG(ERROR) << connectionName << " MQTT: Keep-alive timer expired. Interval was: " << keepAlive;
197 mqttContext->close();
198 },
199 keepAlive);
200 }
201
202 mqttContext->getSocketConnection()->setTimeout(0);
203 }
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
void send(const iot::mqtt::ControlPacket &controlPacket) const
Definition Mqtt.cpp:205

References iot::mqtt::MqttContext::close(), connectionName, iot::mqtt::MqttContext::getSocketConnection(), keepAliveTimer, mqttContext, utils::Timeval::operator*=(), core::timer::Timer::operator=(), utils::Timeval::operator>(), iot::mqtt::Session::outgoingPublishMap, iot::mqtt::Session::pubrelPacketIdentifierSet, send(), sendPubrel(), session, core::socket::stream::SocketConnection::setTimeout(), and core::timer::Timer::singleshotTimer().

Referenced by iot::mqtt::client::Mqtt::_onConnack(), and iot::mqtt::server::Mqtt::initSession().

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

◆ onConnected()

void iot::mqtt::Mqtt::onConnected ( )
virtual

Definition at line 95 of file Mqtt.cpp.

95 {
96 LOG(INFO) << "MQTT: Connected";
97 }

Referenced by iot::mqtt::MqttContext::onConnected().

Here is the caller graph for this function:

◆ onDisconnected()

void iot::mqtt::Mqtt::onDisconnected ( )
virtual

Definition at line 166 of file Mqtt.cpp.

166 {
167 LOG(INFO) << connectionName << " MQTT: Disconnected";
168 }

References connectionName.

Referenced by iot::mqtt::MqttContext::onDisconnected().

Here is the caller graph for this function:

◆ onPuback()

void iot::mqtt::Mqtt::onPuback ( const iot::mqtt::packets::Puback & puback)
protectedvirtual

Definition at line 254 of file Mqtt.cpp.

254 {
255 }

Referenced by _onPuback().

Here is the caller graph for this function:

◆ onPubcomp()

void iot::mqtt::Mqtt::onPubcomp ( const iot::mqtt::packets::Pubcomp & pubcomp)
protectedvirtual

Definition at line 263 of file Mqtt.cpp.

263 {
264 }

Referenced by _onPubcomp().

Here is the caller graph for this function:

◆ onPublish()

void iot::mqtt::Mqtt::onPublish ( const iot::mqtt::packets::Publish & publish)
protectedvirtual

Definition at line 251 of file Mqtt.cpp.

251 {
252 }

Referenced by iot::mqtt::client::Mqtt::deliverPublish(), and iot::mqtt::server::Mqtt::deliverPublish().

Here is the caller graph for this function:

◆ onPubrec()

void iot::mqtt::Mqtt::onPubrec ( const iot::mqtt::packets::Pubrec & pubrec)
protectedvirtual

Definition at line 257 of file Mqtt.cpp.

257 {
258 }

Referenced by _onPubrec().

Here is the caller graph for this function:

◆ onPubrel()

void iot::mqtt::Mqtt::onPubrel ( const iot::mqtt::packets::Pubrel & pubrel)
protectedvirtual

Definition at line 260 of file Mqtt.cpp.

260 {
261 }

Referenced by _onPubrel().

Here is the caller graph for this function:

◆ onReceivedFromPeer()

std::size_t iot::mqtt::Mqtt::onReceivedFromPeer ( )
private

Definition at line 99 of file Mqtt.cpp.

99 {
100 std::size_t consumed = 0;
101
102 switch (state) {
103 case 0:
104 consumed = fixedHeader.deserialize(mqttContext);
105
106 if (!fixedHeader.isComplete()) {
107 break;
108 }
109 if (fixedHeader.isError()) {
110 mqttContext->close();
111 break;
112 }
114
116
117 fixedHeader.reset();
118
119 if (controlPacketDeserializer == nullptr) {
120 LOG(DEBUG) << connectionName << " MQTT: Received packet-type is unavailable ... closing connection";
121
122 mqttContext->close();
123 break;
124 }
125 if (controlPacketDeserializer->isError()) {
126 LOG(DEBUG) << connectionName << " MQTT: Fixed header has error ... closing connection";
127
130
131 mqttContext->close();
132 break;
133 }
134
135 state++;
136
137 [[fallthrough]];
138 case 1:
139 consumed += controlPacketDeserializer->deserialize(mqttContext);
140
141 if (controlPacketDeserializer->isError()) {
142 LOG(DEBUG) << connectionName << " MQTT: Control packet has error ... closing connection";
143 mqttContext->close();
144
147
148 state = 0;
149 } else if (controlPacketDeserializer->isComplete()) {
151
154
155 state = 0;
156
157 keepAliveTimer.restart();
158 }
159
160 break;
161 }
162
163 return consumed;
164 }
iot::mqtt::FixedHeader fixedHeader
Definition Mqtt.h:148
virtual iot::mqtt::ControlPacketDeserializer * createControlPacketDeserializer(iot::mqtt::FixedHeader &staticHeader) const =0
virtual void deliverPacket(iot::mqtt::ControlPacketDeserializer *controlPacketDeserializer)=0
void printFixedHeader(const iot::mqtt::FixedHeader &fixedHeader) const
Definition Mqtt.cpp:409

References iot::mqtt::FixedHeader::deserialize(), fixedHeader, mqttContext, and state.

Referenced by iot::mqtt::MqttContext::onReceivedFromPeer().

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

◆ onSignal()

virtual bool iot::mqtt::Mqtt::onSignal ( int sig)
pure virtual

Implemented in iot::mqtt::client::Mqtt, and iot::mqtt::server::Mqtt.

Referenced by iot::mqtt::MqttContext::onSignal().

Here is the caller graph for this function:

◆ operator=() [1/2]

Mqtt & iot::mqtt::Mqtt::operator= ( const Mqtt & )
delete

◆ operator=() [2/2]

Mqtt & iot::mqtt::Mqtt::operator= ( Mqtt && )
delete

◆ printFixedHeader()

void iot::mqtt::Mqtt::printFixedHeader ( const iot::mqtt::FixedHeader & fixedHeader) const
protected

Definition at line 409 of file Mqtt.cpp.

409 {
410 LOG(INFO) << connectionName << " MQTT: ====================================";
411
412 LOG(TRACE) << connectionName << " MQTT: Received data (fixed header):\n" << toHexString(fixedHeader.serialize());
413
414 LOG(DEBUG) << connectionName << " MQTT: Fixed Header: PacketType: 0x" << std::hex << std::setfill('0') << std::setw(2)
415 << static_cast<uint16_t>(fixedHeader.getType()) << " (" << iot::mqtt::mqttPackageName[fixedHeader.getType()] << ")"
416 << std::dec;
417 LOG(DEBUG) << connectionName << " MQTT: PacketFlags: 0x" << std::hex << std::setfill('0') << std::setw(2)
418 << static_cast<uint16_t>(fixedHeader.getFlags()) << std::dec;
419 LOG(DEBUG) << connectionName << " MQTT: RemainingLength: " << fixedHeader.getRemainingLength();
420 }
const std::vector< std::string > mqttPackageName

References connectionName, iot::mqtt::FixedHeader::getFlags(), iot::mqtt::FixedHeader::getRemainingLength(), iot::mqtt::FixedHeader::getType(), iot::mqtt::mqttPackageName, iot::mqtt::FixedHeader::serialize(), and toHexString().

Here is the call graph for this function:

◆ printVP()

void iot::mqtt::Mqtt::printVP ( const iot::mqtt::ControlPacket & packet) const
protected

Definition at line 400 of file Mqtt.cpp.

400 {
401 LOG(INFO) << connectionName << " MQTT: " << packet.getName() << " received: " << clientId;
402
403 const std::string hexString = toHexString(packet.serializeVP());
404 if (!hexString.empty()) {
405 LOG(TRACE) << connectionName << " MQTT: Received data (variable header and payload):\n" << hexString;
406 }
407 }
const std::string & getName() const
virtual std::vector< char > serializeVP() const =0

References clientId, connectionName, iot::mqtt::ControlPacket::getName(), iot::mqtt::ControlPacket::serializeVP(), and toHexString().

Referenced by iot::mqtt::client::packets::Connack::deliverPacket(), iot::mqtt::client::packets::Pingresp::deliverPacket(), iot::mqtt::client::packets::Puback::deliverPacket(), iot::mqtt::client::packets::Pubcomp::deliverPacket(), iot::mqtt::client::packets::Publish::deliverPacket(), iot::mqtt::client::packets::Pubrec::deliverPacket(), iot::mqtt::client::packets::Pubrel::deliverPacket(), iot::mqtt::client::packets::Suback::deliverPacket(), iot::mqtt::client::packets::Unsuback::deliverPacket(), iot::mqtt::server::packets::Connect::deliverPacket(), iot::mqtt::server::packets::Disconnect::deliverPacket(), iot::mqtt::server::packets::Pingreq::deliverPacket(), iot::mqtt::server::packets::Puback::deliverPacket(), iot::mqtt::server::packets::Pubcomp::deliverPacket(), iot::mqtt::server::packets::Publish::deliverPacket(), iot::mqtt::server::packets::Pubrec::deliverPacket(), iot::mqtt::server::packets::Pubrel::deliverPacket(), iot::mqtt::server::packets::Subscribe::deliverPacket(), and iot::mqtt::server::packets::Unsubscribe::deliverPacket().

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

◆ send() [1/2]

void iot::mqtt::Mqtt::send ( const iot::mqtt::ControlPacket & controlPacket) const
protected

Definition at line 205 of file Mqtt.cpp.

205 {
206 LOG(INFO) << connectionName << " MQTT: " << controlPacket.getName() << " send: " << clientId;
207
208 send(controlPacket.serialize());
209 }
std::vector< char > serialize() const

References clientId, connectionName, iot::mqtt::ControlPacket::getName(), send(), and iot::mqtt::ControlPacket::serialize().

Referenced by initSession(), iot::mqtt::server::Mqtt::sendConnack(), iot::mqtt::client::Mqtt::sendConnect(), iot::mqtt::client::Mqtt::sendDisconnect(), iot::mqtt::client::Mqtt::sendPingreq(), iot::mqtt::server::Mqtt::sendPingresp(), sendPuback(), sendPubcomp(), sendPublish(), sendPubrec(), sendPubrel(), iot::mqtt::server::Mqtt::sendSuback(), iot::mqtt::client::Mqtt::sendSubscribe(), iot::mqtt::server::Mqtt::sendUnsuback(), and iot::mqtt::client::Mqtt::sendUnsubscribe().

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

◆ send() [2/2]

void iot::mqtt::Mqtt::send ( const std::vector< char > & data) const
private

Definition at line 211 of file Mqtt.cpp.

211 {
212 LOG(TRACE) << connectionName << " MQTT: Send data (full message):\n" << toHexString(data);
213
214 mqttContext->send(data.data(), data.size());
215 }

References connectionName, mqttContext, iot::mqtt::MqttContext::send(), and toHexString().

Referenced by send().

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

◆ sendPuback()

void iot::mqtt::Mqtt::sendPuback ( uint16_t packetIdentifier) const

Definition at line 235 of file Mqtt.cpp.

235 { // Server & Client
236 send(iot::mqtt::packets::Puback(packetIdentifier));
237 }

References iot::mqtt::packets::Puback::Puback(), and send().

Referenced by _onPublish().

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

◆ sendPubcomp()

void iot::mqtt::Mqtt::sendPubcomp ( uint16_t packetIdentifier) const

Definition at line 247 of file Mqtt.cpp.

247 { // Server & Client
248 send(iot::mqtt::packets::Pubcomp(packetIdentifier));
249 }

References iot::mqtt::packets::Pubcomp::Pubcomp(), and send().

Referenced by _onPubrel().

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

◆ sendPublish()

void iot::mqtt::Mqtt::sendPublish ( const std::string & topic,
const std::string & message,
uint8_t qoS,
bool retain ) const

Definition at line 217 of file Mqtt.cpp.

217 {
218 const uint16_t packetIdentifier = qoS != 0 ? getPacketIdentifier() : 0;
219
220 send(iot::mqtt::packets::Publish(packetIdentifier, topic, message, qoS, false, retain));
221
222 LOG(INFO) << connectionName << " MQTT: Topic: " << topic;
223 LOG(INFO) << connectionName << " MQTT: Message:\n" << toHexString(message);
224 LOG(DEBUG) << connectionName << " MQTT: QoS: " << static_cast<uint16_t>(qoS);
225 LOG(DEBUG) << connectionName << " MQTT: PacketIdentifier: " << packetIdentifier;
226 LOG(DEBUG) << connectionName << " MQTT: DUP: " << false;
227 LOG(DEBUG) << connectionName << " MQTT: Retain: " << retain;
228
229 if (qoS >= 1) {
230 session->outgoingPublishMap.insert_or_assign(packetIdentifier,
231 iot::mqtt::packets::Publish(packetIdentifier, topic, message, qoS, true, retain));
232 }
233 }
uint16_t getPacketIdentifier() const
Definition Mqtt.cpp:431

References connectionName, getPacketIdentifier(), iot::mqtt::Session::outgoingPublishMap, iot::mqtt::packets::Publish::Publish(), send(), session, and toHexString().

Referenced by iot::mqtt::server::broker::Session::sendPublish().

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

◆ sendPubrec()

void iot::mqtt::Mqtt::sendPubrec ( uint16_t packetIdentifier) const

Definition at line 239 of file Mqtt.cpp.

239 { // Server & Client
240 send(iot::mqtt::packets::Pubrec(packetIdentifier));
241 }

References iot::mqtt::packets::Pubrec::Pubrec(), and send().

Referenced by _onPublish().

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

◆ sendPubrel()

void iot::mqtt::Mqtt::sendPubrel ( uint16_t packetIdentifier) const

Definition at line 243 of file Mqtt.cpp.

243 { // Server & Client
244 send(iot::mqtt::packets::Pubrel(packetIdentifier));
245 }

References iot::mqtt::packets::Pubrel::Pubrel(), and send().

Referenced by _onPubrec(), and initSession().

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

◆ setMqttContext()

void iot::mqtt::Mqtt::setMqttContext ( MqttContext * mqttContext)
private

Definition at line 87 of file Mqtt.cpp.

87 {
89 }

References mqttContext.

Referenced by iot::mqtt::MqttContext::MqttContext().

Here is the caller graph for this function:

◆ toHexString() [1/2]

std::string iot::mqtt::Mqtt::toHexString ( const std::string & data)
static

Definition at line 427 of file Mqtt.cpp.

427 {
428 return toHexString(std::vector<char>(data.begin(), data.end()));
429 }

References toHexString().

Referenced by _onPublish(), iot::mqtt::server::broker::RetainTree::TopicLevel::appear(), iot::mqtt::server::broker::RetainTree::TopicLevel::appear(), iot::mqtt::server::broker::SubscriptionTree::TopicLevel::publish(), iot::mqtt::server::broker::RetainTree::TopicLevel::retain(), sendPublish(), and iot::mqtt::server::broker::Session::sendPublish().

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

◆ toHexString() [2/2]

std::string iot::mqtt::Mqtt::toHexString ( const std::vector< char > & data)
static

Definition at line 422 of file Mqtt.cpp.

422 {
423 const std::string hexDump = utils::hexDump(data, 32);
424 return !hexDump.empty() ? std::string(32, ' ').append(hexDump) : "";
425 }
std::string hexDump(const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:58

References utils::hexDump().

Referenced by printFixedHeader(), printVP(), send(), and toHexString().

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

◆ MqttContext

friend class MqttContext
friend

Definition at line 162 of file Mqtt.h.

Member Data Documentation

◆ _packetIdentifier

uint16_t iot::mqtt::Mqtt::_packetIdentifier = 0
mutableprivate

Definition at line 151 of file Mqtt.h.

Referenced by getPacketIdentifier().

◆ clientId

◆ connectionName

◆ controlPacketDeserializer

iot::mqtt::ControlPacketDeserializer* iot::mqtt::Mqtt::controlPacketDeserializer = nullptr
private

Definition at line 149 of file Mqtt.h.

Referenced by ~Mqtt().

◆ fixedHeader

iot::mqtt::FixedHeader iot::mqtt::Mqtt::fixedHeader
private

Definition at line 148 of file Mqtt.h.

Referenced by onReceivedFromPeer().

◆ keepAliveTimer

core::timer::Timer iot::mqtt::Mqtt::keepAliveTimer
private

Definition at line 153 of file Mqtt.h.

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

◆ mqttContext

◆ session

Session* iot::mqtt::Mqtt::session = nullptr
private

◆ state

int iot::mqtt::Mqtt::state = 0
private

Definition at line 155 of file Mqtt.h.

Referenced by onReceivedFromPeer().


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