Skip to content

Commit 17f4606

Browse files
HJianBotigercl
authored andcommitted
refactor(gen_coap): upgrade gen_coap to v0.3.0
1 parent c23d082 commit 17f4606

File tree

3 files changed

+71
-34
lines changed

3 files changed

+71
-34
lines changed

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{deps,
22
[
3-
{gen_coap, {git, "https://github.com/emqx/gen_coap", {tag, "v0.2.4"}}}
3+
{gen_coap, {git, "https://github.com/emqx/gen_coap", {tag, "v0.3.0"}}}
44
]}.
55

66
{edoc_opts, [{preprocess, true}]}.

src/emqx_coap_server.erl

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,82 @@
2222
, stop/0
2323
]).
2424

25+
%%--------------------------------------------------------------------
26+
%% APIs
27+
%%--------------------------------------------------------------------
28+
2529
start() ->
2630
{ok, _} = application:ensure_all_started(gen_coap),
27-
start_udp(),
28-
start_dtls(),
31+
start_listeners(),
2932
coap_server_registry:add_handler([<<"mqtt">>], emqx_coap_resource, undefined),
3033
coap_server_registry:add_handler([<<"ps">>], emqx_coap_ps_resource, undefined),
3134
emqx_coap_ps_topics:start_link().
3235

3336
stop() ->
34-
stop_udp(),
35-
stop_dtls().
37+
stop_listeners().
38+
39+
%%--------------------------------------------------------------------
40+
%% Internal funcs
41+
%%--------------------------------------------------------------------
3642

37-
start_udp() ->
38-
BindUdps = application:get_env(?APP, bind_udp, [{5683, []}]),
39-
lists:foreach(fun({Port, InetOpt}) ->
40-
Name = process_name(coap_udp_socket, Port),
41-
coap_server:start_udp(Name, Port, InetOpt)
42-
end, BindUdps).
43+
start_listeners() ->
44+
lists:foreach(fun start_listener/1, listeners_confs()).
4345

44-
start_dtls() ->
46+
stop_listeners() ->
47+
lists:foreach(fun stop_listener/1, listeners_confs()).
48+
49+
start_listener({Proto, ListenOn, Opts}) ->
50+
case start_listener(Proto, ListenOn, Opts) of
51+
{ok, _Pid} ->
52+
io:format("Start coap:~s listener on ~s successfully.~n",
53+
[Proto, format(ListenOn)]);
54+
{error, Reason} ->
55+
io:format(standard_error, "Failed to start coap:~s listener on ~s - ~0p~n!",
56+
[Proto, format(ListenOn), Reason]),
57+
error(Reason)
58+
end.
59+
60+
start_listener(udp, ListenOn, Opts) ->
61+
coap_server:start_udp('coap:udp', ListenOn, Opts);
62+
start_listener(dtls, ListenOn, Opts) ->
63+
coap_server:start_dtls('coap:dtls', ListenOn, Opts).
64+
65+
stop_listener({Proto, ListenOn, _Opts}) ->
66+
Ret = stop_listener(Proto, ListenOn),
67+
case Ret of
68+
ok -> io:format("Stop coap:~s listener on ~s successfully.~n",
69+
[Proto, format(ListenOn)]);
70+
{error, Reason} ->
71+
io:format(standard_error, "Failed to stop coap:~s listener on ~s - ~p~n.",
72+
[Proto, format(ListenOn), Reason])
73+
end,
74+
Ret.
75+
76+
stop_listener(udp, ListenOn) ->
77+
coap_server:stop_udp('coap:udp', ListenOn);
78+
stop_listener(dtls, ListenOn) ->
79+
coap_server:stop_dtls('coap:dtls', ListenOn).
80+
81+
%% XXX: It is a temporary func to convert conf format for esockd
82+
listeners_confs() ->
83+
listeners_confs(udp) ++ listeners_confs(dtls).
84+
85+
listeners_confs(udp) ->
86+
Udps = application:get_env(?APP, bind_udp, []),
87+
[{udp, Port, [{udp_options, InetOpts}]} || {Port, InetOpts} <- Udps];
88+
89+
listeners_confs(dtls) ->
4590
case application:get_env(?APP, dtls_opts, []) of
46-
[] -> ok;
91+
[] -> [];
4792
DtlsOpts ->
48-
BindDtls = application:get_env(?APP, bind_dtls, [{5684, []}]),
49-
lists:foreach(fun({DtlsPort, InetOpt}) ->
50-
Name = process_name(coap_dtls_socket, DtlsPort),
51-
coap_server:start_dtls(Name, DtlsPort, InetOpt ++ DtlsOpts)
52-
end, BindDtls)
93+
BindDtls = application:get_env(?APP, bind_dtls, []),
94+
[{dtls, Port, [{dtls_options, InetOpts ++ DtlsOpts}]} || {Port, InetOpts} <- BindDtls]
5395
end.
5496

55-
stop_udp() ->
56-
BindUdps = application:get_env(?APP, bind_udp, [{5683, []}]),
57-
lists:foreach(fun({Port, _}) ->
58-
Name = process_name(coap_udp_socket, Port),
59-
coap_server:stop_udp(Name)
60-
end, BindUdps).
61-
62-
stop_dtls() ->
63-
BindDtls = application:get_env(?APP, bind_dtls, [{5684, []}]),
64-
lists:foreach(fun({Port, _}) ->
65-
Name = process_name(coap_dtls_socket, Port),
66-
coap_server:stop_dtls(Name)
67-
end, BindDtls).
68-
69-
process_name(Mod, Port) ->
70-
list_to_atom(atom_to_list(Mod) ++ "_" ++ integer_to_list(Port)).
97+
format(Port) when is_integer(Port) ->
98+
io_lib:format("0.0.0.0:~w", [Port]);
99+
format({Addr, Port}) when is_list(Addr) ->
100+
io_lib:format("~s:~w", [Addr, Port]);
101+
format({Addr, Port}) when is_tuple(Addr) ->
102+
io_lib:format("~s:~w", [inet:ntoa(Addr), Port]).
103+

test/emqx_coap_SUITE.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ init_per_suite(Config) ->
3232
Config.
3333

3434
set_sepecial_cfg(emqx_coap) ->
35+
Opts = application:get_env(emqx_coap, dtls_opts,[]),
36+
Opts2 = [{keyfile, emqx_ct_helpers:deps_path(emqx, "etc/certs/key.pem")},
37+
{certfile, emqx_ct_helpers:deps_path(emqx, "etc/certs/cert.pem")}],
38+
application:set_env(emqx_coap, dtls_opts, emqx_misc:merge_opts(Opts, Opts2)),
3539
application:set_env(emqx_coap, enable_stats, true);
3640
set_sepecial_cfg(_) ->
3741
ok.

0 commit comments

Comments
 (0)