#pragma once #include #include #include #include "esp_err.h" // Reliable bulk link on top of espnow_tx. Wire format is documented in // led-driver/src/espnow_rx.py: // All frames are 0x4C wire packets with extra message types (see led-driver/src/espnow_rx.py): // data [4C][20][flags][seq u16][ver u16][idx][cnt] + fragment of an inner packet (<= 9 + 238) // ack [4C][21][0][seq u16][cnt][mask u32] (driver -> us) // digest [4C][22][ver u16] (broadcast) // resync [4C][23][ver u16] (driver -> us) // echo [4C][24][beat_idx u16] (driver -> us, bench) // ping [4C][06][ping_id u32][len][name] (driver -> us, reply to [4C][05][ping_id]) #define REL_FRAG_MAX 238 // inner packet bytes per frame; 4C 03/04 wrappers plus an envelope fit in one #define REL_MAX_FRAGS 32 typedef struct { uint32_t acked; uint32_t ack_timeouts; uint32_t retransmits; uint32_t resyncs; uint32_t digests; } rel_stats_t; typedef void (*rel_resync_cb_t)(const uint8_t mac[6]); esp_err_t rel_init(void); // Send a message of any size up to REL_MAX_FRAGS * REL_FRAG_MAX. // mac == NULL broadcasts it `repeat` times with one sequence number (no acks); // otherwise it is unicast, acknowledged by the driver and retransmitted until // acked or given up on. commit makes the driver adopt the current state version. esp_err_t rel_send(const uint8_t *mac, const void *data, size_t len, bool commit, int repeat); // rel_send that also returns the sequence number, for rel_wait esp_err_t rel_send_seq(const uint8_t *mac, const void *data, size_t len, bool commit, int repeat, uint16_t *seq); // Block until the unicast message (mac, seq) is fully acked (ESP_OK), given up on (ESP_FAIL) // or timeout_ms passes (ESP_ERR_TIMEOUT). Only the last few outcomes are kept, so wait soon after sending. esp_err_t rel_wait(const uint8_t *mac, uint16_t seq, int timeout_ms); // Unicast send, retried while the radio queue is full, then rel_wait esp_err_t rel_send_wait(const uint8_t *mac, const void *data, size_t len, int timeout_ms); // A broadcast state change happened: advance the version drivers compare in digests void rel_bump_ver(void); uint16_t rel_ver(void); void rel_set_resync_cb(rel_resync_cb_t cb); // A driver announced itself at boot: a 0x4C ANNOUNCE packet or its JSON hello (called from the rel task) typedef void (*rel_announce_cb_t)(const uint8_t mac[6], const uint8_t *pkt, int len); void rel_set_announce_cb(rel_announce_cb_t cb); // A driver's PING_RSP ([4C][06][ping_id u32][name len][name]), called from the rel task. Drivers // answer a broadcast PING_REQ ([4C][05][ping_id u32]) after a random 50-500 ms. typedef void (*rel_ping_cb_t)(const uint8_t mac[6], const uint8_t *pkt, int len); void rel_set_ping_cb(rel_ping_cb_t cb); void rel_stats(rel_stats_t *out); // Milliseconds since the driver last acked; -1 if never. fails = consecutive unacked messages int rel_peer_status(const uint8_t mac[6], int *fails);