Skip to content

Commit 7c3227a

Browse files
feat: Add TCP keepalive configuration
1 parent b5b8033 commit 7c3227a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

include/mqtt_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ typedef struct esp_mqtt_client_config_t {
341341
int refresh_connection_after_ms; /*!< Refresh connection after this value (in milliseconds) */
342342
bool disable_auto_reconnect; /*!< Client will reconnect to server (when errors/disconnect). Set
343343
`disable_auto_reconnect=true` to disable */
344+
esp_transport_keep_alive_t tcp_keep_alive_cfg; /*!< Transport keep-alive config*/
344345
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. */
345346
struct ifreq * if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
346347
} network; /*!< Network configuration */

lib/include/mqtt_client_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ typedef struct {
100100
uint64_t outbox_limit;
101101
esp_transport_handle_t transport;
102102
struct ifreq * if_name;
103+
esp_transport_keep_alive_t tcp_keep_alive_cfg;
103104
} mqtt_config_storage_t;
104105

105106
typedef enum {

mqtt_client.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl
429429
client->config->port = config->broker.address.port;
430430
}
431431

432+
if (config->network.tcp_keep_alive_cfg.keep_alive_enable) {
433+
client->config->tcp_keep_alive_cfg = config->network.tcp_keep_alive_cfg;
434+
}
435+
432436
err = ESP_ERR_NO_MEM;
433437
ESP_MEM_CHECK(TAG, esp_mqtt_set_if_config(config->broker.address.hostname, &client->config->host), goto _mqtt_set_config_failed);
434438
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
521525
} else {
522526
client->config->reconnect_timeout_ms = MQTT_RECON_DEFAULT_MS;
523527
}
524-
528+
525529
client->config->transport = config->network.transport;
526530

527531
if (config->network.if_name) {
@@ -1608,6 +1612,10 @@ static void esp_mqtt_task(void *pv)
16081612
esp_mqtt_set_ssl_transport_properties(client->transport_list, client->config);
16091613
#endif
16101614

1615+
if(client->config->tcp_keep_alive_cfg.keep_alive_enable) {
1616+
esp_transport_tcp_set_keep_alive(client->transport, &client->config->tcp_keep_alive_cfg);
1617+
}
1618+
16111619
client->event.event_id = MQTT_EVENT_BEFORE_CONNECT;
16121620
esp_mqtt_dispatch_event_with_msgid(client);
16131621

0 commit comments

Comments
 (0)