BA / Mbed OS BaBoRo_test2
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 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, int8_t* datarate)
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     *datarate = (uint8_t) dr;
00378     return true;
00379 }
00380 
00381 bool LoRaPHYAU915::tx_config(tx_config_params_t* params, int8_t* tx_power,
00382                              lorawan_time_t* tx_toa)
00383 {
00384     int8_t phy_dr = datarates_AU915 [params->datarate];
00385 
00386     if (params->tx_power > bands[channels[params->channel].band].max_tx_pwr) {
00387         params->tx_power = bands[channels[params->channel].band].max_tx_pwr;
00388     }
00389 
00390     uint32_t bandwidth = get_bandwidth(params->datarate);
00391     int8_t phy_tx_power = 0;
00392 
00393     // Calculate physical TX power
00394     phy_tx_power = compute_tx_power(params->tx_power, params->max_eirp,
00395                                     params->antenna_gain);
00396 
00397     // setting up radio tx configurations
00398 
00399     _radio->lock();
00400 
00401     _radio->set_channel(channels[params->channel].frequency);
00402 
00403     _radio->set_tx_config(MODEM_LORA, phy_tx_power, 0, bandwidth, phy_dr, 1, 8,
00404                           false, true, 0, 0, false, 3000);
00405 
00406     // Setup maximum payload lenght of the radio driver
00407     _radio->set_max_payload_length(MODEM_LORA, params->pkt_len);
00408 
00409     *tx_toa = _radio->time_on_air(MODEM_LORA, params->pkt_len);
00410 
00411     _radio->unlock();
00412 
00413     *tx_power = params->tx_power;
00414 
00415     return true;
00416 }
00417 
00418 uint8_t LoRaPHYAU915::link_ADR_request(adr_req_params_t* params,
00419                                        int8_t* dr_out, int8_t* tx_power_out,
00420                                        uint8_t* nb_rep_out,
00421                                        uint8_t* nb_bytes_parsed)
00422 {
00423     uint8_t status = 0x07;
00424     link_adr_params_t adr_settings;
00425     uint8_t next_index = 0;
00426     uint8_t bytes_processed = 0;
00427     uint16_t temp_channel_masks[AU915_CHANNEL_MASK_SIZE] = { 0, 0, 0, 0, 0};
00428 
00429     verify_adr_params_t verify_params;
00430 
00431     // Initialize local copy of channels mask
00432     copy_channel_mask(temp_channel_masks, channel_mask, AU915_CHANNEL_MASK_SIZE);
00433 
00434     while (bytes_processed < params->payload_size) {
00435         next_index = parse_link_ADR_req(&(params->payload [bytes_processed]),
00436                                         &adr_settings);
00437 
00438         if (next_index == 0) {
00439             break; // break loop, since no more request has been found
00440         }
00441 
00442         // Update bytes processed
00443         bytes_processed += next_index;
00444 
00445         // Revert status, as we only check the last ADR request for the channel mask KO
00446         status = 0x07;
00447 
00448         if (adr_settings.ch_mask_ctrl == 6) {
00449             // Enable all 125 kHz channels
00450             temp_channel_masks[0] = 0xFFFF;
00451             temp_channel_masks[1] = 0xFFFF;
00452             temp_channel_masks[2] = 0xFFFF;
00453             temp_channel_masks[3] = 0xFFFF;
00454             // Apply chMask to channels 64 to 71
00455             temp_channel_masks[4] = adr_settings.channel_mask;
00456         } else if (adr_settings.ch_mask_ctrl == 7) {
00457             // Disable all 125 kHz channels
00458             temp_channel_masks[0] = 0x0000;
00459             temp_channel_masks[1] = 0x0000;
00460             temp_channel_masks[2] = 0x0000;
00461             temp_channel_masks[3] = 0x0000;
00462             // Apply chMask to channels 64 to 71
00463             temp_channel_masks[4] = adr_settings.channel_mask;
00464         } else if (adr_settings.ch_mask_ctrl == 5) {
00465             // RFU
00466             status &= 0xFE; // Channel mask KO
00467         } else {
00468             temp_channel_masks[adr_settings.ch_mask_ctrl] = adr_settings.channel_mask;
00469         }
00470     }
00471 
00472     // FCC 15.247 paragraph F mandates to hop on at least 2 125 kHz channels
00473     if ((adr_settings.datarate < DR_6)
00474             && (num_active_channels(temp_channel_masks, 0, 4) < 2)) {
00475         status &= 0xFE; // Channel mask KO
00476     }
00477 
00478     verify_params.status  = status;
00479     verify_params.adr_enabled  = params->adr_enabled ;
00480     verify_params.datarate  = adr_settings.datarate;
00481     verify_params.tx_power  = adr_settings.tx_power;
00482     verify_params.nb_rep  = adr_settings.nb_rep;
00483     verify_params.current_datarate  = params->current_datarate ;
00484     verify_params.current_tx_power  = params->current_tx_power ;
00485     verify_params.current_nb_rep  = params->current_nb_rep ;
00486     verify_params.channel_mask  = temp_channel_masks;
00487 
00488 
00489     // Verify the parameters and update, if necessary
00490     status = verify_link_ADR_req(&verify_params, &adr_settings.datarate,
00491                                  &adr_settings.tx_power, &adr_settings.nb_rep);
00492 
00493     // Update cchannel mask if everything is correct
00494     if (status == 0x07) {
00495         // Copy Mask
00496         copy_channel_mask(channel_mask, temp_channel_masks, AU915_CHANNEL_MASK_SIZE);
00497 
00498         current_channel_mask[0] &= channel_mask[0];
00499         current_channel_mask[1] &= channel_mask[1];
00500         current_channel_mask[2] &= channel_mask[2];
00501         current_channel_mask[3] &= channel_mask[3];
00502         current_channel_mask[4] = channel_mask[4];
00503     }
00504 
00505     // Update status variables
00506     *dr_out = adr_settings.datarate;
00507     *tx_power_out = adr_settings.tx_power;
00508     *nb_rep_out = adr_settings.nb_rep;
00509     *nb_bytes_parsed = bytes_processed;
00510 
00511     return status;
00512 }
00513 
00514 uint8_t LoRaPHYAU915::accept_rx_param_setup_req(rx_param_setup_req_t* params)
00515 {
00516     uint8_t status = 0x07;
00517     uint32_t freq = params->frequency;
00518 
00519     // Verify radio frequency
00520     _radio->lock();
00521 
00522     if ((_radio->check_rf_frequency(freq) == false)
00523             || (freq < AU915_FIRST_RX1_CHANNEL)
00524             || (freq > AU915_LAST_RX1_CHANNEL)
00525             || (((freq - (uint32_t) AU915_FIRST_RX1_CHANNEL)
00526                     % (uint32_t) AU915_STEPWIDTH_RX1_CHANNEL) != 0)) {
00527         status &= 0xFE; // Channel frequency KO
00528     }
00529 
00530     _radio->unlock();
00531 
00532     // Verify datarate
00533     if (val_in_range(params->datarate, AU915_RX_MIN_DATARATE, AU915_RX_MAX_DATARATE) == 0) {
00534         status &= 0xFD; // Datarate KO
00535     }
00536 
00537     if ((params->datarate == DR_7) || (params->datarate > DR_13)) {
00538         status &= 0xFD; // Datarate KO
00539     }
00540 
00541     // Verify datarate offset
00542     if (val_in_range(params->dr_offset, AU915_MIN_RX1_DR_OFFSET, AU915_MAX_RX1_DR_OFFSET) == 0) {
00543         status &= 0xFB; // Rx1DrOffset range KO
00544     }
00545 
00546     return status;
00547 }
00548 
00549 int8_t LoRaPHYAU915::get_alternate_DR(uint8_t nb_trials)
00550 {
00551     int8_t datarate = 0;
00552 
00553     // Re-enable 500 kHz default channels
00554     channel_mask[4] = 0x00FF;
00555 
00556     if ((nb_trials & 0x01) == 0x01) {
00557         datarate = DR_6;
00558     } else {
00559         datarate = DR_0;
00560     }
00561 
00562     return datarate;
00563 }
00564 
00565 lorawan_status_t LoRaPHYAU915::set_next_channel(channel_selection_params_t* next_chan_params,
00566                                                 uint8_t* channel, lorawan_time_t* time,
00567                                                 lorawan_time_t* aggregated_timeOff)
00568 {
00569     uint8_t nb_enabled_channels = 0;
00570     uint8_t delay_tx = 0;
00571     uint8_t enabled_channels[AU915_MAX_NB_CHANNELS] = { 0 };
00572     lorawan_time_t next_tx_delay = 0;
00573 
00574     // Count 125kHz channels
00575     if (num_active_channels(current_channel_mask, 0, 4) == 0) {
00576         // Reactivate 125 kHz default channels
00577         copy_channel_mask(current_channel_mask, channel_mask, 4);
00578     }
00579 
00580     // Check other channels
00581     if (next_chan_params->current_datarate >= DR_6) {
00582         if ((current_channel_mask[4] & 0x00FF) == 0) {
00583             // fall back to 500 kHz default channels
00584             current_channel_mask[4] = channel_mask[4];
00585         }
00586     }
00587 
00588     if (next_chan_params->aggregate_timeoff <= _lora_time.get_elapsed_time(next_chan_params->last_aggregate_tx_time)) {
00589         // Reset Aggregated time off
00590         *aggregated_timeOff = 0;
00591 
00592         // Update bands Time OFF
00593         next_tx_delay = update_band_timeoff(next_chan_params->joined,
00594                                             next_chan_params->dc_enabled,
00595                                             bands, AU915_MAX_NB_BANDS);
00596 
00597         // Search how many channels are enabled
00598         nb_enabled_channels = enabled_channel_count(next_chan_params->joined,
00599                                                      next_chan_params->current_datarate,
00600                                                      current_channel_mask,
00601                                                      enabled_channels, &delay_tx);
00602     } else {
00603         delay_tx++;
00604         next_tx_delay = next_chan_params->aggregate_timeoff - _lora_time.get_elapsed_time(next_chan_params->last_aggregate_tx_time);
00605     }
00606 
00607     if (nb_enabled_channels > 0) {
00608         // We found a valid channel
00609         *channel = enabled_channels[get_random(0, nb_enabled_channels - 1)];
00610         // Disable the channel in the mask
00611         disable_channel(current_channel_mask, *channel,
00612         AU915_MAX_NB_CHANNELS - 8);
00613 
00614         *time = 0;
00615         return LORAWAN_STATUS_OK;
00616     } else {
00617         if (delay_tx > 0) {
00618             // Delay transmission due to AggregatedTimeOff or to a band time off
00619             *time = next_tx_delay;
00620             return LORAWAN_STATUS_DUTYCYCLE_RESTRICTED;
00621         }
00622         // Datarate not supported by any channel
00623         *time = 0;
00624         return LORAWAN_STATUS_NO_CHANNEL_FOUND;
00625     }
00626 }
00627 
00628 uint8_t LoRaPHYAU915::apply_DR_offset(int8_t dr, int8_t dr_offset)
00629 {
00630     int8_t datarate = datarate_offsets_AU915 [dr][dr_offset];
00631 
00632     if (datarate < 0) {
00633         datarate = DR_0;
00634     }
00635     return datarate;
00636 }