emqttd_protocol.hrl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. %%%-----------------------------------------------------------------------------
  2. %%% @Copyright (C) 2012-2015, Feng Lee <feng@emqtt.io>
  3. %%%
  4. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  5. %%% of this software and associated documentation files (the "Software"), to deal
  6. %%% in the Software without restriction, including without limitation the rights
  7. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. %%% copies of the Software, and to permit persons to whom the Software is
  9. %%% furnished to do so, subject to the following conditions:
  10. %%%
  11. %%% The above copyright notice and this permission notice shall be included in all
  12. %%% copies or substantial portions of the Software.
  13. %%%
  14. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. %%% SOFTWARE.
  21. %%%-----------------------------------------------------------------------------
  22. %%% @doc
  23. %%% MQTT Protocol Header.
  24. %%%
  25. %%% @end
  26. %%%-----------------------------------------------------------------------------
  27. %%------------------------------------------------------------------------------
  28. %% MQTT Protocol Version and Levels
  29. %%------------------------------------------------------------------------------
  30. -define(MQTT_PROTO_V31, 3).
  31. -define(MQTT_PROTO_V311, 4).
  32. -define(PROTOCOL_NAMES, [
  33. {?MQTT_PROTO_V31, <<"MQIsdp">>},
  34. {?MQTT_PROTO_V311, <<"MQTT">>}]).
  35. -type mqtt_vsn() :: ?MQTT_PROTO_V31 | ?MQTT_PROTO_V311.
  36. %%------------------------------------------------------------------------------
  37. %% MQTT QoS
  38. %%------------------------------------------------------------------------------
  39. -define(QOS_0, 0). %% At most once
  40. -define(QOS_1, 1). %% At least once
  41. -define(QOS_2, 2). %% Exactly once
  42. -define(QOS0, 0). %% At most once
  43. -define(QOS1, 1). %% At least once
  44. -define(QOS2, 2). %% Exactly once
  45. -define(IS_QOS(I), (I >= ?QOS0 andalso I =< ?QOS2)).
  46. -type mqtt_qos() :: ?QOS0 | ?QOS1 | ?QOS2.
  47. -type mqtt_qos_name() :: qos0 | at_most_once |
  48. qos1 | at_least_once |
  49. qos2 | exactly_once.
  50. -define(QOS_I(Name),
  51. begin
  52. (case Name of
  53. ?QOS_0 -> ?QOS_0;
  54. qos0 -> ?QOS_0;
  55. at_most_once -> ?QOS_0;
  56. ?QOS_1 -> ?QOS_1;
  57. qos1 -> ?QOS_1;
  58. at_least_once -> ?QOS_1;
  59. ?QOS_2 -> ?QOS_2;
  60. qos2 -> ?QOS_2;
  61. exactly_once -> ?QOS_2
  62. end)
  63. end).
  64. %%------------------------------------------------------------------------------
  65. %% Max ClientId Length. Why 1024? NiDongDe!
  66. %%------------------------------------------------------------------------------
  67. -define(MAX_CLIENTID_LEN, 1024).
  68. %%------------------------------------------------------------------------------
  69. %% MQTT Control Packet Types
  70. %%------------------------------------------------------------------------------
  71. -define(RESERVED, 0). %% Reserved
  72. -define(CONNECT, 1). %% Client request to connect to Server
  73. -define(CONNACK, 2). %% Server to Client: Connect acknowledgment
  74. -define(PUBLISH, 3). %% Publish message
  75. -define(PUBACK, 4). %% Publish acknowledgment
  76. -define(PUBREC, 5). %% Publish received (assured delivery part 1)
  77. -define(PUBREL, 6). %% Publish release (assured delivery part 2)
  78. -define(PUBCOMP, 7). %% Publish complete (assured delivery part 3)
  79. -define(SUBSCRIBE, 8). %% Client subscribe request
  80. -define(SUBACK, 9). %% Server Subscribe acknowledgment
  81. -define(UNSUBSCRIBE, 10). %% Unsubscribe request
  82. -define(UNSUBACK, 11). %% Unsubscribe acknowledgment
  83. -define(PINGREQ, 12). %% PING request
  84. -define(PINGRESP, 13). %% PING response
  85. -define(DISCONNECT, 14). %% Client is disconnecting
  86. -define(TYPE_NAMES, [
  87. 'CONNECT',
  88. 'CONNACK',
  89. 'PUBLISH',
  90. 'PUBACK',
  91. 'PUBREC',
  92. 'PUBREL',
  93. 'PUBCOMP',
  94. 'SUBSCRIBE',
  95. 'SUBACK',
  96. 'UNSUBSCRIBE',
  97. 'UNSUBACK',
  98. 'PINGREQ',
  99. 'PINGRESP',
  100. 'DISCONNECT']).
  101. -type mqtt_packet_type() :: ?RESERVED..?DISCONNECT.
  102. %%------------------------------------------------------------------------------
  103. %% MQTT Connect Return Codes
  104. %%------------------------------------------------------------------------------
  105. -define(CONNACK_ACCEPT, 0). %% Connection accepted
  106. -define(CONNACK_PROTO_VER, 1). %% Unacceptable protocol version
  107. -define(CONNACK_INVALID_ID, 2). %% Client Identifier is correct UTF-8 but not allowed by the Server
  108. -define(CONNACK_SERVER, 3). %% Server unavailable
  109. -define(CONNACK_CREDENTIALS, 4). %% Username or password is malformed
  110. -define(CONNACK_AUTH, 5). %% Client is not authorized to connect
  111. -type mqtt_connack() :: ?CONNACK_ACCEPT..?CONNACK_AUTH.
  112. %%------------------------------------------------------------------------------
  113. %% MQTT Parser and Serialiser
  114. %%------------------------------------------------------------------------------
  115. -define(MAX_LEN, 16#fffffff).
  116. -define(HIGHBIT, 2#10000000).
  117. -define(LOWBITS, 2#01111111).
  118. %%------------------------------------------------------------------------------
  119. %% MQTT Packet Fixed Header
  120. %%------------------------------------------------------------------------------
  121. -record(mqtt_packet_header, {
  122. type = ?RESERVED :: mqtt_packet_type(),
  123. dup = false :: boolean(),
  124. qos = ?QOS_0 :: mqtt_qos(),
  125. retain = false :: boolean()}).
  126. %%------------------------------------------------------------------------------
  127. %% MQTT Packets
  128. %%------------------------------------------------------------------------------
  129. -type mqtt_client_id() :: binary().
  130. -type mqtt_packet_id() :: 1..16#ffff | undefined.
  131. -record(mqtt_packet_connect, {
  132. client_id = <<>> :: mqtt_client_id(),
  133. proto_ver = ?MQTT_PROTO_V311 :: mqtt_vsn(),
  134. proto_name = <<"MQTT">> :: binary(),
  135. will_retain = false :: boolean(),
  136. will_qos = ?QOS_0 :: mqtt_qos(),
  137. will_flag = false :: boolean(),
  138. clean_sess = false :: boolean(),
  139. keep_alive = 60 :: non_neg_integer(),
  140. will_topic = undefined :: undefined | binary(),
  141. will_msg = undefined :: undefined | binary(),
  142. username = undefined :: undefined | binary(),
  143. password = undefined :: undefined | binary()}).
  144. -record(mqtt_packet_connack, {
  145. ack_flags = ?RESERVED :: 0 | 1,
  146. return_code :: mqtt_connack() }).
  147. -record(mqtt_packet_publish, {
  148. topic_name :: binary(),
  149. packet_id :: mqtt_packet_id() }).
  150. -record(mqtt_packet_puback, {
  151. packet_id :: mqtt_packet_id() }).
  152. -record(mqtt_packet_subscribe, {
  153. packet_id :: mqtt_packet_id(),
  154. topic_table :: list({binary(), mqtt_qos()}) }).
  155. -record(mqtt_packet_unsubscribe, {
  156. packet_id :: mqtt_packet_id(),
  157. topics :: list(binary()) }).
  158. -record(mqtt_packet_suback, {
  159. packet_id :: mqtt_packet_id(),
  160. qos_table :: list(mqtt_qos() | 128) }).
  161. -record(mqtt_packet_unsuback, {
  162. packet_id :: mqtt_packet_id() }).
  163. %%------------------------------------------------------------------------------
  164. %% MQTT Control Packet
  165. %%------------------------------------------------------------------------------
  166. -record(mqtt_packet, {
  167. header :: #mqtt_packet_header{},
  168. variable :: #mqtt_packet_connect{} | #mqtt_packet_connack{}
  169. | #mqtt_packet_publish{} | #mqtt_packet_puback{}
  170. | #mqtt_packet_subscribe{} | #mqtt_packet_suback{}
  171. | #mqtt_packet_unsubscribe{} | #mqtt_packet_unsuback{}
  172. | mqtt_packet_id() | undefined,
  173. payload :: binary() | undefined }).
  174. -type mqtt_packet() :: #mqtt_packet{}.
  175. %%------------------------------------------------------------------------------
  176. %% MQTT Packet Match
  177. %%------------------------------------------------------------------------------
  178. -define(CONNECT_PACKET(Var),
  179. #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT}, variable = Var}).
  180. -define(CONNACK_PACKET(ReturnCode),
  181. #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK},
  182. variable = #mqtt_packet_connack{return_code = ReturnCode}}).
  183. -define(CONNACK_PACKET(ReturnCode, SessPresent),
  184. #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK},
  185. variable = #mqtt_packet_connack{ack_flags = SessPresent,
  186. return_code = ReturnCode}}).
  187. -define(PUBLISH_PACKET(Qos, PacketId),
  188. #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  189. qos = Qos},
  190. variable = #mqtt_packet_publish{packet_id = PacketId}}).
  191. -define(PUBLISH_PACKET(Qos, Topic, PacketId, Payload),
  192. #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  193. qos = Qos},
  194. variable = #mqtt_packet_publish{topic_name = Topic,
  195. packet_id = PacketId},
  196. payload = Payload}).
  197. -define(PUBACK_PACKET(Type, PacketId),
  198. #mqtt_packet{header = #mqtt_packet_header{type = Type},
  199. variable = #mqtt_packet_puback{packet_id = PacketId}}).
  200. -define(PUBREL_PACKET(PacketId),
  201. #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREL, qos = ?QOS_1},
  202. variable = #mqtt_packet_puback{packet_id = PacketId}}).
  203. -define(SUBSCRIBE_PACKET(PacketId, TopicTable),
  204. #mqtt_packet{header = #mqtt_packet_header{type = ?SUBSCRIBE, qos = ?QOS_1},
  205. variable = #mqtt_packet_subscribe{packet_id = PacketId,
  206. topic_table = TopicTable}}).
  207. -define(SUBACK_PACKET(PacketId, QosTable),
  208. #mqtt_packet{header = #mqtt_packet_header{type = ?SUBACK},
  209. variable = #mqtt_packet_suback{packet_id = PacketId,
  210. qos_table = QosTable}}).
  211. -define(UNSUBSCRIBE_PACKET(PacketId, Topics),
  212. #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBSCRIBE, qos = ?QOS_1},
  213. variable = #mqtt_packet_unsubscribe{packet_id = PacketId,
  214. topics = Topics}}).
  215. -define(UNSUBACK_PACKET(PacketId),
  216. #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBACK},
  217. variable = #mqtt_packet_unsuback{packet_id = PacketId}}).
  218. -define(PACKET(Type),
  219. #mqtt_packet{header = #mqtt_packet_header{type = Type}}).