esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
zb_mac_frame.h
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 *
4 * IEEE 802.15.4 MAC frames as a Zigbee coordinator needs them (frame version 0, no MAC
5 * security): parse any frame header, build and parse beacons, build the commands and data
6 * frames the coordinator sends. Buffers never include the FCS (the radio adds and checks it).
7 * Portable C, no Zephyr. Layouts: IEEE 802.15.4-2015 section 7; examples with their FCS in
8 * tests/unit/zigbee_mac.
9 */
10#ifndef ZIGBEE_ZB_MAC_FRAME_H_
11#define ZIGBEE_ZB_MAC_FRAME_H_
12
13#include <stdbool.h>
14#include <stddef.h>
15#include <stdint.h>
16
17/* The radio takes at most 125 bytes (127 with the FCS and the 2 byte PHR margin of the driver). */
18#define ZB_MAC_MAX_FRAME 125
19
20#ifndef BIT
21#define BIT(n) (1U << (n))
22#endif
23
24enum zb_mac_ftype {
25 ZB_FT_BEACON = 0,
26 ZB_FT_DATA = 1,
27 ZB_FT_ACK = 2,
28 ZB_FT_CMD = 3,
29};
30
31enum zb_mac_cmd {
32 ZB_CMD_ASSOC_REQ = 0x01,
33 ZB_CMD_ASSOC_RSP = 0x02,
34 ZB_CMD_DISASSOC = 0x03,
35 ZB_CMD_DATA_REQ = 0x04,
36 ZB_CMD_PAN_CONFLICT = 0x05,
37 ZB_CMD_ORPHAN = 0x06,
38 ZB_CMD_BEACON_REQ = 0x07,
39 ZB_CMD_REALIGN = 0x08,
40 ZB_CMD_GTS_REQ = 0x09,
41};
42
43enum zb_addr_mode {
44 ZB_ADDR_NONE = 0,
45 ZB_ADDR_SHORT = 2,
46 ZB_ADDR_EXT = 3,
47};
48
49enum zb_assoc_status {
50 ZB_ASSOC_OK = 0x00,
51 ZB_ASSOC_PAN_FULL = 0x01,
52 ZB_ASSOC_DENIED = 0x02,
53};
54
55#define ZB_PAN_BROADCAST 0xFFFF
56#define ZB_SHORT_BROADCAST 0xFFFF
57#define ZB_SHORT_NONE 0xFFFE
58
59/* Capability information of an association request. */
60#define ZB_CAP_FFD BIT(1)
61#define ZB_CAP_MAINS BIT(2)
62#define ZB_CAP_RX_IDLE BIT(3)
63#define ZB_CAP_SECURITY BIT(6)
64#define ZB_CAP_ALLOC_ADDR BIT(7)
65
66struct zb_mac_hdr {
67 uint8_t type;
68 uint8_t seq;
69 uint8_t ver;
70 bool sec, pend, ar, comp;
71 uint8_t dst_mode, src_mode;
72 uint16_t dst_pan, src_pan;
73 uint16_t dst_short, src_short;
74 /* Little endian, as on air. */
75 uint8_t dst_ext[8], src_ext[8];
76 /* Header length in bytes: the payload (for a command, the command id first) follows. */
77 uint8_t len;
78};
79
80/*
81 * Parse the MAC header of @p n bytes (no FCS). Returns 0, -EINVAL (malformed) or -ENOTSUP
82 * (frame version 2 or 3, or security enabled: the radio would not ACK them either).
83 */
84int zb_mac_parse(const uint8_t *f, size_t n, struct zb_mac_hdr *h);
85
86/* The Zigbee view of a beacon, as received or to send. */
88 uint16_t pan;
89 uint16_t src_short;
90 uint8_t channel;
91 bool permit;
92 bool pan_coord;
93 uint8_t stack_profile;
94 uint8_t proto_ver;
95 bool router_cap;
96 bool ed_cap;
97 uint8_t depth;
98 uint8_t upd;
99 /* Little endian, as on air. */
100 uint8_t epid[8];
101 uint8_t lqi;
102 int8_t rssi;
103};
104
105/* Decode the beacon payload that follows the header @p h; -EINVAL if it is no Zigbee beacon. */
106int zb_mac_parse_beacon(const struct zb_mac_hdr *h, const uint8_t *payload, size_t n,
107 struct zb_beacon_info *b);
108
109/* Builders return the frame length. All frames use version 0. */
110size_t zb_mac_build_beacon_req(uint8_t *out, uint8_t seq);
111size_t zb_mac_build_beacon(uint8_t *out, uint8_t bsn, const struct zb_beacon_info *b);
112size_t zb_mac_build_assoc_req(uint8_t *out, uint8_t seq, uint16_t pan, uint16_t dst_short,
113 const uint8_t src_ext[8], uint8_t cap);
114size_t zb_mac_build_assoc_rsp(uint8_t *out, uint8_t seq, uint16_t pan, const uint8_t dst_ext[8],
115 const uint8_t src_ext[8], uint16_t short_addr, uint8_t status);
116/* Data request: from a joiner that only has its extended address (src_short == NULL) or a poll. */
117size_t zb_mac_build_data_req(uint8_t *out, uint8_t seq, uint16_t pan, uint16_t dst_short,
118 const uint8_t *src_ext, const uint16_t *src_short);
119/* Data frame between short addresses inside one PAN (compression); @p ar for unicast. */
120size_t zb_mac_build_data(uint8_t *out, uint8_t seq, uint16_t pan, uint16_t dst_short,
121 uint16_t src_short, bool ar, const uint8_t *payload, size_t plen);
122/* Zero-length data frame answering a data request that finds nothing queued. */
123size_t zb_mac_build_empty_data(uint8_t *out, uint8_t seq, uint16_t pan, const uint16_t *dst_short,
124 const uint8_t *dst_ext, uint16_t src_short);
125
126#endif /* ZIGBEE_ZB_MAC_FRAME_H_ */
Definition zb_mac_frame.h:87
Definition zb_mac_frame.h:66