LoboMQ
Loading...
Searching...
No Matches
PubSub.h File Reference
#include <stdint.h>
#include "commons/Includes.h"

Go to the source code of this file.

Classes

struct  MessageBase
 Structure that contains the fields used by every message. More...
 
struct  SubscribeAnnouncement
 Structure that contains the fields used by a subscribe message, apart from those inherited from the MessageBase. More...
 
struct  UnsubscribeAnnouncement
 Structure that contains the fields used by a unsubscribe message, apart from those inherited from the MessageBase. More...
 
struct  PublishContent
 Structure that contains the fields used by a publish message, apart from those inherited from the MessageBase. More...
 
struct  PayloadContent
 Structure representing the content of a payload This structure holds properties of the content received inside a publication message. More...
 

Macros

#define MAXTOPICLENGTH   24
 
#define MAXCONTENTSIZE   30
 

Enumerations

enum  MessageType { MSGTYPE_SUBSCRIBE = (uint8_t)0x00 , MSGTYPE_UNSUBSCRIBE , MSGTYPE_PUBLISH }
 Enumerates every type of message sent between the broker and the clients. More...
 

Functions

LMQErrType publish (uint8_t *mac, char *topic, void *payload, size_t payloadSize, Elog *_logger=disableLogger())
 Publishes a message to the broker.
 
LMQErrType subscribe (uint8_t *mac, char *topic, Elog *_logger=disableLogger())
 Subscribes to a topic on the broker.
 
LMQErrType unsubscribe (uint8_t *mac, char *topic, Elog *_logger=disableLogger())
 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.
 

Detailed Description

Author
Rubén Gómez Villegas

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

Definition in file PubSub.h.

Macro Definition Documentation

◆ MAXCONTENTSIZE

#define MAXCONTENTSIZE   30

Definition at line 18 of file PubSub.h.

◆ MAXTOPICLENGTH

#define MAXTOPICLENGTH   24

Definition at line 17 of file PubSub.h.

Enumeration Type Documentation

◆ MessageType

Enumerates every type of message sent between the broker and the clients.

Enumerator
MSGTYPE_SUBSCRIBE 

Subscribe message, sent from subscriber to broker.

MSGTYPE_UNSUBSCRIBE 

Unsubscribe message, sent from subscriber to broker.

MSGTYPE_PUBLISH 

Publish message, sent from publisher to broker or from broker to subscriber.

Definition at line 25 of file PubSub.h.

Function Documentation

◆ 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.

◆ 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.

◆ 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.

◆ 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.