emqtt.hrl

This commit is contained in:
Ery Lee 2015-04-15 17:21:02 +08:00
parent 3cde3c5001
commit 99633fb815
11 changed files with 88 additions and 58 deletions

View File

@ -0,0 +1,69 @@
%%%-----------------------------------------------------------------------------
%%% @Copyright (C) 2012-2015, Feng Lee <feng@emqtt.io>
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in all
%%% copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
%%% SOFTWARE.
%%%-----------------------------------------------------------------------------
%%% @doc
%%% MQTT Common Header.
%%%
%%% @end
%%%-----------------------------------------------------------------------------
%%------------------------------------------------------------------------------
%% MQTT Protocol Version and Levels
%%------------------------------------------------------------------------------
-define(MQTT_PROTO_V31, 3).
-define(MQTT_PROTO_V311, 4).
-define(PROTOCOL_NAMES, [
{?MQTT_PROTO_V31, <<"MQIsdp">>},
{?MQTT_PROTO_V311, <<"MQTT">>}]).
-type mqtt_vsn() :: ?MQTT_PROTO_V31 | ?MQTT_PROTO_V311.
%%------------------------------------------------------------------------------
%% QoS Levels
%%------------------------------------------------------------------------------
-define(QOS_0, 0).
-define(QOS_1, 1).
-define(QOS_2, 2).
-define(IS_QOS(I), (I >= ?QOS_0 andalso I =< ?QOS_2)).
-type mqtt_qos() :: ?QOS_0 | ?QOS_1 | ?QOS_2.
%%------------------------------------------------------------------------------
%% MQTT Message
%%------------------------------------------------------------------------------
-type mqtt_msgid() :: undefined | 1..16#ffff.
-record(mqtt_message, {
%% topic is first for message may be retained
topic :: binary(),
qos = ?QOS_0 :: mqtt_qos(),
retain = false :: boolean(),
dup = false :: boolean(),
msgid :: mqtt_msgid(),
payload :: binary()
}).
-type mqtt_message() :: #mqtt_message{}.

View File

@ -25,33 +25,6 @@
%%% @end
%%%-----------------------------------------------------------------------------
-author("feng@emqtt.io").
%%------------------------------------------------------------------------------
%% MQTT Protocol Version and Levels
%%------------------------------------------------------------------------------
-define(MQTT_PROTO_V31, 3).
-define(MQTT_PROTO_V311, 4).
-define(PROTOCOL_NAMES, [
{?MQTT_PROTO_V31, <<"MQIsdp">>},
{?MQTT_PROTO_V311, <<"MQTT">>}]).
-type mqtt_vsn() :: ?MQTT_PROTO_V31 | ?MQTT_PROTO_V311.
%%------------------------------------------------------------------------------
%% QoS Levels
%%------------------------------------------------------------------------------
-define(QOS_0, 0).
-define(QOS_1, 1).
-define(QOS_2, 2).
-define(QOS_ERR, 128).
-define(IS_QOS(I), (I >= ?QOS_0 andalso I =< ?QOS_2)).
-type mqtt_qos() :: ?QOS_0 | ?QOS_1 | ?QOS_2.
%%------------------------------------------------------------------------------
%% Max ClientId Length. Why 1024? NiDongDe!
%%------------------------------------------------------------------------------

View File

@ -2,9 +2,7 @@
[
{description, "Erlang MQTT Common Library"},
{vsn, "0.6.0"},
{modules, [
emqtt_parser
]},
{modules, []},
{registered, []},
{applications, [
kernel,

View File

@ -1,18 +0,0 @@
-module(emqtt).
%% emqtt: emqtt library's entry point.
-export([my_func/0]).
%% API
my_func() ->
ok().
%% Internals
ok() ->
ok.
%% End of Module.

View File

@ -28,6 +28,8 @@
-author("feng@emqtt.io").
-include("emqtt.hrl").
-include("emqtt_packet.hrl").
%% API

View File

@ -28,6 +28,8 @@
-author("feng@emqtt.io").
-include("emqtt.hrl").
-include("emqtt_packet.hrl").
%% API

View File

@ -28,6 +28,8 @@
-author("feng@emqtt.io").
-include("emqtt.hrl").
-include("emqtt_packet.hrl").
%% API

View File

@ -26,6 +26,7 @@
%%%-----------------------------------------------------------------------------
-module(emqtt_parser_tests).
-include("emqtt.hrl").
-include("emqtt_packet.hrl").
-ifdef(TEST).
@ -41,9 +42,9 @@ parse_connect_test() ->
dup = false,
qos = 0,
retain = false},
variable = #mqtt_packet_connect{proto_ver = 3,
variable = #mqtt_packet_connect{proto_ver = 3,
proto_name = <<"MQIsdp">>,
client_id = <<"mosqpub/10451-iMac.loca">>,
clientid = <<"mosqpub/10451-iMac.loca">>,
clean_sess = true,
keep_alive = 60}}, <<>>}, emqtt_parser:parse(V31ConnBin, State)),
%% CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
@ -53,11 +54,11 @@ parse_connect_test() ->
dup = false,
qos = 0,
retain = false},
variable = #mqtt_packet_connect{proto_ver = 4,
variable = #mqtt_packet_connect{proto_ver = 4,
proto_name = <<"MQTT">>,
client_id = <<"mosqpub/10451-iMac.loca">>,
clientid = <<"mosqpub/10451-iMac.loca">>,
clean_sess = true,
keep_alive = 60 } }, <<>>}, emqtt_parser:parse(V311ConnBin, State)),
keep_alive = 60 } }, <<>>}, emqtt_parser:parse(V311ConnBin, State)),
%% CONNECT(Qos=0, Retain=false, Dup=false, ClientId="", ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60)
V311ConnWithoutClientId = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>,
@ -68,7 +69,7 @@ parse_connect_test() ->
retain = false},
variable = #mqtt_packet_connect{proto_ver = 4,
proto_name = <<"MQTT">>,
client_id = <<>>,
clientid = <<>>,
clean_sess = true,
keep_alive = 60 } }, <<>>}, emqtt_parser:parse(V311ConnWithoutClientId, State)),
%%CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10452-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=test, Password=******, Will(Qos=1, Retain=false, Topic=/will, Msg=willmsg))
@ -80,7 +81,7 @@ parse_connect_test() ->
retain = false},
variable = #mqtt_packet_connect{proto_ver = 3,
proto_name = <<"MQIsdp">>,
client_id = <<"mosqpub/10452-iMac.loca">>,
clientid = <<"mosqpub/10452-iMac.loca">>,
clean_sess = true,
keep_alive = 60,
will_retain = false,

View File

@ -26,6 +26,7 @@
%%%-----------------------------------------------------------------------------
-module(emqtt_serialiser_tests).
-include("emqtt.hrl").
-include("emqtt_packet.hrl").
-ifdef(TEST).

View File

@ -42,13 +42,13 @@
-export([retain/1, redeliver/2]).
mnesia(create) ->
ok = emqtt_mnesia:create_table(message, [
ok = emqttd_mnesia:create_table(message, [
{type, ordered_set},
{ram_copies, [node()]},
{record_name, mqtt_message},
{attributes, record_info(fields, mqtt_message)}]);
mnesia(replicate) ->
ok = emqtt_mnesia:copy_table(message).
ok = emqttd_mnesia:copy_table(message).

View File

@ -47,7 +47,7 @@
{app, lager, [{mod_cond, app}, {incl_cond, include}]},
{app, esockd, [{mod_cond, app}, {incl_cond, include}]},
{app, mochiweb, [{mod_cond, app}, {incl_cond, include}]},
{app, emqtt, [{mod_cond, app}, {incl_cond, include}]}
{app, emqtt, [{mod_cond, app}, {incl_cond, include}]},
{app, emqttd, [{mod_cond, app}, {incl_cond, include}]}
]}.