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
This commit is contained in:
Marius Vikhammer
2019-09-27 19:58:09 +08:00
committed by David Cermak
parent 78e2a7050e
commit 3e35fc8323
2 changed files with 3 additions and 5 deletions

View File

@ -30,6 +30,7 @@ typedef struct outbox_message {
typedef enum pending_state { typedef enum pending_state {
QUEUED, QUEUED,
TRANSMITTED, TRANSMITTED,
ACKNOWLEDGED,
CONFIRMED CONFIRMED
} pending_state_t; } pending_state_t;

View File

@ -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 --; client->mqtt_state.pending_msg_count --;
return true; 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; return false;
} }
@ -935,7 +931,7 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client)
case MQTT_MSG_TYPE_PUBREC: case MQTT_MSG_TYPE_PUBREC:
ESP_LOGD(TAG, "received 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); 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); mqtt_write_data(client);
break; break;
case MQTT_MSG_TYPE_PUBREL: 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"); ESP_LOGD(TAG, "received MQTT_MSG_TYPE_PUBCOMP");
if (is_valid_mqtt_msg(client, MQTT_MSG_TYPE_PUBLISH, msg_id)) { if (is_valid_mqtt_msg(client, MQTT_MSG_TYPE_PUBLISH, msg_id)) {
ESP_LOGD(TAG, "Receive MQTT_MSG_TYPE_PUBCOMP, finish QoS2 publish"); 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; client->event.event_id = MQTT_EVENT_PUBLISHED;
esp_mqtt_dispatch_event_with_msgid(client); esp_mqtt_dispatch_event_with_msgid(client);
} }