mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[esp32_ble] Optimize BLE hex formatting to eliminate sprintf dependency (#10714)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		
				
					committed by
					
						 Jesse Hills
						Jesse Hills
					
				
			
			
				
	
			
			
			
						parent
						
							6b147312cd
						
					
				
				
					commit
					926fdcbecd
				
			| @@ -130,7 +130,9 @@ class BluetoothProxy final : public esp32_ble_tracker::ESPBTDeviceListener, publ | ||||
|  | ||||
|   std::string get_bluetooth_mac_address_pretty() { | ||||
|     const uint8_t *mac = esp_bt_dev_get_address(); | ||||
|     return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | ||||
|     char buf[18]; | ||||
|     format_mac_addr_upper(mac, buf); | ||||
|     return std::string(buf); | ||||
|   } | ||||
|  | ||||
|  protected: | ||||
|   | ||||
| @@ -7,6 +7,7 @@ | ||||
| #include <cstdio> | ||||
| #include <cinttypes> | ||||
| #include "esphome/core/log.h" | ||||
| #include "esphome/core/helpers.h" | ||||
|  | ||||
| namespace esphome::esp32_ble { | ||||
|  | ||||
| @@ -169,22 +170,42 @@ bool ESPBTUUID::operator==(const ESPBTUUID &uuid) const { | ||||
| } | ||||
| esp_bt_uuid_t ESPBTUUID::get_uuid() const { return this->uuid_; } | ||||
| std::string ESPBTUUID::to_string() const { | ||||
|   char buf[40];  // Enough for 128-bit UUID with dashes | ||||
|   char *pos = buf; | ||||
|  | ||||
|   switch (this->uuid_.len) { | ||||
|     case ESP_UUID_LEN_16: | ||||
|       return str_snprintf("0x%02X%02X", 6, this->uuid_.uuid.uuid16 >> 8, this->uuid_.uuid.uuid16 & 0xff); | ||||
|       *pos++ = '0'; | ||||
|       *pos++ = 'x'; | ||||
|       *pos++ = format_hex_pretty_char(this->uuid_.uuid.uuid16 >> 12); | ||||
|       *pos++ = format_hex_pretty_char((this->uuid_.uuid.uuid16 >> 8) & 0x0F); | ||||
|       *pos++ = format_hex_pretty_char((this->uuid_.uuid.uuid16 >> 4) & 0x0F); | ||||
|       *pos++ = format_hex_pretty_char(this->uuid_.uuid.uuid16 & 0x0F); | ||||
|       *pos = '\0'; | ||||
|       return std::string(buf); | ||||
|  | ||||
|     case ESP_UUID_LEN_32: | ||||
|       return str_snprintf("0x%02" PRIX32 "%02" PRIX32 "%02" PRIX32 "%02" PRIX32, 10, (this->uuid_.uuid.uuid32 >> 24), | ||||
|                           (this->uuid_.uuid.uuid32 >> 16 & 0xff), (this->uuid_.uuid.uuid32 >> 8 & 0xff), | ||||
|                           this->uuid_.uuid.uuid32 & 0xff); | ||||
|       *pos++ = '0'; | ||||
|       *pos++ = 'x'; | ||||
|       for (int shift = 28; shift >= 0; shift -= 4) { | ||||
|         *pos++ = format_hex_pretty_char((this->uuid_.uuid.uuid32 >> shift) & 0x0F); | ||||
|       } | ||||
|       *pos = '\0'; | ||||
|       return std::string(buf); | ||||
|  | ||||
|     default: | ||||
|     case ESP_UUID_LEN_128: | ||||
|       std::string buf; | ||||
|       // Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX | ||||
|       for (int8_t i = 15; i >= 0; i--) { | ||||
|         buf += str_snprintf("%02X", 2, this->uuid_.uuid.uuid128[i]); | ||||
|         if (i == 6 || i == 8 || i == 10 || i == 12) | ||||
|           buf += "-"; | ||||
|         uint8_t byte = this->uuid_.uuid.uuid128[i]; | ||||
|         *pos++ = format_hex_pretty_char(byte >> 4); | ||||
|         *pos++ = format_hex_pretty_char(byte & 0x0F); | ||||
|         if (i == 12 || i == 10 || i == 8 || i == 6) { | ||||
|           *pos++ = '-'; | ||||
|         } | ||||
|       } | ||||
|       return buf; | ||||
|       *pos = '\0'; | ||||
|       return std::string(buf); | ||||
|   } | ||||
|   return ""; | ||||
| } | ||||
|   | ||||
| @@ -31,12 +31,13 @@ void ESP32BLEBeacon::dump_config() { | ||||
|   char uuid[37]; | ||||
|   char *bpos = uuid; | ||||
|   for (int8_t ii = 0; ii < 16; ++ii) { | ||||
|     bpos += sprintf(bpos, "%02X", this->uuid_[ii]); | ||||
|     *bpos++ = format_hex_pretty_char(this->uuid_[ii] >> 4); | ||||
|     *bpos++ = format_hex_pretty_char(this->uuid_[ii] & 0x0F); | ||||
|     if (ii == 3 || ii == 5 || ii == 7 || ii == 9) { | ||||
|       bpos += sprintf(bpos, "-"); | ||||
|       *bpos++ = '-'; | ||||
|     } | ||||
|   } | ||||
|   uuid[36] = '\0'; | ||||
|   *bpos = '\0'; | ||||
|   ESP_LOGCONFIG(TAG, | ||||
|                 "  UUID: %s, Major: %u, Minor: %u, Min Interval: %ums, Max Interval: %ums, Measured Power: %d" | ||||
|                 ", TX Power: %ddBm", | ||||
|   | ||||
| @@ -60,11 +60,14 @@ class BLEClientBase : public espbt::ESPBTClient, public Component { | ||||
|     if (address == 0) { | ||||
|       this->address_str_ = ""; | ||||
|     } else { | ||||
|       this->address_str_ = | ||||
|           str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, (uint8_t) (this->address_ >> 40) & 0xff, | ||||
|                        (uint8_t) (this->address_ >> 32) & 0xff, (uint8_t) (this->address_ >> 24) & 0xff, | ||||
|                        (uint8_t) (this->address_ >> 16) & 0xff, (uint8_t) (this->address_ >> 8) & 0xff, | ||||
|                        (uint8_t) (this->address_ >> 0) & 0xff); | ||||
|       char buf[18]; | ||||
|       uint8_t mac[6] = { | ||||
|           (uint8_t) ((this->address_ >> 40) & 0xff), (uint8_t) ((this->address_ >> 32) & 0xff), | ||||
|           (uint8_t) ((this->address_ >> 24) & 0xff), (uint8_t) ((this->address_ >> 16) & 0xff), | ||||
|           (uint8_t) ((this->address_ >> 8) & 0xff),  (uint8_t) ((this->address_ >> 0) & 0xff), | ||||
|       }; | ||||
|       format_mac_addr_upper(mac, buf); | ||||
|       this->address_str_ = buf; | ||||
|     } | ||||
|   } | ||||
|   const std::string &address_str() const { return this->address_str_; } | ||||
|   | ||||
| @@ -607,9 +607,8 @@ void ESPBTDevice::parse_adv_(const uint8_t *payload, uint8_t len) { | ||||
| } | ||||
|  | ||||
| std::string ESPBTDevice::address_str() const { | ||||
|   char mac[24]; | ||||
|   snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X", this->address_[0], this->address_[1], this->address_[2], | ||||
|            this->address_[3], this->address_[4], this->address_[5]); | ||||
|   char mac[18]; | ||||
|   format_mac_addr_upper(this->address_, mac); | ||||
|   return mac; | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user