Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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