diff --git a/apps/emqx_gateway/etc/emqx_sn.conf b/apps/emqx_gateway/etc/emqx_sn.conf deleted file mode 100644 index e05f1e7be..000000000 --- a/apps/emqx_gateway/etc/emqx_sn.conf +++ /dev/null @@ -1,53 +0,0 @@ -##-------------------------------------------------------------------- -## MQTT-SN -##-------------------------------------------------------------------- - -## The UDP port which emq-sn is listening on. -## -## Value: IP:Port | Port -## -## Examples: 1884, "127.0.0.1:1884", "::1:1884" -mqtt.sn.port = 1884 - -## The duration that emqx-sn broadcast ADVERTISE message through. -## -## Value: Duration -mqtt.sn.advertise_duration = 15m - -## The MQTT-SN Gateway id in ADVERTISE message. -## -## Value: Number -mqtt.sn.gateway_id = 1 - -## To control whether write statistics data into ETS table for dashbord to read. -## -## Value: on | off -mqtt.sn.enable_stats = off - -## To control whether accept and process the received publish message with qos=-1. -## -## Value: on | off -mqtt.sn.enable_qos3 = off - -## MQTT SN idle timeout, specified in seconds. -## -## Value: Duration -mqtt.sn.idle_timeout = 30s - -## The pre-defined topic name corresponding to the pre-defined topic id of N. -## Note that the pre-defined topic id of 0 is reserved. -mqtt.sn.predefined.topic.0 = reserved -mqtt.sn.predefined.topic.1 = "/predefined/topic/name/hello" -mqtt.sn.predefined.topic.2 = "/predefined/topic/name/nice" - -## Default username for MQTT-SN. This parameter is optional. If specified, -## emq-sn will connect EMQ core with this username. It is useful if any auth -## plug-in is enabled. -## -## Value: String -mqtt.sn.username = mqtt_sn_user - -## This parameter is optional. Pair with username above. -## -## Value: String -mqtt.sn.password = abc diff --git a/apps/emqx_gateway/etc/priv/emqx_sn.schema b/apps/emqx_gateway/etc/priv/emqx_sn.schema deleted file mode 100644 index edc76db37..000000000 --- a/apps/emqx_gateway/etc/priv/emqx_sn.schema +++ /dev/null @@ -1,70 +0,0 @@ -%%-*- mode: erlang -*- -%% emqx_sn config mapping -{mapping, "mqtt.sn.port", "emqx_sn.port", [ - {default, 1884}, - {datatype, [integer, ip]} -]}. - -{translation, "emqx_sn.port", fun(Conf) -> - case cuttlefish:conf_get("mqtt.sn.port", Conf, undefined) of - Port when is_integer(Port) -> - {{0,0,0,0}, Port}; - {Ip, Port} -> - case inet:parse_address(Ip) of - {ok ,R} -> {R, Port}; - _ -> {Ip, Port} - end - end -end}. - -{mapping, "mqtt.sn.advertise_duration", "emqx_sn.advertise_duration", [ - {default, "15s"}, - {datatype, {duration, ms}} -]}. - -{mapping, "mqtt.sn.gateway_id", "emqx_sn.gateway_id", [ - {default, 1}, - {datatype, integer} -]}. - -{mapping, "mqtt.sn.username", "emqx_sn.username", [ - {datatype, string} -]}. - -{mapping, "mqtt.sn.password", "emqx_sn.password", [ - {datatype, string} -]}. - -{mapping, "mqtt.sn.idle_timeout", "emqx_sn.idle_timeout", [ - {default, "30s"}, - {datatype, {duration, ms}} -]}. - -{mapping, "mqtt.sn.enable_stats", "emqx_sn.enable_stats", [ - {datatype, flag} -]}. - -{mapping, "mqtt.sn.enable_qos3", "emqx_sn.enable_qos3", [ - {datatype, flag} -]}. - -{mapping, "mqtt.sn.predefined.topic.$id", "emqx_sn.predefined", [ - {datatype, string} -]}. - -{translation, "emqx_sn.username", fun(Conf) -> - Username = cuttlefish:conf_get("mqtt.sn.username", Conf), - list_to_binary(Username) -end}. - -{translation, "emqx_sn.password", fun(Conf) -> - Password = cuttlefish:conf_get("mqtt.sn.password", Conf), - list_to_binary(Password) -end}. - -{translation, "emqx_sn.predefined", fun(Conf) -> - List = cuttlefish_variable:filter_by_prefix("mqtt.sn.predefined.topic", Conf), - TopicIdList = lists:sort([{list_to_integer(I), iolist_to_binary(TopicName)} - || {["mqtt", "sn", "predefined", "topic", I], TopicName} - <- List, I =/= "0"]) -end}. diff --git a/apps/emqx_gateway/src/mqttsn/emqx_sn_app.erl b/apps/emqx_gateway/src/mqttsn/emqx_sn_app.erl deleted file mode 100644 index 9575523f8..000000000 --- a/apps/emqx_gateway/src/mqttsn/emqx_sn_app.erl +++ /dev/null @@ -1,148 +0,0 @@ -%%-------------------------------------------------------------------- -%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. 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. -%%-------------------------------------------------------------------- - --module(emqx_sn_app). - --behaviour(application). - --emqx_plugin(protocol). - --export([ start/2 - , stop/1 - ]). - --export([ start_listeners/0 - , start_listener/1 - , start_listener/3 - , stop_listeners/0 - , stop_listener/1 - , stop_listener/3 - ]). - --define(UDP_SOCKOPTS, []). - --type(listener() :: {esockd:proto(), esockd:listen_on(), [esockd:option()]}). - -%%-------------------------------------------------------------------- -%% Application -%%-------------------------------------------------------------------- - -start(_Type, _Args) -> - Addr = application:get_env(emqx_sn, port, 1884), - GwId = application:get_env(emqx_sn, gateway_id, 1), - PredefTopics = application:get_env(emqx_sn, predefined, []), - {ok, Sup} = emqx_sn_sup:start_link(Addr, GwId, PredefTopics), - start_listeners(), - {ok, Sup}. - -stop(_State) -> - stop_listeners(), - ok. - -%%-------------------------------------------------------------------- -%% Listners -%%-------------------------------------------------------------------- - --spec start_listeners() -> ok. -start_listeners() -> - lists:foreach(fun start_listener/1, listeners_confs()). - --spec start_listener(listener()) -> ok. -start_listener({Proto, ListenOn, Options}) -> - case start_listener(Proto, ListenOn, Options) of - {ok, _} -> io:format("Start mqttsn:~s listener on ~s successfully.~n", - [Proto, format(ListenOn)]); - {error, Reason} -> - io:format(standard_error, "Failed to start mqttsn:~s listener on ~s: ~0p~n", - [Proto, format(ListenOn), Reason]), - error(Reason) - end. - -%% Start MQTTSN listener --spec start_listener(esockd:proto(), esockd:listen_on(), [esockd:option()]) - -> {ok, pid()} | {error, term()}. -start_listener(udp, ListenOn, Options) -> - start_udp_listener('mqttsn:udp', ListenOn, Options); -start_listener(dtls, ListenOn, Options) -> - start_udp_listener('mqttsn:dtls', ListenOn, Options). - -%% @private -start_udp_listener(Name, ListenOn, Options) -> - SockOpts = esockd:parse_opt(Options), - esockd:open_udp(Name, ListenOn, merge_default(SockOpts), - {emqx_sn_gateway, start_link, [Options -- SockOpts]}). - --spec stop_listeners() -> ok. -stop_listeners() -> - lists:foreach(fun stop_listener/1, listeners_confs()). - --spec stop_listener(listener()) -> ok | {error, term()}. -stop_listener({Proto, ListenOn, Opts}) -> - StopRet = stop_listener(Proto, ListenOn, Opts), - case StopRet of - ok -> io:format("Stop mqttsn:~s listener on ~s successfully.~n", - [Proto, format(ListenOn)]); - {error, Reason} -> - io:format(standard_error, "Failed to stop mqttsn:~s listener on ~s: ~0p~n", - [Proto, format(ListenOn), Reason]) - end, - StopRet. - --spec stop_listener(esockd:proto(), esockd:listen_on(), [esockd:option()]) - -> ok | {error, term()}. -stop_listener(udp, ListenOn, _Opts) -> - esockd:close('mqttsn:udp', ListenOn); -stop_listener(dtls, ListenOn, _Opts) -> - esockd:close('mqttsn:dtls', ListenOn). - -%%-------------------------------------------------------------------- -%% Internal funcs -%%-------------------------------------------------------------------- - -%% @private -%% In order to compatible with the old version of the configuration format -listeners_confs() -> - ListenOn = application:get_env(emqx_sn, port, 1884), - GwId = application:get_env(emqx_sn, gateway_id, 1), - Username = application:get_env(emqx_sn, username, undefined), - Password = application:get_env(emqx_sn, password, undefined), - EnableQos3 = application:get_env(emqx_sn, enable_qos3, false), - EnableStats = application:get_env(emqx_sn, enable_stats, false), - IdleTimeout = application:get_env(emqx_sn, idle_timeout, 30000), - [{udp, ListenOn, [{gateway_id, GwId}, - {username, Username}, - {password, Password}, - {enable_qos3, EnableQos3}, - {enable_stats, EnableStats}, - {idle_timeout, IdleTimeout}, - {max_connections, 1024000}, - {max_conn_rate, 1000}, - {udp_options, []}]}]. - -merge_default(Options) -> - case lists:keytake(udp_options, 1, Options) of - {value, {udp_options, TcpOpts}, Options1} -> - [{udp_options, emqx_misc:merge_opts(?UDP_SOCKOPTS, TcpOpts)} | Options1]; - false -> - [{udp_options, ?UDP_SOCKOPTS} | Options] - end. - -format(Port) when is_integer(Port) -> - io_lib:format("0.0.0.0:~w", [Port]); -format({Addr, Port}) when is_list(Addr) -> - io_lib:format("~s:~w", [Addr, Port]); -format({Addr, Port}) when is_tuple(Addr) -> - io_lib:format("~s:~w", [inet:ntoa(Addr), Port]). diff --git a/apps/emqx_gateway/src/mqttsn/emqx_sn_sup.erl b/apps/emqx_gateway/src/mqttsn/emqx_sn_sup.erl deleted file mode 100644 index e78b41766..000000000 --- a/apps/emqx_gateway/src/mqttsn/emqx_sn_sup.erl +++ /dev/null @@ -1,41 +0,0 @@ -%%-------------------------------------------------------------------- -%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. 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. -%%-------------------------------------------------------------------- - --module(emqx_sn_sup). - --behaviour(supervisor). - --export([ start_link/3 - , init/1 - ]). - -start_link(Addr, GwId, PredefTopics) -> - supervisor:start_link({local, ?MODULE}, ?MODULE, [Addr, GwId, PredefTopics]). - -init([{_Ip, Port}, GwId, PredefTopics]) -> - Broadcast = #{id => emqx_sn_broadcast, - start => {emqx_sn_broadcast, start_link, [GwId, Port]}, - restart => permanent, - shutdown => brutal_kill, - type => worker, - modules => [emqx_sn_broadcast]}, - Registry = #{id => emqx_sn_registry, - start => {emqx_sn_registry, start_link, [PredefTopics]}, - restart => permanent, - shutdown => brutal_kill, - type => worker, - modules => [emqx_sn_registry]}, - {ok, {{one_for_one, 10, 3600}, [Broadcast, Registry]}}.