LoRaMac-api-v3.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*!
  2. * \file LoRaMac-api-v3.h
  3. *
  4. * \brief LoRa MAC wrapper 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 Jäckle ( STACKFORCE )
  30. */
  31. #ifndef __LORAMAC_API_V3_H__
  32. #define __LORAMAC_API_V3_H__
  33. // Includes board dependent definitions such as channels frequencies
  34. #include "LoRaMac.h"
  35. #include "LoRaMac-board.h"
  36. /*!
  37. * Beacon interval in us
  38. */
  39. #define BEACON_INTERVAL 128000000
  40. /*!
  41. * Class A&B receive delay in us
  42. */
  43. #define RECEIVE_DELAY1 1000000
  44. #define RECEIVE_DELAY2 2000000
  45. /*!
  46. * Join accept receive delay in us
  47. */
  48. #define JOIN_ACCEPT_DELAY1 5000000
  49. #define JOIN_ACCEPT_DELAY2 6000000
  50. /*!
  51. * Class A&B maximum receive window delay in us
  52. */
  53. #define MAX_RX_WINDOW 3000000
  54. /*!
  55. * Maximum allowed gap for the FCNT field
  56. */
  57. #define MAX_FCNT_GAP 16384
  58. /*!
  59. * ADR acknowledgement counter limit
  60. */
  61. #define ADR_ACK_LIMIT 64
  62. /*!
  63. * Number of ADR acknowledgement requests before returning to default datarate
  64. */
  65. #define ADR_ACK_DELAY 32
  66. /*!
  67. * Number of seconds after the start of the second reception window without
  68. * receiving an acknowledge.
  69. * AckTimeout = ACK_TIMEOUT + Random( -ACK_TIMEOUT_RND, ACK_TIMEOUT_RND )
  70. */
  71. #define ACK_TIMEOUT 2000000
  72. /*!
  73. * Random number of seconds after the start of the second reception window without
  74. * receiving an acknowledge
  75. * AckTimeout = ACK_TIMEOUT + Random( -ACK_TIMEOUT_RND, ACK_TIMEOUT_RND )
  76. */
  77. #define ACK_TIMEOUT_RND 1000000
  78. /*!
  79. * Check the Mac layer state every MAC_STATE_CHECK_TIMEOUT
  80. */
  81. #define MAC_STATE_CHECK_TIMEOUT 1000000
  82. /*!
  83. * Maximum number of times the MAC layer tries to get an acknowledge.
  84. */
  85. #define MAX_ACK_RETRIES 8
  86. /*!
  87. * RSSI free threshold
  88. */
  89. #define RSSI_FREE_TH ( int8_t )( -90 ) // [dBm]
  90. /*!
  91. * Frame direction definition
  92. */
  93. #define UP_LINK 0
  94. #define DOWN_LINK 1
  95. /*!
  96. * Sets the length of the LoRaMAC footer field.
  97. * Mainly indicates the MIC field length
  98. */
  99. #define LORAMAC_MFR_LEN 4
  100. /*!
  101. * Syncword for Private LoRa networks
  102. */
  103. #define LORA_MAC_PRIVATE_SYNCWORD 0x12
  104. /*!
  105. * Syncword for Public LoRa networks
  106. */
  107. #define LORA_MAC_PUBLIC_SYNCWORD 0x34
  108. /*!
  109. * LoRaMAC event flags
  110. */
  111. typedef union
  112. {
  113. uint8_t Value;
  114. struct
  115. {
  116. uint8_t Tx : 1;
  117. uint8_t Rx : 1;
  118. uint8_t RxData : 1;
  119. uint8_t Multicast : 1;
  120. uint8_t RxSlot : 2;
  121. uint8_t LinkCheck : 1;
  122. uint8_t JoinAccept : 1;
  123. }Bits;
  124. }LoRaMacEventFlags_t;
  125. /*!
  126. * LoRaMAC event information
  127. */
  128. typedef struct
  129. {
  130. LoRaMacEventInfoStatus_t Status;
  131. bool TxAckReceived;
  132. uint8_t TxNbRetries;
  133. uint8_t TxDatarate;
  134. uint8_t RxPort;
  135. uint8_t *RxBuffer;
  136. uint8_t RxBufferSize;
  137. int16_t RxRssi;
  138. uint8_t RxSnr;
  139. uint16_t Energy;
  140. uint8_t DemodMargin;
  141. uint8_t NbGateways;
  142. }LoRaMacEventInfo_t;
  143. /*!
  144. * LoRaMAC events structure
  145. * Used to notify upper layers of MAC events
  146. */
  147. typedef struct sLoRaMacCallbacks
  148. {
  149. /*!
  150. * MAC layer event callback prototype.
  151. *
  152. * \param [IN] flags Bit field indicating the MAC events occurred
  153. * \param [IN] info Details about MAC events occurred
  154. */
  155. void ( *MacEvent )( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info );
  156. /*!
  157. * Function callback to get the current battery level
  158. *
  159. * \retval batteryLevel Current battery level
  160. */
  161. uint8_t ( *GetBatteryLevel )( void );
  162. }LoRaMacCallbacks_t;
  163. /*!
  164. * LoRaMAC layer initialization
  165. *
  166. * \param [IN] callbacks Pointer to a structure defining the LoRaMAC
  167. * callback functions.
  168. */
  169. void LoRaMacInit( LoRaMacCallbacks_t *callbacks );
  170. /*!
  171. * Enables/Disables the ADR (Adaptive Data Rate)
  172. *
  173. * \param [IN] enable [true: ADR ON, false: ADR OFF]
  174. */
  175. void LoRaMacSetAdrOn( bool enable );
  176. /*!
  177. * Initializes the network IDs. Device address,
  178. * network session AES128 key and application session AES128 key.
  179. *
  180. * \remark To be only used when Over-the-Air activation isn't used.
  181. *
  182. * \param [IN] netID 24 bits network identifier
  183. * ( provided by network operator )
  184. * \param [IN] devAddr 32 bits device address on the network
  185. * (must be unique to the network)
  186. * \param [IN] nwkSKey Pointer to the network session AES128 key array
  187. * ( 16 bytes )
  188. * \param [IN] appSKey Pointer to the application session AES128 key array
  189. * ( 16 bytes )
  190. */
  191. void LoRaMacInitNwkIds( uint32_t netID, uint32_t devAddr, uint8_t *nwkSKey, uint8_t *appSKey );
  192. /*
  193. * Wrapper function which calls \ref LoRaMacMulticastChannelLink.
  194. */
  195. void LoRaMacMulticastChannelAdd( MulticastParams_t *channelParam );
  196. /*
  197. * Wrapper function which calls \ref LoRaMacMulticastChannelUnlink.
  198. */
  199. void LoRaMacMulticastChannelRemove( MulticastParams_t *channelParam );
  200. /*!
  201. * Initiates the Over-the-Air activation
  202. *
  203. * \param [IN] devEui Pointer to the device EUI array ( 8 bytes )
  204. * \param [IN] appEui Pointer to the application EUI array ( 8 bytes )
  205. * \param [IN] appKey Pointer to the application AES128 key array ( 16 bytes )
  206. *
  207. * \retval status [0: OK, 1: Tx error, 2: Already joined a network]
  208. */
  209. uint8_t LoRaMacJoinReq( uint8_t *devEui, uint8_t *appEui, uint8_t *appKey );
  210. /*!
  211. * Sends a LinkCheckReq MAC command on the next uplink frame
  212. *
  213. * \retval status Function status [0: OK, 1: Busy]
  214. */
  215. uint8_t LoRaMacLinkCheckReq( void );
  216. /*!
  217. * LoRaMAC layer send frame
  218. *
  219. * \param [IN] fPort MAC payload port (must be > 0)
  220. * \param [IN] fBuffer MAC data buffer to be sent
  221. * \param [IN] fBufferSize MAC data buffer size
  222. *
  223. * \retval status [0: OK, 1: Busy, 2: No network joined,
  224. * 3: Length or port error, 4: Unknown MAC command
  225. * 5: Unable to find a free channel
  226. * 6: Device switched off]
  227. */
  228. uint8_t LoRaMacSendFrame( uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
  229. /*!
  230. * LoRaMAC layer send frame
  231. *
  232. * \param [IN] fPort MAC payload port (must be > 0)
  233. * \param [IN] fBuffer MAC data buffer to be sent
  234. * \param [IN] fBufferSize MAC data buffer size
  235. * \param [IN] fBufferSize MAC data buffer size
  236. * \param [IN] nbRetries Number of retries to receive the acknowledgement
  237. *
  238. * \retval status [0: OK, 1: Busy, 2: No network joined,
  239. * 3: Length or port error, 4: Unknown MAC command
  240. * 5: Unable to find a free channel
  241. * 6: Device switched off]
  242. */
  243. uint8_t LoRaMacSendConfirmedFrame( uint8_t fPort, void *fBuffer, uint16_t fBufferSize, uint8_t nbRetries );
  244. /*!
  245. * ============================================================================
  246. * = LoRaMac test functions =
  247. * ============================================================================
  248. */
  249. /*!
  250. * LoRaMAC layer generic send frame
  251. *
  252. * \param [IN] macHdr MAC header field
  253. * \param [IN] fOpts MAC commands buffer
  254. * \param [IN] fPort MAC payload port
  255. * \param [IN] fBuffer MAC data buffer to be sent
  256. * \param [IN] fBufferSize MAC data buffer size
  257. * \retval status [0: OK, 1: Busy, 2: No network joined,
  258. * 3: Length or port error, 4: Unknown MAC command
  259. * 5: Unable to find a free channel
  260. * 6: Device switched off]
  261. */
  262. uint8_t LoRaMacSend( LoRaMacHeader_t *macHdr, uint8_t *fOpts, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
  263. /*!
  264. * LoRaMAC layer frame buffer initialization.
  265. *
  266. * \param [IN] channel Channel parameters
  267. * \param [IN] macHdr MAC header field
  268. * \param [IN] fCtrl MAC frame control field
  269. * \param [IN] fOpts MAC commands buffer
  270. * \param [IN] fPort MAC payload port
  271. * \param [IN] fBuffer MAC data buffer to be sent
  272. * \param [IN] fBufferSize MAC data buffer size
  273. * \retval status [0: OK, 1: N/A, 2: No network joined,
  274. * 3: Length or port error, 4: Unknown MAC command]
  275. */
  276. uint8_t LoRaMacPrepareFrame( ChannelParams_t channel,LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t *fOpts, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
  277. /*!
  278. * LoRaMAC layer prepared frame buffer transmission with channel specification
  279. *
  280. * \remark LoRaMacPrepareFrame must be called at least once before calling this
  281. * function.
  282. *
  283. * \param [IN] channel Channel parameters
  284. * \retval status [0: OK, 1: Busy]
  285. */
  286. uint8_t LoRaMacSendFrameOnChannel( ChannelParams_t channel );
  287. /*!
  288. * LoRaMAC layer generic send frame with channel specification
  289. *
  290. * \param [IN] channel Channel parameters
  291. * \param [IN] macHdr MAC header field
  292. * \param [IN] fCtrl MAC frame control field
  293. * \param [IN] fOpts MAC commands buffer
  294. * \param [IN] fPort MAC payload port
  295. * \param [IN] fBuffer MAC data buffer to be sent
  296. * \param [IN] fBufferSize MAC data buffer size
  297. * \retval status [0: OK, 1: Busy, 2: No network joined,
  298. * 3: Length or port error, 4: Unknown MAC command]
  299. */
  300. uint8_t LoRaMacSendOnChannel( ChannelParams_t channel, LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t *fOpts, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
  301. /*!
  302. * ============================================================================
  303. * = LoRaMac setup functions =
  304. * ============================================================================
  305. */
  306. /*
  307. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  308. * set the LoRaWan device class.
  309. */
  310. void LoRaMacSetDeviceClass( DeviceClass_t deviceClass );
  311. /*
  312. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  313. * set the network type to public or private.
  314. */
  315. void LoRaMacSetPublicNetwork( bool enable );
  316. /*
  317. * Wrapper function which calls \ref LoRaMacChannelAdd.
  318. */
  319. void LoRaMacSetChannel( uint8_t id, ChannelParams_t params );
  320. /*
  321. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  322. * set the receive window 2 channel.
  323. */
  324. void LoRaMacSetRx2Channel( Rx2ChannelParams_t param );
  325. /*!
  326. * Sets channels tx output power
  327. *
  328. * \param [IN] txPower [TX_POWER_20_DBM, TX_POWER_14_DBM,
  329. TX_POWER_11_DBM, TX_POWER_08_DBM,
  330. TX_POWER_05_DBM, TX_POWER_02_DBM]
  331. */
  332. void LoRaMacSetChannelsTxPower( int8_t txPower );
  333. /*!
  334. * Sets channels datarate
  335. *
  336. * \param [IN] datarate eu868 - [DR_0, DR_1, DR_2, DR_3, DR_4, DR_5, DR_6, DR_7]
  337. * us915 - [DR_0, DR_1, DR_2, DR_3, DR_4]
  338. */
  339. void LoRaMacSetChannelsDatarate( int8_t datarate );
  340. /*
  341. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  342. * set the channels mask.
  343. */
  344. void LoRaMacSetChannelsMask( uint16_t *mask );
  345. /*
  346. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  347. * set the number of repetitions on a channel.
  348. */
  349. void LoRaMacSetChannelsNbRep( uint8_t nbRep );
  350. /*
  351. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  352. * set the maximum receive window duration in [us].
  353. */
  354. void LoRaMacSetMaxRxWindow( uint32_t delay );
  355. /*
  356. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  357. * set the receive delay 1 in [us].
  358. */
  359. void LoRaMacSetReceiveDelay1( uint32_t delay );
  360. /*
  361. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  362. * set the receive delay 2 in [us].
  363. */
  364. void LoRaMacSetReceiveDelay2( uint32_t delay );
  365. /*
  366. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  367. * set the join accept delay 1 in [us].
  368. */
  369. void LoRaMacSetJoinAcceptDelay1( uint32_t delay );
  370. /*
  371. * Wrapper function which calls \ref LoRaMacMibSetRequestConfirm to
  372. * set the join accept delay 2 in [us].
  373. */
  374. void LoRaMacSetJoinAcceptDelay2( uint32_t delay );
  375. /*
  376. * Wrapper function which calls \ref LoRaMacMibGetRequestConfirm to
  377. * get the up-link counter.
  378. */
  379. uint32_t LoRaMacGetUpLinkCounter( void );
  380. /*
  381. * Wrapper function which calls \ref LoRaMacMibGetRequestConfirm to
  382. * get the down-link counter.
  383. */
  384. uint32_t LoRaMacGetDownLinkCounter( void );
  385. /*
  386. * ============================================================================
  387. * = LoRaMac test functions =
  388. * ============================================================================
  389. */
  390. /*!
  391. * Disables/Enables the duty cycle enforcement (EU868)
  392. *
  393. * \param [IN] enable - Enabled or disables the duty cycle
  394. */
  395. void LoRaMacTestSetDutyCycleOn( bool enable );
  396. /*!
  397. * Disables/Enables the reception windows opening
  398. *
  399. * \param [IN] enable [true: enable, false: disable]
  400. */
  401. void LoRaMacTestRxWindowsOn( bool enable );
  402. /*!
  403. * Enables the MIC field test
  404. *
  405. * \param [IN] upLinkCounter Fixed Tx packet counter value
  406. */
  407. void LoRaMacTestSetMic( uint16_t upLinkCounter );
  408. #endif /* __LORAMAC_API_V3_H__ */