diff --git a/include/mqtt5_client.h b/include/mqtt5_client.h index d6605a8..25254c4 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 5e84e5f..ea187e5 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 2337e06..1b5f04b 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 6059d14..4e49f40 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 99f2b5f..f02e763 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,