From c0b50c72be1473a0765d9a2b30ea799803b7a0c5 Mon Sep 17 00:00:00 2001 From: Marcin Niestroj Date: Thu, 12 Mar 2020 17:45:44 +0100 Subject: [PATCH] net: mqtt: use MQTT_UTF8_LITERAL() to simplify code and fix debug log Contents of mqtt_3_1_0_proto_desc and mqtt_3_1_1_proto_desc were logged with following code: MQTT_TRC("Encoding Protocol Description. Str:%s Size:%08x.", mqtt_proto_desc->utf8, mqtt_proto_desc->size); This resulted in invalid log, since they were not NULL-terminated strings. Use MQTT_UTF8_LITERAL() to initialize both utf8 strings to make sure they are NULL-terminated now and valid to print and log. Additionally this makes the code a bit shorter. Signed-off-by: Marcin Niestroj --- subsys/net/lib/mqtt/mqtt_encoder.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/subsys/net/lib/mqtt/mqtt_encoder.c b/subsys/net/lib/mqtt/mqtt_encoder.c index ce63df0b08b..4c70394eb00 100644 --- a/subsys/net/lib/mqtt/mqtt_encoder.c +++ b/subsys/net/lib/mqtt/mqtt_encoder.c @@ -15,25 +15,11 @@ LOG_MODULE_REGISTER(net_mqtt_enc, CONFIG_MQTT_LOG_LEVEL); #include "mqtt_internal.h" #include "mqtt_os.h" -#define MQTT_3_1_0_PROTO_DESC_LEN 6 -#define MQTT_3_1_1_PROTO_DESC_LEN 4 +static const struct mqtt_utf8 mqtt_3_1_0_proto_desc = + MQTT_UTF8_LITERAL("MQIsdp"); -static const u8_t mqtt_3_1_0_proto_desc_str[MQTT_3_1_0_PROTO_DESC_LEN] = { - 'M', 'Q', 'I', 's', 'd', 'p' -}; -static const u8_t mqtt_3_1_1_proto_desc_str[MQTT_3_1_1_PROTO_DESC_LEN] = { - 'M', 'Q', 'T', 'T' -}; - -static const struct mqtt_utf8 mqtt_3_1_0_proto_desc = { - .utf8 = (u8_t *)mqtt_3_1_0_proto_desc_str, - .size = MQTT_3_1_0_PROTO_DESC_LEN -}; - -static const struct mqtt_utf8 mqtt_3_1_1_proto_desc = { - .utf8 = (u8_t *)mqtt_3_1_1_proto_desc_str, - .size = MQTT_3_1_1_PROTO_DESC_LEN -}; +static const struct mqtt_utf8 mqtt_3_1_1_proto_desc = + MQTT_UTF8_LITERAL("MQTT"); /** Never changing ping request, needed for Keep Alive. */ static const u8_t ping_packet[MQTT_FIXED_HEADER_MIN_SIZE] = {