LoRaMac.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*!
  2. * \file LoRaMac.h
  3. *
  4. * \brief LoRa MAC 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. * \defgroup LORAMAC LoRa MAC layer implementation
  32. * This module specifies the API implementation of the LoRaMAC layer.
  33. * This is a placeholder for a detailed description of the LoRaMac
  34. * layer and the supported features.
  35. * \{
  36. *
  37. * \example classA/LoRaMote/main.c
  38. * LoRaWAN class A application example for the LoRaMote.
  39. *
  40. * \example classB/LoRaMote/main.c
  41. * LoRaWAN class B application example for the LoRaMote.
  42. *
  43. * \example classC/LoRaMote/main.c
  44. * LoRaWAN class C application example for the LoRaMote.
  45. */
  46. #ifndef __LORAMAC_H__
  47. #define __LORAMAC_H__
  48. // Includes board dependent definitions such as channels frequencies
  49. //#include "LoRaMac-board.h"
  50. /********************************
  51. Lora物理层帧结构定义
  52. ********************************/
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. //#include <malloc.h>
  56. #include <stdint.h>
  57. #include <stdbool.h>
  58. /*!
  59. * LoRaMAC Status
  60. */
  61. typedef enum eLoRaMacStatus
  62. {
  63. /*!
  64. * Service started successfully
  65. */
  66. LORAMAC_STATUS_OK,
  67. /*!
  68. * Service not started - LoRaMAC is busy
  69. */
  70. LORAMAC_STATUS_BUSY,
  71. /*!
  72. * Service unknown
  73. */
  74. LORAMAC_STATUS_SERVICE_UNKNOWN,
  75. /*!
  76. * Service not started - invalid parameter
  77. */
  78. LORAMAC_STATUS_PARAMETER_INVALID,
  79. /*!
  80. * Service not started - invalid frequency
  81. */
  82. LORAMAC_STATUS_FREQUENCY_INVALID,
  83. /*!
  84. * Service not started - invalid datarate
  85. */
  86. LORAMAC_STATUS_DATARATE_INVALID,
  87. /*!
  88. * Service not started - invalid frequency and datarate
  89. */
  90. LORAMAC_STATUS_FREQ_AND_DR_INVALID,
  91. /*!
  92. * Service not started - the device is not in a LoRaWAN
  93. */
  94. LORAMAC_STATUS_NO_NETWORK_JOINED,
  95. /*!
  96. * Service not started - playload lenght error
  97. */
  98. LORAMAC_STATUS_LENGTH_ERROR,
  99. /*!
  100. * Service not started - playload lenght error
  101. */
  102. LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR,
  103. /*!
  104. * Service not started - the device is switched off
  105. */
  106. LORAMAC_STATUS_DEVICE_OFF,
  107. }LoRaMacStatus_t;
  108. /*!
  109. * LoRaMAC frame types
  110. *
  111. * LoRaWAN Specification V1.0, chapter 4.2.1, table 1
  112. */
  113. typedef enum eLoRaMacFrameType
  114. {
  115. /*!
  116. * LoRaMAC join request frame
  117. */
  118. FRAME_TYPE_JOIN_REQ = 0x00,
  119. /*!
  120. * LoRaMAC join accept frame
  121. */
  122. FRAME_TYPE_JOIN_ACCEPT = 0x01,
  123. /*!
  124. * LoRaMAC unconfirmed up-link frame
  125. */
  126. FRAME_TYPE_DATA_UNCONFIRMED_UP = 0x02,
  127. /*!
  128. * LoRaMAC unconfirmed down-link frame
  129. */
  130. FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03,
  131. /*!
  132. * LoRaMAC confirmed up-link frame
  133. */
  134. FRAME_TYPE_DATA_CONFIRMED_UP = 0x04,
  135. /*!
  136. * LoRaMAC confirmed down-link frame
  137. */
  138. FRAME_TYPE_DATA_CONFIRMED_DOWN = 0x05,
  139. /*!
  140. * LoRaMAC RFU frame
  141. */
  142. FRAME_TYPE_RFU = 0x06,
  143. /*!
  144. * LoRaMAC proprietary frame
  145. */
  146. FRAME_TYPE_PROPRIETARY = 0x07,
  147. }LoRaMacFrameType_t;
  148. #define UP_LINK 0
  149. /*!
  150. * Frame direction definition for down-link communications
  151. */
  152. #define DOWN_LINK 1
  153. /*!
  154. * Sets the length of the LoRaMAC footer field.
  155. * Mainly indicates the MIC field length
  156. */
  157. #define LORAMAC_MFR_LEN 4
  158. /*!
  159. * LoRaMAC header field definition (MHDR field)
  160. *
  161. * LoRaWAN Specification V1.0, chapter 4.2
  162. */
  163. typedef union uLoRaMacHeader
  164. {
  165. /*!
  166. * Byte-access to the bits
  167. */
  168. uint8_t Value;
  169. /*!
  170. * Structure containing single access to header bits
  171. */
  172. struct sHdrBits
  173. {
  174. /*!
  175. * Major version
  176. */
  177. uint8_t Major : 2;
  178. /*!
  179. * RFU
  180. */
  181. uint8_t RFU : 3;
  182. /*!
  183. * Message type
  184. */
  185. uint8_t MType : 3;
  186. }Bits;
  187. }LoRaMacHeader_t;
  188. /*!
  189. * LoRaMAC frame control field definition (FCtrl)
  190. *
  191. * LoRaWAN Specification V1.0, chapter 4.3.1
  192. */
  193. typedef union uLoRaMacFrameCtrl
  194. {
  195. /*!
  196. * Byte-access to the bits
  197. */
  198. uint8_t Value;
  199. struct sCtrlBits
  200. {
  201. /*!
  202. * Frame options length
  203. */
  204. uint8_t FOptsLen : 4;
  205. /*!
  206. * Frame pending bit
  207. */
  208. uint8_t FPending : 1;
  209. /*!
  210. * Message acknowledge bit
  211. */
  212. uint8_t Ack : 1;
  213. /*!
  214. * ADR acknowledgment request bit
  215. */
  216. uint8_t AdrAckReq : 1;
  217. /*!
  218. * ADR control in frame header
  219. */
  220. uint8_t Adr : 1;
  221. }Bits;
  222. }LoRaMacFrameCtrl_t;
  223. /*!
  224. * Device IEEE EUI
  225. */
  226. //static uint8_t *LoRaMacDevEui;
  227. typedef struct
  228. {
  229. uint8_t ADR; //实际只用1bit
  230. uint8_t ADRACKReq; //实际只用1bit
  231. uint8_t ACK; //实际只用1bit
  232. uint8_t FOptsLen; //实际只用4bit
  233. }FCtrl_t;
  234. typedef struct
  235. {
  236. uint32_t DevAddr;
  237. LoRaMacFrameCtrl_t FCtrl;
  238. uint16_t FCnt;
  239. uint8_t Fopts; //变长,约定1字节
  240. }FHDR_t;
  241. typedef struct
  242. {
  243. uint8_t AppEUI[8]; //uint64_t AppEUI;
  244. uint8_t DevEUI[8]; //uint64_t DevEUI;
  245. uint16_t DevNonce;
  246. uint32_t AppNonce; //实际只用3字节
  247. uint32_t NetID; //实际只用3字节
  248. uint32_t DevAddr;
  249. uint8_t DLSettings;
  250. uint8_t RxDelay;
  251. uint8_t CFList[16];
  252. FHDR_t FHDR;
  253. uint8_t Fport; //变长,约定1字节
  254. uint8_t FRMPayload[128];
  255. }MACPayload_t;
  256. typedef struct
  257. {
  258. LoRaMacHeader_t MHDR;
  259. MACPayload_t MACPayload;
  260. uint32_t MIC;
  261. }PHYPayload_t;
  262. typedef struct sPHYPayload
  263. {
  264. PHYPayload_t *PHYPayload;
  265. struct sPHYPayload *Next;
  266. }sPHYPayload_t;
  267. void sendJoinRequest(void);
  268. void sendUnconfirmUp(void);
  269. #endif // __LORAMAC_H__