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.h
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 *
4 * The coordinator's MAC: one thread that owns the radio. It answers beacon requests, runs
5 * the association handshake and the indirect transmission that goes with it, and runs energy
6 * and active scans, with the frames of zb_mac_frame.h.
7 */
8#ifndef ZIGBEE_ZB_MAC_H_
9#define ZIGBEE_ZB_MAC_H_
10
11#include <zephyr/kernel.h>
12
13#include <zigbee/zb_mac_frame.h>
14#include <zigbee/zb_radio.h>
15
16#define ZB_MAC_PENDING_MAX 8
17
18/* macTransactionPersistenceTime: 0x01F4 * 15.36 ms. */
19#define ZB_MAC_PERSISTENCE_MS 7680
20#define ZB_MAC_MAX_RETRIES 3
21/* Longest scan duration exponent: 15.36 ms * (2^14 + 1) is about 252 s per channel. */
22#define ZB_MAC_SCAN_EXP_MAX 14
23
24struct zb_mac;
25
26/* Callbacks into the layer above; all run in the MAC thread and must not block long. */
27struct zb_mac_up {
28 /*
29 * Association request of @p ext with capability information @p cap while joining is
30 * permitted. Return an enum zb_assoc_status; on ZB_ASSOC_OK set *short_addr.
31 */
32 uint8_t (*assoc_req)(void *ctx, const uint8_t ext[8], uint8_t cap, uint16_t *short_addr);
33 /* The response to @p ext was ACKed (delivered) or expired. */
34 void (*assoc_done)(void *ctx, const uint8_t ext[8], uint16_t short_addr, bool delivered);
35 /* A data frame for this node; @p payload follows the header. */
36 void (*data_ind)(void *ctx, const struct zb_mac_hdr *h, const uint8_t *payload, size_t n,
37 uint8_t lqi, int8_t rssi);
38 /* Optional: called at least once a second, for timers of the layer above. */
39 void (*tick)(void *ctx);
40 /* Optional: every frame sent or received (no FCS). */
41 void (*trace)(void *ctx, bool tx, const uint8_t *f, size_t n, uint8_t lqi, int8_t rssi);
42 void *ctx;
43};
44
46 uint8_t n;
47 uint8_t lqi;
48 int8_t rssi;
49 uint8_t f[ZB_MAC_MAX_FRAME];
50};
51
52struct zb_mac_job {
53 int (*fn)(struct zb_mac *m, void *arg);
54 void *arg;
55 struct k_sem *done;
56 int *ret;
57};
58
60 bool is_job;
61 union {
62 struct zb_mac_rx_item rx;
63 struct zb_mac_job job;
64 };
65};
66
68 bool used;
69 bool assoc;
70 bool by_ext;
71 uint8_t ext[8];
72 uint16_t short_addr;
73 uint8_t f[ZB_MAC_MAX_FRAME];
74 uint8_t n;
75 int64_t expiry;
76};
77
78struct zb_mac {
79 const struct zb_radio_ops *radio;
80 void *rctx;
81 struct zb_mac_up up;
82 uint8_t eui[8];
83
84 /* The PAN the coordinator runs (valid while started). */
85 bool started;
86 uint16_t pan;
87 uint8_t channel;
88 struct zb_beacon_info bcn;
89 int64_t permit_until; /* uptime ms; 0 closed, INT64_MAX forever */
90 uint8_t dsn, bsn;
91 int8_t txpower;
92 bool txpower_set;
93
94 /* Set from before a scan reprograms the radio until the operating setup is back. */
95 atomic_t scanning;
96 struct zb_mac_pending pending[ZB_MAC_PENDING_MAX];
97
98 struct k_msgq q;
99 struct k_msgq scanq;
100 char qbuf[16 * sizeof(struct zb_mac_event)];
101 char scanbuf[16 * sizeof(struct zb_mac_rx_item)];
102 struct k_thread thread;
103 k_tid_t tid;
104
105 uint32_t rx_dropped, tx_fail;
106};
107
108/* Set up; the radio is not touched until zb_mac_run(). */
109void zb_mac_init(struct zb_mac *m, const struct zb_radio_ops *radio, void *rctx,
110 const struct zb_mac_up *up, const uint8_t eui[8]);
111
112/* Start the MAC thread (stack of @p stack_size bytes, defined by the caller). */
113void zb_mac_run(struct zb_mac *m, k_thread_stack_t *stack, size_t stack_size, int prio);
114
115/* The radio's receive path calls this for each frame (no FCS); any thread, never blocks. */
116void zb_mac_rx(struct zb_mac *m, const uint8_t *f, uint8_t n, uint8_t lqi, int8_t rssi);
117
118/* Run @p fn in the MAC thread and wait for it (directly when called from it). */
119int zb_mac_call(struct zb_mac *m, int (*fn)(struct zb_mac *m, void *arg), void *arg);
120
121/* Queue @p fn for the MAC thread without waiting (@p arg must stay valid). */
122int zb_mac_post(struct zb_mac *m, int (*fn)(struct zb_mac *m, void *arg), void *arg);
123
124/*
125 * Energy scan: for each channel in @p mask (ascending), the maximum energy over the scan
126 * duration 15.36 ms * (2^exp + 1), as 0..255 into @p ed (one byte per channel). @p exp is
127 * 0..ZB_MAC_SCAN_EXP_MAX (-EINVAL above).
128 */
129int zb_mac_energy_scan(struct zb_mac *m, uint32_t mask, uint8_t exp, uint8_t *ed);
130
131/* Active scan: beacons seen on the channels of @p mask; *count of at most @p max. */
132int zb_mac_active_scan(struct zb_mac *m, uint32_t mask, uint8_t exp, struct zb_beacon_info *out,
133 size_t max, size_t *count);
134
135/*
136 * Become the PAN coordinator of @p pan on @p channel (short address 0x0000): program the radio
137 * and answer beacon requests with @p bcn (permit join closed until zb_mac_permit()).
138 */
139int zb_mac_start(struct zb_mac *m, uint16_t pan, uint8_t channel, const struct zb_beacon_info *bcn);
140int zb_mac_stop(struct zb_mac *m);
141
142/* Association permit: 0 closes, 0xFF stays open, otherwise seconds. */
143int zb_mac_permit(struct zb_mac *m, uint8_t seconds);
144bool zb_mac_permit_open(struct zb_mac *m);
145
146/* Router and end device capacity bits of the beacon. */
147int zb_mac_set_capacity(struct zb_mac *m, bool router_cap, bool ed_cap);
148
149/* Beacon fields that follow the NIB: extended PAN ID and network update id. */
150int zb_mac_set_network(struct zb_mac *m, const uint8_t epid[8], uint8_t upd);
151
152/* Transmit power in dBm, applied at the next radio start and now (a running network restarts). */
153int zb_mac_set_txpower(struct zb_mac *m, int8_t dbm);
154
155/* The extended address in use (while stopped; the host may change it with a reset). */
156int zb_mac_set_eui(struct zb_mac *m, const uint8_t eui[8]);
157
158/* Send a data frame to a short address with MAC retries; 0, -ENOMSG (no ACK), -EAGAIN. */
159int zb_mac_send_data(struct zb_mac *m, uint16_t dst_short, const uint8_t *payload, size_t n,
160 bool ar);
161
162#endif /* ZIGBEE_ZB_MAC_H_ */
Definition zb_mac_frame.h:87
Definition zb_mac.h:59
Definition zb_mac_frame.h:66
Definition zb_mac.h:52
Definition zb_mac.h:67
Definition zb_mac.h:45
Definition zb_mac.h:27
Definition zb_mac.h:78
Definition zb_radio.h:15