From 5cce2c4f3582e2be3bf5ddf81ddc3d81ced79eec Mon Sep 17 00:00:00 2001 From: yuanjianmin Date: Fri, 17 Mar 2023 16:55:25 +0800 Subject: [PATCH] mqtt5: Fix flow control will increase count when send fragmented packet Closes https://github.com/espressif/esp-mqtt/issues/255 --- mqtt5_client.c | 3 +-- mqtt_client.c | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/mqtt5_client.c b/mqtt5_client.c index d5daba1..82d4f41 100644 --- a/mqtt5_client.c +++ b/mqtt5_client.c @@ -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); } diff --git a/mqtt_client.c b/mqtt_client.c index 2a50254..258d1da 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -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; } @@ -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; @@ -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);