diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 9793565ee5..8bce14c9cc 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -3,6 +3,7 @@ #include "api_pb2.h" #include "api_pb2_size.h" #include "esphome/core/log.h" +#include "esphome/core/helpers.h" #include @@ -3510,7 +3511,7 @@ void SubscribeLogsResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" message: "); - out.append("'").append(this->message).append("'"); + out.append(format_hex_pretty(this->message)); out.append("\n"); out.append(" send_failed: "); @@ -3538,7 +3539,7 @@ void NoiseEncryptionSetKeyRequest::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("NoiseEncryptionSetKeyRequest {\n"); out.append(" key: "); - out.append("'").append(this->key).append("'"); + out.append(format_hex_pretty(this->key)); out.append("\n"); out.append("}"); } @@ -4284,7 +4285,7 @@ void CameraImageResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append(" done: "); @@ -6811,7 +6812,7 @@ void BluetoothServiceData::dump_to(std::string &out) const { } out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append("}"); } @@ -6894,7 +6895,7 @@ void BluetoothLEAdvertisementResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" name: "); - out.append("'").append(this->name).append("'"); + out.append(format_hex_pretty(this->name)); out.append("\n"); out.append(" rssi: "); @@ -6987,7 +6988,7 @@ void BluetoothLERawAdvertisement::dump_to(std::string &out) const { out.append("\n"); out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append("}"); } @@ -7514,7 +7515,7 @@ void BluetoothGATTReadResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append("}"); } @@ -7578,7 +7579,7 @@ void BluetoothGATTWriteRequest::dump_to(std::string &out) const { out.append("\n"); out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append("}"); } @@ -7670,7 +7671,7 @@ void BluetoothGATTWriteDescriptorRequest::dump_to(std::string &out) const { out.append("\n"); out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append("}"); } @@ -7772,7 +7773,7 @@ void BluetoothGATTNotifyDataResponse::dump_to(std::string &out) const { out.append("\n"); out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append("}"); } @@ -8492,7 +8493,7 @@ void VoiceAssistantAudio::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("VoiceAssistantAudio {\n"); out.append(" data: "); - out.append("'").append(this->data).append("'"); + out.append(format_hex_pretty(this->data)); out.append("\n"); out.append(" end: "); diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index fc91d83972..b4923c7af0 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -4,13 +4,13 @@ #include "esphome/core/hal.h" #include "esphome/core/log.h" +#include #include #include #include #include #include #include -#include #ifdef USE_HOST #ifndef _WIN32 @@ -43,10 +43,10 @@ #include #endif #ifdef USE_ESP32 -#include "rom/crc.h" -#include "esp_mac.h" #include "esp_efuse.h" #include "esp_efuse_table.h" +#include "esp_mac.h" +#include "rom/crc.h" #endif #ifdef USE_LIBRETINY @@ -393,6 +393,21 @@ std::string format_hex_pretty(const uint16_t *data, size_t length) { return ret; } std::string format_hex_pretty(const std::vector &data) { return format_hex_pretty(data.data(), data.size()); } +std::string format_hex_pretty(const std::string &data) { + if (data.empty()) + return ""; + std::string ret; + ret.resize(3 * data.length() - 1); + for (size_t i = 0; i < data.length(); i++) { + ret[3 * i] = format_hex_pretty_char((data[i] & 0xF0) >> 4); + ret[3 * i + 1] = format_hex_pretty_char(data[i] & 0x0F); + if (i != data.length() - 1) + ret[3 * i + 2] = '.'; + } + if (data.length() > 4) + return ret + " (" + std::to_string(data.length()) + ")"; + return ret; +} std::string format_bin(const uint8_t *data, size_t length) { std::string result; diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 7d5366f323..362f3d1fa4 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -348,6 +348,8 @@ std::string format_hex_pretty(const uint16_t *data, size_t length); std::string format_hex_pretty(const std::vector &data); /// Format the vector \p data in pretty-printed, human-readable hex. std::string format_hex_pretty(const std::vector &data); +/// Format the string \p data in pretty-printed, human-readable hex. +std::string format_hex_pretty(const std::string &data); /// Format an unsigned integer in pretty-printed, human-readable hex, starting with the most significant byte. template::value, int> = 0> std::string format_hex_pretty(T val) { val = convert_big_endian(val); diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index ad8e41ba5e..615f5bbfda 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -530,7 +530,7 @@ class BytesType(TypeInfo): wire_type = WireType.LENGTH_DELIMITED # Uses wire type 2 def dump(self, name: str) -> str: - o = f'out.append("\'").append({name}).append("\'");' + o = f"out.append(format_hex_pretty({name}));" return o def get_size_calculation(self, name: str, force: bool = False) -> str: @@ -1255,6 +1255,7 @@ def main() -> None: #include "api_pb2.h" #include "api_pb2_size.h" #include "esphome/core/log.h" + #include "esphome/core/helpers.h" #include