esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
zb_net.h
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 *
4 * The coordinator's network layer, phase 2 part: NV-backed network state in Z-Stack's item
5 * layout, the factory defaults hosts expect, formation and restore (what BDB commissioning
6 * does), the child table and the association policy. NWK security and routing come later.
7 */
8#ifndef ZIGBEE_ZB_NET_H_
9#define ZIGBEE_ZB_NET_H_
10
11#include <zigbee/zb_frame.h>
12#include <zigbee/zb_mac.h>
13#include <zigbee/znp_nv.h>
14
15/* Z-Stack OSAL NV item ids (zigpy-znp types/nvids.py, zigbee-herdsman constants/common.ts). */
16enum zb_nv_id {
17 ZB_NV_EXTADDR = 0x0001,
18 ZB_NV_STARTUP_OPTION = 0x0003,
19 ZB_NV_NIB = 0x0021,
20 ZB_NV_EXTENDED_PAN_ID = 0x002D,
21 ZB_NV_BCAST_DELIVERY_TIME = 0x0030,
22 ZB_NV_CONCENTRATOR_ENABLE = 0x0032,
23 ZB_NV_CONCENTRATOR_DISCOVERY = 0x0033,
24 ZB_NV_CONCENTRATOR_RC = 0x0036,
25 ZB_NV_SRC_RTG_EXPIRY_TIME = 0x0038,
26 ZB_NV_NWK_ACTIVE_KEY_INFO = 0x003A,
27 ZB_NV_NWK_ALTERN_KEY_INFO = 0x003B,
28 ZB_NV_NWK_CHILD_AGE_ENABLE = 0x003E,
29 ZB_NV_APS_USE_EXT_PANID = 0x0047,
30 ZB_NV_APS_LINK_KEY_TABLE = 0x004C,
31 ZB_NV_BDB_NODE_IS_ON_NETWORK = 0x0055,
32 ZB_NV_MIGRATION_ID = 0x005F,
33 ZB_NV_HAS_CONFIGURED_ZSTACK3 = 0x0060,
34 ZB_NV_PRECFGKEY = 0x0062,
35 ZB_NV_PRECFGKEYS_ENABLE = 0x0063,
36 ZB_NV_NWKKEY = 0x0082,
37 ZB_NV_PANID = 0x0083,
38 ZB_NV_CHANLIST = 0x0084,
39 ZB_NV_LOGICAL_TYPE = 0x0087,
40 ZB_NV_ZDO_DIRECT_CB = 0x008F,
41 ZB_NV_TCLK_SEED = 0x0101,
42 /* Private to this firmware: the APS security frame counter, written ahead. */
43 ZB_NV_APS_FRAME_COUNTER = 0x0F20,
44};
45
46/* Extended NV item ids (system ZNP_NV_SYS_ZSTACK): tables, one sub id per entry. */
47enum zb_nv_ex {
48 ZB_NVEX_ADDRMGR = 1,
49 ZB_NVEX_TCLK_TABLE = 4,
50 ZB_NVEX_APS_KEY_DATA = 6,
51 ZB_NVEX_NWK_SEC_MATERIAL = 7,
52};
53
54#define ZB_NIB_LEN 116 /* Z-Stack 3.30, 32 bit aligned */
55#define ZB_NWKKEY_LEN 24
56#define ZB_ADDRMGR_LEN 12
57#define ZB_TCLK_LEN 20
58#define ZB_APS_KEY_LEN 24
59#define ZB_SECMAT_LEN 12
60#define ZB_KEYDESC_LEN 17 /* key sequence number, key[16] */
61#define ZB_LINK_KEY_TABLE_ENTRIES 16
62
63#define ZB_NET_MAX_CHILDREN 32
64#define ZB_TABLE_ENTRIES 32 /* ADDRMGR, TCLK and APS key tables */
65#define ZB_SECMAT_ENTRIES 4
66
67#define ZB_ALL_CHANNELS 0x07FFF800U
68
69/* NIB byte offsets (aligned layout, host-visible through NV item 0x0021). */
70#define ZB_NIB_SECURITY_LEVEL 12
71#define ZB_NIB_DEV_ADDR 22
72#define ZB_NIB_CHANNEL 24
73#define ZB_NIB_COORD_ADDR 26
74#define ZB_NIB_COORD_EXT 28
75#define ZB_NIB_PAN_ID 36
76#define ZB_NIB_STATE 38
77#define ZB_NIB_CHANNEL_LIST 40
78#define ZB_NIB_EPID 57
79#define ZB_NIB_KEY_LOADED 65
80#define ZB_NIB_MANAGER_ADDR 110
81#define ZB_NIB_UPDATE_ID 114
82
83/* NWK state values in the NIB. */
84#define ZB_NWK_INIT 0
85#define ZB_NWK_ROUTER 8
86
87/* ZDO device states (ZDO_STATE_CHANGE_IND, UTIL_GET_DEVICE_INFO). */
88enum zb_dev_state {
89 ZB_DEV_HOLD = 0,
90 ZB_DEV_COORD_STARTING = 8,
91 ZB_DEV_COORD = 9,
92};
93
94enum zb_bdb_mode {
95 ZB_BDB_STEERING = 0x02,
96 ZB_BDB_FORMATION = 0x04,
97};
98
99enum zb_bdb_status {
100 ZB_BDB_SUCCESS = 0,
101 ZB_BDB_IN_PROGRESS = 1,
102 ZB_BDB_FORMATION_FAILURE = 8,
103};
104
105/* STARTUP_OPTION bits applied at a reset. */
106#define ZB_STARTUP_CLEAR_CONFIG BIT(0)
107#define ZB_STARTUP_CLEAR_STATE BIT(1)
108
109struct zb_child {
110 bool used;
111 /* True from the association until the response is ACKed. */
112 bool tentative;
113 /* The network key has been delivered: frames from it are accepted. */
114 bool authorized;
115 uint16_t nwk;
116 uint8_t ext[8];
117 uint8_t cap;
118 /* Highest NWK frame counter seen from it (replay protection). */
119 uint32_t rx_fc;
120 int64_t last_seen;
121};
122
123/* An application frame received (APS payload of an authorized neighbour). */
124struct zb_aps_rx {
125 uint16_t src;
126 uint16_t cluster, profile;
127 uint8_t src_ep, dst_ep;
128 bool broadcast;
129 uint8_t lqi;
130 uint8_t aps_counter;
131 const uint8_t *data;
132 uint8_t len;
133};
134
135/* An application frame to send. */
136struct zb_aps_tx {
137 /* NWK destination: a short address or one of the broadcast addresses. */
138 uint16_t dst;
139 bool broadcast;
140 uint8_t src_ep, dst_ep;
141 uint16_t cluster, profile;
142 bool ack_req;
143 /* Report the outcome through zb_net_cb.data_confirm (not for ZDO and broadcasts). */
144 bool confirm;
145 /* Handed back with the confirmation. */
146 uint32_t tag;
147 const uint8_t *data;
148 size_t len;
149};
150
151/* Result codes of a confirmation (Z-Stack's). */
152#define ZB_CONFIRM_OK 0x00
153#define ZB_CONFIRM_FAILURE 0x01
154#define ZB_CONFIRM_APS_NO_ACK 0xB7
155#define ZB_CONFIRM_NO_ROUTE 0xCD
156#define ZB_CONFIRM_CHANNEL_BUSY 0xE1
157#define ZB_CONFIRM_MAC_NO_ACK 0xE9
158
159/* Longest application payload with NWK security (no APS security). */
160#define ZB_APS_MAX_PAYLOAD 82
161
162/* What the net layer reports upwards; the callbacks run in the MAC thread. */
163struct zb_net_cb {
164 void (*state)(void *ctx, uint8_t dev_state);
165 void (*bdb)(void *ctx, uint8_t status, uint8_t mode, uint8_t remaining);
166 /* Beacons of an active scan asked by the host, then the confirmation (status 0). */
167 void (*beacons)(void *ctx, const struct zb_beacon_info *b, size_t n);
168 void (*discovery_done)(void *ctx, uint8_t status);
169 /* Energy scan asked by the host: one ED byte per channel of @p mask. */
170 void (*energy_done)(void *ctx, int status, uint32_t mask, const uint8_t *ed, size_t n,
171 uint32_t tag);
172 void (*permit)(void *ctx, uint8_t duration);
173 /* A device was given the network key (the trust center's join). */
174 void (*tc_join)(void *ctx, uint16_t nwk, const uint8_t ext[8], uint16_t parent);
175 /* An application frame came in. */
176 void (*aps_rx)(void *ctx, const struct zb_aps_rx *rx);
177 /* Outcome of a zb_net_send() with confirm set. */
178 void (*data_confirm)(void *ctx, uint8_t status, uint32_t tag);
179 /* A line of protocol trace (why a frame was dropped, what the trust center did). */
180 void (*trace)(void *ctx, const char *msg);
181 void *ctx;
182};
183
184#define ZB_APS_WAITS 6
185#define ZB_APS_DUPS 8
186
188 bool used;
189 uint16_t dst;
190 uint8_t counter;
191 uint32_t tag;
192 int64_t deadline;
193};
194
196 uint16_t src;
197 uint8_t counter;
198 int64_t until;
199};
200
202 bool busy;
203 uint32_t mask;
204 uint8_t exp;
205 uint8_t mode;
206 uint32_t tag;
207 uint8_t ed[16];
208 struct zb_beacon_info beacons[16];
209};
210
211struct zb_net {
212 struct zb_mac *mac;
213 struct znp_nv *nv;
214 struct zb_net_cb cb;
215 struct k_mutex lock;
216
217 uint8_t eui[8];
218 uint8_t dev_state;
219 bool formed;
220 uint16_t pan;
221 uint8_t channel;
222 uint8_t epid[8];
223 uint8_t upd;
224 uint32_t bdb_primary, bdb_secondary;
225 struct zb_child children[ZB_NET_MAX_CHILDREN];
226 struct zb_net_job job;
227
228 /* Network security: the active key and the frame counters (written ahead to flash). */
229 bool keys_ok;
230 uint8_t nwk_key[ZB_KEY_LEN];
231 uint8_t nwk_key_seq;
232 uint8_t tc_link_key[ZB_KEY_LEN];
233 uint32_t nwk_fc, nwk_fc_limit;
234 uint32_t aps_fc, aps_fc_limit;
235 uint8_t nwk_seq, aps_counter;
236 int64_t next_link_status;
237 struct zb_aps_wait waits[ZB_APS_WAITS];
238 struct zb_aps_dup dups[ZB_APS_DUPS];
239};
240
241/* The MAC callbacks of the net layer (association policy); pass to zb_mac_init(). */
242void zb_net_mac_up(struct zb_net *n, struct zb_mac_up *up);
243
244void zb_net_init(struct zb_net *n, struct zb_mac *mac, struct znp_nv *nv,
245 const struct zb_net_cb *cb);
246
247/*
248 * Boot the network layer from NV (a power up or a reset): apply STARTUP_OPTION, make sure the
249 * factory items exist, load the identity and children, and bring a formed network up.
250 */
251int zb_net_boot(struct zb_net *n, const uint8_t default_eui[8]);
252
253/* The factory default NV items; @p reset_config replaces the config items that exist. */
254void zb_nvdef_ensure(struct znp_nv *nv, const uint8_t eui[8], bool reset_config);
255/* ClearState: forget the network (NIB, keys, children, tables) but keep identity and counters. */
256void zb_nvdef_clear_state(struct znp_nv *nv);
257
258/* BDB: the channel masks formation chooses from, and starting a commissioning mode. */
259void zb_net_bdb_set_channel(struct zb_net *n, bool primary, uint32_t mask);
260/* Returns 0 when the mode is accepted; progress and result follow as callbacks. */
261int zb_net_bdb_start(struct zb_net *n, uint8_t mode);
262
263/* Association permit: 0 closes, 0xFF stays open, otherwise seconds. */
264int zb_net_permit(struct zb_net *n, uint8_t seconds);
265
266/* Asynchronous scans for the host; one at a time (-EBUSY). Results come through zb_net_cb. */
267int zb_net_discover(struct zb_net *n, uint32_t mask, uint8_t exp);
268/* @p tag comes back with the result: the caller's way to tell scans apart. */
269int zb_net_energy_scan(struct zb_net *n, uint32_t mask, uint8_t exp, uint32_t tag);
270
271/*
272 * Move the formed network to @p channel (ZDO Mgmt_NWK_Update, ScanDuration 0xFE): the NIB gets
273 * the channel and an incremented network update id, then the MAC restarts. -EINVAL, or
274 * -ENOTCONN without a network.
275 */
276int zb_net_change_channel(struct zb_net *n, uint8_t channel);
277/* ScanDuration 0xFF: the channel mask and network manager address in the NIB. */
278int zb_net_set_channel_mask(struct zb_net *n, uint32_t mask, uint16_t manager);
279
280/*
281 * Send an application frame to a neighbour (or everyone): NWK secured, no routing yet. Returns
282 * 0 once accepted, with the outcome (MAC and APS acknowledgements) through data_confirm;
283 * -EMSGSIZE, -ENOTCONN.
284 */
285int zb_net_send(struct zb_net *n, const struct zb_aps_tx *tx);
286
287/* Association table for the host's UTIL_ASSOC_* commands; slot index or -1. */
288int zb_net_child_add(struct zb_net *n, uint16_t nwk, const uint8_t ext[8], uint8_t cap);
289int zb_net_child_remove(struct zb_net *n, const uint8_t ext[8]);
290
291/* Child table. */
292struct zb_child *zb_net_child_by_ext(struct zb_net *n, const uint8_t ext[8]);
293struct zb_child *zb_net_child_by_nwk(struct zb_net *n, uint16_t nwk);
294size_t zb_net_child_count(struct zb_net *n);
295
296/* The IEEE address in use (NV 0x0001, set by the host or the factory default). */
297const uint8_t *zb_net_eui(const struct zb_net *n);
298
299#endif /* ZIGBEE_ZB_NET_H_ */
Definition zb_net.h:195
Definition zb_net.h:124
Definition zb_net.h:136
Definition zb_net.h:187
Definition zb_mac_frame.h:87
Definition zb_net.h:109
Definition zb_mac.h:27
Definition zb_mac.h:78
Definition zb_net.h:163
Definition zb_net.h:201
Definition zb_net.h:211
Definition znp_nv.h:47