From f1fc00ca4436ad9b7e786dc0c1d63d382bf78e61 Mon Sep 17 00:00:00 2001 From: Bogdan Kolendovskyy Date: Fri, 24 Apr 2026 14:15:29 +0200 Subject: [PATCH] feat: Provide reason code in MQTT event --- docs/Doxyfile | 1 + include/mqtt5_client.h | 22 ++++++++++++++++--- include/mqtt_client.h | 5 ++--- mqtt5_client.c | 4 ++++ mqtt_client.c | 8 +++++++ .../mqtt_conformance/main/mqtt_conformance.c | 2 +- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/docs/Doxyfile b/docs/Doxyfile index 35dabbc..1b62486 100644 --- a/docs/Doxyfile +++ b/docs/Doxyfile @@ -36,6 +36,7 @@ MACRO_EXPANSION = YES EXPAND_ONLY_PREDEF = YES PREDEFINED = \ $(ENV_DOXYGEN_DEFINES) \ +__attribute__(x)= ## Do not complain about not having dot ## diff --git a/include/mqtt5_client.h b/include/mqtt5_client.h index 0353a7e..5f0cfcd 100644 --- a/include/mqtt5_client.h +++ b/include/mqtt5_client.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -18,7 +18,17 @@ 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 */ -typedef enum mqtt5_error_reason_code_t { +typedef enum mqtt5_reason_code_t { + MQTT5_SUCCESS = 0x00, + MQTT5_NORMAL_DISCONNECT = 0x00, + MQTT5_GRANTED_QOS0 = 0x00, + MQTT5_GRANTED_QOS1 = 0x01, + MQTT5_GRANTED_QOS2 = 0x02, + MQTT5_DISCONNECT_WITH_WILL = 0x04, + MQTT5_NO_MATCHING_SUBSCRIBERS = 0x10, + MQTT5_NO_SUBSCRIPTION_EXISTED = 0x11, + MQTT5_CONTINUE_AUTHENTICATION = 0x18, + MQTT5_REAUTHENTICATE = 0x19, MQTT5_UNSPECIFIED_ERROR = 0x80, MQTT5_MALFORMED_PACKET = 0x81, MQTT5_PROTOCOL_ERROR = 0x82, @@ -59,7 +69,13 @@ typedef enum mqtt5_error_reason_code_t { MQTT5_MAXIMUM_CONNECT_TIME = 0xA0, MQTT5_SUBSCRIBE_IDENTIFIER_NOT_SUPPORT = 0xA1, MQTT5_WILDCARD_SUBSCRIBE_NOT_SUPPORT = 0xA2, -} esp_mqtt5_error_reason_code_t; +} esp_mqtt5_reason_code_t; + +/** + * \deprecated alias esp_mqtt5_error_reason_code_t for esp_mqtt5_reason_code_t exists for backwards compatibility reasons + * and will be removed in subsequent updates. + */ +typedef __attribute__((deprecated)) esp_mqtt5_reason_code_t esp_mqtt5_error_reason_code_t; /** * MQTT5 user property handle diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 5196b81..e26edf3 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -199,9 +199,7 @@ typedef struct esp_mqtt_error_codes { 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 */ + esp_mqtt5_reason_code_t disconnect_return_code __attribute__((deprecated)); /*!<\deprecated disconnection reason code reported from *MQTT* broker on disconnection, deprecated - use reason_code in event instead */ #endif /* tcp_transport extension */ int esp_transport_sock_errno; /*!< errno from the underlying socket */ @@ -234,6 +232,7 @@ typedef struct esp_mqtt_event_t { esp_mqtt_protocol_ver_t protocol_ver; /*!< MQTT protocol version used for connection, defaults to value from menuconfig*/ #ifdef CONFIG_MQTT_PROTOCOL_5 + esp_mqtt5_reason_code_t reason_code; /*!< *MQTT* 5 reason code */ esp_mqtt5_event_property_t *property; /*!< MQTT 5 property associated with this event */ #endif diff --git a/mqtt5_client.c b/mqtt5_client.c index 8cc7e32..77a9af9 100644 --- a/mqtt5_client.c +++ b/mqtt5_client.c @@ -43,6 +43,7 @@ void esp_mqtt5_parse_pubcomp(esp_mqtt5_client_handle_t client) ESP_LOGD(TAG, "MQTT_MSG_TYPE_PUBCOMP return code is %d", mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len)); size_t msg_data_len = client->mqtt_state.in_buffer_read_len; + client->event.reason_code = mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len); client->event.data = mqtt5_get_pubcomp_data(client->mqtt_state.in_buffer, &msg_data_len, &client->event.property->user_property); client->event.data_len = msg_data_len; @@ -57,6 +58,7 @@ void esp_mqtt5_parse_puback(esp_mqtt5_client_handle_t client) ESP_LOGD(TAG, "MQTT_MSG_TYPE_PUBACK return code is %d", mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len)); size_t msg_data_len = client->mqtt_state.in_buffer_read_len; + client->event.reason_code = mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len); client->event.data = mqtt5_get_puback_data(client->mqtt_state.in_buffer, &msg_data_len, &client->event.property->user_property); client->event.data_len = msg_data_len; @@ -71,6 +73,7 @@ void esp_mqtt5_parse_unsuback(esp_mqtt5_client_handle_t client) ESP_LOGD(TAG, "MQTT_MSG_TYPE_UNSUBACK return code is %d", mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len)); size_t msg_data_len = client->mqtt_state.in_buffer_read_len; + client->event.reason_code = mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len); client->event.data = mqtt5_get_unsuback_data(client->mqtt_state.in_buffer, &msg_data_len, &client->event.property->user_property); client->event.data_len = msg_data_len; @@ -84,6 +87,7 @@ void esp_mqtt5_parse_suback(esp_mqtt5_client_handle_t client) if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) { ESP_LOGD(TAG, "MQTT_MSG_TYPE_SUBACK return code is %d", mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len)); + client->event.reason_code = mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len); } } diff --git a/mqtt_client.c b/mqtt_client.c index 4f8de82..aab7cad 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -1746,7 +1746,11 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client) int disconnect_rsp_code; esp_mqtt5_parse_disconnect(client, &disconnect_rsp_code); client->event.event_id = MQTT_EVENT_DISCONNECTED; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" client->event.error_handle->disconnect_return_code = disconnect_rsp_code; + client->event.reason_code = disconnect_rsp_code; +#pragma GCC diagnostic pop esp_mqtt_dispatch_event_with_msgid(client); } @@ -2670,7 +2674,11 @@ static void esp_mqtt_client_dispatch_transport_error(esp_mqtt_client_handle_t cl client->event.error_handle->error_type = MQTT_ERROR_TYPE_TCP_TRANSPORT; client->event.error_handle->connect_return_code = 0; #ifdef MQTT_PROTOCOL_5 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" client->event.error_handle->disconnect_return_code = 0; + client->event.reason_code = 0; +#pragma GCC diagnostic pop #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( diff --git a/test/apps/mqtt_conformance/main/mqtt_conformance.c b/test/apps/mqtt_conformance/main/mqtt_conformance.c index 4cfd114..01d7b96 100644 --- a/test/apps/mqtt_conformance/main/mqtt_conformance.c +++ b/test/apps/mqtt_conformance/main/mqtt_conformance.c @@ -33,7 +33,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ case MQTT_EVENT_DISCONNECTED: #if CONFIG_MQTT_PROTOCOL_5 if (event->error_handle) { - ESP_LOGW(TAG, "DISCONNECT_REASON=%d", event->error_handle->disconnect_return_code); + ESP_LOGW(TAG, "DISCONNECT_REASON=%d", event->reason_code); } #endif