From 3e35fc832308b6d40b5e291005f8edfed779b2cf Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Fri, 27 Sep 2019 19:58:09 +0800 Subject: [PATCH] MQTT: fix MQTT_PUBLISHED_EVENT not always being posted - Some PUBLISHED events would not be posted due to outbox messages being deleted before receiving PUBCOMP. - Current pending_message is no longer used to check for acknowledgements, as it doesn't work with multiple or out of order messages. Closes https://github.com/espressif/esp-mqtt/issues/132 --- lib/include/mqtt_outbox.h | 1 + mqtt_client.c | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/include/mqtt_outbox.h b/lib/include/mqtt_outbox.h index 36981e5..5c84b66 100644 --- a/lib/include/mqtt_outbox.h +++ b/lib/include/mqtt_outbox.h @@ -30,6 +30,7 @@ typedef struct outbox_message { typedef enum pending_state { QUEUED, TRANSMITTED, + ACKNOWLEDGED, CONFIRMED } pending_state_t; diff --git a/mqtt_client.c b/mqtt_client.c index 9337eec..4d228ee 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -682,10 +682,6 @@ static bool is_valid_mqtt_msg(esp_mqtt_client_handle_t client, int msg_type, int client->mqtt_state.pending_msg_count --; return true; } - if (client->mqtt_state.pending_msg_type == msg_type && client->mqtt_state.pending_msg_id == msg_id) { - client->mqtt_state.pending_msg_count --; - return true; - } return false; } @@ -935,7 +931,7 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client) case MQTT_MSG_TYPE_PUBREC: ESP_LOGD(TAG, "received MQTT_MSG_TYPE_PUBREC"); client->mqtt_state.outbound_message = mqtt_msg_pubrel(&client->mqtt_state.mqtt_connection, msg_id); - outbox_set_pending(client->outbox, msg_id, CONFIRMED); + outbox_set_pending(client->outbox, msg_id, ACKNOWLEDGED); mqtt_write_data(client); break; case MQTT_MSG_TYPE_PUBREL: @@ -947,6 +943,7 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client) ESP_LOGD(TAG, "received MQTT_MSG_TYPE_PUBCOMP"); if (is_valid_mqtt_msg(client, MQTT_MSG_TYPE_PUBLISH, msg_id)) { ESP_LOGD(TAG, "Receive MQTT_MSG_TYPE_PUBCOMP, finish QoS2 publish"); + outbox_set_pending(client->outbox, msg_id, CONFIRMED); client->event.event_id = MQTT_EVENT_PUBLISHED; esp_mqtt_dispatch_event_with_msgid(client); }