Application example using LoRaWAN-lib MAC layer implementation

Dependencies:   mbed LoRaWAN-lib SX1272Lib

LoRaWAN-demo is a ClassA device example project using LoRaWAN-lib and SX1272Lib 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:
Thu Jan 07 15:11:08 2016 +0000
Revision:
0:45496a70a8a5
Initial commit

Who changed what in which revision?

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