Addressed feedback on CSMA implementation

This commit is contained in:
Amalinda 2023-10-29 09:49:59 +08:00
parent da57f1cdef
commit d5c80b0163
2 changed files with 35 additions and 10 deletions

View file

@ -37,6 +37,10 @@ LoRaWANNode::LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band) {
this->startChannel = -1; this->startChannel = -1;
this->numChannels = -1; this->numChannels = -1;
this->backupFreq = this->band->backupChannel.freqStart; this->backupFreq = this->band->backupChannel.freqStart;
this->difsSlots = 2;
this->backoffMax = 6;
this->enableCSMA = false;
} }
void LoRaWANNode::wipe() { void LoRaWANNode::wipe() {
@ -44,6 +48,13 @@ void LoRaWANNode::wipe() {
mod->hal->wipePersistentStorage(); mod->hal->wipePersistentStorage();
} }
void LoRaWANNode::setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA) {
this->backoffMax = backoffMax;
this->difsSlots = difsSlots;
this->enableCSMA = enableCSMA;
}
int16_t LoRaWANNode::restoreOTAA() { int16_t LoRaWANNode::restoreOTAA() {
int16_t state = this->setPhyProperties(); int16_t state = this->setPhyProperties();
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
@ -638,7 +649,7 @@ int16_t LoRaWANNode::uplink(uint8_t* data, size_t len, uint8_t port) {
} }
// perform CSMA if enabled. // perform CSMA if enabled.
if (CSMA_ENABLE) { if (enableCSMA) {
performCSMA(); performCSMA();
} }
@ -1767,19 +1778,20 @@ void LoRaWANNode::hton(uint8_t* buff, T val, size_t size) {
} }
} }
// The following function enables LMAC, a CSMA scheme for LoRa as specified in the LoRa Alliance Technical Reccomendation #13. // The following function enables LMAC, a CSMA scheme for LoRa as specified
// in the LoRa Alliance Technical Recommendation #13.
// A user may enable CSMA to provide frames an additional layer of protection from interference. // A user may enable CSMA to provide frames an additional layer of protection from interference.
// https://resources.lora-alliance.org/technical-recommendations/tr013-1-0-0-csma // https://resources.lora-alliance.org/technical-recommendations/tr013-1-0-0-csma
void LoRaWANNode::performCSMA() { void LoRaWANNode::performCSMA() {
// Compute initial random back-off. // Compute initial random back-off.
// When BO is reduced to zero, the function returns and the frame is transmitted. // When BO is reduced to zero, the function returns and the frame is transmitted.
uint32_t BO = random(1, BO_MAX + 1); uint32_t BO = this->phyLayer->random(1, this->backoffMax + 1);
while (BO > 0) { while (BO > 0) {
// DIFS: Check channel for DIFS_slots // DIFS: Check channel for DIFS_slots
bool channelFreeDuringDIFS = true; bool channelFreeDuringDIFS = true;
for (uint8_t i = 0; i < DIFS_SLOTS; i++) { for (uint8_t i = 0; i < this->difsSlots; i++) {
if (performCAD()) { if (performCAD()) {
RADIOLIB_DEBUG_PRINTLN("OCCUPIED CHANNEL DURING DIFS"); RADIOLIB_DEBUG_PRINTLN("OCCUPIED CHANNEL DURING DIFS");
channelFreeDuringDIFS = false; channelFreeDuringDIFS = false;
@ -1805,7 +1817,6 @@ void LoRaWANNode::performCSMA() {
} }
} }
bool LoRaWANNode::performCAD() { bool LoRaWANNode::performCAD() {
int16_t state = this->phyLayer->scanChannel(); int16_t state = this->phyLayer->scanChannel();

View file

@ -176,11 +176,6 @@
// the maximum number of simultaneously available channels // the maximum number of simultaneously available channels
#define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (8) #define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (8)
// CSMA definitions
#define DIFS_SLOTS (2) // Number of CADs to estimate a clear channel.
#define BO_MAX (6) // Number of maximum CADs to back-off. Set to 0 to disable.
#define CSMA_ENABLE (true) // Enables/disables CSMA functionality.
/*! /*!
\struct LoRaWANChannelSpan_t \struct LoRaWANChannelSpan_t
\brief Structure to save information about LoRaWAN channels. \brief Structure to save information about LoRaWAN channels.
@ -301,6 +296,17 @@ class LoRaWANNode {
(e.g. 8 for US915 FSB2 used by TTN). By default -1 (no channel offset). */ (e.g. 8 for US915 FSB2 used by TTN). By default -1 (no channel offset). */
int8_t numChannels; int8_t numChannels;
/*! \brief Num of Back Off(BO) slots to be decremented after DIFS phase. 0 to disable BO.
A random BO avoids collisions in the case where two or more nodes start the CSMA
process at the same time. */
uint8_t backoffMax;
/*! \brief Num of CADs to estimate a clear CH. */
uint8_t difsSlots;
/*! \brief enable/disable CSMA for LoRaWAN. */
bool enableCSMA;
/*! /*!
\brief Default constructor. \brief Default constructor.
\param phy Pointer to the PhysicalLayer radio module. \param phy Pointer to the PhysicalLayer radio module.
@ -314,6 +320,14 @@ class LoRaWANNode {
*/ */
void wipe(); void wipe();
/*!
\brief Configures CSMA for LoRaWAN as per TR-13, LoRa Alliance.
\param backoffMax Num of BO slots to be decremented after DIFS phase. 0 to disable BO.
\param difsSlots Num of CADs to estimate a clear CH.
\param enableCSMA enable/disable CSMA for LoRaWAN.
*/
void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA = false);
/*! /*!
\brief Restore OTAA session by loading information from persistent storage. \brief Restore OTAA session by loading information from persistent storage.
\returns \ref status_codes \returns \ref status_codes