LoRaMacClassB.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*!
  2. * \file LoRaMacClassB.h
  3. *
  4. * \brief LoRa MAC Class B layer implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013 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. * \defgroup LORAMACCLASSB LoRa MAC Class B layer implementation
  32. * This module specifies the API implementation of the LoRaMAC Class B layer.
  33. * This is a placeholder for a detailed description of the LoRaMac
  34. * layer and the supported features.
  35. * \{
  36. */
  37. #ifndef __LORAMACCLASSB_H__
  38. #define __LORAMACCLASSB_H__
  39. #ifdef __cplusplus
  40. extern "C"
  41. {
  42. #endif
  43. #include "systime.h"
  44. #include "LoRaMacTypes.h"
  45. /*!
  46. * States of the class B beacon acquisition and tracking
  47. */
  48. typedef enum eBeaconState
  49. {
  50. /*!
  51. * Initial state to acquire the beacon
  52. */
  53. BEACON_STATE_ACQUISITION,
  54. /*!
  55. * Beacon acquisition state when a time reference is available
  56. */
  57. BEACON_STATE_ACQUISITION_BY_TIME,
  58. /*!
  59. * Handles the state when the beacon reception fails
  60. */
  61. BEACON_STATE_TIMEOUT,
  62. /*!
  63. * Handles the state when the beacon was missed due to an uplink
  64. */
  65. BEACON_STATE_BEACON_MISSED,
  66. /*!
  67. * Reacquisition state which applies the algorithm to enlarge the reception
  68. * windows
  69. */
  70. BEACON_STATE_REACQUISITION,
  71. /*!
  72. * The node has locked a beacon successfully
  73. */
  74. BEACON_STATE_LOCKED,
  75. /*!
  76. * The beacon state machine is stopped due to operations with higher priority
  77. */
  78. BEACON_STATE_HALT,
  79. /*!
  80. * The node currently operates in the beacon window and is idle. In this
  81. * state, the temperature measurement takes place
  82. */
  83. BEACON_STATE_IDLE,
  84. /*!
  85. * The node operates in the guard time of class B
  86. */
  87. BEACON_STATE_GUARD,
  88. /*!
  89. * The node is in receive mode to lock a beacon
  90. */
  91. BEACON_STATE_RX,
  92. /*!
  93. * The nodes switches the device class
  94. */
  95. BEACON_STATE_LOST,
  96. }BeaconState_t;
  97. /*!
  98. * States of the class B ping slot mechanism
  99. */
  100. typedef enum ePingSlotState
  101. {
  102. /*!
  103. * Calculation of the ping slot offset
  104. */
  105. PINGSLOT_STATE_CALC_PING_OFFSET,
  106. /*!
  107. * State to set the timer to open the next ping slot
  108. */
  109. PINGSLOT_STATE_SET_TIMER,
  110. /*!
  111. * The node is in idle state
  112. */
  113. PINGSLOT_STATE_IDLE,
  114. /*!
  115. * The node opens up a ping slot window
  116. */
  117. PINGSLOT_STATE_RX,
  118. }PingSlotState_t;
  119. /*!
  120. * Class B ping slot context structure
  121. */
  122. typedef struct sPingSlotContext
  123. {
  124. /*!
  125. * Ping slot length time in ms
  126. */
  127. uint32_t PingSlotWindow;
  128. /*!
  129. * Ping offset
  130. */
  131. uint16_t PingOffset;
  132. /*!
  133. * Current symbol timeout. The node enlarges this variable in case of beacon
  134. * loss.
  135. */
  136. uint16_t SymbolTimeout;
  137. /*!
  138. * The multicast channel which will be enabled next.
  139. */
  140. MulticastCtx_t *NextMulticastChannel;
  141. }PingSlotContext_t;
  142. /*!
  143. * Class B beacon context structure
  144. */
  145. typedef struct sBeaconContext
  146. {
  147. struct sBeaconCtrl
  148. {
  149. /*!
  150. * Set if the node receives beacons
  151. */
  152. uint8_t BeaconMode : 1;
  153. /*!
  154. * Set if the node has acquired the beacon
  155. */
  156. uint8_t BeaconAcquired : 1;
  157. /*!
  158. * Set if a beacon delay was set for the beacon acquisition
  159. */
  160. uint8_t BeaconDelaySet : 1;
  161. /*!
  162. * Set if a beacon channel was set for the beacon acquisition
  163. */
  164. uint8_t BeaconChannelSet : 1;
  165. /*!
  166. * Set if beacon acquisition is pending
  167. */
  168. uint8_t AcquisitionPending : 1;
  169. /*!
  170. * Set if the beacon state machine will be resumed
  171. */
  172. uint8_t ResumeBeaconing : 1;
  173. }Ctrl;
  174. /*!
  175. * Current temperature
  176. */
  177. float Temperature;
  178. /*!
  179. * Beacon time received with the beacon frame
  180. */
  181. SysTime_t BeaconTime;
  182. /*!
  183. * Time when the last beacon was received
  184. */
  185. SysTime_t LastBeaconRx;
  186. /*!
  187. * Time when the next beacon will be received
  188. */
  189. SysTime_t NextBeaconRx;
  190. /*!
  191. * This is the time where the RX window will be opened.
  192. * Its base is NextBeaconRx with temperature compensations
  193. * and RX window movement.
  194. */
  195. TimerTime_t NextBeaconRxAdjusted;
  196. /*!
  197. * Current symbol timeout. The node enlarges this variable in case of beacon
  198. * loss.
  199. */
  200. uint16_t SymbolTimeout;
  201. /*!
  202. * Specifies how much time the beacon window will be moved.
  203. */
  204. TimerTime_t BeaconWindowMovement;
  205. /*!
  206. * Beacon timing channel for next beacon
  207. */
  208. uint8_t BeaconTimingChannel;
  209. /*!
  210. * Delay for next beacon in ms
  211. */
  212. TimerTime_t BeaconTimingDelay;
  213. TimerTime_t TimeStamp;
  214. }BeaconContext_t;
  215. /*!
  216. * Data structure which contains the callbacks
  217. */
  218. typedef struct sLoRaMacClassBCallback
  219. {
  220. /*!
  221. * \brief Measures the temperature level
  222. *
  223. * \retval Temperature level
  224. */
  225. float ( *GetTemperatureLevel )( void );
  226. /*!
  227. *\brief Will be called each time a Radio IRQ is handled by the MAC
  228. * layer.
  229. *
  230. *\warning Runs in a IRQ context. Should only change variables state.
  231. */
  232. void ( *MacProcessNotify )( void );
  233. }LoRaMacClassBCallback_t;
  234. /*!
  235. * Data structure which pointers to the properties LoRaMAC
  236. */
  237. typedef struct sLoRaMacClassBParams
  238. {
  239. /*!
  240. * Pointer to the MlmeIndication structure
  241. */
  242. MlmeIndication_t *MlmeIndication;
  243. /*!
  244. * Pointer to the McpsIndication structure
  245. */
  246. McpsIndication_t *McpsIndication;
  247. /*!
  248. * Pointer to the MlmeConfirm structure
  249. */
  250. MlmeConfirm_t *MlmeConfirm;
  251. /*!
  252. * Pointer to the LoRaMacFlags structure
  253. */
  254. LoRaMacFlags_t *LoRaMacFlags;
  255. /*!
  256. * Pointer to the LoRaMac device address
  257. */
  258. uint32_t *LoRaMacDevAddr;
  259. /*!
  260. * Pointer to the LoRaMac region definition
  261. */
  262. LoRaMacRegion_t *LoRaMacRegion;
  263. /*!
  264. * Pointer to the LoRaMacParams structure
  265. */
  266. LoRaMacParams_t *LoRaMacParams;
  267. /*!
  268. * Pointer to the multicast channel list
  269. */
  270. MulticastCtx_t *MulticastChannels;
  271. }LoRaMacClassBParams_t;
  272. /*!
  273. * Signature of callback function to be called by this module when the
  274. * non-volatile needs to be saved.
  275. */
  276. typedef void ( *LoRaMacClassBNvmEvent )( void );
  277. /*!
  278. * \brief Initialize LoRaWAN Class B
  279. *
  280. * \param [IN] classBParams Information and feedback parameter
  281. * \param [IN] callbacks Contains the callback which the Class B implementation needs
  282. * \param [IN] callback function which will be called when the non-volatile context needs to be saved.
  283. */
  284. void LoRaMacClassBInit( LoRaMacClassBParams_t *classBParams, LoRaMacClassBCallback_t *callbacks, LoRaMacClassBNvmEvent classBNvmCtxChanged );
  285. /*!
  286. * Restores the internal non-volatile context from passed pointer.
  287. *
  288. * \param [IN] classBNvmCtx - Pointer to non-volatile class B module context to be restored.
  289. *
  290. * \retval - Status of the operation
  291. */
  292. bool LoRaMacClassBRestoreNvmCtx( void* classBNvmCtx );
  293. /*!
  294. * Returns a pointer to the internal non-volatile context.
  295. *
  296. * \param [IN] classBNvmCtxSize - Size of the module non-volatile context
  297. *
  298. * \retval - Points to a structure where the module store its non-volatile context
  299. */
  300. void* LoRaMacClassBGetNvmCtx( size_t* classBNvmCtxSize );
  301. /*!
  302. * \brief Set the state of the beacon state machine
  303. *
  304. * \param [IN] beaconState Beacon state.
  305. */
  306. void LoRaMacClassBSetBeaconState( BeaconState_t beaconState );
  307. /*!
  308. * \brief Set the state of the ping slot state machine
  309. *
  310. * \param [IN] pingSlotState Ping slot state.
  311. */
  312. void LoRaMacClassBSetPingSlotState( PingSlotState_t pingSlotState );
  313. /*!
  314. * \brief Set the state of the multicast slot state machine
  315. *
  316. * \param [IN] pingSlotState multicast slot state.
  317. */
  318. void LoRaMacClassBSetMulticastSlotState( PingSlotState_t multicastSlotState );
  319. /*!
  320. * \brief Verifies if an acquisition procedure is in progress
  321. *
  322. * \retval [true, if the acquisition is in progress; false, if not]
  323. */
  324. bool LoRaMacClassBIsAcquisitionInProgress( void );
  325. /*!
  326. * \brief State machine of the Class B for beaconing
  327. */
  328. void LoRaMacClassBBeaconTimerEvent( void* context );
  329. /*!
  330. * \brief State machine of the Class B for ping slots
  331. */
  332. void LoRaMacClassBPingSlotTimerEvent( void* context );
  333. /*!
  334. * \brief State machine of the Class B for multicast slots
  335. */
  336. void LoRaMacClassBMulticastSlotTimerEvent( void* context );
  337. /*!
  338. * \brief Receives and decodes the beacon frame
  339. *
  340. * \param [IN] payload Pointer to the payload
  341. * \param [IN] size Size of the payload
  342. * \retval [true, if the node has received a beacon; false, if not]
  343. */
  344. bool LoRaMacClassBRxBeacon( uint8_t *payload, uint16_t size );
  345. /*!
  346. * \brief The function validates, if the node expects a beacon
  347. * at the current time.
  348. *
  349. * \retval [true, if the node expects a beacon; false, if not]
  350. */
  351. bool LoRaMacClassBIsBeaconExpected( void );
  352. /*!
  353. * \brief The function validates, if the node expects a ping slot
  354. * at the current time.
  355. *
  356. * \retval [true, if the node expects a ping slot; false, if not]
  357. */
  358. bool LoRaMacClassBIsPingExpected( void );
  359. /*!
  360. * \brief The function validates, if the node expects a multicast slot
  361. * at the current time.
  362. *
  363. * \retval [true, if the node expects a multicast slot; false, if not]
  364. */
  365. bool LoRaMacClassBIsMulticastExpected( void );
  366. /*!
  367. * \brief Verifies if the acquisition pending bit is set
  368. *
  369. * \retval [true, if the bit is set; false, if not]
  370. */
  371. bool LoRaMacClassBIsAcquisitionPending( void );
  372. /*!
  373. * \brief Verifies if the beacon mode active bit is set
  374. *
  375. * \retval [true, if the bit is set; false, if not]
  376. */
  377. bool LoRaMacClassBIsBeaconModeActive( void );
  378. /*!
  379. * \brief Stops the beacon and ping slot operation
  380. */
  381. void LoRaMacClassBHaltBeaconing( void );
  382. /*!
  383. * \brief Resumes the beacon and ping slot operation
  384. */
  385. void LoRaMacClassBResumeBeaconing( void );
  386. /*!
  387. * \brief Sets the periodicity of the ping slots
  388. *
  389. * \param [IN] periodicity Periodicity
  390. */
  391. void LoRaMacClassBSetPingSlotInfo( uint8_t periodicity );
  392. /*!
  393. * \brief Switches the device class
  394. *
  395. * \param [IN] nextClass Device class to switch to
  396. *
  397. * \retval LoRaMacStatus_t Status of the operation.
  398. */
  399. LoRaMacStatus_t LoRaMacClassBSwitchClass( DeviceClass_t nextClass );
  400. /*!
  401. * \brief LoRaMAC ClassB MIB-Get
  402. *
  403. * \details The mac information base service to get attributes of the LoRaMac
  404. * Class B layer.
  405. *
  406. * \param [IN] mibRequest - MIB-GET-Request to perform. Refer to \ref MibRequestConfirm_t.
  407. *
  408. * \retval LoRaMacStatus_t Status of the operation. Possible returns are:
  409. * \ref LORAMAC_STATUS_OK,
  410. * \ref LORAMAC_STATUS_SERVICE_UNKNOWN,
  411. * \ref LORAMAC_STATUS_PARAMETER_INVALID.
  412. */
  413. LoRaMacStatus_t LoRaMacClassBMibGetRequestConfirm( MibRequestConfirm_t *mibGet );
  414. /*!
  415. * \brief LoRaMAC Class B MIB-Set
  416. *
  417. * \details The mac information base service to set attributes of the LoRaMac
  418. * Class B layer.
  419. *
  420. * \param [IN] mibRequest - MIB-SET-Request to perform. Refer to \ref MibRequestConfirm_t.
  421. *
  422. * \retval LoRaMacStatus_t Status of the operation. Possible returns are:
  423. * \ref LORAMAC_STATUS_OK,
  424. * \ref LORAMAC_STATUS_BUSY,
  425. * \ref LORAMAC_STATUS_SERVICE_UNKNOWN,
  426. * \ref LORAMAC_STATUS_PARAMETER_INVALID.
  427. */
  428. LoRaMacStatus_t LoRaMacMibClassBSetRequestConfirm( MibRequestConfirm_t *mibSet );
  429. /*!
  430. * \brief This function handles the PING_SLOT_FREQ_ANS
  431. */
  432. void LoRaMacClassBPingSlotInfoAns( void );
  433. /*!
  434. * \brief This function handles the PING_SLOT_CHANNEL_REQ
  435. *
  436. * \param [IN] datarate Device class to switch to
  437. * \param [IN] frequency Device class to switch to
  438. *
  439. * \retval Status for the MAC answer.
  440. */
  441. uint8_t LoRaMacClassBPingSlotChannelReq( uint8_t datarate, uint32_t frequency );
  442. /*!
  443. * \brief This function handles the BEACON_TIMING_ANS
  444. *
  445. * \param [IN] beaconTimingDelay The beacon timing delay
  446. * \param [IN] beaconTimingChannel The beacon timing channel
  447. * \param [IN] lastRxDone The time of the last frame reception
  448. */
  449. void LoRaMacClassBBeaconTimingAns( uint16_t beaconTimingDelay, uint8_t beaconTimingChannel, TimerTime_t lastRxDone );
  450. /*!
  451. * \brief This function handles the ClassB DEVICE_TIME_ANS
  452. */
  453. void LoRaMacClassBDeviceTimeAns( void );
  454. /*!
  455. * \brief This function handles the BEACON_FREQ_REQ
  456. *
  457. * \param [IN] frequency Frequency to set
  458. *
  459. * \retval [true, if MAC shall send an answer; false, if not]
  460. */
  461. bool LoRaMacClassBBeaconFreqReq( uint32_t frequency );
  462. /*!
  463. * \brief Queries the ping slot window time
  464. *
  465. * \param [IN] txTimeOnAir TX time on air for the next uplink
  466. *
  467. * \retval Returns the time the uplink should be delayed
  468. */
  469. TimerTime_t LoRaMacClassBIsUplinkCollision( TimerTime_t txTimeOnAir );
  470. /*!
  471. * \brief Stops the timers for the RX slots. This includes the
  472. * timers for ping and multicast slots.
  473. */
  474. void LoRaMacClassBStopRxSlots( void );
  475. /*!
  476. * \brief Starts the timers for the RX slots. This includes the
  477. * timers for ping and multicast slots.
  478. */
  479. void LoRaMacClassBStartRxSlots( void );
  480. /*!
  481. * \brief Starts the timers for the RX slots. This includes the
  482. * timers for ping and multicast slots.
  483. *
  484. * \param [IN] periodicity Downlink periodicity
  485. *
  486. * \param [IN] multicastChannel Related multicast channel
  487. */
  488. void LoRaMacClassBSetMulticastPeriodicity( MulticastCtx_t* multicastChannel );
  489. void LoRaMacClassBProcess( void );
  490. #ifdef __cplusplus
  491. }
  492. #endif
  493. #endif // __LORAMACCLASSB_H__