Skip to content

Commit

Permalink
Network.erl fix sta_rssi() crash when not started.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
petermm committed Jan 22, 2025
1 parent abac2d6 commit c813647
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 11 additions & 7 deletions libs/eavmlib/src/network.erl
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,17 @@ stop() ->
%%-----------------------------------------------------------------------------
-spec sta_rssi() -> {ok, Rssi :: db()} | {error, Reason :: term()}.
sta_rssi() ->
Port = get_port(),
Ref = make_ref(),
Port ! {self(), Ref, rssi},
receive
{Ref, {error, Reason}} -> {error, Reason};
{Ref, {rssi, Rssi}} -> {ok, Rssi};
Other -> {error, Other}
case whereis(network_port) of
undefined ->
{error, network_down};
Pid ->
Ref = make_ref(),
Pid ! {self(), Ref, rssi},
receive
{Ref, {error, Reason}} -> {error, Reason};
{Ref, {rssi, Rssi}} -> {ok, Rssi};
Other -> {error, Other}
end
end.

%%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
start() ->
case verify_platform(atomvm:platform()) of
ok ->
network:sta_rssi(),
ok = start_network(),
ok = network:stop(),
start_network(),
Expand Down Expand Up @@ -59,7 +60,8 @@ start_network() ->
],
case network:start(Config) of
{ok, _Pid} ->
io:format("Network started.~n");
io:format("Network started.~n"),
ok;
Error ->
Error
end.
Expand Down

0 comments on commit c813647

Please sign in to comment.