Skip to content

Commit c813647

Browse files
committed
Network.erl fix sta_rssi() crash when not started.
Calling network:sta_rssi() would crash if network was not started. Handle this and return error tuple, and added test. Signed-off-by: Peter M <petermm@gmail.com>
1 parent abac2d6 commit c813647

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

libs/eavmlib/src/network.erl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,17 @@ stop() ->
295295
%%-----------------------------------------------------------------------------
296296
-spec sta_rssi() -> {ok, Rssi :: db()} | {error, Reason :: term()}.
297297
sta_rssi() ->
298-
Port = get_port(),
299-
Ref = make_ref(),
300-
Port ! {self(), Ref, rssi},
301-
receive
302-
{Ref, {error, Reason}} -> {error, Reason};
303-
{Ref, {rssi, Rssi}} -> {ok, Rssi};
304-
Other -> {error, Other}
298+
case whereis(network_port) of
299+
undefined ->
300+
{error, network_down};
301+
Pid ->
302+
Ref = make_ref(),
303+
Pid ! {self(), Ref, rssi},
304+
receive
305+
{Ref, {error, Reason}} -> {error, Reason};
306+
{Ref, {rssi, Rssi}} -> {ok, Rssi};
307+
Other -> {error, Other}
308+
end
305309
end.
306310

307311
%%

src/platforms/esp32/test/main/test_erl_sources/test_wifi_example.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
start() ->
2626
case verify_platform(atomvm:platform()) of
2727
ok ->
28+
network:sta_rssi(),
2829
ok = start_network(),
2930
ok = network:stop(),
3031
start_network(),
@@ -59,7 +60,8 @@ start_network() ->
5960
],
6061
case network:start(Config) of
6162
{ok, _Pid} ->
62-
io:format("Network started.~n");
63+
io:format("Network started.~n"),
64+
ok;
6365
Error ->
6466
Error
6567
end.

0 commit comments

Comments
 (0)