emqtt_packet.hrl 8.8 KB

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