From 17fd713bced4f2d00df7ed664ed82a7d108ab317 Mon Sep 17 00:00:00 2001 From: Mikael Kanstrup Date: Thu, 25 Oct 2018 09:50:01 +0200 Subject: [PATCH 1/2] Avoid further wait period after reconnect timeout occurs When reconnect timer expires an additional waiting period of half the timeout period is seen. Skip this extra waiting period when timeout is detected and perform the connect attempt right away. This change makes configured reconnect timeout value MQTT_RECONNECT_TIMEOUT_MS accurate. --- mqtt_client.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mqtt_client.c b/mqtt_client.c index bf695f8..fa63fc7 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -845,6 +845,7 @@ static void esp_mqtt_task(void *pv) client->state = MQTT_STATE_INIT; client->reconnect_tick = platform_tick_get_ms(); ESP_LOGD(TAG, "Reconnecting..."); + break; } vTaskDelay(client->wait_timeout_ms / 2 / portTICK_RATE_MS); break; From df455d2a5fe562dd1b8351da99a1d6d82b66eff3 Mon Sep 17 00:00:00 2001 From: Mikael Kanstrup Date: Wed, 24 Oct 2018 10:57:45 +0200 Subject: [PATCH 2/2] Add client force reconnect function esp-mqtt library is unaware of underlying network connectivity states and current auto reconnect mechanism is built around timed retry attempts every MQTT_RECONNECT_TIMEOUT_MS. As application code usually keeps track of network connectity state export a new function that application can use to request a forced reconnect attempt as soon as connected to the network. --- include/mqtt_client.h | 1 + mqtt_client.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 56d6aea..e4e11d4 100755 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -110,6 +110,7 @@ typedef struct { esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *config); esp_err_t esp_mqtt_client_set_uri(esp_mqtt_client_handle_t client, const char *uri); esp_err_t esp_mqtt_client_start(esp_mqtt_client_handle_t client); +esp_err_t esp_mqtt_client_reconnect(esp_mqtt_client_handle_t client); esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client); esp_err_t esp_mqtt_client_subscribe(esp_mqtt_client_handle_t client, const char *topic, int qos); esp_err_t esp_mqtt_client_unsubscribe(esp_mqtt_client_handle_t client, const char *topic); diff --git a/mqtt_client.c b/mqtt_client.c index fa63fc7..9903462 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -75,6 +75,7 @@ struct esp_mqtt_client { }; const static int STOPPED_BIT = BIT0; +const static int RECONNECT_BIT = BIT1; static esp_err_t esp_mqtt_dispatch_event(esp_mqtt_client_handle_t client); static esp_err_t esp_mqtt_dispatch_event_with_msgid(esp_mqtt_client_handle_t client); @@ -769,6 +770,7 @@ static void esp_mqtt_task(void *pv) switch ((int)client->state) { case MQTT_STATE_INIT: + xEventGroupClearBits(client->status_bits, RECONNECT_BIT); client->event.event_id = MQTT_EVENT_BEFORE_CONNECT; esp_mqtt_dispatch_event_with_msgid(client); @@ -847,7 +849,8 @@ static void esp_mqtt_task(void *pv) ESP_LOGD(TAG, "Reconnecting..."); break; } - vTaskDelay(client->wait_timeout_ms / 2 / portTICK_RATE_MS); + xEventGroupWaitBits(client->status_bits, RECONNECT_BIT, false, true, + client->wait_timeout_ms / 2 / portTICK_RATE_MS); break; } } @@ -879,6 +882,18 @@ esp_err_t esp_mqtt_client_start(esp_mqtt_client_handle_t client) return ESP_OK; } +esp_err_t esp_mqtt_client_reconnect(esp_mqtt_client_handle_t client) +{ + ESP_LOGI(TAG, "Client force reconnect requested"); + if (client->state != MQTT_STATE_WAIT_TIMEOUT) { + ESP_LOGD(TAG, "The client is not waiting for reconnection. Ignore the request"); + return ESP_FAIL; + } + client->wait_timeout_ms = 0; + xEventGroupSetBits(client->status_bits, RECONNECT_BIT); + return ESP_OK; +} + esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client) { if (client->run) {