From fa087cb2fe0aa9e15dd6037a8c32f5e95c5e1aa1 Mon Sep 17 00:00:00 2001 From: Tijn Kooijmans Date: Fri, 21 Jul 2017 21:39:24 +0200 Subject: [PATCH] implemented support for non-string type last will messages --- include/mqtt.h | 1 + include/mqtt_msg.h | 1 + mqtt.c | 6 +++++- mqtt_msg.c | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index 44d2428..d4bc2da 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -58,6 +58,7 @@ typedef struct mqtt_settings { char password[CONFIG_MQTT_MAX_PASSWORD_LEN]; char lwt_topic[CONFIG_MQTT_MAX_LWT_TOPIC]; char lwt_msg[CONFIG_MQTT_MAX_LWT_MSG]; + uint32_t lwt_msg_len; uint32_t lwt_qos; uint32_t lwt_retain; uint32_t clean_session; diff --git a/include/mqtt_msg.h b/include/mqtt_msg.h index 44da0cc..758aa51 100644 --- a/include/mqtt_msg.h +++ b/include/mqtt_msg.h @@ -93,6 +93,7 @@ typedef struct mqtt_connect_info char* will_topic; char* will_message; int keepalive; + int will_length; int will_qos; int will_retain; int clean_session; diff --git a/mqtt.c b/mqtt.c index 75bc4b5..ab85ce2 100644 --- a/mqtt.c +++ b/mqtt.c @@ -529,6 +529,10 @@ mqtt_client *mqtt_start(mqtt_settings *settings) } memset(client, 0, sizeof(mqtt_client)); + if (settings->lwt_msg_len > CONFIG_MQTT_MAX_LWT_MSG) { + mqtt_error("Last will message longer than CONFIG_MQTT_MAX_LWT_MSG!"); + } + client->settings = settings; client->connect_info.client_id = settings->client_id; client->connect_info.username = settings->username; @@ -537,7 +541,7 @@ mqtt_client *mqtt_start(mqtt_settings *settings) client->connect_info.will_message = settings->lwt_msg; client->connect_info.will_qos = settings->lwt_qos; client->connect_info.will_retain = settings->lwt_retain; - + client->connect_info.will_length = settings->lwt_msg_len; client->keepalive_tick = settings->keepalive / 2; diff --git a/mqtt_msg.c b/mqtt_msg.c index b88ca4e..5be6fa3 100644 --- a/mqtt_msg.c +++ b/mqtt_msg.c @@ -328,7 +328,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf if (append_string(connection, info->will_topic, strlen(info->will_topic)) < 0) return fail_message(connection); - if (append_string(connection, info->will_message, strlen(info->will_message)) < 0) + if (append_string(connection, info->will_message, info->will_length) < 0) return fail_message(connection); variable_header->flags |= MQTT_CONNECT_FLAG_WILL;