mqtt_common.h
Go to the documentation of this file.
1 /**
2  * @file mqtt_common.h
3  * @brief Definitions common to MQTT client and server
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.4
29  **/
30 
31 #ifndef _MQTT_COMMON_H
32 #define _MQTT_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //MQTT port number
38 #define MQTT_PORT 1883
39 //MQTT over TLS port number
40 #define MQTT_TLS_PORT 8883
41 
42 //MQTT 3.1 protocol name
43 #define MQTT_PROTOCOL_NAME_3_1 "MQIsdp"
44 //MQTT 3.1.1 protocol name
45 #define MQTT_PROTOCOL_NAME_3_1_1 "MQTT"
46 
47 //Minimum size of MQTT header
48 #define MQTT_MIN_HEADER_SIZE 2
49 //Maximum size of MQTT header
50 #define MQTT_MAX_HEADER_SIZE 5
51 
52 //C++ guard
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 
58 /**
59  * @brief MQTT protocol level
60  */
61 
62 typedef enum
63 {
64  MQTT_VERSION_3_1 = 3, ///<MQTT version 3.1
65  MQTT_VERSION_3_1_1 = 4 ///<MQTT version 3.1.1
67 
68 
69 /**
70  * @brief Transport protocol
71  **/
72 
73 typedef enum
74 {
75  MQTT_TRANSPORT_PROTOCOL_TCP = 1, ///TCP protocol
76  MQTT_TRANSPORT_PROTOCOL_TLS = 2, ///TLS protocol
77  MQTT_TRANSPORT_PROTOCOL_WS = 3, ///WebSocket protocol
78  MQTT_TRANSPORT_PROTOCOL_WSS = 4, ///Secure WebSocket protocol
80 
81 
82 /**
83  * @brief Quality of service level
84  **/
85 
86 typedef enum
87 {
88  MQTT_QOS_LEVEL_0 = 0, ///<At most once delivery
89  MQTT_QOS_LEVEL_1 = 1, ///<At least once delivery
90  MQTT_QOS_LEVEL_2 = 2 ///<Exactly once delivery
92 
93 
94 /**
95  * @brief MQTT control packet type
96  **/
97 
98 typedef enum
99 {
100  MQTT_PACKET_TYPE_INVALID = 0, ///<Invalid packet
101  MQTT_PACKET_TYPE_CONNECT = 1, ///<Client request to connect to server
102  MQTT_PACKET_TYPE_CONNACK = 2, ///<Connect acknowledgment
103  MQTT_PACKET_TYPE_PUBLISH = 3, ///<Publish message
104  MQTT_PACKET_TYPE_PUBACK = 4, ///<Publish acknowledgment
105  MQTT_PACKET_TYPE_PUBREC = 5, ///<Publish received (assured delivery part 1)
106  MQTT_PACKET_TYPE_PUBREL = 6, ///<Publish release (assured delivery part 2)
107  MQTT_PACKET_TYPE_PUBCOMP = 7, ///<Publish complete (assured delivery part 3)
108  MQTT_PACKET_TYPE_SUBSCRIBE = 8, ///<Client subscribe request
109  MQTT_PACKET_TYPE_SUBACK = 9, ///<Subscribe acknowledgment
110  MQTT_PACKET_TYPE_UNSUBSCRIBE = 10, ///<Unsubscribe request
111  MQTT_PACKET_TYPE_UNSUBACK = 11, ///<Unsubscribe acknowledgment
112  MQTT_PACKET_TYPE_PINGREQ = 12, ///<Ping request
113  MQTT_PACKET_TYPE_PINGRESP = 13, ///<Ping response
114  MQTT_PACKET_TYPE_DISCONNECT = 14 ///<Client is disconnecting
116 
117 
118 /**
119  * @brief Connect flags
120  **/
121 
122 typedef enum
123 {
133 
134 
135 /**
136  * @brief Connect Acknowledge flags
137  **/
138 
139 typedef enum
140 {
143 
144 
145 /**
146  * @brief Connect return codes
147  **/
148 
149 typedef enum
150 {
158 
159 
160 //CC-RX, CodeWarrior or Win32 compiler?
161 #if defined(__CCRX__)
162  #pragma pack
163 #elif defined(__CWCC__) || defined(_WIN32)
164  #pragma pack(push, 1)
165 #endif
166 
167 
168 /**
169  * @brief Fixed header
170  **/
171 
173 {
174 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
175  uint8_t type : 4; //0
176  uint8_t dup : 1;
177  uint8_t qos : 2;
178  uint8_t retain : 1;
179 #else
180  uint8_t retain : 1; //0
181  uint8_t qos : 2;
182  uint8_t dup : 1;
183  uint8_t type : 4;
184 #endif
185  uint8_t length[]; //1
187 
188 
189 /**
190  * @brief UTF-8 encoded string
191  **/
192 
193 typedef __packed_struct
194 {
195  uint16_t length; //0-1
196  uint8_t data[]; //2
198 
199 
200 //CC-RX, CodeWarrior or Win32 compiler?
201 #if defined(__CCRX__)
202  #pragma unpack
203 #elif defined(__CWCC__) || defined(_WIN32)
204  #pragma pack(pop)
205 #endif
206 
207 //C++ guard
208 #ifdef __cplusplus
209 }
210 #endif
211 
212 #endif
@ MQTT_CONNECT_FLAG_USERNAME
Definition: mqtt_common.h:131
@ MQTT_CONNECT_RET_CODE_NOT_AUTHORIZED
Definition: mqtt_common.h:156
@ MQTT_PACKET_TYPE_INVALID
Invalid packet.
Definition: mqtt_common.h:100
@ MQTT_PACKET_TYPE_DISCONNECT
Client is disconnecting.
Definition: mqtt_common.h:114
uint8_t length[]
Definition: mqtt_common.h:185
@ MQTT_QOS_LEVEL_2
Exactly once delivery.
Definition: mqtt_common.h:90
MqttTransportProtocol
Transport protocol.
Definition: mqtt_common.h:74
@ MQTT_CONNECT_RET_CODE_ACCEPTED
Definition: mqtt_common.h:151
MqttConnectRetCode
Connect return codes.
Definition: mqtt_common.h:150
@ MQTT_CONNECT_RET_CODE_ID_REJECTED
Definition: mqtt_common.h:153
@ MQTT_CONNECT_FLAG_WILL_QOS_1
Definition: mqtt_common.h:127
@ MQTT_PACKET_TYPE_PUBLISH
Publish message.
Definition: mqtt_common.h:103
@ MQTT_PACKET_TYPE_PUBREC
Publish received (assured delivery part 1)
Definition: mqtt_common.h:105
MqttPacketHeader
Definition: mqtt_common.h:186
@ MQTT_TRANSPORT_PROTOCOL_WS
TLS protocol.
Definition: mqtt_common.h:77
uint8_t data[]
Definition: mqtt_common.h:196
uint8_t qos
Definition: mqtt_common.h:181
@ MQTT_PACKET_TYPE_CONNACK
Connect acknowledgment.
Definition: mqtt_common.h:102
@ MQTT_CONNECT_FLAG_WILL_RETAIN
Definition: mqtt_common.h:129
typedef __packed_struct
Fixed header.
Definition: mqtt_common.h:173
uint8_t type
Definition: mqtt_common.h:183
@ MQTT_PACKET_TYPE_PINGRESP
Ping response.
Definition: mqtt_common.h:113
@ MQTT_QOS_LEVEL_0
At most once delivery.
Definition: mqtt_common.h:88
@ MQTT_PACKET_TYPE_PUBACK
Publish acknowledgment.
Definition: mqtt_common.h:104
MqttQosLevel
Quality of service level.
Definition: mqtt_common.h:87
@ MQTT_PACKET_TYPE_PINGREQ
Ping request.
Definition: mqtt_common.h:112
@ MQTT_PACKET_TYPE_SUBACK
Subscribe acknowledgment.
Definition: mqtt_common.h:109
@ MQTT_TRANSPORT_PROTOCOL_TCP
Definition: mqtt_common.h:75
@ MQTT_CONNECT_RET_CODE_BAD_USER_NAME
Definition: mqtt_common.h:155
@ MQTT_PACKET_TYPE_CONNECT
Client request to connect to server.
Definition: mqtt_common.h:101
@ MQTT_PACKET_TYPE_SUBSCRIBE
Client subscribe request.
Definition: mqtt_common.h:108
@ MQTT_PACKET_TYPE_UNSUBACK
Unsubscribe acknowledgment.
Definition: mqtt_common.h:111
MqttVersion
MQTT protocol level.
Definition: mqtt_common.h:63
@ MQTT_PACKET_TYPE_UNSUBSCRIBE
Unsubscribe request.
Definition: mqtt_common.h:110
@ MQTT_CONNECT_RET_CODE_UNACCEPTABLE_VERSION
Definition: mqtt_common.h:152
@ MQTT_CONNECT_FLAG_WILL_QOS_2
Definition: mqtt_common.h:128
@ MQTT_CONNECT_ACK_FLAG_SESSION_PRESENT
Definition: mqtt_common.h:141
MqttConnectAckFlags
Connect Acknowledge flags.
Definition: mqtt_common.h:140
@ MQTT_TRANSPORT_PROTOCOL_TLS
TCP protocol.
Definition: mqtt_common.h:76
@ MQTT_CONNECT_FLAG_PASSWORD
Definition: mqtt_common.h:130
@ MQTT_PACKET_TYPE_PUBCOMP
Publish complete (assured delivery part 3)
Definition: mqtt_common.h:107
MqttConnectFlags
Connect flags.
Definition: mqtt_common.h:123
@ MQTT_TRANSPORT_PROTOCOL_WSS
WebSocket protocol.
Definition: mqtt_common.h:78
uint8_t dup
Definition: mqtt_common.h:182
@ MQTT_CONNECT_FLAG_CLEAN_SESSION
Definition: mqtt_common.h:124
@ MQTT_PACKET_TYPE_PUBREL
Publish release (assured delivery part 2)
Definition: mqtt_common.h:106
@ MQTT_CONNECT_FLAG_WILL_QOS_0
Definition: mqtt_common.h:126
@ MQTT_VERSION_3_1_1
MQTT version 3.1.1.
Definition: mqtt_common.h:65
@ MQTT_QOS_LEVEL_1
At least once delivery.
Definition: mqtt_common.h:89
TCP/IP stack core.
@ MQTT_VERSION_3_1
MQTT version 3.1.
Definition: mqtt_common.h:64
@ MQTT_CONNECT_RET_CODE_SERVER_UNAVAILABLE
Definition: mqtt_common.h:154
MqttString
Definition: mqtt_common.h:197
MqttPacketType
MQTT control packet type.
Definition: mqtt_common.h:99
@ MQTT_CONNECT_FLAG_WILL
Definition: mqtt_common.h:125