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-2019 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 1.9.6
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
66 } MqttVersion;
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
91 } MqttQosLevel;
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 //CodeWarrior or Win32 compiler?
161 #if defined(__CWCC__) || defined(_WIN32)
162  #pragma pack(push, 1)
163 #endif
164 
165 
166 /**
167  * @brief Fixed header
168  **/
169 
170 typedef __start_packed struct
171 {
172 #ifdef _CPU_BIG_ENDIAN
173  uint8_t type : 4; //0
174  uint8_t dup : 1;
175  uint8_t qos : 2;
176  uint8_t retain : 1;
177 #else
178  uint8_t retain : 1; //0
179  uint8_t qos : 2;
180  uint8_t dup : 1;
181  uint8_t type : 4;
182 #endif
183  uint8_t length[]; //1
185 
186 
187 /**
188  * @brief UTF-8 encoded string
189  **/
190 
191 typedef __start_packed struct
192 {
193  uint16_t length; //0-1
194  uint8_t data[]; //2
196 
197 
198 //CodeWarrior or Win32 compiler?
199 #if defined(__CWCC__) || defined(_WIN32)
200  #pragma pack(pop)
201 #endif
202 
203 //C++ guard
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 #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
@ MQTT_QOS_LEVEL_2
Exactly once delivery.
Definition: mqtt_common.h:90
MqttTransportProtocol
Transport protocol.
Definition: mqtt_common.h:73
@ MQTT_CONNECT_RET_CODE_ACCEPTED
Definition: mqtt_common.h:151
MqttConnectRetCode
Connect return codes.
Definition: mqtt_common.h:149
@ 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
__start_packed struct @231 MqttString
UTF-8 encoded string.
@ MQTT_TRANSPORT_PROTOCOL_WS
TLS protocol.
Definition: mqtt_common.h:77
uint8_t data[]
Definition: mqtt_common.h:194
uint8_t qos
Definition: mqtt_common.h:179
@ MQTT_PACKET_TYPE_CONNACK
Connect acknowledgment.
Definition: mqtt_common.h:102
@ MQTT_CONNECT_FLAG_WILL_RETAIN
Definition: mqtt_common.h:129
uint8_t type
Definition: mqtt_common.h:181
@ 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:86
uint8_t retain
Definition: mqtt_common.h:178
@ 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
__start_packed struct _Ipv4Header __end_packed
@ 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:62
@ 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
uint8_t length[]
Definition: mqtt_common.h:183
MqttConnectAckFlags
Connect Acknowledge flags.
Definition: mqtt_common.h:139
@ 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:122
@ MQTT_TRANSPORT_PROTOCOL_WSS
WebSocket protocol.
Definition: mqtt_common.h:78
uint8_t dup
Definition: mqtt_common.h:180
@ 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
__start_packed struct @230 MqttPacketHeader
Fixed header.
MqttPacketType
MQTT control packet type.
Definition: mqtt_common.h:98
@ MQTT_CONNECT_FLAG_WILL
Definition: mqtt_common.h:125