Forked (hacked) for Legacy Gateway testing

Dependencies:   mbed LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72 by Miguel Luis

Revision:
9:e395d85afe81
Parent:
6:f9224695ed01
--- a/app/main.cpp	Tue Apr 04 08:23:15 2017 +0000
+++ b/app/main.cpp	Thu Apr 20 14:30:02 2017 +0000
@@ -23,7 +23,7 @@
 /*!
  * Defines the application data transmission duty cycle. 5s, value in [ms].
  */
-#define APP_TX_DUTYCYCLE                            5000
+#define APP_TX_DUTYCYCLE                            8000
 
 /*!
  * Defines a random delay for application data transmission duty cycle. 1s,
@@ -34,7 +34,7 @@
 /*!
  * Default datarate
  */
-#define LORAWAN_DEFAULT_DATARATE                    DR_0
+#define LORAWAN_DEFAULT_DATARATE                    DR_7
 
 /*!
  * LoRaWAN confirmed messages
@@ -46,9 +46,9 @@
  *
  * \remark Please note that when ADR is enabled the end-device should be static
  */
-#define LORAWAN_ADR_ON                              1
+#define LORAWAN_ADR_ON                              0
 
-#if defined( USE_BAND_868 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
 
 #include "LoRaMacTest.h"
 
@@ -57,9 +57,9 @@
  *
  * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
  */
-#define LORAWAN_DUTYCYCLE_ON                        true
+#define LORAWAN_DUTYCYCLE_ON                        false
 
-#define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP          1
+#define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP          0
 
 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) 
 
@@ -68,9 +68,20 @@
 #define LC6                { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
 #define LC7                { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
 #define LC8                { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
-#define LC9                { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
+#define LC9                { 868270000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
 #define LC10               { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
 
+#else
+
+#define LC4                { 868540000, { ( ( DR_6 << 4 ) | DR_6 ) }, 0 }
+#define LC5                { 867720000, { ( ( DR_0 << 4 ) | DR_0 ) }, 0 }
+#define LC6                { 867200000, { ( ( DR_0 << 4 ) | DR_0 ) }, 0 }
+#define LC7                { 867890000, { ( ( DR_0 << 4 ) | DR_0 ) }, 0 }
+#define LC8                { 868195000, { ( ( DR_5 << 4 ) | DR_5 ) }, 0 }
+#define LC9                { 866600000, { ( ( DR_5 << 4 ) | DR_5 ) }, 0 }
+#define LC10               { 868345000, { ( ( DR_5 << 4 ) | DR_5 ) }, 0 }
+#define LC11               { 865150000, { ( ( DR_4 << 4 ) | DR_4 ) }, 0 }
+
 #endif
 
 #endif
@@ -160,6 +171,9 @@
 volatile bool Led2State = false;
 volatile bool Led2StateChanged = false;
 
+uint8_t CurrentDatarate = LORAWAN_DEFAULT_DATARATE;
+InterruptIn DatarateButton(USER_BUTTON);
+
 /*!
  * Indicates if a new packet can be sent
  */
@@ -254,7 +268,7 @@
     SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
 
     SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
-#if defined( USE_BAND_868 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
     SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
 #else
     SerialDisplayUpdateDutyCycle( false );
@@ -347,7 +361,7 @@
         mcpsReq.Type = MCPS_UNCONFIRMED;
         mcpsReq.Req.Unconfirmed.fBuffer = NULL;
         mcpsReq.Req.Unconfirmed.fBufferSize = 0;
-        mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
+        mcpsReq.Req.Unconfirmed.Datarate = CurrentDatarate;
 
         LoRaMacUplinkStatus.Acked = false;
         LoRaMacUplinkStatus.Port = 0;
@@ -369,7 +383,7 @@
             mcpsReq.Req.Unconfirmed.fPort = AppPort;
             mcpsReq.Req.Unconfirmed.fBuffer = AppData;
             mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
-            mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
+            mcpsReq.Req.Unconfirmed.Datarate = CurrentDatarate;
         }
         else
         {
@@ -378,7 +392,7 @@
             mcpsReq.Req.Confirmed.fBuffer = AppData;
             mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
             mcpsReq.Req.Confirmed.NbTrials = 8;
-            mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
+            mcpsReq.Req.Confirmed.Datarate = CurrentDatarate;
         }
     }
 
@@ -589,7 +603,7 @@
                     mibReq.Param.AdrEnable = true;
                     LoRaMacMibSetRequestConfirm( &mibReq );
 
-#if defined( USE_BAND_868 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
                     LoRaMacTestSetDutyCycleOn( false );
 #endif
                 }
@@ -610,7 +624,7 @@
                     mibReq.Type = MIB_ADR;
                     mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
                     LoRaMacMibSetRequestConfirm( &mibReq );
-#if defined( USE_BAND_868 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
                     LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
 #endif
                     break;
@@ -656,7 +670,7 @@
                         mibReq.Type = MIB_ADR;
                         mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
                         LoRaMacMibSetRequestConfirm( &mibReq );
-#if defined( USE_BAND_868 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
                         LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
 #endif
 
@@ -738,6 +752,30 @@
     UplinkStatusUpdated = true;
 }
 
+void UserButtonPressed( void )
+{
+    switch( CurrentDatarate )
+    {
+        case DR_7:
+            CurrentDatarate = DR_6;
+            break;
+        case DR_6:
+            CurrentDatarate = DR_5;
+            break;
+        case DR_5:
+            CurrentDatarate = DR_4;
+            break;
+        case DR_4:
+            CurrentDatarate = DR_0;
+            break;
+        case DR_0:
+            CurrentDatarate = DR_7;
+            break;
+        default:
+            break;
+    }
+}
+
 /**
  * Main application entry point.
  */
@@ -763,6 +801,8 @@
 
     DeviceState = DEVICE_STATE_INIT;
 
+    DatarateButton.fall( &UserButtonPressed );
+
     while( 1 )
     {
         SerialRxProcess( );
@@ -826,7 +866,7 @@
                 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
                 LoRaMacMibSetRequestConfirm( &mibReq );
 
-#if defined( USE_BAND_868 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
                 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
                 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
 
@@ -842,6 +882,19 @@
                 mibReq.Type = MIB_RX2_CHANNEL;
                 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
                 LoRaMacMibSetRequestConfirm( &mibReq );
+#else
+                LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
+                LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
+                LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
+                LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
+                LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
+                LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
+                LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
+                LoRaMacChannelAdd( 10, ( ChannelParams_t )LC11 );
+
+                mibReq.Type = MIB_RX2_CHANNEL;
+                mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 865150000, DR_4 };
+                LoRaMacMibSetRequestConfirm( &mibReq );
 #endif
 
 #endif