ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bluedroid_hex.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <cstring>
6
8
9namespace esphome::esp32_ble {
10
13inline bool parse_bdaddr(const char *str, uint8_t *addr) {
14 uint8_t out[6];
15 for (size_t i = 0; i < sizeof(out); i++) {
16 uint8_t value = parse_hex_char(*str++);
17 if (value == INVALID_HEX_CHAR)
18 return false;
19 uint8_t low = parse_hex_char(*str);
20 if (low != INVALID_HEX_CHAR) {
21 value = (value << 4) | low;
22 str++;
23 }
24 if (i < sizeof(out) - 1 && *str++ != ':')
25 return false;
26 out[i] = value;
27 }
28 memcpy(addr, out, sizeof(out));
29 return true;
30}
31
34inline bool parse_config_bin(const char *str, size_t len, uint8_t *value, size_t *length) {
35 if (len % 2 != 0 || len / 2 > *length)
36 return false;
37 for (size_t i = 0; i < len; i++) {
38 if (parse_hex_char(str[i]) == INVALID_HEX_CHAR)
39 return false;
40 }
41 parse_hex(str, len, value, len / 2);
42 *length = len / 2;
43 return true;
44}
45
46// Covers every btc_config_get_bin caller in Bluetooth LE builds (bonding keys are at most 28 bytes)
47static constexpr size_t CONFIG_BIN_STACK_BYTES = 32;
48
50template<typename GetStr>
51bool config_get_bin(GetStr get_str, const char *section, const char *key, uint8_t *value, size_t *length) {
52 // A stored string too long for *length is truncated to an odd length, which parse_config_bin rejects
53 size_t size = 2 * *length + 2;
55 int size_bytes = static_cast<int>(size);
56 if (!get_str(section, key, buf.get(), &size_bytes))
57 return false;
58 return parse_config_bin(buf.get(), static_cast<size_t>(size_bytes) - 1, value, length);
59}
60
61} // namespace esphome::esp32_ble
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:872
const char * key
std::vector< uint8_t > & out
Definition gif.cpp:49
bool parse_bdaddr(const char *str, uint8_t *addr)
Parse "xx:xx:xx:xx:xx:xx" as Bluedroid's string_to_bdaddr does: one or two hex digits per byte,...
bool config_get_bin(GetStr get_str, const char *section, const char *key, uint8_t *value, size_t *length)
btc_config_get_bin on top of btc_config_get_str, passed as get_str, which strlcpy's the stored string...
bool parse_config_bin(const char *str, size_t len, uint8_t *value, size_t *length)
Decode a stored hex string as Bluedroid's btc_config_get_bin does: even length, at most *length bytes...
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:370
uint16_t size
Definition helpers.cpp:34
constexpr uint8_t parse_hex_char(char c)
Definition helpers.h:1523
uint16_t length
Definition tt21100.cpp:0