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 
00224 LoRaPHYAU915::LoRaPHYAU915(LoRaWANTimeHandler &lora_time)
00225         : LoRaPHY(lora_time)
00226 {
00227     bands[0] = AU915_BAND0 ;
00228 
00229     // Activate Channels
00230     // 125 kHz channels Upstream only
00231     for (uint8_t i = 0; i < AU915_MAX_NB_CHANNELS - 8; i++) {
00232         channels[i].frequency = 915200000 + i * 200000;
00233         channels[i].dr_range.value = ( DR_5 << 4) | DR_0;
00234         channels[i].band = 0;
00235     }
00236     // 500 kHz channels
00237     // Upstream and downstream both
00238     for (uint8_t i = AU915_MAX_NB_CHANNELS - 8; i < AU915_MAX_NB_CHANNELS; i++) {
00239         channels[i].frequency = 915900000 + (i - ( AU915_MAX_NB_CHANNELS - 8)) * 1600000;
00240         channels[i].dr_range.value = ( DR_6 << 4) | DR_6;
00241         channels[i].band = 0;
00242     }
00243 
00244     // Initialize channels default mask
00245     // All channels are default channels here
00246     // Join request needs to alternate between 125 KHz and 500 KHz channels
00247     // randomly.
00248     default_channel_mask[0] = 0xFFFF;
00249     default_channel_mask[1] = 0xFFFF;
00250     default_channel_mask[2] = 0xFFFF;
00251     default_channel_mask[3] = 0xFFFF;
00252     default_channel_mask[4] = 0x00FF;
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 bands for EU868 spectrum
00267     phy_params.bands.table = (void *) bands;
00268     phy_params.bands.size = AU915_MAX_NB_BANDS;
00269 
00270     // set bandwidths available in EU868 spectrum
00271     phy_params.bandwidths.table = (void *) bandwidths_AU915 ;
00272     phy_params.bandwidths.size = 16;
00273 
00274     // set data rates available in EU868 spectrum
00275     phy_params.datarates.table = (void *) datarates_AU915 ;
00276     phy_params.datarates.size = 16;
00277 
00278     // set payload sizes with respect to data rates
00279     phy_params.payloads.table = (void *) max_payload_AU915 ;
00280     phy_params.payloads.size = 16;
00281     phy_params.payloads_with_repeater.table = (void *) max_payload_with_repeater_AU915 ;
00282     phy_params.payloads_with_repeater.size = 16;
00283 
00284     // dwell time setting
00285     phy_params.ul_dwell_time_setting = 0;
00286     phy_params.dl_dwell_time_setting = 0;
00287     phy_params.dwell_limit_datarate = AU915_DEFAULT_DATARATE;
00288 
00289     phy_params.duty_cycle_enabled = AU915_DUTY_CYCLE_ENABLED;
00290     phy_params.accept_tx_param_setup_req = false;
00291     phy_params.custom_channelplans_supported = false;
00292     phy_params.cflist_supported = false;
00293     phy_params.fsk_supported = false;
00294 
00295     phy_params.default_channel_cnt = AU915_MAX_NB_CHANNELS;
00296     phy_params.max_channel_cnt = AU915_MAX_NB_CHANNELS;
00297     phy_params.cflist_channel_cnt = 0;
00298     phy_params.min_tx_datarate = AU915_TX_MIN_DATARATE;
00299     phy_params.max_tx_datarate = AU915_TX_MAX_DATARATE;
00300     phy_params.min_rx_datarate = AU915_RX_MIN_DATARATE;
00301     phy_params.max_rx_datarate = AU915_RX_MAX_DATARATE;
00302     phy_params.default_datarate = AU915_DEFAULT_DATARATE;
00303     phy_params.default_max_datarate = AU915_TX_MAX_DATARATE;
00304     phy_params.min_rx1_dr_offset = AU915_MIN_RX1_DR_OFFSET;
00305     phy_params.max_rx1_dr_offset = AU915_MAX_RX1_DR_OFFSET;
00306     phy_params.default_rx1_dr_offset = AU915_DEFAULT_RX1_DR_OFFSET;
00307     phy_params.min_tx_power = AU915_MIN_TX_POWER;
00308     phy_params.max_tx_power = AU915_MAX_TX_POWER;
00309     phy_params.default_tx_power = AU915_DEFAULT_TX_POWER;
00310     phy_params.default_max_eirp = AU915_DEFAULT_MAX_EIRP;
00311     phy_params.default_antenna_gain = AU915_DEFAULT_ANTENNA_GAIN;
00312     phy_params.adr_ack_limit = AU915_ADR_ACK_LIMIT;
00313     phy_params.adr_ack_delay = AU915_ADR_ACK_DELAY;
00314     phy_params.max_rx_window = AU915_MAX_RX_WINDOW;
00315     phy_params.recv_delay1 = AU915_RECEIVE_DELAY1;
00316     phy_params.recv_delay2 = AU915_RECEIVE_DELAY2;
00317 
00318     phy_params.join_accept_delay1 = AU915_JOIN_ACCEPT_DELAY1;
00319     phy_params.join_accept_delay2 = AU915_JOIN_ACCEPT_DELAY2;
00320     phy_params.max_fcnt_gap = AU915_MAX_FCNT_GAP;
00321     phy_params.ack_timeout = AU915_ACKTIMEOUT;
00322     phy_params.ack_timeout_rnd = AU915_ACK_TIMEOUT_RND;
00323     phy_params.rx_window2_datarate = AU915_RX_WND_2_DR;
00324     phy_params.rx_window2_frequency = AU915_RX_WND_2_FREQ;
00325 }
00326 
00327 LoRaPHYAU915::~LoRaPHYAU915()
00328 {
00329 }
00330 
00331 bool LoRaPHYAU915::rx_config(rx_config_params_t * params, int8_t* datarate)
00332 {
00333     int8_t dr = params->datarate ;
00334     uint8_t max_payload = 0;
00335     int8_t phy_dr = 0;
00336     uint32_t frequency = params->frequency ;
00337 
00338     if (_radio->get_status() != RF_IDLE) {
00339         return false;
00340     }
00341 
00342     if (params->rx_slot  == RX_SLOT_WIN_1 ) {
00343         // Apply window 1 frequency
00344         frequency = AU915_FIRST_RX1_CHANNEL
00345                 + (params->channel  % 8) * AU915_STEPWIDTH_RX1_CHANNEL;
00346     }
00347 
00348     // Read the physical datarate from the datarates table
00349     phy_dr = datarates_AU915 [dr];
00350 
00351     _radio->lock();
00352 
00353     _radio->set_channel(frequency);
00354 
00355     // Radio configuration
00356     _radio->set_rx_config(MODEM_LORA, params->bandwidth , phy_dr, 1, 0, 8,
00357                           params->window_timeout , false, 0, false, 0, 0, true,
00358                           params->is_rx_continuous );
00359 
00360     if (params->is_repeater_supported  == true) {
00361         max_payload = max_payload_with_repeater_AU915 [dr];
00362     } else {
00363         max_payload = max_payload_AU915 [dr];
00364     }
00365     _radio->set_max_payload_length(MODEM_LORA,
00366                                    max_payload + LORA_MAC_FRMPAYLOAD_OVERHEAD);
00367 
00368     _radio->unlock();
00369 
00370     *datarate = (uint8_t) dr;
00371     return true;
00372 }
00373 
00374 bool LoRaPHYAU915::tx_config(tx_config_params_t* params, int8_t* tx_power,
00375                              lorawan_time_t* tx_toa)
00376 {
00377     int8_t phy_dr = datarates_AU915 [params->datarate];
00378 
00379     if (params->tx_power > bands[channels[params->channel].band].max_tx_pwr) {
00380         params->tx_power = bands[channels[params->channel].band].max_tx_pwr;
00381     }
00382 
00383     uint32_t bandwidth = get_bandwidth(params->datarate);
00384     int8_t phy_tx_power = 0;
00385 
00386     // Calculate physical TX power
00387     phy_tx_power = compute_tx_power(params->tx_power, params->max_eirp,
00388                                     params->antenna_gain);
00389 
00390     // setting up radio tx configurations
00391 
00392     _radio->lock();
00393 
00394     _radio->set_channel(channels[params->channel].frequency);
00395 
00396     _radio->set_tx_config(MODEM_LORA, phy_tx_power, 0, bandwidth, phy_dr, 1, 8,
00397                           false, true, 0, 0, false, 3000);
00398 
00399     // Setup maximum payload lenght of the radio driver
00400     _radio->set_max_payload_length(MODEM_LORA, params->pkt_len);
00401 
00402     *tx_toa = _radio->time_on_air(MODEM_LORA, params->pkt_len);
00403 
00404     _radio->unlock();
00405 
00406     *tx_power = params->tx_power;
00407 
00408     return true;
00409 }
00410 
00411 uint8_t LoRaPHYAU915::link_ADR_request(adr_req_params_t* params,
00412                                        int8_t* dr_out, int8_t* tx_power_out,
00413                                        uint8_t* nb_rep_out,
00414                                        uint8_t* nb_bytes_parsed)
00415 {
00416     uint8_t status = 0x07;
00417     link_adr_params_t adr_settings;
00418     uint8_t next_index = 0;
00419     uint8_t bytes_processed = 0;
00420     uint16_t temp_channel_masks[AU915_CHANNEL_MASK_SIZE] = { 0, 0, 0, 0, 0};
00421 
00422     verify_adr_params_t verify_params;
00423 
00424     // Initialize local copy of channels mask
00425     copy_channel_mask(temp_channel_masks, channel_mask, AU915_CHANNEL_MASK_SIZE);
00426 
00427     while (bytes_processed < params->payload_size) {
00428         next_index = parse_link_ADR_req(&(params->payload [bytes_processed]),
00429                                         &adr_settings);
00430 
00431         if (next_index == 0) {
00432             break; // break loop, since no more request has been found
00433         }
00434 
00435         // Update bytes processed
00436         bytes_processed += next_index;
00437 
00438         // Revert status, as we only check the last ADR request for the channel mask KO
00439         status = 0x07;
00440 
00441         if (adr_settings.ch_mask_ctrl == 6) {
00442             // Enable all 125 kHz channels
00443             temp_channel_masks[0] = 0xFFFF;
00444             temp_channel_masks[1] = 0xFFFF;
00445             temp_channel_masks[2] = 0xFFFF;
00446             temp_channel_masks[3] = 0xFFFF;
00447             // Apply chMask to channels 64 to 71
00448             temp_channel_masks[4] = adr_settings.channel_mask;
00449         } else if (adr_settings.ch_mask_ctrl == 7) {
00450             // Disable all 125 kHz channels
00451             temp_channel_masks[0] = 0x0000;
00452             temp_channel_masks[1] = 0x0000;
00453             temp_channel_masks[2] = 0x0000;
00454             temp_channel_masks[3] = 0x0000;
00455             // Apply chMask to channels 64 to 71
00456             temp_channel_masks[4] = adr_settings.channel_mask;
00457         } else if (adr_settings.ch_mask_ctrl == 5) {
00458             // RFU
00459             status &= 0xFE; // Channel mask KO
00460         } else {
00461             temp_channel_masks[adr_settings.ch_mask_ctrl] = adr_settings.channel_mask;
00462         }
00463     }
00464 
00465     // FCC 15.247 paragraph F mandates to hop on at least 2 125 kHz channels
00466     if ((adr_settings.datarate < DR_6)
00467             && (num_active_channels(temp_channel_masks, 0, 4) < 2)) {
00468         status &= 0xFE; // Channel mask KO
00469     }
00470 
00471     verify_params.status  = status;
00472     verify_params.adr_enabled  = params->adr_enabled ;
00473     verify_params.datarate  = adr_settings.datarate;
00474     verify_params.tx_power  = adr_settings.tx_power;
00475     verify_params.nb_rep  = adr_settings.nb_rep;
00476     verify_params.current_datarate  = params->current_datarate ;
00477     verify_params.current_tx_power  = params->current_tx_power ;
00478     verify_params.current_nb_rep  = params->current_nb_rep ;
00479     verify_params.channel_mask  = temp_channel_masks;
00480 
00481 
00482     // Verify the parameters and update, if necessary
00483     status = verify_link_ADR_req(&verify_params, &adr_settings.datarate,
00484                                  &adr_settings.tx_power, &adr_settings.nb_rep);
00485 
00486     // Update cchannel mask if everything is correct
00487     if (status == 0x07) {
00488         // Copy Mask
00489         copy_channel_mask(channel_mask, temp_channel_masks, AU915_CHANNEL_MASK_SIZE);
00490 
00491         current_channel_mask[0] &= channel_mask[0];
00492         current_channel_mask[1] &= channel_mask[1];
00493         current_channel_mask[2] &= channel_mask[2];
00494         current_channel_mask[3] &= channel_mask[3];
00495         current_channel_mask[4] = channel_mask[4];
00496     }
00497 
00498     // Update status variables
00499     *dr_out = adr_settings.datarate;
00500     *tx_power_out = adr_settings.tx_power;
00501     *nb_rep_out = adr_settings.nb_rep;
00502     *nb_bytes_parsed = bytes_processed;
00503 
00504     return status;
00505 }
00506 
00507 uint8_t LoRaPHYAU915::accept_rx_param_setup_req(rx_param_setup_req_t* params)
00508 {
00509     uint8_t status = 0x07;
00510     uint32_t freq = params->frequency;
00511 
00512     // Verify radio frequency
00513     _radio->lock();
00514 
00515     if ((_radio->check_rf_frequency(freq) == false)
00516             || (freq < AU915_FIRST_RX1_CHANNEL)
00517             || (freq > AU915_LAST_RX1_CHANNEL)
00518             || (((freq - (uint32_t) AU915_FIRST_RX1_CHANNEL)
00519                     % (uint32_t) AU915_STEPWIDTH_RX1_CHANNEL) != 0)) {
00520         status &= 0xFE; // Channel frequency KO
00521     }
00522 
00523     _radio->unlock();
00524 
00525     // Verify datarate
00526     if (val_in_range(params->datarate, AU915_RX_MIN_DATARATE, AU915_RX_MAX_DATARATE) == 0) {
00527         status &= 0xFD; // Datarate KO
00528     }
00529 
00530     if ((params->datarate == DR_7) || (params->datarate > DR_13)) {
00531         status &= 0xFD; // Datarate KO
00532     }
00533 
00534     // Verify datarate offset
00535     if (val_in_range(params->dr_offset, AU915_MIN_RX1_DR_OFFSET, AU915_MAX_RX1_DR_OFFSET) == 0) {
00536         status &= 0xFB; // Rx1DrOffset range KO
00537     }
00538 
00539     return status;
00540 }
00541 
00542 int8_t LoRaPHYAU915::get_alternate_DR(uint8_t nb_trials)
00543 {
00544     int8_t datarate = 0;
00545 
00546     // Re-enable 500 kHz default channels
00547     channel_mask[4] = 0x00FF;
00548 
00549     if ((nb_trials & 0x01) == 0x01) {
00550         datarate = DR_6;
00551     } else {
00552         datarate = DR_0;
00553     }
00554 
00555     return datarate;
00556 }
00557 
00558 bool LoRaPHYAU915::set_next_channel(channel_selection_params_t* next_chan_params,
00559                                     uint8_t* channel, lorawan_time_t* time,
00560                                     lorawan_time_t* aggregated_timeOff)
00561 {
00562     uint8_t nb_enabled_channels = 0;
00563     uint8_t delay_tx = 0;
00564     uint8_t enabled_channels[AU915_MAX_NB_CHANNELS] = { 0 };
00565     lorawan_time_t next_tx_delay = 0;
00566 
00567     // Count 125kHz channels
00568     if (num_active_channels(current_channel_mask, 0, 4) == 0) {
00569         // Reactivate 125 kHz default channels
00570         copy_channel_mask(current_channel_mask, channel_mask, 4);
00571     }
00572 
00573     // Check other channels
00574     if (next_chan_params->current_datarate >= DR_6) {
00575         if ((current_channel_mask[4] & 0x00FF) == 0) {
00576             // fall back to 500 kHz default channels
00577             current_channel_mask[4] = channel_mask[4];
00578         }
00579     }
00580 
00581     if (next_chan_params->aggregate_timeoff <= _lora_time.get_elapsed_time(next_chan_params->last_aggregate_tx_time)) {
00582         // Reset Aggregated time off
00583         *aggregated_timeOff = 0;
00584 
00585         // Update bands Time OFF
00586         next_tx_delay = update_band_timeoff(next_chan_params->joined,
00587                                             next_chan_params->dc_enabled,
00588                                             bands, AU915_MAX_NB_BANDS);
00589 
00590         // Search how many channels are enabled
00591         nb_enabled_channels = enabled_channel_count(next_chan_params->joined,
00592                                                      next_chan_params->current_datarate,
00593                                                      current_channel_mask,
00594                                                      enabled_channels, &delay_tx);
00595     } else {
00596         delay_tx++;
00597         next_tx_delay = next_chan_params->aggregate_timeoff - _lora_time.get_elapsed_time(next_chan_params->last_aggregate_tx_time);
00598     }
00599 
00600     if (nb_enabled_channels > 0) {
00601         // We found a valid channel
00602         *channel = enabled_channels[get_random(0, nb_enabled_channels - 1)];
00603         // Disable the channel in the mask
00604         disable_channel(current_channel_mask, *channel,
00605         AU915_MAX_NB_CHANNELS - 8);
00606 
00607         *time = 0;
00608         return true;
00609     } else {
00610         if (delay_tx > 0) {
00611             // Delay transmission due to AggregatedTimeOff or to a band time off
00612             *time = next_tx_delay;
00613             return true;
00614         }
00615         // Datarate not supported by any channel
00616         *time = 0;
00617         return false;
00618     }
00619 }
00620 
00621 uint8_t LoRaPHYAU915::apply_DR_offset(int8_t dr, int8_t dr_offset)
00622 {
00623     int8_t datarate = datarate_offsets_AU915 [dr][dr_offset];
00624 
00625     if (datarate < 0) {
00626         datarate = DR_0;
00627     }
00628     return datarate;
00629 }