Skip to content

Commit 0b35979

Browse files
committed
[nrf fromtree] net: mqtt: Add MQTT 5.0 support for CONNECT
Add support for CONNECT message specified in MQTT 5.0, along with property encoders required to encode MQTT properties which is a new concept in MQTT 5.0. Connect and will properties can be specified by the application in the mqtt_client structure before connecting. Introduce a helper function which allows to verify whether MQTT 5.0 is used or not, so that it's still possible to use MQTT 3.1 even if MQTT 5.0 support is enabled in Kconfig. Signed-off-by: Robert Lubos <[email protected]> (cherry picked from commit 8fe2965)
1 parent 070f6fa commit 0b35979

File tree

5 files changed

+616
-23
lines changed

5 files changed

+616
-23
lines changed

include/zephyr/net/mqtt.h

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ enum mqtt_evt_type {
8484
/** @brief MQTT version protocol level. */
8585
enum mqtt_version {
8686
MQTT_VERSION_3_1_0 = 3, /**< Protocol level for 3.1.0. */
87-
MQTT_VERSION_3_1_1 = 4 /**< Protocol level for 3.1.1. */
87+
MQTT_VERSION_3_1_1 = 4, /**< Protocol level for 3.1.1. */
88+
MQTT_VERSION_5_0 = 5, /**< Protocol level for 5.0. */
8889
};
8990

9091
/** @brief MQTT Quality of Service types. */
@@ -185,6 +186,13 @@ struct mqtt_topic {
185186
uint8_t qos;
186187
};
187188

189+
/** @brief Abstracts MQTT UTF-8 encoded string pair.
190+
*/
191+
struct mqtt_utf8_pair {
192+
struct mqtt_utf8 name;
193+
struct mqtt_utf8 value;
194+
};
195+
188196
/** @brief Parameters for a publish message. */
189197
struct mqtt_publish_message {
190198
struct mqtt_topic topic; /**< Topic on which data was published. */
@@ -523,6 +531,32 @@ struct mqtt_client {
523531
*/
524532
struct mqtt_utf8 *password;
525533

534+
#if defined(CONFIG_MQTT_VERSION_5_0)
535+
/** MQTT 5.0 Will properties. */
536+
struct {
537+
/** MQTT 5.0, chapter 3.1.3.2.8 User Property. */
538+
struct mqtt_utf8_pair user_prop[CONFIG_MQTT_USER_PROPERTIES_MAX];
539+
540+
/** MQTT 5.0, chapter 3.1.3.2.5 Content Type. */
541+
struct mqtt_utf8 content_type;
542+
543+
/** MQTT 5.0, chapter 3.1.3.2.6 Response Topic. */
544+
struct mqtt_utf8 response_topic;
545+
546+
/** MQTT 5.0, chapter 3.1.3.2.7 Correlation Data. */
547+
struct mqtt_binstr correlation_data;
548+
549+
/** MQTT 5.0, chapter 3.1.3.2.2 Will Delay Interval. */
550+
uint32_t will_delay_interval;
551+
552+
/** MQTT 5.0, chapter 3.1.3.2.4 Message Expiry Interval. */
553+
uint32_t message_expiry_interval;
554+
555+
/** MQTT 5.0, chapter 3.1.3.2.3 Payload Format Indicator. */
556+
uint8_t payload_format_indicator;
557+
} will_prop;
558+
#endif /* CONFIG_MQTT_VERSION_5_0 */
559+
526560
/** Will topic and QoS. Can be NULL. */
527561
struct mqtt_topic *will_topic;
528562

@@ -558,6 +592,35 @@ struct mqtt_client {
558592
/** Unanswered PINGREQ count on this connection. */
559593
int8_t unacked_ping;
560594

595+
#if defined(CONFIG_MQTT_VERSION_5_0)
596+
/** MQTT 5.0 properties. */
597+
struct {
598+
/** MQTT 5.0, chapter 3.1.2.11.8 User Property. */
599+
struct mqtt_utf8_pair user_prop[CONFIG_MQTT_USER_PROPERTIES_MAX];
600+
601+
/** MQTT 5.0, chapter 3.1.2.11.9 Authentication Method. */
602+
struct mqtt_utf8 auth_method;
603+
604+
/** MQTT 5.0, chapter 3.1.2.11.10 Authentication Data. */
605+
struct mqtt_binstr auth_data;
606+
607+
/** MQTT 5.0, chapter 3.1.2.11.2 Session Expiry Interval. */
608+
uint32_t session_expiry_interval;
609+
610+
/** MQTT 5.0, chapter 3.1.2.11.4 Maximum Packet Size. */
611+
uint32_t maximum_packet_size;
612+
613+
/** MQTT 5.0, chapter 3.1.2.11.3 Receive Maximum. */
614+
uint16_t receive_maximum;
615+
616+
/** MQTT 5.0, chapter 3.1.2.11.6 Request Response Information. */
617+
bool request_response_info;
618+
619+
/** MQTT 5.0, chapter 3.1.2.11.7 Request Response Information. */
620+
bool request_problem_info;
621+
} prop;
622+
#endif /* CONFIG_MQTT_VERSION_5_0 */
623+
561624
/** Will retain flag, 1 if will message shall be retained persistently.
562625
*/
563626
uint8_t will_retain : 1;

subsys/net/lib/mqtt/Kconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,31 @@ config MQTT_CLEAN_SESSION
8181
the client. Setting this flag to 0 allows the client to create a
8282
persistent session.
8383

84+
#if MQTT_VERSION_5_0
85+
86+
config MQTT_USER_PROPERTIES_MAX
87+
int "Maximum number of user properties in a packet"
88+
default 1
89+
range 1 $(UINT16_MAX)
90+
help
91+
Maximum number of user properties that the client can include in a
92+
packet or parse on input.
93+
94+
config MQTT_TOPIC_ALIAS_MAX
95+
int "Maximum number of supported topic aliases"
96+
default 5
97+
range 0 $(UINT16_MAX)
98+
help
99+
Maximum number of supported topic aliases used in PUBLISH packets.
100+
If set to 0, topic aliasing is disabled.
101+
102+
config MQTT_TOPIC_ALIAS_STRING_MAX
103+
int "The longest topic name that can be aliased"
104+
default 64
105+
range 8 $(UINT16_MAX)
106+
help
107+
Specifies a size of a buffer for storing aliased topics.
108+
109+
#endif # MQTT_VERSION_5_0
110+
84111
endif # MQTT_LIB

subsys/net/lib/mqtt/mqtt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ void mqtt_client_init(struct mqtt_client *client)
176176
MQTT_STATE_INIT(client);
177177
mqtt_mutex_init(client);
178178

179-
client->protocol_version = MQTT_VERSION_3_1_1;
179+
client->protocol_version = IS_ENABLED(CONFIG_MQTT_VERSION_5_0) ?
180+
MQTT_VERSION_5_0 : MQTT_VERSION_3_1_1;
180181
client->clean_session = MQTT_CLEAN_SESSION;
181182
client->keepalive = MQTT_KEEPALIVE;
182183
}

0 commit comments

Comments
 (0)