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 distributePublish (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 68 of file Mqtt.cpp.

70 }
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 72 of file Mqtt.cpp.

75 }
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 77 of file Mqtt.cpp.

77 {
78 if (controlPacketDeserializer != nullptr) {
81 }
82
83 keepAliveTimer.cancel();
84 }
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 322 of file Mqtt.cpp.

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

References iot::mqtt::MqttContext::close(), connectionName, logger::DEBUG, logger::ERROR, iot::mqtt::packets::Puback::getPacketIdentifier(), logger::LogMessage::LogMessage(), 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 384 of file Mqtt.cpp.

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

References iot::mqtt::MqttContext::close(), connectionName, logger::DEBUG, logger::ERROR, iot::mqtt::packets::Pubcomp::getPacketIdentifier(), logger::LogMessage::LogMessage(), 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 265 of file Mqtt.cpp.

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

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

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

References iot::mqtt::MqttContext::close(), connectionName, logger::DEBUG, logger::ERROR, iot::mqtt::packets::Pubrec::getPacketIdentifier(), logger::LogMessage::LogMessage(), 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 353 of file Mqtt.cpp.

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

References iot::mqtt::MqttContext::close(), connectionName, logger::DEBUG, distributePublish(), logger::ERROR, iot::mqtt::packets::Pubrel::getPacketIdentifier(), iot::mqtt::Session::incomingPublishMap, logger::INFO, logger::LogMessage::LogMessage(), mqttContext, onPubrel(), iot::mqtt::Session::pubcompPacketIdentifierSet, sendPubcomp(), session, and logger::WARNING.

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

◆ distributePublish()

virtual void iot::mqtt::Mqtt::distributePublish ( 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 169 of file Mqtt.cpp.

169 {
170 return connectionName;
171 }

References connectionName.

◆ getMqttContext()

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

Definition at line 90 of file Mqtt.cpp.

90 {
91 return mqttContext;
92 }

References mqttContext.

◆ getPacketIdentifier()

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

Definition at line 430 of file Mqtt.cpp.

430 {
431 do {
433 if (_packetIdentifier == 0) {
435 }
436 } while (session->outgoingPublishMap.contains(_packetIdentifier) || session->pubrelPacketIdentifierSet.contains(_packetIdentifier));
437
438 return _packetIdentifier;
439 }
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 173 of file Mqtt.cpp.

173 {
174 this->session = session;
175
176 for (const auto& [packetIdentifier, publish] : session->outgoingPublishMap) {
177 LOG(INFO) << connectionName << " MQTT: PUBLISH Resend";
178
179 send(publish);
180 }
181
182 for (const uint16_t packetIdentifier : session->pubrelPacketIdentifierSet) {
183 LOG(INFO) << connectionName << " MQTT: PUBREL Resend";
184
185 sendPubrel(packetIdentifier);
186 }
187
188 if (keepAlive > 0) {
189 keepAlive *= 1.5;
190
191 LOG(INFO) << connectionName << " MQTT: Keep alive initialized with: " << keepAlive;
192
194 [this, keepAlive]() {
195 LOG(ERROR) << connectionName << " MQTT: Keep-alive timer expired. Interval was: " << keepAlive;
196 mqttContext->close();
197 },
198 keepAlive);
199 }
200
201 mqttContext->getSocketConnection()->setTimeout(0);
202 }
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:204

References iot::mqtt::MqttContext::close(), connectionName, logger::ERROR, iot::mqtt::MqttContext::getSocketConnection(), logger::INFO, keepAliveTimer, logger::LogMessage::LogMessage(), 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 94 of file Mqtt.cpp.

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

References logger::INFO, and logger::LogMessage::LogMessage().

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

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

◆ onDisconnected()

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

Definition at line 165 of file Mqtt.cpp.

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

References connectionName, logger::INFO, and logger::LogMessage::LogMessage().

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

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

◆ onPuback()

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

Definition at line 253 of file Mqtt.cpp.

253 {
254 }

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

262 {
263 }

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

250 {
251 }

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

Here is the caller graph for this function:

◆ onPubrec()

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

Definition at line 256 of file Mqtt.cpp.

256 {
257 }

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

259 {
260 }

Referenced by _onPubrel().

Here is the caller graph for this function:

◆ onReceivedFromPeer()

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

Definition at line 98 of file Mqtt.cpp.

98 {
99 std::size_t consumed = 0;
100
101 switch (state) {
102 case 0:
103 consumed = fixedHeader.deserialize(mqttContext);
104
105 if (!fixedHeader.isComplete()) {
106 break;
107 }
108 if (fixedHeader.isError()) {
109 mqttContext->close();
110 break;
111 }
113
115
116 fixedHeader.reset();
117
118 if (controlPacketDeserializer == nullptr) {
119 LOG(DEBUG) << connectionName << " MQTT: Received packet-type is unavailable ... closing connection";
120
121 mqttContext->close();
122 break;
123 }
124 if (controlPacketDeserializer->isError()) {
125 LOG(DEBUG) << connectionName << " MQTT: Fixed header has error ... closing connection";
126
129
130 mqttContext->close();
131 break;
132 }
133
134 state++;
135
136 [[fallthrough]];
137 case 1:
138 consumed += controlPacketDeserializer->deserialize(mqttContext);
139
140 if (controlPacketDeserializer->isError()) {
141 LOG(DEBUG) << connectionName << " MQTT: Control packet has error ... closing connection";
142 mqttContext->close();
143
146
147 state = 0;
148 } else if (controlPacketDeserializer->isComplete()) {
150
153
154 state = 0;
155
156 keepAliveTimer.restart();
157 }
158
159 break;
160 }
161
162 return consumed;
163 }
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:408

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

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

References connectionName, logger::DEBUG, iot::mqtt::FixedHeader::getFlags(), iot::mqtt::FixedHeader::getRemainingLength(), iot::mqtt::FixedHeader::getType(), logger::INFO, logger::LogMessage::LogMessage(), iot::mqtt::mqttPackageName, iot::mqtt::FixedHeader::serialize(), toHexString(), and logger::TRACE.

Here is the call graph for this function:

◆ printVP()

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

Definition at line 399 of file Mqtt.cpp.

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

References clientId, connectionName, iot::mqtt::ControlPacket::getName(), logger::INFO, logger::LogMessage::LogMessage(), iot::mqtt::ControlPacket::serializeVP(), toHexString(), and logger::TRACE.

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

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

References clientId, connectionName, iot::mqtt::ControlPacket::getName(), logger::INFO, logger::LogMessage::LogMessage(), 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 210 of file Mqtt.cpp.

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

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

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

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

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

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

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

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

References connectionName, logger::DEBUG, getPacketIdentifier(), logger::INFO, logger::LogMessage::LogMessage(), 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 238 of file Mqtt.cpp.

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

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

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

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

86 {
88 }

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

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

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

421 {
422 const std::string hexDump = utils::hexDump(data, 32);
423 return !hexDump.empty() ? std::string(32, ' ').append(hexDump) : "";
424 }
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: