From 5eccbfb2ada0a3b96cb52b7a429ff3d86bc11e72 Mon Sep 17 00:00:00 2001
From: Johann Fischer <j.fischer@phytec.de>
Date: Thu, 26 Nov 2015 17:24:40 +0100
Subject: [PATCH] tests/driver_kw2xrf: adapt for netdev2 and add testmodes

---
 tests/driver_kw2xrf/Makefile |   1 +
 tests/driver_kw2xrf/main.c   | 135 ++++++++++++++++++++++++++++++++++-
 2 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/tests/driver_kw2xrf/Makefile b/tests/driver_kw2xrf/Makefile
index 936b69c279..f570ab8895 100644
--- a/tests/driver_kw2xrf/Makefile
+++ b/tests/driver_kw2xrf/Makefile
@@ -9,6 +9,7 @@ USEMODULE += auto_init_gnrc_netif
 USEMODULE += gnrc_netif
 USEMODULE += gnrc_txtsnd
 USEMODULE += gnrc_nomac
+USEMODULE += gnrc_netdev_default
 USEMODULE += gnrc_pktdump
 USEMODULE += shell
 USEMODULE += shell_commands
diff --git a/tests/driver_kw2xrf/main.c b/tests/driver_kw2xrf/main.c
index c0d6390fd3..668139b9ae 100644
--- a/tests/driver_kw2xrf/main.c
+++ b/tests/driver_kw2xrf/main.c
@@ -20,9 +20,142 @@
 #include <stdio.h>
 
 #include "shell.h"
+#include "kw2xrf.h"
 #include "shell_commands.h"
 #include "net/gnrc.h"
 #include "net/gnrc/pktdump.h"
+#include "net/gnrc/netif.h"
+#include "net/gnrc/netapi.h"
+#include "net/netopt.h"
+
+#ifdef KW2XRF_TESTMODE
+#include "kw2xrf_tm.h"
+
+/* utility functions */
+static bool _is_number(char *str)
+{
+    for (; *str; str++) {
+        if (*str < '0' || *str > '9') {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+static bool _is_iface(kernel_pid_t dev)
+{
+    kernel_pid_t ifs[GNRC_NETIF_NUMOF];
+    size_t numof = gnrc_netif_get(ifs);
+
+    for (size_t i = 0; i < numof && i < GNRC_NETIF_NUMOF; i++) {
+        if (ifs[i] == dev) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+static void _set_test_mode(int argc, char **argv, uint8_t mode)
+{
+    (void) argc;
+    if (_is_number(argv[1])) {
+        kernel_pid_t dev = (kernel_pid_t)atoi(argv[1]);
+
+        if (_is_iface(dev)) {
+            gnrc_netapi_set(dev, NETOPT_RF_TESTMODE, 0, (void *)&mode, sizeof(mode));
+            return;
+        }
+    }
+    printf("usage: %s <if_id>\n", argv[0]);
+    return;
+}
+
+static int _tm_idle(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, NETOPT_RF_TESTMODE_IDLE);
+    return 0;
+}
+
+static int _tm_crx(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, NETOPT_RF_TESTMODE_CRX);
+    return 0;
+}
+
+static int _tm_ctx_cw(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, NETOPT_RF_TESTMODE_CTX_CW);
+    return 0;
+}
+
+static int _tm_ctx_prbs9(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, NETOPT_RF_TESTMODE_CTX_PRBS9);
+    return 0;
+}
+
+static int _tm_ctx_preamble(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, KW2XRF_TM_CTX_PREAMBLE);
+    return 0;
+}
+
+static int _tm_ctx_2mhz(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, KW2XRF_TM_CTX_2MHZ);
+    return 0;
+}
+
+static int _tm_ctx_200khz(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, KW2XRF_TM_CTX_200KHZ);
+    return 0;
+}
+
+static int _tm_ctx_1mbps_prbs9(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, KW2XRF_TM_CTX_1MBPS_PRBS9);
+    return 0;
+}
+
+static int _tm_ctx_ext(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, KW2XRF_TM_CTX_EXT);
+    return 0;
+}
+
+static int _tm_ctx_nm0(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, KW2XRF_TM_CTX_NM0);
+    return 0;
+}
+
+static int _tm_ctx_nm1(int argc, char **argv)
+{
+    _set_test_mode(argc, argv, KW2XRF_TM_CTX_NM1);
+    return 0;
+}
+
+#endif
+
+static const shell_command_t shell_commands[] = {
+#ifdef KW2XRF_TESTMODE
+    { "idle", "xcvr idle mode", _tm_idle },
+    { "ctx_prbs9", "continues transmit the prbs9 pattern", _tm_ctx_prbs9 },
+    { "crx", "continues receive mode, useful for current measuring", _tm_crx },
+    { "ctx_preamble", "continues transmit the 10101010 pattern", _tm_ctx_preamble },
+    { "ctx_cw", "continues transmit carrier wave", _tm_ctx_cw },
+    { "ctx_2mhz", "continues transmit modulated carrier", _tm_ctx_2mhz },
+    { "ctx_200khz", "continues transmit modulated carrier", _tm_ctx_200khz },
+    { "ctx_1mbps_prbs9", "continues transmit the prbs9 pattern", _tm_ctx_1mbps_prbs9 },
+    { "ctx_ext", "continues transmit modulated carrier", _tm_ctx_ext },
+    { "ctx_nm0", "continues transmit modulated carrier", _tm_ctx_nm0 },
+    { "ctx_nm1", "continues transmit modulated carrier", _tm_ctx_nm1 },
+#endif
+    { NULL, NULL, NULL }
+};
 
 int main(void)
 {
@@ -39,7 +172,7 @@ int main(void)
     puts("Initialization successful - starting the shell now");
 
     char line_buf[SHELL_DEFAULT_BUFSIZE];
-    shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
+    shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
 
     return 0;
 }
-- 
GitLab