From accef325a2f2e1016b4f317adb9f98eab27a9857 Mon Sep 17 00:00:00 2001 From: Marcin Niestroj Date: Thu, 12 Mar 2020 17:41:14 +0100 Subject: [PATCH] net: mqtt: add MQTT_UTF8_LITERAL() helper macro This macro allows to easily initialize utf8 strings (struct mqtt_utf8) from C literals, as it automatically calculates string length. Signed-off-by: Marcin Niestroj --- include/net/mqtt.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/net/mqtt.h b/include/net/mqtt.h index dd295ef2eae..a78debe02de 100644 --- a/include/net/mqtt.h +++ b/include/net/mqtt.h @@ -154,6 +154,18 @@ struct mqtt_utf8 { u32_t size; /**< Size of UTF string, in bytes. */ }; +/** + * @brief Initialize UTF-8 encoded string from C literal string. + * + * Use it as follows: + * + * struct mqtt_utf8 password = MQTT_UTF8_LITERAL("my_pass"); + * + * @param[in] literal Literal string from which to generate mqtt_utf8 object. + */ +#define MQTT_UTF8_LITERAL(literal) \ + ((struct mqtt_utf8) {literal, sizeof(literal) - 1}) + /** @brief Abstracts binary strings. */ struct mqtt_binstr { u8_t *data; /**< Pointer to binary stream. */