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 Apr 23, 2024
1 parent f3576c4 commit 4f527fa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Support for DragonFly BSD (generic_unix platform).
- Obtain RSSI of the current connection with `network:sta_rssi/0` on ESP32.
- Pico-W support for `network:sta_rssi/0`.

### Fixed

Expand Down
56 changes: 48 additions & 8 deletions src/platforms/rp2040/src/lib/networkdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#pragma GCC diagnostic ignored "-Wpedantic"

#include <cyw43.h>
#include <cyw43_ll.h>
#include <dhserver.h>
#include <hardware/rtc.h>
#include <lwip/apps/sntp.h>
Expand Down Expand Up @@ -58,11 +59,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 +605,32 @@ 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)
{
int32_t sta_rssi;
int err = cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_RSSI, sizeof sta_rssi, (uint8_t *) &sta_rssi, CYW43_ITF_STA);

if (UNLIKELY(err != 0)) {
size_t heap_size = TUPLE_SIZE(2);
if (UNLIKELY(memory_ensure_free(ctx, heap_size) != MEMORY_GC_OK)) {
port_send_reply(ctx, pid, ref, OUT_OF_MEMORY_ATOM);
return;
}
term error = port_create_error_tuple(ctx, term_from_int(err));
port_send_reply(ctx, pid, ref, error);
return;
}
// {Ref, {rssi, -25}}
BEGIN_WITH_STACK_HEAP(PORT_REPLY_SIZE + TUPLE_SIZE(2) + sizeof(int), heap);
{
term rssi = term_from_int32(sta_rssi);

term reply = port_heap_create_tuple2(&heap, globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "rssi")), rssi);
port_send_reply(ctx, pid, ref, reply);
}
END_WITH_STACK_HEAP(heap, ctx->global);
}

static NativeHandlerResult consume_mailbox(Context *ctx)
{
Message *message = mailbox_first(&ctx->mailbox);
Expand All @@ -614,17 +643,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 4f527fa

Please sign in to comment.