mqtt_sn_common.h
Go to the documentation of this file.
1 /**
2  * @file mqtt_sn_common.h
3  * @brief Definitions common to MQTT-SN client and gateway
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _MQTT_SN_COMMON_H
32 #define _MQTT_SN_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //MQTT-SN port number
38 #define MQTT_SN_PORT 1883
39 
40 //C++ guard
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 //MQTT-SN protocol identifier
46 #define MQTT_SN_PROTOCOL_ID 0x01
47 //Reserved topic identifier
48 #define MQTT_SN_INVALID_TOPIC_ID 0x0000
49 
50 
51 /**
52  * @brief MQTT-SN transport protocols
53  **/
54 
55 typedef enum {
56  MQTT_SN_TRANSPORT_PROTOCOL_UDP = 1, ///<UDP protocol
57  MQTT_SN_TRANSPORT_PROTOCOL_DTLS = 2 ///<DTLS protocol
59 
60 
61 /**
62  * @brief Quality of service level
63  **/
64 
65 typedef enum
66 {
67  MQTT_SN_QOS_LEVEL_0 = 0, ///<At most once delivery
68  MQTT_SN_QOS_LEVEL_1 = 1, ///<At least once delivery
69  MQTT_SN_QOS_LEVEL_2 = 2, ///<Exactly once delivery
70  MQTT_SN_QOS_LEVEL_MINUS_1 = 3 ///<No connection setup
72 
73 
74 /**
75  * @brief Topic ID types
76  **/
77 
78 typedef enum
79 {
80  MQTT_SN_NORMAL_TOPIC_ID = 0, ///<Normal topic ID
81  MQTT_SN_PREDEFINED_TOPIC_ID = 1, ///<Predefined topic ID
82  MQTT_SN_NORMAL_TOPIC_NAME = 0, ///<Normal topic name
83  MQTT_SN_SHORT_TOPIC_NAME = 2 ///<Short topic name
85 
86 
87 /**
88  * @brief MQTT-SN message types
89  **/
90 
91 typedef enum
92 {
122 
123 
124 /**
125  * @brief MQTT-SN return codes
126  **/
127 
128 typedef enum
129 {
135 
136 
137 //CC-RX, CodeWarrior or Win32 compiler?
138 #if defined(__CCRX__)
139  #pragma pack
140 #elif defined(__CWCC__) || defined(_WIN32)
141  #pragma pack(push, 1)
142 #endif
143 
144 
145 /**
146  * @brief MQTT-SN flags
147  **/
148 
150 {
151  __packed_union
152  {
153  uint8_t all;
155  {
156 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
157  uint8_t dup : 1;
158  uint8_t qos : 2;
159  uint8_t retain : 1;
160  uint8_t will : 1;
161  uint8_t cleanSession : 1;
162  uint8_t topicIdType : 2;
163 #else
164  uint8_t topicIdType : 2;
165  uint8_t cleanSession : 1;
166  uint8_t will : 1;
167  uint8_t retain : 1;
168  uint8_t qos : 2;
169  uint8_t dup : 1;
170 #endif
171  };
172  };
174 
175 
176 /**
177  * @brief Message header
178  **/
179 
180 typedef __packed_struct
181 {
182  uint8_t length;
183  uint8_t msgType;
184  uint8_t data[];
186 
187 
188 /**
189  * @brief Extended message header
190  **/
191 
192 typedef __packed_struct
193 {
194  uint8_t prefix;
195  uint16_t length;
196  uint8_t msgType;
197  uint8_t data[];
199 
200 
201 /**
202  * @brief ADVERTISE message
203  **/
204 
205 typedef __packed_struct
206 {
207  uint8_t gwId;
208  uint16_t duration;
210 
211 
212 /**
213  * @brief SEARCHGW message
214  **/
215 
216 typedef __packed_struct
217 {
218  uint8_t radius;
220 
221 
222 /**
223  * @brief GWINFO message
224  **/
225 
226 typedef __packed_struct
227 {
228  uint8_t gwId;
229  uint8_t gwAdd[];
231 
232 
233 /**
234  * @brief CONNECT message
235  **/
236 
237 typedef __packed_struct
238 {
240  uint8_t protocolId;
241  uint16_t duration;
244 
245 
246 /**
247  * @brief CONNACK message
248  **/
249 
250 typedef __packed_struct
251 {
252  uint8_t returnCode;
254 
255 
256 /**
257  * @brief WILLTOPICREQ message
258  **/
259 
260 typedef void MqttSnWillTopicReq;
261 
262 
263 /**
264  * @brief WILLTOPIC message
265  **/
266 
267 typedef __packed_struct
268 {
272 
273 
274 /**
275  * @brief WILLMSGREQ message
276  **/
277 
278 typedef void MqttSnWillMsgReq;
279 
280 
281 /**
282  * @brief WILLMSG message
283  **/
284 
285 typedef void MqttSnWillMsg;
286 
287 
288 /**
289  * @brief REGISTER message
290  **/
291 
292 typedef __packed_struct
293 {
294  uint16_t topicId;
295  uint16_t msgId;
298 
299 
300 /**
301  * @brief REGACK message
302  **/
303 
304 typedef __packed_struct
305 {
306  uint16_t topicId;
307  uint16_t msgId;
308  uint8_t returnCode;
310 
311 
312 /**
313  * @brief PUBLISH message
314  **/
315 
316 typedef __packed_struct
317 {
319  uint16_t topicId;
320  uint16_t msgId;
321  uint8_t data[];
323 
324 
325 /**
326  * @brief PUBACK message
327  **/
328 
329 typedef __packed_struct
330 {
331  uint16_t topicId;
332  uint16_t msgId;
333  uint8_t returnCode;
335 
336 
337 /**
338  * @brief PUBREC message
339  **/
340 
341 typedef __packed_struct
342 {
343  uint16_t msgId;
345 
346 
347 /**
348  * @brief PUBREL message
349  **/
350 
351 typedef __packed_struct
352 {
353  uint16_t msgId;
355 
356 
357 /**
358  * @brief PUBCOMP message
359  **/
360 
361 typedef __packed_struct
362 {
363  uint16_t msgId;
365 
366 
367 /**
368  * @brief SUBSCRIBE message
369  **/
370 
371 typedef __packed_struct
372 {
374  uint16_t msgId;
375  char_t topicName[];
377 
378 
379 /**
380  * @brief SUBACK message
381  **/
382 
383 typedef __packed_struct
384 {
386  uint16_t topicId;
387  uint16_t msgId;
388  uint8_t returnCode;
390 
391 
392 /**
393  * @brief UNSUBSCRIBE message
394  **/
395 
396 typedef __packed_struct
397 {
399  uint16_t msgId;
400  char_t topicName[];
402 
403 
404 /**
405  * @brief UNSUBACK message
406  **/
407 
408 typedef __packed_struct
409 {
410  uint16_t msgId;
412 
413 
414 /**
415  * @brief PINGREQ message
416  **/
417 
418 typedef void MqttSnPingReq;
419 
420 
421 /**
422  * @brief PINGRESP message
423  **/
424 
425 typedef void MqttSnPingResp;
426 
427 
428 /**
429  * @brief DISCONNECT message
430  **/
431 
432 typedef __packed_struct
433 {
434  uint16_t duration;
436 
437 
438 /**
439  * @brief WILLTOPICUPD message
440  **/
441 
442 typedef __packed_struct
443 {
445  char_t willTopic[];
447 
448 
449 /**
450  * @brief WILLTOPICRESP message
451  **/
452 
453 typedef __packed_struct
454 {
455  uint8_t returnCode;
457 
458 
459 /**
460  * @brief WILLMSGUPD message
461  **/
462 
463 typedef void MqttSnWillMsgUpd;
464 
465 
466 /**
467  * @brief WILLMSGRESP message
468  **/
469 
470 typedef __packed_struct
471 {
472  uint8_t returnCode;
474 
475 
476 //CC-RX, CodeWarrior or Win32 compiler?
477 #if defined(__CCRX__)
478  #pragma unpack
479 #elif defined(__CWCC__) || defined(_WIN32)
480  #pragma pack(pop)
481 #endif
482 
483 
484 /**
485  * @brief Predefined topic
486  **/
487 
488 typedef struct
489 {
490  const char_t *topicName; ///<Topic name
491  uint16_t topicId; ///<Topic identifier
493 
494 
495 //C++ guard
496 #ifdef __cplusplus
497 }
498 #endif
499 
500 #endif
char char_t
Definition: compiler_port.h:48
Ipv6Addr prefix
uint8_t dup
Definition: mqtt_common.h:182
uint8_t qos
Definition: mqtt_common.h:181
MqttSnMsgType
MQTT-SN message types.
@ MQTT_SN_MSG_TYPE_WILLTOPICRESP
@ MQTT_SN_MSG_TYPE_SUBACK
@ MQTT_SN_MSG_TYPE_PINGREQ
@ MQTT_SN_MSG_TYPE_DISCONNECT
@ MQTT_SN_MSG_TYPE_UNSUBACK
@ MQTT_SN_MSG_TYPE_UNSUBSCRIBE
@ MQTT_SN_MSG_TYPE_GWINFO
@ MQTT_SN_MSG_TYPE_PUBLISH
@ MQTT_SN_MSG_TYPE_WILLTOPICUPD
@ MQTT_SN_MSG_TYPE_CONNECT
@ MQTT_SN_MSG_TYPE_ADVERTISE
@ MQTT_SN_MSG_TYPE_SUBSCRIBE
@ MQTT_SN_MSG_TYPE_PUBACK
@ MQTT_SN_MSG_TYPE_REGISTER
@ MQTT_SN_MSG_TYPE_WILLTOPICREQ
@ MQTT_SN_MSG_TYPE_PINGRESP
@ MQTT_SN_MSG_TYPE_WILLTOPIC
@ MQTT_SN_MSG_TYPE_WILLMSGUPD
@ MQTT_SN_MSG_TYPE_PUBREC
@ MQTT_SN_MSG_TYPE_WILLMSGRESP
@ MQTT_SN_MSG_TYPE_PUBCOMP
@ MQTT_SN_MSG_TYPE_WILLMSGREQ
@ MQTT_SN_MSG_TYPE_PUBREL
@ MQTT_SN_MSG_TYPE_REGACK
@ MQTT_SN_MSG_TYPE_ENCAPSULATED
@ MQTT_SN_MSG_TYPE_CONNACK
@ MQTT_SN_MSG_TYPE_SEARCHGW
@ MQTT_SN_MSG_TYPE_WILLMSG
MqttSnPubRel
uint16_t length
MqttSnSubAck
MqttSnPubComp
MqttSnSubscribe
char_t willTopic[]
MqttSnFlags
MqttSnTransportProtocol
MQTT-SN transport protocols.
@ MQTT_SN_TRANSPORT_PROTOCOL_DTLS
DTLS protocol.
@ MQTT_SN_TRANSPORT_PROTOCOL_UDP
UDP protocol.
uint16_t msgId
MqttSnAdvertise
MqttSnUnsubscribe
MqttSnWillTopicResp
MqttSnPubRec
MqttSnWillMsgResp
MqttSnPublish
MqttSnGwInfo
MqttSnQosLevel
Quality of service level.
@ MQTT_SN_QOS_LEVEL_MINUS_1
No connection setup.
@ MQTT_SN_QOS_LEVEL_1
At least once delivery.
@ MQTT_SN_QOS_LEVEL_0
At most once delivery.
@ MQTT_SN_QOS_LEVEL_2
Exactly once delivery.
MqttSnHeader
uint16_t duration
uint8_t protocolId
uint8_t data[]
MqttSnPubAck
MqttSnExtHeader
MqttSnWillTopicUpd
MqttSnTopicIdType
Topic ID types.
@ MQTT_SN_NORMAL_TOPIC_ID
Normal topic ID.
@ MQTT_SN_SHORT_TOPIC_NAME
Short topic name.
@ MQTT_SN_PREDEFINED_TOPIC_ID
Predefined topic ID.
@ MQTT_SN_NORMAL_TOPIC_NAME
Normal topic name.
void MqttSnPingResp
PINGRESP message.
MqttSnDisconnect
MqttSnRegAck
MqttSnSearchGw
void MqttSnWillMsgUpd
WILLMSGUPD message.
void MqttSnWillTopicReq
WILLTOPICREQ message.
char_t topicName[]
uint8_t msgType
void MqttSnWillMsg
WILLMSG message.
void MqttSnWillMsgReq
WILLMSGREQ message.
uint16_t topicId
uint8_t returnCode
uint8_t gwAdd[]
MqttSnReturnCode
MQTT-SN return codes.
@ MQTT_SN_RETURN_CODE_ACCEPTED
@ MQTT_SN_RETURN_CODE_REJECTED_INVALID_TOPIC_ID
@ MQTT_SN_RETURN_CODE_REJECTED_NOT_SUPPORTED
@ MQTT_SN_RETURN_CODE_REJECTED_CONGESTION
char_t clientId[]
MqttSnRegister
MqttSnConnAck
MqttSnConnect
typedef __packed_struct
MQTT-SN flags.
MqttSnUnsubAck
MqttSnWillTopic
void MqttSnPingReq
PINGREQ message.
TCP/IP stack core.
Predefined topic.
const char_t * topicName
Topic name.
uint16_t topicId
Topic identifier.
uint8_t flags
Definition: tcp.h:351