ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld24xx.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
7
8#include <algorithm>
9#include <cstring>
10#include <memory>
11#include <span>
12
13#ifdef USE_SENSOR
15#endif
16
17#define SAFE_PUBLISH_SENSOR(sensor, value) (sensor).publish_state_if_not_dup(value)
18
19#define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) (sensor).publish_state_unknown()
20
21#define highbyte(val) (uint8_t) ((val) >> 8)
22#define lowbyte(val) (uint8_t) ((val) & 0xff)
23
24namespace esphome::ld24xx {
25
26// Index of value in a constant table that may live in flash, or none
27template<typename T, size_t N> optional<size_t> find_index(const T (&arr)[N], T value) {
28 static_assert(sizeof(T) == 1 || sizeof(T) == 4, "flash tables are read as bytes or aligned 32 bit words");
29 for (size_t i = 0; i < N; i++) {
30 T entry;
31 if constexpr (sizeof(T) == 1) {
32 entry = progmem_read_byte(reinterpret_cast<const uint8_t *>(&arr[i]));
33 } else {
34 entry = arr[i]; // an aligned 32 bit read from flash is safe on ESP8266
35 }
36 if (entry == value)
37 return i;
38 }
39 return {};
40}
41
42// Formats the MAC address into the buffer, or "unknown" when it is not valid
43inline const char *format_mac_str(const uint8_t *mac_address, std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buffer) {
44 // mac_address_is_valid() rejects the multicast bit, which radar Bluetooth MACs may have set; only all-00 and all-FF
45 // mean no address
46 uint8_t any_set = 0x00;
47 uint8_t all_set = 0xFF;
48 for (size_t i = 0; i < MAC_ADDRESS_SIZE; i++) {
49 any_set |= mac_address[i];
50 all_set &= mac_address[i];
51 }
52 if (any_set != 0x00 && all_set != 0xFF) {
53 format_mac_addr_upper(mac_address, buffer.data());
54 } else {
55 ESPHOME_strncpy_P(buffer.data(), ESPHOME_PSTR("unknown"), buffer.size());
56 }
57 return buffer.data();
58}
59
60// MAC address the module uses when Bluetooth is disabled
61static constexpr uint8_t NO_MAC[MAC_ADDRESS_SIZE] PROGMEM = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01};
62
63// Stores the radar's reported MAC, or clears it when the radar reports Bluetooth off; returns whether Bluetooth is on
64inline bool store_mac(const uint8_t *reported, uint8_t *mac_address) {
65 uint8_t no_mac[MAC_ADDRESS_SIZE];
66 progmem_memcpy(no_mac, NO_MAC, MAC_ADDRESS_SIZE);
67 if (std::memcmp(reported, no_mac, MAC_ADDRESS_SIZE) == 0) {
68 std::memset(mac_address, 0, MAC_ADDRESS_SIZE);
69 return false;
70 }
71 std::memcpy(mac_address, reported, MAC_ADDRESS_SIZE);
72 return true;
73}
74
75// Helper function to format firmware version with stack allocation
76// Buffer must be exactly 20 bytes (format: "x.xxXXXXXX" fits in 11 + null terminator, 20 for safety)
77inline void format_version_str(const uint8_t *version, std::span<char, 20> buffer) {
78 ESPHOME_snprintf_P(buffer.data(), buffer.size(), ESPHOME_PSTR("%u.%02X.%02X%02X%02X%02X"), version[1], version[0],
79 version[5], version[4], version[3], version[2]);
80}
81
82// Commands and their answers share this header, a little-endian byte count and this footer
83static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
84static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA};
85static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01};
86
87inline bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) {
88 return std::memcmp(header_footer, buffer, HEADER_FOOTER_SIZE) == 0;
89}
90
91// Whole command frame length, or 0 until the count arrives; 32 bit so 0xFFFF cannot wrap to 0
92inline uint32_t cmd_frame_length(const uint8_t *buffer, uint8_t count) {
93 if (count < HEADER_FOOTER_SIZE + 2) {
94 return 0;
95 }
96 return HEADER_FOOTER_SIZE + 2 + encode_value_le<uint16_t>(&buffer[HEADER_FOOTER_SIZE]) + HEADER_FOOTER_SIZE;
97}
98
99// Drops the first `from` bytes and anything after them that does not begin a header; returns the bytes kept
100inline uint8_t drop_to_next_header(uint8_t *buffer, uint8_t count, uint8_t from, const uint8_t *data_header) {
101 for (; from < count; from++) {
102 const size_t n = std::min<size_t>(HEADER_FOOTER_SIZE, count - from);
103 if (std::memcmp(&buffer[from], CMD_FRAME_HEADER, n) == 0 || std::memcmp(&buffer[from], data_header, n) == 0) {
104 std::memmove(buffer, &buffer[from], count - from);
105 return count - from;
106 }
107 }
108 return 0;
109}
110
111// Feeds one byte into a frame buffer. A frame opens with the command header or `data_header` and ends at
112// `frame_length(buffer, count)` bytes (0 while unknown); `handle_frame(len)` returns false for a footer out
113// of place. A dropped or oversized frame calls `on_drop()`. The handler may overwrite `pos`; the splitter
114// works from the count it saved.
115template<size_t N, typename LengthFn, typename HandleFn, typename DropFn>
116void feed_frame_byte(uint8_t (&buffer)[N], uint8_t &pos, uint8_t byte, const uint8_t *data_header,
117 LengthFn frame_length, HandleFn handle_frame, DropFn on_drop) {
118 // The first byte picks the header; bytes that do not continue it are dropped
119 if (pos > 0 && pos < HEADER_FOOTER_SIZE) {
120 const uint8_t *header = buffer[0] == data_header[0] ? data_header : CMD_FRAME_HEADER;
121 if (byte != header[pos]) {
122 pos = 0;
123 }
124 }
125 if (pos == 0 && byte != data_header[0] && byte != CMD_FRAME_HEADER[0]) {
126 return;
127 }
128 buffer[pos++] = byte;
129 // Drain every whole frame left in the buffer
130 while (true) {
131 const uint32_t frame_len = frame_length(buffer, pos);
132 if (frame_len == 0 || (frame_len <= N && pos < frame_len)) {
133 return;
134 }
135 const uint8_t total = pos;
136 // Keep only what begins a header: after the frame, or after its first byte when it is dropped
137 uint8_t from = 1;
138 if (frame_len <= N && handle_frame(static_cast<uint8_t>(frame_len))) {
139 from = frame_len;
140 } else {
141 on_drop();
142 }
143 pos = drop_to_next_header(buffer, total, from, data_header);
144 if (pos == 0) {
145 return;
146 }
147 }
148}
149
150} // namespace esphome::ld24xx
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
bool store_mac(const uint8_t *reported, uint8_t *mac_address)
Definition ld24xx.h:64
uint8_t drop_to_next_header(uint8_t *buffer, uint8_t count, uint8_t from, const uint8_t *data_header)
Definition ld24xx.h:100
void format_version_str(const uint8_t *version, std::span< char, 20 > buffer)
Definition ld24xx.h:77
bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer)
Definition ld24xx.h:87
const char * format_mac_str(const uint8_t *mac_address, std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buffer)
Definition ld24xx.h:43
void feed_frame_byte(uint8_t(&buffer)[N], uint8_t &pos, uint8_t byte, const uint8_t *data_header, LengthFn frame_length, HandleFn handle_frame, DropFn on_drop)
Definition ld24xx.h:116
optional< size_t > find_index(const T(&arr)[N], T value)
Definition ld24xx.h:27
uint32_t cmd_frame_length(const uint8_t *buffer, uint8_t count)
Definition ld24xx.h:92
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:41
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
size_t size_t pos
Definition helpers.h:1333
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1754
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t