feat: Add API to get MQTT client's status

Add a function which returns the status of MQTT client, either initialized,
connected, reconnecting, or disconnected.

Closes https://github.com/espressif/esp-mqtt/issues/307
This commit is contained in:
Bogdan Kolendovskyy
2025-10-13 16:13:54 +02:00
parent 55ee628c30
commit 77a875c804
3 changed files with 73 additions and 1 deletions
+24
View File
@@ -153,6 +153,17 @@ typedef enum esp_mqtt_protocol_ver_t {
MQTT_PROTOCOL_V_5,
} esp_mqtt_protocol_ver_t;
/**
* States of MQTT client connection
*/
typedef enum esp_mqtt_client_connection_state_t {
MQTT_CLIENT_STATE_NOT_INITIALIZED = 0, /*!< MQTT Client is not initialized */
MQTT_CLIENT_STATE_NOT_STARTED, /*!< MQTT Client is initialized, but not started */
MQTT_CLIENT_STATE_DISCONNECTED, /*!< MQTT Client is started, but not connected to the broker */
MQTT_CLIENT_STATE_CONNECTED, /*!< MQTT Client is connected to the broker */
MQTT_CLIENT_STATE_WAITING_RECONNECT, /*!< MQTT Client is waiting for reconnection request */
} esp_mqtt_client_connection_state_t;
/**
* @brief *MQTT* error code structure to be passed as a contextual information
* into ERROR event
@@ -706,6 +717,19 @@ esp_err_t esp_mqtt_dispatch_custom_event(esp_mqtt_client_handle_t client, esp_mq
*
*/
esp_transport_handle_t esp_mqtt_client_get_transport(esp_mqtt_client_handle_t client, char *transport_scheme);
/**
* @brief Get MQTT client's current state
*
* Get the current state of MQTT client. Returns a value to indicate whether is it initialized, connected, waiting for
* reconnection, or disconnected.
*
* @param client *MQTT* client handle
* @return MQTT client state on success
* MQTT_CLIENT_STATE_INVALID in case of error
*/
esp_mqtt_client_connection_state_t esp_mqtt_client_get_state(esp_mqtt_client_handle_t client);
#ifdef __cplusplus
}
#endif //__cplusplus