mirror of
https://github.com/espressif/esp-mqtt.git
synced 2025-05-17 15:19:08 +08:00
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:
@ -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
|
||||
|
@ -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 */
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user