Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the parsing of DISCONNECT packet for MQTT 5 (IDFGH-14489) #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/mqtt5_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions include/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
1 change: 1 addition & 0 deletions lib/include/mqtt5_client_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions mqtt5_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Loading