Skip to content

Commit

Permalink
Merge branch 'feature/tcp_keepalive' into 'master'
Browse files Browse the repository at this point in the history
feat: Add TCP keepalive configuration

Closes IDF-8049

See merge request espressif/esp-mqtt!220
  • Loading branch information
euripedesrocha committed Sep 25, 2024
2 parents e89f239 + 7c3227a commit 9de024c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions lib/include/mqtt_client_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1627,6 +1631,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);

Expand Down

0 comments on commit 9de024c

Please sign in to comment.