ESP32 Transceiver IEEE 802.15.4 Library
Loading...
Searching...
No Matches
Frame.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdbool.h>
4#include <stddef.h>
5#include <stdint.h>
6
7#include <cstdio>
8#include <cstring>
9#include <vector>
10
11#include "esp_assert.h"
12#include "esp_ieee802154.h"
13
14// Constants
15#define IEEE802154_FCF_SIZE 2
16#define IEEE802154_MAX_ADDR_LEN 8
17#define IEEE802154_PAN_ID_LEN 2
18#define IEEE802154_RSSI_LQI_SIZE 1 // 1 byte for combined RSSI and LQI
19
20#define MAX_FRAME_LEN 128
21
22namespace ieee802154 {
23
25enum class Frameype_t : uint8_t {
26 BEACON = 0x0, // Beacon frame
27 DATA = 0x1, // Data frame
28 ACK = 0x2, // Acknowledgment frame
29 MAC_CMD = 0x3, // MAC Command frame
30 // 0x4 to 0x7 are reserved
31};
32
34enum class addr_mode_t : uint8_t {
35 NONE = 0x0, // No address
36 RESERVED = 0x1, // Reserved
37 SHORT = 0x2, // 16-bit short address
38 EXTENDED = 0x3, // 64-bit extended address
39};
40
42enum class frame_version_t : uint8_t {
43 V_2003 = 0x0, // IEEE 802.15.4-2003
44 V_2006 = 0x1, // IEEE 802.15.4-2006
45 V_RESERVED1 = 0x2, // Reserved
46 V_RESERVED2 = 0x3, // Reserved
47};
48
54 uint8_t frameType : 3 = 0x1;
55 uint8_t securityEnabled : 1 = 0; // Security Enabled (bit 3)
56 uint8_t framePending : 1 = 0; // Frame Pending (bit 4)
57 uint8_t ackRequest : 1 = 0; // Acknowledgment Request (bit 5)
58 uint8_t panIdCompression : 1 = 0; // PAN ID Compression (bit 6)
59 uint8_t reserved : 1 = 0; // Reserved (bit 7)
61 0; // Sequence Number Suppression (bit 8)
63 0; // Information Elements Present (bit 9)
64 uint8_t destAddrMode : 2 = 0; // Destination Address Mode (bits 10-11)
65 uint8_t frameVersion : 2 =
66 (uint8_t)frame_version_t::V_2006; // Frame Version (bits 12-13)
67 uint8_t srcAddrMode : 2 = 0; // Source Address Mode (bits 14-15)
68};
69
77class Address {
78 public:
82 Address() = default;
88 Address(const uint8_t* addr, addr_mode_t mode) : local_addr_mode(mode) {
89 if (mode == addr_mode_t::SHORT) {
90 memcpy(local_address, addr, 2);
91 } else if (mode == addr_mode_t::EXTENDED) {
92 memcpy(local_address, addr, 8);
93 }
94 }
95
102 template <size_t N>
103 Address(const uint8_t (&addr)[N]) {
104 static_assert(N == 2 || N == 8, "Address must be 2 or 8 bytes");
105 if constexpr (N == 2) {
107 memcpy(local_address, addr, 2);
108 } else if constexpr (N == 8) {
110 memcpy(local_address, addr, 8);
111 }
112 }
113
118 uint8_t* data() { return local_address; }
119
125
130 const char* to_str() const { return to_str(local_address, local_addr_mode); }
131
138 static const char* to_str(const uint8_t* addr, int len) {
139 static char str[25];
140 if (len == 2) {
141 snprintf(str, sizeof(str), "%02X:%02X", addr[0], addr[1]);
142 } else if (len == 8) {
143 snprintf(str, sizeof(str), "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
144 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6],
145 addr[7]);
146 } else {
147 snprintf(str, sizeof(str), "Invalid");
148 }
149 return str;
150 }
151
158 static const char* to_str(const uint8_t* addr, addr_mode_t mode) {
159 switch (mode) {
161 return "None";
163 return to_str(addr, 2);
165 return to_str(addr, 8);
166 default:
167 return "Invalid";
168 }
169 }
170
171 protected:
179 uint8_t local_address[8] = {0};
180};
181
183struct Frame {
184 FrameControlField fcf{}; // Frame Control Field
185 uint8_t sequenceNumber = 0; // Sequence Number (if not suppressed)
186 uint16_t destPanId = 0; // Destination PAN ID (if present)
187 uint8_t destAddress[8]; // Destination Address (0, 2, or 8 bytes)
188 uint16_t srcPanId = 0; // Source PAN ID (if present)
189 uint8_t srcAddress[8]; // Source Address (0, 2, or 8 bytes)
190 uint8_t destAddrLen = 0; // Length of destination address
191 uint8_t srcAddrLen = 0; // Length of source address
192 size_t payloadLen = 0; // Length of payload
193 uint8_t* payload = nullptr; // Pointer to payload data
194 uint8_t rssi_lqi = 0; // RSSI and LQI (combined in 1 byte)
195
197 bool parse(const uint8_t* data, bool verbose);
198
200 size_t build(uint8_t* buffer, bool verbose) const;
201
203 const char* to_str(uint8_t frameType);
204
206 void setSourceAddress(Address address) {
207 memcpy(srcAddress, address.data(),
208 address.mode() == addr_mode_t::SHORT ? 2 : 8);
209 srcAddrLen = address.mode() == addr_mode_t::SHORT ? 2 : 8;
210 fcf.srcAddrMode = static_cast<uint8_t>(address.mode());
211 }
212
215 memcpy(destAddress, address.data(),
216 address.mode() == addr_mode_t::SHORT ? 2 : 8);
217 destAddrLen = address.mode() == addr_mode_t::SHORT ? 2 : 8;
218 fcf.destAddrMode = static_cast<uint8_t>(address.mode());
219 }
220
222 void setPayload(const uint8_t* data, size_t len) {
223 if (buffer.size() < len) {
224 buffer.resize(len); // Resize buffer if needed
225 }
226 payload = buffer.data();
227 memcpy(payload, data, len);
228 payloadLen = len;
229 }
230
232 void setPAN(uint16_t panId) {
233 destPanId = panId;
234 srcPanId = panId;
235 fcf.panIdCompression = 1; // Enable PAN ID Compression
236 }
237
238 protected:
239 std::vector<uint8_t> buffer; // Buffer for building frames
240};
241
244 uint8_t frame[MAX_FRAME_LEN]; // Raw frame data
245 esp_ieee802154_frame_info_t frame_info; // Frame info (RSSI, LQI, etc.)
246};
247
248
249// Ensure FCF structure is exactly 2 bytes
251 "ieee802154_fcf_t must be 2 bytes");
252
253} // namespace ieee802154
#define IEEE802154_FCF_SIZE
Definition Frame.h:15
#define MAX_FRAME_LEN
Definition Frame.h:20
IEEE 802.15.4 Address abstraction.
Definition Frame.h:77
Address(const uint8_t *addr, addr_mode_t mode)
Construct an Address from a pointer and mode.
Definition Frame.h:88
Address(const uint8_t(&addr)[N])
Template constructor to deduce address length and mode at compile time.
Definition Frame.h:103
addr_mode_t mode()
Get the address mode (NONE, SHORT, EXTENDED).
Definition Frame.h:124
addr_mode_t local_addr_mode
Local address mode (NONE, SHORT, EXTENDED).
Definition Frame.h:175
Address()=default
Default constructor. Initializes address mode to NONE.
static const char * to_str(const uint8_t *addr, int len)
Get a human-readable string for a raw address and length.
Definition Frame.h:138
uint8_t * data()
Get a pointer to the address bytes.
Definition Frame.h:118
static const char * to_str(const uint8_t *addr, addr_mode_t mode)
Get a human-readable string for a raw address and mode.
Definition Frame.h:158
uint8_t local_address[8]
Local address bytes (0, 2, or 8 bytes used).
Definition Frame.h:179
const char * to_str() const
Get a human-readable string representation of the address.
Definition Frame.h:130
Definition ESP32TransceiverIEEE802_15_4.cpp:15
frame_version_t
IEEE 802.15.4 frame version enumerations.
Definition Frame.h:42
addr_mode_t
IEEE 802.15.4 address mode enumerations.
Definition Frame.h:34
ESP_STATIC_ASSERT(sizeof(FrameControlField)==IEEE802154_FCF_SIZE, "ieee802154_fcf_t must be 2 bytes")
Frameype_t
IEEE 802.15.4 FCF field value enumerations.
Definition Frame.h:25
IEEE 802.15.4 Frame Control Field (FCF) structure Bit fields are ordered LSB to MSB to match IEEE 802...
Definition Frame.h:53
uint8_t informationElementsPresent
Definition Frame.h:63
uint8_t frameType
Definition Frame.h:54
uint8_t destAddrMode
Definition Frame.h:64
uint8_t framePending
Definition Frame.h:56
uint8_t frameVersion
Definition Frame.h:66
uint8_t panIdCompression
Definition Frame.h:58
uint8_t srcAddrMode
Definition Frame.h:67
uint8_t reserved
Definition Frame.h:59
uint8_t securityEnabled
Definition Frame.h:55
uint8_t sequenceNumberSuppression
Definition Frame.h:61
uint8_t ackRequest
Definition Frame.h:57
IEEE 802.15.4 MAC frame structure.
Definition Frame.h:183
void setDestinationAddress(Address address)
Set the destination address for the frame.
Definition Frame.h:214
uint8_t rssi_lqi
Definition Frame.h:194
uint16_t srcPanId
Definition Frame.h:188
uint8_t sequenceNumber
Definition Frame.h:185
uint8_t srcAddrLen
Definition Frame.h:191
uint8_t srcAddress[8]
Definition Frame.h:189
uint8_t * payload
Definition Frame.h:193
FrameControlField fcf
Definition Frame.h:184
void setPayload(const uint8_t *data, size_t len)
Set the payload for the frame.
Definition Frame.h:222
size_t build(uint8_t *buffer, bool verbose) const
Build IEEE 802.15.4 frame array with length byte at start and 0x00 at end.
Definition Frame.cpp:284
size_t payloadLen
Definition Frame.h:192
uint8_t destAddrLen
Definition Frame.h:190
const char * to_str(uint8_t frameType)
Get a human-readable string representation of the frame.
Definition Frame.cpp:378
uint16_t destPanId
Definition Frame.h:186
void setPAN(uint16_t panId)
Defines the Personal Area Network Identifier (PAN ID) for the frame.
Definition Frame.h:232
uint8_t destAddress[8]
Definition Frame.h:187
bool parse(const uint8_t *data, bool verbose)
parse frame from raw data
Definition Frame.cpp:127
std::vector< uint8_t > buffer
Definition Frame.h:239
void setSourceAddress(Address address)
Set the source address for the frame.
Definition Frame.h:206
Structure to hold frame data and frame info.
Definition Frame.h:243
esp_ieee802154_frame_info_t frame_info
Definition Frame.h:245
uint8_t frame[MAX_FRAME_LEN]
Definition Frame.h:244