esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
znp.h
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 *
4 * ZNP server: parses MT frames from a byte stream, dispatches SREQs to per-subsystem
5 * handlers and sends their SRSPs, and carries the stack's AREQ callbacks to the host.
6 * Speaks the Z-Stack 3.x dialect (transport revision 2) that zigpy-znp and zigbee2mqtt
7 * detect as a CC2652 coordinator.
8 */
9#ifndef ZIGBEE_ZNP_H_
10#define ZIGBEE_ZNP_H_
11
12#include <zephyr/kernel.h>
13
14#include <zigbee/mt.h>
15#include <zigbee/znp_nv.h>
16
17/* What SYS_VERSION and SYS_RESET_IND report (Z-Stack 3.30 on a coordinator). */
18#define ZNP_TRANSPORT_REV 2
19#define ZNP_PRODUCT_ID 1
20#define ZNP_VERSION_MAJOR 2
21#define ZNP_VERSION_MINOR 7
22#define ZNP_VERSION_MAINT 1
23
24enum znp_reset_reason {
25 ZNP_RESET_POWER_UP = 0,
26 ZNP_RESET_EXTERNAL = 1,
27 ZNP_RESET_WATCHDOG = 2,
28};
29
30struct znp;
31
32/*
33 * A subsystem handler. @p rsp is pre-set with type SRSP and the request's subsystem and
34 * command for an SREQ and has len 0; fill data and len. Return 0, or an enum mt_rpc_error
35 * to answer with an RPC error instead. For an AREQ @p rsp is NULL.
36 */
37typedef int (*znp_handler_t)(struct znp *z, const struct mt_frame *req, struct mt_frame *rsp);
38
39struct znp_config {
40 /* Called with one whole wire frame; serialised by the ZNP's tx lock. */
41 void (*tx)(void *user, const uint8_t *buf, size_t len);
42 /* SYS_RESET_REQ: type 0 hard, 1 soft; the stack restarts and the app then calls
43 * znp_send_reset_ind(). NULL: the ZNP only sends the indication.
44 */
45 void (*reset)(void *user, uint8_t type);
46 /* SYS_SET_TX_POWER in dBm; returns the applied power. NULL: accepted, ignored. */
47 int8_t (*set_tx_power)(void *user, int8_t dbm);
48 void *user;
49 struct znp_nv *nv;
50 /* The device's IEEE address (EUI-64, little endian on the wire as stored). */
51 uint8_t ext_addr[8];
52};
53
54#define ZNP_DEFER_MAX 4
55#define ZNP_DEFER_DATA 128
56
58 uint8_t sub, cmd, len;
59 uint8_t data[ZNP_DEFER_DATA];
60};
61
62struct znp {
63 struct znp_config cfg;
64 /* The Zigbee stack behind this ZNP (struct zb_stack), NULL for the bare protocol. */
65 void *stack;
66 k_tid_t dispatching;
67 uint8_t ndeferred;
68 struct znp_deferred deferred[ZNP_DEFER_MAX];
69 struct mt_parser parser;
70 struct mt_frame rsp;
71 /* SYS_PING capabilities: one bit per registered subsystem (MT_SYS bit 0 ... MT_UTIL
72 * bit 6, MT_ZGP bit 9, MT_APP_CNF bit 10). Hosts take a missing APP_CNF for Z-Stack 1.2.
73 */
74 uint16_t caps;
75 struct k_mutex tx_lock;
76 znp_handler_t handler[MT_SUBSYS_COUNT];
77 int8_t tx_power;
78};
79
80/* Sets up SYS and UTIL; other subsystems come in through znp_register(). */
81void znp_init(struct znp *z, const struct znp_config *cfg);
82
83void znp_register(struct znp *z, enum mt_subsys sub, znp_handler_t handler);
84
85/* Feed bytes from the transport; complete frames are handled in this call. */
86void znp_rx(struct znp *z, const uint8_t *buf, size_t n);
87
88/* The byte stream paused mid-frame: forget the partial frame (the transport's idle timer). */
89void znp_rx_idle(struct znp *z);
90
91/* Send an AREQ (stack callback) to the host; any thread. */
92void znp_send_areq(struct znp *z, enum mt_subsys sub, uint8_t cmd, const void *data, uint8_t len);
93
94/*
95 * Send an AREQ that belongs after the SRSP of the request being handled (a callback that the
96 * request caused). From a handler it is held back until the SRSP is out; from any other thread
97 * it goes at once.
98 */
99void znp_send_areq_after(struct znp *z, enum mt_subsys sub, uint8_t cmd, const void *data,
100 uint8_t len);
101
102/* SYS_RESET_IND: sent at boot and after a reset. */
103void znp_send_reset_ind(struct znp *z, enum znp_reset_reason reason);
104
105#endif /* ZIGBEE_ZNP_H_ */
Definition mt.h:61
Definition mt.h:70
Definition znp.h:39
Definition znp.h:57
Definition znp_nv.h:47
Definition znp.h:62