[core] Add format_hex_pretty_to buffer helper and reduce code duplication (#12687)

This commit is contained in:
J. Nick Koston
2025-12-30 11:51:51 -10:00
committed by GitHub
parent dae7ba604a
commit bd3ecad3a1
7 changed files with 96 additions and 68 deletions
@@ -123,10 +123,11 @@ void ZWaveProxy::process_uart_() {
}
void ZWaveProxy::dump_config() {
char hex_buf[format_hex_pretty_size(ZWAVE_HOME_ID_SIZE)];
ESP_LOGCONFIG(TAG,
"Z-Wave Proxy:\n"
" Home ID: %s",
format_hex_pretty(this->home_id_.data(), this->home_id_.size(), ':', false).c_str());
format_hex_pretty_to(hex_buf, this->home_id_.data(), this->home_id_.size()));
}
void ZWaveProxy::api_connection_authenticated(api::APIConnection *conn) {
@@ -167,7 +168,8 @@ bool ZWaveProxy::set_home_id(const uint8_t *new_home_id) {
return false; // No change
}
std::memcpy(this->home_id_.data(), new_home_id, this->home_id_.size());
ESP_LOGI(TAG, "Home ID: %s", format_hex_pretty(this->home_id_.data(), this->home_id_.size(), ':', false).c_str());
char hex_buf[format_hex_pretty_size(ZWAVE_HOME_ID_SIZE)];
ESP_LOGI(TAG, "Home ID: %s", format_hex_pretty_to(hex_buf, this->home_id_.data(), this->home_id_.size()));
this->home_id_ready_ = true;
return true; // Home ID was changed
}
+3 -2
View File
@@ -14,6 +14,7 @@
namespace esphome::zwave_proxy {
static constexpr size_t MAX_ZWAVE_FRAME_SIZE = 257; // Maximum Z-Wave frame size
static constexpr size_t ZWAVE_HOME_ID_SIZE = 4; // Z-Wave Home ID size in bytes
enum ZWaveResponseTypes : uint8_t {
ZWAVE_FRAME_TYPE_ACK = 0x06,
@@ -73,8 +74,8 @@ class ZWaveProxy : public uart::UARTDevice, public Component {
// Pre-allocated message - always ready to send
api::ZWaveProxyFrame outgoing_proto_msg_;
std::array<uint8_t, MAX_ZWAVE_FRAME_SIZE> buffer_; // Fixed buffer for incoming data
std::array<uint8_t, 4> home_id_{0, 0, 0, 0}; // Fixed buffer for home ID
std::array<uint8_t, MAX_ZWAVE_FRAME_SIZE> buffer_; // Fixed buffer for incoming data
std::array<uint8_t, ZWAVE_HOME_ID_SIZE> home_id_{}; // Fixed buffer for home ID
// Pointers and 32-bit values (aligned together)
api::APIConnection *api_connection_{nullptr}; // Current subscribed client