From 470cb93049aea2dd51c7bb9db4dae963370d4a98 Mon Sep 17 00:00:00 2001 From: Flavia Caforio Date: Tue, 18 Feb 2025 16:24:13 +0100 Subject: [PATCH] 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 --- include/mqtt5_client.h | 4 ++-- include/mqtt_client.h | 5 +++++ lib/include/mqtt5_client_priv.h | 1 + mqtt5_client.c | 8 ++++++++ mqtt_client.c | 15 +++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/mqtt5_client.h b/include/mqtt5_client.h index d6605a82..25254c41 100644 --- a/include/mqtt5_client.h +++ b/include/mqtt5_client.h @@ -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 diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 5e84e5fd..ea187e5c 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -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 */ diff --git a/lib/include/mqtt5_client_priv.h b/lib/include/mqtt5_client_priv.h index 2337e064..1b5f04be 100644 --- a/lib/include/mqtt5_client_priv.h +++ b/lib/include/mqtt5_client_priv.h @@ -42,6 +42,7 @@ void esp_mqtt5_parse_pubcomp(esp_mqtt5_client_handle_t client); void esp_mqtt5_parse_puback(esp_mqtt5_client_handle_t client); void esp_mqtt5_parse_unsuback(esp_mqtt5_client_handle_t client); void esp_mqtt5_parse_suback(esp_mqtt5_client_handle_t client); +void esp_mqtt5_parse_disconnect(esp_mqtt5_client_handle_t client, int *disconnect_rsp_code); esp_err_t esp_mqtt5_parse_connack(esp_mqtt5_client_handle_t client, int *connect_rsp_code); void esp_mqtt5_client_destory(esp_mqtt5_client_handle_t client); esp_err_t esp_mqtt5_client_publish_check(esp_mqtt5_client_handle_t client, int qos, int retain); diff --git a/mqtt5_client.c b/mqtt5_client.c index 6059d149..4e49f403 100644 --- a/mqtt5_client.c +++ b/mqtt5_client.c @@ -76,6 +76,14 @@ void esp_mqtt5_parse_suback(esp_mqtt5_client_handle_t client) } } +void esp_mqtt5_parse_disconnect(esp_mqtt5_client_handle_t client, int *disconnect_rsp_code) +{ + if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) { + *disconnect_rsp_code = mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len); + ESP_LOGD(TAG, "MQTT_MSG_TYPE_DISCONNECT return code is %d", *disconnect_rsp_code); + } +} + esp_err_t esp_mqtt5_parse_connack(esp_mqtt5_client_handle_t client, int *connect_rsp_code) { size_t len = client->mqtt_state.in_buffer_read_len; diff --git a/mqtt_client.c b/mqtt_client.c index 99f2b5ff..f02e7635 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -1511,6 +1511,18 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client) */ client->keepalive_tick = platform_tick_get_ms(); break; + case MQTT_MSG_TYPE_DISCONNECT: + ESP_LOGD(TAG, "MQTT_MSG_TYPE_DISCONNECT"); +#ifdef MQTT_PROTOCOL_5 + if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) { + int disconnect_rsp_code; + esp_mqtt5_parse_disconnect(client, &disconnect_rsp_code); + client->event.event_id = MQTT_EVENT_DISCONNECTED; + client->event.error_handle->disconnect_return_code = disconnect_rsp_code; + esp_mqtt_dispatch_event_with_msgid(client); + } +#endif + break; } client->mqtt_state.in_buffer_read_len = 0; @@ -2274,6 +2286,9 @@ static void esp_mqtt_client_dispatch_transport_error(esp_mqtt_client_handle_t cl client->event.event_id = MQTT_EVENT_ERROR; client->event.error_handle->error_type = MQTT_ERROR_TYPE_TCP_TRANSPORT; client->event.error_handle->connect_return_code = 0; +#ifdef MQTT_PROTOCOL_5 + client->event.error_handle->disconnect_return_code = 0; +#endif #ifdef MQTT_SUPPORTED_FEATURE_TRANSPORT_ERR_REPORTING client->event.error_handle->esp_tls_last_esp_err = esp_tls_get_and_clear_last_error(esp_transport_get_error_handle(client->transport), &client->event.error_handle->esp_tls_stack_err,