-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
mqtt_protocol.h
282 lines (263 loc) · 11.5 KB
/
mqtt_protocol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Copyright (c) 2009-2020 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
https://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#ifndef MQTT_PROTOCOL_H
#define MQTT_PROTOCOL_H
/*
* File: mqtt_protocol.h
*
* This header contains definitions of MQTT values as defined in the specifications.
*/
#define PROTOCOL_NAME_v31 "MQIsdp"
#define PROTOCOL_VERSION_v31 3
#define PROTOCOL_NAME "MQTT"
#define PROTOCOL_VERSION_v311 4
#define PROTOCOL_VERSION_v5 5
/* Message types */
#define CMD_CONNECT 0x10U
#define CMD_CONNACK 0x20U
#define CMD_PUBLISH 0x30U
#define CMD_PUBACK 0x40U
#define CMD_PUBREC 0x50U
#define CMD_PUBREL 0x60U
#define CMD_PUBCOMP 0x70U
#define CMD_SUBSCRIBE 0x80U
#define CMD_SUBACK 0x90U
#define CMD_UNSUBSCRIBE 0xA0U
#define CMD_UNSUBACK 0xB0U
#define CMD_PINGREQ 0xC0U
#define CMD_PINGRESP 0xD0U
#define CMD_DISCONNECT 0xE0U
#define CMD_AUTH 0xF0U
/* Mosquitto only: for distinguishing CONNECT and WILL properties */
#define CMD_WILL 0x100
/* Enum: mqtt311_connack_codes
*
* The CONNACK results for MQTT v3.1.1, and v3.1.
*
* Values:
* CONNACK_ACCEPTED - 0
* CONNACK_REFUSED_PROTOCOL_VERSION - 1
* CONNACK_REFUSED_IDENTIFIER_REJECTED - 2
* CONNACK_REFUSED_SERVER_UNAVAILABLE - 3
* CONNACK_REFUSED_BAD_USERNAME_PASSWORD - 4
* CONNACK_REFUSED_NOT_AUTHORIZED - 5
*/
enum mqtt311_connack_codes {
CONNACK_ACCEPTED = 0,
CONNACK_REFUSED_PROTOCOL_VERSION = 1,
CONNACK_REFUSED_IDENTIFIER_REJECTED = 2,
CONNACK_REFUSED_SERVER_UNAVAILABLE = 3,
CONNACK_REFUSED_BAD_USERNAME_PASSWORD = 4,
CONNACK_REFUSED_NOT_AUTHORIZED = 5,
};
/* Enum: mqtt5_return_codes
* The reason codes returned in various MQTT commands.
*
* Values:
* MQTT_RC_SUCCESS - 0
* MQTT_RC_NORMAL_DISCONNECTION - 0
* MQTT_RC_GRANTED_QOS0 - 0
* MQTT_RC_GRANTED_QOS1 - 1
* MQTT_RC_GRANTED_QOS2 - 2
* MQTT_RC_DISCONNECT_WITH_WILL_MSG - 4
* MQTT_RC_NO_MATCHING_SUBSCRIBERS - 16
* MQTT_RC_NO_SUBSCRIPTION_EXISTED - 17
* MQTT_RC_CONTINUE_AUTHENTICATION - 24
* MQTT_RC_REAUTHENTICATE - 25
* MQTT_RC_UNSPECIFIED - 128
* MQTT_RC_MALFORMED_PACKET - 129
* MQTT_RC_PROTOCOL_ERROR - 130
* MQTT_RC_IMPLEMENTATION_SPECIFIC - 131
* MQTT_RC_UNSUPPORTED_PROTOCOL_VERSION - 132
* MQTT_RC_CLIENTID_NOT_VALID - 133
* MQTT_RC_BAD_USERNAME_OR_PASSWORD - 134
* MQTT_RC_NOT_AUTHORIZED - 135
* MQTT_RC_SERVER_UNAVAILABLE - 136
* MQTT_RC_SERVER_BUSY - 137
* MQTT_RC_BANNED - 138
* MQTT_RC_SERVER_SHUTTING_DOWN - 139
* MQTT_RC_BAD_AUTHENTICATION_METHOD - 140
* MQTT_RC_KEEP_ALIVE_TIMEOUT - 141
* MQTT_RC_SESSION_TAKEN_OVER - 142
* MQTT_RC_TOPIC_FILTER_INVALID - 143
* MQTT_RC_TOPIC_NAME_INVALID - 144
* MQTT_RC_PACKET_ID_IN_USE - 145
* MQTT_RC_PACKET_ID_NOT_FOUND - 146
* MQTT_RC_RECEIVE_MAXIMUM_EXCEEDED - 147
* MQTT_RC_TOPIC_ALIAS_INVALID - 148
* MQTT_RC_PACKET_TOO_LARGE - 149
* MQTT_RC_MESSAGE_RATE_TOO_HIGH - 150
* MQTT_RC_QUOTA_EXCEEDED - 151
* MQTT_RC_ADMINISTRATIVE_ACTION - 152
* MQTT_RC_PAYLOAD_FORMAT_INVALID - 153
* MQTT_RC_RETAIN_NOT_SUPPORTED - 154
* MQTT_RC_QOS_NOT_SUPPORTED - 155
* MQTT_RC_USE_ANOTHER_SERVER - 156
* MQTT_RC_SERVER_MOVED - 157
* MQTT_RC_SHARED_SUBS_NOT_SUPPORTED - 158
* MQTT_RC_CONNECTION_RATE_EXCEEDED - 159
* MQTT_RC_MAXIMUM_CONNECT_TIME - 160
* MQTT_RC_SUBSCRIPTION_IDS_NOT_SUPPORTED - 161
* MQTT_RC_WILDCARD_SUBS_NOT_SUPPORTED - 162
*/
enum mqtt5_return_codes {
MQTT_RC_SUCCESS = 0, /* CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK, AUTH */
MQTT_RC_NORMAL_DISCONNECTION = 0, /* DISCONNECT */
MQTT_RC_GRANTED_QOS0 = 0, /* SUBACK */
MQTT_RC_GRANTED_QOS1 = 1, /* SUBACK */
MQTT_RC_GRANTED_QOS2 = 2, /* SUBACK */
MQTT_RC_DISCONNECT_WITH_WILL_MSG = 4, /* DISCONNECT */
MQTT_RC_NO_MATCHING_SUBSCRIBERS = 16, /* PUBACK, PUBREC */
MQTT_RC_NO_SUBSCRIPTION_EXISTED = 17, /* UNSUBACK */
MQTT_RC_CONTINUE_AUTHENTICATION = 24, /* AUTH */
MQTT_RC_REAUTHENTICATE = 25, /* AUTH */
MQTT_RC_UNSPECIFIED = 128, /* CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT */
MQTT_RC_MALFORMED_PACKET = 129, /* CONNACK, DISCONNECT */
MQTT_RC_PROTOCOL_ERROR = 130, /* DISCONNECT */
MQTT_RC_IMPLEMENTATION_SPECIFIC = 131, /* CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT */
MQTT_RC_UNSUPPORTED_PROTOCOL_VERSION = 132, /* CONNACK */
MQTT_RC_CLIENTID_NOT_VALID = 133, /* CONNACK */
MQTT_RC_BAD_USERNAME_OR_PASSWORD = 134, /* CONNACK */
MQTT_RC_NOT_AUTHORIZED = 135, /* CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT */
MQTT_RC_SERVER_UNAVAILABLE = 136, /* CONNACK */
MQTT_RC_SERVER_BUSY = 137, /* CONNACK, DISCONNECT */
MQTT_RC_BANNED = 138, /* CONNACK */
MQTT_RC_SERVER_SHUTTING_DOWN = 139, /* DISCONNECT */
MQTT_RC_BAD_AUTHENTICATION_METHOD = 140, /* CONNACK */
MQTT_RC_KEEP_ALIVE_TIMEOUT = 141, /* DISCONNECT */
MQTT_RC_SESSION_TAKEN_OVER = 142, /* DISCONNECT */
MQTT_RC_TOPIC_FILTER_INVALID = 143, /* SUBACK, UNSUBACK, DISCONNECT */
MQTT_RC_TOPIC_NAME_INVALID = 144, /* CONNACK, PUBACK, PUBREC, DISCONNECT */
MQTT_RC_PACKET_ID_IN_USE = 145, /* PUBACK, SUBACK, UNSUBACK */
MQTT_RC_PACKET_ID_NOT_FOUND = 146, /* PUBREL, PUBCOMP */
MQTT_RC_RECEIVE_MAXIMUM_EXCEEDED = 147, /* DISCONNECT */
MQTT_RC_TOPIC_ALIAS_INVALID = 148, /* DISCONNECT */
MQTT_RC_PACKET_TOO_LARGE = 149, /* CONNACK, PUBACK, PUBREC, DISCONNECT */
MQTT_RC_MESSAGE_RATE_TOO_HIGH = 150, /* DISCONNECT */
MQTT_RC_QUOTA_EXCEEDED = 151, /* PUBACK, PUBREC, SUBACK, DISCONNECT */
MQTT_RC_ADMINISTRATIVE_ACTION = 152, /* DISCONNECT */
MQTT_RC_PAYLOAD_FORMAT_INVALID = 153, /* CONNACK, DISCONNECT */
MQTT_RC_RETAIN_NOT_SUPPORTED = 154, /* CONNACK, DISCONNECT */
MQTT_RC_QOS_NOT_SUPPORTED = 155, /* CONNACK, DISCONNECT */
MQTT_RC_USE_ANOTHER_SERVER = 156, /* CONNACK, DISCONNECT */
MQTT_RC_SERVER_MOVED = 157, /* CONNACK, DISCONNECT */
MQTT_RC_SHARED_SUBS_NOT_SUPPORTED = 158, /* SUBACK, DISCONNECT */
MQTT_RC_CONNECTION_RATE_EXCEEDED = 159, /* CONNACK, DISCONNECT */
MQTT_RC_MAXIMUM_CONNECT_TIME = 160, /* DISCONNECT */
MQTT_RC_SUBSCRIPTION_IDS_NOT_SUPPORTED = 161, /* SUBACK, DISCONNECT */
MQTT_RC_WILDCARD_SUBS_NOT_SUPPORTED = 162, /* SUBACK, DISCONNECT */
};
/* Enum: mqtt5_property
* Options for use with MQTTv5 properties.
* Options:
*
* MQTT_PROP_PAYLOAD_FORMAT_INDICATOR - property option.
* MQTT_PROP_MESSAGE_EXPIRY_INTERVAL - property option.
* MQTT_PROP_CONTENT_TYPE - property option.
* MQTT_PROP_RESPONSE_TOPIC - property option.
* MQTT_PROP_CORRELATION_DATA - property option.
* MQTT_PROP_SUBSCRIPTION_IDENTIFIER - property option.
* MQTT_PROP_SESSION_EXPIRY_INTERVAL - property option.
* MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER - property option.
* MQTT_PROP_SERVER_KEEP_ALIVE - property option.
* MQTT_PROP_AUTHENTICATION_METHOD - property option.
* MQTT_PROP_AUTHENTICATION_DATA - property option.
* MQTT_PROP_REQUEST_PROBLEM_INFORMATION - property option.
* MQTT_PROP_WILL_DELAY_INTERVAL - property option.
* MQTT_PROP_REQUEST_RESPONSE_INFORMATION - property option.
* MQTT_PROP_RESPONSE_INFORMATION - property option.
* MQTT_PROP_SERVER_REFERENCE - property option.
* MQTT_PROP_REASON_STRING - property option.
* MQTT_PROP_RECEIVE_MAXIMUM - property option.
* MQTT_PROP_TOPIC_ALIAS_MAXIMUM - property option.
* MQTT_PROP_TOPIC_ALIAS - property option.
* MQTT_PROP_MAXIMUM_QOS - property option.
* MQTT_PROP_RETAIN_AVAILABLE - property option.
* MQTT_PROP_USER_PROPERTY - property option.
* MQTT_PROP_MAXIMUM_PACKET_SIZE - property option.
* MQTT_PROP_WILDCARD_SUB_AVAILABLE - property option.
* MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE - property option.
* MQTT_PROP_SHARED_SUB_AVAILABLE - property option.
*/
enum mqtt5_property {
MQTT_PROP_PAYLOAD_FORMAT_INDICATOR = 1, /* Byte : PUBLISH, Will Properties */
MQTT_PROP_MESSAGE_EXPIRY_INTERVAL = 2, /* 4 byte int : PUBLISH, Will Properties */
MQTT_PROP_CONTENT_TYPE = 3, /* UTF-8 string : PUBLISH, Will Properties */
MQTT_PROP_RESPONSE_TOPIC = 8, /* UTF-8 string : PUBLISH, Will Properties */
MQTT_PROP_CORRELATION_DATA = 9, /* Binary Data : PUBLISH, Will Properties */
MQTT_PROP_SUBSCRIPTION_IDENTIFIER = 11, /* Variable byte int : PUBLISH, SUBSCRIBE */
MQTT_PROP_SESSION_EXPIRY_INTERVAL = 17, /* 4 byte int : CONNECT, CONNACK, DISCONNECT */
MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER = 18, /* UTF-8 string : CONNACK */
MQTT_PROP_SERVER_KEEP_ALIVE = 19, /* 2 byte int : CONNACK */
MQTT_PROP_AUTHENTICATION_METHOD = 21, /* UTF-8 string : CONNECT, CONNACK, AUTH */
MQTT_PROP_AUTHENTICATION_DATA = 22, /* Binary Data : CONNECT, CONNACK, AUTH */
MQTT_PROP_REQUEST_PROBLEM_INFORMATION = 23, /* Byte : CONNECT */
MQTT_PROP_WILL_DELAY_INTERVAL = 24, /* 4 byte int : Will properties */
MQTT_PROP_REQUEST_RESPONSE_INFORMATION = 25,/* Byte : CONNECT */
MQTT_PROP_RESPONSE_INFORMATION = 26, /* UTF-8 string : CONNACK */
MQTT_PROP_SERVER_REFERENCE = 28, /* UTF-8 string : CONNACK, DISCONNECT */
MQTT_PROP_REASON_STRING = 31, /* UTF-8 string : All except Will properties */
MQTT_PROP_RECEIVE_MAXIMUM = 33, /* 2 byte int : CONNECT, CONNACK */
MQTT_PROP_TOPIC_ALIAS_MAXIMUM = 34, /* 2 byte int : CONNECT, CONNACK */
MQTT_PROP_TOPIC_ALIAS = 35, /* 2 byte int : PUBLISH */
MQTT_PROP_MAXIMUM_QOS = 36, /* Byte : CONNACK */
MQTT_PROP_RETAIN_AVAILABLE = 37, /* Byte : CONNACK */
MQTT_PROP_USER_PROPERTY = 38, /* UTF-8 string pair : All */
MQTT_PROP_MAXIMUM_PACKET_SIZE = 39, /* 4 byte int : CONNECT, CONNACK */
MQTT_PROP_WILDCARD_SUB_AVAILABLE = 40, /* Byte : CONNACK */
MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE = 41, /* Byte : CONNACK */
MQTT_PROP_SHARED_SUB_AVAILABLE = 42, /* Byte : CONNACK */
};
enum mqtt5_property_type {
MQTT_PROP_TYPE_BYTE = 1,
MQTT_PROP_TYPE_INT16 = 2,
MQTT_PROP_TYPE_INT32 = 3,
MQTT_PROP_TYPE_VARINT = 4,
MQTT_PROP_TYPE_BINARY = 5,
MQTT_PROP_TYPE_STRING = 6,
MQTT_PROP_TYPE_STRING_PAIR = 7
};
/* Enum: mqtt5_sub_options
* Options for use with MQTTv5 subscriptions.
*
* MQTT_SUB_OPT_NO_LOCAL - with this option set, if this client publishes to
* a topic to which it is subscribed, the broker will not publish the
* message back to the client.
*
* MQTT_SUB_OPT_RETAIN_AS_PUBLISHED - with this option set, messages
* published for this subscription will keep the retain flag as was set by
* the publishing client. The default behaviour without this option set has
* the retain flag indicating whether a message is fresh/stale.
*
* MQTT_SUB_OPT_SEND_RETAIN_ALWAYS - with this option set, pre-existing
* retained messages are sent as soon as the subscription is made, even
* if the subscription already exists. This is the default behaviour, so
* it is not necessary to set this option.
*
* MQTT_SUB_OPT_SEND_RETAIN_NEW - with this option set, pre-existing retained
* messages for this subscription will be sent when the subscription is made,
* but only if the subscription does not already exist.
*
* MQTT_SUB_OPT_SEND_RETAIN_NEVER - with this option set, pre-existing
* retained messages will never be sent for this subscription.
*/
enum mqtt5_sub_options {
MQTT_SUB_OPT_NO_LOCAL = 0x04,
MQTT_SUB_OPT_RETAIN_AS_PUBLISHED = 0x08,
MQTT_SUB_OPT_SEND_RETAIN_ALWAYS = 0x00,
MQTT_SUB_OPT_SEND_RETAIN_NEW = 0x10,
MQTT_SUB_OPT_SEND_RETAIN_NEVER = 0x20,
};
#define MQTT_MAX_PAYLOAD 268435455U
#endif