From 55f04a8e61a0e3bacf2b40518d9d522e9b79e615 Mon Sep 17 00:00:00 2001 From: oldgreen Date: Fri, 16 Mar 2018 12:25:34 +0100 Subject: [PATCH] Abort MQTT connection if sending of ping fails (#30) * Remove unused code: the state here is always MQTT_STATE_CONNECTED. Under the assumption that only the esp_mqtt_task can change the client state (otherwise we are missing a lock around the state usage everywhere). * Change the return type of esp_mqtt_client_ping from int to esp_err_t. * Abort the MQTT connection if we can't send ping. Try to handle the situation when the underlying connection changes and the sockets have wrong status (see esp-idf/docs/api-guides/wifi.rst). --- mqtt_client.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/mqtt_client.c b/mqtt_client.c index 3c53851..0053207 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -78,7 +78,7 @@ static esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_ static esp_err_t esp_mqtt_destroy_config(esp_mqtt_client_handle_t client); static esp_err_t esp_mqtt_connect(esp_mqtt_client_handle_t client, int timeout_ms); static esp_err_t esp_mqtt_abort_connection(esp_mqtt_client_handle_t client); -static int esp_mqtt_client_ping(esp_mqtt_client_handle_t client); +static esp_err_t esp_mqtt_client_ping(esp_mqtt_client_handle_t client); static char *create_string(const char *ptr, int len); static esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_client_config_t *config) @@ -692,7 +692,10 @@ static void esp_mqtt_task(void *pv) } if (platform_tick_get_ms() - client->keepalive_tick > client->connect_info.keepalive * 1000 / 2) { - esp_mqtt_client_ping(client); + if (esp_mqtt_client_ping(client) == ESP_FAIL) { + esp_mqtt_abort_connection(client); + break; + } client->keepalive_tick = platform_tick_get_ms(); } @@ -745,21 +748,16 @@ esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client) return ESP_OK; } -static int esp_mqtt_client_ping(esp_mqtt_client_handle_t client) +static esp_err_t esp_mqtt_client_ping(esp_mqtt_client_handle_t client) { - if (client->state != MQTT_STATE_CONNECTED) { - ESP_LOGE(TAG, "Client has not connected"); - return -1; - } - client->mqtt_state.outbound_message = mqtt_msg_pingreq(&client->mqtt_state.mqtt_connection); if (mqtt_write_data(client) != ESP_OK) { ESP_LOGE(TAG, "Error sending ping"); - return -1; + return ESP_FAIL; } ESP_LOGD(TAG, "Sent PING successful"); - return 0; + return ESP_OK; } int esp_mqtt_client_subscribe(esp_mqtt_client_handle_t client, const char *topic, int qos)