Skip to content

Commit fc4f392

Browse files
committed
Network.erl fix sta_rssi() crash when not started.
Calling network:sta_rssi() would crash if network was not started. And just if network_port was started but not connected yet. Handle this and return error tuple, and added tests for both scenarios. Signed-off-by: Peter M <petermm@gmail.com>
1 parent b80f131 commit fc4f392

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
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+
Port ->
302+
Ref = make_ref(),
303+
Port ! {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/components/avm_builtins/network_driver.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static const char *const sta_connected_atom = ATOM_STR("\xD", "sta_connected");
7979
static const char *const sta_beacon_timeout_atom = ATOM_STR("\x12", "sta_beacon_timeout");
8080
static const char *const sta_disconnected_atom = ATOM_STR("\x10", "sta_disconnected");
8181
static const char *const sta_got_ip_atom = ATOM_STR("\xA", "sta_got_ip");
82+
static const char *const network_down_atom = ATOM_STR("\x0C", "network_down");
8283

8384
ESP_EVENT_DECLARE_BASE(sntp_event_base);
8485
ESP_EVENT_DEFINE_BASE(sntp_event_base);
@@ -847,13 +848,24 @@ static void get_sta_rssi(Context *ctx, term pid, term ref)
847848
size_t tuple_reply_size = PORT_REPLY_SIZE + TUPLE_SIZE(2);
848849

849850
int sta_rssi = 0;
850-
esp_err_t err = esp_wifi_sta_get_rssi(&sta_rssi);
851-
if (UNLIKELY(err != ESP_OK)) {
852-
term error_term = term_from_int(err);
853-
ESP_LOGE(TAG, "error obtaining RSSI: [%i] %u", err, error_term);
854-
// Reply: {Ref, {error, Reason}}
851+
wifi_ap_record_t ap_info;
852+
esp_err_t status = esp_wifi_sta_get_ap_info(&ap_info);
853+
if (status == ESP_OK) {
854+
esp_err_t err = esp_wifi_sta_get_rssi(&sta_rssi);
855+
if (UNLIKELY(err != ESP_OK)) {
856+
term error_term = term_from_int(err);
857+
ESP_LOGE(TAG, "error obtaining RSSI: [%i] %u", err, error_term);
858+
// Reply: {Ref, {error, Reason}}
859+
port_ensure_available(ctx, tuple_reply_size);
860+
term error = port_create_error_tuple(ctx, error_term);
861+
port_send_reply(ctx, pid, ref, error);
862+
return;
863+
}
864+
} else {
865+
ESP_LOGE(TAG, "Device is not connected to any AP.");
866+
// Reply: {Ref, {error, network_down}}
855867
port_ensure_available(ctx, tuple_reply_size);
856-
term error = port_create_error_tuple(ctx, error_term);
868+
term error = port_create_error_tuple(ctx, make_atom(ctx->global, network_down_atom));
857869
port_send_reply(ctx, pid, ref, error);
858870
return;
859871
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
start() ->
2626
case verify_platform(atomvm:platform()) of
2727
ok ->
28+
% test that sta_rssi() can be safely called, when network is not started
29+
{error, network_down} = network:sta_rssi(),
2830
ok = start_network(),
2931
ok = network:stop(),
3032
start_network(),
@@ -59,7 +61,11 @@ start_network() ->
5961
],
6062
case network:start(Config) of
6163
{ok, _Pid} ->
62-
io:format("Network started.~n");
64+
% test that sta_rssi() can be safely called
65+
% when network is just started, but may not yet be connected.
66+
network:sta_rssi(),
67+
io:format("Network started.~n"),
68+
ok;
6369
Error ->
6470
Error
6571
end.

0 commit comments

Comments
 (0)