Skip to content

Commit

Permalink
Merge pull request RIOT-OS#11022 from jia200x/pr/gnrc_lorawan
Browse files Browse the repository at this point in the history
gnrc_lorawan: add support for GNRC based LoRaWAN stack
  • Loading branch information
MichelRottleuthner authored Nov 27, 2019
2 parents 574c7d5 + 43b7164 commit bf676d4
Show file tree
Hide file tree
Showing 29 changed files with 3,085 additions and 4 deletions.
12 changes: 12 additions & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ ifneq (,$(filter gnrc_gomach,$(USEMODULE)))
FEATURES_REQUIRED += periph_rtt
endif

ifneq (,$(filter gnrc_lorawan,$(USEMODULE)))
USEMODULE += xtimer
USEMODULE += random
USEMODULE += hashes
USEMODULE += crypto
USEMODULE += netdev_layer
USEMODULE += gnrc_neterr
endif

ifneq (,$(filter nhdp,$(USEMODULE)))
USEMODULE += sock_udp
USEMODULE += xtimer
Expand Down Expand Up @@ -158,6 +167,9 @@ ifneq (,$(filter gnrc_netif,$(USEMODULE)))
ifneq (,$(filter netdev_eth,$(USEMODULE)))
USEMODULE += gnrc_netif_ethernet
endif
ifneq (,$(filter gnrc_lorawan,$(USEMODULE)))
USEMODULE += gnrc_netif_lorawan
endif
endif

ifneq (,$(filter ieee802154 nrfmin esp_now cc110x gnrc_sixloenc,$(USEMODULE)))
Expand Down
6 changes: 6 additions & 0 deletions drivers/include/net/netdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ typedef enum {
NETDEV_EVENT_CRC_ERROR, /**< wrong CRC */
NETDEV_EVENT_FHSS_CHANGE_CHANNEL, /**< channel changed */
NETDEV_EVENT_CAD_DONE, /**< channel activity detection done */
NETDEV_EVENT_MLME_CONFIRM, /**< MAC MLME confirm event */
NETDEV_EVENT_MLME_INDICATION, /**< MAC MLME indication event */
NETDEV_EVENT_MCPS_CONFIRM, /**< MAC MCPS confirm event */
NETDEV_EVENT_MCPS_INDICATION, /**< MAC MCPS indication event */
NETDEV_EVENT_MLME_GET_BUFFER, /**< MAC layer requests MLME buffer */
NETDEV_EVENT_MCPS_GET_BUFFER, /**< MAC layer requests MCPS buffer */
/* expand this list if needed */
} netdev_event_t;

Expand Down
60 changes: 60 additions & 0 deletions examples/gnrc_lorawan/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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

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

# Turn on developer helpers
DEVELHELP ?= 1

# use SX1276 by default
DRIVER ?= sx1276

USEMODULE += $(DRIVER)

# Required for the cipher module */
CFLAGS += -DCRYPTO_AES
#
# We can reduce the size of the packet buffer for LoRaWAN, since there's no IP
# support. This will reduce RAM consumption.
CFLAGS += -DGNRC_PKTBUF_SIZE=512


########################### COMPILE TIME CONFIGURATION ########################
# NOTE: The following options can be configured on runtime as well using
# `ifconfig`

# OTAA compile time configuration keys
CFLAGS += -DLORAMAC_APP_KEY_DEFAULT=\{0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\}
CFLAGS += -DLORAMAC_APP_EUI_DEFAULT=\{0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\}
CFLAGS += -DLORAMAC_DEV_EUI_DEFAULT=\{0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\}

# Uncomment and replace with proper keys for joining with ABP
# NOTE: This values will be overriten in case of OTAA.
#CFLAGS += -DLORAMAC_DEV_ADDR_DEFAULT=\{0x00\,0x00\,0x00\,0x00\}
#CFLAGS += -DLORAMAC_NWK_SKEY_DEFAULT=\{0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\}
#CFLAGS += -DLORAMAC_APP_SKEY_DEFAULT=\{0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\,0x00\}

# Comment/uncomment as necessary
CFLAGS += -DLORAMAC_DEFAULT_JOIN_PROCEDURE=LORAMAC_JOIN_OTAA
#CFLAGS += -DLORAMAC_DEFAULT_JOIN_PROCEDURE=LORAMAC_JOIN_ABP

# Uncomment to set the highest DR for the EU868 in order to maximize throughput.
# If uncommented, the default value (DR0) is used.
# Note this value is also used for the OTAA.
#CFLAGS += -DLORAMAC_DEFAULT_DR=LORAMAC_DR_5

# Set the default RX2 datarate to DR3 (used by The Things Network)
CFLAGS += -DLORAMAC_DEFAULT_RX2_DR=LORAMAC_DR_3

# Set default messages to unconfirmable
CFLAGS += -DLORAMAC_DEFAULT_TX_MODE=LORAMAC_TX_CNF

include $(RIOTBASE)/Makefile.include
20 changes: 20 additions & 0 deletions examples/gnrc_lorawan/Makefile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
BOARD_INSUFFICIENT_MEMORY := \
arduino-duemilanove \
arduino-leonardo \
arduino-mega2560 \
arduino-nano \
arduino-uno \
atmega328p \
nucleo-f031k6 \
nucleo-f042k6 \
nucleo-l031k6 \
stm32f030f4-demo \
stm32f0discovery \
msb-430 \
msb-430h \
telosb \
waspmote-pro \
wsn430-v1_3b \
wsn430-v1_4 \
z1 \
#
145 changes: 145 additions & 0 deletions examples/gnrc_lorawan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
GNRC LoRaWAN application
=============================

This application is a showcase for testing GNRC LoRaWAN stack. You should be
able to send and receive LoRaWAN packets and perform basic LoRaWAN commands
(Link Check).

The MAC layers still doesn't implement any duty cycle restriction mechanism.
However, it respects the retransmission procedure.

Only Class A and EU868 region are supported so far.

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).

## OTAA

Join by OTAA is set by default.
Set the Application Key, Device EUI and Application EUI using ifconfig. Assuming
the interface pid is 3:

```
ifconfig 3 set deveui AAAAAAAAAAAAAAAA
ifconfig 3 set appeui BBBBBBBBBBBBBBBB
ifconfig 3 set appkey CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ifconfig 3 up
```

Wait for 5-6 seconds. Type `ifconfig`. The link status should be `up`:

```
Iface 3 HWaddr: 26:01:27:2F Frequency: 868500000Hz BW: 125kHz SF: 7
CR: 4/5 Link: up
TX-Power: 14dBm State: SLEEP Demod margin.: 0 Num gateways.: 0
IQ_INVERT
RX_SINGLE OTAA
```

## ABP

Deactivate OTAA using ifconfig and set the AppSKey, NwkSKey and DevAddr;

```
ifconfig 3 -otaa
ifconfig 3 set appskey DDDDDDDDDDDDDDDD
ifconfig 3 set nwkskey EEEEEEEEEEEEEEEE
ifconfig 3 set addr FFFFFFFF
ifconfig 3 up
```

The join by ABP occurs immediately.

Alternatively all keys can be set using CFLAGS so it's only required to
select join mode and type `ifconfig <if_pid> up`.

E.g in the application Makefile:

```
CFLAGS += -DLORAMAC_DEV_EUI_DEFAULT=\{0xAA\,0xAA\,0xAA\,0xAA\,0xAA\,0xAA\,0xAA\,0xAA\}
CFLAGS += -DLORAMAC_APP_EUI_DEFAULT=\{0xBB\,0xBB\,0xBB\,0xBB\,0xBB\,0xBB\,0xBB\,0xBB\}
CFLAGS += -DLORAMAC_APP_KEY_DEFAULT=\{0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\,0xCC\}
CFLAGS += -DLORAMAC_APP_SKEY_DEFAULT=\{0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\,0xDD\}
CFLAGS += -DLORAMAC_NWK_SKEY_DEFAULT=\{0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\,0xEE\}
CFLAGS += -DLORAMAC_DEV_ADDR_DEFAULT=\{0xFF\,0xFF\,0xFF\,0xFF\}
```

## Send data

After join, send data using `send` command. E.g to send "Hello RIOT!" to port 2:

```
send 3 "Hello RIOT!" 2
```

## Changing datarate of transmission
Use `ifconfig` to change the datarate of the transmission. E.g to set the DR to
2:

```
ifconfig 3 set dr 2
```

## Perform a Link Check

Use `ifconfig` to request a Link Check on the next transmission:

```
ifconfig 3 link_check
```

Send some data. The result of the Link Check request can be seen with
`ifconfig`.

```
ifconfig 3 link_check
send 3 "Join the RIOT!"
```

Check demodulation margin and number of gateways using `ifconfig`

```
ifconfig
Iface 3 HWaddr: 26:01:2C:EA Frequency: 867500000Hz BW: 125kHz SF: 7
CR: 4/5 Link: up
TX-Power: 14dBm State: SLEEP Demod margin.: 14 Num gateways.: 2
IQ_INVERT
RX_SINGLE OTAA
```

## Confirmable and unconfirmable messages

Use `ifconfig` to set the `ack_req` flag. With this flag on, messages are
confirmable.

E.g send confirmable messages:

```
ifconfig 3 ack_req
send "My confirmable message"
```

And unconfirmable messages:

```
ifconfig 3 -ack_req
send "My unconfirmable message"
```

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
cycle restrictions and some FOps are missing. Work in progress.

Next steps:
- Add other regions (US915, etc)
- Add Adaptive Data Rate
- Add Duty Cycle restrictions
- Add support for RTC
109 changes: 109 additions & 0 deletions examples/gnrc_lorawan/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (C) 2019 HAW Hamburg
*
* 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.
*/

/**
* @ingroup tests
* @{
* @file
* @brief Test application for GNRC LoRaWAN
*
* @author José Ignacio Alamos <[email protected]>
* @}
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "thread.h"
#include "xtimer.h"
#include "shell.h"
#include "shell_commands.h"

#include "board.h"

#include "net/gnrc/netapi.h"
#include "net/gnrc/netif.h"

#include "net/gnrc/pktbuf.h"
#include "net/gnrc/pktdump.h"
#include "net/gnrc/netreg.h"

#define LORAWAN_PORT (2U)

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 = LORAWAN_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_netapi_send(interface, pkt);

msg_t msg;
/* wait for packet status and check */
msg_receive(&msg);
if ((msg.type != GNRC_NETERR_MSG_TYPE) ||
(msg.content.value != GNRC_NETERR_SUCCESS)) {
puts("Error sending packet (not joined?)");
}
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(LORAWAN_PORT,
gnrc_pktdump_pid);
gnrc_netreg_register(GNRC_NETTYPE_LORAWAN, &dump);
char line_buf[SHELL_DEFAULT_BUFSIZE];

shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

return 0;
}
4 changes: 4 additions & 0 deletions sys/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ ifneq (,$(filter gnrc_sixlowpan_frag_rb,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/gnrc/network_layer/sixlowpan/frag
endif

ifneq (,$(filter gnrc_lorawan,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/gnrc/link_layer/lorawan/include
endif

ifneq (,$(filter gnrc_sock,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/gnrc/sock/include
ifneq (,$(filter gnrc_ipv6,$(USEMODULE)))
Expand Down
Loading

0 comments on commit bf676d4

Please sign in to comment.