Skip to content

Commit

Permalink
pkg/uwb-core: move twr helpers to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
fjmolinas committed Jun 9, 2022
1 parent 20d3304 commit d01ffbc
Show file tree
Hide file tree
Showing 17 changed files with 589 additions and 373 deletions.
68 changes: 68 additions & 0 deletions dist/pythonlibs/riotctrl_shell/tests/test_twr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (C) 2021 Inria
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

from statistics import mean
import riotctrl_shell.twr

from .common import init_ctrl

TEST_TWR_DATA = '{"t": 16236, "src": "98:9C", "dst": "02:A1", "d_cm": 477}'


def test_TwrData_from_json_str():
data = riotctrl_shell.twr.TwrData.from_json_str(TEST_TWR_DATA)
assert data.t == 16236
assert data.src == "98:9C"
assert data.dst == "02:A1"
assert data.d_cm == 477


def test_twr_listen_on():
rc = init_ctrl(output=riotctrl_shell.twr.TWR_LISTEN_ON_CMD_OUTPUT)
si = riotctrl_shell.twr.TwrCmd(rc)
cmd_output = si.twr_listen(on=True)
assert riotctrl_shell.twr.TWR_LISTEN_ON_CMD_OUTPUT in cmd_output


def test_twr_listen_off():
rc = init_ctrl(output=riotctrl_shell.twr.TWR_LISTEN_OFF_CMD_OUTPUT)
si = riotctrl_shell.twr.TwrCmd(rc)
cmd_output = si.twr_listen(on=False)
assert riotctrl_shell.twr.TWR_LISTEN_OFF_CMD_OUTPUT in cmd_output


def test_twr_request_and_parser():
_output = riotctrl_shell.twr.TWR_REQUEST_CMD_OUTPUT
count = 10
for _ in range(0, count):
_output += f"\n\r{TEST_TWR_DATA}"
rc = init_ctrl(output=_output)
si = riotctrl_shell.twr.TwrCmd(rc)
cmd_output = si.twr_request(count=count)
assert riotctrl_shell.twr.TWR_REQUEST_CMD_OUTPUT in cmd_output
parser = riotctrl_shell.twr.TwrRequestParser()
d_cm = parser.parse(cmd_output)
assert len(d_cm) == 10
assert mean(d_cm) == 477


def test_twr_ifconfig_and_parser():
output = """
Iface 3 HWaddr: 57:81 Channel: 5 NID: DE:CA
Long HWaddr: 08:22:61:44:4D:83:57:81
TX-Power: 8.0dBm TC-PGdelay: 0xb5
"""
rc = init_ctrl(output=output)
si = riotctrl_shell.twr.TwrCmd(rc)
cmd_output = si.ifconfig()
parser = riotctrl_shell.twr.TwrIfconfigParser()
netif = parser.parse(cmd_output)
assert netif["netif"] == "3"
assert netif["hwaddr"] == "57:81"
assert netif["hwaddr64"] == "08:22:61:44:4D:83:57:81"
assert netif["panid"] == "DE:CA"
assert netif["channel"] == "5"
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,42 @@
twr-aloha shell interactions
"""

from dataclasses import dataclass
import json
import re
from typing import Union

from riotctrl.shell import ShellInteraction, ShellInteractionParser

TWR_LISTEN_ON_CMD_OUTPUT = "[twr]: start listening"
TWR_LISTEN_OFF_CMD_OUTPUT = "[twr]: stop listening"
TWR_REQUEST_CMD_OUTPUT = "[twr]: start ranging"


@dataclass
class TwrData:
t: int
src: str
dst: str
d_cm: Union[int, float]

@staticmethod
def from_json_str(json_string: str):
json_dict = json.loads(json_string)
return TwrData(**json_dict)


class TwrRequestParser(ShellInteractionParser):
def parse(self, cmd_output):
d_cm = []
for line in cmd_output.splitlines():
try:
twr_data = TwrData.from_json_str(line)
except json.decoder.JSONDecodeError:
continue
d_cm.append(twr_data.d_cm)
return d_cm


class TwrIfconfigParser(ShellInteractionParser):
iface_c = re.compile(r"Iface\s+(?P<name>\d+)\s")
Expand Down Expand Up @@ -60,9 +92,11 @@ def twr_listen(self, on=True, timeout=-1, async_=False):
args=("lst", "on" if on else "off"), timeout=timeout, async_=async_
)

def twr_req(self, addr="ff:ff", count=1, interval=1000, proto="ss", timeout=-1, async_=False):
def twr_request(
self, addr="ff:ff", count=1, itvl=1000, proto="ss", timeout=-1, async_=False
):
return self.twr_cmd(
args=("req", f"{addr}", f"-c {count}", f"-p {proto}", f"-i {interval}"),
args=("req", f"{addr}", f"-c {count}", f"-p {proto}", f"-i {itvl}"),
timeout=timeout,
async_=async_,
)
Expand Down
9 changes: 3 additions & 6 deletions examples/twr_aloha/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ DEVELHELP ?= 1
# Include uwb-core, uwb-dw1000
USEPKG += uwb-core
USEPKG += uwb-dw1000
# Use event threads instead of a custom event queue
## Use event threads instead of a custom event queue
USEMODULE += uwb-core_event_thread
CFLAGS += -DEVENT_THREAD_MEDIUM_STACKSIZE=THREAD_STACKSIZE_DEFAULT

# Include TWR helper modules
USEMODULE += uwb-core_twr
# Include all ranging algorithms
USEMODULE += uwb-core_twr_ss
USEMODULE += uwb-core_twr_ss_ack
Expand All @@ -34,11 +36,6 @@ USEMODULE += uwb-core_twr_ds_ext
USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps
USEMODULE += l2util
USEMODULE += event_callback
USEMODULE += event_timeout_ztimer
USEMODULE += event_periodic
USEMODULE += test_utils_result_output

# All uwb-core applications need to enable `-fms-extensions`
CFLAGS += -fms-extensions
Expand Down
5 changes: 1 addition & 4 deletions examples/twr_aloha/app.config.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ CONFIG_MODULE_UWB-CORE_TWR_SS_ACK=y
CONFIG_MODULE_UWB-CORE_TWR_SS_EXT=y
CONFIG_MODULE_UWB-CORE_TWR_DS=y
CONFIG_MODULE_UWB-CORE_TWR_DS_EXT=y
CONFIG_MODULE_UWB-CORE_TWR=y
CONFIG_MODULE_SHELL=y
CONFIG_MODULE_SHELL_COMMANDS=y
CONFIG_MODULE_PS=y
CONFIG_MODULE_L2UTIL=y
CONFIG_MODULE_EVENT_PERIODIC=y
CONFIG_MODULE_EVENT_TIMEOUT_ZTIMER=y
CONFIG_MODULE_TEST_UTILS_RESULT_OUTPUT=y
17 changes: 3 additions & 14 deletions examples/twr_aloha/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,12 @@
#include <stdio.h>
#include "shell.h"

#include "control.h"

extern int _twr_handler(int argc, char **argv);
extern int _twr_ifconfig(int argc, char **argv);

static const shell_command_t shell_commands[] = {
{ "twr", "Two-way-ranging (TWR) cli", _twr_handler },
{ "ifconfig", "Network interface information", _twr_ifconfig},
{ NULL, NULL, NULL }
};

int main(void)
{
/* this should start ranging... */
uwb_core_rng_init();
puts("RIOT uwb-core package Two-Way-Ranging example");
/* define buffer to be used by the shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
return 1;
}
13 changes: 9 additions & 4 deletions examples/twr_aloha/tests-with-config/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

from riotctrl.ctrl import RIOTCtrl
from riotctrl_shell.sys import Reboot
from twr_shell import TwrCmd, TwrIfconfigParser
from riotctrl_shell.twr import (
TWR_LISTEN_OFF_CMD_OUTPUT,
TWR_LISTEN_ON_CMD_OUTPUT,
TWR_REQUEST_CMD_OUTPUT,
)
from riotctrl_shell.twr import TwrCmd, TwrIfconfigParser


class TwrShell(Reboot, TwrCmd):
Expand Down Expand Up @@ -75,11 +80,11 @@ def test_ifconfig(self):
assert self.shell.channel() == "5" # default channel is 5

def test_listen(self):
assert "[twr]: start listening" in self.shell.twr_listen(on=True)
assert "[twr]: stop listening" in self.shell.twr_listen(on=False)
assert TWR_LISTEN_ON_CMD_OUTPUT in self.shell.twr_listen(on=True)
assert TWR_LISTEN_OFF_CMD_OUTPUT in self.shell.twr_listen(on=False)

def test_req(self):
assert "[twr]: start ranging" in self.shell.twr_req()
assert TWR_REQUEST_CMD_OUTPUT in self.shell.twr_request()


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit d01ffbc

Please sign in to comment.