Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnrc_lorawan: encode LoRaWAN port in netif header #16080

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions examples/gnrc_lorawan/Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
# name of your application
APPLICATION = gnrc_lorawan

USEMODULE += shell
USEMODULE += shell_commands
# Turn on developer helpers
DEVELHELP ?= 1

BOARD ?= b-l072z-lrwan1
RIOTBASE ?= ../../

# 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
USEMODULE += gnrc_neterr

BOARD ?= b-l072z-lrwan1
RIOTBASE ?= ../../
# Include the shell and shell commands.
USEMODULE += shell
USEMODULE += shell_commands

# Turn on developer helpers
DEVELHELP ?= 1
# Use GNRC Txtsnd to transmit LoRaWAN from the shell
USEMODULE += gnrc_txtsnd

# use SX1276 by default
DRIVER ?= sx1276
# Uncomment as needed if a board doesn't include a LoRa radio by default
# USEMODULE += sx1272
# USEMODULE += sx1276

USEMODULE += $(DRIVER)
# 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

Expand All @@ -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\"
Expand Down
43 changes: 36 additions & 7 deletions examples/gnrc_lorawan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <if> <hex_port> <data>
```

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
Expand All @@ -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`
Expand All @@ -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 > <APP> > Devices > <DEV> > 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
Expand Down
2 changes: 2 additions & 0 deletions examples/gnrc_lorawan/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_KCONFIG_USEMODULE_GNRC_NETIF=y
CONFIG_GNRC_NETIF_LORAWAN_NETIF_HDR=y
72 changes: 7 additions & 65 deletions examples/gnrc_lorawan/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <if_pid> <payload> [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;
}
15 changes: 15 additions & 0 deletions sys/include/net/gnrc/netif/lorawan.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ extern "C" {
*/
#define GNRC_NETIF_LORAWAN_FLAGS_LINK_CHECK (0x1U)

/**
* @brief Encode LoRaWAN port in GNRC netif header.
*
* When set, GNRC netif will interpret the destination address of the
* GNRC netif header as the LoRaWAN port. For downlinks, a GNRC netif header
* with the received port in destination field will be included in the
* first snip.
*
* @deprecated From Release 2021.10 all GNRC LoRaWAN packets will include
* the GNRC Netif header. Therefore this parameter will be removed
*/
#if defined(DOXYGEN)
#define CONFIG_GNRC_NETIF_LORAWAN_NETIF_HDR
#endif

/**
* @brief GNRC LoRaWAN interface descriptor
*/
Expand Down
14 changes: 14 additions & 0 deletions sys/include/net/netopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,20 @@ typedef enum {
/**
* @brief (uint8_t) LoRaWAN TX application port
* - LoRaWAN: between 1 and 223 (included)
*
* @deprecated This option is deprecated and will be removed in the
* 2022.01 Release.
* The port is encoded now as a one byte
* destination address in a @ref net_gnrc_netif_hdr snip
* prepended in the packet.
* The user must take care of prepending the required snip
* during transmission. On reception, the
* snip is prepended automatically by the stack and shall be
* consumed by the user.
* During the deprecation period it is required to
* compile with @ref
* CONFIG_GNRC_NETIF_LORAWAN_NETIF_HDR
*
*/
NETOPT_LORAWAN_TX_PORT,

Expand Down
5 changes: 5 additions & 0 deletions sys/net/gnrc/link_layer/lorawan/gnrc_lorawan_mcps.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ int gnrc_lorawan_parse_dl(gnrc_lorawan_t *mac, uint8_t *buf, size_t len,
return -1;
}

if (pkt->port > LORAMAC_PORT_MAX) {
DEBUG("gnrc_lorawan: packet with port > 223. Drop\n");
return -1;
}

if (buf < p_mic) {
pkt->enc_payload.iol_base = buf;
pkt->enc_payload.iol_len = p_mic - buf;
Expand Down
13 changes: 13 additions & 0 deletions sys/net/gnrc/netif/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ config GNRC_NETIF_PKTQ_TIMER_US
Set to -1 to deactivate dequeing by timer. For this it has to be ensured
that none of the notifications by the driver are missed!

config GNRC_NETIF_LORAWAN_NETIF_HDR
bool "Encode LoRaWAN port in GNRC netif header"
depends on USEMODULE_GNRC_LORAWAN
help
When set, GNRC Netif will interpret
the destination address of the GNRC netif header as
the LoRaWAN port. For downlinks, a GNRC netif header
with the received port in destination field will be
included in the first snip. From Release 2021.10 all
GNRC LoRaWAN packets will include the GNRC Netif
header. Therefore this parameter will be removed


endif # KCONFIG_USEMODULE_GNRC_NETIF
Loading