Skip to content
Permalink
Browse files

Add MQTT support

  • Loading branch information...
zinid committed Feb 25, 2019
1 parent 3491fa3 commit a3df791373c30ccc79a6082f4c910a378d726cdc
Showing with 3,748 additions and 0 deletions.
  1. +183 −0 include/mqtt.hrl
  2. +1 −0 rebar.config
  3. +561 −0 src/mod_mqtt.erl
  4. +132 −0 src/mod_mqtt_mnesia.erl
  5. +1,318 −0 src/mod_mqtt_session.erl
  6. +151 −0 src/mod_mqtt_sql.erl
  7. +1,402 −0 src/mqtt_codec.erl
@@ -0,0 +1,183 @@
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2002-2019 ProcessOne, SARL. All Rights Reserved.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%%-------------------------------------------------------------------
-define(MQTT_VERSION_4, 4).
-define(MQTT_VERSION_5, 5).

-record(connect, {proto_level = 4 :: non_neg_integer(),
will :: undefined | publish(),
clean_start = true :: boolean(),
keep_alive = 0 :: non_neg_integer(),
client_id = <<>> :: binary(),
username = <<>> :: binary(),
password = <<>> :: binary(),
will_properties = #{} :: properties(),
properties = #{} :: properties()}).
-record(connack, {session_present = false :: boolean(),
code = success :: reason_code(),
properties = #{} :: properties()}).

-record(publish, {id :: undefined | non_neg_integer(),
dup = false :: boolean(),
qos = 0 :: qos(),
retain = false :: boolean(),
topic :: binary(),
payload :: binary(),
properties = #{} :: properties(),
meta = #{} :: map()}).
-record(puback, {id :: non_neg_integer(),
code = success :: reason_code(),
properties = #{} :: properties()}).
-record(pubrec, {id :: non_neg_integer(),
code = success :: reason_code(),
properties = #{} :: properties()}).
-record(pubrel, {id :: non_neg_integer(),
code = success :: reason_code(),
properties = #{} :: properties(),
meta = #{} :: map()}).
-record(pubcomp, {id :: non_neg_integer(),
code = success :: reason_code(),
properties = #{} :: properties()}).

-record(subscribe, {id :: non_neg_integer(),
filters :: [{binary(), sub_opts()}],
properties = #{} :: properties(),
meta = #{} :: map()}).
-record(suback, {id :: non_neg_integer(),
codes = [] :: [char() | reason_code()],
properties = #{} :: properties()}).

-record(unsubscribe, {id :: non_neg_integer(),
filters :: [binary()],
properties = #{} :: properties(),
meta = #{} :: map()}).
-record(unsuback, {id :: non_neg_integer(),
codes = [] :: [reason_code()],
properties = #{} :: properties()}).

-record(pingreq, {meta = #{} :: map()}).
-record(pingresp, {}).

-record(disconnect, {code = 'normal-disconnection' :: reason_code(),
properties = #{} :: properties()}).

-record(auth, {code = success :: reason_code(),
properties = #{} :: properties()}).

-record(sub_opts, {qos = 0 :: qos(),
no_local = false :: boolean(),
retain_as_published = false :: boolean(),
retain_handling = 0 :: 0..2}).

-type qos() :: 0|1|2.
-type sub_opts() :: #sub_opts{}.
-type utf8_pair() :: {binary(), binary()}.
-type properties() :: map().
-type property() :: assigned_client_identifier |
authentication_data |
authentication_method |
content_type |
correlation_data |
maximum_packet_size |
maximum_qos |
message_expiry_interval |
payload_format_indicator |
reason_string |
receive_maximum |
request_problem_information |
request_response_information |
response_information |
response_topic |
retain_available |
server_keep_alive |
server_reference |
session_expiry_interval |
shared_subscription_available |
subscription_identifier |
subscription_identifiers_available |
topic_alias |
topic_alias_maximum |
user_property |
wildcard_subscription_available |
will_delay_interval.
-type reason_code() :: 'success' |
'normal-disconnection' |
'granted-qos-0' |
'granted-qos-1' |
'granted-qos-2' |
'disconnect-with-will-message' |
'no-matching-subscribers' |
'no-subscription-existed' |
'continue-authentication' |
're-authenticate' |
'unspecified-error' |
'malformed-packet' |
'protocol-error' |
'implementation-specific-error' |
'unsupported-protocol-version' |
'client-identifier-not-valid' |
'bad-user-name-or-password' |
'not-authorized' |
'server-unavailable' |
'server-busy' |
'banned' |
'server-shutting-down' |
'bad-authentication-method' |
'keep-alive-timeout' |
'session-taken-over' |
'topic-filter-invalid' |
'topic-name-invalid' |
'packet-identifier-in-use' |
'packet-identifier-not-found' |
'receive-maximum-exceeded' |
'topic-alias-invalid' |
'packet-too-large' |
'message-rate-too-high' |
'quota-exceeded' |
'administrative-action' |
'payload-format-invalid' |
'retain-not-supported' |
'qos-not-supported' |
'use-another-server' |
'server-moved' |
'shared-subscriptions-not-supported' |
'connection-rate-exceeded' |
'maximum-connect-time' |
'subscription-identifiers-not-supported' |
'wildcard-subscriptions-not-supported'.

-type connect() :: #connect{}.
-type connack() :: #connack{}.
-type publish() :: #publish{}.
-type puback() :: #puback{}.
-type pubrel() :: #pubrel{}.
-type pubrec() :: #pubrec{}.
-type pubcomp() :: #pubcomp{}.
-type subscribe() :: #subscribe{}.
-type suback() :: #suback{}.
-type unsubscribe() :: #unsubscribe{}.
-type unsuback() :: #unsuback{}.
-type pingreq() :: #pingreq{}.
-type pingresp() :: #pingresp{}.
-type disconnect() :: #disconnect{}.
-type auth() :: #auth{}.

-type mqtt_packet() :: connect() | connack() | publish() | puback() |
pubrel() | pubrec() | pubcomp() | subscribe() |
suback() | unsubscribe() | unsuback() | pingreq() |
pingresp() | disconnect() | auth().
-type mqtt_version() :: ?MQTT_VERSION_4 | ?MQTT_VERSION_5.
@@ -31,6 +31,7 @@
{pkix, ".*", {git, "https://github.com/processone/pkix", {tag, "1.0.0"}}},
{jose, ".*", {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.8.4"}}},
{eimp, ".*", {git, "https://github.com/processone/eimp", {tag, "1.0.9"}}},
{mqtree, ".*", {git, "https://github.com/processone/mqtree", {tag, "1.0.1"}}},
{if_var_true, stun, {stun, ".*", {git, "https://github.com/processone/stun", {tag, "1.0.26"}}}},
{if_var_true, sip, {esip, ".*", {git, "https://github.com/processone/esip", {tag, "1.0.27"}}}},
{if_var_true, mysql, {p1_mysql, ".*", {git, "https://github.com/processone/p1_mysql",
Oops, something went wrong.

0 comments on commit a3df791

Please sign in to comment.
You can’t perform that action at this time.