RegionCommon.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "Region.h"
  45. typedef struct sRegionCommonLinkAdrParams
  46. {
  47. /*!
  48. * Number of repetitions.
  49. */
  50. uint8_t NbRep;
  51. /*!
  52. * Datarate.
  53. */
  54. int8_t Datarate;
  55. /*!
  56. * Tx power.
  57. */
  58. int8_t TxPower;
  59. /*!
  60. * Channels mask control field.
  61. */
  62. uint8_t ChMaskCtrl;
  63. /*!
  64. * Channels mask field.
  65. */
  66. uint16_t ChMask;
  67. }RegionCommonLinkAdrParams_t;
  68. typedef struct sRegionCommonLinkAdrReqVerifyParams
  69. {
  70. /*!
  71. * LoRaWAN specification Version
  72. */
  73. Version_t Version;
  74. /*!
  75. * The current status of the AdrLinkRequest.
  76. */
  77. uint8_t Status;
  78. /*!
  79. * Set to true, if ADR is enabled.
  80. */
  81. bool AdrEnabled;
  82. /*!
  83. * The datarate the AdrLinkRequest wants to set.
  84. */
  85. int8_t Datarate;
  86. /*!
  87. * The TX power the AdrLinkRequest wants to set.
  88. */
  89. int8_t TxPower;
  90. /*!
  91. * The number of repetitions the AdrLinkRequest wants to set.
  92. */
  93. uint8_t NbRep;
  94. /*!
  95. * The current datarate the node is using.
  96. */
  97. int8_t CurrentDatarate;
  98. /*!
  99. * The current TX power the node is using.
  100. */
  101. int8_t CurrentTxPower;
  102. /*!
  103. * The current number of repetitions the node is using.
  104. */
  105. int8_t CurrentNbRep;
  106. /*!
  107. * The number of channels.
  108. */
  109. uint8_t NbChannels;
  110. /*!
  111. * Pointer to the first element of the channels mask.
  112. */
  113. uint16_t* ChannelsMask;
  114. /*!
  115. * The minimum possible datarate.
  116. */
  117. int8_t MinDatarate;
  118. /*!
  119. * The maximum possible datarate.
  120. */
  121. int8_t MaxDatarate;
  122. /*!
  123. * Pointer to the channels.
  124. */
  125. ChannelParams_t* Channels;
  126. /*!
  127. * The minimum possible TX power.
  128. */
  129. int8_t MinTxPower;
  130. /*!
  131. * The maximum possible TX power.
  132. */
  133. int8_t MaxTxPower;
  134. }RegionCommonLinkAdrReqVerifyParams_t;
  135. typedef struct sRegionCommonCalcBackOffParams
  136. {
  137. /*!
  138. * A pointer to region specific channels.
  139. */
  140. ChannelParams_t* Channels;
  141. /*!
  142. * A pointer to region specific bands.
  143. */
  144. Band_t* Bands;
  145. /*!
  146. * Set to true, if the last uplink was a join request.
  147. */
  148. bool LastTxIsJoinRequest;
  149. /*!
  150. * Set to true, if the node is joined.
  151. */
  152. bool Joined;
  153. /*!
  154. * Set to true, if the duty cycle is enabled.
  155. */
  156. bool DutyCycleEnabled;
  157. /*!
  158. * The current channel.
  159. */
  160. uint8_t Channel;
  161. /*!
  162. * The elapsed time since initialization.
  163. */
  164. SysTime_t ElapsedTime;
  165. /*!
  166. * The time on air of the last Tx frame.
  167. */
  168. TimerTime_t TxTimeOnAir;
  169. }RegionCommonCalcBackOffParams_t;
  170. typedef struct sRegionCommonRxBeaconSetupParams
  171. {
  172. /*!
  173. * A pointer to the available datarates.
  174. */
  175. const uint8_t* Datarates;
  176. /*!
  177. * Frequency
  178. */
  179. uint32_t Frequency;
  180. /*!
  181. * The size of the beacon frame.
  182. */
  183. uint8_t BeaconSize;
  184. /*!
  185. * The datarate of the beacon.
  186. */
  187. uint8_t BeaconDatarate;
  188. /*!
  189. * The channel bandwidth of the beacon.
  190. */
  191. uint8_t BeaconChannelBW;
  192. /*!
  193. * The RX time.
  194. */
  195. uint32_t RxTime;
  196. /*!
  197. * The symbol timeout of the RX procedure.
  198. */
  199. uint16_t SymbolTimeout;
  200. }RegionCommonRxBeaconSetupParams_t;
  201. /*!
  202. * \brief Calculates the join duty cycle.
  203. * This is a generic function and valid for all regions.
  204. *
  205. * \param [IN] elapsedTime Elapsed time since the start of the device.
  206. *
  207. * \retval Duty cycle restriction.
  208. */
  209. uint16_t RegionCommonGetJoinDc( SysTime_t elapsedTime );
  210. /*!
  211. * \brief Verifies, if a value is in a given range.
  212. * This is a generic function and valid for all regions.
  213. *
  214. * \param [IN] value Value to verify, if it is in range.
  215. *
  216. * \param [IN] min Minimum possible value.
  217. *
  218. * \param [IN] max Maximum possible value.
  219. *
  220. * \retval Returns 1 if the value is in range, otherwise 0.
  221. */
  222. uint8_t RegionCommonValueInRange( int8_t value, int8_t min, int8_t max );
  223. /*!
  224. * \brief Verifies, if a datarate is available on an active channel.
  225. * This is a generic function and valid for all regions.
  226. *
  227. * \param [IN] nbChannels Number of channels.
  228. *
  229. * \param [IN] channelsMask The channels mask of the region.
  230. *
  231. * \param [IN] dr The datarate to verify.
  232. *
  233. * \param [IN] minDr Minimum datarate.
  234. *
  235. * \param [IN] maxDr Maximum datarate.
  236. *
  237. * \param [IN] channels The channels of the region.
  238. *
  239. * \retval Returns true if the datarate is supported, false if not.
  240. */
  241. bool RegionCommonChanVerifyDr( uint8_t nbChannels, uint16_t* channelsMask, int8_t dr,
  242. int8_t minDr, int8_t maxDr, ChannelParams_t* channels );
  243. /*!
  244. * \brief Disables a channel in a given channels mask.
  245. * This is a generic function and valid for all regions.
  246. *
  247. * \param [IN] channelsMask The channels mask of the region.
  248. *
  249. * \param [IN] id The id of the channels mask to disable.
  250. *
  251. * \param [IN] maxChannels Maximum number of channels.
  252. *
  253. * \retval Returns true if the channel could be disabled, false if not.
  254. */
  255. bool RegionCommonChanDisable( uint16_t* channelsMask, uint8_t id, uint8_t maxChannels );
  256. /*!
  257. * \brief Counts the number of active channels in a given channels mask.
  258. * This is a generic function and valid for all regions.
  259. *
  260. * \param [IN] channelsMask The channels mask of the region.
  261. *
  262. * \param [IN] startIdx Start index.
  263. *
  264. * \param [IN] stopIdx Stop index ( the channels of this index will not be counted ).
  265. *
  266. * \retval Returns the number of active channels.
  267. */
  268. uint8_t RegionCommonCountChannels( uint16_t* channelsMask, uint8_t startIdx, uint8_t stopIdx );
  269. /*!
  270. * \brief Copy a channels mask.
  271. * This is a generic function and valid for all regions.
  272. *
  273. * \param [IN] channelsMaskDest The destination channels mask.
  274. *
  275. * \param [IN] channelsMaskSrc The source channels mask.
  276. *
  277. * \param [IN] len The index length to copy.
  278. */
  279. void RegionCommonChanMaskCopy( uint16_t* channelsMaskDest, uint16_t* channelsMaskSrc, uint8_t len );
  280. /*!
  281. * \brief Sets the last tx done property.
  282. * This is a generic function and valid for all regions.
  283. *
  284. * \param [IN] joined Set to true, if the node has joined the network
  285. *
  286. * \param [IN] band The band to be updated.
  287. *
  288. * \param [IN] lastTxDone The time of the last TX done.
  289. */
  290. void RegionCommonSetBandTxDone( bool joined, Band_t* band, TimerTime_t lastTxDone );
  291. /*!
  292. * \brief Updates the time-offs of the bands.
  293. * This is a generic function and valid for all regions.
  294. *
  295. * \param [IN] joined Set to true, if the node has joined the network
  296. *
  297. * \param [IN] dutyCycle Set to true, if the duty cycle is enabled.
  298. *
  299. * \param [IN] bands A pointer to the bands.
  300. *
  301. * \param [IN] nbBands The number of bands available.
  302. *
  303. * \retval Returns the time which must be waited to perform the next uplink.
  304. */
  305. TimerTime_t RegionCommonUpdateBandTimeOff( bool joined, bool dutyCycle, Band_t* bands, uint8_t nbBands );
  306. /*!
  307. * \brief Parses the parameter of an LinkAdrRequest.
  308. * This is a generic function and valid for all regions.
  309. *
  310. * \param [IN] payload Pointer to the payload containing the MAC commands. The payload
  311. * must contain the CMD identifier, following by the parameters.
  312. *
  313. * \param [OUT] parseLinkAdr The function fills the structure with the ADR parameters.
  314. *
  315. * \retval Returns the length of the ADR request, if a request was found. Otherwise, the
  316. * function returns 0.
  317. */
  318. uint8_t RegionCommonParseLinkAdrReq( uint8_t* payload, RegionCommonLinkAdrParams_t* parseLinkAdr );
  319. /*!
  320. * \brief Verifies and updates the datarate, the TX power and the number of repetitions
  321. * of a LinkAdrRequest. This depends on the configuration of ADR also.
  322. *
  323. * \param [IN] verifyParams Pointer to a structure containing input parameters.
  324. *
  325. * \param [OUT] dr The updated datarate.
  326. *
  327. * \param [OUT] txPow The updated TX power.
  328. *
  329. * \param [OUT] nbRep The updated number of repetitions.
  330. *
  331. * \retval Returns the status according to the LinkAdrRequest definition.
  332. */
  333. uint8_t RegionCommonLinkAdrReqVerifyParams( RegionCommonLinkAdrReqVerifyParams_t* verifyParams, int8_t* dr, int8_t* txPow, uint8_t* nbRep );
  334. /*!
  335. * \brief Computes the symbol time for LoRa modulation.
  336. *
  337. * \param [IN] phyDr Physical datarate to use.
  338. *
  339. * \param [IN] bandwidth Bandwidth to use.
  340. *
  341. * \retval Returns the symbol time.
  342. */
  343. double RegionCommonComputeSymbolTimeLoRa( uint8_t phyDr, uint32_t bandwidth );
  344. /*!
  345. * \brief Computes the symbol time for FSK modulation.
  346. *
  347. * \param [IN] phyDr Physical datarate to use.
  348. *
  349. * \param [IN] bandwidth Bandwidth to use.
  350. *
  351. * \retval Returns the symbol time.
  352. */
  353. double RegionCommonComputeSymbolTimeFsk( uint8_t phyDr );
  354. /*!
  355. * \brief Computes the RX window timeout and the RX window offset.
  356. *
  357. * \param [IN] tSymbol Symbol timeout.
  358. *
  359. * \param [IN] minRxSymbols Minimum required number of symbols to detect an Rx frame.
  360. *
  361. * \param [IN] rxError System maximum timing error of the receiver. In milliseconds
  362. * The receiver will turn on in a [-rxError : +rxError] ms interval around RxOffset.
  363. *
  364. * \param [IN] wakeUpTime Wakeup time of the system.
  365. *
  366. * \param [OUT] windowTimeout RX window timeout.
  367. *
  368. * \param [OUT] windowOffset RX window time offset to be applied to the RX delay.
  369. */
  370. void RegionCommonComputeRxWindowParameters( double tSymbol, uint8_t minRxSymbols, uint32_t rxError, uint32_t wakeUpTime, uint32_t* windowTimeout, int32_t* windowOffset );
  371. /*!
  372. * \brief Computes the txPower, based on the max EIRP and the antenna gain.
  373. *
  374. * \remark US915 region uses a conducted power as input value for maxEirp.
  375. * Thus, the antennaGain parameter must be set to 0.
  376. *
  377. * \param [IN] txPower TX power index.
  378. *
  379. * \param [IN] maxEirp Maximum EIRP.
  380. *
  381. * \param [IN] antennaGain Antenna gain. Referenced to the isotropic antenna.
  382. * Value is in dBi. ( antennaGain[dBi] = measuredAntennaGain[dBd] + 2.15 )
  383. *
  384. * \retval Returns the physical TX power.
  385. */
  386. int8_t RegionCommonComputeTxPower( int8_t txPowerIndex, float maxEirp, float antennaGain );
  387. /*!
  388. * \brief Calculates the duty cycle for the current band.
  389. *
  390. * \param [IN] calcBackOffParams A pointer to the input parameters.
  391. */
  392. void RegionCommonCalcBackOff( RegionCommonCalcBackOffParams_t* calcBackOffParams );
  393. /*!
  394. * \brief Sets up the radio into RX beacon mode.
  395. *
  396. * \param [IN] rxBeaconSetupParams A pointer to the input parameters.
  397. */
  398. void RegionCommonRxBeaconSetup( RegionCommonRxBeaconSetupParams_t* rxBeaconSetupParams );
  399. /*! \} defgroup REGIONCOMMON */
  400. #ifdef __cplusplus
  401. }
  402. #endif
  403. #endif // __REGIONCOMMON_H__