mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	[api] Dump bytes fields as hex instead of unreadable string (#9288)
This commit is contained in:
		| @@ -3,6 +3,7 @@ | ||||
| #include "api_pb2.h" | ||||
| #include "api_pb2_size.h" | ||||
| #include "esphome/core/log.h" | ||||
| #include "esphome/core/helpers.h" | ||||
|  | ||||
| #include <cinttypes> | ||||
|  | ||||
| @@ -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: "); | ||||
|   | ||||
| @@ -4,13 +4,13 @@ | ||||
| #include "esphome/core/hal.h" | ||||
| #include "esphome/core/log.h" | ||||
|  | ||||
| #include <strings.h> | ||||
| #include <algorithm> | ||||
| #include <cctype> | ||||
| #include <cmath> | ||||
| #include <cstdarg> | ||||
| #include <cstdio> | ||||
| #include <cstring> | ||||
| #include <strings.h> | ||||
|  | ||||
| #ifdef USE_HOST | ||||
| #ifndef _WIN32 | ||||
| @@ -43,10 +43,10 @@ | ||||
| #include <random> | ||||
| #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<uint16_t> &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; | ||||
|   | ||||
| @@ -348,6 +348,8 @@ std::string format_hex_pretty(const uint16_t *data, size_t length); | ||||
| std::string format_hex_pretty(const std::vector<uint8_t> &data); | ||||
| /// Format the vector \p data in pretty-printed, human-readable hex. | ||||
| std::string format_hex_pretty(const std::vector<uint16_t> &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<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex_pretty(T val) { | ||||
|   val = convert_big_endian(val); | ||||
|   | ||||
| @@ -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 <cinttypes> | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user