LoRaMacTypes.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*!
  2. * \file LoRaMacTypes.h
  3. *
  4. * \brief LoRa MAC layer internal types definition. Please do not include in application sources.
  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 Daniel Jaeckle ( STACKFORCE )
  28. *
  29. * \author Johannes Bruder ( STACKFORCE )
  30. *
  31. * addtogroup LORAMAC
  32. * \{
  33. *
  34. */
  35. #ifndef __LORAMAC_TYPES_H__
  36. #define __LORAMAC_TYPES_H__
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include "lora-radio-timer.h"
  44. /*!
  45. * Start value for unicast keys enumeration
  46. */
  47. #define LORAMAC_CRYPTO_UNICAST_KEYS 0
  48. /*!
  49. * Start value for multicast keys enumeration
  50. */
  51. #define LORAMAC_CRYPTO_MULTICAST_KEYS 127
  52. /*!
  53. * LoRaWAN devices classes definition
  54. *
  55. * LoRaWAN Specification V1.0.2, chapter 2.1
  56. */
  57. typedef enum DeviceClass_e
  58. {
  59. /*!
  60. * LoRaWAN device class A
  61. *
  62. * LoRaWAN Specification V1.0.2, chapter 3
  63. */
  64. CLASS_A = 0x00,
  65. /*!
  66. * LoRaWAN device class B
  67. *
  68. * LoRaWAN Specification V1.0.2, chapter 8
  69. */
  70. CLASS_B = 0x01,
  71. /*!
  72. * LoRaWAN device class C
  73. *
  74. * LoRaWAN Specification V1.0.2, chapter 17
  75. */
  76. CLASS_C = 0x02,
  77. }DeviceClass_t;
  78. /*!
  79. * LoRaWAN Frame type enumeration to differ between the possible data up/down frame configurations.
  80. *
  81. * Note: The naming is implementation specific since there is no definition
  82. * in the LoRaWAN specification included.
  83. */
  84. typedef enum eFType
  85. {
  86. /*!
  87. * Frame type A
  88. *
  89. * FOptsLen > 0, Fopt present, FPort > 0, FRMPayload present
  90. */
  91. FRAME_TYPE_A,
  92. /*!
  93. * Frame type B
  94. *
  95. * FOptsLen > 0, Fopt present, FPort not present, FRMPayload not present
  96. */
  97. FRAME_TYPE_B,
  98. /*!
  99. * Frame type C
  100. *
  101. * FOptsLen = 0, Fopt not present, FPort = 0 , FRMPayload containing MAC commands
  102. */
  103. FRAME_TYPE_C,
  104. /*!
  105. * Frame type D
  106. *
  107. * FOptsLen = 0, Fopt not present, FPort > 0 , FRMPayload present
  108. */
  109. FRAME_TYPE_D,
  110. }FType_t;
  111. /*!
  112. * LoRaWAN Frame counter identifier.
  113. */
  114. typedef enum eFCntIdentifier
  115. {
  116. /*!
  117. * Uplink frame counter which is incremented with each uplink.
  118. */
  119. FCNT_UP = 0,
  120. /*!
  121. * Network downlink frame counter which is incremented with each downlink on FPort 0
  122. * or when the FPort field is missing.
  123. */
  124. N_FCNT_DOWN,
  125. /*!
  126. * Application downlink frame counter which is incremented with each downlink
  127. * on a port different than 0.
  128. */
  129. A_FCNT_DOWN,
  130. /*!
  131. * In case if the device is connected to a LoRaWAN 1.0 Server,
  132. * this counter is used for every kind of downlink frame.
  133. */
  134. FCNT_DOWN,
  135. /*!
  136. * Multicast downlink counter for index 0
  137. */
  138. MC_FCNT_DOWN_0,
  139. /*!
  140. * Multicast downlink counter for index 1
  141. */
  142. MC_FCNT_DOWN_1,
  143. /*!
  144. * Multicast downlink counter for index 2
  145. */
  146. MC_FCNT_DOWN_2,
  147. /*!
  148. * Multicast downlink counter for index 3
  149. */
  150. MC_FCNT_DOWN_3,
  151. }FCntIdentifier_t;
  152. /*!
  153. * LoRaMac Key identifier
  154. */
  155. typedef enum eKeyIdentifier
  156. {
  157. /*!
  158. * Application root key
  159. */
  160. APP_KEY = 0,
  161. /*!
  162. * Application root key
  163. * Used to derive McRootKey for 1.0.x devices
  164. */
  165. GEN_APP_KEY,
  166. /*!
  167. * Network root key
  168. */
  169. NWK_KEY,
  170. /*!
  171. * Join session integrity key
  172. */
  173. J_S_INT_KEY,
  174. /*!
  175. * Join session encryption key
  176. */
  177. J_S_ENC_KEY,
  178. /*!
  179. * Forwarding Network session integrity key
  180. */
  181. F_NWK_S_INT_KEY,
  182. /*!
  183. * Serving Network session integrity key
  184. */
  185. S_NWK_S_INT_KEY,
  186. /*!
  187. * Network session encryption key
  188. */
  189. NWK_S_ENC_KEY,
  190. /*!
  191. * Application session key
  192. */
  193. APP_S_KEY,
  194. /*!
  195. * Multicast root key
  196. */
  197. MC_ROOT_KEY,
  198. /*!
  199. * Multicast key encryption key
  200. */
  201. MC_KE_KEY = LORAMAC_CRYPTO_MULTICAST_KEYS,
  202. /*!
  203. * Multicast root key index 0
  204. */
  205. MC_KEY_0,
  206. /*!
  207. * Multicast Application session key index 0
  208. */
  209. MC_APP_S_KEY_0,
  210. /*!
  211. * Multicast Network session key index 0
  212. */
  213. MC_NWK_S_KEY_0,
  214. /*!
  215. * Multicast root key index 1
  216. */
  217. MC_KEY_1,
  218. /*!
  219. * Multicast Application session key index 1
  220. */
  221. MC_APP_S_KEY_1,
  222. /*!
  223. * Multicast Network session key index 1
  224. */
  225. MC_NWK_S_KEY_1,
  226. /*!
  227. * Multicast root key index 2
  228. */
  229. MC_KEY_2,
  230. /*!
  231. * Multicast Application session key index 2
  232. */
  233. MC_APP_S_KEY_2,
  234. /*!
  235. * Multicast Network session key index 2
  236. */
  237. MC_NWK_S_KEY_2,
  238. /*!
  239. * Multicast root key index 3
  240. */
  241. MC_KEY_3,
  242. /*!
  243. * Multicast Application session key index 3
  244. */
  245. MC_APP_S_KEY_3,
  246. /*!
  247. * Multicast Network session key index 3
  248. */
  249. MC_NWK_S_KEY_3,
  250. /*!
  251. * Zero key for slot randomization in class B
  252. */
  253. SLOT_RAND_ZERO_KEY,
  254. /*!
  255. * No Key
  256. */
  257. NO_KEY,
  258. }KeyIdentifier_t;
  259. /*!
  260. * LoRaMac Crypto address identifier
  261. */
  262. typedef enum eAddressIdentifier
  263. {
  264. /*!
  265. * Multicast Address for index 0
  266. */
  267. MULTICAST_0_ADDR = 0,
  268. /*!
  269. * Multicast Address for index 1
  270. */
  271. MULTICAST_1_ADDR = 1,
  272. /*!
  273. * Multicast Address for index 2
  274. */
  275. MULTICAST_2_ADDR = 2,
  276. /*!
  277. * Multicast Address for index 3
  278. */
  279. MULTICAST_3_ADDR = 3,
  280. /*!
  281. * Unicast End-device address
  282. */
  283. UNICAST_DEV_ADDR = 4,
  284. }AddressIdentifier_t;
  285. /*
  286. * Multicast Rx window parameters
  287. */
  288. typedef union uMcRxParams
  289. {
  290. struct
  291. {
  292. /*!
  293. * Reception frequency of the ping slot windows
  294. */
  295. uint32_t Frequency;
  296. /*!
  297. * Datarate of the ping slot
  298. */
  299. int8_t Datarate;
  300. /*!
  301. * This parameter is necessary for class B operation. It defines the
  302. * periodicity of the multicast downlink slots
  303. */
  304. uint16_t Periodicity;
  305. }ClassB;
  306. struct
  307. {
  308. /*!
  309. * Reception frequency of the ping slot windows
  310. */
  311. uint32_t Frequency;
  312. /*!
  313. * Datarate of the ping slot
  314. */
  315. int8_t Datarate;
  316. }ClassC;
  317. }McRxParams_t;
  318. /*!
  319. * Multicast channel
  320. */
  321. typedef struct sMcChannelParams
  322. {
  323. /*!
  324. * Multicats channel LoRaWAN class B or C
  325. */
  326. DeviceClass_t Class;
  327. /*!
  328. * True if the entry is active
  329. */
  330. bool IsEnabled;
  331. /*
  332. * Address identifier
  333. */
  334. AddressIdentifier_t GroupID;
  335. /*!
  336. * Address
  337. */
  338. uint32_t Address;
  339. /*!
  340. * Encrypted multicast key
  341. */
  342. uint8_t *McKeyE;
  343. /*!
  344. * Minimum multicast frame counter value
  345. */
  346. uint32_t FCountMin;
  347. /*!
  348. * Maximum multicast frame counter value
  349. */
  350. uint32_t FCountMax;
  351. /*!
  352. * Multicast reception parameters
  353. */
  354. McRxParams_t RxParams;
  355. }McChannelParams_t;
  356. /*!
  357. * Multicast context
  358. */
  359. typedef struct sMulticastCtx
  360. {
  361. /*!
  362. * Multicast channel parameters
  363. */
  364. McChannelParams_t ChannelParams;
  365. /*!
  366. * Downlink counter
  367. */
  368. uint32_t* DownLinkCounter;
  369. /*
  370. * Following parameters are only used for ClassB multicast channels
  371. */
  372. /*!
  373. * Number of multicast slots. The variable can be
  374. * calculated as follows:
  375. * PingNb = 128 / ( 1 << periodicity ), where
  376. * 0 <= periodicity <= 7
  377. */
  378. uint8_t PingNb;
  379. /*!
  380. * Period of the multicast slots. The variable can be
  381. * calculated as follows:
  382. * PingPeriod = 4096 / PingNb
  383. */
  384. uint16_t PingPeriod;
  385. /*!
  386. * Ping offset of the multicast channel for Class B
  387. */
  388. uint16_t PingOffset;
  389. }MulticastCtx_t;
  390. /*!
  391. * LoRaMac join-request / rejoin type identifier
  392. */
  393. typedef enum eJoinReqIdentifier
  394. {
  395. /*!
  396. * Rejoin type 0
  397. */
  398. REJOIN_REQ_0 = 0x00,
  399. /*!
  400. * Rejoin type 1
  401. */
  402. REJOIN_REQ_1 = 0x01,
  403. /*!
  404. * Rejoin type 2
  405. */
  406. REJOIN_REQ_2 = 0x02,
  407. /*!
  408. * Join-request
  409. */
  410. JOIN_REQ = 0xFF,
  411. }JoinReqIdentifier_t;
  412. /*!
  413. * LoRaMAC mote MAC commands
  414. *
  415. * LoRaWAN Specification V1.1.0, chapter 5, table 4
  416. */
  417. typedef enum eLoRaMacMoteCmd
  418. {
  419. /*!
  420. * LinkCheckReq
  421. */
  422. MOTE_MAC_LINK_CHECK_REQ = 0x02,
  423. /*!
  424. * LinkADRAns
  425. */
  426. MOTE_MAC_LINK_ADR_ANS = 0x03,
  427. /*!
  428. * DutyCycleAns
  429. */
  430. MOTE_MAC_DUTY_CYCLE_ANS = 0x04,
  431. /*!
  432. * RXParamSetupAns
  433. */
  434. MOTE_MAC_RX_PARAM_SETUP_ANS = 0x05,
  435. /*!
  436. * DevStatusAns
  437. */
  438. MOTE_MAC_DEV_STATUS_ANS = 0x06,
  439. /*!
  440. * NewChannelAns
  441. */
  442. MOTE_MAC_NEW_CHANNEL_ANS = 0x07,
  443. /*!
  444. * RXTimingSetupAns
  445. */
  446. MOTE_MAC_RX_TIMING_SETUP_ANS = 0x08,
  447. /*!
  448. * TXParamSetupAns
  449. */
  450. MOTE_MAC_TX_PARAM_SETUP_ANS = 0x09,
  451. /*!
  452. * DlChannelAns
  453. */
  454. MOTE_MAC_DL_CHANNEL_ANS = 0x0A,
  455. /*!
  456. * DeviceTimeReq
  457. */
  458. MOTE_MAC_DEVICE_TIME_REQ = 0x0D,
  459. /*!
  460. * PingSlotInfoReq
  461. */
  462. MOTE_MAC_PING_SLOT_INFO_REQ = 0x10,
  463. /*!
  464. * PingSlotFreqAns
  465. */
  466. MOTE_MAC_PING_SLOT_FREQ_ANS = 0x11,
  467. /*!
  468. * BeaconTimingReq
  469. */
  470. MOTE_MAC_BEACON_TIMING_REQ = 0x12,
  471. /*!
  472. * BeaconFreqAns
  473. */
  474. MOTE_MAC_BEACON_FREQ_ANS = 0x13,
  475. }LoRaMacMoteCmd_t;
  476. /*!
  477. * LoRaMAC server MAC commands
  478. *
  479. * LoRaWAN Specification V1.1.0 chapter 5, table 4
  480. */
  481. typedef enum eLoRaMacSrvCmd
  482. {
  483. /*!
  484. * ResetInd
  485. */
  486. SRV_MAC_RESET_CONF = 0x01,
  487. /*!
  488. * LinkCheckAns
  489. */
  490. SRV_MAC_LINK_CHECK_ANS = 0x02,
  491. /*!
  492. * LinkADRReq
  493. */
  494. SRV_MAC_LINK_ADR_REQ = 0x03,
  495. /*!
  496. * DutyCycleReq
  497. */
  498. SRV_MAC_DUTY_CYCLE_REQ = 0x04,
  499. /*!
  500. * RXParamSetupReq
  501. */
  502. SRV_MAC_RX_PARAM_SETUP_REQ = 0x05,
  503. /*!
  504. * DevStatusReq
  505. */
  506. SRV_MAC_DEV_STATUS_REQ = 0x06,
  507. /*!
  508. * NewChannelReq
  509. */
  510. SRV_MAC_NEW_CHANNEL_REQ = 0x07,
  511. /*!
  512. * RXTimingSetupReq
  513. */
  514. SRV_MAC_RX_TIMING_SETUP_REQ = 0x08,
  515. /*!
  516. * NewChannelReq
  517. */
  518. SRV_MAC_TX_PARAM_SETUP_REQ = 0x09,
  519. /*!
  520. * DlChannelReq
  521. */
  522. SRV_MAC_DL_CHANNEL_REQ = 0x0A,
  523. /*!
  524. * DeviceTimeAns
  525. */
  526. SRV_MAC_DEVICE_TIME_ANS = 0x0D,
  527. /*!
  528. * PingSlotInfoAns
  529. */
  530. SRV_MAC_PING_SLOT_INFO_ANS = 0x10,
  531. /*!
  532. * PingSlotChannelReq
  533. */
  534. SRV_MAC_PING_SLOT_CHANNEL_REQ = 0x11,
  535. /*!
  536. * BeaconTimingAns
  537. */
  538. SRV_MAC_BEACON_TIMING_ANS = 0x12,
  539. /*!
  540. * BeaconFreqReq
  541. */
  542. SRV_MAC_BEACON_FREQ_REQ = 0x13,
  543. }LoRaMacSrvCmd_t;
  544. /*!
  545. * LoRaMAC band parameters definition
  546. */
  547. typedef struct sBand
  548. {
  549. /*!
  550. * Duty cycle
  551. */
  552. uint16_t DCycle;
  553. /*!
  554. * Maximum Tx power
  555. */
  556. int8_t TxMaxPower;
  557. /*!
  558. * Time stamp of the last JoinReq Tx frame.
  559. */
  560. TimerTime_t LastJoinTxDoneTime;
  561. /*!
  562. * Time stamp of the last Tx frame
  563. */
  564. TimerTime_t LastTxDoneTime;
  565. /*!
  566. * Holds the time where the device is off
  567. */
  568. TimerTime_t TimeOff;
  569. }Band_t;
  570. /*!
  571. * LoRaMAC frame types
  572. *
  573. * LoRaWAN Specification V1.0.2, chapter 4.2.1, table 1
  574. */
  575. typedef enum eLoRaMacFrameType
  576. {
  577. /*!
  578. * LoRaMAC join request frame
  579. */
  580. FRAME_TYPE_JOIN_REQ = 0x00,
  581. /*!
  582. * LoRaMAC join accept frame
  583. */
  584. FRAME_TYPE_JOIN_ACCEPT = 0x01,
  585. /*!
  586. * LoRaMAC unconfirmed up-link frame
  587. */
  588. FRAME_TYPE_DATA_UNCONFIRMED_UP = 0x02,
  589. /*!
  590. * LoRaMAC unconfirmed down-link frame
  591. */
  592. FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03,
  593. /*!
  594. * LoRaMAC confirmed up-link frame
  595. */
  596. FRAME_TYPE_DATA_CONFIRMED_UP = 0x04,
  597. /*!
  598. * LoRaMAC confirmed down-link frame
  599. */
  600. FRAME_TYPE_DATA_CONFIRMED_DOWN = 0x05,
  601. /*!
  602. * LoRaMAC proprietary frame
  603. */
  604. FRAME_TYPE_PROPRIETARY = 0x07,
  605. }LoRaMacFrameType_t;
  606. /*!
  607. * LoRaMAC Battery level indicator
  608. */
  609. typedef enum eLoRaMacBatteryLevel
  610. {
  611. /*!
  612. * External power source
  613. */
  614. BAT_LEVEL_EXT_SRC = 0x00,
  615. /*!
  616. * Battery level empty
  617. */
  618. BAT_LEVEL_EMPTY = 0x01,
  619. /*!
  620. * Battery level full
  621. */
  622. BAT_LEVEL_FULL = 0xFE,
  623. /*!
  624. * Battery level - no measurement available
  625. */
  626. BAT_LEVEL_NO_MEASURE = 0xFF,
  627. }LoRaMacBatteryLevel_t;
  628. #ifdef __cplusplus
  629. }
  630. #endif
  631. #endif // __LORAMAC_TYPES_H__