Functions#

This section provides a reference to the MQTT API functions.

Note

  • Event/Callback handlers must not contain function calls or code which can block or delay the execution of the event/callback handler as it will cause all the other events to queue up and delay the execution of other events since all the events are invoked and handled from a single thread.

  • Do not call any synchronous SDK APIs from within the Event/Callback handlers.

Functions#

sl_status_t
sl_mqtt_client_init(sl_mqtt_client_t *client, sl_mqtt_client_event_handler_t event_handler)

Initialize the MQTT client and register the event handler.

sl_status_t
sl_mqtt_client_deinit(sl_mqtt_client_t *client)

Deinitialize the MQTT client.

sl_status_t
sl_mqtt_client_connect(sl_mqtt_client_t *client, const sl_mqtt_broker_t *broker, const sl_mqtt_client_last_will_message_t *last_will_message, const sl_mqtt_client_configuration_t *configuration, uint32_t timeout)

Connect the client to an MQTT broker.

sl_status_t
sl_mqtt_client_disconnect(sl_mqtt_client_t *client, uint32_t timeout)

Disconnect the client from the MQTT broker.

sl_status_t
sl_mqtt_client_publish(sl_mqtt_client_t *client, const sl_mqtt_client_message_t *message, uint32_t timeout, void *context)

Publishes the message to the MQTT broker.

sl_status_t
sl_mqtt_client_subscribe(sl_mqtt_client_t *client, const uint8_t *topic, uint16_t topic_length, sl_mqtt_qos_t qos_level, uint32_t timeout, sl_mqtt_client_message_received_t message_handler, void *context)

Subscribe a client to a specific topic on the MQTT broker.

sl_status_t
sl_mqtt_client_unsubscribe(sl_mqtt_client_t *client, const uint8_t *topic, uint16_t topic_length, uint32_t timeout, void *context)

Unsubscribe a client from a specific topic on the MQTT broker.

Function Documentation#

sl_mqtt_client_init#

sl_status_t sl_mqtt_client_init (sl_mqtt_client_t * client, sl_mqtt_client_event_handler_t event_handler)

Initialize the MQTT client and register the event handler.

Parameters
[in]client

Pointer to the MQTT client structure of type sl_mqtt_client_t. This pointer must not be NULL.

[in]event_handler

Event handler of type sl_mqtt_client_event_handler_t that would be called for various events on the client. This pointer must not be NULL.

This function initializes the MQTT client by setting up the event handler, and initializing the subscription list. It also assigns the provided client structure to the global MQTT client pointer.

Returns


Definition at line 46 of file components/service/mqtt/inc/sl_mqtt_client.h

sl_mqtt_client_deinit#

sl_status_t sl_mqtt_client_deinit (sl_mqtt_client_t * client)

Deinitialize the MQTT client.

Parameters
[in]client

Pointer to the MQTT client structure of type sl_mqtt_client_t. This pointer must not be NULL.

This function deinitializes the MQTT client by clearing its state and resetting the global MQTT client pointer. It ensures that the client is in a disconnected state before deinitializing.

  • The MQTT client must be in the disconnected state before calling this function.

Returns


Definition at line 66 of file components/service/mqtt/inc/sl_mqtt_client.h

sl_mqtt_client_connect#

sl_status_t sl_mqtt_client_connect (sl_mqtt_client_t * client, const sl_mqtt_broker_t * broker, const sl_mqtt_client_last_will_message_t * last_will_message, const sl_mqtt_client_configuration_t * configuration, uint32_t timeout)

Connect the client to an MQTT broker.

Parameters
[in]client

Pointer to the MQTT client structure of type sl_mqtt_client_t that would be connected to the broker.

[in]broker

Pointer to the broker configuration of type sl_mqtt_broker_t. This parameter could be NULL for subsequent calls to retain the previous broker configuration.

[in]last_will_message

Pointer to the last will message of the client of type sl_mqtt_client_last_will_message_t. This parameter could be NULL for subsequent calls if no will message is to be sent.

[in]configuration

Pointer to the client configuration of type sl_mqtt_client_configuration_t. This parameter could be NULL for subsequent calls to retain the previous configuration.

[in]timeout

Timeout for the API is in milliseconds. If the value is zero, the API operates in asynchronous mode.

This function connects the MQTT client to the specified broker using the provided configuration and last will message. It supports both synchronous, and asynchronous operations based on the timeout value.

Returns

Note

  • In subsequent calls to connect, it is optional to provide broker, last_will_message, and configuration parameters. If broker and configuration parameters are assigned as NULL, the values are retains from the first connect() call. For the last_will_message parameter, values assigned in each connect() call are considered. If the last_will_message parameter value is NULL, no will message are sent to the broker. is_clean_session, credential_id, client_id, and client_id_length of sl_mqtt_client_configuration_t are consider. The topic length of the last_will_message must be less than SI91X_MQTT_CLIENT_WILL_TOPIC_MAXIMUM_LENGTH. The client ID length should be less than SI91X_MQTT_CLIENT_CLIENT_ID_MAXIMUM_LENGTH. The username and password length must be less than SI91X_MQTT_CLIENT_USERNAME_MAXIMUM_LENGTH and SI91X_MQTT_CLIENT_PASSWORD_MAXIMUM_LENGTH, respectively.


Definition at line 110 of file components/service/mqtt/inc/sl_mqtt_client.h

sl_mqtt_client_disconnect#

sl_status_t sl_mqtt_client_disconnect (sl_mqtt_client_t * client, uint32_t timeout)

Disconnect the client from the MQTT broker.

Parameters
[in]client

Pointer to the MQTT client structure of type sl_mqtt_client_t disconnects from the broker. The pointer value must not be NULL.

[in]timeout

Timeout for the API in milliseconds. If the value is zero, the API is asynchronous.

This function disconnects the MQTT client from the broker and deinitializes the client context. It supports synchronous, and asynchronous operations based on the timeout value. The function ensures that the client is in a valid state before attempting to disconnect.

  • Synchronous Mode: If the timeout value is non-zero then, the function waits for the disconnect operation to complete within the specified timeout period.

  • Asynchronous Mode: If the timeout value is zero then, the function returns immediately, and the results are provided via the sl_mqtt_client_event_handler_t callback.

Returns

  • sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.

    • SL_STATUS_OK: Operation successful.

    • SL_STATUS_IN_PROGRESS: Operation is in progress (for asynchronous calls).

    • SL_STATUS_INVALID_PARAMETER: One or more parameters are invalid.

    • SL_STATUS_INVALID_STATE: The client is not in a valid state to disconnect.

    • SL_STATUS_FAIL: Operation failed.

Note

  • The function deinitializes the MQTT client context after the disconnection. The client must be in one of the following states to call this function: SL_MQTT_CLIENT_CONNECTED, SL_MQTT_CLIENT_TA_DISCONNECTED, or SL_MQTT_CLIENT_CONNECTION_FAILED.


Definition at line 146 of file components/service/mqtt/inc/sl_mqtt_client.h

sl_mqtt_client_publish#

sl_status_t sl_mqtt_client_publish (sl_mqtt_client_t * client, const sl_mqtt_client_message_t * message, uint32_t timeout, void * context)

Publishes the message to the MQTT broker.

Parameters
[in]client


Pointer to the MQTT client structure of type sl_mqtt_client_t publishes the message. This pointer value must not be NULL, and the client must be in a connected state.

[in]message


Pointer publishes the message structure of sl_mqtt_client_message_t type.The pointer must not be NULL, and the topic lenth value must be less than SI91X_MQTT_CLIENT_TOPIC_MAXIMUM_LENGTH.

[in]timeout


Timeout for the API is in milliseconds. If the value is zero, the API is asynchronous.

[in]context


Pointer to the context returns the event handler if the API is called asynchronously. The caller ensures that the lifecycle of the context is retained until the callback is invoked. The deallocation of the context is the responsibility of the caller.

This function publishes a message to the specified topic of the MQTT broker. It supports both synchronous, and asynchronous operations based on the timeout value. The function ensures that the client is in the connected state before attempting to publish the message.

Returns


  • sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.

    • SL_STATUS_OK: Operation successful.

    • SL_STATUS_IN_PROGRESS: Operation is in progress (for asynchronous calls).

    • SL_STATUS_INVALID_PARAMETER: One or more parameters are invalid.

    • SL_STATUS_INVALID_STATE: The client is not in a valid state to publish.

    • SL_STATUS_ALLOCATION_FAILED: Memory allocation failed.

    • SL_STATUS_FAIL: Operation failed.

Note

  • The maximum length of the topic must be less than SI91X_MQTT_CLIENT_TOPIC_MAXIMUM_LENGTH. The function allocates memory for the publish request, and free the space after the operation.


Definition at line 183 of file components/service/mqtt/inc/sl_mqtt_client.h

sl_mqtt_client_subscribe#

sl_status_t sl_mqtt_client_subscribe (sl_mqtt_client_t * client, const uint8_t * topic, uint16_t topic_length, sl_mqtt_qos_t qos_level, uint32_t timeout, sl_mqtt_client_message_received_t message_handler, void * context)

Subscribe a client to a specific topic on the MQTT broker.

Parameters
[in]client


Pointer to the MQTT client structure of type sl_mqtt_client_t that needs to subscribe to the topic. This pointer must not be NULL and the client must be in a connected state.

[in]topic

Pointer to the topic string that the client wants to subscribe to. This pointer must not be NULL.

[in]topic_length


Length of the topic string. The maximum length must be less than SI91X_MQTT_CLIENT_TOPIC_MAXIMUM_LENGTH.

[in]qos_level


Quality of Service level of type sl_mqtt_qos_t is subscribed by the client to use the topic.

[in]timeout


Timeout for the API is in milliseconds. If the value is zero, the API operates in asynchronous mode.

[in]message_handler


Pointer to the message handler function of type sl_mqtt_client_message_received_t that will be invoked for messages published on the subscribed topic. This pointer must not be NULL.

[in]context

Pointer to the context that will be sent back to the message handler. The caller must ensure that the lifecycle of the context is retained until the callback is invoked. The deallocation of the context is also the responsibility of the caller.

This function subscribes the specified client to a given topic on the MQTT broker. It supports both synchronous, and asynchronous operations based on the timeout value. The function ensures that the client is in a connected state before attempting to subscribe to the topic.

Returns


  • sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.

    • SL_STATUS_OK: Operation successful.

    • SL_STATUS_IN_PROGRESS: Operation is in progress (for asynchronous calls).

    • SL_STATUS_INVALID_PARAMETER: One or more parameters are invalid.

    • SL_STATUS_INVALID_STATE: The client is not in a valid state to subscribe.

    • SL_STATUS_ALLOCATION_FAILED: Memory allocation failed.

    • SL_STATUS_FAIL: Operation failed.

Note

  • The maximum length of the topic should be less than SI91X_MQTT_CLIENT_TOPIC_MAXIMUM_LENGTH.


Definition at line 231 of file components/service/mqtt/inc/sl_mqtt_client.h

sl_mqtt_client_unsubscribe#

sl_status_t sl_mqtt_client_unsubscribe (sl_mqtt_client_t * client, const uint8_t * topic, uint16_t topic_length, uint32_t timeout, void * context)

Unsubscribe a client from a specific topic on the MQTT broker.

Parameters
[in]client


Pointer to the MQTT client structure of type sl_mqtt_client_t that needs to unsubscribe from the topic. This pointer must not be NULL and the client must be in a connected state.

[in]topic

Pointer to the topic string that the client wants to unsubscribe from. This pointer must not be NULL.

[in]topic_length


Length of the topic string. The maximum length should be less than SI91X_MQTT_CLIENT_TOPIC_MAXIMUM_LENGTH.

[in]timeout


Timeout for the API in milliseconds. If the value is zero, the API is asynchronous.

[in]context

Pointer to the context that will be sent back to the event handler if the API is called asynchronously. The caller must ensure that the lifecycle of the context is retained until the callback is invoked. The deallocation of the context is also the responsibility of the caller.

This function unsubscribes the specified client from a given topic on the MQTT broker. It supports both synchronous, and asynchronous operations based on the timeout value. The function ensures that the client is in a connected state before attempting to unsubscribe from the topic.

Returns


  • sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.

    • SL_STATUS_OK: Operation successful.

    • SL_STATUS_IN_PROGRESS: Operation is in progress (for asynchronous calls).

    • SL_STATUS_INVALID_PARAMETER: One or more parameters are invalid.

    • SL_STATUS_INVALID_STATE: The client is not in a valid state to unsubscribe.

    • SL_STATUS_ALLOCATION_FAILED: Memory allocation failed.

    • SL_STATUS_FAIL: Operation failed.

Note

  • The maximum length of the topic must be less than SI91X_MQTT_CLIENT_TOPIC_MAXIMUM_LENGTH.


Definition at line 276 of file components/service/mqtt/inc/sl_mqtt_client.h