ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
mqtt.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file mqtt.h
6 * @brief The MQTT Client (PROTOCORE_ENABLE_MQTT).
7 *
8 * The governing document is "MQTT Version 3.1.1", an **OASIS Standard** dated 29 October 2014,
9 * amended by "MQTT Version 3.1.1 Plus Errata 01" (OASIS Standard Incorporating Approved Errata 01,
10 * 10 December 2015). MQTT is not an IETF protocol and carries no RFC number; every section cited
11 * below is a section of that OASIS document. The same text is published as ISO/IEC 20922:2016.
12 *
13 * The Client and the Server exchange MQTT Control Packets over a Network Connection (sec 4.2). Each
14 * packet is a fixed header (sec 2.2) carrying the Control Packet type (sec 2.2.1), four type-specific
15 * flag bits (sec 2.2.2) and a Remaining Length (sec 2.2.3), then an optional variable header and
16 * payload.
17 *
18 * Two halves behind one handle:
19 *
20 * - The codec builds and reads packet octets in the caller's buffers and holds nothing, so it is
21 * unit-tested on the host.
22 * - The transport drives one Network Connection over the outbound TCP client, and `mqtts://` over
23 * the shared persistent client TLS session. No heap; one Server at a time.
24 *
25 * QoS 2 runs the four-packet PUBLISH / PUBREC / PUBREL / PUBCOMP exchange (sec 4.3.3). Outbound
26 * QoS 1 and QoS 2 messages sit in a fixed in-flight pool and are re-delivered with DUP set
27 * (sec 3.3.1.1) until acknowledged. An inbound QoS 2 Packet Identifier is held from PUBREC until
28 * PUBCOMP so a repeat delivers the Application Message once (sec 4.3.3).
29 *
30 * The module exports one symbol, @ref Mqtt. Everything in mqtt.c has internal linkage.
31 *
32 * @author Douglas Quigg (dstroy0)
33 * @date 2026
34 */
35
36#ifndef PROTOCORE_MQTT_H
37#define PROTOCORE_MQTT_H
38
39#include "protocore_config.h" // the entry point: protocore_types.h for the widths
40
41#if PROTOCORE_ENABLE_MQTT
42
43EMBED_BEGIN_DECLS
44
45// ---------------------------------------------------------------------------
46// Literals
47// ---------------------------------------------------------------------------
48
49/** @brief The largest Remaining Length a four-octet field encodes (MQTT 3.1.1 sec 2.2.3). */
50#define PROTOCORE_MQTT_REMAINING_LENGTH_MAX 268435455u
51
52/** @brief The Protocol Level a 3.1.1 CONNECT carries (MQTT 3.1.1 sec 3.1.2.2). */
53#define PROTOCORE_MQTT_PROTOCOL_LEVEL 0x04
54
55/** @brief The SUBACK return code that reports a failed subscription (MQTT 3.1.1 sec 3.9.3). */
56#define PROTOCORE_MQTT_SUBACK_FAILURE 0x80
57
58// ---------------------------------------------------------------------------
59// Typedefs
60// ---------------------------------------------------------------------------
61
62/** @brief MQTT Control Packet type, byte 1 bits 7-4 (MQTT 3.1.1 sec 2.2.1, Table 2.1). */
63EMBED_ENUM_BYTE(MqttType,
64 MQTT_CONNECT = 1, ///< Client request to connect to a Server
65 MQTT_CONNACK = 2, ///< Connect acknowledgment
66 MQTT_PUBLISH = 3, ///< Publish message
67 MQTT_PUBACK = 4, ///< Publish acknowledgment
68 MQTT_PUBREC = 5, ///< Publish received (QoS 2 publish received, part 1)
69 MQTT_PUBREL = 6, ///< Publish release (QoS 2 publish received, part 2)
70 MQTT_PUBCOMP = 7, ///< Publish complete (QoS 2 publish received, part 3)
71 MQTT_SUBSCRIBE = 8, ///< Client subscribe request
72 MQTT_SUBACK = 9, ///< Subscribe acknowledgment
73 MQTT_UNSUBSCRIBE = 10, ///< Unsubscribe request
74 MQTT_UNSUBACK = 11, ///< Unsubscribe acknowledgment
75 MQTT_PINGREQ = 12, ///< PING request
76 MQTT_PINGRESP = 13, ///< PING response
77 MQTT_DISCONNECT = 14, ///< Client is disconnecting
78);
79
80/** @brief Where an inbound PUBLISH's Topic Name and Payload are delivered (MQTT 3.1.1 sec 3.3). */
81typedef void (*MqttMessageCb)(const char *topic_name, const uint8_t *payload, size_t payload_len);
82
83/** @brief MQTT 3.1.1 sec 4.2: the Network Connection this Client opens to a Server. */
84typedef struct
85{
86 const char *host; ///< the Server's name; read on every connect step, so it outlives the call
87 uint16_t port; ///< its TCP port
88 embed_bool use_tls; ///< the Network Connection runs over TLS (`mqtts://`)
89} MqttServerArgs;
90
91/**
92 * @brief MQTT 3.1.1 sec 3.1: the CONNECT variable header and payload, less the Will.
93 *
94 * A null @c user_name clears the User Name flag (sec 3.1.2.8) and a null @c password clears the
95 * Password flag (sec 3.1.2.9), so neither field is written into the payload.
96 */
97typedef struct
98{
99 const char *client_id; ///< Client Identifier (sec 3.1.3.1); "" asks the Server to assign one
100 const char *user_name; ///< User Name (sec 3.1.3.4), or null for none
101 const char *password; ///< Password (sec 3.1.3.5), or null for none
102 uint16_t keep_alive; ///< Keep Alive seconds (sec 3.1.2.10); 0 turns the mechanism off
103 embed_bool clean_session; ///< Clean Session (sec 3.1.2.4)
104} MqttSessionArgs;
105
106/**
107 * @brief MQTT 3.1.1 sec 3.1.2.5 - sec 3.1.2.7, sec 3.1.3.2, sec 3.1.3.3: the Will a CONNECT carries.
108 *
109 * A null @c topic clears the Will Flag, and with it Will QoS and Will Retain (sec 3.1.2.5).
110 */
111typedef struct
112{
113 const char *topic; ///< Will Topic (sec 3.1.3.2), or null for no Will
114 const uint8_t *message; ///< Will Message (sec 3.1.3.3); may be null when @c message_len is 0
115 size_t message_len; ///< its octet count
116 uint8_t qos; ///< Will QoS, 0 to 2 (sec 3.1.2.6)
117 embed_bool retain; ///< Will Retain (sec 3.1.2.7)
118} MqttWillArgs;
119
120/**
121 * @brief MQTT 3.1.1 sec 3.3: a PUBLISH's Topic Name, Payload and fixed-header flags.
122 *
123 * @c topic_name is what a build writes; @c topic_out is where a parse copies the Topic Name it read,
124 * NUL terminated, with @c topic_len reporting its octet count.
125 */
126typedef struct
127{
128 const char *topic_name; ///< Topic Name a build writes (sec 3.3.2.1); no wildcards (MQTT-3.3.2-2)
129 char *topic_out; ///< where a parse copies the Topic Name it read
130 size_t topic_cap; ///< its room, the NUL included
131 size_t topic_len; ///< the Topic Name octets a parse copied
132 const uint8_t *payload; ///< Payload a build writes, or where a parse found it inside @c in (sec 3.3.3)
133 size_t payload_len; ///< its octet count
134 uint8_t qos; ///< QoS level, 0 to 2 (sec 3.3.1.2)
135 embed_bool retain; ///< RETAIN (sec 3.3.1.3)
136 embed_bool dup; ///< DUP, set on a re-delivery (sec 3.3.1.1)
137} MqttPublishArgs;
138
139/** @brief MQTT 3.1.1 sec 3.8.3, sec 3.10.3: the Topic Filter a SUBSCRIBE or UNSUBSCRIBE names. */
140typedef struct
141{
142 const char *topic_filter; ///< Topic Filter (sec 4.7); wildcards are allowed here
143 uint8_t qos; ///< Requested QoS, 0 to 2 (sec 3.8.3.1)
144} MqttFilterArgs;
145
146/** @brief MQTT 3.1.1 sec 2.2, sec 2.3.1: the fixed header a build stamps or a parse reads. */
147typedef struct
148{
149 MqttType type; ///< MQTT Control Packet type (sec 2.2.1)
150 uint8_t flags; ///< the type-specific flags, byte 1 bits 3-0 (sec 2.2.2)
151 uint32_t remaining_length; ///< Remaining Length (sec 2.2.3)
152 uint16_t packet_id; ///< Packet Identifier (sec 2.3.1); never 0 on the wire
153} MqttPacketArgs;
154
155/**
156 * @brief The octets a codec call writes or reads.
157 *
158 * A build assembles the variable header and payload in @c body, because the fixed header's Remaining
159 * Length is not known until that part is finished, then composes the whole Control Packet into
160 * @c out. The codec declares no storage, so the caller lends both.
161 */
162typedef struct
163{
164 uint8_t *out; ///< where a build writes the whole Control Packet
165 size_t cap; ///< its room
166 uint8_t *body; ///< scratch the variable header and payload assemble in
167 size_t body_cap; ///< its room
168 const uint8_t *in; ///< the octets a parse reads
169 size_t avail; ///< how many are readable there
170} MqttBufArgs;
171
172/** @brief Where the Client hands an inbound Application Message on (MQTT 3.1.1 sec 3.3). */
173typedef struct
174{
175 MqttMessageCb on_message; ///< the Topic Name and Payload sink; null delivers nowhere
176} MqttDeliveryArgs;
177
178/**
179 * @brief The MQTT Client (OASIS MQTT Version 3.1.1).
180 *
181 * A caller sets the members a call takes, invokes it through ::Mqtt, and reads the outcome off the
182 * same handle. The session, its in-flight window and its buffers are behind @ref internal.
183 *
184 * No slot member: this Client holds one Network Connection at a time (sec 4.2), so no call names a
185 * row.
186 *
187 * @var MqttNs::server the Network Connection this Client opens (sec 4.2)
188 * @var MqttNs::session the CONNECT variable header and payload (sec 3.1)
189 * @var MqttNs::will the Will a CONNECT carries (sec 3.1.2.5, sec 3.1.3.2, sec 3.1.3.3)
190 * @var MqttNs::message a PUBLISH's Topic Name, Payload and flags (sec 3.3)
191 * @var MqttNs::filter the Topic Filter a SUBSCRIBE or UNSUBSCRIBE names (sec 4.7)
192 * @var MqttNs::packet the fixed header a build stamps or a parse reads (sec 2.2, sec 2.3.1)
193 * @var MqttNs::buf the octets a codec call writes or reads
194 * @var MqttNs::delivery where an inbound Application Message is handed on (sec 3.3)
195 * @var MqttNs::ok a call's true/false outcome
196 * @var MqttNs::n
197 * An octet count: the Control Packet a build wrote, the fixed header a parse read, or the Remaining
198 * Length field an encode or decode covered. 0 when a build did not fit @c cap or @c body_cap.
199 * @var MqttNs::i32 the Connect Return code a CONNACK carried (sec 3.2.2.3), or -1 if malformed
200 * @var MqttNs::u8 the first return code a SUBACK carried (sec 3.9.3)
201 * @var MqttNs::session_present the Session Present flag a CONNACK carried (sec 3.2.2.2)
202 * @var MqttNs::encode_remaining_length
203 * Write @c packet.remaining_length into @c out as a Remaining Length field of 1 to 4 octets
204 * (sec 2.2.3). @c n reports the octets written, 0 above ::PROTOCORE_MQTT_REMAINING_LENGTH_MAX or
205 * when the field would not fit @c cap.
206 * @var MqttNs::decode_remaining_length
207 * Read a Remaining Length field from @c in into @c packet.remaining_length, @c n reporting the octets
208 * it consumed. False when the field is incomplete or runs past four octets (sec 2.2.3).
209 * @var MqttNs::build_connect
210 * Build a CONNECT from @c session and @c will: Protocol Name "MQTT", Protocol Level 4, Connect Flags,
211 * Keep Alive, then the payload's Client Identifier, Will Topic, Will Message, User Name and Password
212 * (sec 3.1).
213 * @var MqttNs::build_publish
214 * Build a PUBLISH from @c message, its Packet Identifier taken from @c packet.packet_id when
215 * @c message.qos is above 0 (sec 3.3). A Topic Name holding `+` or `#` is refused (MQTT-3.3.2-2).
216 * @var MqttNs::build_subscribe
217 * Build a SUBSCRIBE carrying @c packet.packet_id and one Topic Filter at @c filter.qos, with the
218 * fixed-header flags the spec reserves as 0,0,1,0 (sec 3.8.1, sec 3.8.3).
219 * @var MqttNs::build_unsubscribe
220 * Build an UNSUBSCRIBE carrying @c packet.packet_id and one Topic Filter, with the fixed-header flags
221 * the spec reserves as 0,0,1,0 (sec 3.10.1, sec 3.10.3).
222 * @var MqttNs::build_ack
223 * Build the four-octet PUBACK, PUBREC, PUBREL or PUBCOMP named by @c packet.type carrying
224 * @c packet.packet_id (sec 3.4 - sec 3.7). PUBREL takes the reserved flags 0,0,1,0 (sec 3.6.1).
225 * @var MqttNs::build_pingreq build the two-octet PINGREQ (sec 3.12)
226 * @var MqttNs::build_disconnect build the two-octet DISCONNECT (sec 3.14)
227 * @var MqttNs::parse_fixed_header
228 * Read the fixed header at @c in into @c packet.type, @c packet.flags and @c packet.remaining_length,
229 * @c n reporting its size (sec 2.2). False until @c avail holds the whole header.
230 * @var MqttNs::parse_publish
231 * Read the @c packet.remaining_length octets after a PUBLISH fixed header: copy the Topic Name into
232 * @c message.topic_out, point @c message.payload at the Payload, and take the Packet Identifier into
233 * @c packet.packet_id when @c packet.flags carries a QoS above 0 (sec 3.3). False on a malformed
234 * Topic Name (sec 1.5.3) or on both QoS bits set (MQTT-3.3.1-4).
235 * @var MqttNs::parse_ack
236 * Read the Packet Identifier from a PUBACK, PUBREC, PUBREL, PUBCOMP or UNSUBACK body into
237 * @c packet.packet_id (sec 2.3.1). 0 reports a malformed body, since no real identifier is 0.
238 * @var MqttNs::parse_connack
239 * Read a CONNACK body into @c session_present (sec 3.2.2.2) and @c i32, the Connect Return code
240 * (sec 3.2.2.3). @c i32 is -1 when the body is malformed.
241 * @var MqttNs::parse_suback
242 * Read a SUBACK body into @c packet.packet_id and @c u8, the first return code of the payload list
243 * (sec 3.9.2, sec 3.9.3). ::PROTOCORE_MQTT_SUBACK_FAILURE there is a refused subscription.
244 * @var MqttNs::on_message record @c delivery.on_message; call it before @ref MqttNs::connect
245 * @var MqttNs::connect
246 * Open the Network Connection to @c server and frame the CONNECT built from @c session and @c will.
247 * Returns straight away: @ref MqttNs::loop steps the transport, then the handshake, then the CONNACK,
248 * and gives the whole thing up past ::PROTOCORE_MQTT_CONNECT_MS. @c session and @c will are read only
249 * during this call.
250 * @var MqttNs::publish
251 * Send @c message as a PUBLISH (sec 3.3). QoS 0 goes out and is forgotten; QoS 1 and QoS 2 take an
252 * in-flight slot and are re-delivered with DUP until acknowledged (sec 4.3.2, sec 4.3.3).
253 * @var MqttNs::subscribe send a SUBSCRIBE for @c filter (sec 3.8)
254 * @var MqttNs::unsubscribe send an UNSUBSCRIBE for @c filter.topic_filter (sec 3.10)
255 * @var MqttNs::loop
256 * Pump the Network Connection: read inbound Control Packets, deliver PUBLISH to
257 * @c delivery.on_message and run the QoS 1 and QoS 2 acknowledgement flows, re-deliver unacknowledged
258 * in-flight messages, and send PINGREQ when Keep Alive is due (sec 3.1.2.10). Call once per loop().
259 * False once the Network Connection is gone.
260 * @var MqttNs::connected true while the Server accepted the CONNECT
261 * @var MqttNs::disconnect
262 * Send DISCONNECT and close the Network Connection (sec 3.14).
263 */
264typedef struct
265{
266 MqttServerArgs server; ///< the Network Connection this Client opens
267 MqttSessionArgs session; ///< what a CONNECT states about the session
268 MqttWillArgs will; ///< the Will a CONNECT carries
269 MqttPublishArgs message; ///< a PUBLISH's Topic Name, Payload and flags
270 MqttFilterArgs filter; ///< the Topic Filter a SUBSCRIBE or UNSUBSCRIBE names
271 MqttPacketArgs packet; ///< the fixed header a build stamps or a parse reads
272 MqttBufArgs buf; ///< the octets a codec call moves
273 MqttDeliveryArgs delivery; ///< where an inbound Application Message is handed on
274 embed_bool ok;
275 size_t n;
276 int32_t i32;
277 uint8_t u8;
278 embed_bool session_present;
279} MqttVars;
280
281/** @brief The operands and the outcome. */
282extern MqttVars MqttV;
283
284/** @brief The entries. */
285typedef struct
286{
287 void (*const encode_remaining_length)(const uint8_t *work);
288 void (*const decode_remaining_length)(const uint8_t *work);
289 void (*const build_connect)(const uint8_t *work);
290 void (*const build_publish)(const uint8_t *work);
291 void (*const build_subscribe)(const uint8_t *work);
292 void (*const build_unsubscribe)(const uint8_t *work);
293 void (*const build_ack)(const uint8_t *work);
294 void (*const build_pingreq)(const uint8_t *work);
295 void (*const build_disconnect)(const uint8_t *work);
296 void (*const parse_fixed_header)(const uint8_t *work);
297 void (*const parse_publish)(const uint8_t *work);
298 void (*const parse_ack)(const uint8_t *work);
299 void (*const parse_connack)(const uint8_t *work);
300 void (*const parse_suback)(const uint8_t *work);
301 void (*const on_message)(const uint8_t *work);
302 void (*const connect)(const uint8_t *work);
303 void (*const publish)(const uint8_t *work);
304 void (*const subscribe)(const uint8_t *work);
305 void (*const unsubscribe)(const uint8_t *work);
306 void (*const loop)(const uint8_t *work);
307 void (*const connected)(const uint8_t *work);
308 void (*const disconnect)(const uint8_t *work);
309} MqttNs;
310
311// What the table binds, defined once in the .c and taking one parameter each: everything
312// else an entry needs is an operand in MqttV or a region of the borrow at a fixed offset.
313void protocore_mqtt_encode_remaining_length(const uint8_t *work);
314void protocore_mqtt_decode_remaining_length(const uint8_t *work);
315void protocore_mqtt_build_connect(const uint8_t *work);
316void protocore_mqtt_build_publish(const uint8_t *work);
317void protocore_mqtt_build_subscribe(const uint8_t *work);
318void protocore_mqtt_build_unsubscribe(const uint8_t *work);
319void protocore_mqtt_build_ack(const uint8_t *work);
320void protocore_mqtt_build_pingreq(const uint8_t *work);
321void protocore_mqtt_build_disconnect(const uint8_t *work);
322void protocore_mqtt_parse_fixed_header(const uint8_t *work);
323void protocore_mqtt_parse_publish(const uint8_t *work);
324void protocore_mqtt_parse_ack(const uint8_t *work);
325void protocore_mqtt_parse_connack(const uint8_t *work);
326void protocore_mqtt_parse_suback(const uint8_t *work);
327void protocore_mqtt_on_message(const uint8_t *work);
328void protocore_mqtt_connect(const uint8_t *work);
329void protocore_mqtt_publish(const uint8_t *work);
330void protocore_mqtt_subscribe(const uint8_t *work);
331void protocore_mqtt_unsubscribe(const uint8_t *work);
332void protocore_mqtt_loop(const uint8_t *work);
333void protocore_mqtt_connected(const uint8_t *work);
334void protocore_mqtt_disconnect(const uint8_t *work);
335
336// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
337// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
338// `Mqtt.encode_remaining_length(work)` resolves to a named function and becomes a DIRECT call. An extern table
339// leaves the call indirect and the symbol live at every level, -O2 -flto included.
340static const MqttNs Mqtt __attribute__((unused)) = {
341 .encode_remaining_length = protocore_mqtt_encode_remaining_length,
342 .decode_remaining_length = protocore_mqtt_decode_remaining_length,
343 .build_connect = protocore_mqtt_build_connect,
344 .build_publish = protocore_mqtt_build_publish,
345 .build_subscribe = protocore_mqtt_build_subscribe,
346 .build_unsubscribe = protocore_mqtt_build_unsubscribe,
347 .build_ack = protocore_mqtt_build_ack,
348 .build_pingreq = protocore_mqtt_build_pingreq,
349 .build_disconnect = protocore_mqtt_build_disconnect,
350 .parse_fixed_header = protocore_mqtt_parse_fixed_header,
351 .parse_publish = protocore_mqtt_parse_publish,
352 .parse_ack = protocore_mqtt_parse_ack,
353 .parse_connack = protocore_mqtt_parse_connack,
354 .parse_suback = protocore_mqtt_parse_suback,
355 .on_message = protocore_mqtt_on_message,
356 .connect = protocore_mqtt_connect,
357 .publish = protocore_mqtt_publish,
358 .subscribe = protocore_mqtt_subscribe,
359 .unsubscribe = protocore_mqtt_unsubscribe,
360 .loop = protocore_mqtt_loop,
361 .connected = protocore_mqtt_connected,
362 .disconnect = protocore_mqtt_disconnect,
363};
364
365/**
366 * @brief The PROTOCORE_MQTT_BORROW bytes this module's state lives in.
367 *
368 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
369 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
370 * walks, so the state lasts the life of the program.
371 *
372 * @return the span.
373 */
374#if PROTOCORE_HAS_NET_STACK
375uint8_t *protocore_mqtt_span(void);
376#endif // PROTOCORE_HAS_NET_STACK
377
378EMBED_END_DECLS
379
380#endif // PROTOCORE_ENABLE_MQTT
381
382#endif // PROTOCORE_MQTT_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.