Application example using LoRaWAN-lib MAC layer implementation

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Dependents:   LoRaWAN-mbed-client-ttn0

LoRaWAN-demo is a ClassA device example project using LoRaWAN-lib and SX1276Lib libraries.

This demo application sends a frame every 4 to 6 seconds (randomly) and displays its current status using a serial port as display(VT100).

The serial port settings are as shown in below image. To access the serial port settings please click on "Setup" menu and then "Serial port..."

/media/uploads/mluis/serial_port_settings.png

The terminal window should be setup as shown in below image. To access the terminal window settings please click on "Setup" menu and then "Terminal..."

/media/uploads/mluis/terminal_window_settings.png

The image below shows the VT100 application status.

Application main screen

The application gives the possibility to either activate the device using

  • Over The Air Activation (OTAA)
  • Personalization activation (PA)

The activation mode can be adjusted in Comissioning.h by changing the following parameter:

/*!
 * When set to 1 the application uses the Over-the-Air activation procedure
 * When set to 0 the application uses the Personalization activation procedure
 */
#define OVER_THE_AIR_ACTIVATION                     1


The application gives the possibility to select which kind of network we are connecting to.

  • Public Network (true)
  • Private Network (false)

The netork type can be changed as follows:

/*!
 * Indicates if the end-device is to be connected to a private or public network
 */
#define LORAWAN_PUBLIC_NETWORK                      true


OTAA
When OTAA is selected the user must porvide a device EUI, an application EUI and an application key.
These can be adjusted by changing the following parameters:

/*!
 * Mote device IEEE EUI (big endian)
 */
#define LORAWAN_DEVICE_EUI                          { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }

/*!
 * Application IEEE EUI (big endian)
 */
#define LORAWAN_APPLICATION_EUI                     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }

/*!
 * AES encryption/decryption cipher application key
 */
#define LORAWAN_APPLICATION_KEY                     { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }


PA
When PA is selected the user must porvide a network ID, a device address, a network session key and an application session key.
These can be adjusted by changing the following parameters:

/*!
 * Current network ID
 */
#define LORAWAN_NETWORK_ID                          ( uint32_t )0

/*!
 * Device address on the network (big endian)
 */
#define LORAWAN_DEVICE_ADDRESS                      ( uint32_t )0x12345678

/*!
 * AES encryption/decryption cipher network session key
 */
#define LORAWAN_NWKSKEY                             { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }

/*!
 * AES encryption/decryption cipher application session key
 */
#define LORAWAN_APPSKEY                             { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }


On top of main.c the user has the possibility to tweak some application settings such as:

  • Join requests transmission frequency
  • Frames transmission frequency
  • Application default datarate
  • Confirmed or Unconfirmed frames transmission
  • ADR (Adaptive Datarate) ON/OFF
  • Application port to be used by the transmitted frames

The join requests transmission frequency can be adjusted by changing the follwoing parameter:

/*!
 * Join requests trials duty cycle.
 */
#define OVER_THE_AIR_ACTIVATION_DUTYCYCLE           10000000  // 10 [s] value in us


The frame transmission frequency can be adjusted by changing the follwoing parameters:

/*!
 * Defines the application data transmission duty cycle. 5s, value in [us].
 */
#define APP_TX_DUTYCYCLE                            5000000

/*!
 * Defines a random delay for application data transmission duty cycle. 1s,
 * value in [us].
 */
#define APP_TX_DUTYCYCLE_RND                        1000000


The frame transmission scheduling is then executed as follows:

        if( ScheduleNextTx == true )
        {
            ScheduleNextTx = false;
            // Schedule next packet transmission
            TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
            TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
            TimerStart( &TxNextPacketTimer );
        }


The application default datarate can be adjusted by changing the following parameter:

Quote:

When ADR is off this setting is the fixed datarate that will be used by the application.
When ADR is on this setting is the initial datarate used by the application.

/*!
 * Default mote datarate
 */
#define LORAWAN_DEFAULT_DATARATE                    DR_0


The transmitted frame contents will depend on LORAWAN_CONFIRMED_MSG_ON value.

/*!
 * LoRaWAN confirmed messages
 */
#define LORAWAN_CONFIRMED_MSG_ON                    true
  • If LORAWAN_CONFIRMED_MSG_ON equals false then the application payload is one byte corresponding to the AppLed state.
  • If LORAWAN_CONFIRMED_MSG_ON equals true then the application payload is six bytes corresponding to the AppLed state, Downlink counter (unsigned 16 bits), received RSSI (signed 16 bits) and received SNR (signed 8 bits).

/*!
 * \brief   Prepares the payload of the frame
 */
static void PrepareTxFrame( uint8_t port )
{

    switch( port )
    {
    case 15:
        {
            AppData[0] = AppLedStateOn;
            if( IsTxConfirmed == true )
            {
                AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
                AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
                AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
                AppData[4] = LoRaMacDownlinkStatus.Rssi;
                AppData[5] = LoRaMacDownlinkStatus.Snr;
            }
        }
        break;
    case 224:
...
}


The ADR enabling/disabling can be adjusted by changing the following parameter:

/*!
 * LoRaWAN Adaptive Data Rate
 *
 * \remark Please note that when ADR is enabled the end-device should be static
 */
#define LORAWAN_ADR_ON                              1


The application port can be adjusted by changing the following parameter:

/*!
 * LoRaWAN application port
 */
#define LORAWAN_APP_PORT                            15
Committer:
mluis
Date:
Mon Apr 24 13:38:31 2017 +0000
Revision:
9:ee9dcbb9708d
Parent:
1:352f608c3337
WARNING: Radio API timings changed from micro-seconds to milliseconds; ; Synchronized with https://github.com/Lora-net/LoRaMac-node git revision e506c246652fa44c3f24cecb89d0707b49ece739; Updated all libraries to the latest versions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 1:352f608c3337 1 /*
mluis 1:352f608c3337 2 ---------------------------------------------------------------------------
mluis 1:352f608c3337 3 Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
mluis 1:352f608c3337 4
mluis 1:352f608c3337 5 LICENSE TERMS
mluis 1:352f608c3337 6
mluis 1:352f608c3337 7 The redistribution and use of this software (with or without changes)
mluis 1:352f608c3337 8 is allowed without the payment of fees or royalties provided that:
mluis 1:352f608c3337 9
mluis 1:352f608c3337 10 1. source code distributions include the above copyright notice, this
mluis 1:352f608c3337 11 list of conditions and the following disclaimer;
mluis 1:352f608c3337 12
mluis 1:352f608c3337 13 2. binary distributions include the above copyright notice, this list
mluis 1:352f608c3337 14 of conditions and the following disclaimer in their documentation;
mluis 1:352f608c3337 15
mluis 1:352f608c3337 16 3. the name of the copyright holder is not used to endorse products
mluis 1:352f608c3337 17 built using this software without specific written permission.
mluis 1:352f608c3337 18
mluis 1:352f608c3337 19 DISCLAIMER
mluis 1:352f608c3337 20
mluis 1:352f608c3337 21 This software is provided 'as is' with no explicit or implied warranties
mluis 1:352f608c3337 22 in respect of its properties, including, but not limited to, correctness
mluis 1:352f608c3337 23 and/or fitness for purpose.
mluis 1:352f608c3337 24 ---------------------------------------------------------------------------
mluis 1:352f608c3337 25 Issue 09/09/2006
mluis 1:352f608c3337 26
mluis 1:352f608c3337 27 This is an AES implementation that uses only 8-bit byte operations on the
mluis 1:352f608c3337 28 cipher state.
mluis 1:352f608c3337 29 */
mluis 1:352f608c3337 30
mluis 1:352f608c3337 31 #ifndef AES_H
mluis 1:352f608c3337 32 #define AES_H
mluis 1:352f608c3337 33
mluis 1:352f608c3337 34 #if 1
mluis 1:352f608c3337 35 # define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */
mluis 1:352f608c3337 36 #endif
mluis 1:352f608c3337 37 #if 0
mluis 1:352f608c3337 38 # define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */
mluis 1:352f608c3337 39 #endif
mluis 1:352f608c3337 40 #if 0
mluis 1:352f608c3337 41 # define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
mluis 1:352f608c3337 42 #endif
mluis 1:352f608c3337 43 #if 0
mluis 1:352f608c3337 44 # define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
mluis 1:352f608c3337 45 #endif
mluis 1:352f608c3337 46 #if 0
mluis 1:352f608c3337 47 # define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
mluis 1:352f608c3337 48 #endif
mluis 1:352f608c3337 49 #if 0
mluis 1:352f608c3337 50 # define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */
mluis 1:352f608c3337 51 #endif
mluis 1:352f608c3337 52
mluis 1:352f608c3337 53 #define N_ROW 4
mluis 1:352f608c3337 54 #define N_COL 4
mluis 1:352f608c3337 55 #define N_BLOCK (N_ROW * N_COL)
mluis 1:352f608c3337 56 #define N_MAX_ROUNDS 14
mluis 1:352f608c3337 57
mluis 1:352f608c3337 58 typedef uint8_t return_type;
mluis 1:352f608c3337 59
mluis 1:352f608c3337 60 /* Warning: The key length for 256 bit keys overflows a byte
mluis 1:352f608c3337 61 (see comment below)
mluis 1:352f608c3337 62 */
mluis 1:352f608c3337 63
mluis 1:352f608c3337 64 typedef uint8_t length_type;
mluis 1:352f608c3337 65
mluis 1:352f608c3337 66 typedef struct
mluis 1:352f608c3337 67 { uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
mluis 1:352f608c3337 68 uint8_t rnd;
mluis 1:352f608c3337 69 } aes_context;
mluis 1:352f608c3337 70
mluis 1:352f608c3337 71 /* The following calls are for a precomputed key schedule
mluis 1:352f608c3337 72
mluis 1:352f608c3337 73 NOTE: If the length_type used for the key length is an
mluis 1:352f608c3337 74 unsigned 8-bit character, a key length of 256 bits must
mluis 1:352f608c3337 75 be entered as a length in bytes (valid inputs are hence
mluis 1:352f608c3337 76 128, 192, 16, 24 and 32).
mluis 1:352f608c3337 77 */
mluis 1:352f608c3337 78
mluis 1:352f608c3337 79 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
mluis 1:352f608c3337 80
mluis 1:352f608c3337 81 return_type aes_set_key( const uint8_t key[],
mluis 1:352f608c3337 82 length_type keylen,
mluis 1:352f608c3337 83 aes_context ctx[1] );
mluis 1:352f608c3337 84 #endif
mluis 1:352f608c3337 85
mluis 1:352f608c3337 86 #if defined( AES_ENC_PREKEYED )
mluis 1:352f608c3337 87
mluis 1:352f608c3337 88 return_type aes_encrypt( const uint8_t in[N_BLOCK],
mluis 1:352f608c3337 89 uint8_t out[N_BLOCK],
mluis 1:352f608c3337 90 const aes_context ctx[1] );
mluis 1:352f608c3337 91
mluis 1:352f608c3337 92 return_type aes_cbc_encrypt( const uint8_t *in,
mluis 1:352f608c3337 93 uint8_t *out,
mluis 1:352f608c3337 94 int32_t n_block,
mluis 1:352f608c3337 95 uint8_t iv[N_BLOCK],
mluis 1:352f608c3337 96 const aes_context ctx[1] );
mluis 1:352f608c3337 97 #endif
mluis 1:352f608c3337 98
mluis 1:352f608c3337 99 #if defined( AES_DEC_PREKEYED )
mluis 1:352f608c3337 100
mluis 1:352f608c3337 101 return_type aes_decrypt( const uint8_t in[N_BLOCK],
mluis 1:352f608c3337 102 uint8_t out[N_BLOCK],
mluis 1:352f608c3337 103 const aes_context ctx[1] );
mluis 1:352f608c3337 104
mluis 1:352f608c3337 105 return_type aes_cbc_decrypt( const uint8_t *in,
mluis 1:352f608c3337 106 uint8_t *out,
mluis 1:352f608c3337 107 int32_t n_block,
mluis 1:352f608c3337 108 uint8_t iv[N_BLOCK],
mluis 1:352f608c3337 109 const aes_context ctx[1] );
mluis 1:352f608c3337 110 #endif
mluis 1:352f608c3337 111
mluis 1:352f608c3337 112 /* The following calls are for 'on the fly' keying. In this case the
mluis 1:352f608c3337 113 encryption and decryption keys are different.
mluis 1:352f608c3337 114
mluis 1:352f608c3337 115 The encryption subroutines take a key in an array of bytes in
mluis 1:352f608c3337 116 key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
mluis 1:352f608c3337 117 192, and 256 bits respectively. They then encrypts the input
mluis 1:352f608c3337 118 data, in[] with this key and put the reult in the output array
mluis 1:352f608c3337 119 out[]. In addition, the second key array, o_key[L], is used
mluis 1:352f608c3337 120 to output the key that is needed by the decryption subroutine
mluis 1:352f608c3337 121 to reverse the encryption operation. The two key arrays can
mluis 1:352f608c3337 122 be the same array but in this case the original key will be
mluis 1:352f608c3337 123 overwritten.
mluis 1:352f608c3337 124
mluis 1:352f608c3337 125 In the same way, the decryption subroutines output keys that
mluis 1:352f608c3337 126 can be used to reverse their effect when used for encryption.
mluis 1:352f608c3337 127
mluis 1:352f608c3337 128 Only 128 and 256 bit keys are supported in these 'on the fly'
mluis 1:352f608c3337 129 modes.
mluis 1:352f608c3337 130 */
mluis 1:352f608c3337 131
mluis 1:352f608c3337 132 #if defined( AES_ENC_128_OTFK )
mluis 1:352f608c3337 133 void aes_encrypt_128( const uint8_t in[N_BLOCK],
mluis 1:352f608c3337 134 uint8_t out[N_BLOCK],
mluis 1:352f608c3337 135 const uint8_t key[N_BLOCK],
mluis 1:352f608c3337 136 uint8_t o_key[N_BLOCK] );
mluis 1:352f608c3337 137 #endif
mluis 1:352f608c3337 138
mluis 1:352f608c3337 139 #if defined( AES_DEC_128_OTFK )
mluis 1:352f608c3337 140 void aes_decrypt_128( const uint8_t in[N_BLOCK],
mluis 1:352f608c3337 141 uint8_t out[N_BLOCK],
mluis 1:352f608c3337 142 const uint8_t key[N_BLOCK],
mluis 1:352f608c3337 143 uint8_t o_key[N_BLOCK] );
mluis 1:352f608c3337 144 #endif
mluis 1:352f608c3337 145
mluis 1:352f608c3337 146 #if defined( AES_ENC_256_OTFK )
mluis 1:352f608c3337 147 void aes_encrypt_256( const uint8_t in[N_BLOCK],
mluis 1:352f608c3337 148 uint8_t out[N_BLOCK],
mluis 1:352f608c3337 149 const uint8_t key[2 * N_BLOCK],
mluis 1:352f608c3337 150 uint8_t o_key[2 * N_BLOCK] );
mluis 1:352f608c3337 151 #endif
mluis 1:352f608c3337 152
mluis 1:352f608c3337 153 #if defined( AES_DEC_256_OTFK )
mluis 1:352f608c3337 154 void aes_decrypt_256( const uint8_t in[N_BLOCK],
mluis 1:352f608c3337 155 uint8_t out[N_BLOCK],
mluis 1:352f608c3337 156 const uint8_t key[2 * N_BLOCK],
mluis 1:352f608c3337 157 uint8_t o_key[2 * N_BLOCK] );
mluis 1:352f608c3337 158 #endif
mluis 1:352f608c3337 159
mluis 1:352f608c3337 160 #endif