From ffe25df82ad8b9ec591cfc0c2f946196c0fd3856 Mon Sep 17 00:00:00 2001 From: Justin Brzozoski Date: Thu, 27 Jun 2019 14:24:23 -0400 Subject: [PATCH] mqtt: Allow client to override keepalive This change will allow an MQTT client to override the compile-time keepalive if desired. The change is structured such that the compile-time default will still be setup by calling mqtt_client_init, but can be changed by the application before calling mqtt_connect if desired. Signed-off-by: Justin Brzozoski --- include/net/mqtt.h | 5 +++++ subsys/net/lib/mqtt/mqtt.c | 5 +++-- subsys/net/lib/mqtt/mqtt_encoder.c | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/net/mqtt.h b/include/net/mqtt.h index 883765cc391..f8782e7310c 100644 --- a/include/net/mqtt.h +++ b/include/net/mqtt.h @@ -476,6 +476,11 @@ struct mqtt_client { /** Size of transmit buffer. */ u32_t tx_buf_size; + /** Keepalive interval for this client in seconds. + * Default is CONFIG_MQTT_KEEPALIVE. + */ + u16_t keepalive; + /** MQTT protocol version. */ u8_t protocol_version; diff --git a/subsys/net/lib/mqtt/mqtt.c b/subsys/net/lib/mqtt/mqtt.c index f3ab439e502..5bfd7d26d8b 100644 --- a/subsys/net/lib/mqtt/mqtt.c +++ b/subsys/net/lib/mqtt/mqtt.c @@ -168,6 +168,7 @@ void mqtt_client_init(struct mqtt_client *client) client->protocol_version = MQTT_VERSION_3_1_1; client->clean_session = 1U; + client->keepalive = MQTT_KEEPALIVE; } int mqtt_connect(struct mqtt_client *client) @@ -562,8 +563,8 @@ int mqtt_live(struct mqtt_client *client) elapsed_time = mqtt_elapsed_time_in_ms_get( client->internal.last_activity); - if ((MQTT_KEEPALIVE > 0) && - (elapsed_time >= (MQTT_KEEPALIVE * 1000))) { + if ((client->keepalive > 0) && + (elapsed_time >= (client->keepalive * 1000))) { (void)mqtt_ping(client); } } diff --git a/subsys/net/lib/mqtt/mqtt_encoder.c b/subsys/net/lib/mqtt/mqtt_encoder.c index 28e5b4005d0..ce63df0b08b 100644 --- a/subsys/net/lib/mqtt/mqtt_encoder.c +++ b/subsys/net/lib/mqtt/mqtt_encoder.c @@ -325,8 +325,8 @@ int connect_request_encode(const struct mqtt_client *client, return err_code; } - MQTT_TRC("Encoding Keep Alive Time %04x.", MQTT_KEEPALIVE); - err_code = pack_uint16(MQTT_KEEPALIVE, buf); + MQTT_TRC("Encoding Keep Alive Time %04x.", client->keepalive); + err_code = pack_uint16(client->keepalive, buf); if (err_code != 0) { return err_code; }