emqttd.hrl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. %%--------------------------------------------------------------------
  17. %% Banner
  18. %%--------------------------------------------------------------------
  19. -define(COPYRIGHT, "Copyright (c) 2013-2017 EMQ Enterprise, Inc.").
  20. -define(LICENSE_MESSAGE, "Licensed under the Apache License, Version 2.0").
  21. -define(PROTOCOL_VERSION, "MQTT/5.0").
  22. -define(ERTS_MINIMUM, "8.0").
  23. %%--------------------------------------------------------------------
  24. %% Sys/Queue/Share Topics' Prefix
  25. %%--------------------------------------------------------------------
  26. -define(SYSTOP, <<"$SYS/">>). %% System Topic
  27. -define(QUEUE, <<"$queue/">>). %% Queue Topic
  28. -define(SHARE, <<"$share/">>). %% Shared Topic
  29. %%--------------------------------------------------------------------
  30. %% PubSub
  31. %%--------------------------------------------------------------------
  32. -type(pubsub() :: publish | subscribe).
  33. -define(PS(PS), (PS =:= publish orelse PS =:= subscribe)).
  34. %%--------------------------------------------------------------------
  35. %% MQTT Topic
  36. %%--------------------------------------------------------------------
  37. -record(mqtt_topic,
  38. { topic :: binary(),
  39. flags = [] :: [retained | static]
  40. }).
  41. -type(mqtt_topic() :: #mqtt_topic{}).
  42. %%--------------------------------------------------------------------
  43. %% MQTT Subscription
  44. %%--------------------------------------------------------------------
  45. -record(mqtt_subscription,
  46. { subid :: binary() | atom(),
  47. topic :: binary(),
  48. qos :: 0 | 1 | 2
  49. }).
  50. -type(mqtt_subscription() :: #mqtt_subscription{}).
  51. %%--------------------------------------------------------------------
  52. %% MQTT Client
  53. %%--------------------------------------------------------------------
  54. -type(ws_header_key() :: atom() | binary() | string()).
  55. -type(ws_header_val() :: atom() | binary() | string() | integer()).
  56. -record(mqtt_client,
  57. { client_id :: binary() | undefined,
  58. client_pid :: pid(),
  59. username :: binary() | undefined,
  60. peername :: {inet:ip_address(), inet:port_number()},
  61. clean_sess :: boolean(),
  62. proto_ver :: 3 | 4,
  63. keepalive = 0,
  64. will_topic :: undefined | binary(),
  65. ws_initial_headers :: list({ws_header_key(), ws_header_val()}),
  66. mountpoint :: undefined | binary(),
  67. connected_at :: erlang:timestamp()
  68. }).
  69. -type(mqtt_client() :: #mqtt_client{}).
  70. %%--------------------------------------------------------------------
  71. %% MQTT Session
  72. %%--------------------------------------------------------------------
  73. -record(mqtt_session,
  74. { client_id :: binary(),
  75. sess_pid :: pid(),
  76. clean_sess :: boolean()
  77. }).
  78. -type(mqtt_session() :: #mqtt_session{}).
  79. %%--------------------------------------------------------------------
  80. %% MQTT Message
  81. %%--------------------------------------------------------------------
  82. -type(mqtt_msg_id() :: binary() | undefined).
  83. -type(mqtt_pktid() :: 1..16#ffff | undefined).
  84. -type(mqtt_msg_from() :: atom() | {binary(), undefined | binary()}).
  85. -record(mqtt_message,
  86. { %% Global unique message ID
  87. id :: mqtt_msg_id(),
  88. %% PacketId
  89. pktid :: mqtt_pktid(),
  90. %% ClientId and Username
  91. from :: mqtt_msg_from(),
  92. %% Topic that the message is published to
  93. topic :: binary(),
  94. %% Message QoS
  95. qos = 0 :: 0 | 1 | 2,
  96. %% Message Flags
  97. flags = [] :: [retain | dup | sys],
  98. %% Retain flag
  99. retain = false :: boolean(),
  100. %% Dup flag
  101. dup = false :: boolean(),
  102. %% $SYS flag
  103. sys = false :: boolean(),
  104. %% Headers
  105. headers = [] :: list(),
  106. %% Payload
  107. payload :: binary(),
  108. %% Timestamp
  109. timestamp :: erlang:timestamp()
  110. }).
  111. -type(mqtt_message() :: #mqtt_message{}).
  112. %%--------------------------------------------------------------------
  113. %% MQTT Delivery
  114. %%--------------------------------------------------------------------
  115. -record(mqtt_delivery,
  116. { sender :: pid(), %% Pid of the sender/publisher
  117. message :: mqtt_message(), %% Message
  118. flows :: list()
  119. }).
  120. -type(mqtt_delivery() :: #mqtt_delivery{}).
  121. %%--------------------------------------------------------------------
  122. %% MQTT Route
  123. %%--------------------------------------------------------------------
  124. -record(mqtt_route,
  125. { topic :: binary(),
  126. node :: node()
  127. }).
  128. -type(mqtt_route() :: #mqtt_route{}).
  129. %%--------------------------------------------------------------------
  130. %% MQTT Alarm
  131. %%--------------------------------------------------------------------
  132. -record(mqtt_alarm,
  133. { id :: binary(),
  134. severity :: warning | error | critical,
  135. title :: iolist() | binary(),
  136. summary :: iolist() | binary(),
  137. timestamp :: erlang:timestamp()
  138. }).
  139. -type(mqtt_alarm() :: #mqtt_alarm{}).
  140. %%--------------------------------------------------------------------
  141. %% MQTT Plugin
  142. %%--------------------------------------------------------------------
  143. -record(mqtt_plugin, { name, version, descr, active = false }).
  144. -type(mqtt_plugin() :: #mqtt_plugin{}).
  145. %%--------------------------------------------------------------------
  146. %% MQTT CLI Command. For example: 'broker metrics'
  147. %%--------------------------------------------------------------------
  148. -record(mqtt_cli, { name, action, args = [], opts = [], usage, descr }).
  149. -type(mqtt_cli() :: #mqtt_cli{}).