lw_packets.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************
  2. Copyright (c) 2017 Theodor Tobias Rohde (tr@lobaro.com)
  3. Lobaro - Industrial IoT Solutions
  4. www.lobaro.com
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. *****************************************************************************/
  21. #ifndef DRV_LOBARO_LW_PACKETS_H_
  22. #define DRV_LOBARO_LW_PACKETS_H_
  23. #include <stdbool.h>
  24. #include <stddef.h>
  25. #include <github.com/lobaro/c-utils/datetime.h>
  26. #include "lw_state.h"
  27. // not supported yet!
  28. // yet only used to throw compiler warning at important positons
  29. #define USE_LORAWAN_1_1 (0)
  30. // LoRaWAN MAC header (MHDR)
  31. typedef enum {
  32. MTYPE_JOIN_REQUEST = 0,
  33. MTYPE_JOIN_ACCEPT,
  34. MTYPE_UNCONFIRMED_DATA_UP,
  35. MTYPE_UNCONFIRMED_DATA_DOWN,
  36. MTYPE_CONFIRMED_DATA_UP,
  37. MTYPE_CONFIRMED_DATA_DOWN,
  38. MTYPE_REJOIN_REQUEST,
  39. MTYPE_PROPRIETARY = 0b111
  40. } MHDR_Mtype_t;
  41. typedef enum {
  42. LORAWAN_R1 = 0,
  43. } MHDR_LoRaWAN_MajorVersion_t;
  44. typedef struct {
  45. MHDR_Mtype_t type;
  46. MHDR_LoRaWAN_MajorVersion_t version;
  47. } MHDR_t;
  48. typedef enum {
  49. ResetInd =0x01,
  50. ResetConf =0x01,
  51. LinkCheckReq =0x02,
  52. LinkCheckAns = 0x02,
  53. LinkADRReq = 0x03,
  54. LinkADRAns =0x03,
  55. DutyCycleReq = 0x04,
  56. DutyCycleAns = 0x04,
  57. RXParamSetupReq = 0x05,
  58. RXParamSetupAns = 0x05,
  59. DevStatusReq = 0x06,
  60. DevStatusAns = 0x06,
  61. NewChannelReq = 0x07,
  62. NewChannelAns = 0x07,
  63. RXTimingSetupReq = 0x08,
  64. RXTimingSetupAns = 0x08,
  65. TxParamSetupReq = 0x09,
  66. TxParamSetupAns = 0x09,
  67. DlChannelReq = 0x0A,
  68. DlChannelAns = 0x0A,
  69. RekeyInd = 0x0B,
  70. RekeyConf = 0x0B,
  71. ADRParamSetupReq = 0x0C,
  72. ADRParamSetupAns = 0x0C,
  73. DeviceTimeReq = 0x0D,
  74. DeviceTimeAns = 0x0D,
  75. ForceRejoinReq = 0x0E,
  76. RejoinParamSetupReq = 0x0F,
  77. RejoinParamSetupAns = 0x0F,
  78. // 0x80 .. 0xFF Reserved for proprietary network command extensions
  79. } Lorawan_MacCommand_t; // Corresponds to the CID
  80. // part of FHDR_FCtrl_t
  81. typedef struct {
  82. uint8_t ADR :1; // Adaptive data rate control bit
  83. uint8_t RFU :1; // Reserved for Future Use
  84. uint8_t ACK :1;
  85. uint8_t FPending :1;
  86. uint8_t FOptsLen :4;
  87. } FHDR_FCtrl_downlink_t;
  88. // part of FHDR_FCtrl_t
  89. typedef struct {
  90. uint8_t ADR :1; // Adaptive data rate control bit
  91. uint8_t ADRACKReq :1;
  92. uint8_t ACK :1;
  93. uint8_t ClassB :1;
  94. uint8_t FOptsLen :4;
  95. } FHDR_FCtrl_uplink_t;
  96. // part of FHDR_t
  97. typedef union {
  98. FHDR_FCtrl_downlink_t downlink;
  99. FHDR_FCtrl_uplink_t uplink;
  100. } FHDR_FCtrl_t;
  101. // part of MACPayload_t
  102. typedef struct {
  103. uint32_t DevAddr;
  104. FHDR_FCtrl_t FCtrl;
  105. uint16_t FCnt16; // only LSB of 32 bit frame Counter
  106. uint8_t FOpts[16]; // Max 15 bytes! Any reason for 16 byte? Alignment?
  107. } FHDR_t;
  108. // part of MsgBody_t (union)
  109. typedef struct {
  110. FHDR_t FHDR; // LoRaWAN Frame header
  111. //uint8_t* payload; // optional, NOTE: payload pointer is in lorawan_packet_t struct
  112. uint8_t payloadLength;
  113. uint8_t FPort; // must be set if payload is present else optional
  114. } MACPayload_t;
  115. // part of MsgBody_t (union)
  116. typedef struct {
  117. uint8_t joinEUI[8]; // before LoRaWAN1.1 this was also called the appEUI
  118. uint8_t devEUI[8];
  119. uint16_t devnonce; // must be random for each join request
  120. } JoinRequest_t;
  121. // part of JoinAccept_t
  122. typedef struct {
  123. uint8_t OptNeg :1;
  124. uint8_t Rx1DRoffset :3;
  125. uint8_t Rx2DR :4;
  126. } DLsettings_t;
  127. // part of JoinAccept_t
  128. typedef struct {
  129. uint8_t FreqCH4[3];
  130. uint8_t FreqCH5[3];
  131. uint8_t FreqCH6[3];
  132. uint8_t FreqCH7[3];
  133. uint8_t FreqCH8[3];
  134. // + RFU (1 Byte)
  135. } CFlist_t;
  136. // part of MsgBody_t (union)
  137. typedef struct {
  138. uint32_t JoinNonce; // 24 bit (3 Byte), server nonce
  139. uint32_t HomeNetID; // 24 bit (3 Byte), network identifier
  140. uint32_t DevAddr; // 32 bit (4 Byte), end-device address
  141. DLsettings_t DLsettings; // 8 bit (1 Byte), providing some of the downlink parameter
  142. uint8_t RxDelay; // 8 bit (1 Byte), the delay between TX and RX
  143. // total: 12
  144. CFlist_t CFlist; // 16 byte, optional list of network parameters (e.g. frequencies for EU868)
  145. // total: 12+16=28
  146. bool hasCFlist;
  147. uint8_t derived_nwkskey[16]; // todo use malloc instead
  148. uint8_t derived_appskey[16];
  149. } JoinAccept_t;
  150. typedef union {
  151. MACPayload_t MACPayload;
  152. JoinRequest_t JoinRequest;
  153. JoinAccept_t JoinAccept; // For Join-Accept, the MIC field is encrypted with the payload and is not a separate field
  154. } MsgBody_t;
  155. typedef struct {
  156. Time_t DeviceTime; // From DeviceTimeAns, 0 if not set
  157. } DecodedMacResponse_t;
  158. // Complete PHYPayload packet
  159. typedef struct {
  160. // user mutable fields
  161. MHDR_t MHDR; // LoRaWAN MAC header (1 Byte)
  162. MsgBody_t BODY; // MHDR defines either MACPayload OR Join/Rejoin-Request OR Join-Accept (then MIC encrypted inside payload)
  163. // calculated field
  164. uint32_t MIC;
  165. // internal control flag
  166. uint8_t* pPayload; // != NULL if the body contains some memory that must be freed (maybe the case for dataUp/dataDown msg)
  167. // this ensures a graceful delete of packet independent of MHDR type
  168. DecodedMacResponse_t MacResp; // Field to store decoded mac responses for the application
  169. } lorawan_packet_t;
  170. // external function dependencies
  171. // function pointers maybe NULL if using the defaults
  172. typedef struct {
  173. void* (*malloc)(size_t size); // default: malloc (stdlib.h)
  174. void (*free)(void* buf); // default: free (stdlib.h)
  175. void (*LogInfo)(const char * format, ...); // default: "logNothingDummy()" function
  176. void (*LogError)(const char * format, ...); // default: "logNothingDummy()" function
  177. } lwPackets_api_t;
  178. // external state dependencies
  179. // these parameters should be known by your LoRaWAN stack which uses this lib
  180. // maybe a simple wrapper is needed
  181. typedef struct {
  182. Lorawan_fcnt_t* pFCntCtrl; // pointer to external App netCtrl structure (see lw_state.h)
  183. Lorawan_devCfg_t* pDevCfg; // pointer to external App devCtrl structure (see lw_state.h)
  184. } lwPackets_state_t;
  185. // Setup external dependencies
  186. // must be called at least once before usage
  187. void LoRaWAN_PacketsUtil_Init(lwPackets_api_t api, lwPackets_state_t state);
  188. // LoRaWAN packet parser
  189. lorawan_packet_t* LoRaWAN_UnmarshalPacket(uint8_t* dataToParse, uint8_t length); // must be deleted again!
  190. // payload: optional external payload buffer with data to be copied into new packet
  191. // length: size of external payload
  192. // result: lorawan_packet_t* which must be deleted again by user!
  193. lorawan_packet_t* LoRaWAN_NewPacket(uint8_t* payload, uint8_t length); // must be deleted again!
  194. uint8_t LoRaWAN_MarshalPacket(lorawan_packet_t* packet, uint8_t* buffer, uint8_t bufferSize);
  195. void LoRaWAN_DeletePacket(lorawan_packet_t* packet);
  196. #endif