From 9b2507d71aa89b544a15ac072fcb845fa4d7ec60 Mon Sep 17 00:00:00 2001 From: Hasnain Virk Date: Tue, 21 Aug 2018 16:13:30 +0300 Subject: [PATCH] Adding helpers for Channel mask manipulation A few protected member functions are introduced in LoRaPHY class that help manipulate channel masks in various ways. --- features/lorawan/lorastack/phy/LoRaPHY.cpp | 39 +++++++++++++++++++ features/lorawan/lorastack/phy/LoRaPHY.h | 20 ++++++++++ .../lorawan/lorastack/phy/LoRaPHYAU915.cpp | 33 ++++++---------- .../lorawan/lorastack/phy/LoRaPHYUS915.cpp | 37 +++++++----------- 4 files changed, 84 insertions(+), 45 deletions(-) diff --git a/features/lorawan/lorastack/phy/LoRaPHY.cpp b/features/lorawan/lorastack/phy/LoRaPHY.cpp index b6131db844..7a8462fa8e 100644 --- a/features/lorawan/lorastack/phy/LoRaPHY.cpp +++ b/features/lorawan/lorastack/phy/LoRaPHY.cpp @@ -248,6 +248,45 @@ void LoRaPHY::copy_channel_mask(uint16_t *dest_mask, uint16_t *src_mask, uint8_t } } +void LoRaPHY::intersect_channel_mask(const uint16_t *source, + uint16_t *destination, uint8_t size) +{ + if (!source || !destination || size == 0) { + return; + } + + for (uint8_t i = 0; i < size; i++) { + destination[i] &= source[i]; + } +} + +void LoRaPHY::fill_channel_mask_with_fsb(const uint16_t *expectation, + const uint16_t *fsb_mask, + uint16_t *destination, + uint8_t size) +{ + if (!expectation || !fsb_mask || !destination || size == 0) { + return; + } + + for (uint8_t i = 0; i < size; i++) { + destination[i] = expectation[i] & fsb_mask[i]; + } + +} + +void LoRaPHY::fill_channel_mask_with_value(uint16_t *channel_mask, + uint16_t value, uint8_t size) +{ + if (!channel_mask || size == 0) { + return; + } + + for (uint8_t i = 0; i < size; i++) { + channel_mask[i] = value; + } +} + void LoRaPHY::set_last_tx_done(uint8_t channel, bool joined, lorawan_time_t last_tx_done_time) { band_t *band_table = (band_t *) phy_params.bands.table; diff --git a/features/lorawan/lorastack/phy/LoRaPHY.h b/features/lorawan/lorastack/phy/LoRaPHY.h index 21885298e1..5b4ed32b8b 100644 --- a/features/lorawan/lorastack/phy/LoRaPHY.h +++ b/features/lorawan/lorastack/phy/LoRaPHY.h @@ -535,6 +535,26 @@ public: //Verifiers protected: LoRaPHY(); + /** + * Sets the intersection of source and destination channel masks + * into the destination. + */ + void intersect_channel_mask(const uint16_t *source, uint16_t *destination, + uint8_t size); + + /** + * Fills channel mask array based upon the provided FSB mask + */ + void fill_channel_mask_with_fsb(const uint16_t *expectation, + const uint16_t *fsb_mask, + uint16_t *channel_mask, uint8_t size); + + /** + * Fills channel mask array with a given value + */ + void fill_channel_mask_with_value(uint16_t *channel_mask, + uint16_t value, uint8_t size); + /** * Looks up corresponding band for a frequency. Returns -1 if not in any band. */ diff --git a/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp b/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp index 938075172e..357d382844 100644 --- a/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp +++ b/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp @@ -222,6 +222,8 @@ static const uint8_t max_payload_with_repeater_AU915[] = { 51, 51, 51, 115, static const uint16_t fsb_mask[] = MBED_CONF_LORA_FSB_MASK; +static const uint16_t full_channel_mask [] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}; + LoRaPHYAU915::LoRaPHYAU915() { bands[0] = AU915_BAND0; @@ -244,18 +246,10 @@ LoRaPHYAU915::LoRaPHYAU915() // Initialize channels default mask // All channels are default channels here // Join request needs to alternate between 125 KHz and 500 KHz channels - // randomly. - // ChannelsMask - for (uint8_t i = 0; i < AU915_MAX_NB_CHANNELS; i++) { - if (i == (AU915_MAX_NB_CHANNELS - 1)) { - // 64 - 71, 500 kHz channels will get enabled - default_channel_mask[i] = 0x00FF & fsb_mask[i]; - continue; - } - - // 0 - 63 125 kHz channels will get enabled - default_channel_mask[i] = 0xFFFF & fsb_mask[i]; - } + // randomly. Fill in the default channel mask depending upon the given + // fsb_mask + fill_channel_mask_with_fsb(full_channel_mask, fsb_mask, + default_channel_mask, AU915_CHANNEL_MASK_SIZE); memset(channel_mask, 0, sizeof(channel_mask)); memset(current_channel_mask, 0, sizeof(current_channel_mask)); @@ -452,17 +446,15 @@ uint8_t LoRaPHYAU915::link_ADR_request(adr_req_params_t* params, if (adr_settings.ch_mask_ctrl == 6) { // Enable all 125 kHz channels - for (uint8_t i = 0; i < AU915_CHANNEL_MASK_SIZE - 1; i++) { - temp_channel_masks[i] = 0xFFFF; - } + fill_channel_mask_with_value(temp_channel_masks, 0xFFFF, + AU915_CHANNEL_MASK_SIZE - 1); // Apply chMask to channels 64 to 71 temp_channel_masks[4] = adr_settings.channel_mask; } else if (adr_settings.ch_mask_ctrl == 7) { // Disable all 125 kHz channels - for (uint8_t i = 0; i < AU915_CHANNEL_MASK_SIZE - 1; i++) { - temp_channel_masks[i] = 0x0000; - } + fill_channel_mask_with_value(temp_channel_masks, 0x0000, + AU915_CHANNEL_MASK_SIZE - 1); // Apply chMask to channels 64 to 71 temp_channel_masks[4] = adr_settings.channel_mask; @@ -500,9 +492,8 @@ uint8_t LoRaPHYAU915::link_ADR_request(adr_req_params_t* params, // Copy Mask copy_channel_mask(channel_mask, temp_channel_masks, AU915_CHANNEL_MASK_SIZE); - for (uint8_t i = 0; i < AU915_CHANNEL_MASK_SIZE; i++) { - current_channel_mask[i] &= channel_mask[i]; - } + intersect_channel_mask(channel_mask, current_channel_mask, + AU915_CHANNEL_MASK_SIZE); } // Update status variables diff --git a/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp b/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp index 1864bc71c4..a531de055b 100644 --- a/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp +++ b/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp @@ -212,6 +212,7 @@ static const uint8_t max_payloads_US915[] = {11, 53, 125, 242, 242, 0, 0, 0, 53, static const uint8_t max_payloads_with_repeater_US915[] = {11, 53, 125, 242, 242, 0, 0, 0, 33, 109, 222, 222, 222, 222, 0, 0}; static const uint16_t fsb_mask[] = MBED_CONF_LORA_FSB_MASK; +static const uint16_t full_channel_mask [] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}; LoRaPHYUS915::LoRaPHYUS915() { @@ -231,17 +232,9 @@ LoRaPHYUS915::LoRaPHYUS915() channels[i].band = 0; } - // ChannelsMask - for (uint8_t i = 0; i < US915_MAX_NB_CHANNELS; i++) { - if (i == (US915_MAX_NB_CHANNELS - 1)) { - // 64 - 71, 500 kHz channels will get enabled - default_channel_mask[i] = 0x00FF & fsb_mask[i]; - continue; - } - - // 0 - 63 125 kHz channels will get enabled - default_channel_mask[i] = 0xFFFF & fsb_mask[i]; - } + // Fill-up default channel mask and apply FSB mask too + fill_channel_mask_with_fsb(full_channel_mask, fsb_mask, + default_channel_mask, US915_CHANNEL_MASK_SIZE); memset(channel_mask, 0, sizeof(channel_mask)); memset(current_channel_mask, 0, sizeof(current_channel_mask)); @@ -353,10 +346,8 @@ void LoRaPHYUS915::restore_default_channels() // Copy channels default mask copy_channel_mask(channel_mask, default_channel_mask, US915_CHANNEL_MASK_SIZE); - for (uint8_t i = 0; i < US915_CHANNEL_MASK_SIZE; i++) { - // Copy-And the channels mask - current_channel_mask[i] &= channel_mask[i]; - } + // Update running channel mask + intersect_channel_mask(channel_mask, current_channel_mask, US915_CHANNEL_MASK_SIZE); } bool LoRaPHYUS915::rx_config(rx_config_params_t* config) @@ -487,9 +478,8 @@ uint8_t LoRaPHYUS915::link_ADR_request(adr_req_params_t* params, if (adr_settings.ch_mask_ctrl == 6) { // Enable all 125 kHz channels - for (uint8_t i = 0; i < US915_CHANNEL_MASK_SIZE - 1; i++) { - temp_channel_masks[i] = 0xFFFF; - } + fill_channel_mask_with_value(temp_channel_masks, 0xFFFF, + US915_CHANNEL_MASK_SIZE - 1); // Apply chMask to channels 64 to 71 temp_channel_masks[4] = adr_settings.channel_mask; @@ -497,9 +487,8 @@ uint8_t LoRaPHYUS915::link_ADR_request(adr_req_params_t* params, } else if (adr_settings.ch_mask_ctrl == 7) { // Disable all 125 kHz channels - for (uint8_t i = 0; i < US915_CHANNEL_MASK_SIZE - 1; i++) { - temp_channel_masks[i] = 0x0000; - } + fill_channel_mask_with_value(temp_channel_masks, 0x0000, + US915_CHANNEL_MASK_SIZE - 1); // Apply chMask to channels 64 to 71 temp_channel_masks[4] = adr_settings.channel_mask; @@ -540,9 +529,9 @@ uint8_t LoRaPHYUS915::link_ADR_request(adr_req_params_t* params, // Copy Mask copy_channel_mask(channel_mask, temp_channel_masks, US915_CHANNEL_MASK_SIZE); - for (uint8_t i = 0; i < US915_CHANNEL_MASK_SIZE; i++) { - current_channel_mask[i] &= channel_mask[i]; - } + // update running channel mask + intersect_channel_mask(channel_mask, current_channel_mask, + US915_CHANNEL_MASK_SIZE); } // Update status variables