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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _MQTT_COMMON_H
30 #define _MQTT_COMMON_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //MQTT port number
36 #define MQTT_PORT 1883
37 //MQTT over TLS port number
38 #define MQTT_TLS_PORT 8883
39 
40 //MQTT 3.1 protocol name
41 #define MQTT_PROTOCOL_NAME_3_1 "MQIsdp"
42 //MQTT 3.1.1 protocol name
43 #define MQTT_PROTOCOL_NAME_3_1_1 "MQTT"
44 
45 //Minimum size of MQTT header
46 #define MQTT_MIN_HEADER_SIZE 2
47 //Maximum size of MQTT header
48 #define MQTT_MAX_HEADER_SIZE 5
49 
50 //C++ guard
51 #ifdef __cplusplus
52  extern "C" {
53 #endif
54 
55 
56 /**
57  * @brief MQTT protocol level
58  */
59 
60 typedef enum
61 {
62  MQTT_PROTOCOL_LEVEL_3_1 = 3, ///<MQTT version 3.1
63  MQTT_PROTOCOL_LEVEL_3_1_1 = 4 ///<MQTT version 3.1.1
65 
66 
67 /**
68  * @brief Transport protocol
69  **/
70 
71 typedef enum
72 {
73  MQTT_TRANSPORT_PROTOCOL_TCP = 1, ///TCP protocol
74  MQTT_TRANSPORT_PROTOCOL_TLS = 2, ///TLS protocol
75  MQTT_TRANSPORT_PROTOCOL_WS = 3, ///WebSocket protocol
76  MQTT_TRANSPORT_PROTOCOL_WSS = 4, ///Secure WebSocket protocol
78 
79 
80 /**
81  * @brief Quality of service level
82  **/
83 
84 typedef enum
85 {
86  MQTT_QOS_LEVEL_0 = 0, ///<At most once delivery
87  MQTT_QOS_LEVEL_1 = 1, ///<At least once delivery
88  MQTT_QOS_LEVEL_2 = 2 ///<Exactly once delivery
89 } MqttQosLevel;
90 
91 
92 /**
93  * @brief MQTT control packet type
94  **/
95 
96 typedef enum
97 {
98  MQTT_PACKET_TYPE_INVALID = 0, ///<Invalid packet
99  MQTT_PACKET_TYPE_CONNECT = 1, ///<Client request to connect to server
100  MQTT_PACKET_TYPE_CONNACK = 2, ///<Connect acknowledgment
101  MQTT_PACKET_TYPE_PUBLISH = 3, ///<Publish message
102  MQTT_PACKET_TYPE_PUBACK = 4, ///<Publish acknowledgment
103  MQTT_PACKET_TYPE_PUBREC = 5, ///<Publish received (assured delivery part 1)
104  MQTT_PACKET_TYPE_PUBREL = 6, ///<Publish release (assured delivery part 2)
105  MQTT_PACKET_TYPE_PUBCOMP = 7, ///<Publish complete (assured delivery part 3)
106  MQTT_PACKET_TYPE_SUBSCRIBE = 8, ///<Client subscribe request
107  MQTT_PACKET_TYPE_SUBACK = 9, ///<Subscribe acknowledgment
108  MQTT_PACKET_TYPE_UNSUBSCRIBE = 10, ///<Unsubscribe request
109  MQTT_PACKET_TYPE_UNSUBACK = 11, ///<Unsubscribe acknowledgment
110  MQTT_PACKET_TYPE_PINGREQ = 12, ///<Ping request
111  MQTT_PACKET_TYPE_PINGRESP = 13, ///<Ping response
112  MQTT_PACKET_TYPE_DISCONNECT = 14 ///<Client is disconnecting
114 
115 
116 /**
117  * @brief Connect flags
118  **/
119 
120 typedef enum
121 {
131 
132 
133 /**
134  * @brief Connect Acknowledge flags
135  **/
136 
137 typedef enum
138 {
141 
142 
143 /**
144  * @brief Connect return codes
145  **/
146 
147 typedef enum
148 {
156 
157 
158 //CodeWarrior or Win32 compiler?
159 #if defined(__CWCC__) || defined(_WIN32)
160  #pragma pack(push, 1)
161 #endif
162 
163 
164 /**
165  * @brief Fixed header
166  **/
167 
168 typedef __start_packed struct
169 {
170 #ifdef _CPU_BIG_ENDIAN
171  uint8_t type : 4; //0
172  uint8_t dup : 1;
173  uint8_t qos : 2;
174  uint8_t retain : 1;
175 #else
176  uint8_t retain : 1; //0
177  uint8_t qos : 2;
178  uint8_t dup : 1;
179  uint8_t type : 4;
180 #endif
181  uint8_t length[]; //1
183 
184 
185 /**
186  * @brief UTF-8 encoded string
187  **/
188 
189 typedef __start_packed struct
190 {
191  uint16_t length; //0-1
192  uint8_t data[]; //2
194 
195 
196 //CodeWarrior or Win32 compiler?
197 #if defined(__CWCC__) || defined(_WIN32)
198  #pragma pack(pop)
199 #endif
200 
201 //C++ guard
202 #ifdef __cplusplus
203  }
204 #endif
205 
206 #endif
MqttConnectFlags
Connect flags.
Definition: mqtt_common.h:120
MqttTransportProtocol
Transport protocol.
Definition: mqtt_common.h:71
MqttPacketType
MQTT control packet type.
Definition: mqtt_common.h:96
TCP/IP stack core.
Invalid packet.
Definition: mqtt_common.h:98
MqttConnectRetCode
Connect return codes.
Definition: mqtt_common.h:147
Client subscribe request.
Definition: mqtt_common.h:106
Publish received (assured delivery part 1)
Definition: mqtt_common.h:103
Exactly once delivery.
Definition: mqtt_common.h:88
uint8_t qos
Definition: mqtt_common.h:177
__start_packed struct @233 MqttPacketHeader
Fixed header.
Client request to connect to server.
Definition: mqtt_common.h:99
MqttQosLevel
Quality of service level.
Definition: mqtt_common.h:84
Publish release (assured delivery part 2)
Definition: mqtt_common.h:104
Publish acknowledgment.
Definition: mqtt_common.h:102
Client is disconnecting.
Definition: mqtt_common.h:112
WebSocket protocol.
Definition: mqtt_common.h:76
__start_packed struct _Ipv4Header __end_packed
uint8_t length[]
Definition: mqtt_common.h:181
Unsubscribe request.
Definition: mqtt_common.h:108
uint8_t retain
Definition: mqtt_common.h:176
Unsubscribe acknowledgment.
Definition: mqtt_common.h:109
uint8_t type
Definition: mqtt_common.h:179
At least once delivery.
Definition: mqtt_common.h:87
Publish message.
Definition: mqtt_common.h:101
At most once delivery.
Definition: mqtt_common.h:86
MqttConnectAckFlags
Connect Acknowledge flags.
Definition: mqtt_common.h:137
uint8_t dup
Definition: mqtt_common.h:178
Subscribe acknowledgment.
Definition: mqtt_common.h:107
uint8_t data[]
Definition: mqtt_common.h:192
Connect acknowledgment.
Definition: mqtt_common.h:100
MQTT version 3.1.
Definition: mqtt_common.h:62
__start_packed struct @234 MqttString
UTF-8 encoded string.
Publish complete (assured delivery part 3)
Definition: mqtt_common.h:105
MqttProtocolLevel
MQTT protocol level.
Definition: mqtt_common.h:60
MQTT version 3.1.1.
Definition: mqtt_common.h:63