mqtt_protocol.h 12 KB

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