LoboMQ
Loading...
Searching...
No Matches
PubSub.cpp File Reference
#include "LoboMQ/PubSub.h"

Go to the source code of this file.

Functions

bool configureESPNOW (uint8_t *mac)
 
int fixTopicAndCheckLength (char *topic)
 
bool isASCII (char c)
 
int pubTopicCheck (char *topic)
 
int subTopicCheck (char *topic)
 
LMQErrType publish (uint8_t *mac, char *topic, void *payload, size_t payloadSize, Elog *_logger)
 Publishes a message to the broker.
 
LMQErrType subscribe (uint8_t *mac, char *topic, Elog *_logger)
 Subscribes to a topic on the broker.
 
LMQErrType unsubscribe (uint8_t *mac, char *topic, Elog *_logger)
 Unsubscribes from a topic on the broker.
 
bool isLMQMessage (const uint8_t *incomingData)
 Checks if the data received is a MQ message.
 
PayloadContent getLMQPayload (const uint8_t *incomingData)
 Gets the payload content inside a published message This function extracts the payload from the bytes of a received publication message.
 

Variables

Elog * logger
 

Detailed Description

Author
Rubén Gómez Villegas

This file contains the necessary logic to publish, subscribe and unsubscribe as a LoboMQ client.

Definition in file PubSub.cpp.

Function Documentation

◆ configureESPNOW()

bool configureESPNOW ( uint8_t * mac)

Definition at line 13 of file PubSub.cpp.

◆ fixTopicAndCheckLength()

int fixTopicAndCheckLength ( char * topic)

Definition at line 41 of file PubSub.cpp.

◆ getLMQPayload()

PayloadContent getLMQPayload ( const uint8_t * incomingData)

Gets the payload content inside a published message This function extracts the payload from the bytes of a received publication message.

Parameters
incomingDataThe data received.
Returns
A PayloadContent structure containing the extracted payload content.
Note
This function is recommended to be used at the subscriber side in the data receive callback alongside isLMQMessage(). Pseudocode example:
OnReceiveCallback(incomingData) {
if isLMQMessage(incomingData)
payload = getLMQPayload(incomingData)
}
PayloadContent getLMQPayload(const uint8_t *incomingData)
Gets the payload content inside a published message This function extracts the payload from the bytes...
Definition PubSub.cpp:191
bool isLMQMessage(const uint8_t *incomingData)
Checks if the data received is a MQ message.
Definition PubSub.cpp:186

Definition at line 191 of file PubSub.cpp.

◆ isASCII()

bool isASCII ( char c)

Definition at line 66 of file PubSub.cpp.

◆ isLMQMessage()

bool isLMQMessage ( const uint8_t * incomingData)

Checks if the data received is a MQ message.

This function checks if the received bytes represent a MQ message from this library.

Parameters
incomingDataThe data received.
Return values
`true`if the data is a MQ message.
`false`otherwise.
Note
This function is recommended to be used at the subscriber side in the data receive callback alongside getLMQPayload(). Pseudocode example:
OnReceiveCallback(incomingData) {
if isLMQMessage(incomingData)
payload = getLMQPayload(incomingData)
}

Definition at line 186 of file PubSub.cpp.

◆ publish()

LMQErrType publish ( uint8_t * mac,
char * topic,
void * payload,
size_t payloadSize,
Elog * _logger = disableLogger() )

Publishes a message to the broker.

This function takes the payload and builds a message that will be published to the specified topic on the broker.

Parameters
macThe broker MAC address.
topicThe topic to publish the message to. It can't contain wildcard characters (+, #) nor non-UTF-8 characters. Invalid example: +/café. Valid example: kitchen/coffee.
payloadPointer to the message payload.
payloadSizeSize of the message payload.
_loggerPointer to the logger object.
Return values
`LMQ_ERR_SUCCESS`if the message is successfully published.
`LMQ_ERR_BAD_ESP_CONFIG`if ESP-NOW couldn't be initialized.
`LMQ_ERR_INVAL_TOPIC`if the given topic is invalid.
`LMQ_ERR_ESP_SEND_FAIL`if the message couldn't be sent.

Definition at line 105 of file PubSub.cpp.

◆ pubTopicCheck()

int pubTopicCheck ( char * topic)

Definition at line 70 of file PubSub.cpp.

◆ subscribe()

LMQErrType subscribe ( uint8_t * mac,
char * topic,
Elog * _logger = disableLogger() )

Subscribes to a topic on the broker.

This function sends a message to the broker announcing that the calling board is interested in receiving all the messages compatible with the specified topic.

Parameters
macThe broker MAC address.
topicThe topic the board subscribes to. Is compatible with wildcard characters (+, #) when used properly, and can't contain non-UTF-8 characters. Invalid example: résumé/+/#/garden. Valid example: +/+/out/#.
_loggerPointer to the logger object.
Return values
`LMQ_ERR_SUCCESS`if the message is successfully published.
`LMQ_ERR_BAD_ESP_CONFIG`if ESP-NOW couldn't be initialized.
`LMQ_ERR_INVAL_TOPIC`if the given topic is invalid.
`LMQ_ERR_ESP_SEND_FAIL`if the message couldn't be sent.

Definition at line 133 of file PubSub.cpp.

◆ subTopicCheck()

int subTopicCheck ( char * topic)

Definition at line 81 of file PubSub.cpp.

◆ unsubscribe()

LMQErrType unsubscribe ( uint8_t * mac,
char * topic,
Elog * _logger = disableLogger() )

Unsubscribes from a topic on the broker.

This function sends a message to the broker announcing that the calling board is no longer interested in receiving all the messages compatible with the specified topic.

Parameters
macThe broker MAC address.
topicThe MQ topic the board unsubscribes from. Is compatible with wildcard characters (+, #) when used properly, and can't contain non-UTF-8 characters. Invalid example: résumé/+/#/garden. Valid example: +/+/out/#.
_loggerPointer to the logger object.
Return values
`LMQ_ERR_SUCCESS`if the message is successfully published.
`LMQ_ERR_BAD_ESP_CONFIG`if ESP-NOW couldn't be initialized.
`LMQ_ERR_INVAL_TOPIC`if the given topic is invalid.
`LMQ_ERR_ESP_SEND_FAIL`if the message couldn't be sent.

Definition at line 159 of file PubSub.cpp.

Variable Documentation

◆ logger

Elog* logger

Definition at line 11 of file PubSub.cpp.