feat: Provide reason code in MQTT event

This commit is contained in:
Bogdan Kolendovskyy
2026-04-24 14:15:29 +02:00
parent 996177a46d
commit f1fc00ca44
6 changed files with 35 additions and 7 deletions
+1
View File
@@ -36,6 +36,7 @@ MACRO_EXPANSION = YES
EXPAND_ONLY_PREDEF = YES
PREDEFINED = \
$(ENV_DOXYGEN_DEFINES) \
__attribute__(x)=
## Do not complain about not having dot
##
+19 -3
View File
@@ -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
+2 -3
View File
@@ -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
+4
View File
@@ -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);
}
}
+8
View File
@@ -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(
@@ -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