diff --git a/include/mqtt_client.h b/include/mqtt_client.h index bd6d3ba..8bd0ed4 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -341,6 +341,7 @@ typedef struct esp_mqtt_client_config_t { int refresh_connection_after_ms; /*!< Refresh connection after this value (in milliseconds) */ bool disable_auto_reconnect; /*!< Client will reconnect to server (when errors/disconnect). Set `disable_auto_reconnect=true` to disable */ + esp_transport_keep_alive_t tcp_keep_alive_cfg; /*!< Transport keep-alive config*/ esp_transport_handle_t transport; /*!< Custom transport handle to use, leave it NULL to allow MQTT client create or recreate its own. Warning: The transport should be valid during the client lifetime and is destroyed when esp_mqtt_client_destroy is called. */ struct ifreq * if_name; /*!< The name of interface for data to go through. Use the default interface without setting */ } network; /*!< Network configuration */ diff --git a/lib/include/mqtt_client_priv.h b/lib/include/mqtt_client_priv.h index 0b152b0..235631f 100644 --- a/lib/include/mqtt_client_priv.h +++ b/lib/include/mqtt_client_priv.h @@ -100,6 +100,7 @@ typedef struct { uint64_t outbox_limit; esp_transport_handle_t transport; struct ifreq * if_name; + esp_transport_keep_alive_t tcp_keep_alive_cfg; } mqtt_config_storage_t; typedef enum { diff --git a/mqtt_client.c b/mqtt_client.c index a5afa3c..8c46b6b 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -429,6 +429,10 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl client->config->port = config->broker.address.port; } + if (config->network.tcp_keep_alive_cfg.keep_alive_enable) { + client->config->tcp_keep_alive_cfg = config->network.tcp_keep_alive_cfg; + } + err = ESP_ERR_NO_MEM; ESP_MEM_CHECK(TAG, esp_mqtt_set_if_config(config->broker.address.hostname, &client->config->host), goto _mqtt_set_config_failed); ESP_MEM_CHECK(TAG, esp_mqtt_set_if_config(config->broker.address.path, &client->config->path), goto _mqtt_set_config_failed); @@ -521,7 +525,7 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl } else { client->config->reconnect_timeout_ms = MQTT_RECON_DEFAULT_MS; } - + client->config->transport = config->network.transport; if (config->network.if_name) { @@ -1608,6 +1612,10 @@ static void esp_mqtt_task(void *pv) esp_mqtt_set_ssl_transport_properties(client->transport_list, client->config); #endif + if(client->config->tcp_keep_alive_cfg.keep_alive_enable) { + esp_transport_tcp_set_keep_alive(client->transport, &client->config->tcp_keep_alive_cfg); + } + client->event.event_id = MQTT_EVENT_BEFORE_CONNECT; esp_mqtt_dispatch_event_with_msgid(client);