Skip to content

Commit

Permalink
mqtt: Allow client to override keepalive
Browse files Browse the repository at this point in the history
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 28, 2019
1 parent fb898e3 commit ffe25df
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions include/net/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions subsys/net/lib/mqtt/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions subsys/net/lib/mqtt/mqtt_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit ffe25df

Please sign in to comment.