Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

626 lines
18 KiB

  1. /*!
  2. * \file RegionCommon.h
  3. *
  4. * \brief Region independent implementations which are common to all regions.
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * ___ _____ _ ___ _ _____ ___ ___ ___ ___
  18. * / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
  19. * \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
  20. * |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
  21. * embedded.connectivity.solutions===============
  22. *
  23. * \endcode
  24. *
  25. * \author Miguel Luis ( Semtech )
  26. *
  27. * \author Gregory Cristian ( Semtech )
  28. *
  29. * \author Daniel Jaeckle ( STACKFORCE )
  30. *
  31. * \author Johannes Bruder ( STACKFORCE )
  32. *
  33. * \defgroup REGIONCOMMON Common region implementation
  34. * Region independent implementations which are common to all regions.
  35. * \{
  36. */
  37. #ifndef __REGIONCOMMON_H__
  38. #define __REGIONCOMMON_H__
  39. #ifdef __cplusplus
  40. extern "C"
  41. {
  42. #endif
  43. #include "LoRaMacTypes.h"
  44. #include "LoRaMacHeaderTypes.h"
  45. #include "region/Region.h"
  46. // Constants that are common to all the regions.
  47. /*!
  48. * Receive delay of 1 second.
  49. */
  50. #define REGION_COMMON_DEFAULT_RECEIVE_DELAY1 1000
  51. /*!
  52. * Receive delay of 2 seconds.
  53. */
  54. #define REGION_COMMON_DEFAULT_RECEIVE_DELAY2 ( REGION_COMMON_DEFAULT_RECEIVE_DELAY1 + 1000 )
  55. /*!
  56. * Join accept delay of 5 seconds.
  57. */
  58. #define REGION_COMMON_DEFAULT_JOIN_ACCEPT_DELAY1 5000
  59. /*!
  60. * Join accept delay of 6 seconds.
  61. */
  62. #define REGION_COMMON_DEFAULT_JOIN_ACCEPT_DELAY2 ( REGION_COMMON_DEFAULT_JOIN_ACCEPT_DELAY1 + 1000 )
  63. /*!
  64. * ADR ack limit.
  65. */
  66. #define REGION_COMMON_DEFAULT_ADR_ACK_LIMIT 64
  67. /*!
  68. * ADR ack delay.
  69. */
  70. #define REGION_COMMON_DEFAULT_ADR_ACK_DELAY 32
  71. /*!
  72. * Retransmission timeout for ACK in milliseconds.
  73. */
  74. #define REGION_COMMON_DEFAULT_RETRANSMIT_TIMEOUT 2000
  75. /*!
  76. * Rounding limit for generating random retransmission timeout for ACK.
  77. * In milliseconds.
  78. */
  79. #define REGION_COMMON_DEFAULT_RETRANSMIT_TIMEOUT_RND 1000
  80. /*!
  81. * Default Rx1 receive datarate offset
  82. */
  83. #define REGION_COMMON_DEFAULT_RX1_DR_OFFSET 0
  84. /*!
  85. * Default downlink dwell time configuration
  86. */
  87. #define REGION_COMMON_DEFAULT_DOWNLINK_DWELL_TIME 0
  88. /*!
  89. * Default ping slots periodicity
  90. *
  91. * Periodicity is equal to 2^REGION_COMMON_DEFAULT_PING_SLOT_PERIODICITY seconds.
  92. * Example: 2^7 = 128 seconds. The end-device will open an Rx slot every 128 seconds.
  93. */
  94. #define REGION_COMMON_DEFAULT_PING_SLOT_PERIODICITY 7
  95. /*!
  96. * Default reponse timeout for class b and class c confirmed
  97. * downlink frames in milli seconds.
  98. *
  99. * The value shall not be smaller than RETRANSMIT_TIMEOUT plus
  100. * the maximum time on air.
  101. */
  102. #define REGION_COMMON_CLASS_B_C_RESP_TIMEOUT 8000
  103. typedef struct sRegionCommonLinkAdrParams
  104. {
  105. /*!
  106. * Number of repetitions.
  107. */
  108. uint8_t NbRep;
  109. /*!
  110. * Datarate.
  111. */
  112. int8_t Datarate;
  113. /*!
  114. * Tx power.
  115. */
  116. int8_t TxPower;
  117. /*!
  118. * Channels mask control field.
  119. */
  120. uint8_t ChMaskCtrl;
  121. /*!
  122. * Channels mask field.
  123. */
  124. uint16_t ChMask;
  125. }RegionCommonLinkAdrParams_t;
  126. typedef struct sRegionCommonLinkAdrReqVerifyParams
  127. {
  128. /*!
  129. * LoRaWAN specification Version
  130. */
  131. Version_t Version;
  132. /*!
  133. * The current status of the AdrLinkRequest.
  134. */
  135. uint8_t Status;
  136. /*!
  137. * Set to true, if ADR is enabled.
  138. */
  139. bool AdrEnabled;
  140. /*!
  141. * The datarate the AdrLinkRequest wants to set.
  142. */
  143. int8_t Datarate;
  144. /*!
  145. * The TX power the AdrLinkRequest wants to set.
  146. */
  147. int8_t TxPower;
  148. /*!
  149. * The number of repetitions the AdrLinkRequest wants to set.
  150. */
  151. uint8_t NbRep;
  152. /*!
  153. * The current datarate the node is using.
  154. */
  155. int8_t CurrentDatarate;
  156. /*!
  157. * The current TX power the node is using.
  158. */
  159. int8_t CurrentTxPower;
  160. /*!
  161. * The current number of repetitions the node is using.
  162. */
  163. int8_t CurrentNbRep;
  164. /*!
  165. * The number of channels.
  166. */
  167. uint8_t NbChannels;
  168. /*!
  169. * Pointer to the first element of the channels mask.
  170. */
  171. uint16_t* ChannelsMask;
  172. /*!
  173. * The minimum possible datarate.
  174. */
  175. int8_t MinDatarate;
  176. /*!
  177. * The maximum possible datarate.
  178. */
  179. int8_t MaxDatarate;
  180. /*!
  181. * Pointer to the channels.
  182. */
  183. ChannelParams_t* Channels;
  184. /*!
  185. * The minimum possible TX power.
  186. */
  187. int8_t MinTxPower;
  188. /*!
  189. * The maximum possible TX power.
  190. */
  191. int8_t MaxTxPower;
  192. }RegionCommonLinkAdrReqVerifyParams_t;
  193. typedef struct sRegionCommonRxBeaconSetupParams
  194. {
  195. /*!
  196. * A pointer to the available datarates.
  197. */
  198. const uint8_t* Datarates;
  199. /*!
  200. * Frequency
  201. */
  202. uint32_t Frequency;
  203. /*!
  204. * The size of the beacon frame.
  205. */
  206. uint8_t BeaconSize;
  207. /*!
  208. * The datarate of the beacon.
  209. */
  210. uint8_t BeaconDatarate;
  211. /*!
  212. * The channel bandwidth of the beacon.
  213. */
  214. uint8_t BeaconChannelBW;
  215. /*!
  216. * The RX time.
  217. */
  218. uint32_t RxTime;
  219. /*!
  220. * The symbol timeout of the RX procedure.
  221. */
  222. uint16_t SymbolTimeout;
  223. }RegionCommonRxBeaconSetupParams_t;
  224. typedef struct sRegionCommonCountNbOfEnabledChannelsParams
  225. {
  226. /*!
  227. * Set to true, if the device is joined.
  228. */
  229. bool Joined;
  230. /*!
  231. * The datarate to count the available channels.
  232. */
  233. uint8_t Datarate;
  234. /*!
  235. * A pointer to the channels mask to verify.
  236. */
  237. uint16_t* ChannelsMask;
  238. /*!
  239. * A pointer to the channels.
  240. */
  241. ChannelParams_t* Channels;
  242. /*!
  243. * A pointer to the bands.
  244. */
  245. Band_t* Bands;
  246. /*!
  247. * The number of available channels.
  248. */
  249. uint16_t MaxNbChannels;
  250. /*!
  251. * A pointer to the bitmask containing the
  252. * join channels. Shall have the same dimension as the
  253. * ChannelsMask with a number of MaxNbChannels channels.
  254. */
  255. uint16_t* JoinChannels;
  256. }RegionCommonCountNbOfEnabledChannelsParams_t;
  257. typedef struct sRegionCommonIdentifyChannelsParam
  258. {
  259. /*!
  260. * Aggregated time-off time.
  261. */
  262. TimerTime_t AggrTimeOff;
  263. /*!
  264. * Time of the last aggregated TX.
  265. */
  266. TimerTime_t LastAggrTx;
  267. /*!
  268. * Set to true, if the duty cycle is enabled, otherwise false.
  269. */
  270. bool DutyCycleEnabled;
  271. /*!
  272. * Maximum number of bands.
  273. */
  274. uint8_t MaxBands;
  275. /*!
  276. * Elapsed time since the start of the node.
  277. */
  278. SysTime_t ElapsedTimeSinceStartUp;
  279. /*!
  280. * Joined Set to true, if the last uplink was a join request
  281. */
  282. bool LastTxIsJoinRequest;
  283. /*!
  284. * Expected time-on-air
  285. */
  286. TimerTime_t ExpectedTimeOnAir;
  287. /*!
  288. * Pointer to a structure of RegionCommonCountNbOfEnabledChannelsParams_t.
  289. */
  290. RegionCommonCountNbOfEnabledChannelsParams_t* CountNbOfEnabledChannelsParam;
  291. }RegionCommonIdentifyChannelsParam_t;
  292. typedef struct sRegionCommonSetDutyCycleParams
  293. {
  294. /*!
  295. * Duty cycle period.
  296. */
  297. TimerTime_t DutyCycleTimePeriod;
  298. /*!
  299. * Number of bands available.
  300. */
  301. uint8_t MaxBands;
  302. /*!
  303. * A pointer to the bands.
  304. */
  305. Band_t* Bands;
  306. }RegionCommonSetDutyCycleParams_t;
  307. typedef struct sRegionCommonGetNextLowerTxDrParams
  308. {
  309. int8_t CurrentDr;
  310. int8_t MaxDr;
  311. int8_t MinDr;
  312. uint8_t NbChannels;
  313. uint16_t* ChannelsMask;
  314. ChannelParams_t* Channels;
  315. }RegionCommonGetNextLowerTxDrParams_t;
  316. /*!
  317. * \brief Verifies, if a value is in a given range.
  318. * This is a generic function and valid for all regions.
  319. *
  320. * \param [IN] value Value to verify, if it is in range.
  321. *
  322. * \param [IN] min Minimum possible value.
  323. *
  324. * \param [IN] max Maximum possible value.
  325. *
  326. * \retval Returns 1 if the value is in range, otherwise 0.
  327. */
  328. uint8_t RegionCommonValueInRange( int8_t value, int8_t min, int8_t max );
  329. /*!
  330. * \brief Verifies, if a datarate is available on an active channel.
  331. * This is a generic function and valid for all regions.
  332. *
  333. * \param [IN] nbChannels Number of channels.
  334. *
  335. * \param [IN] channelsMask The channels mask of the region.
  336. *
  337. * \param [IN] dr The datarate to verify.
  338. *
  339. * \param [IN] minDr Minimum datarate.
  340. *
  341. * \param [IN] maxDr Maximum datarate.
  342. *
  343. * \param [IN] channels The channels of the region.
  344. *
  345. * \retval Returns true if the datarate is supported, false if not.
  346. */
  347. bool RegionCommonChanVerifyDr( uint8_t nbChannels, uint16_t* channelsMask, int8_t dr,
  348. int8_t minDr, int8_t maxDr, ChannelParams_t* channels );
  349. /*!
  350. * \brief Disables a channel in a given channels mask.
  351. * This is a generic function and valid for all regions.
  352. *
  353. * \param [IN] channelsMask The channels mask of the region.
  354. *
  355. * \param [IN] id The id of the channels mask to disable.
  356. *
  357. * \param [IN] maxChannels Maximum number of channels.
  358. *
  359. * \retval Returns true if the channel could be disabled, false if not.
  360. */
  361. bool RegionCommonChanDisable( uint16_t* channelsMask, uint8_t id, uint8_t maxChannels );
  362. /*!
  363. * \brief Counts the number of active channels in a given channels mask.
  364. * This is a generic function and valid for all regions.
  365. *
  366. * \param [IN] channelsMask The channels mask of the region.
  367. *
  368. * \param [IN] startIdx Start index.
  369. *
  370. * \param [IN] stopIdx Stop index ( the channels of this index will not be counted ).
  371. *
  372. * \retval Returns the number of active channels.
  373. */
  374. uint8_t RegionCommonCountChannels( uint16_t* channelsMask, uint8_t startIdx, uint8_t stopIdx );
  375. /*!
  376. * \brief Copy a channels mask.
  377. * This is a generic function and valid for all regions.
  378. *
  379. * \param [IN] channelsMaskDest The destination channels mask.
  380. *
  381. * \param [IN] channelsMaskSrc The source channels mask.
  382. *
  383. * \param [IN] len The index length to copy.
  384. */
  385. void RegionCommonChanMaskCopy( uint16_t* channelsMaskDest, uint16_t* channelsMaskSrc, uint8_t len );
  386. /*!
  387. * \brief Sets the last tx done property.
  388. * This is a generic function and valid for all regions.
  389. *
  390. * \param [IN] band The band to be updated.
  391. *
  392. * \param [IN] lastTxAirTime The time on air of the last TX frame.
  393. *
  394. * \param [IN] joined Set to true if the device has joined.
  395. *
  396. * \param [IN] elapsedTimeSinceStartup Elapsed time since initialization.
  397. */
  398. void RegionCommonSetBandTxDone( Band_t* band, TimerTime_t lastTxAirTime, bool joined, SysTime_t elapsedTimeSinceStartup );
  399. /*!
  400. * \brief Updates the time-offs of the bands.
  401. * This is a generic function and valid for all regions.
  402. *
  403. * \param [IN] joined Set to true, if the node has joined the network
  404. *
  405. * \param [IN] bands A pointer to the bands.
  406. *
  407. * \param [IN] nbBands The number of bands available.
  408. *
  409. * \param [IN] dutyCycleEnabled Set to true, if the duty cycle is enabled.
  410. *
  411. * \param [IN] lastTxIsJoinRequest Set to true, if the last TX is a join request.
  412. *
  413. * \param [IN] elapsedTimeSinceStartup Elapsed time since start up.
  414. *
  415. * \param [IN] expectedTimeOnAir Expected time on air for the next transmission.
  416. *
  417. * \retval Returns the time which must be waited to perform the next uplink.
  418. */
  419. TimerTime_t RegionCommonUpdateBandTimeOff( bool joined, Band_t* bands,
  420. uint8_t nbBands, bool dutyCycleEnabled,
  421. bool lastTxIsJoinRequest, SysTime_t elapsedTimeSinceStartup,
  422. TimerTime_t expectedTimeOnAir );
  423. /*!
  424. * \brief Parses the parameter of an LinkAdrRequest.
  425. * This is a generic function and valid for all regions.
  426. *
  427. * \param [IN] payload Pointer to the payload containing the MAC commands. The payload
  428. * must contain the CMD identifier, following by the parameters.
  429. *
  430. * \param [OUT] parseLinkAdr The function fills the structure with the ADR parameters.
  431. *
  432. * \retval Returns the length of the ADR request, if a request was found. Otherwise, the
  433. * function returns 0.
  434. */
  435. uint8_t RegionCommonParseLinkAdrReq( uint8_t* payload, RegionCommonLinkAdrParams_t* parseLinkAdr );
  436. /*!
  437. * \brief Verifies and updates the datarate, the TX power and the number of repetitions
  438. * of a LinkAdrRequest. This depends on the configuration of ADR also.
  439. *
  440. * \param [IN] verifyParams Pointer to a structure containing input parameters.
  441. *
  442. * \param [OUT] dr The updated datarate.
  443. *
  444. * \param [OUT] txPow The updated TX power.
  445. *
  446. * \param [OUT] nbRep The updated number of repetitions.
  447. *
  448. * \retval Returns the status according to the LinkAdrRequest definition.
  449. */
  450. uint8_t RegionCommonLinkAdrReqVerifyParams( RegionCommonLinkAdrReqVerifyParams_t* verifyParams, int8_t* dr, int8_t* txPow, uint8_t* nbRep );
  451. /*!
  452. * \brief Computes the symbol time for LoRa modulation.
  453. *
  454. * \param [IN] phyDr Physical datarate to use.
  455. *
  456. * \param [IN] bandwidth Bandwidth to use.
  457. *
  458. * \retval Returns the symbol time in microseconds.
  459. */
  460. uint32_t RegionCommonComputeSymbolTimeLoRa( uint8_t phyDr, uint32_t bandwidthInHz );
  461. /*!
  462. * \brief Computes the symbol time for FSK modulation.
  463. *
  464. * \param [IN] phyDr Physical datarate to use.
  465. *
  466. * \retval Returns the symbol time in microseconds.
  467. */
  468. uint32_t RegionCommonComputeSymbolTimeFsk( uint8_t phyDrInKbps );
  469. /*!
  470. * \brief Computes the RX window timeout and the RX window offset.
  471. *
  472. * \param [IN] tSymbolInUs Symbol timeout.
  473. *
  474. * \param [IN] minRxSymbols Minimum required number of symbols to detect an Rx frame.
  475. *
  476. * \param [IN] rxErrorInMs System maximum timing error of the receiver. In milliseconds
  477. * The receiver will turn on in a [-rxErrorInMs : +rxErrorInMs] ms interval around RxOffset.
  478. *
  479. * \param [IN] wakeUpTimeInMs Wakeup time of the system.
  480. *
  481. * \param [OUT] windowTimeoutInSymbols RX window timeout.
  482. *
  483. * \param [OUT] windowOffsetInMs RX window time offset to be applied to the RX delay.
  484. */
  485. void RegionCommonComputeRxWindowParameters( uint32_t tSymbolInUs, uint8_t minRxSymbols, uint32_t rxErrorInMs, uint32_t wakeUpTimeInMs, uint32_t* windowTimeoutInSymbols, int32_t* windowOffsetInMs );
  486. /*!
  487. * \brief Computes the txPower, based on the max EIRP and the antenna gain.
  488. *
  489. * \remark US915 region uses a conducted power as input value for maxEirp.
  490. * Thus, the antennaGain parameter must be set to 0.
  491. *
  492. * \param [IN] txPower TX power index.
  493. *
  494. * \param [IN] maxEirp Maximum EIRP.
  495. *
  496. * \param [IN] antennaGain Antenna gain. Referenced to the isotropic antenna.
  497. * Value is in dBi. ( antennaGain[dBi] = measuredAntennaGain[dBd] + 2.15 )
  498. *
  499. * \retval Returns the physical TX power.
  500. */
  501. int8_t RegionCommonComputeTxPower( int8_t txPowerIndex, float maxEirp, float antennaGain );
  502. /*!
  503. * \brief Sets up the radio into RX beacon mode.
  504. *
  505. * \param [IN] rxBeaconSetupParams A pointer to the input parameters.
  506. */
  507. void RegionCommonRxBeaconSetup( RegionCommonRxBeaconSetupParams_t* rxBeaconSetupParams );
  508. /*!
  509. * \brief Counts the number of enabled channels.
  510. *
  511. * \param [IN] countNbOfEnabledChannelsParams A pointer to the input parameters.
  512. *
  513. * \param [OUT] enabledChannels A pointer to an array of size XX_MAX_NB_CHANNELS. The function
  514. * stores the available channels into this array.
  515. *
  516. * \param [OUT] nbEnabledChannels The number of available channels found.
  517. *
  518. * \param [OUT] nbRestrictedChannels It contains the number of channel
  519. * which are available, but restricted due to duty cycle.
  520. */
  521. void RegionCommonCountNbOfEnabledChannels( RegionCommonCountNbOfEnabledChannelsParams_t* countNbOfEnabledChannelsParams,
  522. uint8_t* enabledChannels, uint8_t* nbEnabledChannels, uint8_t* nbRestrictedChannels );
  523. /*!
  524. * \brief Identifies all channels which are available currently.
  525. *
  526. * \param [IN] identifyChannelsParam A pointer to the input parameters.
  527. *
  528. * \param [OUT] aggregatedTimeOff The new value of the aggregatedTimeOff. The function
  529. * may resets it to 0.
  530. *
  531. * \param [OUT] enabledChannels A pointer to an array of size XX_MAX_NB_CHANNELS. The function
  532. * stores the available channels into this array.
  533. *
  534. * \param [OUT] nbEnabledChannels The number of available channels found.
  535. *
  536. * \param [OUT] nbRestrictedChannels It contains the number of channel
  537. * which are available, but restricted due to duty cycle.
  538. *
  539. * \param [OUT] nextTxDelay Holds the time which has to be waited for the next possible
  540. * uplink transmission.
  541. *
  542. *\retval Status of the operation.
  543. */
  544. LoRaMacStatus_t RegionCommonIdentifyChannels( RegionCommonIdentifyChannelsParam_t* identifyChannelsParam,
  545. TimerTime_t* aggregatedTimeOff, uint8_t* enabledChannels,
  546. uint8_t* nbEnabledChannels, uint8_t* nbRestrictedChannels,
  547. TimerTime_t* nextTxDelay );
  548. /*!
  549. * \brief Selects the next lower datarate.
  550. *
  551. * \param [IN] params Data structure providing parameters based on \ref RegionCommonGetNextLowerTxDrParams_t
  552. *
  553. * \retval The next lower datarate.
  554. */
  555. int8_t RegionCommonGetNextLowerTxDr( RegionCommonGetNextLowerTxDrParams_t *params );
  556. /*!
  557. * \brief Limits the TX power.
  558. *
  559. * \param [IN] txPower Current TX power.
  560. *
  561. * \param [IN] maxBandTxPower Maximum possible TX power.
  562. *
  563. * \retval Limited TX power.
  564. */
  565. int8_t RegionCommonLimitTxPower( int8_t txPower, int8_t maxBandTxPower );
  566. /*!
  567. * \brief Gets the bandwidth.
  568. *
  569. * \param [IN] drIndex Datarate index.
  570. *
  571. * \param [IN] bandwidths A pointer to the bandwidth table.
  572. *
  573. * \retval Bandwidth.
  574. */
  575. uint32_t RegionCommonGetBandwidth( uint32_t drIndex, const uint32_t* bandwidths );
  576. /*! \} defgroup REGIONCOMMON */
  577. #ifdef __cplusplus
  578. }
  579. #endif
  580. #endif // __REGIONCOMMON_H__