Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Dot-Examples Dot-AT-Firmware Dot-Examples TEST_FF1705 ... more
ChannelPlan_RU864.cpp
00001 /********************************************************************** 00002 * COPYRIGHT 2016 MULTI-TECH SYSTEMS, INC. 00003 * 00004 * ALL RIGHTS RESERVED BY AND FOR THE EXCLUSIVE BENEFIT OF 00005 * MULTI-TECH SYSTEMS, INC. 00006 * 00007 * MULTI-TECH SYSTEMS, INC. - CONFIDENTIAL AND PROPRIETARY 00008 * INFORMATION AND/OR TRADE SECRET. 00009 * 00010 * NOTICE: ALL CODE, PROGRAM, INFORMATION, SCRIPT, INSTRUCTION, 00011 * DATA, AND COMMENT HEREIN IS AND SHALL REMAIN THE CONFIDENTIAL 00012 * INFORMATION AND PROPERTY OF MULTI-TECH SYSTEMS, INC. 00013 * USE AND DISCLOSURE THEREOF, EXCEPT AS STRICTLY AUTHORIZED IN A 00014 * WRITTEN AGREEMENT SIGNED BY MULTI-TECH SYSTEMS, INC. IS PROHIBITED. 00015 * 00016 ***********************************************************************/ 00017 00018 #include "ChannelPlan_RU864.h" 00019 #include "limits.h" 00020 00021 using namespace lora; 00022 00023 // MWF - changed RU864_TX_POWERS to match final 1.0.2 regional spec 00024 const uint8_t ChannelPlan_RU864::RU864_TX_POWERS[] = { 16, 14, 12, 10, 8, 6, 4, 2 }; 00025 const uint8_t ChannelPlan_RU864::RU864_RADIO_POWERS[] = { 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 19, 20 }; 00026 const uint8_t ChannelPlan_RU864::RU864_MAX_PAYLOAD_SIZE[] = { 51, 51, 51, 115, 242, 242, 242, 242, 0, 0, 0, 0, 0, 0, 0, 0 }; 00027 const uint8_t ChannelPlan_RU864::RU864_MAX_PAYLOAD_SIZE_REPEATER[] = { 51, 51, 51, 115, 222, 222, 222, 222, 0, 0, 0, 0, 0, 0, 0, 0 }; 00028 00029 ChannelPlan_RU864::ChannelPlan_RU864() 00030 : 00031 ChannelPlan(NULL, NULL) 00032 { 00033 00034 } 00035 00036 ChannelPlan_RU864::ChannelPlan_RU864(Settings* settings) 00037 : 00038 ChannelPlan(NULL, settings) 00039 { 00040 00041 } 00042 00043 ChannelPlan_RU864::ChannelPlan_RU864(SxRadio* radio, Settings* settings) 00044 : 00045 ChannelPlan(radio, settings) 00046 { 00047 00048 } 00049 00050 ChannelPlan_RU864::~ChannelPlan_RU864() { 00051 00052 } 00053 00054 void ChannelPlan_RU864::Init() { 00055 00056 _datarates.clear(); 00057 _channels.clear(); 00058 _dutyBands.clear(); 00059 00060 DutyBand band; 00061 00062 band.Index = 0; 00063 band.DutyCycle = 0; 00064 00065 Datarate dr; 00066 00067 _plan = RU864; 00068 _planName = "RU864"; 00069 _maxTxPower = 27; 00070 _minTxPower = 0; 00071 00072 _minFrequency = RU864_FREQ_MIN; 00073 _maxFrequency = RU864_FREQ_MAX; 00074 00075 TX_POWERS = RU864_TX_POWERS; 00076 RADIO_POWERS = RU864_RADIO_POWERS; 00077 MAX_PAYLOAD_SIZE = RU864_MAX_PAYLOAD_SIZE; 00078 MAX_PAYLOAD_SIZE_REPEATER = RU864_MAX_PAYLOAD_SIZE_REPEATER; 00079 00080 _minDatarate = RU864_MIN_DATARATE; 00081 _maxDatarate = RU864_MAX_DATARATE; 00082 00083 _minRx2Datarate = DR_0; 00084 _maxRx2Datarate = DR_7; 00085 00086 _minDatarateOffset = RU864_MIN_DATARATE_OFFSET; 00087 _maxDatarateOffset = RU864_MAX_DATARATE_OFFSET; 00088 00089 _numChans125k = RU864_125K_NUM_CHANS; 00090 _numChans500k = 0; 00091 _numDefaultChans = RU864_DEFAULT_NUM_CHANS; 00092 00093 GetSettings()->Session.Rx2Frequency = RU864_RX2_FREQ; 00094 GetSettings()->Session.Rx2DatarateIndex = DR_0; 00095 00096 GetSettings()->Session.BeaconFrequency = RU864_BEACON_FREQ; 00097 GetSettings()->Session.BeaconFreqHop = false; 00098 GetSettings()->Session.PingSlotFrequency = RU864_PING_SLOT_FREQ; 00099 GetSettings()->Session.PingSlotDatarateIndex = RU864_BEACON_DR; 00100 GetSettings()->Session.PingSlotFreqHop = false; 00101 00102 logInfo("Initialize datarates..."); 00103 00104 dr.SpreadingFactor = SF_12; 00105 00106 // Add DR0-5 00107 while (dr.SpreadingFactor >= SF_7) { 00108 AddDatarate(-1, dr); 00109 dr.SpreadingFactor--; 00110 dr.Index++; 00111 } 00112 00113 // Add DR6 00114 dr.SpreadingFactor = SF_7; 00115 dr.Bandwidth = BW_250; 00116 AddDatarate(-1, dr); 00117 dr.Index++; 00118 00119 // Add DR7 00120 dr.SpreadingFactor = SF_FSK; 00121 dr.Bandwidth = BW_FSK; 00122 dr.PreambleLength = 10; 00123 dr.Coderate = 0; 00124 AddDatarate(-1, dr); 00125 dr.Index++; 00126 00127 _maxDatarate = DR_7; 00128 00129 // Skip DR8-15 RFU 00130 dr.SpreadingFactor = SF_INVALID; 00131 while (dr.Index++ <= DR_15) { 00132 AddDatarate(-1, dr); 00133 } 00134 00135 GetSettings()->Session.TxDatarate = 0; 00136 00137 logInfo("Initialize channels..."); 00138 00139 Channel chan; 00140 chan.DrRange.Fields.Min = DR_0; 00141 chan.DrRange.Fields.Max = DR_5; 00142 chan.Index = 0; 00143 chan.Frequency = RU864_125K_FREQ_BASE; 00144 SetNumberOfChannels(RU864_125K_NUM_CHANS); 00145 00146 for (uint8_t i = 0; i < RU864_DEFAULT_NUM_CHANS; i++) { 00147 chan.DrRange.Fields.Max = DR_5; 00148 00149 AddChannel(i, chan); 00150 chan.Index++; 00151 chan.Frequency += RU864_125K_FREQ_STEP; 00152 } 00153 00154 chan.DrRange.Value = 0; 00155 chan.Frequency = 0; 00156 00157 for (uint8_t i = RU864_DEFAULT_NUM_CHANS; i < RU864_125K_NUM_CHANS; i++) { 00158 AddChannel(i, chan); 00159 chan.Index++; 00160 } 00161 00162 // Add downlink channel defaults 00163 chan.Index = 0; 00164 _dlChannels.resize(16); 00165 for (uint8_t i = 0; i < 16; i++) { 00166 AddDownlinkChannel(i, chan); 00167 chan.Index++; 00168 } 00169 00170 SetChannelMask(0, 0x07); 00171 00172 band.Index = 0; 00173 band.FrequencyMin = RU864_MILLI_FREQ_MIN; 00174 band.FrequencyMax = RU864_MILLI_FREQ_MAX; 00175 band.PowerMax = 14; 00176 band.TimeOffEnd = 0; 00177 00178 band.DutyCycle = 1000; 00179 00180 AddDutyBand(-1, band); 00181 00182 band.Index++; 00183 band.FrequencyMin = RU864_MILLI_FREQ_MAX; 00184 band.FrequencyMax = RU864_FREQ_MAX; 00185 band.PowerMax = 14; 00186 band.TimeOffEnd = 0; 00187 band.DutyCycle = 0; 00188 00189 AddDutyBand(-1, band); 00190 00191 GetSettings()->Session.TxPower = GetSettings()->Network.TxPower; 00192 } 00193 00194 uint8_t ChannelPlan_RU864::AddChannel(int8_t index, Channel channel) { 00195 logTrace("Add Channel %d : %lu : %02x %d", index, channel.Frequency, channel.DrRange.Value, _channels.size()); 00196 00197 assert(index < (int) _channels.size()); 00198 00199 if (index >= 0) { 00200 _channels[index] = channel; 00201 } else { 00202 _channels.push_back(channel); 00203 } 00204 00205 return LORA_OK; 00206 } 00207 00208 uint8_t ChannelPlan_RU864::HandleJoinAccept(const uint8_t* buffer, uint8_t size) { 00209 00210 if (size == 33) { 00211 Channel ch; 00212 int index = 2; 00213 for (int i = 13; i < size - 5; i += 3) { 00214 00215 ch.Frequency = ((buffer[i]) | (buffer[i + 1] << 8) | (buffer[i + 2] << 16)) * 100u; 00216 00217 if (ch.Frequency > 0) { 00218 ch.Index = index; 00219 ch.DrRange.Fields.Min = static_cast<int8_t>(DR_0); 00220 ch.DrRange.Fields.Max = static_cast<int8_t>(DR_5); 00221 AddChannel(index, ch); 00222 00223 if (GetDutyBand(ch.Frequency) > -1) 00224 _channelMask[0] |= (1 << index); 00225 else 00226 _channelMask[0] |= ~(1 << index); 00227 00228 index += 1; 00229 } 00230 } 00231 } 00232 00233 return LORA_OK; 00234 } 00235 00236 uint8_t ChannelPlan_RU864::SetTxConfig() { 00237 00238 logInfo("Configure radio for TX"); 00239 00240 uint8_t band = GetDutyBand(GetChannel(_txChannel).Frequency); 00241 Datarate txDr = GetDatarate(GetSettings()->Session.TxDatarate); 00242 int8_t max_pwr = _dutyBands[band].PowerMax; 00243 00244 int8_t pwr = 0; 00245 00246 pwr = std::min < int8_t > (GetSettings()->Session.TxPower, max_pwr); 00247 pwr -= GetSettings()->Network.AntennaGain; 00248 00249 for (int i = 20; i >= 0; i--) { 00250 if (RADIO_POWERS[i] <= pwr) { 00251 pwr = i; 00252 break; 00253 } 00254 if (i == 0) { 00255 pwr = i; 00256 } 00257 } 00258 00259 logDebug("Session pwr: %d ant: %d max: %d", GetSettings()->Session.TxPower, GetSettings()->Network.AntennaGain, max_pwr); 00260 logDebug("Radio Power index: %d output: %d total: %d", pwr, RADIO_POWERS[pwr], RADIO_POWERS[pwr] + GetSettings()->Network.AntennaGain); 00261 00262 uint32_t bw = txDr.Bandwidth; 00263 uint32_t sf = txDr.SpreadingFactor; 00264 uint8_t cr = txDr.Coderate; 00265 uint8_t pl = txDr.PreambleLength; 00266 uint16_t fdev = 0; 00267 bool crc = txDr.Crc; 00268 bool iq = txDr.TxIQ; 00269 00270 if (GetSettings()->Network.DisableCRC == true) 00271 crc = false; 00272 00273 SxRadio::RadioModems_t modem = SxRadio::MODEM_LORA; 00274 00275 if (sf == SF_FSK) { 00276 modem = SxRadio::MODEM_FSK; 00277 sf = 50e3; 00278 fdev = 25e3; 00279 bw = 0; 00280 } 00281 00282 GetRadio()->SetTxConfig(modem, pwr, fdev, bw, sf, cr, pl, false, crc, false, 0, iq, 3e3); 00283 00284 logDebug("TX PWR: %u DR: %u SF: %u BW: %u CR: %u PL: %u CRC: %d IQ: %d", pwr, txDr.Index, sf, bw, cr, pl, crc, iq); 00285 00286 return LORA_OK; 00287 } 00288 00289 uint8_t ChannelPlan_RU864::SetRxConfig(uint8_t window, bool continuous, uint16_t wnd_growth) { 00290 00291 RxWindow rxw = GetRxWindow(window); 00292 00293 if (_dlChannels[_txChannel].Frequency != 0 && window == 1) 00294 GetRadio()->SetChannel(_dlChannels[_txChannel].Frequency); 00295 else 00296 GetRadio()->SetChannel(rxw.Frequency); 00297 00298 Datarate rxDr = GetDatarate(rxw.DatarateIndex); 00299 uint32_t bw = rxDr.Bandwidth; 00300 uint32_t sf = rxDr.SpreadingFactor; 00301 uint8_t cr = rxDr.Coderate; 00302 uint8_t pl = rxDr.PreambleLength; 00303 uint16_t sto = rxDr.SymbolTimeout() * wnd_growth; 00304 uint32_t afc = 0; 00305 bool fixLen = false; 00306 uint8_t payloadLen = 0U; 00307 bool crc = false; // downlink does not use CRC according to LORAWAN 00308 00309 if (GetSettings()->Network.DisableCRC == true) 00310 crc = false; 00311 00312 Datarate txDr = GetDatarate(GetSettings()->Session.TxDatarate); 00313 bool iq = txDr.RxIQ; 00314 00315 if (P2PEnabled()) { 00316 iq = txDr.TxIQ; 00317 } 00318 00319 // Beacon modifications - no I/Q inversion, fixed length rx, preamble 00320 if (window == RX_BEACON) { 00321 iq = txDr.TxIQ; 00322 fixLen = true; 00323 payloadLen = sizeof(BCNPayload); 00324 pl = BEACON_PREAMBLE_LENGTH; 00325 } 00326 00327 SxRadio::RadioModems_t modem = SxRadio::MODEM_LORA; 00328 00329 if (sf == SF_FSK) { 00330 modem = SxRadio::MODEM_FSK; 00331 sf = 50e3; 00332 cr = 0; 00333 bw = 0; 00334 afc = 83333; 00335 iq = false; 00336 crc = true; // FSK must use CRC 00337 } 00338 00339 // Disable printf's to actually receive packets, printing to debug may mess up the timing 00340 // logTrace("Configure radio for RX%d on freq: %lu", window, rxw.Frequency); 00341 // logTrace("RX SF: %u BW: %u CR: %u PL: %u STO: %u CRC: %d IQ: %d", sf, bw, cr, pl, sto, crc, iq); 00342 00343 GetRadio()->SetRxConfig(modem, bw, sf, cr, afc, pl, sto, fixLen, payloadLen, crc, false, 0, iq, continuous); 00344 00345 return LORA_OK; 00346 } 00347 00348 Channel ChannelPlan_RU864::GetChannel(int8_t index) { 00349 Channel chan; 00350 memset(&chan, 0, sizeof(Channel)); 00351 00352 chan = _channels[index]; 00353 00354 return chan; 00355 } 00356 00357 uint8_t ChannelPlan_RU864::SetFrequencySubBand(uint8_t sub_band) { 00358 return LORA_OK; 00359 } 00360 00361 void ChannelPlan_RU864::LogRxWindow(uint8_t wnd) { 00362 00363 RxWindow rxw = GetRxWindow(wnd); 00364 Datarate rxDr = GetDatarate(rxw.DatarateIndex); 00365 uint8_t bw = rxDr.Bandwidth; 00366 uint8_t sf = rxDr.SpreadingFactor; 00367 uint8_t cr = rxDr.Coderate; 00368 uint8_t pl = rxDr.PreambleLength; 00369 uint16_t sto = rxDr.SymbolTimeout(); 00370 bool crc = false; // downlink does not use CRC according to LORAWAN 00371 bool iq = GetTxDatarate().RxIQ; 00372 uint32_t freq = rxw.Frequency; 00373 00374 if (wnd == 1 && _dlChannels[_txChannel].Frequency != 0) 00375 freq = _dlChannels[_txChannel].Frequency; 00376 00377 logTrace("RX%d on freq: %lu", wnd, freq); 00378 logTrace("RX DR: %u SF: %u BW: %u CR: %u PL: %u STO: %u CRC: %d IQ: %d", rxDr.Index, sf, bw, cr, pl, sto, crc, iq); 00379 } 00380 00381 RxWindow ChannelPlan_RU864::GetRxWindow(uint8_t window) { 00382 RxWindow rxw; 00383 int index = 0; 00384 00385 if (P2PEnabled()) { 00386 rxw.Frequency = GetSettings()->Network.TxFrequency; 00387 index = GetSettings()->Session.TxDatarate; 00388 } else { 00389 switch (window) { 00390 case RX_1: 00391 rxw.Frequency = _channels[_txChannel].Frequency; 00392 00393 if (GetSettings()->Session.TxDatarate > GetSettings()->Session.Rx1DatarateOffset) { 00394 index = GetSettings()->Session.TxDatarate - GetSettings()->Session.Rx1DatarateOffset; 00395 } else { 00396 index = 0; 00397 } 00398 00399 break; 00400 00401 case RX_BEACON: 00402 rxw.Frequency = GetSettings()->Session.BeaconFrequency; 00403 index = RU864_BEACON_DR; 00404 break; 00405 00406 case RX_SLOT: 00407 rxw.Frequency = GetSettings()->Session.PingSlotFrequency; 00408 index = GetSettings()->Session.PingSlotDatarateIndex; 00409 break; 00410 00411 // RX2, RXC, RX_TEST, etc.. 00412 default: 00413 rxw.Frequency = GetSettings()->Session.Rx2Frequency; 00414 index = GetSettings()->Session.Rx2DatarateIndex; 00415 } 00416 } 00417 00418 rxw.DatarateIndex = index; 00419 00420 return rxw; 00421 } 00422 00423 uint8_t ChannelPlan_RU864::HandleRxParamSetup(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) { 00424 status = 0x07; 00425 int8_t datarate = 0; 00426 int8_t drOffset = 0; 00427 uint32_t freq = 0; 00428 00429 drOffset = payload[index++]; 00430 datarate = drOffset & 0x0F; 00431 drOffset = (drOffset >> 4) & 0x07; 00432 00433 freq = payload[index++]; 00434 freq |= payload[index++] << 8; 00435 freq |= payload[index++] << 16; 00436 freq *= 100; 00437 00438 if (!CheckRfFrequency(freq)) { 00439 logInfo("Freq KO"); 00440 status &= 0xFE; // Channel frequency KO 00441 } 00442 00443 if (datarate < _minRx2Datarate || datarate > _maxRx2Datarate) { 00444 logInfo("DR KO"); 00445 status &= 0xFD; // Datarate KO 00446 } 00447 00448 if (drOffset < 0 || drOffset > _maxDatarateOffset) { 00449 logInfo("DR Offset KO"); 00450 status &= 0xFB; // Rx1DrOffset range KO 00451 } 00452 00453 if ((status & 0x07) == 0x07) { 00454 logInfo("RxParamSetup accepted Rx2DR: %d Rx2Freq: %d Rx1Offset: %d", datarate, freq, drOffset); 00455 SetRx2DatarateIndex(datarate); 00456 SetRx2Frequency(freq); 00457 SetRx1Offset(drOffset); 00458 } else { 00459 logInfo("RxParamSetup rejected Rx2DR: %d Rx2Freq: %d Rx1Offset: %d", datarate, freq, drOffset); 00460 } 00461 00462 return LORA_OK; 00463 } 00464 00465 uint8_t ChannelPlan_RU864::HandleNewChannel(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) { 00466 00467 status = 0x03; 00468 uint8_t channelIndex = 0; 00469 Channel chParam; 00470 00471 channelIndex = payload[index++]; 00472 lora::CopyFreqtoInt(payload + index, chParam.Frequency); 00473 index += 3; 00474 chParam.DrRange.Value = payload[index++]; 00475 00476 if (channelIndex < 3 || channelIndex > _channels.size() - 1) { 00477 logError("New Channel index KO"); 00478 status &= 0xFE; // Channel index KO 00479 } 00480 00481 if (chParam.Frequency == 0) { 00482 chParam.DrRange.Value = 0; 00483 } else if (chParam.Frequency < _minFrequency || chParam.Frequency > _maxFrequency) { 00484 logError("New Channel frequency KO"); 00485 status &= 0xFE; // Channel frequency KO 00486 } 00487 00488 if (chParam.DrRange.Fields.Min > chParam.DrRange.Fields.Max && chParam.Frequency != 0) { 00489 logError("New Channel datarate min/max KO"); 00490 status &= 0xFD; // Datarate range KO 00491 } else if ((chParam.DrRange.Fields.Min < _minDatarate || chParam.DrRange.Fields.Min > _maxDatarate) && 00492 chParam.Frequency != 0) { 00493 logError("New Channel datarate min KO"); 00494 status &= 0xFD; // Datarate range KO 00495 } else if ((chParam.DrRange.Fields.Max < _minDatarate || chParam.DrRange.Fields.Max > _maxDatarate) && 00496 chParam.Frequency != 0) { 00497 logError("New Channel datarate max KO"); 00498 status &= 0xFD; // Datarate range KO 00499 } 00500 00501 if ((status & 0x03) == 0x03) { 00502 logInfo("New Channel accepted index: %d freq: %lu drRange: %02x", channelIndex, chParam.Frequency, chParam.DrRange.Value); 00503 AddChannel(channelIndex, chParam); 00504 SetChannelMask(0, _channelMask[0] | 1 << (channelIndex)); 00505 } 00506 00507 return LORA_OK; 00508 } 00509 00510 uint8_t ChannelPlan_RU864::HandlePingSlotChannelReq(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) { 00511 uint8_t datarate = 0; 00512 uint32_t freq = 0; 00513 00514 status = 0x03; 00515 00516 freq = payload[index++]; 00517 freq |= payload[index++] << 8; 00518 freq |= payload[index++] << 16; 00519 freq *= 100; 00520 00521 datarate = payload[index] & 0x0F; 00522 00523 if (freq == 0U) { 00524 logInfo("Received request to reset ping slot frequency to default"); 00525 freq = RU864_PING_SLOT_FREQ; 00526 } else if (!CheckRfFrequency(freq)) { 00527 logInfo("Freq KO"); 00528 status &= 0xFE; // Channel frequency KO 00529 } 00530 00531 if (datarate < _minRx2Datarate || datarate > _maxRx2Datarate) { 00532 logInfo("DR KO"); 00533 status &= 0xFD; // Datarate KO 00534 } 00535 00536 if ((status & 0x03) == 0x03) { 00537 logInfo("PingSlotChannelReq accepted DR: %d Freq: %d", datarate, freq); 00538 GetSettings()->Session.PingSlotFrequency = freq; 00539 GetSettings()->Session.PingSlotDatarateIndex = datarate; 00540 } else { 00541 logInfo("PingSlotChannelReq rejected DR: %d Freq: %d", datarate, freq); 00542 } 00543 00544 return LORA_OK; 00545 } 00546 00547 uint8_t ChannelPlan_RU864::HandleBeaconFrequencyReq(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) 00548 { 00549 uint32_t freq = 0; 00550 00551 status = 0x01; 00552 00553 freq = payload[index++]; 00554 freq |= payload[index++] << 8; 00555 freq |= payload[index] << 16; 00556 freq *= 100; 00557 00558 if (freq == 0U) { 00559 logInfo("Received request to reset beacon frequency to default"); 00560 freq = RU864_BEACON_FREQ; 00561 } else if (!CheckRfFrequency(freq)) { 00562 logInfo("Freq KO"); 00563 status &= 0xFE; // Channel frequency KO 00564 } 00565 00566 if (status & 0x01) { 00567 logInfo("BeaconFrequencyReq accepted Freq: %d", freq); 00568 GetSettings()->Session.BeaconFrequency = freq; 00569 } else { 00570 logInfo("BeaconFrequencyReq rejected Freq: %d", freq); 00571 } 00572 00573 return LORA_OK; 00574 } 00575 00576 uint8_t ChannelPlan_RU864::HandleAdrCommand(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) { 00577 00578 uint8_t power = 0; 00579 uint8_t datarate = 0; 00580 uint16_t mask = 0; 00581 uint16_t new_mask = 0; 00582 uint8_t ctrl = 0; 00583 uint8_t nbRep = 0; 00584 00585 status = 0x07; 00586 datarate = payload[index++]; 00587 power = datarate & 0x0F; 00588 datarate = (datarate >> 4) & 0x0F; 00589 00590 mask = payload[index++]; 00591 mask |= payload[index++] << 8; 00592 00593 nbRep = payload[index++]; 00594 ctrl = (nbRep >> 4) & 0x07; 00595 nbRep &= 0x0F; 00596 00597 if (nbRep == 0) { 00598 nbRep = 1; 00599 } 00600 00601 if (datarate > _maxDatarate) { 00602 status &= 0xFD; // Datarate KO 00603 } 00604 // 00605 // Remark MaxTxPower = 0 and MinTxPower = 7 00606 // 00607 if (power > 7) { 00608 status &= 0xFB; // TxPower KO 00609 } 00610 00611 switch (ctrl) { 00612 case 0: 00613 SetChannelMask(0, mask); 00614 break; 00615 00616 case 6: 00617 // enable all currently defined channels 00618 // set bits 0 - N of a number by (2<<N)-1 00619 new_mask = (1 << _channels.size()) - 1; 00620 SetChannelMask(0, new_mask); 00621 break; 00622 00623 default: 00624 logWarning("rejecting RFU or unknown control value %d", ctrl); 00625 status &= 0xFE; // ChannelMask KO 00626 return LORA_ERROR; 00627 } 00628 00629 if (GetSettings()->Network.ADREnabled) { 00630 GetSettings()->Session.TxDatarate = datarate; 00631 GetSettings()->Session.TxPower = TX_POWERS[power]; 00632 GetSettings()->Session.Redundancy = nbRep; 00633 } else { 00634 logDebug("ADR is disabled, DR and Power not changed."); 00635 status &= 0xFB; // TxPower KO 00636 status &= 0xFD; // Datarate KO 00637 } 00638 00639 logDebug("ADR DR: %u PWR: %u Ctrl: %02x Mask: %04x NbRep: %u Stat: %02x", datarate, power, ctrl, mask, nbRep, status); 00640 00641 return LORA_OK; 00642 } 00643 00644 uint8_t ChannelPlan_RU864::ValidateAdrConfiguration() { 00645 uint8_t status = 0x07; 00646 uint8_t datarate = GetSettings()->Session.TxDatarate; 00647 uint8_t power = GetSettings()->Session.TxPower; 00648 00649 if (!GetSettings()->Network.ADREnabled) { 00650 logDebug("ADR disabled - no applied changes to validate"); 00651 return status; 00652 } 00653 00654 if (datarate > _maxDatarate) { 00655 logWarning("ADR Datarate KO - outside allowed range"); 00656 status &= 0xFD; // Datarate KO 00657 } 00658 if (power < _minTxPower || power > _maxTxPower) { 00659 logWarning("ADR TX Power KO - outside allowed range"); 00660 status &= 0xFB; // TxPower KO 00661 } 00662 00663 // mask must not contain any undefined channels 00664 for (int i = 3; i < 16; i++) { 00665 if ((_channelMask[0] & (1 << i)) && (_channels[i].Frequency == 0)) { 00666 logWarning("ADR Channel Mask KO - cannot enable undefined channel"); 00667 status &= 0xFE; // ChannelMask KO 00668 break; 00669 } 00670 } 00671 00672 return status; 00673 } 00674 00675 uint8_t ChannelPlan_RU864::HandleAckTimeout() { 00676 00677 if (!GetSettings()->Network.ADREnabled) { 00678 return LORA_ADR_OFF; 00679 } 00680 00681 if ((++(GetSettings()->Session.AckCounter) % 2) == 0) { 00682 if (GetSettings()->Session.TxPower < GetSettings()->Network.TxPowerMax) { 00683 logTrace("ADR Setting power to maximum"); 00684 GetSettings()->Session.TxPower = GetSettings()->Network.TxPowerMax; 00685 } else if (GetSettings()->Session.TxDatarate > 0) { 00686 logTrace("ADR Lowering datarate"); 00687 (GetSettings()->Session.TxDatarate)--; 00688 } 00689 } 00690 00691 return LORA_OK; 00692 } 00693 00694 00695 uint32_t ChannelPlan_RU864::GetTimeOffAir() 00696 { 00697 if (GetSettings()->Test.DisableDutyCycle == lora::ON || _LBT_TimeUs > 0) 00698 return 0; 00699 00700 uint32_t min = 0; 00701 uint32_t now = _dutyCycleTimer.read_ms(); 00702 00703 00704 min = UINT_MAX; 00705 int8_t band = 0; 00706 00707 if (P2PEnabled()) { 00708 int8_t band = GetDutyBand(GetSettings()->Network.TxFrequency); 00709 if (_dutyBands[band].TimeOffEnd > now) { 00710 min = _dutyBands[band].TimeOffEnd - now; 00711 } else { 00712 min = 0; 00713 } 00714 } else { 00715 for (size_t i = 0; i < _channels.size(); i++) { 00716 if (IsChannelEnabled(i) && GetChannel(i).Frequency != 0 && 00717 !(GetSettings()->Session.TxDatarate < GetChannel(i).DrRange.Fields.Min || 00718 GetSettings()->Session.TxDatarate > GetChannel(i).DrRange.Fields.Max)) { 00719 00720 band = GetDutyBand(GetChannel(i).Frequency); 00721 if (band != -1) { 00722 // logDebug("band: %d time-off: %d now: %d", band, _dutyBands[band].TimeOffEnd, now); 00723 if (_dutyBands[band].TimeOffEnd > now) { 00724 min = std::min < uint32_t > (min, _dutyBands[band].TimeOffEnd - now); 00725 } else { 00726 min = 0; 00727 break; 00728 } 00729 } 00730 } 00731 } 00732 } 00733 00734 00735 if (GetSettings()->Session.AggregatedTimeOffEnd > 0 && GetSettings()->Session.AggregatedTimeOffEnd > now) { 00736 min = std::max < uint32_t > (min, GetSettings()->Session.AggregatedTimeOffEnd - now); 00737 } 00738 00739 now = time(NULL); 00740 uint32_t join_time = 0; 00741 00742 if (GetSettings()->Session.JoinFirstAttempt != 0 && now < GetSettings()->Session.JoinTimeOffEnd) { 00743 join_time = (GetSettings()->Session.JoinTimeOffEnd - now) * 1000; 00744 } 00745 00746 min = std::max < uint32_t > (join_time, min); 00747 00748 return min; 00749 } 00750 00751 00752 void ChannelPlan_RU864::UpdateDutyCycle(uint32_t freq, uint32_t time_on_air_ms) { 00753 if (GetSettings()->Test.DisableDutyCycle == lora::ON || _LBT_TimeUs > 0) { 00754 _dutyCycleTimer.stop(); 00755 for (size_t i = 0; i < _dutyBands.size(); i++) { 00756 _dutyBands[i].TimeOffEnd = 0; 00757 } 00758 return; 00759 } 00760 00761 _dutyCycleTimer.start(); 00762 00763 if (GetSettings()->Session.MaxDutyCycle > 0 && GetSettings()->Session.MaxDutyCycle <= 15) { 00764 GetSettings()->Session.AggregatedTimeOffEnd = _dutyCycleTimer.read_ms() + time_on_air_ms * GetSettings()->Session.AggregateDutyCycle; 00765 logDebug("Updated Aggregate DCycle Time-off: %lu DC: %f%%", GetSettings()->Session.AggregatedTimeOffEnd, 1 / float(GetSettings()->Session.AggregateDutyCycle)); 00766 } else { 00767 GetSettings()->Session.AggregatedTimeOffEnd = 0; 00768 } 00769 00770 00771 uint32_t time_off_air = 0; 00772 uint32_t now = _dutyCycleTimer.read_ms(); 00773 00774 for (size_t i = 0; i < _dutyBands.size(); i++) { 00775 if (_dutyBands[i].TimeOffEnd < now) { 00776 _dutyBands[i].TimeOffEnd = 0; 00777 } else { 00778 _dutyBands[i].TimeOffEnd -= now; 00779 } 00780 00781 if (freq >= _dutyBands[i].FrequencyMin && freq <= _dutyBands[i].FrequencyMax) { 00782 logDebug("update TOE: freq: %d i:%d toa: %d DC:%d", freq, i, time_on_air_ms, _dutyBands[i].DutyCycle); 00783 time_off_air = time_on_air_ms * _dutyBands[i].DutyCycle; 00784 _dutyBands[i].TimeOffEnd = time_off_air; 00785 } 00786 } 00787 00788 ResetDutyCycleTimer(); 00789 } 00790 00791 std::vector<uint32_t> lora::ChannelPlan_RU864::GetChannels() { 00792 std::vector < uint32_t > chans; 00793 00794 for (int8_t i = 0; i < (int) _channels.size(); i++) { 00795 chans.push_back(_channels[i].Frequency); 00796 } 00797 chans.push_back(GetRxWindow(2).Frequency); 00798 00799 return chans; 00800 } 00801 00802 std::vector<uint8_t> lora::ChannelPlan_RU864::GetChannelRanges() { 00803 std::vector < uint8_t > ranges; 00804 00805 for (int8_t i = 0; i < (int) _channels.size(); i++) { 00806 ranges.push_back(_channels[i].DrRange.Value); 00807 } 00808 00809 ranges.push_back(GetRxWindow(2).DatarateIndex); 00810 00811 return ranges; 00812 00813 } 00814 00815 void lora::ChannelPlan_RU864::EnableDefaultChannels() { 00816 _channelMask[0] |= 0x0007; 00817 } 00818 00819 uint8_t ChannelPlan_RU864::GetNextChannel() 00820 { 00821 if (GetSettings()->Session.AggregatedTimeOffEnd != 0) { 00822 return LORA_AGGREGATED_DUTY_CYCLE; 00823 } 00824 00825 if (P2PEnabled() || GetSettings()->Network.TxFrequency != 0) { 00826 logDebug("Using frequency %d", GetSettings()->Network.TxFrequency); 00827 00828 if (GetSettings()->Test.DisableDutyCycle != lora::ON && _LBT_TimeUs == 0) { 00829 int8_t band = GetDutyBand(GetSettings()->Network.TxFrequency); 00830 logDebug("band: %d freq: %d", band, GetSettings()->Network.TxFrequency); 00831 if (band != -1 && _dutyBands[band].TimeOffEnd != 0) { 00832 return LORA_NO_CHANS_ENABLED; 00833 } 00834 } 00835 00836 GetRadio()->SetChannel(GetSettings()->Network.TxFrequency); 00837 return LORA_OK; 00838 } 00839 00840 uint8_t start = 0; 00841 uint8_t maxChannels = _numChans125k; 00842 uint8_t nbEnabledChannels = 0; 00843 uint8_t *enabledChannels = new uint8_t[maxChannels]; 00844 00845 if (GetTxDatarate().Bandwidth == BW_500) { 00846 maxChannels = _numChans500k; 00847 start = _numChans125k; 00848 } 00849 00850 // Search how many channels are enabled 00851 DatarateRange range; 00852 uint8_t dr_index = GetSettings()->Session.TxDatarate; 00853 uint32_t now = _dutyCycleTimer.read_ms(); 00854 00855 for (size_t i = 0; i < _dutyBands.size(); i++) { 00856 if (_dutyBands[i].TimeOffEnd < now || GetSettings()->Test.DisableDutyCycle == lora::ON || _LBT_TimeUs > 0) { 00857 _dutyBands[i].TimeOffEnd = 0; 00858 } 00859 } 00860 00861 for (uint8_t i = start; i < start + maxChannels; i++) { 00862 range = GetChannel(i).DrRange; 00863 // logDebug("chan: %d freq: %d range:%02x", i, GetChannel(i).Frequency, range.Value); 00864 00865 if (IsChannelEnabled(i) && (dr_index >= range.Fields.Min && dr_index <= range.Fields.Max)) { 00866 int8_t band = GetDutyBand(GetChannel(i).Frequency); 00867 // logDebug("band: %d freq: %d", band, _channels[i].Frequency); 00868 if (band != -1 && _dutyBands[band].TimeOffEnd == 0) { 00869 enabledChannels[nbEnabledChannels++] = i; 00870 } 00871 } 00872 } 00873 00874 logTrace("Number of available channels: %d", nbEnabledChannels); 00875 00876 uint32_t freq = 0; 00877 uint8_t sf = GetTxDatarate().SpreadingFactor; 00878 uint8_t bw = GetTxDatarate().Bandwidth; 00879 int16_t thres = DEFAULT_FREE_CHAN_RSSI_THRESHOLD; 00880 00881 if (nbEnabledChannels == 0) { 00882 delete [] enabledChannels; 00883 return LORA_NO_CHANS_ENABLED; 00884 } 00885 00886 00887 if (GetSettings()->Network.CADEnabled) { 00888 // Search for free channel with ms timeout 00889 int16_t timeout = 10000; 00890 Timer tmr; 00891 tmr.start(); 00892 00893 for (uint8_t j = rand_r(0, nbEnabledChannels - 1); tmr.read_ms() < timeout; j++) { 00894 freq = GetChannel(enabledChannels[j]).Frequency; 00895 00896 if (GetRadio()->IsChannelFree(SxRadio::MODEM_LORA, freq, sf, thres, bw)) { 00897 _txChannel = enabledChannels[j]; 00898 break; 00899 } 00900 } 00901 } else { 00902 uint8_t j = rand_r(0, nbEnabledChannels - 1); 00903 _txChannel = enabledChannels[j]; 00904 freq = GetChannel(_txChannel).Frequency; 00905 } 00906 00907 assert(freq != 0); 00908 00909 logDebug("Using channel %d : %d", _txChannel, freq); 00910 GetRadio()->SetChannel(freq); 00911 00912 00913 delete [] enabledChannels; 00914 return LORA_OK; 00915 } 00916 00917 00918 uint8_t lora::ChannelPlan_RU864::GetJoinDatarate() { 00919 uint8_t dr = GetSettings()->Session.TxDatarate; 00920 static uint8_t cnt = 0; 00921 00922 if (GetSettings()->Test.DisableRandomJoinDatarate == lora::OFF) { 00923 if ((cnt++ % 20) == 0) { 00924 dr = lora::DR_0; 00925 } else if ((cnt % 16) == 0) { 00926 dr = lora::DR_1; 00927 } else if ((cnt % 12) == 0) { 00928 dr = lora::DR_2; 00929 } else if ((cnt % 8) == 0) { 00930 dr = lora::DR_3; 00931 } else if ((cnt % 4) == 0) { 00932 dr = lora::DR_4; 00933 } else { 00934 dr = lora::DR_5; 00935 } 00936 } 00937 00938 return dr; 00939 } 00940 00941 uint8_t ChannelPlan_RU864::CalculateJoinBackoff(uint8_t size) { 00942 00943 time_t now = time(NULL); 00944 uint32_t time_on_max = 0; 00945 static uint32_t time_off_max = 15; 00946 uint32_t rand_time_off = 0; 00947 00948 // TODO: calc time-off-max based on RTC time from JoinFirstAttempt, time-off-max is lost over sleep 00949 00950 if ((time_t)GetSettings()->Session.JoinTimeOffEnd > now) { 00951 return LORA_JOIN_BACKOFF; 00952 } 00953 00954 uint32_t secs_since_first_attempt = (now - GetSettings()->Session.JoinFirstAttempt); 00955 uint16_t hours_since_first_attempt = secs_since_first_attempt / (60 * 60); 00956 00957 static uint8_t join_cnt = 0; 00958 00959 join_cnt = (join_cnt+1) % 8; 00960 00961 if (GetSettings()->Session.JoinFirstAttempt == 0) { 00962 /* 1 % duty-cycle for first hour 00963 * 0.1 % next 10 hours 00964 * 0.01 % upto 24 hours */ 00965 GetSettings()->Session.JoinFirstAttempt = now; 00966 GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size); 00967 GetSettings()->Session.JoinTimeOffEnd = now + (GetTimeOnAir(size) / 10); 00968 } else if (join_cnt == 0) { 00969 if (hours_since_first_attempt < 1) { 00970 time_on_max = 36000; 00971 rand_time_off = rand_r(time_off_max - 1, time_off_max + 1); 00972 // time off max 1 hour 00973 time_off_max = std::min < uint32_t > (time_off_max * 2, 60 * 60); 00974 00975 if (GetSettings()->Session.JoinTimeOnAir < time_on_max) { 00976 GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size); 00977 GetSettings()->Session.JoinTimeOffEnd = now + rand_time_off; 00978 } else { 00979 logWarning("Max time-on-air limit met for current join backoff period"); 00980 GetSettings()->Session.JoinTimeOffEnd = GetSettings()->Session.JoinFirstAttempt + 60 * 60; 00981 } 00982 } else if (hours_since_first_attempt < 11) { 00983 if (GetSettings()->Session.JoinTimeOnAir < 36000) { 00984 GetSettings()->Session.JoinTimeOnAir = 36000; 00985 } 00986 time_on_max = 72000; 00987 rand_time_off = rand_r(time_off_max - 1, time_off_max + 1); 00988 // time off max 1 hour 00989 time_off_max = std::min < uint32_t > (time_off_max * 2, 60 * 60); 00990 00991 if (GetSettings()->Session.JoinTimeOnAir < time_on_max) { 00992 GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size); 00993 GetSettings()->Session.JoinTimeOffEnd = now + rand_time_off; 00994 } else { 00995 logWarning("Max time-on-air limit met for current join backoff period"); 00996 GetSettings()->Session.JoinTimeOffEnd = GetSettings()->Session.JoinFirstAttempt + 11 * 60 * 60; 00997 } 00998 } else { 00999 if (GetSettings()->Session.JoinTimeOnAir < 72000) { 01000 GetSettings()->Session.JoinTimeOnAir = 72000; 01001 } 01002 uint32_t join_time = 1200; 01003 01004 time_on_max = 80700; 01005 time_off_max = 1 * 60 * 60; // 1 hour 01006 rand_time_off = rand_r(time_off_max - 1, time_off_max + 1); 01007 01008 // allow one final join attempt as long as it doesn't start past the max time on air 01009 if (GetSettings()->Session.JoinTimeOnAir < time_on_max - join_time) { 01010 GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size); 01011 GetSettings()->Session.JoinTimeOffEnd = now + rand_time_off; 01012 } else { 01013 logWarning("Max time-on-air limit met for current join backoff period"); 01014 // Reset the join time on air and set end of restriction to the next 24 hour period 01015 GetSettings()->Session.JoinTimeOnAir = 72000; 01016 uint16_t days = (now - GetSettings()->Session.JoinFirstAttempt) / (24 * 60 * 60) + 1; 01017 logWarning("days : %d", days); 01018 GetSettings()->Session.JoinTimeOffEnd = GetSettings()->Session.JoinFirstAttempt + ((days * 24) + 11) * 60 * 60; 01019 } 01020 } 01021 01022 logWarning("JoinBackoff: %lu seconds Time On Air: %lu / %lu", GetSettings()->Session.JoinTimeOffEnd - now, GetSettings()->Session.JoinTimeOnAir, time_on_max); 01023 } else { 01024 GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size); 01025 GetSettings()->Session.JoinTimeOffEnd = now + (GetTimeOnAir(size) / 10); 01026 } 01027 01028 return LORA_OK; 01029 } 01030 01031 bool ChannelPlan_RU864::DecodeBeacon(const uint8_t* payload, size_t size, BeaconData_t& data) { 01032 uint16_t crc1, crc1_rx, crc2, crc2_rx; 01033 const BCNPayload* beacon = (const BCNPayload*)payload; 01034 01035 // First check the size of the packet 01036 if (size != sizeof(BCNPayload)) 01037 return false; 01038 01039 // Next we verify the CRCs are correct 01040 crc1 = CRC16(beacon->RFU, sizeof(beacon->RFU) + sizeof(beacon->Time)); 01041 memcpy((uint8_t*)&crc1_rx, beacon->CRC1, sizeof(uint16_t)); 01042 01043 if (crc1 != crc1_rx) 01044 return false; 01045 01046 crc2 = CRC16(beacon->GwSpecific, sizeof(beacon->GwSpecific)); 01047 memcpy((uint8_t*)&crc2_rx, beacon->CRC2, sizeof(uint16_t)); 01048 01049 if (crc2 != crc2_rx) 01050 return false; 01051 01052 // Now that we have confirmed this packet is a beacon, parse and complete the output struct 01053 memcpy(&data.Time, beacon->Time, sizeof(beacon->Time)); 01054 data.InfoDesc = beacon->GwSpecific[0]; 01055 01056 // Update the GPS fields if we have a gps info descriptor 01057 if (data.InfoDesc == GPS_FIRST_ANTENNA || 01058 data.InfoDesc == GPS_SECOND_ANTENNA || 01059 data.InfoDesc == GPS_THIRD_ANTENNA) { 01060 // Latitude and Longitude 3 bytes in length 01061 memcpy(&data.Latitude, &beacon->GwSpecific[1], 3); 01062 memcpy(&data.Longitude, &beacon->GwSpecific[4], 3); 01063 } 01064 01065 return true; 01066 }
Generated on Tue Jul 12 2022 18:19:47 by
1.7.2


L-Tek FF1705