emqttd_protocol.hrl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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(IS_QOS(I), (I >= ?QOS_0 andalso I =< ?QOS_2)).
  43. -type mqtt_qos() :: ?QOS_0 | ?QOS_1 | ?QOS_2.
  44. %%------------------------------------------------------------------------------
  45. %% Max ClientId Length. Why 1024? NiDongDe!
  46. %%------------------------------------------------------------------------------
  47. -define(MAX_CLIENTID_LEN, 1024).
  48. %%------------------------------------------------------------------------------
  49. %% MQTT Control Packet Types
  50. %%------------------------------------------------------------------------------
  51. -define(RESERVED, 0). %% Reserved
  52. -define(CONNECT, 1). %% Client request to connect to Server
  53. -define(CONNACK, 2). %% Server to Client: Connect acknowledgment
  54. -define(PUBLISH, 3). %% Publish message
  55. -define(PUBACK, 4). %% Publish acknowledgment
  56. -define(PUBREC, 5). %% Publish received (assured delivery part 1)
  57. -define(PUBREL, 6). %% Publish release (assured delivery part 2)
  58. -define(PUBCOMP, 7). %% Publish complete (assured delivery part 3)
  59. -define(SUBSCRIBE, 8). %% Client subscribe request
  60. -define(SUBACK, 9). %% Server Subscribe acknowledgment
  61. -define(UNSUBSCRIBE, 10). %% Unsubscribe request
  62. -define(UNSUBACK, 11). %% Unsubscribe acknowledgment
  63. -define(PINGREQ, 12). %% PING request
  64. -define(PINGRESP, 13). %% PING response
  65. -define(DISCONNECT, 14). %% Client is disconnecting
  66. -define(TYPE_NAMES, [
  67. 'CONNECT',
  68. 'CONNACK',
  69. 'PUBLISH',
  70. 'PUBACK',
  71. 'PUBREC',
  72. 'PUBREL',
  73. 'PUBCOMP',
  74. 'SUBSCRIBE',
  75. 'SUBACK',
  76. 'UNSUBSCRIBE',
  77. 'UNSUBACK',
  78. 'PINGREQ',
  79. 'PINGRESP',
  80. 'DISCONNECT']).
  81. -type mqtt_packet_type() :: ?RESERVED..?DISCONNECT.
  82. %%------------------------------------------------------------------------------
  83. %% MQTT Connect Return Codes
  84. %%------------------------------------------------------------------------------
  85. -define(CONNACK_ACCEPT, 0). %% Connection accepted
  86. -define(CONNACK_PROTO_VER, 1). %% Unacceptable protocol version
  87. -define(CONNACK_INVALID_ID, 2). %% Client Identifier is correct UTF-8 but not allowed by the Server
  88. -define(CONNACK_SERVER, 3). %% Server unavailable
  89. -define(CONNACK_CREDENTIALS, 4). %% Username or password is malformed
  90. -define(CONNACK_AUTH, 5). %% Client is not authorized to connect
  91. -type mqtt_connack() :: ?CONNACK_ACCEPT..?CONNACK_AUTH.
  92. %%------------------------------------------------------------------------------
  93. %% MQTT Parser and Serialiser
  94. %%------------------------------------------------------------------------------
  95. -define(MAX_LEN, 16#fffffff).
  96. -define(HIGHBIT, 2#10000000).
  97. -define(LOWBITS, 2#01111111).
  98. %%------------------------------------------------------------------------------
  99. %% MQTT Packet Fixed Header
  100. %%------------------------------------------------------------------------------
  101. -record(mqtt_packet_header, {
  102. type = ?RESERVED :: mqtt_packet_type(),
  103. dup = false :: boolean(),
  104. qos = ?QOS_0 :: mqtt_qos(),
  105. retain = false :: boolean()}).
  106. %%------------------------------------------------------------------------------
  107. %% MQTT Packets
  108. %%------------------------------------------------------------------------------
  109. -type mqtt_client_id() :: binary().
  110. -type mqtt_packet_id() :: 1..16#ffff | undefined.
  111. -record(mqtt_packet_connect, {
  112. client_id = <<>> :: mqtt_client_id(),
  113. proto_ver = ?MQTT_PROTO_V311 :: mqtt_vsn(),
  114. proto_name = <<"MQTT">> :: binary(),
  115. will_retain = false :: boolean(),
  116. will_qos = ?QOS_0 :: mqtt_qos(),
  117. will_flag = false :: boolean(),
  118. clean_sess = false :: boolean(),
  119. keep_alive = 60 :: non_neg_integer(),
  120. will_topic = undefined :: undefined | binary(),
  121. will_msg = undefined :: undefined | binary(),
  122. username = undefined :: undefined | binary(),
  123. password = undefined :: undefined | binary()}).
  124. -record(mqtt_packet_connack, {
  125. ack_flags = ?RESERVED :: 0 | 1,
  126. return_code :: mqtt_connack() }).
  127. -record(mqtt_packet_publish, {
  128. topic_name :: binary(),
  129. packet_id :: mqtt_packet_id() }).
  130. -record(mqtt_packet_puback, {
  131. packet_id :: mqtt_packet_id() }).
  132. -record(mqtt_packet_subscribe, {
  133. packet_id :: mqtt_packet_id(),
  134. topic_table :: list({binary(), mqtt_qos()}) }).
  135. -record(mqtt_packet_unsubscribe, {
  136. packet_id :: mqtt_packet_id(),
  137. topics :: list(binary()) }).
  138. -record(mqtt_packet_suback, {
  139. packet_id :: mqtt_packet_id(),
  140. qos_table :: list(mqtt_qos() | 128) }).
  141. -record(mqtt_packet_unsuback, {
  142. packet_id :: mqtt_packet_id() }).
  143. %%------------------------------------------------------------------------------
  144. %% MQTT Control Packet
  145. %%------------------------------------------------------------------------------
  146. -record(mqtt_packet, {
  147. header :: #mqtt_packet_header{},
  148. variable :: #mqtt_packet_connect{} | #mqtt_packet_connack{}
  149. | #mqtt_packet_publish{} | #mqtt_packet_puback{}
  150. | #mqtt_packet_subscribe{} | #mqtt_packet_suback{}
  151. | #mqtt_packet_unsubscribe{} | #mqtt_packet_unsuback{}
  152. | mqtt_packet_id() | undefined,
  153. payload :: binary() | undefined }).
  154. -type mqtt_packet() :: #mqtt_packet{}.
  155. %%------------------------------------------------------------------------------
  156. %% MQTT Packet Match
  157. %%------------------------------------------------------------------------------
  158. -define(CONNECT_PACKET(Var),
  159. #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT}, variable = Var}).
  160. -define(CONNACK_PACKET(ReturnCode),
  161. #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK},
  162. variable = #mqtt_packet_connack{return_code = ReturnCode}}).
  163. -define(PUBLISH_PACKET(Qos, Topic, PacketId, Payload),
  164. #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  165. qos = Qos},
  166. variable = #mqtt_packet_publish{topic_name = Topic,
  167. packet_id = PacketId},
  168. payload = Payload}).
  169. -define(PUBLISH(Qos, PacketId),
  170. #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  171. qos = Qos},
  172. variable = #mqtt_packet_publish{packet_id = PacketId}}).
  173. -define(PUBACK_PACKET(Type, PacketId),
  174. #mqtt_packet{header = #mqtt_packet_header{type = Type},
  175. variable = #mqtt_packet_puback{packet_id = PacketId}}).
  176. -define(PUBREL_PACKET(PacketId),
  177. #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREL, qos = ?QOS_1},
  178. variable = #mqtt_packet_puback{packet_id = PacketId}}).
  179. -define(SUBSCRIBE_PACKET(PacketId, TopicTable),
  180. #mqtt_packet{header = #mqtt_packet_header{type = ?SUBSCRIBE, qos = ?QOS_1},
  181. variable = #mqtt_packet_subscribe{packet_id = PacketId,
  182. topic_table = TopicTable}}).
  183. -define(SUBACK_PACKET(PacketId, QosTable),
  184. #mqtt_packet{header = #mqtt_packet_header{type = ?SUBACK},
  185. variable = #mqtt_packet_suback{packet_id = PacketId,
  186. qos_table = QosTable}}).
  187. -define(UNSUBSCRIBE_PACKET(PacketId, Topics),
  188. #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBSCRIBE, qos = ?QOS_1},
  189. variable = #mqtt_packet_unsubscribe{packet_id = PacketId,
  190. topics = Topics}}).
  191. -define(UNSUBACK_PACKET(PacketId),
  192. #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBACK},
  193. variable = #mqtt_packet_unsuback{packet_id = PacketId}}).
  194. -define(PACKET(Type),
  195. #mqtt_packet{header = #mqtt_packet_header{type = Type}}).