Merge branch 'feature/support_no_keepalive' into 'master'

mqtt_config: Add config value to disable keepalive mechanism

See merge request espressif/esp-mqtt!83
This commit is contained in:
David Čermák
2020-11-19 22:07:05 +08:00
3 changed files with 10 additions and 2 deletions

View File

@ -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;
/**

View File

@ -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;

View File

@ -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");