Skip to content

Commit c109048

Browse files
committed
Fixed ABP join handling to be similar to OTAA join handling
1 parent 2f0df87 commit c109048

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

src/mac/LoRaMac.c

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,25 @@
7878
*/
7979
#define ADR_ACK_COUNTER_MAX 0xFFFFFFFF
8080

81+
/*!
82+
* Delay required to simulate an ABP join like an OTAA join
83+
*/
84+
#define ABP_JOIN_PENDING_DELAY_MS 10
85+
8186
/*!
8287
* LoRaMac internal states
8388
*/
8489
enum eLoRaMacState
8590
{
86-
LORAMAC_IDLE = 0x00000000,
87-
LORAMAC_STOPPED = 0x00000001,
88-
LORAMAC_TX_RUNNING = 0x00000002,
89-
LORAMAC_RX = 0x00000004,
90-
LORAMAC_ACK_RETRY = 0x00000010,
91-
LORAMAC_TX_DELAYED = 0x00000020,
92-
LORAMAC_TX_CONFIG = 0x00000040,
93-
LORAMAC_RX_ABORT = 0x00000080,
91+
LORAMAC_IDLE = 0x00000000,
92+
LORAMAC_STOPPED = 0x00000001,
93+
LORAMAC_TX_RUNNING = 0x00000002,
94+
LORAMAC_RX = 0x00000004,
95+
LORAMAC_ACK_RETRY = 0x00000010,
96+
LORAMAC_TX_DELAYED = 0x00000020,
97+
LORAMAC_TX_CONFIG = 0x00000040,
98+
LORAMAC_RX_ABORT = 0x00000080,
99+
LORAMAC_ABP_JOIN_PENDING = 0x00000100,
94100
};
95101

96102
/*
@@ -252,6 +258,10 @@ typedef struct sLoRaMacCtx
252258
* Start time of the response timeout
253259
*/
254260
TimerTime_t ResponseTimeoutStartTime;
261+
/*
262+
* Timer required to simulate an ABP join like an OTAA join
263+
*/
264+
TimerEvent_t AbpJoinPendingTimer;
255265
/*
256266
* Buffer containing the MAC layer commands
257267
*/
@@ -5198,10 +5208,41 @@ LoRaMacStatus_t LoRaMacMcChannelSetupRxParams( AddressIdentifier_t groupID, McRx
51985208
return LORAMAC_STATUS_OK;
51995209
}
52005210

5211+
/*!
5212+
* \brief Function executed on AbpJoinPendingTimer timer event
5213+
*/
5214+
static void OnAbpJoinPendingTimerEvent( void *context )
5215+
{
5216+
MacCtx.MacState &= ~LORAMAC_ABP_JOIN_PENDING;
5217+
MacCtx.MacFlags.Bits.MacDone = 1;
5218+
OnMacProcessNotify( );
5219+
}
5220+
5221+
/*!
5222+
* \brief Start ABP join simulation
5223+
*/
5224+
static void AbpJoinPendingStart( void )
5225+
{
5226+
static bool initialized = false;
5227+
5228+
if( initialized == false )
5229+
{
5230+
initialized = true;
5231+
TimerInit( &MacCtx.AbpJoinPendingTimer, OnAbpJoinPendingTimerEvent );
5232+
}
5233+
5234+
MacCtx.MacState |= LORAMAC_ABP_JOIN_PENDING;
5235+
5236+
TimerStop( &MacCtx.AbpJoinPendingTimer );
5237+
TimerSetValue( &MacCtx.AbpJoinPendingTimer, ABP_JOIN_PENDING_DELAY_MS );
5238+
TimerStart( &MacCtx.AbpJoinPendingTimer );
5239+
}
5240+
52015241
LoRaMacStatus_t LoRaMacMlmeRequest( MlmeReq_t* mlmeRequest )
52025242
{
52035243
LoRaMacStatus_t status = LORAMAC_STATUS_SERVICE_UNKNOWN;
52045244
MlmeConfirmQueue_t queueElement;
5245+
bool isAbpJoinPending = false;
52055246
uint8_t macCmdPayload[2] = { 0x00, 0x00 };
52065247

52075248
if( mlmeRequest == NULL )
@@ -5246,7 +5287,7 @@ LoRaMacStatus_t LoRaMacMlmeRequest( MlmeReq_t* mlmeRequest )
52465287
{
52475288
ResetMacParameters( false );
52485289

5249-
Nvm.MacGroup1.ChannelsDatarate = RegionAlternateDr( Nvm.MacGroup2.Region, mlmeRequest->Req.Join.Datarate, ALTERNATE_DR );
5290+
Nvm.MacGroup1.ChannelsDatarate = RegionAlternateDr( Nvm.MacGroup2.Region, mlmeRequest->Req.Join.Datarate, ALTERNATE_DR );
52505291

52515292
queueElement.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL;
52525293

@@ -5271,8 +5312,7 @@ LoRaMacStatus_t LoRaMacMlmeRequest( MlmeReq_t* mlmeRequest )
52715312
Nvm.MacGroup2.NetworkActivation = mlmeRequest->Req.Join.NetworkActivation;
52725313
queueElement.Status = LORAMAC_EVENT_INFO_STATUS_OK;
52735314
queueElement.ReadyToHandle = true;
5274-
OnMacProcessNotify( );
5275-
MacCtx.MacFlags.Bits.MacDone = 1;
5315+
isAbpJoinPending = true;
52765316
status = LORAMAC_STATUS_OK;
52775317
}
52785318
break;
@@ -5393,6 +5433,10 @@ LoRaMacStatus_t LoRaMacMlmeRequest( MlmeReq_t* mlmeRequest )
53935433
else
53945434
{
53955435
LoRaMacConfirmQueueAdd( &queueElement );
5436+
if( isAbpJoinPending == true )
5437+
{
5438+
AbpJoinPendingStart( );
5439+
}
53965440
}
53975441
return status;
53985442
}

0 commit comments

Comments
 (0)