From f7325bfa10c7cacabb76e58fcd2e1622a9e1b589 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Mon, 10 Aug 2020 14:47:05 +0800 Subject: [PATCH 1/2] mqtt: esp_mqtt_client_publish now returns msd id for QoS>0 when offline --- mqtt_client.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mqtt_client.c b/mqtt_client.c index edfec7a..4a6b7d5 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -1634,6 +1634,9 @@ int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic, /* Skip sending if not connected (rely on resending) */ if (client->state != MQTT_STATE_CONNECTED) { ESP_LOGD(TAG, "Publish: client is not connected"); + if (qos > 0) { + ret = pending_msg_id; + } goto cannot_publish; } From bdadd77c6e24382234c1960600ac2bdf4a6ace9d Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Mon, 10 Aug 2020 15:47:46 +0800 Subject: [PATCH 2/2] mqtt: deleted expired messages even when offline As long as the client was disconnect no cleanup were performed, consuming memory for every message published. Even if they were already expired and would be discarded when reconnected. Also moves delete of expired msgs before retransmit is done. This avoids situtations where a message could be sent even if it was expired. Closes https://github.com/espressif/esp-idf/issues/5668 --- include/mqtt_config.h | 7 ++++++- mqtt_client.c | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/include/mqtt_config.h b/include/mqtt_config.h index 2c9d403..e259a2a 100644 --- a/include/mqtt_config.h +++ b/include/mqtt_config.h @@ -84,11 +84,16 @@ #endif #endif +#ifdef CONFIG_OUTBOX_EXPIRED_TIMEOUT_MS +#define OUTBOX_EXPIRED_TIMEOUT_MS CONFIG_OUTBOX_EXPIRED_TIMEOUT_MS +#else +#define OUTBOX_EXPIRED_TIMEOUT_MS (30*1000) +#endif #define MQTT_ENABLE_SSL CONFIG_MQTT_TRANSPORT_SSL #define MQTT_ENABLE_WS CONFIG_MQTT_TRANSPORT_WEBSOCKET #define MQTT_ENABLE_WSS CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE -#define OUTBOX_EXPIRED_TIMEOUT_MS (30*1000) + #define OUTBOX_MAX_SIZE (4*1024) #endif diff --git a/mqtt_client.c b/mqtt_client.c index 4a6b7d5..d3d1528 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -1344,6 +1344,14 @@ static void esp_mqtt_task(void *pv) break; } + //Delete message after OUTBOX_EXPIRED_TIMEOUT_MS miliseconds + int deleted = outbox_delete_expired(client->outbox, platform_tick_get_ms(), OUTBOX_EXPIRED_TIMEOUT_MS); + client->mqtt_state.pending_msg_count -= deleted; + + if (client->mqtt_state.pending_msg_count < 0) { + client->mqtt_state.pending_msg_count = 0; + } + // resend all non-transmitted messages first outbox_item_handle_t item = outbox_dequeue(client->outbox, QUEUED, NULL); if (item) { @@ -1384,13 +1392,6 @@ static void esp_mqtt_task(void *pv) client->state = MQTT_STATE_INIT; } - //Delete message after 30 seconds - int deleted = outbox_delete_expired(client->outbox, platform_tick_get_ms(), OUTBOX_EXPIRED_TIMEOUT_MS); - client->mqtt_state.pending_msg_count -= deleted; - if (client->mqtt_state.pending_msg_count < 0) { - client->mqtt_state.pending_msg_count = 0; - } - // outbox_cleanup(client->outbox, OUTBOX_MAX_SIZE); break; case MQTT_STATE_WAIT_TIMEOUT: @@ -1637,6 +1638,15 @@ int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic, if (qos > 0) { ret = pending_msg_id; } + + //Delete message after OUTBOX_EXPIRED_TIMEOUT_MS miliseconds + int deleted = outbox_delete_expired(client->outbox, platform_tick_get_ms(), OUTBOX_EXPIRED_TIMEOUT_MS); + client->mqtt_state.pending_msg_count -= deleted; + + if (client->mqtt_state.pending_msg_count < 0) { + client->mqtt_state.pending_msg_count = 0; + } + goto cannot_publish; }