From 7cf5530cae26b88b06cfdc14c4091cbf2fa1acd6 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Tue, 23 Feb 2021 14:54:06 +0100 Subject: [PATCH] examples/gnrc_lorawan: refactor --- examples/gnrc_lorawan/Makefile | 49 +++++++++++++++------- examples/gnrc_lorawan/README.md | 43 +++++++++++++++---- examples/gnrc_lorawan/app.config | 2 + examples/gnrc_lorawan/main.c | 72 ++++---------------------------- 4 files changed, 80 insertions(+), 86 deletions(-) create mode 100644 examples/gnrc_lorawan/app.config diff --git a/examples/gnrc_lorawan/Makefile b/examples/gnrc_lorawan/Makefile index 16c25c0bdd..b3dd1762ba 100644 --- a/examples/gnrc_lorawan/Makefile +++ b/examples/gnrc_lorawan/Makefile @@ -1,24 +1,37 @@ # name of your application APPLICATION = gnrc_lorawan -USEMODULE += shell -USEMODULE += shell_commands -USEMODULE += gnrc_netdev_default -USEMODULE += auto_init_gnrc_netif -USEMODULE += gnrc_lorawan -USEMODULE += gnrc_pktdump -USEMODULE += gnrc_neterr - -BOARD ?= b-l072z-lrwan1 -RIOTBASE ?= ../../ - # Turn on developer helpers DEVELHELP ?= 1 -# use SX1276 by default -DRIVER ?= sx1276 +BOARD ?= b-l072z-lrwan1 +RIOTBASE ?= ../../ -USEMODULE += $(DRIVER) +# Include board's default network devices and auto-initialization of GNRC +# interfaces +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif + +# Add support for GNRC LoRaWAN +USEMODULE += gnrc_lorawan + +# Use GNRC pktdump to print downlink messages +USEMODULE += gnrc_pktdump + +# Include the shell and shell commands. +USEMODULE += shell +USEMODULE += shell_commands + +# Use GNRC Txtsnd to transmit LoRaWAN from the shell +USEMODULE += gnrc_txtsnd + +# Uncomment as needed if a board doesn't include a LoRa radio by default +# USEMODULE += sx1272 +# USEMODULE += sx1276 + +# As there is an 'app.config' we want to explicitly disable Kconfig by setting +# the variable to empty +SHOULD_RUN_KCONFIG ?= include $(RIOTBASE)/Makefile.include @@ -33,6 +46,14 @@ include $(RIOTBASE)/Makefile.include # Check if being configured via Kconfig ifndef CONFIG_KCONFIG_USEMODULE_LORAWAN + # Tell GNRC to encode LoRaWAN port in the GNRC netif header. + # This allows us to use `gnrc_txtsnd` to send data from the shell using the + # `txtsnd` command. + # + # Note: From Release 22.01 all GNRC LoRaWAN packets will include the netif + # header. Therefore this flag will be removed after that + CFLAGS += -DCONFIG_GNRC_NETIF_LORAWAN_NETIF_HDR + CFLAGS += -DCONFIG_LORAMAC_APP_KEY_DEFAULT=\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\" CFLAGS += -DCONFIG_LORAMAC_APP_EUI_DEFAULT=\"BBBBBBBBBBBBBBBB\" CFLAGS += -DCONFIG_LORAMAC_DEV_EUI_DEFAULT=\"CCCCCCCCCCCCCCCC\" diff --git a/examples/gnrc_lorawan/README.md b/examples/gnrc_lorawan/README.md index bdc859e2ed..17414cc6cc 100644 --- a/examples/gnrc_lorawan/README.md +++ b/examples/gnrc_lorawan/README.md @@ -15,7 +15,6 @@ Usage It's necessary to join the LoRaWAN network either via OTAA or ABP. All keys, addresses and EUIs are in network endian (big endian). -The application listens to downlinks on Port 2 by default. Region need to be set in the Makefile. ## OTAA @@ -73,10 +72,15 @@ CFLAGS += -DCONFIG_LORAMAC_DEV_ADDR_DEFAULT=\"FFFFFFFF\" ## Send data -After join, send data using `send` command. E.g to send "Hello RIOT!" to port 2: +After join, send data using `txtsnd` command: +``` +txtsnd +``` + +E.g to send "Hello RIOT!" to LoRaWAN port 123 (hex 0x7B) via interface 3: ``` -send 3 "Hello RIOT!" 2 +txtsnd 3 7B "Hello RIOT!" ``` ## Changing datarate of transmission @@ -100,7 +104,7 @@ Send some data. The result of the Link Check request can be seen with ``` ifconfig 3 link_check -send 3 "Join the RIOT!" +txtsnd 3 01 "Join the RIOT!" ``` Check demodulation margin and number of gateways using `ifconfig` @@ -124,18 +128,43 @@ E.g send confirmable messages: ``` ifconfig 3 ack_req -send 3 "My confirmable message" +txtsnd 3 01 "My confirmable message" ``` And unconfirmable messages: ``` ifconfig 3 -ack_req -send 3 "My unconfirmable message" +txtsnd 3 01 "My unconfirmable message" ``` +## Receiving data + +Schedule a downlink for the LoRaWAN node in the Application Server. If using +TTN, this can be done under `Applications > > Devices > > Overview` +and then check the `Downlink` section. + +After sending data, the LoRaWAN Network Server will reply with the downlink +data. For simplicity, this application is configured to dump downlink data to +[GNRC pktdump](https://doc.riot-os.org/pktdump_8h.html). + +E.g: +``` +PKTDUMP: data received: +~~ SNIP 0 - size: 2 byte, type: NETTYPE_UNDEF (0) +00000000 AA AA +~~ SNIP 1 - size: 9 byte, type: NETTYPE_NETIF (-1) +if_pid: 3 rssi: -32768 lqi: 0 +flags: 0x0 +src_l2addr: (nil) +dst_l2addr: 01 +~~ PKT - 2 snips, total size: 11 byte +``` + +This downlink was sent to port 1 (check `dst_l2addr` field) + Current state and future plans -============ +============================== The current GNRC LoRaWAN stack is still in an experimental state. It's still not compliant with the LoRaWAN specification because some features like duty diff --git a/examples/gnrc_lorawan/app.config b/examples/gnrc_lorawan/app.config new file mode 100644 index 0000000000..293cd467ef --- /dev/null +++ b/examples/gnrc_lorawan/app.config @@ -0,0 +1,2 @@ +CONFIG_KCONFIG_USEMODULE_GNRC_NETIF=y +CONFIG_GNRC_NETIF_LORAWAN_NETIF_HDR=y diff --git a/examples/gnrc_lorawan/main.c b/examples/gnrc_lorawan/main.c index bd14363813..bd1995c700 100644 --- a/examples/gnrc_lorawan/main.c +++ b/examples/gnrc_lorawan/main.c @@ -32,81 +32,23 @@ #include "net/gnrc/netif.h" #include "net/gnrc/pktbuf.h" -#include "net/gnrc/pktdump.h" #include "net/gnrc/netreg.h" +#include "net/gnrc/pktdump.h" #include "net/loramac.h" -#define LORAWAN_QUEUE_SIZE (4U) - -static void _usage(void) -{ - puts("usage: send [port]"); -} - -int tx_cmd(int argc, char **argv) -{ - gnrc_pktsnip_t *pkt; - uint8_t port = CONFIG_LORAMAC_DEFAULT_TX_PORT; /* Default: 2 */ - int interface; - - if (argc < 3) { - _usage(); - return 1; - } - - interface = atoi(argv[1]); - /* handle optional parameters */ - if (argc > 3) { - port = atoi(argv[3]); - if (port == 0 || port >= 224) { - printf("error: invalid port given '%d', " - "port can only be between 1 and 223\n", port); - return 1; - } - } - - pkt = gnrc_pktbuf_add(NULL, argv[2], strlen(argv[2]), GNRC_NETTYPE_UNDEF); - - /* register for returned packet status */ - if (gnrc_neterr_reg(pkt) != 0) { - puts("Can not register for error reporting"); - return 0; - } - - gnrc_netapi_set(interface, NETOPT_LORAWAN_TX_PORT, 0, &port, sizeof(port)); - gnrc_netif_send(gnrc_netif_get_by_pid(interface), pkt); - - /* wait for packet status and check */ - msg_t msg; - msg_receive(&msg); - if ((msg.type != GNRC_NETERR_MSG_TYPE) || - (msg.content.value != GNRC_NETERR_SUCCESS)) { - printf("Error sending packet: (status: %d\n)", (int) msg.content.value); - } - else { - puts("Successfully sent packet"); - } - - return 0; -} - -static const shell_command_t shell_commands[] = { - { "send", "Send LoRaWAN data", tx_cmd }, - { NULL, NULL, NULL } -}; - - int main(void) { /* start the shell */ puts("Initialization successful - starting the shell now"); - gnrc_netreg_entry_t dump = GNRC_NETREG_ENTRY_INIT_PID(CONFIG_LORAMAC_DEFAULT_TX_PORT, + + /* Receive LoRaWAN packets in GNRC pktdump */ + gnrc_netreg_entry_t dump = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, gnrc_pktdump_pid); - gnrc_netreg_register(GNRC_NETTYPE_LORAWAN, &dump); - char line_buf[SHELL_DEFAULT_BUFSIZE]; + gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &dump); - shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); + char line_buf[SHELL_DEFAULT_BUFSIZE]; + shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE); return 0; }