6using packet_interface::PacketBuffer;
7using packet_interface::PacketMetaData;
11static constexpr uint8_t FLAG_BYTE = 0x7E;
12static constexpr uint8_t CONTROL_BYTE = 0x7D;
13static constexpr uint8_t ESCAPE_XOR = 0x20;
15static constexpr size_t TX_CHUNK_SIZE = 32;
22 if (
byte == FLAG_BYTE) {
33 if (
byte == CONTROL_BYTE) {
42 ESP_LOGD(TAG,
"Packet too large, discarding");
51 size_t size = data.size();
58 uint8_t
raw[TX_CHUNK_SIZE]{};
60 uint8_t
out[TX_CHUNK_SIZE * 2 + 2];
62 out[out_len++] = FLAG_BYTE;
63 for (
size_t offset = 0; offset <
size;) {
64 size_t count = data.copy_to(
raw, TX_CHUNK_SIZE, offset);
66 for (
size_t i = 0; i !=
count; i++) {
67 uint8_t
byte =
raw[i];
68 if (
byte == FLAG_BYTE ||
byte == CONTROL_BYTE) {
69 out[out_len++] = CONTROL_BYTE;
72 out[out_len++] = byte;
75 out[out_len++] = FLAG_BYTE;
A buffer class that efficiently represents a composite buffer with three sections:
const size_t max_packet_size_
void on_receive_from_interface_(const PacketBuffer &data, const PacketMetaData &meta_data)
This function should be called by implementing classes with received data to be passed to any listene...
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
void write_array(const uint8_t *data, size_t len)
const std::span< uint8_t > rx_buffer_
bool send_to_interface(const packet_interface::PacketBuffer &data, const packet_interface::PacketMetaData &meta_data) override
void handle_rx_byte_(uint8_t byte)
std::vector< uint8_t > & out
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")