Skip to content

Commit 470cb93

Browse files
committed
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 <[email protected]>
1 parent 7e5e482 commit 470cb93

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

include/mqtt5_client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef struct esp_mqtt_client *esp_mqtt5_client_handle_t;
1818
/**
1919
* MQTT5 protocol error reason code, more details refer to MQTT5 protocol document section 2.4
2020
*/
21-
enum mqtt5_error_reason_code {
21+
typedef enum mqtt5_error_reason_code_t {
2222
MQTT5_UNSPECIFIED_ERROR = 0x80,
2323
MQTT5_MALFORMED_PACKET = 0x81,
2424
MQTT5_PROTOCOL_ERROR = 0x82,
@@ -59,7 +59,7 @@ enum mqtt5_error_reason_code {
5959
MQTT5_MAXIMUM_CONNECT_TIME = 0xA0,
6060
MQTT5_SUBSCRIBE_IDENTIFIER_NOT_SUPPORT = 0xA1,
6161
MQTT5_WILDCARD_SUBSCRIBE_NOT_SUPPORT = 0xA2,
62-
};
62+
} esp_mqtt5_error_reason_code_t;
6363

6464
/**
6565
* MQTT5 user property handle

include/mqtt_client.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ typedef struct esp_mqtt_error_codes {
182182
esp_mqtt_connect_return_code_t
183183
connect_return_code; /*!< connection refused error code reported from
184184
*MQTT* broker on connection */
185+
#ifdef CONFIG_MQTT_PROTOCOL_5
186+
esp_mqtt5_error_reason_code_t
187+
disconnect_return_code; /*!< disconnection reason code reported from
188+
*MQTT* broker on disconnection */
189+
#endif
185190
/* tcp_transport extension */
186191
int esp_transport_sock_errno; /*!< errno from the underlying socket */
187192

lib/include/mqtt5_client_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void esp_mqtt5_parse_pubcomp(esp_mqtt5_client_handle_t client);
4242
void esp_mqtt5_parse_puback(esp_mqtt5_client_handle_t client);
4343
void esp_mqtt5_parse_unsuback(esp_mqtt5_client_handle_t client);
4444
void esp_mqtt5_parse_suback(esp_mqtt5_client_handle_t client);
45+
void esp_mqtt5_parse_disconnect(esp_mqtt5_client_handle_t client, int *disconnect_rsp_code);
4546
esp_err_t esp_mqtt5_parse_connack(esp_mqtt5_client_handle_t client, int *connect_rsp_code);
4647
void esp_mqtt5_client_destory(esp_mqtt5_client_handle_t client);
4748
esp_err_t esp_mqtt5_client_publish_check(esp_mqtt5_client_handle_t client, int qos, int retain);

mqtt5_client.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ void esp_mqtt5_parse_suback(esp_mqtt5_client_handle_t client)
7676
}
7777
}
7878

79+
void esp_mqtt5_parse_disconnect(esp_mqtt5_client_handle_t client, int *disconnect_rsp_code)
80+
{
81+
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
82+
*disconnect_rsp_code = mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len);
83+
ESP_LOGD(TAG, "MQTT_MSG_TYPE_DISCONNECT return code is %d", *disconnect_rsp_code);
84+
}
85+
}
86+
7987
esp_err_t esp_mqtt5_parse_connack(esp_mqtt5_client_handle_t client, int *connect_rsp_code)
8088
{
8189
size_t len = client->mqtt_state.in_buffer_read_len;

mqtt_client.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,18 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client)
15111511
*/
15121512
client->keepalive_tick = platform_tick_get_ms();
15131513
break;
1514+
case MQTT_MSG_TYPE_DISCONNECT:
1515+
ESP_LOGD(TAG, "MQTT_MSG_TYPE_DISCONNECT");
1516+
#ifdef MQTT_PROTOCOL_5
1517+
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
1518+
int disconnect_rsp_code;
1519+
esp_mqtt5_parse_disconnect(client, &disconnect_rsp_code);
1520+
client->event.event_id = MQTT_EVENT_DISCONNECTED;
1521+
client->event.error_handle->disconnect_return_code = disconnect_rsp_code;
1522+
esp_mqtt_dispatch_event_with_msgid(client);
1523+
}
1524+
#endif
1525+
break;
15141526
}
15151527

15161528
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
22742286
client->event.event_id = MQTT_EVENT_ERROR;
22752287
client->event.error_handle->error_type = MQTT_ERROR_TYPE_TCP_TRANSPORT;
22762288
client->event.error_handle->connect_return_code = 0;
2289+
#ifdef MQTT_PROTOCOL_5
2290+
client->event.error_handle->disconnect_return_code = 0;
2291+
#endif
22772292
#ifdef MQTT_SUPPORTED_FEATURE_TRANSPORT_ERR_REPORTING
22782293
client->event.error_handle->esp_tls_last_esp_err = esp_tls_get_and_clear_last_error(esp_transport_get_error_handle(client->transport),
22792294
&client->event.error_handle->esp_tls_stack_err,

0 commit comments

Comments
 (0)