esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
mt.h
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 *
4 * Z-Stack MT (monitor and test) serial framing, as ZNP hosts such as zigpy-znp and
5 * zigbee2mqtt speak it: SOF 0xFE, length, CMD0 (type in bits 7..5, subsystem in 4..0),
6 * CMD1, payload, XOR of everything between SOF and FCS. Portable C, no Zephyr.
7 */
8#ifndef ZIGBEE_MT_H_
9#define ZIGBEE_MT_H_
10
11#include <stddef.h>
12#include <stdint.h>
13
14#define MT_SOF 0xFE
15#define MT_MAX_PAYLOAD 250
16/* SOF, length, CMD0, CMD1, payload, FCS. */
17#define MT_MAX_WIRE (MT_MAX_PAYLOAD + 5)
18
19enum mt_type {
20 MT_POLL = 0,
21 MT_SREQ = 1,
22 MT_AREQ = 2,
23 MT_SRSP = 3,
24};
25
26enum mt_subsys {
27 MT_RPC_ERROR = 0x00,
28 MT_SYS = 0x01,
29 MT_MAC = 0x02,
30 MT_NWK = 0x03,
31 MT_AF = 0x04,
32 MT_ZDO = 0x05,
33 MT_SAPI = 0x06,
34 MT_UTIL = 0x07,
35 MT_DEBUG = 0x08,
36 MT_APP = 0x09,
37 MT_APP_CNF = 0x0F,
38 MT_ZGP = 0x15,
39 MT_SUBSYS_COUNT = 0x20,
40};
41
42/* Error codes of the RPC error SRSP (subsystem 0, command 0). */
43enum mt_rpc_error {
44 MT_ERR_SUBSYSTEM = 0x01,
45 MT_ERR_COMMAND = 0x02,
46 MT_ERR_PARAMETER = 0x03,
47 MT_ERR_LENGTH = 0x04,
48};
49
50/* Status bytes used in SRSP payloads (Z-Stack ZStatus_t). */
51enum mt_status {
52 MT_STATUS_SUCCESS = 0x00,
53 MT_STATUS_FAILURE = 0x01,
54 MT_STATUS_INVALID_PARAMETER = 0x02,
55 MT_STATUS_NV_ITEM_UNINIT = 0x09,
56 MT_STATUS_NV_OPER_FAILED = 0x0A,
57 MT_STATUS_NV_BAD_ITEM_LEN = 0x0C,
58 MT_STATUS_BUFFER_FULL = 0x11,
59};
60
61struct mt_frame {
62 uint8_t type;
63 uint8_t sub;
64 uint8_t cmd;
65 uint8_t len;
66 uint8_t data[MT_MAX_PAYLOAD];
67};
68
69/* Byte-wise parser; a bad checksum rescans the bytes after the failed SOF. */
70struct mt_parser {
71 uint8_t raw[MT_MAX_WIRE];
72 uint16_t n;
73 /* Bad frames dropped (checksum or length) since init. */
74 uint32_t errors;
75 struct mt_frame frame;
76};
77
78enum mt_feed {
79 MT_FEED_MORE = 0,
80 MT_FEED_FRAME = 1,
81};
82
83void mt_parser_init(struct mt_parser *p);
84
85/* Drop a partial frame: the transport calls this after an idle gap in the byte stream. */
86static inline void mt_parser_reset(struct mt_parser *p)
87{
88 p->n = 0;
89}
90
91/* MT_FEED_FRAME: p->frame holds a complete frame, valid until the next feed. */
92enum mt_feed mt_parser_feed(struct mt_parser *p, uint8_t byte);
93
94/* Serialise @p f to @p out (at least f->len + 5 bytes); returns the byte count. */
95size_t mt_encode(const struct mt_frame *f, uint8_t *out);
96
97static inline uint16_t mt_get_u16(const uint8_t *p)
98{
99 return (uint16_t)(p[0] | (p[1] << 8));
100}
101
102static inline uint32_t mt_get_u32(const uint8_t *p)
103{
104 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
105 ((uint32_t)p[3] << 24);
106}
107
108static inline void mt_put_u16(uint8_t *p, uint16_t v)
109{
110 p[0] = (uint8_t)v;
111 p[1] = (uint8_t)(v >> 8);
112}
113
114static inline void mt_put_u32(uint8_t *p, uint32_t v)
115{
116 mt_put_u16(p, (uint16_t)v);
117 mt_put_u16(p + 2, (uint16_t)(v >> 16));
118}
119
120#endif /* ZIGBEE_MT_H_ */
Definition mt.h:61
Definition mt.h:70