mqtt_client: Manage disconnect packet

In the mqtt5 protocol the broker can disconnect the client with
a disconnect packet. This packet contains a reason value that can be
useful for certain applications in which it is important to know
the reason of disconnection.
While the client is connected is possible that a disconnect packet
is reaceived by the broker to force a disconnection. Before this
patch this approach causes a generic error on transport in case of
disconnection from the broker. If the packet is managed before getting
an error it is possible to save the reason code in the
disconnect_return_code variable in the error_handle, and dispatch
the disconnect event that can be managed by the application event
loop, that now can know the reason of disconnection from the broker.
Reset the variable in case of error.

Signed-off-by: Flavia Caforio <flavia.caforio@amarulasolutions.com>
This commit is contained in:
Flavia Caforio
2025-02-18 16:24:13 +01:00
parent 7e5e4821fa
commit 470cb93049
5 changed files with 31 additions and 2 deletions
+2 -2
View File
@@ -18,7 +18,7 @@ typedef struct esp_mqtt_client *esp_mqtt5_client_handle_t;
/**
* MQTT5 protocol error reason code, more details refer to MQTT5 protocol document section 2.4
*/
enum mqtt5_error_reason_code {
typedef enum mqtt5_error_reason_code_t {
MQTT5_UNSPECIFIED_ERROR = 0x80,
MQTT5_MALFORMED_PACKET = 0x81,
MQTT5_PROTOCOL_ERROR = 0x82,
@@ -59,7 +59,7 @@ enum mqtt5_error_reason_code {
MQTT5_MAXIMUM_CONNECT_TIME = 0xA0,
MQTT5_SUBSCRIBE_IDENTIFIER_NOT_SUPPORT = 0xA1,
MQTT5_WILDCARD_SUBSCRIBE_NOT_SUPPORT = 0xA2,
};
} esp_mqtt5_error_reason_code_t;
/**
* MQTT5 user property handle
+5
View File
@@ -182,6 +182,11 @@ typedef struct esp_mqtt_error_codes {
esp_mqtt_connect_return_code_t
connect_return_code; /*!< connection refused error code reported from
*MQTT* broker on connection */
#ifdef CONFIG_MQTT_PROTOCOL_5
esp_mqtt5_error_reason_code_t
disconnect_return_code; /*!< disconnection reason code reported from
*MQTT* broker on disconnection */
#endif
/* tcp_transport extension */
int esp_transport_sock_errno; /*!< errno from the underlying socket */