BSB Developer Docs
Loading...
Searching...
No Matches
mqtt.h File Reference

MQTT & BUSY account service API. More...

#include "mqtt_config.h"
#include <core/pubsub.h>
#include <core/string.h>
#include <time.h>

Go to the source code of this file.

Data Structures

struct  MqttProperty
 Data type for a single MQTT property. More...
 
struct  MqttEventLinkPinReceived
 Data type describing the event of type MqttEventTypeLinkPinReceived. More...
 
struct  MqttEventStatusChanged
 Data type describing the event of type MqttEventTypeStatusChanged. More...
 
struct  MqttEvent
 Data type describing the generic MQTT service event, complete with type and specific data. More...
 
struct  MqttSessionInfo
 Data type for a linked account information. More...
 

Macros

#define RECORD_MQTT   "mqtt"
 The string key for MQTT instance access.
 
#define MQTT_LINK_PIN_LEN   (4)
 The length of the link PIN string.
 

Typedefs

typedef struct Mqtt Mqtt
 Opaque data type for the MQTT service instance.
 
typedef struct MqttMessage MqttMessage
 Opaque data type for the MQTT message object.
 
typedef struct MqttSubscription MqttSubscription
 Opaque data type for the MQTT subscription object.
 
typedef void(* MqttSubscriptionCallback) (const MqttMessage *message, void *context)
 Subscription callback function type.
 

Enumerations

enum  MqttPropertyType { MqttPropertyTypeExpiryInterval , MqttPropertyTypeResponseTopic , MqttPropertyTypeCorrelationData , MqttPropertyTypeMax }
 Enumeration of available property types. More...
 
enum  MqttEventType {
  MqttEventTypeStatusChanged , MqttEventTypeLinkPinReceived , MqttEventTypeLinkDone , MqttEventTypeUnlinked ,
  MqttEventTypeMax
}
 Enumeration of possible event types emitted by the MQTT service. More...
 
enum  MqttStatus {
  MqttStatusError , MqttStatusNotConnected , MqttStatusConnectedNotLinked , MqttStatusConnectedLinked ,
  MqttStatusMax
}
 Enumeration of possible states that the MQTT service can be in. More...
 
enum  MqttQos { MqttQosAtMostOnce = 0 , MqttQosAtLeastOnce = 1 , MqttQosExactlyOnce = 2 , MqttQosMax }
 Enumeration of possible MQTT QoS (Quality of Service) levels. More...
 

Functions

FuriPubSub * mqtt_get_pubsub (Mqtt *instance)
 Get the pointer to the associated FuriPubSub object.
 
MqttStatus mqtt_get_status (Mqtt *instance)
 Get the current status of the MQTT service.
 
bool mqtt_request_link_pin (Mqtt *instance)
 Request the account linking PIN from the BUSY cloud.
 
void mqtt_unlink (Mqtt *instance)
 Unlink the device from the BUSY account.
 
void mqtt_get_session_info (Mqtt *instance, MqttSessionInfo *info)
 Get current session information.
 
void mqtt_get_config (Mqtt *instance, MqttConfig *config)
 Get the current backend configuration.
 
bool mqtt_set_config (Mqtt *instance, const MqttConfig *config)
 Set the backend configuration.
 
bool mqtt_publish (Mqtt *instance, MqttQos qos, const char *topic, const void *data, size_t data_size)
 Publish a message to a topic on the MQTT broker.
 
bool mqtt_publish_ex (Mqtt *instance, MqttQos qos, const char *topic, const void *data, size_t data_size, const MqttProperty *props, uint32_t props_count)
 Publish a message to a topic on the MQTT broker with additional properties.
 
MqttSubscription * mqtt_subscribe (Mqtt *instance, MqttQos qos, const char *topic, MqttSubscriptionCallback callback, void *context)
 Subscribe to a topic on the MQTT broker and start receiving messages.
 
void mqtt_unsubscribe (Mqtt *instance, MqttSubscription *subscription)
 Unsubscribe from a topic on the MQTT broker and stop receiving messages.
 
const void * mqtt_message_get_data (const MqttMessage *message, size_t *data_size)
 Get the payload (data) contained in a MqttMessage object.
 
bool mqtt_message_get_string_property (const MqttMessage *message, MqttPropertyType property_type, FuriString *value)
 Get the string property possibly attached to a MqttMessage object.
 
bool mqtt_message_get_integer_property (const MqttMessage *message, MqttPropertyType property_type, uint32_t *value)
 Get the integer property possibly attached to a MqttMessage object.
 

Detailed Description

MQTT & BUSY account service API.

The MQTT service is responsible for:

  • Initiating and managing connections to the remote MQTT broker,
  • BUSY account handling (linking & unlinking),
  • Subscribing and publishing to session topics (see below).

Limitations:

  • Wildcard subscriptions are NOT supported,
  • Attempts to publish when offline or not linked will result in an error,
  • Unsubscribing from a topic will cause a reconnection to the MQTT broker.

Topic format: With the below API, subscription/publishing is only possible on the session topic.

Internally, MQTT session topics have the following format:

  • sessions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/down/v1/topic/name for subscriptions
  • sessions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/up/v1/topic/name for publishing

where aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee is a unique session identifier.

The MQTT service API abstracts it so the application must use only the topic/name part when interacting with it. This rule also applies to some MQTT message properties such as Response Topic (see below).

Data lifetime:

  • Output functions (e.g. mqtt_publish) will block briefly until the provided data is copied and sent internally. It is safe to delete the data afterwards.
  • Data returned in callbacks is only valid inside the callback. No pointers should be stored upon exiting from it and all necessary data must be copied to recipient-owned storage.

Macro Definition Documentation

◆ RECORD_MQTT

#define RECORD_MQTT   "mqtt"

The string key for MQTT instance access.

Get the instance pointer by calling furi_record_open(RECORD_MQTT);

Typedef Documentation

◆ Mqtt

typedef struct Mqtt Mqtt

Opaque data type for the MQTT service instance.

Get the instance pointer by calling furi_record_open(RECORD_MQTT);

◆ MqttSubscriptionCallback

typedef void(* MqttSubscriptionCallback) (const MqttMessage *message, void *context)

Subscription callback function type.

A function with this signature may be used to deliver MQTT messages via invoking the mqtt_subscribe() API.

Note
The value pointed to by the message parameter is only valid within the context of the callback it was delivered in.
Parameters
[in]messagepointer to the delivered message
[in,out]contextpointer to the user-specific object (set during subscribing)

Enumeration Type Documentation

◆ MqttEventType

Enumeration of possible event types emitted by the MQTT service.

Enumerator
MqttEventTypeStatusChanged 

A status change has occurred.

MqttEventTypeLinkPinReceived 

An account linking PIN has been received in response to a request.

MqttEventTypeLinkDone 

Account linking has been successfully completed.

MqttEventTypeUnlinked 

Account was unlinked from the device.

MqttEventTypeMax 

Special value, internal use.

◆ MqttPropertyType

Enumeration of available property types.

Note
Not all possible properties are implemented. Add new ones as needed.

See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901029 for reference.

Enumerator
MqttPropertyTypeExpiryInterval 

Data type: integer.

MqttPropertyTypeResponseTopic 

Data type: string.

MqttPropertyTypeCorrelationData 

Data type: string.

MqttPropertyTypeMax 

Special value, internal use.

◆ MqttQos

enum MqttQos

Enumeration of possible MQTT QoS (Quality of Service) levels.

See https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718099 for reference.

Enumerator
MqttQosAtMostOnce 

Deliver at most once (lowest)

MqttQosAtLeastOnce 

Deliver at least once.

MqttQosExactlyOnce 

Deliver exactly once (highest)

MqttQosMax 

Special value, internal use.

◆ MqttStatus

enum MqttStatus

Enumeration of possible states that the MQTT service can be in.

Enumerator
MqttStatusError 

An error has occurred, usually because clent certificates are missing.

MqttStatusNotConnected 

Not connected to MQTT broker.

MqttStatusConnectedNotLinked 

Connected to MQTT broker, but not linked to an account.

MqttStatusConnectedLinked 

Connected to MQTT broker, and linked to an account.

MqttStatusMax 

Special value, internal use.

Function Documentation

◆ mqtt_get_config()

void mqtt_get_config ( Mqtt * instance,
MqttConfig * config )

Get the current backend configuration.

Parameters
[in]instancepointer to the MQTT service instance to be queried
[out]configpointer to a MqttConfig structure to contain the result (must be allocated)

◆ mqtt_get_pubsub()

FuriPubSub * mqtt_get_pubsub ( Mqtt * instance)

Get the pointer to the associated FuriPubSub object.

Use furi_pubsub_subscribe() to subscribe to the MQTT service events. The delivered events will be of type MqttEvent.

Parameters
[in,out]instancepointer to the MQTT service instance
Returns
pointer to the FuriPubSub object

◆ mqtt_get_session_info()

void mqtt_get_session_info ( Mqtt * instance,
MqttSessionInfo * info )

Get current session information.

Parameters
[in]instancepointer to the MQTT service instance to be queried
[in,out]infopointer to the MqttSessionInfo structure

◆ mqtt_get_status()

MqttStatus mqtt_get_status ( Mqtt * instance)

Get the current status of the MQTT service.

Parameters
[in]instancepointer to the MQTT service instance to be queried
Returns
numeric value corresponding to the current status

◆ mqtt_message_get_data()

const void * mqtt_message_get_data ( const MqttMessage * message,
size_t * data_size )

Get the payload (data) contained in a MqttMessage object.

Note
objects of type MqttMessage are only valid within the context of the callback that delivered them.
data_size may be NULL if it is not required.
Parameters
[in]messagepointer to the MqttMessage object to be queried
[out]data_sizepointer to the value to contain the size (length) of the data
Returns
pointer to the data held by the message

◆ mqtt_message_get_integer_property()

bool mqtt_message_get_integer_property ( const MqttMessage * message,
MqttPropertyType property_type,
uint32_t * value )

Get the integer property possibly attached to a MqttMessage object.

Warning
The value of property_type MUST correspond to the underlying integer data type.
Parameters
[in]messagepointer to the MqttMessage object to be queried
[in]property_typevalue from the MqttPropertyType enum denoting the property type
[out]valuepointer to an integer variable to contain the value (must be allocated)
Returns
true if a property of property_type was found, false otherwise

◆ mqtt_message_get_string_property()

bool mqtt_message_get_string_property ( const MqttMessage * message,
MqttPropertyType property_type,
FuriString * value )

Get the string property possibly attached to a MqttMessage object.

Warning
The value of property_type MUST correspond to the underlying string data type.
Parameters
[in]messagepointer to the MqttMessage object to be queried
[in]property_typevalue from the MqttPropertyType enum denoting the property type
[out]valuepointer to a FuriString object to contain the value (must be allocated)
Returns
true if a property of property_type was found, false otherwise

◆ mqtt_publish()

bool mqtt_publish ( Mqtt * instance,
MqttQos qos,
const char * topic,
const void * data,
size_t data_size )

Publish a message to a topic on the MQTT broker.

Note
The caller must provide only the unique topic part, e.g. topic/name in the above example
Parameters
[in,out]instancepointer to the MQTT service instance
[in]qosenum value from MqttQos corresponding to a QoS level
[in]topicC-string containing the specific topic part
[in]datapointer to arbitrary data to be published
[in]data_sizesize (or length) of the data to be published
Returns
true if publishing was successful, false otherwise

◆ mqtt_publish_ex()

bool mqtt_publish_ex ( Mqtt * instance,
MqttQos qos,
const char * topic,
const void * data,
size_t data_size,
const MqttProperty * props,
uint32_t props_count )

Publish a message to a topic on the MQTT broker with additional properties.

Note
The caller must provide only the unique topic part, e.g. topic/name in the above example
Parameters
[in,out]instancepointer to the MQTT service instance
[in]qosenum value from MqttQos corresponding to a QoS level
[in]topicC-string containing the specific topic part
[in]datapointer to arbitrary data to be published
[in]data_sizesize (or length) of the data to be published
[in]propspointer to the array of MQTT message properties
[in]props_countnumber of properties contained in the props array
Returns
true if publishing was successful, false otherwise

◆ mqtt_request_link_pin()

bool mqtt_request_link_pin ( Mqtt * instance)

Request the account linking PIN from the BUSY cloud.

Note
The requested PIN will be delivered asynchronously via the MqttEventTypeLinkPinReceived event.
Parameters
[in,out]instancepointer to the MQTT service instance
Returns
true if the request was sent successfully, false otherwise

◆ mqtt_set_config()

bool mqtt_set_config ( Mqtt * instance,
const MqttConfig * config )

Set the backend configuration.

Parameters
[in,out]instancepointer to the MQTT service instance to be modified
[in]configpointer to a MqttConfig structure containing the configuration to be set
Returns
true if the configuration could be successfully set, false otherwise

◆ mqtt_subscribe()

MqttSubscription * mqtt_subscribe ( Mqtt * instance,
MqttQos qos,
const char * topic,
MqttSubscriptionCallback callback,
void * context )

Subscribe to a topic on the MQTT broker and start receiving messages.

Note
The caller must provide only the unique topic part, e.g. topic/name in the above example
Parameters
[in,out]instancepointer to the MQTT service instance
[in]qosenum value from MqttQos corresponding to a QoS level
[in]topicC-string containing the specific topic part
[in]callbackpointer to the function used to asynchronously deliver MQTT messages
[in,out]contextpointer to a user-specific object (will be passed to the callback)
Returns
pointer to the object uniquely identifying the resulting subscription

◆ mqtt_unlink()

void mqtt_unlink ( Mqtt * instance)

Unlink the device from the BUSY account.

Parameters
[in,out]instancepointer to the MQTT service instance

◆ mqtt_unsubscribe()

void mqtt_unsubscribe ( Mqtt * instance,
MqttSubscription * subscription )

Unsubscribe from a topic on the MQTT broker and stop receiving messages.

Warning
Due to current technical limitations, calling this function will trigger a reconnection procedure.
Parameters
[in,out]instancepointer to the MQTT service instance
[in,out]subscriptionpointer to the target subscription object