AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
iot_mqtt_protocol.h
Go to the documentation of this file.
1 /*
2  * IoT MQTT V2.1.0
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
30 #ifndef IOT_MQTT_PROTOCOL_H_
31 #define IOT_MQTT_PROTOCOL_H_
32 
33 /*
34  * MQTT control packet type and flags. Always the first byte of an MQTT
35  * packet.
36  *
37  * For details, see
38  * http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/csprd02/mqtt-v3.1.1-csprd02.html#_Toc385349757
39  */
40 #define MQTT_PACKET_TYPE_CONNECT ( ( uint8_t ) 0x10U )
41 #define MQTT_PACKET_TYPE_CONNACK ( ( uint8_t ) 0x20U )
42 #define MQTT_PACKET_TYPE_PUBLISH ( ( uint8_t ) 0x30U )
43 #define MQTT_PACKET_TYPE_PUBACK ( ( uint8_t ) 0x40U )
44 #define MQTT_PACKET_TYPE_SUBSCRIBE ( ( uint8_t ) 0x82U )
45 #define MQTT_PACKET_TYPE_SUBACK ( ( uint8_t ) 0x90U )
46 #define MQTT_PACKET_TYPE_UNSUBSCRIBE ( ( uint8_t ) 0xa2U )
47 #define MQTT_PACKET_TYPE_UNSUBACK ( ( uint8_t ) 0xb0U )
48 #define MQTT_PACKET_TYPE_PINGREQ ( ( uint8_t ) 0xc0U )
49 #define MQTT_PACKET_TYPE_PINGRESP ( ( uint8_t ) 0xd0U )
50 #define MQTT_PACKET_TYPE_DISCONNECT ( ( uint8_t ) 0xe0U )
52 /*
53  * Positions of each flag in the "Connect Flag" field of an MQTT CONNECT
54  * packet.
55  */
56 #define MQTT_CONNECT_FLAG_CLEAN ( 1 )
57 #define MQTT_CONNECT_FLAG_WILL ( 2 )
58 #define MQTT_CONNECT_FLAG_WILL_QOS1 ( 3 )
59 #define MQTT_CONNECT_FLAG_WILL_QOS2 ( 4 )
60 #define MQTT_CONNECT_FLAG_WILL_RETAIN ( 5 )
61 #define MQTT_CONNECT_FLAG_PASSWORD ( 6 )
62 #define MQTT_CONNECT_FLAG_USERNAME ( 7 )
64 /*
65  * Positions of each flag in the first byte of an MQTT PUBLISH packet's
66  * fixed header.
67  */
68 #define MQTT_PUBLISH_FLAG_RETAIN ( 0 )
69 #define MQTT_PUBLISH_FLAG_QOS1 ( 1 )
70 #define MQTT_PUBLISH_FLAG_QOS2 ( 2 )
71 #define MQTT_PUBLISH_FLAG_DUP ( 3 )
79 #define MQTT_REMAINING_LENGTH_INVALID ( ( size_t ) 268435456 )
80 
87 #define MQTT_PACKET_CONNECT_MAX_SIZE ( 327700UL )
88 
94 #define MQTT_MIN_PUBLISH_REMAINING_LENGTH_QOS0 ( 3U )
95 
100 #define MQTT_MAX_REMAINING_LENGTH ( 268435455UL )
101 
105 #define MQTT_VERSION_3_1_1 ( ( uint8_t ) 4U )
106 
107 /*
108  * Constants relating to CONNACK packets, defined by MQTT 3.1.1 spec.
109  */
110 #define MQTT_PACKET_CONNACK_REMAINING_LENGTH ( ( uint8_t ) 2U )
111 #define MQTT_PACKET_CONNACK_SESSION_PRESENT_MASK ( ( uint8_t ) 0x01U )
113 /*
114  * Constants relating to PUBLISH and PUBACK packets, defined by MQTT
115  * 3.1.1 spec.
116  */
117 #define MQTT_PACKET_PUBACK_SIZE ( 4U )
118 #define MQTT_PACKET_PUBACK_REMAINING_LENGTH ( ( uint8_t ) 2 )
120 /*
121  * Constants relating to SUBACK and UNSUBACK packets, defined by MQTT
122  * 3.1.1 spec.
123  */
124 #define MQTT_PACKET_SUBACK_MINIMUM_SIZE ( 5U )
125 #define MQTT_PACKET_UNSUBACK_REMAINING_LENGTH ( ( uint8_t ) 2 )
127 /*
128  * Constants relating to PINGREQ and PINGRESP packets, defined by MQTT 3.1.1 spec.
129  */
130 #define MQTT_PACKET_PINGREQ_SIZE ( 2U )
131 #define MQTT_PACKET_PINGRESP_REMAINING_LENGTH ( 0U )
133 /*
134  * Constants relating to DISCONNECT packets, defined by MQTT 3.1.1 spec.
135  */
136 #define MQTT_PACKET_DISCONNECT_SIZE ( 2U )
140 #endif /* ifndef _IOT_MQTT_PROTOCOL_H_ */