diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 2d8c376..67c0828 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -199,6 +199,7 @@ typedef struct { bool use_secure_element; /*!< enable secure element for enabling SSL connection */ void *ds_data; /*!< carrier of handle for digital signature parameters */ int network_timeout_ms; /*!< Abort network operation if it is not completed after this value, in milliseconds (defaults to 10s) */ + bool disable_keepalive; /*!< Set disable_keepalive=true to turn off keep-alive mechanism, false by default (keepalive is active by default). Note: setting the config value `keepalive` to `0` doesn't disable keepalive feature, but uses a default keepalive period */ } esp_mqtt_client_config_t; /** diff --git a/lib/include/mqtt_msg.h b/lib/include/mqtt_msg.h index a7b070c..78d6e40 100644 --- a/lib/include/mqtt_msg.h +++ b/lib/include/mqtt_msg.h @@ -83,7 +83,7 @@ typedef struct mqtt_connect_info { char *password; char *will_topic; char *will_message; - int keepalive; + int keepalive; // keepalive=0 -> keepalive is disabled int will_length; int will_qos; int will_retain; diff --git a/mqtt_client.c b/mqtt_client.c index f1221da..6202a54 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -419,6 +419,12 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl if (client->connect_info.keepalive == 0) { client->connect_info.keepalive = MQTT_KEEPALIVE_TICK; } + if (config->disable_keepalive) { + // internal `keepalive` value (in connect_info) is in line with 3.1.2.10 Keep Alive from mqtt spec: + // * keepalive=0: Keep alive mechanism disabled (server not to disconnect the client on its inactivity) + // * period in seconds to send a Control packet if inactive + client->connect_info.keepalive = 0; + } if (config->protocol_ver) { client->connect_info.protocol_ver = config->protocol_ver; @@ -1376,7 +1382,8 @@ static void esp_mqtt_task(void *pv) } } - if (platform_tick_get_ms() - client->keepalive_tick > client->connect_info.keepalive * 1000 / 2) { + if (client->connect_info.keepalive && // connect_info.keepalive=0 means that the keepslive is disabled + platform_tick_get_ms() - client->keepalive_tick > client->connect_info.keepalive * 1000 / 2) { //No ping resp from last ping => Disconnected if (client->wait_for_ping_resp) { ESP_LOGE(TAG, "No PING_RESP, disconnected");