emqttd.hrl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2012-2016 Feng Lee <feng@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. %% MQTT Broker Header
  17. %%--------------------------------------------------------------------
  18. %% Banner
  19. %%--------------------------------------------------------------------
  20. -define(COPYRIGHT, "Copyright (C) 2012-2016, Feng Lee <feng@emqtt.io>").
  21. -define(LICENSE_MESSAGE, "Licensed under the Apache License, Version 2.0").
  22. -define(PROTOCOL_VERSION, "MQTT/3.1.1").
  23. -define(ERTS_MINIMUM, "6.0").
  24. %% System Topics.
  25. -define(SYSTOP, <<"$SYS">>).
  26. %% Queue Topics.
  27. -define(QTop, <<"$Q">>).
  28. %%--------------------------------------------------------------------
  29. %% PubSub
  30. %%--------------------------------------------------------------------
  31. -type pubsub() :: publish | subscribe.
  32. -define(IS_PUBSUB(PS), (PS =:= publish orelse PS =:= subscribe)).
  33. %%--------------------------------------------------------------------
  34. %% MQTT Topic
  35. %%--------------------------------------------------------------------
  36. -record(mqtt_topic, {
  37. topic :: binary(),
  38. flags :: [retained | static]
  39. }).
  40. -type mqtt_topic() :: #mqtt_topic{}.
  41. %%--------------------------------------------------------------------
  42. %% MQTT Subscription
  43. %%--------------------------------------------------------------------
  44. -record(mqtt_subscription, {
  45. subid :: binary() | atom(),
  46. topic :: binary(),
  47. qos = 0 :: 0 | 1 | 2
  48. }).
  49. -type mqtt_subscription() :: #mqtt_subscription{}.
  50. %%--------------------------------------------------------------------
  51. %% MQTT Route
  52. %%--------------------------------------------------------------------
  53. -record(mqtt_route, {
  54. topic :: binary(),
  55. node :: node()
  56. }).
  57. -type mqtt_route() :: #mqtt_route{}.
  58. %%--------------------------------------------------------------------
  59. %% MQTT Client
  60. %%--------------------------------------------------------------------
  61. -type ws_header_key() :: atom() | binary() | string().
  62. -type ws_header_val() :: atom() | binary() | string() | integer().
  63. -record(mqtt_client, {
  64. client_id :: binary() | undefined,
  65. client_pid :: pid(),
  66. username :: binary() | undefined,
  67. peername :: {inet:ip_address(), integer()},
  68. clean_sess :: boolean(),
  69. proto_ver :: 3 | 4,
  70. keepalive = 0,
  71. will_topic :: undefined | binary(),
  72. ws_initial_headers :: list({ws_header_key(), ws_header_val()}),
  73. connected_at :: erlang:timestamp()
  74. }).
  75. -type mqtt_client() :: #mqtt_client{}.
  76. %%--------------------------------------------------------------------
  77. %% MQTT Session
  78. %%--------------------------------------------------------------------
  79. -record(mqtt_session, {
  80. client_id :: binary(),
  81. sess_pid :: pid(),
  82. persistent :: boolean()
  83. }).
  84. -type mqtt_session() :: #mqtt_session{}.
  85. %%--------------------------------------------------------------------
  86. %% MQTT Message
  87. %%--------------------------------------------------------------------
  88. -type mqtt_msgid() :: binary() | undefined.
  89. -type mqtt_pktid() :: 1..16#ffff | undefined.
  90. -record(mqtt_message, {
  91. msgid :: mqtt_msgid(), %% Global unique message ID
  92. pktid :: mqtt_pktid(), %% PacketId
  93. topic :: binary(), %% Topic that the message is published to
  94. from :: binary() | atom(), %% ClientId of the publisher
  95. sender :: binary() | undefined, %% Username of the publisher
  96. qos = 0 :: 0 | 1 | 2, %% Message QoS
  97. flags = [] :: [retain | dup | sys], %% Message Flags
  98. retain = false :: boolean(), %% Retain flag
  99. dup = false :: boolean(), %% Dup flag
  100. sys = false :: boolean(), %% $SYS flag
  101. payload :: binary(), %% Payload
  102. timestamp :: erlang:timestamp() %% os:timestamp
  103. }).
  104. -type mqtt_message() :: #mqtt_message{}.
  105. %%--------------------------------------------------------------------
  106. %% MQTT Alarm
  107. %%--------------------------------------------------------------------
  108. -record(mqtt_alarm, {
  109. id :: binary(),
  110. severity :: warning | error | critical,
  111. title :: iolist() | binary(),
  112. summary :: iolist() | binary(),
  113. timestamp :: erlang:timestamp() %% Timestamp
  114. }).
  115. -type mqtt_alarm() :: #mqtt_alarm{}.
  116. %%--------------------------------------------------------------------
  117. %% MQTT Plugin
  118. %%--------------------------------------------------------------------
  119. -record(mqtt_plugin, {
  120. name,
  121. version,
  122. descr,
  123. config,
  124. active = false
  125. }).
  126. -type mqtt_plugin() :: #mqtt_plugin{}.
  127. %%--------------------------------------------------------------------
  128. %% MQTT CLI Command
  129. %% For example: 'broker metrics'
  130. %%--------------------------------------------------------------------
  131. -record(mqtt_cli, {
  132. name,
  133. action,
  134. args = [],
  135. opts = [],
  136. usage,
  137. descr
  138. }).
  139. -type mqtt_cli() :: #mqtt_cli{}.