Skip to content

Commit

Permalink
Add support for network:sta_rssi/0 to pico-w network driver
Browse files Browse the repository at this point in the history
Adds the ability to get the signal strength, in decibels, of the connection
to the access point the pico-w is connected to.

Signed-off-by: Winford <[email protected]>
  • Loading branch information
UncleGrumpy committed May 16, 2024
1 parent 487263a commit 2e41ba2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add a number of functions to proplists module, such as `delete/2`, `from/to_map/1`, etc...
- Add `esp:deep_sleep_enable_gpio_wakeup/2` to allow wakeup from deep sleep for ESP32C3 and ESP32C6.
- Obtain RSSI of the current connection with `network:sta_rssi/0` on ESP32.
- Pico-W support for `network:sta_rssi/0`.

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions src/platforms/rp2040/src/lib/gpiodriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <string.h>

#include <hardware/gpio.h>
#ifdef LIB_PICO_CYW43_ARCH
#include <pico/cyw43_arch.h>
#endif

#include "defaultatoms.h"
#include "interop.h"
Expand Down
50 changes: 42 additions & 8 deletions src/platforms/rp2040/src/lib/networkdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ enum network_cmd
{
NetworkInvalidCmd = 0,
// TODO add support for scan, ifconfig
NetworkStartCmd
NetworkStartCmd,
NetworkRssiCmd
};

static const AtomStringIntPair cmd_table[] = {
{ ATOM_STR("\x5", "start"), NetworkStartCmd },
{ ATOM_STR("\x4", "rssi"), NetworkRssiCmd },
SELECT_INT_DEFAULT(NetworkInvalidCmd)
};

Expand Down Expand Up @@ -602,6 +604,27 @@ static void start_network(Context *ctx, term pid, term ref, term config)
port_send_reply(ctx, pid, ref, OK_ATOM);
}

static void get_sta_rssi(Context *ctx, term pid, term ref)
{
size_t tuple_reply_size = PORT_REPLY_SIZE + TUPLE_SIZE(2);

int32_t sta_rssi = 0;
int err = cyw43_wifi_get_rssi(&cyw43_state, &sta_rssi);
if (UNLIKELY(err != 0)) {
// Reply: {Ref, {error, Reason}}
port_ensure_available(ctx, tuple_reply_size);
term error = port_create_error_tuple(ctx, term_from_int(err));
port_send_reply(ctx, pid, ref, error);
return;
}

term rssi = term_from_int32(sta_rssi);
// Reply: {Ref, {rssi, Value}}
port_ensure_available(ctx, tuple_reply_size);
term reply = port_create_tuple2(ctx, globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "rssi")), rssi);
port_send_reply(ctx, pid, ref, reply);
}

static NativeHandlerResult consume_mailbox(Context *ctx)
{
Message *message = mailbox_first(&ctx->mailbox);
Expand All @@ -614,17 +637,28 @@ static NativeHandlerResult consume_mailbox(Context *ctx)
term pid = term_get_tuple_element(msg, 0);
term ref = term_get_tuple_element(msg, 1);
term cmd = term_get_tuple_element(msg, 2);
term cmd_term = term_invalid_term();
term config = term_invalid_term();

if (term_is_tuple(cmd) && term_get_tuple_arity(cmd) == 2) {

term cmd_term = term_get_tuple_element(cmd, 0);
term config = term_get_tuple_element(cmd, 1);
if ((term_is_tuple(cmd) && term_get_tuple_arity(cmd) == 2) || term_is_atom(cmd)) {
if (term_is_atom(cmd)) {
cmd_term = cmd;
UNUSED(config);
} else {
cmd_term = term_get_tuple_element(cmd, 0);
config = term_get_tuple_element(cmd, 1);
}

enum network_cmd cmd = interop_atom_term_select_int(cmd_table, cmd_term, ctx->global);
switch (cmd) {
case NetworkStartCmd:
enum network_cmd command = interop_atom_term_select_int(cmd_table, cmd_term, ctx->global);
switch (command) {
case NetworkStartCmd: {
start_network(ctx, pid, ref, config);
break;
}
case NetworkRssiCmd: {
get_sta_rssi(ctx, pid, ref);
break;
}

default: {
// {Ref, {error, badarg}}
Expand Down

0 comments on commit 2e41ba2

Please sign in to comment.