Skip to content

Commit

Permalink
mqtt5: Fix flow control will increase count when send fragmented packet
Browse files Browse the repository at this point in the history
Closes #255
  • Loading branch information
ESP-YJM committed Mar 17, 2023
1 parent 9f2db7b commit 5cce2c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
3 changes: 1 addition & 2 deletions mqtt5_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ static esp_err_t esp_mqtt5_user_property_copy(mqtt5_user_property_handle_t user_
void esp_mqtt5_increment_packet_counter(esp_mqtt5_client_handle_t client)
{
bool msg_dup = mqtt5_get_dup(client->mqtt_state.outbound_message->data);
int msg_qos = mqtt5_get_qos(client->mqtt_state.outbound_message->data);
if ((msg_dup == false) && (msg_qos > 0)) {
if (msg_dup == false) {
client->send_publish_packet_count ++;
ESP_LOGD(TAG, "Sent (%d) qos > 0 publish packet without ack", client->send_publish_packet_count);
}
Expand Down
26 changes: 17 additions & 9 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -943,11 +943,6 @@ static esp_err_t mqtt_write_data(esp_mqtt_client_handle_t client)
esp_mqtt_client_dispatch_transport_error(client);
return ESP_FAIL;
}
#ifdef MQTT_PROTOCOL_5
if (client->connect_info.protocol_ver == MQTT_PROTOCOL_V_5) {
esp_mqtt5_increment_packet_counter(client);
}
#endif
return ESP_OK;
}

Expand Down Expand Up @@ -1475,10 +1470,18 @@ static esp_err_t mqtt_resend_queued(esp_mqtt_client_handle_t client, outbox_item
}

// check if it was QoS-0 publish message
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_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->connect_info.protocol_ver == MQTT_PROTOCOL_V_5) {
esp_mqtt5_increment_packet_counter(client);
}
#endif
}
}
return ESP_OK;
Expand Down Expand Up @@ -2090,6 +2093,11 @@ int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic,
}

if (qos > 0) {
#ifdef MQTT_PROTOCOL_5
if (client->connect_info.protocol_ver == MQTT_PROTOCOL_V_5) {
esp_mqtt5_increment_packet_counter(client);
}
#endif
//Tick is set after transmit to avoid retransmitting too early due slow network speed / big messages
outbox_set_tick(client->outbox, pending_msg_id, platform_tick_get_ms());
outbox_set_pending(client->outbox, pending_msg_id, TRANSMITTED);
Expand Down

0 comments on commit 5cce2c4

Please sign in to comment.