Skip to content

Commit a100ab0

Browse files
author
Hasnain Virk
committed
[IOTCELL-289] Adding Mlme class handle
MAC layer will services will be broken down into independent subsystems. This is the first of those efforts. We have introduced LoRaMacMlme class that handles everything related to MLME subsystem or subservice. To accomodate subsystems we have grouped all protocol level variables into one big data structure. A pointer to that data structure will be passed around the subsystems in order to regulate the entire system. LoRaMac::Send() and LoRaMac::SetTxContWave*() APIs are made public as they are needed to be accessed by the subsystems.
1 parent a3106d2 commit a100ab0

File tree

7 files changed

+556
-375
lines changed

7 files changed

+556
-375
lines changed

features/lorawan/lorastack/mac/LoRaMac.cpp

Lines changed: 196 additions & 290 deletions
Large diffs are not rendered by default.

features/lorawan/lorastack/mac/LoRaMac.h

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@
4444
#include "netsocket/LoRaRadio.h"
4545
#include "lorastack/phy/LoRaPHY.h"
4646
#include "lorawan/system/lorawan_data_structures.h"
47-
#include "LoRaMacCommand.h"
47+
#include "lorastack/mac/LoRaMacCommand.h"
4848
#include "events/EventQueue.h"
49-
50-
49+
#include "lorastack/mac/LoRaMacMlme.h"
5150
/*!
5251
* Maximum PHY layer payload size
5352
*/
@@ -332,6 +331,44 @@ class LoRaMac
332331
*/
333332
void SetMlmeScheduleUplinkIndication( void );
334333

334+
/*!
335+
* \brief LoRaMAC layer generic send frame
336+
*
337+
* \param [IN] macHdr MAC header field
338+
* \param [IN] fPort MAC payload port
339+
* \param [IN] fBuffer MAC data buffer to be sent
340+
* \param [IN] fBufferSize MAC data buffer size
341+
* \retval status Status of the operation.
342+
*/
343+
LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
344+
345+
/*!
346+
* \brief Sets the radio in continuous transmission mode
347+
*
348+
* \remark Uses the radio parameters set on the previous transmission.
349+
*
350+
* \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
351+
* \retval status Status of the operation.
352+
*/
353+
LoRaMacStatus_t SetTxContinuousWave( uint16_t timeout );
354+
355+
/*!
356+
* \brief Sets the radio in continuous transmission mode
357+
*
358+
* \remark Uses the radio parameters set on the previous transmission.
359+
*
360+
* \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
361+
* \param [IN] frequency RF frequency to be set.
362+
* \param [IN] power RF output power to be set.
363+
* \retval status Status of the operation.
364+
*/
365+
LoRaMacStatus_t SetTxContinuousWave1( uint16_t timeout, uint32_t frequency, uint8_t power );
366+
367+
/*!
368+
* \brief Resets MAC specific parameters to default
369+
*/
370+
void ResetMacParameters( void );
371+
335372
#if defined(LORAWAN_COMPLIANCE_TEST)
336373
public: // Test interface
337374

@@ -494,17 +531,6 @@ class LoRaMac
494531
*/
495532
bool ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen );
496533

497-
/*!
498-
* \brief LoRaMAC layer generic send frame
499-
*
500-
* \param [IN] macHdr MAC header field
501-
* \param [IN] fPort MAC payload port
502-
* \param [IN] fBuffer MAC data buffer to be sent
503-
* \param [IN] fBufferSize MAC data buffer size
504-
* \retval status Status of the operation.
505-
*/
506-
LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
507-
508534
/*!
509535
* \brief LoRaMAC layer frame buffer initialization
510536
*
@@ -543,33 +569,6 @@ class LoRaMac
543569
*/
544570
LoRaMacStatus_t SendFrameOnChannel( uint8_t channel );
545571

546-
/*!
547-
* \brief Sets the radio in continuous transmission mode
548-
*
549-
* \remark Uses the radio parameters set on the previous transmission.
550-
*
551-
* \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
552-
* \retval status Status of the operation.
553-
*/
554-
LoRaMacStatus_t SetTxContinuousWave( uint16_t timeout );
555-
556-
/*!
557-
* \brief Sets the radio in continuous transmission mode
558-
*
559-
* \remark Uses the radio parameters set on the previous transmission.
560-
*
561-
* \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
562-
* \param [IN] frequency RF frequency to be set.
563-
* \param [IN] power RF output power to be set.
564-
* \retval status Status of the operation.
565-
*/
566-
LoRaMacStatus_t SetTxContinuousWave1( uint16_t timeout, uint32_t frequency, uint8_t power );
567-
568-
/*!
569-
* \brief Resets MAC specific parameters to default
570-
*/
571-
void ResetMacParameters( void );
572-
573572
/*!
574573
* \brief Resets MAC specific parameters to default
575574
*
@@ -609,10 +608,24 @@ class LoRaMac
609608
*/
610609
LoRaPHY *lora_phy;
611610

611+
/**
612+
* MAC command handle
613+
*/
612614
LoRaMacCommand mac_commands;
613615

616+
/**
617+
* MLME subsystem handle
618+
*/
619+
LoRaMacMlme mlme;
620+
621+
/**
622+
* Timer subsystem handle
623+
*/
614624
LoRaWANTimeHandler &_lora_time;
615625

626+
/**
627+
* Central MAC layer data storage
628+
*/
616629
lora_mac_protocol_params _params;
617630

618631
/**
@@ -625,17 +638,6 @@ class LoRaMac
625638
*/
626639
MulticastParams_t *MulticastChannels;
627640

628-
629-
/*!
630-
* LoRaMac parameters
631-
*/
632-
LoRaMacParams_t LoRaMacParams;
633-
634-
/*!
635-
* LoRaMac default parameters
636-
*/
637-
LoRaMacParams_t LoRaMacParamsDefaults;
638-
639641
/*!
640642
* LoRaMac upper layer event functions
641643
*/
@@ -646,23 +648,6 @@ class LoRaMac
646648
*/
647649
LoRaMacCallback_t *LoRaMacCallbacks;
648650

649-
650-
651-
/*!
652-
* LoRaMac reception windows delay
653-
* \remark normal frame: RxWindowXDelay = ReceiveDelayX - RADIO_WAKEUP_TIME
654-
* join frame : RxWindowXDelay = JoinAcceptDelayX - RADIO_WAKEUP_TIME
655-
*/
656-
uint32_t RxWindow1Delay;
657-
uint32_t RxWindow2Delay;
658-
659-
/*!
660-
* LoRaMac Rx windows configuration
661-
*/
662-
RxConfigParams_t RxWindow1Config;
663-
RxConfigParams_t RxWindow2Config;
664-
665-
666651
/*!
667652
* Structure to hold MCPS indication data.
668653
*/
@@ -674,19 +659,10 @@ class LoRaMac
674659
McpsConfirm_t McpsConfirm;
675660

676661
/*!
677-
* Structure to hold MLME indication data.
678-
*/
679-
MlmeIndication_t MlmeIndication;
680-
681-
/*!
682-
* Structure to hold MLME confirm data.
662+
* Receive Window configurations for PHY layer
683663
*/
684-
MlmeConfirm_t MlmeConfirm;
685-
686-
/*!
687-
* Holds the current rx window slot
688-
*/
689-
LoRaMacRxSlot_t RxSlot;
664+
RxConfigParams_t RxWindow1Config;
665+
RxConfigParams_t RxWindow2Config;
690666

691667
};
692668

features/lorawan/lorastack/mac/LoRaMacCommand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ bool LoRaMacCommand::IsMacCommandsInNextTx() const
252252
}
253253

254254
void LoRaMacCommand::ProcessMacCommands(uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr,
255-
MlmeConfirm_t &MlmeConfirm, LoRaMacCallback_t *LoRaMacCallbacks,
256-
LoRaMacParams_t &LoRaMacParams, LoRaPHY &lora_phy)
255+
MlmeConfirm_t& MlmeConfirm, LoRaMacCallback_t *LoRaMacCallbacks,
256+
lora_mac_system_params_t &LoRaMacParams, LoRaPHY &lora_phy)
257257
{
258258
uint8_t status = 0;
259259

features/lorawan/lorastack/mac/LoRaMacCommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class LoRaMacCommand
134134
*/
135135
void ProcessMacCommands(uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr,
136136
MlmeConfirm_t &MlmeConfirm, LoRaMacCallback_t *LoRaMacCallbacks,
137-
LoRaMacParams_t &LoRaMacParams, LoRaPHY &lora_phy);
137+
lora_mac_system_params_t &LoRaMacParams, LoRaPHY &lora_phy);
138138

139139
/*!
140140
* \brief Verifies if sticky MAC commands are pending.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
/ _____) _ | |
3+
( (____ _____ ____ _| |_ _____ ____| |__
4+
\____ \| ___ | (_ _) ___ |/ ___) _ \
5+
_____) ) ____| | | || |_| ____( (___| | | |
6+
(______/|_____)_|_|_| \__)_____)\____)_| |_|
7+
(C)2013 Semtech
8+
___ _____ _ ___ _ _____ ___ ___ ___ ___
9+
/ __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
10+
\__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
11+
|___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
12+
embedded.connectivity.solutions===============
13+
14+
Description: LoRaWAN stack layer that controls both MAC and PHY underneath
15+
16+
License: Revised BSD License, see LICENSE.TXT file include in the project
17+
18+
Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
19+
20+
21+
Copyright (c) 2017, Arm Limited and affiliates.
22+
23+
SPDX-License-Identifier: BSD-3-Clause
24+
*/
25+
26+
#include "LoRaMac.h"
27+
#include "lorastack/mac/LoRaMacMlme.h"
28+
29+
LoRaMacMlme::LoRaMacMlme()
30+
: _lora_mac(NULL), _lora_phy(NULL), _mac_cmd(NULL)
31+
{
32+
}
33+
34+
LoRaMacMlme::~LoRaMacMlme()
35+
{
36+
}
37+
38+
void LoRaMacMlme::activate_mlme_subsystem(LoRaMac *mac, LoRaPHY *phy,
39+
LoRaMacCommand *cmd)
40+
{
41+
_lora_mac = mac;
42+
_lora_phy = phy;
43+
_mac_cmd = cmd;
44+
}
45+
46+
LoRaMacStatus_t LoRaMacMlme::set_request(MlmeReq_t *mlmeRequest,
47+
lora_mac_protocol_params *params)
48+
{
49+
if (mlmeRequest && params && _lora_mac && _lora_phy && _mac_cmd) {
50+
51+
LoRaMacStatus_t status = LORAMAC_STATUS_SERVICE_UNKNOWN;
52+
LoRaMacHeader_t macHdr;
53+
AlternateDrParams_t altDr;
54+
VerifyParams_t verify;
55+
GetPhyParams_t getPhy;
56+
PhyParam_t phyParam;
57+
58+
59+
if (params->LoRaMacState != LORAMAC_IDLE) {
60+
return LORAMAC_STATUS_BUSY;
61+
}
62+
63+
// Before setting a new MLME request, clear the MLME confirmation
64+
// structure
65+
memset((uint8_t*) &confirmation, 0, sizeof(confirmation));
66+
67+
confirmation.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
68+
69+
switch (mlmeRequest->Type) {
70+
case MLME_JOIN: {
71+
if ((params->LoRaMacState & LORAMAC_TX_DELAYED)
72+
== LORAMAC_TX_DELAYED) {
73+
return LORAMAC_STATUS_BUSY;
74+
}
75+
76+
if ((mlmeRequest->Req.Join.DevEui == NULL)
77+
|| (mlmeRequest->Req.Join.AppEui == NULL)
78+
|| (mlmeRequest->Req.Join.AppKey == NULL)
79+
|| (mlmeRequest->Req.Join.NbTrials == 0)) {
80+
return LORAMAC_STATUS_PARAMETER_INVALID;
81+
}
82+
83+
// Verify the parameter NbTrials for the join procedure
84+
verify.NbJoinTrials = mlmeRequest->Req.Join.NbTrials;
85+
86+
if (_lora_phy->verify(&verify, PHY_NB_JOIN_TRIALS) == false) {
87+
// Value not supported, get default
88+
getPhy.Attribute = PHY_DEF_NB_JOIN_TRIALS;
89+
phyParam = _lora_phy->get_phy_params(&getPhy);
90+
mlmeRequest->Req.Join.NbTrials = (uint8_t) phyParam.Value;
91+
}
92+
93+
params->LoRaMacFlags.Bits.MlmeReq = 1;
94+
confirmation.MlmeRequest = mlmeRequest->Type;
95+
96+
params->keys.LoRaMacDevEui = mlmeRequest->Req.Join.DevEui;
97+
params->keys.LoRaMacAppEui = mlmeRequest->Req.Join.AppEui;
98+
params->keys.LoRaMacAppKey = mlmeRequest->Req.Join.AppKey;
99+
params->MaxJoinRequestTrials = mlmeRequest->Req.Join.NbTrials;
100+
101+
// Reset variable JoinRequestTrials
102+
params->JoinRequestTrials = 0;
103+
104+
// Setup header information
105+
macHdr.Value = 0;
106+
macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ;
107+
108+
_lora_mac->ResetMacParameters();
109+
110+
altDr.NbTrials = params->JoinRequestTrials + 1;
111+
112+
params->sys_params.ChannelsDatarate =
113+
_lora_phy->get_alternate_DR(&altDr);
114+
115+
status = _lora_mac->Send(&macHdr, 0, NULL, 0);
116+
break;
117+
}
118+
case MLME_LINK_CHECK: {
119+
params->LoRaMacFlags.Bits.MlmeReq = 1;
120+
// LoRaMac will send this command piggy-backed
121+
confirmation.MlmeRequest = mlmeRequest->Type;
122+
123+
status = _mac_cmd->AddMacCommand(MOTE_MAC_LINK_CHECK_REQ, 0, 0);
124+
break;
125+
}
126+
case MLME_TXCW: {
127+
confirmation.MlmeRequest = mlmeRequest->Type;
128+
params->LoRaMacFlags.Bits.MlmeReq = 1;
129+
status = _lora_mac->SetTxContinuousWave(mlmeRequest->Req.TxCw.Timeout);
130+
break;
131+
}
132+
case MLME_TXCW_1: {
133+
confirmation.MlmeRequest = mlmeRequest->Type;
134+
params->LoRaMacFlags.Bits.MlmeReq = 1;
135+
status = _lora_mac->SetTxContinuousWave1(mlmeRequest->Req.TxCw.Timeout,
136+
mlmeRequest->Req.TxCw.Frequency,
137+
mlmeRequest->Req.TxCw.Power);
138+
break;
139+
}
140+
default:
141+
break;
142+
}
143+
144+
if (status != LORAMAC_STATUS_OK) {
145+
params->NodeAckRequested = false;
146+
params->LoRaMacFlags.Bits.MlmeReq = 0;
147+
}
148+
149+
return status;
150+
}
151+
152+
return LORAMAC_STATUS_PARAMETER_INVALID;
153+
}
154+
155+
MlmeIndication_t& LoRaMacMlme::get_indication()
156+
{
157+
return indication;
158+
}
159+
160+
MlmeConfirm_t& LoRaMacMlme::get_confirmation()
161+
{
162+
return confirmation;
163+
}

0 commit comments

Comments
 (0)