/* * USB ESP-NOW bridge framing, shared by bridge/ (ESP32-C3 firmware) and main/bridge_serial.c (host). * * Each message is one SLIP frame (END-delimited, ESC-escaped) holding [type][body][crc16 LE], where the * CRC-16/CCITT-FALSE covers type and body. Bytes outside frames are ignored by the host. * * 00 PKT host -> bridge dst_mac(6) + packet sent on ESP-NOW to dst_mac * 00 PKT bridge -> host src_mac(6) + packet every frame received on ESP-NOW * 10 HELLO host -> bridge (empty) answered with INFO * 11 INFO bridge -> host see bridge_info below * 12 SET_CHANNEL host -> bridge channel(1) sets the ESP-NOW channel, answered with INFO * 13 SET_MIC host -> bridge on(1) start (1) or stop (0) streaming the microphone * 40 AUDIO bridge -> host BRIDGE_AUDIO_SAMPLES 16-bit LE mono samples at 16 kHz, while the mic is on * 30 LOG bridge -> host text the bridge's log lines * * INFO: byte 24 is capabilities (bit 0 = a microphone, BRIDGE_CAP_MIC; bit 1 = Ethernet up, BRIDGE_CAP_ETH), * bytes 25-28 the board's Ethernet IPv4 address (0.0.0.0 while it has none). */ #pragma once #include #include #include #define BRIDGE_PROTO_VERSION 1 #define BRIDGE_PKT_MAX 250 // ESP-NOW payload limit #define BRIDGE_AUDIO_SAMPLES 160 // 10 ms at 16 kHz #define BRIDGE_AUDIO_BYTES (2 * BRIDGE_AUDIO_SAMPLES) #define BRIDGE_PKT_BODY (6 + BRIDGE_PKT_MAX) #define BRIDGE_BODY_MAX (BRIDGE_PKT_BODY > BRIDGE_AUDIO_BYTES ? BRIDGE_PKT_BODY : BRIDGE_AUDIO_BYTES) #define BRIDGE_FRAME_MAX (BRIDGE_BODY_MAX + 3) // type + body + crc #define BRIDGE_ENC_MAX (2 * BRIDGE_FRAME_MAX + 2) #define BRIDGE_INFO_LEN 29 #define BRIDGE_CAP_MIC 0x01 #define BRIDGE_CAP_ETH 0x02 enum { BRIDGE_MSG_PKT = 0x00, BRIDGE_MSG_HELLO = 0x10, BRIDGE_MSG_INFO = 0x11, BRIDGE_MSG_SET_CHANNEL = 0x12, BRIDGE_MSG_SET_MIC = 0x13, BRIDGE_MSG_AUDIO = 0x40, BRIDGE_MSG_LOG = 0x30, }; #define SLIP_END 0xC0 #define SLIP_ESC 0xDB #define SLIP_ESC_END 0xDC #define SLIP_ESC_ESC 0xDD static inline uint16_t bridge_crc16_update(uint16_t crc, uint8_t b) { crc ^= (uint16_t)b << 8; for (int i = 0; i < 8; i++) { crc = (crc & 0x8000) ? (uint16_t)((crc << 1) ^ 0x1021) : (uint16_t)(crc << 1); } return crc; } /* Encode one message into out (capacity cap). Returns the frame length, or 0 if it does not fit. */ static inline size_t bridge_encode(uint8_t *out, size_t cap, uint8_t type, const uint8_t *body, size_t n) { size_t off = 0; uint16_t crc = 0xFFFF; if (cap < 2) { return 0; } out[off++] = SLIP_END; /* the type and body bytes, then the CRC, each escaped as they go out */ for (size_t i = 0; i < n + 3; i++) { uint8_t b; if (i == 0) { b = type; crc = bridge_crc16_update(crc, b); } else if (i <= n) { b = body[i - 1]; crc = bridge_crc16_update(crc, b); } else { b = (i == n + 1) ? (uint8_t)(crc & 0xFF) : (uint8_t)(crc >> 8); } if (b == SLIP_END) { if (off + 2 >= cap) return 0; out[off++] = SLIP_ESC; out[off++] = SLIP_ESC_END; } else if (b == SLIP_ESC) { if (off + 2 >= cap) return 0; out[off++] = SLIP_ESC; out[off++] = SLIP_ESC_ESC; } else { if (off + 1 >= cap) return 0; out[off++] = b; } } if (off + 1 > cap) return 0; out[off++] = SLIP_END; return off; } /* Incremental decoder. Feed bytes; when bridge_decode_feed returns true, d->buf holds [type][body] of * length d->frame_len, valid until the next call. */ typedef struct { uint8_t buf[BRIDGE_FRAME_MAX]; size_t len; size_t frame_len; bool esc; bool overflow; } bridge_decoder_t; static inline bool bridge_decode_feed(bridge_decoder_t *d, uint8_t b) { if (b == SLIP_END) { bool ok = false; if (!d->overflow && d->len >= 3) { uint16_t crc = 0xFFFF; for (size_t i = 0; i < d->len - 2; i++) { crc = bridge_crc16_update(crc, d->buf[i]); } uint16_t got = (uint16_t)(d->buf[d->len - 2] | (d->buf[d->len - 1] << 8)); ok = crc == got; if (ok) { d->frame_len = d->len - 2; } } d->len = 0; d->esc = false; d->overflow = false; return ok; } if (d->esc) { d->esc = false; if (b == SLIP_ESC_END) { b = SLIP_END; } else if (b == SLIP_ESC_ESC) { b = SLIP_ESC; } else { d->overflow = true; // not valid SLIP: drop the frame } } else if (b == SLIP_ESC) { d->esc = true; return false; } if (d->len < sizeof(d->buf)) { d->buf[d->len++] = b; } else { d->overflow = true; } return false; }