Skip to content
Permalink
Browse files

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 <justin.brzozoski@signal-fire.com>
  • Loading branch information...
jbrzozoski authored and nashif committed Jun 27, 2019
1 parent fb898e3 commit ffe25df82ad8b9ec591cfc0c2f946196c0fd3856
Showing with 10 additions and 4 deletions.
  1. +5 −0 include/net/mqtt.h
  2. +3 −2 subsys/net/lib/mqtt/mqtt.c
  3. +2 −2 subsys/net/lib/mqtt/mqtt_encoder.c
@@ -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;

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

0 comments on commit ffe25df

Please sign in to comment.
You can’t perform that action at this time.