Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Files at this revision

API Documentation at this revision

Comitter:
Shaun Nelson
Date:
Thu Sep 21 17:02:39 2017 -0400
Branch:
class_b
Parent:
47:e7fd944a7215
Commit message:
Initialize PingSlot datarate
Add control to adjust beacon rx time
Add debug

Changed in this revision

LoRaMacClassB.cpp Show annotated file Show diff for this revision Revisions of this file
LoRaMacClassB.h Show annotated file Show diff for this revision Revisions of this file
--- a/LoRaMacClassB.cpp	Thu Sep 21 17:01:33 2017 -0400
+++ b/LoRaMacClassB.cpp	Thu Sep 21 17:02:39 2017 -0400
@@ -42,7 +42,7 @@
 /*!
  * Class B ping slot context
  */
-static PingSlotContext_t PingSlotCtx;
+PingSlotContext_t PingSlotCtx;
 
 /*!
  * Class B beacon context
@@ -247,6 +247,10 @@
     getPhy.Attribute = PHY_PING_SLOT_SYMBOL_TO_EXPANSION_FACTOR;
     phyParam = RegionGetPhyParam( *LoRaMacClassBParams.LoRaMacRegion, &getPhy );
     PingSlotCtx.Cfg.SymbolToExpansionFactor = phyParam.Value;
+
+    getPhy.Attribute = PHY_BEACON_CHANNEL_DR;
+    phyParam = RegionGetPhyParam( *LoRaMacClassBParams.LoRaMacRegion, &getPhy );
+    PingSlotCtx.Datarate = phyParam.Value;
 #endif // LORAMAC_CLASSB_ENABLED
 }
 
@@ -271,6 +275,8 @@
 	return BeaconEventTime;
 }
 
+extern uint32_t BeaconPingSlotAnsAdjust;
+
 void LoRaMacClassBBeaconTimerEvent( void )
 {
 #ifdef LORAMAC_CLASSB_ENABLED
@@ -309,14 +315,18 @@
                         if( BeaconCtx.NextBeaconRx > currentTime )
                         {
                             BeaconCtx.Ctrl.AcquisitionTimerSet = 1;
-                            beaconEventTime = TimerTempCompensation( BeaconCtx.NextBeaconRx - currentTime, BeaconCtx.Temperature );
+                            beaconEventTime = TimerTempCompensation( BeaconCtx.NextBeaconRx - currentTime, BeaconCtx.Temperature ) - 2000;
+                            BeaconCtx.BeaconAcqTime = beaconEventTime + currentTime;
                         }
                         else
                         {
                             BeaconCtx.Ctrl.BeaconDelaySet = 0;
                             BeaconCtx.Ctrl.AcquisitionPending = 1;
                             BeaconCtx.Ctrl.AcquisitionTimerSet = 0;
-                            beaconEventTime = BeaconCtx.Cfg.Interval;
+
+							// SKN - Closing Beacon Rx window after 10 seconds instead of miss and wait of 128 seconds
+							beaconEventTime = 10000;
+							// beaconEventTime = BeaconCtx.Cfg.Interval;
 
                             rxBeaconSetup.SymbolTimeout = BeaconCtx.SymbolTimeout;
                             rxBeaconSetup.RxTime = 0;
@@ -330,9 +340,11 @@
 
                             RegionRxBeaconSetup( *LoRaMacClassBParams.LoRaMacRegion, &rxBeaconSetup, &LoRaMacClassBParams.McpsIndication->RxDatarate, &beaconChannelSet );
                             BeaconCtx.Ctrl.BeaconChannelSet = beaconChannelSet;
+                            BeaconCtx.BeaconAcqTime = 0;
                         }
                         BeaconCtx.NextBeaconRx = 0;
                         BeaconCtx.BeaconTimingDelay = 0;
+                        BeaconCtx.BeaconTimingAnsTime = 0;
                     }
                     else
                     {
@@ -350,6 +362,7 @@
                         rxBeaconSetup.CustomFrequency = BeaconCtx.Frequency;
                         rxBeaconSetup.BeaconTime = BeaconCtx.BeaconTime;
                         rxBeaconSetup.BeaconInterval = BeaconCtx.Cfg.Interval;
+                        BeaconCtx.BeaconAcqTime = 0;
 
                         RegionRxBeaconSetup( *LoRaMacClassBParams.LoRaMacRegion, &rxBeaconSetup, &LoRaMacClassBParams.McpsIndication->RxDatarate, &beaconChannelSet );
                         BeaconCtx.Ctrl.BeaconChannelSet = beaconChannelSet;
@@ -358,7 +371,9 @@
                 else
                 {
                     BeaconCtx.Ctrl.AcquisitionPending = 1;
-                    beaconEventTime = BeaconCtx.Cfg.Interval;
+                    // SKN - Closing Beacon Rx window after 10 seconds instead of miss and wait of 128 seconds
+                    beaconEventTime = 10000;
+                    // beaconEventTime = BeaconCtx.Cfg.Interval;
                     if( BeaconCtx.Ctrl.AcquisitionTimerSet == 0 )
                     {
                         rxBeaconSetup.SymbolTimeout = BeaconCtx.SymbolTimeout;
@@ -370,6 +385,7 @@
                         rxBeaconSetup.CustomFrequency = BeaconCtx.Frequency;
                         rxBeaconSetup.BeaconTime = BeaconCtx.BeaconTime;
                         rxBeaconSetup.BeaconInterval = BeaconCtx.Cfg.Interval;
+                        BeaconCtx.BeaconAcqTime = 0;
 
                         RegionRxBeaconSetup( *LoRaMacClassBParams.LoRaMacRegion, &rxBeaconSetup, &LoRaMacClassBParams.McpsIndication->RxDatarate, &beaconChannelSet );
                         BeaconCtx.Ctrl.BeaconChannelSet = beaconChannelSet;
@@ -516,6 +532,7 @@
                 LoRaMacClassBParams.LoRaMacFlags->Bits.MacDone = 1;
             }
             BeaconCtx.Ctrl.ResumeBeaconing = 0;
+
             break;
         }
         case BEACON_STATE_IDLE:
@@ -646,6 +663,7 @@
                 // Restore floor plan
                 getPhy.Attribute = PHY_PINGSLOT_CHANNEL_FREQ;
                 getPhy.DeviceAddress = *LoRaMacClassBParams.LoRaMacDevAddr;
+                getPhy.BeaconTime  =  BeaconCtx.BeaconTime;
                 phyParam = RegionGetPhyParam( *LoRaMacClassBParams.LoRaMacRegion, &getPhy );
                 frequency = phyParam.Value;
             }
@@ -776,6 +794,8 @@
                 // Restore floor plan
                 getPhy.Attribute = PHY_PINGSLOT_CHANNEL_FREQ;
                 getPhy.DeviceAddress = PingSlotCtx.NextMulticastChannel->Address;
+                getPhy.BeaconTime    = BeaconCtx.BeaconTime;
+                //BeaconCtx.Cfg.Interval;
                 phyParam = RegionGetPhyParam( *LoRaMacClassBParams.LoRaMacRegion, &getPhy );
                 frequency = phyParam.Value;
             }
@@ -819,6 +839,7 @@
 #endif // LORAMAC_CLASSB_ENABLED
 }
 
+
 bool LoRaMacClassBRxBeacon( uint8_t *payload, uint16_t size )
 {
 #ifdef LORAMAC_CLASSB_ENABLED
@@ -835,17 +856,23 @@
     getPhy.Attribute = PHY_BEACON_SIZE;
     phyParam = RegionGetPhyParam( *LoRaMacClassBParams.LoRaMacRegion, &getPhy );
 
+#define LORAWAN_ONE_ONE
+#ifdef LORAWAN_ONE_ONE
+    rfuOffset1 = 3;
+    rfuOffset2 = 3;
+#else
     // For beacon payload sizes > 17 we need to apply an offset
     if( phyParam.Value > 17 )
     {
         rfuOffset1 = 1;
         rfuOffset2 = 1;
     }
+#endif
 
     // Verify if we are in the state where we expect a beacon
     if( ( BeaconState == BEACON_STATE_RX ) || ( BeaconCtx.Ctrl.AcquisitionPending == 1 ) )
     {
-        if( size == phyParam.Value )
+        if( size ==  phyParam.Value )
         {
             beaconCrc0 = ( ( uint16_t ) payload[6 + rfuOffset1] ) & 0x00FF;
             beaconCrc0 |= ( ( uint16_t ) payload[7 + rfuOffset1] << 8 ) & 0xFF00;
@@ -884,6 +911,9 @@
                 BeaconCtx.SymbolTimeout = BeaconCtx.Cfg.SymbolToDefault;
                 BeaconState = BEACON_STATE_LOCKED;
 
+                BeaconCtx.beaconLen = size;
+				memcpy1( BeaconCtx.beaconPayload, payload, size );
+
                 LoRaMacClassBBeaconTimerEvent( );
             }
 
@@ -1296,12 +1326,14 @@
             // We missed the beacon already
             BeaconCtx.BeaconTimingDelay = 0;
             BeaconCtx.BeaconTimingChannel = 0;
+            BeaconCtx.BeaconTimingAnsTime = 0;
             LoRaMacClassBParams.MlmeConfirmQueue[index].Status = LORAMAC_EVENT_INFO_STATUS_BEACON_NOT_FOUND;
         }
         else
         {
             BeaconCtx.Ctrl.BeaconDelaySet = 1;
             BeaconCtx.Ctrl.BeaconChannelSet = 1;
+            BeaconCtx.BeaconTimingAnsTime = currentTime;
             BeaconCtx.NextBeaconRx = currentTime + BeaconCtx.BeaconTimingDelay;
             LoRaMacClassBParams.MlmeConfirmQueue[index].Status = LORAMAC_EVENT_INFO_STATUS_OK;
         }
--- a/LoRaMacClassB.h	Thu Sep 21 17:01:33 2017 -0400
+++ b/LoRaMacClassB.h	Thu Sep 21 17:02:39 2017 -0400
@@ -213,6 +213,7 @@
          * Set if the beacon state machine will be resumed
          */
         uint8_t ResumeBeaconing      : 1;
+
     }Ctrl;
 
     struct sBeaconCfg
@@ -280,6 +281,7 @@
      * Time when the next beacon will be received
      */
     TimerTime_t NextBeaconRx;
+
     /*!
      * Current symbol timeout. The node enlarges this variable in case of beacon
      * loss.
@@ -298,6 +300,9 @@
      */
     TimerTime_t BeaconTimingDelay;
 
+    TimerTime_t BeaconTimingAnsTime;
+    TimerTime_t BeaconAcqTime;
+
     struct sBeaconRxError
     {
     	uint16_t    count;
@@ -311,6 +316,9 @@
     	sBeaconRxError() : count(0), crc0(0), crc1(0), len(0) {}
     } BeaconRxError;
 
+    uint8_t        beaconPayload[32];
+    uint8_t        beaconLen;
+
 }BeaconContext_t;
 
 /*!