diff --git a/esphome/components/opentherm/opentherm.cpp b/esphome/components/opentherm/opentherm.cpp index c6443f1282..2bf438a52f 100644 --- a/esphome/components/opentherm/opentherm.cpp +++ b/esphome/components/opentherm/opentherm.cpp @@ -561,8 +561,9 @@ const char *OpenTherm::message_id_to_str(MessageId id) { } void OpenTherm::debug_data(OpenthermData &data) { - ESP_LOGD(TAG, "%s %s %s %s", format_bin(data.type).c_str(), format_bin(data.id).c_str(), - format_bin(data.valueHB).c_str(), format_bin(data.valueLB).c_str()); + char type_buf[9], id_buf[9], hb_buf[9], lb_buf[9]; + ESP_LOGD(TAG, "%s %s %s %s", format_bin_to(type_buf, data.type), format_bin_to(id_buf, data.id), + format_bin_to(hb_buf, data.valueHB), format_bin_to(lb_buf, data.valueLB)); ESP_LOGD(TAG, "type: %s; id: %u; HB: %u; LB: %u; uint_16: %u; float: %f", this->message_type_to_str((MessageType) data.type), data.id, data.valueHB, data.valueLB, data.u16(), data.f88()); diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 5de1c70562..4e3761675d 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -404,15 +404,31 @@ std::string format_hex_pretty(const std::string &data, char separator, bool show return format_hex_pretty_uint8(reinterpret_cast(data.data()), data.length(), separator, show_length); } +char *format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length) { + if (buffer_size == 0) { + return buffer; + } + // Calculate max bytes we can format: each byte needs 8 chars + size_t max_bytes = (buffer_size - 1) / 8; + if (max_bytes == 0 || length == 0) { + buffer[0] = '\0'; + return buffer; + } + size_t bytes_to_format = std::min(length, max_bytes); + + for (size_t byte_idx = 0; byte_idx < bytes_to_format; byte_idx++) { + for (size_t bit_idx = 0; bit_idx < 8; bit_idx++) { + buffer[byte_idx * 8 + bit_idx] = ((data[byte_idx] >> (7 - bit_idx)) & 1) + '0'; + } + } + buffer[bytes_to_format * 8] = '\0'; + return buffer; +} + std::string format_bin(const uint8_t *data, size_t length) { std::string result; result.resize(length * 8); - for (size_t byte_idx = 0; byte_idx < length; byte_idx++) { - for (size_t bit_idx = 0; bit_idx < 8; bit_idx++) { - result[byte_idx * 8 + bit_idx] = ((data[byte_idx] >> (7 - bit_idx)) & 1) + '0'; - } - } - + format_bin_to(&result[0], length * 8 + 1, data, length); return result; } diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 0acc6bdc60..409c691cb1 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -1096,9 +1096,66 @@ std::string format_hex_pretty(T val, char separator = '.', bool show_length = tr return format_hex_pretty(reinterpret_cast(&val), sizeof(T), separator, show_length); } +/// Calculate buffer size needed for format_bin_to: "01234567...\0" = bytes * 8 + 1 +constexpr size_t format_bin_size(size_t byte_count) { return byte_count * 8 + 1; } + +/** Format byte array as binary string to buffer. + * + * Each byte is formatted as 8 binary digits (MSB first). + * Truncates output if data exceeds buffer capacity. + * + * @param buffer Output buffer to write to. + * @param buffer_size Size of the output buffer. + * @param data Pointer to the byte array to format. + * @param length Number of bytes in the array. + * @return Pointer to buffer. + * + * Buffer size needed: length * 8 + 1 (use format_bin_size()). + * + * Example: + * @code + * char buf[9]; // format_bin_size(1) + * format_bin_to(buf, sizeof(buf), data, 1); // "10101011" + * @endcode + */ +char *format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length); + +/// Format byte array as binary to buffer. Automatically deduces buffer size. +template inline char *format_bin_to(char (&buffer)[N], const uint8_t *data, size_t length) { + static_assert(N >= 9, "Buffer must hold at least one binary byte (9 chars)"); + return format_bin_to(buffer, N, data, length); +} + +/** Format an unsigned integer in binary to buffer, MSB first. + * + * @tparam N Buffer size (must be >= sizeof(T) * 8 + 1). + * @tparam T Unsigned integer type. + * @param buffer Output buffer to write to. + * @param val The unsigned integer value to format. + * @return Pointer to buffer. + * + * Example: + * @code + * char buf[9]; // format_bin_size(sizeof(uint8_t)) + * format_bin_to(buf, uint8_t{0xAA}); // "10101010" + * char buf16[17]; // format_bin_size(sizeof(uint16_t)) + * format_bin_to(buf16, uint16_t{0x1234}); // "0001001000110100" + * @endcode + */ +template::value, int> = 0> +inline char *format_bin_to(char (&buffer)[N], T val) { + static_assert(N >= sizeof(T) * 8 + 1, "Buffer too small for type"); + val = convert_big_endian(val); + return format_bin_to(buffer, reinterpret_cast(&val), sizeof(T)); +} + /// Format the byte array \p data of length \p len in binary. +/// @warning Allocates heap memory. Use format_bin_to() with a stack buffer instead. +/// Causes heap fragmentation on long-running devices. std::string format_bin(const uint8_t *data, size_t length); /// Format an unsigned integer in binary, starting with the most significant byte. +/// @warning Allocates heap memory. Use format_bin_to() with a stack buffer instead. +/// Causes heap fragmentation on long-running devices. template::value, int> = 0> std::string format_bin(T val) { val = convert_big_endian(val); return format_bin(reinterpret_cast(&val), sizeof(T)); diff --git a/script/ci-custom.py b/script/ci-custom.py index e63e61e096..e227ec873e 100755 --- a/script/ci-custom.py +++ b/script/ci-custom.py @@ -682,6 +682,7 @@ def lint_trailing_whitespace(fname, match): # Heap-allocating helpers that cause fragmentation on long-running embedded devices. # These return std::string and should be replaced with stack-based alternatives. HEAP_ALLOCATING_HELPERS = { + "format_bin": "format_bin_to() with a stack buffer", "format_hex": "format_hex_to() with a stack buffer", "format_hex_pretty": "format_hex_pretty_to() with a stack buffer", "format_mac_address_pretty": "format_mac_addr_upper() with a stack buffer", @@ -699,6 +700,7 @@ HEAP_ALLOCATING_HELPERS = { # get_mac_address(?!_) ensures we don't match get_mac_address_into_buffer, etc. # CPP_RE_EOL captures rest of line so NOLINT comments are detected r"[^\w](" + r"format_bin(?!_)|" r"format_hex(?!_)|" r"format_hex_pretty(?!_)|" r"format_mac_address_pretty|"