|
|
@@ -1,5 +1,5 @@
|
|
|
%%--------------------------------------------------------------------
|
|
|
-%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>.
|
|
|
+%% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
|
|
|
%%
|
|
|
%% Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
%% you may not use this file except in compliance with the License.
|
|
|
@@ -14,23 +14,32 @@
|
|
|
%% limitations under the License.
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
-%% MQTT Protocol Header
|
|
|
+%%--------------------------------------------------------------------
|
|
|
+%% MQTT SockOpts
|
|
|
+%%--------------------------------------------------------------------
|
|
|
+
|
|
|
+-define(MQTT_SOCKOPTS, [binary, {packet, raw}, {reuseaddr, true},
|
|
|
+ {backlog, 512}, {nodelay, true}]).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Protocol Version and Levels
|
|
|
%%--------------------------------------------------------------------
|
|
|
--define(MQTT_PROTO_V31, 3).
|
|
|
--define(MQTT_PROTO_V311, 4).
|
|
|
+
|
|
|
+-define(MQTT_PROTO_V3, 3).
|
|
|
+-define(MQTT_PROTO_V4, 4).
|
|
|
+-define(MQTT_PROTO_V5, 5).
|
|
|
|
|
|
-define(PROTOCOL_NAMES, [
|
|
|
- {?MQTT_PROTO_V31, <<"MQIsdp">>},
|
|
|
- {?MQTT_PROTO_V311, <<"MQTT">>}]).
|
|
|
+ {?MQTT_PROTO_V3, <<"MQIsdp">>},
|
|
|
+ {?MQTT_PROTO_V4, <<"MQTT">>},
|
|
|
+ {?MQTT_PROTO_V5, <<"MQTT">>}]).
|
|
|
|
|
|
--type(mqtt_vsn() :: ?MQTT_PROTO_V31 | ?MQTT_PROTO_V311).
|
|
|
+-type(mqtt_vsn() :: ?MQTT_PROTO_V3 | ?MQTT_PROTO_V4 | ?MQTT_PROTO_V5).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
-%% MQTT QoS
|
|
|
+%% MQTT QoS Level
|
|
|
%%--------------------------------------------------------------------
|
|
|
+
|
|
|
-define(QOS_0, 0). %% At most once
|
|
|
-define(QOS_1, 1). %% At least once
|
|
|
-define(QOS_2, 2). %% Exactly once
|
|
|
@@ -63,28 +72,30 @@
|
|
|
end).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
-%% Max ClientId Length. Why 1024? NiDongDe...
|
|
|
+%% Max ClientId Length. Why 1024?
|
|
|
%%--------------------------------------------------------------------
|
|
|
+
|
|
|
-define(MAX_CLIENTID_LEN, 1024).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Control Packet Types
|
|
|
%%--------------------------------------------------------------------
|
|
|
--define(RESERVED, 0). %% Reserved
|
|
|
--define(CONNECT, 1). %% Client request to connect to Server
|
|
|
--define(CONNACK, 2). %% Server to Client: Connect acknowledgment
|
|
|
--define(PUBLISH, 3). %% Publish message
|
|
|
--define(PUBACK, 4). %% Publish acknowledgment
|
|
|
--define(PUBREC, 5). %% Publish received (assured delivery part 1)
|
|
|
--define(PUBREL, 6). %% Publish release (assured delivery part 2)
|
|
|
--define(PUBCOMP, 7). %% Publish complete (assured delivery part 3)
|
|
|
--define(SUBSCRIBE, 8). %% Client subscribe request
|
|
|
--define(SUBACK, 9). %% Server Subscribe acknowledgment
|
|
|
--define(UNSUBSCRIBE, 10). %% Unsubscribe request
|
|
|
--define(UNSUBACK, 11). %% Unsubscribe acknowledgment
|
|
|
--define(PINGREQ, 12). %% PING request
|
|
|
--define(PINGRESP, 13). %% PING response
|
|
|
--define(DISCONNECT, 14). %% Client is disconnecting
|
|
|
+-define(RESERVED, 0). %% Reserved
|
|
|
+-define(CONNECT, 1). %% Client request to connect to Server
|
|
|
+-define(CONNACK, 2). %% Server to Client: Connect acknowledgment
|
|
|
+-define(PUBLISH, 3). %% Publish message
|
|
|
+-define(PUBACK, 4). %% Publish acknowledgment
|
|
|
+-define(PUBREC, 5). %% Publish received (assured delivery part 1)
|
|
|
+-define(PUBREL, 6). %% Publish release (assured delivery part 2)
|
|
|
+-define(PUBCOMP, 7). %% Publish complete (assured delivery part 3)
|
|
|
+-define(SUBSCRIBE, 8). %% Client subscribe request
|
|
|
+-define(SUBACK, 9). %% Server Subscribe acknowledgment
|
|
|
+-define(UNSUBSCRIBE, 10). %% Unsubscribe request
|
|
|
+-define(UNSUBACK, 11). %% Unsubscribe acknowledgment
|
|
|
+-define(PINGREQ, 12). %% PING request
|
|
|
+-define(PINGRESP, 13). %% PING response
|
|
|
+-define(DISCONNECT, 14). %% Client is disconnecting
|
|
|
+-define(AUTH, 15). %% Authentication exchange
|
|
|
|
|
|
-define(TYPE_NAMES, [
|
|
|
'CONNECT',
|
|
|
@@ -100,25 +111,28 @@
|
|
|
'UNSUBACK',
|
|
|
'PINGREQ',
|
|
|
'PINGRESP',
|
|
|
- 'DISCONNECT']).
|
|
|
+ 'DISCONNECT',
|
|
|
+ 'AUTH']).
|
|
|
|
|
|
-type(mqtt_packet_type() :: ?RESERVED..?DISCONNECT).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Connect Return Codes
|
|
|
%%--------------------------------------------------------------------
|
|
|
--define(CONNACK_ACCEPT, 0). %% Connection accepted
|
|
|
--define(CONNACK_PROTO_VER, 1). %% Unacceptable protocol version
|
|
|
--define(CONNACK_INVALID_ID, 2). %% Client Identifier is correct UTF-8 but not allowed by the Server
|
|
|
--define(CONNACK_SERVER, 3). %% Server unavailable
|
|
|
--define(CONNACK_CREDENTIALS, 4). %% Username or password is malformed
|
|
|
--define(CONNACK_AUTH, 5). %% Client is not authorized to connect
|
|
|
+
|
|
|
+-define(CONNACK_ACCEPT, 0). %% Connection accepted
|
|
|
+-define(CONNACK_PROTO_VER, 1). %% Unacceptable protocol version
|
|
|
+-define(CONNACK_INVALID_ID, 2). %% Client Identifier is correct UTF-8 but not allowed by the Server
|
|
|
+-define(CONNACK_SERVER, 3). %% Server unavailable
|
|
|
+-define(CONNACK_CREDENTIALS, 4). %% Username or password is malformed
|
|
|
+-define(CONNACK_AUTH, 5). %% Client is not authorized to connect
|
|
|
|
|
|
-type(mqtt_connack() :: ?CONNACK_ACCEPT..?CONNACK_AUTH).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Parser and Serializer
|
|
|
%%--------------------------------------------------------------------
|
|
|
+
|
|
|
-define(MAX_LEN, 16#fffffff).
|
|
|
-define(HIGHBIT, 2#10000000).
|
|
|
-define(LOWBITS, 2#01111111).
|
|
|
@@ -126,76 +140,87 @@
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Packet Fixed Header
|
|
|
%%--------------------------------------------------------------------
|
|
|
+
|
|
|
-record(mqtt_packet_header, {
|
|
|
- type = ?RESERVED :: mqtt_packet_type(),
|
|
|
- dup = false :: boolean(),
|
|
|
- qos = ?QOS_0 :: mqtt_qos(),
|
|
|
- retain = false :: boolean()}).
|
|
|
+ type = ?RESERVED :: mqtt_packet_type(),
|
|
|
+ dup = false :: boolean(),
|
|
|
+ qos = ?QOS_0 :: mqtt_qos(),
|
|
|
+ retain = false :: boolean()}).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Packets
|
|
|
%%--------------------------------------------------------------------
|
|
|
--type(mqtt_client_id() :: binary()).
|
|
|
--type(mqtt_username() :: binary() | undefined).
|
|
|
--type(mqtt_packet_id() :: 1..16#ffff | undefined).
|
|
|
|
|
|
--record(mqtt_packet_connect, {
|
|
|
- client_id = <<>> :: mqtt_client_id(),
|
|
|
- proto_ver = ?MQTT_PROTO_V311 :: mqtt_vsn(),
|
|
|
- proto_name = <<"MQTT">> :: binary(),
|
|
|
- will_retain = false :: boolean(),
|
|
|
- will_qos = ?QOS_0 :: mqtt_qos(),
|
|
|
- will_flag = false :: boolean(),
|
|
|
- clean_sess = false :: boolean(),
|
|
|
- keep_alive = 60 :: non_neg_integer(),
|
|
|
- will_topic = undefined :: undefined | binary(),
|
|
|
- will_msg = undefined :: undefined | binary(),
|
|
|
- username = undefined :: undefined | binary(),
|
|
|
- password = undefined :: undefined | binary()}).
|
|
|
-
|
|
|
--record(mqtt_packet_connack, {
|
|
|
- ack_flags = ?RESERVED :: 0 | 1,
|
|
|
- return_code :: mqtt_connack() }).
|
|
|
-
|
|
|
--record(mqtt_packet_publish, {
|
|
|
- topic_name :: binary(),
|
|
|
- packet_id :: mqtt_packet_id() }).
|
|
|
-
|
|
|
--record(mqtt_packet_puback, {
|
|
|
- packet_id :: mqtt_packet_id() }).
|
|
|
-
|
|
|
--record(mqtt_packet_subscribe, {
|
|
|
- packet_id :: mqtt_packet_id(),
|
|
|
- topic_table :: list({binary(), mqtt_qos()}) }).
|
|
|
-
|
|
|
--record(mqtt_packet_unsubscribe, {
|
|
|
- packet_id :: mqtt_packet_id(),
|
|
|
- topics :: list(binary()) }).
|
|
|
-
|
|
|
--record(mqtt_packet_suback, {
|
|
|
- packet_id :: mqtt_packet_id(),
|
|
|
- qos_table :: list(mqtt_qos() | 128) }).
|
|
|
-
|
|
|
--record(mqtt_packet_unsuback, {
|
|
|
- packet_id :: mqtt_packet_id() }).
|
|
|
+-type(mqtt_client_id() :: binary()).
|
|
|
+-type(mqtt_username() :: binary() | undefined).
|
|
|
+-type(mqtt_packet_id() :: 1..16#ffff | undefined).
|
|
|
+
|
|
|
+-record(mqtt_packet_connect,
|
|
|
+ { client_id = <<>> :: mqtt_client_id(),
|
|
|
+ proto_ver = ?MQTT_PROTO_V4 :: mqtt_vsn(),
|
|
|
+ proto_name = <<"MQTT">> :: binary(),
|
|
|
+ will_retain = false :: boolean(),
|
|
|
+ will_qos = ?QOS_0 :: mqtt_qos(),
|
|
|
+ will_flag = false :: boolean(),
|
|
|
+ clean_sess = false :: boolean(),
|
|
|
+ keep_alive = 60 :: non_neg_integer(),
|
|
|
+ will_topic = undefined :: undefined | binary(),
|
|
|
+ will_msg = undefined :: undefined | binary(),
|
|
|
+ username = undefined :: undefined | binary(),
|
|
|
+ password = undefined :: undefined | binary()
|
|
|
+ }).
|
|
|
+
|
|
|
+-record(mqtt_packet_connack,
|
|
|
+ { ack_flags = ?RESERVED :: 0 | 1,
|
|
|
+ return_code :: mqtt_connack()
|
|
|
+ }).
|
|
|
+
|
|
|
+-record(mqtt_packet_publish,
|
|
|
+ { topic_name :: binary(),
|
|
|
+ packet_id :: mqtt_packet_id()
|
|
|
+ }).
|
|
|
+
|
|
|
+-record(mqtt_packet_puback,
|
|
|
+ { packet_id :: mqtt_packet_id() }).
|
|
|
+
|
|
|
+-record(mqtt_packet_subscribe,
|
|
|
+ { packet_id :: mqtt_packet_id(),
|
|
|
+ topic_table :: list({binary(), mqtt_qos()})
|
|
|
+ }).
|
|
|
+
|
|
|
+-record(mqtt_packet_unsubscribe,
|
|
|
+ { packet_id :: mqtt_packet_id(),
|
|
|
+ topics :: list(binary())
|
|
|
+ }).
|
|
|
+
|
|
|
+-record(mqtt_packet_suback,
|
|
|
+ { packet_id :: mqtt_packet_id(),
|
|
|
+ qos_table :: list(mqtt_qos() | 128)
|
|
|
+ }).
|
|
|
+
|
|
|
+-record(mqtt_packet_unsuback,
|
|
|
+ { packet_id :: mqtt_packet_id() }).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Control Packet
|
|
|
%%--------------------------------------------------------------------
|
|
|
--record(mqtt_packet, {
|
|
|
- header :: #mqtt_packet_header{},
|
|
|
- variable :: #mqtt_packet_connect{} | #mqtt_packet_connack{}
|
|
|
- | #mqtt_packet_publish{} | #mqtt_packet_puback{}
|
|
|
- | #mqtt_packet_subscribe{} | #mqtt_packet_suback{}
|
|
|
- | #mqtt_packet_unsubscribe{} | #mqtt_packet_unsuback{}
|
|
|
- | mqtt_packet_id() | undefined,
|
|
|
- payload :: binary() | undefined }).
|
|
|
|
|
|
--type mqtt_packet() :: #mqtt_packet{}.
|
|
|
+-record(mqtt_packet,
|
|
|
+ { header :: #mqtt_packet_header{},
|
|
|
+ variable :: #mqtt_packet_connect{} | #mqtt_packet_connack{}
|
|
|
+ | #mqtt_packet_publish{} | #mqtt_packet_puback{}
|
|
|
+ | #mqtt_packet_subscribe{} | #mqtt_packet_suback{}
|
|
|
+ | #mqtt_packet_unsubscribe{} | #mqtt_packet_unsuback{}
|
|
|
+ | mqtt_packet_id() | undefined,
|
|
|
+ payload :: binary() | undefined
|
|
|
+ }).
|
|
|
+
|
|
|
+-type(mqtt_packet() :: #mqtt_packet{}).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% MQTT Packet Match
|
|
|
%%--------------------------------------------------------------------
|
|
|
+
|
|
|
-define(CONNECT_PACKET(Var),
|
|
|
#mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT}, variable = Var}).
|
|
|
|