Skip to content

Commit

Permalink
fix: Handling of state in the outbox for enqueued QoS 0 messages
Browse files Browse the repository at this point in the history
If a QoS 0 was handled by the enqueued messages transmission, it could
lead to set incorrectly another QoS 0 as transmitted.

Closes #276
  • Loading branch information
euripedesrocha committed May 27, 2024
1 parent bed1207 commit 739cb2d
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1513,21 +1513,6 @@ static esp_err_t mqtt_resend_queued(esp_mqtt_client_handle_t client, outbox_item
return ESP_FAIL;
}

// check if it was QoS-0 publish message
if (client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH) {
if (client->mqtt_state.pending_publish_qos == 0) {
// delete all qos0 publish messages once we process them
if (outbox_delete_item(client->outbox, item) != ESP_OK) {
ESP_LOGE(TAG, "Failed to remove queued qos0 message from the outbox");
}
} else if (client->mqtt_state.pending_publish_qos > 0) {
#ifdef MQTT_PROTOCOL_5
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
esp_mqtt5_increment_packet_counter(client);
}
#endif
}
}
return ESP_OK;
}

Expand Down Expand Up @@ -1675,14 +1660,33 @@ static void esp_mqtt_task(void *pv)
outbox_item_handle_t item = outbox_dequeue(client->outbox, QUEUED, NULL);
if (item) {
if (mqtt_resend_queued(client, item) == ESP_OK) {
outbox_set_pending(client->outbox, client->mqtt_state.pending_msg_id, TRANSMITTED);
if (client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH && client->mqtt_state.pending_publish_qos == 0) {
// delete all qos0 publish messages once we process them
if (outbox_delete_item(client->outbox, item) != ESP_OK) {
ESP_LOGE(TAG, "Failed to remove queued qos0 message from the outbox");
}
}
if (client->mqtt_state.pending_publish_qos > 0) {
outbox_set_pending(client->outbox, client->mqtt_state.pending_msg_id, TRANSMITTED);
#ifdef MQTT_PROTOCOL_5
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
esp_mqtt5_increment_packet_counter(client);
}
#endif
}
}
// resend other "transmitted" messages after 1s
} else if (has_timed_out(last_retransmit, client->config->message_retransmit_timeout)) {
last_retransmit = platform_tick_get_ms();
item = outbox_dequeue(client->outbox, TRANSMITTED, &msg_tick);
if (item && (last_retransmit - msg_tick > client->config->message_retransmit_timeout)) {
mqtt_resend_queued(client, item);
if (mqtt_resend_queued(client, item) == ESP_OK) {
#ifdef MQTT_PROTOCOL_5
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
esp_mqtt5_increment_packet_counter(client);
}
#endif
}
}
}

Expand Down

0 comments on commit 739cb2d

Please sign in to comment.