Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaPHYAU915.cpp Source File

LoRaPHYAU915.cpp

Go to the documentation of this file.
00001 /**
00002  *  @file LoRaPHYAU915.cpp
00003  *
00004  *  @brief Implements LoRaPHY for Australian 915 MHz band
00005  *
00006  *  \code
00007  *   ______                              _
00008  *  / _____)             _              | |
00009  * ( (____  _____ ____ _| |_ _____  ____| |__
00010  *  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00011  *  _____) ) ____| | | || |_| ____( (___| | | |
00012  * (______/|_____)_|_|_| \__)_____)\____)_| |_|
00013  *   (C)2013 Semtech
00014  *  ___ _____ _   ___ _  _____ ___  ___  ___ ___
00015  * / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
00016  * \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
00017  * |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
00018  * embedded.connectivity.solutions===============
00019  *
00020  * \endcode
00021  *
00022  *
00023  * License: Revised BSD License, see LICENSE.TXT file include in the project
00024  *
00025  * Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
00026  *
00027  * Copyright (c) 2017, Arm Limited and affiliates.
00028  * SPDX-License-Identifier: BSD-3-Clause
00029  *
00030  */
00031 
00032 #include "LoRaPHYAU915.h"
00033 #include "lora_phy_ds.h"
00034 
00035 /*!
00036  * Minimal datarate that can be used by the node
00037  */
00038 #define AU915_TX_MIN_DATARATE                       DR_0
00039 
00040 /*!
00041  * Maximal datarate that can be used by the node
00042  */
00043 #define AU915_TX_MAX_DATARATE                       DR_6
00044 
00045 /*!
00046  * Minimal datarate that can be used by the node
00047  */
00048 #define AU915_RX_MIN_DATARATE                       DR_8
00049 
00050 /*!
00051  * Maximal datarate that can be used by the node
00052  */
00053 #define AU915_RX_MAX_DATARATE                       DR_13
00054 
00055 /*!
00056  * Default datarate used by the node
00057  */
00058 #define AU915_DEFAULT_DATARATE                      DR_0
00059 
00060 /*!
00061  * Minimal Rx1 receive datarate offset
00062  */
00063 #define AU915_MIN_RX1_DR_OFFSET                     0
00064 
00065 /*!
00066  * Maximal Rx1 receive datarate offset
00067  */
00068 #define AU915_MAX_RX1_DR_OFFSET                     6
00069 
00070 /*!
00071  * Default Rx1 receive datarate offset
00072  */
00073 #define AU915_DEFAULT_RX1_DR_OFFSET                 0
00074 
00075 /*!
00076  * Minimal Tx output power that can be used by the node
00077  */
00078 #define AU915_MIN_TX_POWER                          TX_POWER_10
00079 
00080 /*!
00081  * Maximal Tx output power that can be used by the node
00082  */
00083 #define AU915_MAX_TX_POWER                          TX_POWER_0
00084 
00085 /*!
00086  * Default Tx output power used by the node
00087  */
00088 #define AU915_DEFAULT_TX_POWER                      TX_POWER_0
00089 
00090 /*!
00091  * Default Max EIRP
00092  */
00093 #define AU915_DEFAULT_MAX_EIRP                      30.0f
00094 
00095 /*!
00096  * Default antenna gain
00097  */
00098 #define AU915_DEFAULT_ANTENNA_GAIN                  2.15f
00099 
00100 /*!
00101  * ADR Ack limit
00102  */
00103 #define AU915_ADR_ACK_LIMIT                         64
00104 
00105 /*!
00106  * ADR Ack delay
00107  */
00108 #define AU915_ADR_ACK_DELAY                         32
00109 
00110 /*!
00111  * Enabled or disabled the duty cycle
00112  */
00113 #define AU915_DUTY_CYCLE_ENABLED                    0
00114 
00115 /*!
00116  * Maximum RX window duration
00117  */
00118 #define AU915_MAX_RX_WINDOW                         3000
00119 
00120 /*!
00121  * Receive delay 1
00122  */
00123 #define AU915_RECEIVE_DELAY1                        1000
00124 
00125 /*!
00126  * Receive delay 2
00127  */
00128 #define AU915_RECEIVE_DELAY2                        2000
00129 
00130 /*!
00131  * Join accept delay 1
00132  */
00133 #define AU915_JOIN_ACCEPT_DELAY1                    5000
00134 
00135 /*!
00136  * Join accept delay 2
00137  */
00138 #define AU915_JOIN_ACCEPT_DELAY2                    6000
00139 
00140 /*!
00141  * Maximum frame counter gap
00142  */
00143 #define AU915_MAX_FCNT_GAP                          16384
00144 
00145 /*!
00146  * Ack timeout
00147  */
00148 #define AU915_ACKTIMEOUT                            2000
00149 
00150 /*!
00151  * Random ack timeout limits
00152  */
00153 #define AU915_ACK_TIMEOUT_RND                       1000
00154 
00155 /*!
00156  * Second reception window channel frequency definition.
00157  */
00158 #define AU915_RX_WND_2_FREQ                         923300000
00159 
00160 /*!
00161  * Second reception window channel datarate definition.
00162  */
00163 #define AU915_RX_WND_2_DR                           DR_8
00164 
00165 /*!
00166  * Band 0 definition
00167  * { DutyCycle, TxMaxPower, LastJoinTxDoneTime, LastTxDoneTime, TimeOff }
00168  */
00169 static const band_t  AU915_BAND0  = {1, AU915_MAX_TX_POWER, 0, 0, 0, 915200000, 927800000}; //  100.0 %
00170 
00171 /*!
00172  * Defines the first channel for RX window 1 for US band
00173  */
00174 #define AU915_FIRST_RX1_CHANNEL                     ((uint32_t) 923300000)
00175 
00176 /*!
00177  * Defines the last channel for RX window 1 for US band
00178  */
00179 #define AU915_LAST_RX1_CHANNEL                      ((uint32_t) 927500000)
00180 
00181 /*!
00182  * Defines the step width of the channels for RX window 1
00183  */
00184 #define AU915_STEPWIDTH_RX1_CHANNEL                 ((uint32_t) 600000)
00185 
00186 /*!
00187  * Data rates table definition
00188  */
00189 static const uint8_t datarates_AU915 [] = {12, 11, 10, 9, 8, 7, 8, 0, 12, 11, 10, 9, 8, 7, 0, 0};
00190 
00191 /*!
00192  * Bandwidths table definition in Hz
00193  */
00194 static const uint32_t bandwidths_AU915 [] = { 125000, 125000, 125000, 125000,
00195     125000, 125000, 500000, 0, 500000, 500000, 500000, 500000, 500000, 500000,
00196     0, 0 };
00197 
00198 /*!
00199  * Up/Down link data rates offset definition
00200  */
00201 static const int8_t datarate_offsets_AU915 [7][6] = { { DR_8, DR_8, DR_8, DR_8,
00202 DR_8, DR_8 }, // DR_0
00203     { DR_9, DR_8, DR_8, DR_8, DR_8, DR_8 }, // DR_1
00204     { DR_10, DR_9, DR_8, DR_8, DR_8, DR_8 }, // DR_2
00205     { DR_11, DR_10, DR_9, DR_8, DR_8, DR_8 }, // DR_3
00206     { DR_12, DR_11, DR_10, DR_9, DR_8, DR_8 }, // DR_4
00207     { DR_13, DR_12, DR_11, DR_10, DR_9, DR_8 }, // DR_5
00208     { DR_13, DR_13, DR_12, DR_11, DR_10, DR_9 }, // DR_6
00209         };
00210 
00211 /*!
00212  * Maximum payload with respect to the datarate index. Cannot operate with repeater.
00213  */
00214 static const uint8_t max_payload_AU915 [] = { 51, 51, 51, 115, 242, 242,
00215     242, 0, 53, 129, 242, 242, 242, 242, 0, 0 };
00216 
00217 /*!
00218  * Maximum payload with respect to the datarate index. Can operate with repeater.
00219  */
00220 static const uint8_t max_payload_with_repeater_AU915 [] = { 51, 51, 51, 115,
00221     222, 222, 222, 0, 33, 109, 222, 222, 222, 222, 0, 0 };
00222 
00223 static const uint16_t fsb_mask[] = MBED_CONF_LORA_FSB_MASK;
00224 
00225 static const uint16_t full_channel_mask [] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF};
00226 
00227 LoRaPHYAU915::LoRaPHYAU915()
00228 {
00229     bands[0] = AU915_BAND0 ;
00230 
00231     // Activate Channels
00232     // 125 kHz channels Upstream only
00233     for (uint8_t i = 0; i < AU915_MAX_NB_CHANNELS - 8; i++) {
00234         channels[i].frequency = 915200000 + i * 200000;
00235         channels[i].dr_range.value = ( DR_5 << 4) | DR_0;
00236         channels[i].band = 0;
00237     }
00238     // 500 kHz channels
00239     // Upstream and downstream both
00240     for (uint8_t i = AU915_MAX_NB_CHANNELS - 8; i < AU915_MAX_NB_CHANNELS; i++) {
00241         channels[i].frequency = 915900000 + (i - ( AU915_MAX_NB_CHANNELS - 8)) * 1600000;
00242         channels[i].dr_range.value = ( DR_6 << 4) | DR_6;
00243         channels[i].band = 0;
00244     }
00245 
00246     // Initialize channels default mask
00247     // All channels are default channels here
00248     // Join request needs to alternate between 125 KHz and 500 KHz channels
00249     // randomly. Fill in the default channel mask depending upon the given
00250     // fsb_mask
00251     fill_channel_mask_with_fsb(full_channel_mask, fsb_mask,
00252                                default_channel_mask, AU915_CHANNEL_MASK_SIZE);
00253 
00254     memset(channel_mask, 0, sizeof(channel_mask));
00255     memset(current_channel_mask, 0, sizeof(current_channel_mask));
00256 
00257     // Copy channels default mask
00258     copy_channel_mask(channel_mask, default_channel_mask, AU915_CHANNEL_MASK_SIZE);
00259 
00260     // Copy into current channels mask
00261     // This mask is used to keep track of the channels which were used in
00262     // previous transmissions as the AU915 band doesn't allow concurrent
00263     // transmission on the same channel
00264     copy_channel_mask(current_channel_mask, channel_mask, AU915_CHANNEL_MASK_SIZE);
00265 
00266     // set default channels
00267     phy_params.channels.channel_list = channels;
00268     phy_params.channels.channel_list_size = AU915_MAX_NB_CHANNELS;
00269     phy_params.channels.mask = channel_mask;
00270     phy_params.channels.default_mask = default_channel_mask;
00271     phy_params.channels.mask_size = AU915_CHANNEL_MASK_SIZE;
00272 
00273     // set bands for AU915 spectrum
00274     phy_params.bands.table = (void *) bands;
00275     phy_params.bands.size = AU915_MAX_NB_BANDS;
00276 
00277     // set bandwidths available in AU915 spectrum
00278     phy_params.bandwidths.table = (void *) bandwidths_AU915 ;
00279     phy_params.bandwidths.size = 16;
00280 
00281     // set data rates available in AU915 spectrum
00282     phy_params.datarates.table = (void *) datarates_AU915 ;
00283     phy_params.datarates.size = 16;
00284 
00285     // set payload sizes with respect to data rates
00286     phy_params.payloads.table = (void *) max_payload_AU915 ;
00287     phy_params.payloads.size = 16;
00288     phy_params.payloads_with_repeater.table = (void *) max_payload_with_repeater_AU915 ;
00289     phy_params.payloads_with_repeater.size = 16;
00290 
00291     // dwell time setting
00292     phy_params.ul_dwell_time_setting = 0;
00293     phy_params.dl_dwell_time_setting = 0;
00294     phy_params.dwell_limit_datarate = AU915_DEFAULT_DATARATE;
00295 
00296     phy_params.duty_cycle_enabled = AU915_DUTY_CYCLE_ENABLED;
00297     phy_params.accept_tx_param_setup_req = false;
00298     phy_params.custom_channelplans_supported = false;
00299     phy_params.cflist_supported = false;
00300     phy_params.fsk_supported = false;
00301 
00302     phy_params.default_channel_cnt = AU915_MAX_NB_CHANNELS;
00303     phy_params.max_channel_cnt = AU915_MAX_NB_CHANNELS;
00304     phy_params.cflist_channel_cnt = 0;
00305     phy_params.min_tx_datarate = AU915_TX_MIN_DATARATE;
00306     phy_params.max_tx_datarate = AU915_TX_MAX_DATARATE;
00307     phy_params.min_rx_datarate = AU915_RX_MIN_DATARATE;
00308     phy_params.max_rx_datarate = AU915_RX_MAX_DATARATE;
00309     phy_params.default_datarate = AU915_DEFAULT_DATARATE;
00310     phy_params.default_max_datarate = AU915_TX_MAX_DATARATE;
00311     phy_params.min_rx1_dr_offset = AU915_MIN_RX1_DR_OFFSET;
00312     phy_params.max_rx1_dr_offset = AU915_MAX_RX1_DR_OFFSET;
00313     phy_params.default_rx1_dr_offset = AU915_DEFAULT_RX1_DR_OFFSET;
00314     phy_params.min_tx_power = AU915_MIN_TX_POWER;
00315     phy_params.max_tx_power = AU915_MAX_TX_POWER;
00316     phy_params.default_tx_power = AU915_DEFAULT_TX_POWER;
00317     phy_params.default_max_eirp = AU915_DEFAULT_MAX_EIRP;
00318     phy_params.default_antenna_gain = AU915_DEFAULT_ANTENNA_GAIN;
00319     phy_params.adr_ack_limit = AU915_ADR_ACK_LIMIT;
00320     phy_params.adr_ack_delay = AU915_ADR_ACK_DELAY;
00321     phy_params.max_rx_window = AU915_MAX_RX_WINDOW;
00322     phy_params.recv_delay1 = AU915_RECEIVE_DELAY1;
00323     phy_params.recv_delay2 = AU915_RECEIVE_DELAY2;
00324 
00325     phy_params.join_accept_delay1 = AU915_JOIN_ACCEPT_DELAY1;
00326     phy_params.join_accept_delay2 = AU915_JOIN_ACCEPT_DELAY2;
00327     phy_params.max_fcnt_gap = AU915_MAX_FCNT_GAP;
00328     phy_params.ack_timeout = AU915_ACKTIMEOUT;
00329     phy_params.ack_timeout_rnd = AU915_ACK_TIMEOUT_RND;
00330     phy_params.rx_window2_datarate = AU915_RX_WND_2_DR;
00331     phy_params.rx_window2_frequency = AU915_RX_WND_2_FREQ;
00332 }
00333 
00334 LoRaPHYAU915::~LoRaPHYAU915()
00335 {
00336 }
00337 
00338 bool LoRaPHYAU915::rx_config(rx_config_params_t * params)
00339 {
00340     int8_t dr = params->datarate ;
00341     uint8_t max_payload = 0;
00342     int8_t phy_dr = 0;
00343     uint32_t frequency = params->frequency ;
00344 
00345     if (_radio->get_status() != RF_IDLE) {
00346         return false;
00347     }
00348 
00349     if (params->rx_slot  == RX_SLOT_WIN_1 ) {
00350         // Apply window 1 frequency
00351         frequency = AU915_FIRST_RX1_CHANNEL
00352                 + (params->channel  % 8) * AU915_STEPWIDTH_RX1_CHANNEL;
00353     }
00354 
00355     // Read the physical datarate from the datarates table
00356     phy_dr = datarates_AU915 [dr];
00357 
00358     _radio->lock();
00359 
00360     _radio->set_channel(frequency);
00361 
00362     // Radio configuration
00363     _radio->set_rx_config(MODEM_LORA, params->bandwidth , phy_dr, 1, 0, 8,
00364                           params->window_timeout , false, 0, false, 0, 0, true,
00365                           params->is_rx_continuous );
00366 
00367     if (params->is_repeater_supported  == true) {
00368         max_payload = max_payload_with_repeater_AU915 [dr];
00369     } else {
00370         max_payload = max_payload_AU915 [dr];
00371     }
00372     _radio->set_max_payload_length(MODEM_LORA,
00373                                    max_payload + LORA_MAC_FRMPAYLOAD_OVERHEAD);
00374 
00375     _radio->unlock();
00376 
00377     return true;
00378 }
00379 
00380 bool LoRaPHYAU915::tx_config(tx_config_params_t* params, int8_t* tx_power,
00381                              lorawan_time_t* tx_toa)
00382 {
00383     int8_t phy_dr = datarates_AU915 [params->datarate];
00384 
00385     if (params->tx_power > bands[channels[params->channel].band].max_tx_pwr) {
00386         params->tx_power = bands[channels[params->channel].band].max_tx_pwr;
00387     }
00388 
00389     uint32_t bandwidth = get_bandwidth(params->datarate);
00390     int8_t phy_tx_power = 0;
00391 
00392     // Calculate physical TX power
00393     phy_tx_power = compute_tx_power(params->tx_power, params->max_eirp,
00394                                     params->antenna_gain);
00395 
00396     // setting up radio tx configurations
00397 
00398     _radio->lock();
00399 
00400     _radio->set_channel(channels[params->channel].frequency);
00401 
00402     _radio->set_tx_config(MODEM_LORA, phy_tx_power, 0, bandwidth, phy_dr, 1, 8,
00403                           false, true, 0, 0, false, 3000);
00404 
00405     // Setup maximum payload lenght of the radio driver
00406     _radio->set_max_payload_length(MODEM_LORA, params->pkt_len);
00407 
00408     *tx_toa = _radio->time_on_air(MODEM_LORA, params->pkt_len);
00409 
00410     _radio->unlock();
00411 
00412     *tx_power = params->tx_power;
00413 
00414     return true;
00415 }
00416 
00417 uint8_t LoRaPHYAU915::link_ADR_request(adr_req_params_t* params,
00418                                        int8_t* dr_out, int8_t* tx_power_out,
00419                                        uint8_t* nb_rep_out,
00420                                        uint8_t* nb_bytes_parsed)
00421 {
00422     uint8_t status = 0x07;
00423     link_adr_params_t adr_settings = {};
00424     uint8_t next_index = 0;
00425     uint8_t bytes_processed = 0;
00426     uint16_t temp_channel_masks[AU915_CHANNEL_MASK_SIZE] = { 0, 0, 0, 0, 0};
00427 
00428     verify_adr_params_t verify_params;
00429 
00430     // Initialize local copy of channels mask
00431     copy_channel_mask(temp_channel_masks, channel_mask, AU915_CHANNEL_MASK_SIZE);
00432 
00433     while (bytes_processed < params->payload_size) {
00434         next_index = parse_link_ADR_req(&(params->payload [bytes_processed]),
00435                                         &adr_settings);
00436 
00437         if (next_index == 0) {
00438             break; // break loop, since no more request has been found
00439         }
00440 
00441         // Update bytes processed
00442         bytes_processed += next_index;
00443 
00444         // Revert status, as we only check the last ADR request for the channel mask KO
00445         status = 0x07;
00446 
00447         if (adr_settings.ch_mask_ctrl == 6) {
00448             // Enable all 125 kHz channels
00449             fill_channel_mask_with_value(temp_channel_masks, 0xFFFF,
00450                                          AU915_CHANNEL_MASK_SIZE - 1);
00451 
00452             // Apply chMask to channels 64 to 71
00453             temp_channel_masks[4] = adr_settings.channel_mask;
00454         } else if (adr_settings.ch_mask_ctrl == 7) {
00455             // Disable all 125 kHz channels
00456             fill_channel_mask_with_value(temp_channel_masks, 0x0000,
00457                                          AU915_CHANNEL_MASK_SIZE - 1);
00458 
00459             // Apply chMask to channels 64 to 71
00460             temp_channel_masks[4] = adr_settings.channel_mask;
00461         } else if (adr_settings.ch_mask_ctrl == 5) {
00462             // RFU
00463             status &= 0xFE; // Channel mask KO
00464         } else {
00465             temp_channel_masks[adr_settings.ch_mask_ctrl] = adr_settings.channel_mask;
00466         }
00467     }
00468 
00469     // FCC 15.247 paragraph F mandates to hop on at least 2 125 kHz channels
00470     if ((adr_settings.datarate < DR_6)
00471             && (num_active_channels(temp_channel_masks, 0, 4) < 2)) {
00472         status &= 0xFE; // Channel mask KO
00473     }
00474 
00475     verify_params.status  = status;
00476     verify_params.adr_enabled  = params->adr_enabled ;
00477     verify_params.datarate  = adr_settings.datarate;
00478     verify_params.tx_power  = adr_settings.tx_power;
00479     verify_params.nb_rep  = adr_settings.nb_rep;
00480     verify_params.current_datarate  = params->current_datarate ;
00481     verify_params.current_tx_power  = params->current_tx_power ;
00482     verify_params.current_nb_rep  = params->current_nb_trans ;
00483     verify_params.channel_mask  = temp_channel_masks;
00484 
00485 
00486     // Verify the parameters and update, if necessary
00487     status = verify_link_ADR_req(&verify_params, &adr_settings.datarate,
00488                                  &adr_settings.tx_power, &adr_settings.nb_rep);
00489 
00490     // Update cchannel mask if everything is correct
00491     if (status == 0x07) {
00492         // Copy Mask
00493         copy_channel_mask(channel_mask, temp_channel_masks, AU915_CHANNEL_MASK_SIZE);
00494 
00495         intersect_channel_mask(channel_mask, current_channel_mask,
00496                                AU915_CHANNEL_MASK_SIZE);
00497     }
00498 
00499     // Update status variables
00500     *dr_out = adr_settings.datarate;
00501     *tx_power_out = adr_settings.tx_power;
00502     *nb_rep_out = adr_settings.nb_rep;
00503     *nb_bytes_parsed = bytes_processed;
00504 
00505     return status;
00506 }
00507 
00508 uint8_t LoRaPHYAU915::accept_rx_param_setup_req(rx_param_setup_req_t* params)
00509 {
00510     uint8_t status = 0x07;
00511     uint32_t freq = params->frequency;
00512 
00513     // Verify radio frequency
00514     _radio->lock();
00515 
00516     if ((_radio->check_rf_frequency(freq) == false)
00517             || (freq < AU915_FIRST_RX1_CHANNEL)
00518             || (freq > AU915_LAST_RX1_CHANNEL)
00519             || (((freq - (uint32_t) AU915_FIRST_RX1_CHANNEL)
00520                     % (uint32_t) AU915_STEPWIDTH_RX1_CHANNEL) != 0)) {
00521         status &= 0xFE; // Channel frequency KO
00522     }
00523 
00524     _radio->unlock();
00525 
00526     // Verify datarate
00527     if (val_in_range(params->datarate, AU915_RX_MIN_DATARATE, AU915_RX_MAX_DATARATE) == 0) {
00528         status &= 0xFD; // Datarate KO
00529     }
00530 
00531     if ((params->datarate == DR_7) || (params->datarate > DR_13)) {
00532         status &= 0xFD; // Datarate KO
00533     }
00534 
00535     // Verify datarate offset
00536     if (val_in_range(params->dr_offset, AU915_MIN_RX1_DR_OFFSET, AU915_MAX_RX1_DR_OFFSET) == 0) {
00537         status &= 0xFB; // Rx1DrOffset range KO
00538     }
00539 
00540     return status;
00541 }
00542 
00543 int8_t LoRaPHYAU915::get_alternate_DR(uint8_t nb_trials)
00544 {
00545     int8_t datarate = 0;
00546 
00547     if ((nb_trials & 0x01) == 0x01) {
00548         datarate = DR_6;
00549     } else {
00550         datarate = DR_0;
00551     }
00552 
00553     return datarate;
00554 }
00555 
00556 lorawan_status_t LoRaPHYAU915::set_next_channel(channel_selection_params_t* next_chan_params,
00557                                                 uint8_t* channel, lorawan_time_t* time,
00558                                                 lorawan_time_t* aggregated_timeOff)
00559 {
00560     uint8_t nb_enabled_channels = 0;
00561     uint8_t delay_tx = 0;
00562     uint8_t enabled_channels[AU915_MAX_NB_CHANNELS] = { 0 };
00563     lorawan_time_t next_tx_delay = 0;
00564 
00565     // Count 125kHz channels
00566     if (num_active_channels(current_channel_mask, 0, 4) == 0) {
00567         // Reactivate 125 kHz default channels
00568         copy_channel_mask(current_channel_mask, channel_mask, 4);
00569     }
00570 
00571     // Check other channels
00572     if ((next_chan_params->current_datarate >= DR_6)
00573             && (current_channel_mask[4] & 0x00FF) == 0) {
00574         // fall back to 500 kHz default channels
00575         current_channel_mask[4] = channel_mask[4];
00576     }
00577 
00578     if (next_chan_params->aggregate_timeoff <= _lora_time->get_elapsed_time(next_chan_params->last_aggregate_tx_time)) {
00579         // Reset Aggregated time off
00580         *aggregated_timeOff = 0;
00581 
00582         // Update bands Time OFF
00583         next_tx_delay = update_band_timeoff(next_chan_params->joined,
00584                                             next_chan_params->dc_enabled,
00585                                             bands, AU915_MAX_NB_BANDS);
00586 
00587         // Search how many channels are enabled
00588         nb_enabled_channels = enabled_channel_count(next_chan_params->current_datarate,
00589                                                     current_channel_mask,
00590                                                     enabled_channels, &delay_tx);
00591     } else {
00592         delay_tx++;
00593         next_tx_delay = next_chan_params->aggregate_timeoff - _lora_time->get_elapsed_time(next_chan_params->last_aggregate_tx_time);
00594     }
00595 
00596     if (nb_enabled_channels > 0) {
00597         // We found a valid channel
00598         *channel = enabled_channels[get_random(0, nb_enabled_channels - 1)];
00599         // Disable the channel in the mask
00600         disable_channel(current_channel_mask, *channel, AU915_MAX_NB_CHANNELS);
00601 
00602         *time = 0;
00603         return LORAWAN_STATUS_OK;
00604     } else {
00605         if (delay_tx > 0) {
00606             // Delay transmission due to AggregatedTimeOff or to a band time off
00607             *time = next_tx_delay;
00608             return LORAWAN_STATUS_DUTYCYCLE_RESTRICTED;
00609         }
00610         // Datarate not supported by any channel
00611         *time = 0;
00612         return LORAWAN_STATUS_NO_CHANNEL_FOUND;
00613     }
00614 }
00615 
00616 uint8_t LoRaPHYAU915::apply_DR_offset(int8_t dr, int8_t dr_offset)
00617 {
00618     return datarate_offsets_AU915 [dr][dr_offset];
00619 }
00620 
00621 void LoRaPHYAU915::intersect_channel_mask(const uint16_t *source,
00622                                      uint16_t *destination, uint8_t size)
00623 {
00624     for (uint8_t i = 0; i < size; i++) {
00625         destination[i] &= source[i];
00626     }
00627 }
00628 
00629 void LoRaPHYAU915::fill_channel_mask_with_fsb(const uint16_t *expectation,
00630                                          const uint16_t *fsb_mask,
00631                                          uint16_t *destination,
00632                                          uint8_t size)
00633 {
00634     for (uint8_t i = 0; i < size; i++) {
00635         destination[i] = expectation[i] & fsb_mask[i];
00636     }
00637 
00638 }
00639 
00640 void LoRaPHYAU915::fill_channel_mask_with_value(uint16_t *channel_mask,
00641                                      uint16_t value, uint8_t size)
00642 {
00643     for (uint8_t i = 0; i < size; i++) {
00644         channel_mask[i] = value;
00645     }
00646 }