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.
 
 
 
 
 
 

545 lines
14 KiB

  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. /*!
  215. * Beacons transmit time precision determined using
  216. * param field of beacon frame format.
  217. */
  218. SysTime_t BeaconTimePrecision;
  219. }BeaconContext_t;
  220. /*!
  221. * Data structure which contains the callbacks
  222. */
  223. typedef struct sLoRaMacClassBCallback
  224. {
  225. /*!
  226. * \brief Measures the temperature level
  227. *
  228. * \retval Temperature level
  229. */
  230. float ( *GetTemperatureLevel )( void );
  231. /*!
  232. *\brief Will be called each time a Radio IRQ is handled by the MAC
  233. * layer.
  234. *
  235. *\warning Runs in a IRQ context. Should only change variables state.
  236. */
  237. void ( *MacProcessNotify )( void );
  238. }LoRaMacClassBCallback_t;
  239. /*!
  240. * Data structure which pointers to the properties LoRaMAC
  241. */
  242. typedef struct sLoRaMacClassBParams
  243. {
  244. /*!
  245. * Pointer to the MlmeIndication structure
  246. */
  247. MlmeIndication_t *MlmeIndication;
  248. /*!
  249. * Pointer to the McpsIndication structure
  250. */
  251. McpsIndication_t *McpsIndication;
  252. /*!
  253. * Pointer to the MlmeConfirm structure
  254. */
  255. MlmeConfirm_t *MlmeConfirm;
  256. /*!
  257. * Pointer to the LoRaMacFlags structure
  258. */
  259. LoRaMacFlags_t *LoRaMacFlags;
  260. /*!
  261. * Pointer to the LoRaMac device address
  262. */
  263. uint32_t *LoRaMacDevAddr;
  264. /*!
  265. * Pointer to the LoRaMac region definition
  266. */
  267. LoRaMacRegion_t *LoRaMacRegion;
  268. /*!
  269. * Pointer to the LoRaMacParams structure
  270. */
  271. LoRaMacParams_t *LoRaMacParams;
  272. /*!
  273. * Pointer to the multicast channel list
  274. */
  275. MulticastCtx_t *MulticastChannels;
  276. /*!
  277. * Pointer to the activation type
  278. */
  279. ActivationType_t *NetworkActivation;
  280. }LoRaMacClassBParams_t;
  281. /*!
  282. * Signature of callback function to be called by this module when the
  283. * non-volatile needs to be saved.
  284. */
  285. typedef void ( *LoRaMacClassBNvmEvent )( void );
  286. /*!
  287. * \brief Initialize LoRaWAN Class B
  288. *
  289. * \param [IN] classBParams Information and feedback parameter
  290. * \param [IN] callbacks Contains the callback which the Class B implementation needs
  291. * \param [IN] nvm Pointer to an external non-volatile memory data structure.
  292. */
  293. void LoRaMacClassBInit( LoRaMacClassBParams_t *classBParams, LoRaMacClassBCallback_t *callbacks,
  294. LoRaMacClassBNvmData_t* nvm );
  295. /*!
  296. * \brief Set the state of the beacon state machine
  297. *
  298. * \param [IN] beaconState Beacon state.
  299. */
  300. void LoRaMacClassBSetBeaconState( BeaconState_t beaconState );
  301. /*!
  302. * \brief Set the state of the ping slot state machine
  303. *
  304. * \param [IN] pingSlotState Ping slot state.
  305. */
  306. void LoRaMacClassBSetPingSlotState( PingSlotState_t pingSlotState );
  307. /*!
  308. * \brief Set the state of the multicast slot state machine
  309. *
  310. * \param [IN] pingSlotState multicast slot state.
  311. */
  312. void LoRaMacClassBSetMulticastSlotState( PingSlotState_t multicastSlotState );
  313. /*!
  314. * \brief Verifies if an acquisition procedure is in progress
  315. *
  316. * \retval [true, if the acquisition is in progress; false, if not]
  317. */
  318. bool LoRaMacClassBIsAcquisitionInProgress( void );
  319. /*!
  320. * \brief State machine of the Class B for beaconing
  321. */
  322. void LoRaMacClassBBeaconTimerEvent( void* context );
  323. /*!
  324. * \brief State machine of the Class B for ping slots
  325. */
  326. void LoRaMacClassBPingSlotTimerEvent( void* context );
  327. /*!
  328. * \brief State machine of the Class B for multicast slots
  329. */
  330. void LoRaMacClassBMulticastSlotTimerEvent( void* context );
  331. /*!
  332. * \brief Receives and decodes the beacon frame
  333. *
  334. * \param [IN] payload Pointer to the payload
  335. * \param [IN] size Size of the payload
  336. * \retval [true, if the node has received a beacon; false, if not]
  337. */
  338. bool LoRaMacClassBRxBeacon( uint8_t *payload, uint16_t size );
  339. /*!
  340. * \brief The function validates, if the node expects a beacon
  341. * at the current time.
  342. *
  343. * \retval [true, if the node expects a beacon; false, if not]
  344. */
  345. bool LoRaMacClassBIsBeaconExpected( void );
  346. /*!
  347. * \brief The function validates, if the node expects a ping slot
  348. * at the current time.
  349. *
  350. * \retval [true, if the node expects a ping slot; false, if not]
  351. */
  352. bool LoRaMacClassBIsPingExpected( void );
  353. /*!
  354. * \brief The function validates, if the node expects a multicast slot
  355. * at the current time.
  356. *
  357. * \retval [true, if the node expects a multicast slot; false, if not]
  358. */
  359. bool LoRaMacClassBIsMulticastExpected( void );
  360. /*!
  361. * \brief Verifies if the acquisition pending bit is set
  362. *
  363. * \retval [true, if the bit is set; false, if not]
  364. */
  365. bool LoRaMacClassBIsAcquisitionPending( void );
  366. /*!
  367. * \brief Verifies if the beacon mode active bit is set
  368. *
  369. * \retval [true, if the bit is set; false, if not]
  370. */
  371. bool LoRaMacClassBIsBeaconModeActive( void );
  372. /*!
  373. * \brief Stops the beacon and ping slot operation
  374. */
  375. void LoRaMacClassBHaltBeaconing( void );
  376. /*!
  377. * \brief Resumes the beacon and ping slot operation
  378. */
  379. void LoRaMacClassBResumeBeaconing( void );
  380. /*!
  381. * \brief Sets the periodicity of the ping slots
  382. *
  383. * \param [IN] periodicity Periodicity
  384. */
  385. void LoRaMacClassBSetPingSlotInfo( uint8_t periodicity );
  386. /*!
  387. * \brief Switches the device class
  388. *
  389. * \param [IN] nextClass Device class to switch to
  390. *
  391. * \retval LoRaMacStatus_t Status of the operation.
  392. */
  393. LoRaMacStatus_t LoRaMacClassBSwitchClass( DeviceClass_t nextClass );
  394. /*!
  395. * \brief LoRaMAC ClassB MIB-Get
  396. *
  397. * \details The mac information base service to get attributes of the LoRaMac
  398. * Class B layer.
  399. *
  400. * \param [IN] mibRequest - MIB-GET-Request to perform. Refer to \ref MibRequestConfirm_t.
  401. *
  402. * \retval LoRaMacStatus_t Status of the operation. Possible returns are:
  403. * \ref LORAMAC_STATUS_OK,
  404. * \ref LORAMAC_STATUS_SERVICE_UNKNOWN,
  405. * \ref LORAMAC_STATUS_PARAMETER_INVALID.
  406. */
  407. LoRaMacStatus_t LoRaMacClassBMibGetRequestConfirm( MibRequestConfirm_t *mibGet );
  408. /*!
  409. * \brief LoRaMAC Class B MIB-Set
  410. *
  411. * \details The mac information base service to set attributes of the LoRaMac
  412. * Class B layer.
  413. *
  414. * \param [IN] mibRequest - MIB-SET-Request to perform. Refer to \ref MibRequestConfirm_t.
  415. *
  416. * \retval LoRaMacStatus_t Status of the operation. Possible returns are:
  417. * \ref LORAMAC_STATUS_OK,
  418. * \ref LORAMAC_STATUS_BUSY,
  419. * \ref LORAMAC_STATUS_SERVICE_UNKNOWN,
  420. * \ref LORAMAC_STATUS_PARAMETER_INVALID.
  421. */
  422. LoRaMacStatus_t LoRaMacMibClassBSetRequestConfirm( MibRequestConfirm_t *mibSet );
  423. /*!
  424. * \brief This function handles the PING_SLOT_FREQ_ANS
  425. */
  426. void LoRaMacClassBPingSlotInfoAns( void );
  427. /*!
  428. * \brief This function handles the PING_SLOT_CHANNEL_REQ
  429. *
  430. * \param [IN] datarate Device class to switch to
  431. * \param [IN] frequency Device class to switch to
  432. *
  433. * \retval Status for the MAC answer.
  434. */
  435. uint8_t LoRaMacClassBPingSlotChannelReq( uint8_t datarate, uint32_t frequency );
  436. /*!
  437. * \brief This function handles the BEACON_TIMING_ANS
  438. *
  439. * \param [IN] beaconTimingDelay The beacon timing delay
  440. * \param [IN] beaconTimingChannel The beacon timing channel
  441. * \param [IN] lastRxDone The time of the last frame reception
  442. */
  443. void LoRaMacClassBBeaconTimingAns( uint16_t beaconTimingDelay, uint8_t beaconTimingChannel, TimerTime_t lastRxDone );
  444. /*!
  445. * \brief This function handles the ClassB DEVICE_TIME_ANS
  446. */
  447. void LoRaMacClassBDeviceTimeAns( void );
  448. /*!
  449. * \brief This function handles the BEACON_FREQ_REQ
  450. *
  451. * \param [IN] frequency Frequency to set
  452. *
  453. * \retval [true, if MAC shall send an answer; false, if not]
  454. */
  455. bool LoRaMacClassBBeaconFreqReq( uint32_t frequency );
  456. /*!
  457. * \brief Queries the ping slot window time
  458. *
  459. * \param [IN] txTimeOnAir TX time on air for the next uplink
  460. *
  461. * \retval Returns the time the uplink should be delayed
  462. */
  463. TimerTime_t LoRaMacClassBIsUplinkCollision( TimerTime_t txTimeOnAir );
  464. /*!
  465. * \brief Stops the timers for the RX slots. This includes the
  466. * timers for ping and multicast slots.
  467. */
  468. void LoRaMacClassBStopRxSlots( void );
  469. /*!
  470. * \brief Starts the timers for the RX slots. This includes the
  471. * timers for ping and multicast slots.
  472. */
  473. void LoRaMacClassBStartRxSlots( void );
  474. /*!
  475. * \brief Starts the timers for the RX slots. This includes the
  476. * timers for ping and multicast slots.
  477. *
  478. * \param [IN] periodicity Downlink periodicity
  479. *
  480. * \param [IN] multicastChannel Related multicast channel
  481. */
  482. void LoRaMacClassBSetMulticastPeriodicity( MulticastCtx_t* multicastChannel );
  483. /*!
  484. * \brief Sets the FPending bit status of the related downlink slot
  485. *
  486. * \param [IN] address Slot address, could be unicast or multicast
  487. *
  488. * \param [IN] fPendingSet Set to 1, if the fPending bit in the
  489. * sequence is set, otherwise 0.
  490. */
  491. void LoRaMacClassBSetFPendingBit( uint32_t address, uint8_t fPendingSet );
  492. /*!
  493. * \brief Class B process function.
  494. */
  495. void LoRaMacClassBProcess( void );
  496. #ifdef __cplusplus
  497. }
  498. #endif
  499. #endif // __LORAMACCLASSB_H__