mirror of
https://github.com/esphome/esphome.git
synced 2026-09-20 19:48:39 +00:00
[esp32_ble] Store device name in flash to reduce RAM usage
This commit is contained in:
@@ -264,7 +264,7 @@ bool ESP32BLE::ble_setup_() {
|
||||
char mac_addr[13];
|
||||
get_mac_address_into_buffer(mac_addr);
|
||||
const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
|
||||
name = make_name_with_suffix(this->name_, '-', mac_suffix_ptr, mac_address_suffix_len);
|
||||
name = make_name_with_suffix(this->name_, strlen(this->name_), '-', mac_suffix_ptr, mac_address_suffix_len);
|
||||
} else {
|
||||
name = this->name_;
|
||||
}
|
||||
|
||||
@@ -238,13 +238,16 @@ std::string str_sprintf(const char *fmt, ...) {
|
||||
// Maximum size for name with suffix: 120 (max friendly name) + 1 (separator) + 6 (MAC suffix) + 1 (null term)
|
||||
static constexpr size_t MAX_NAME_WITH_SUFFIX_SIZE = 128;
|
||||
|
||||
std::string make_name_with_suffix(const char *name, char sep, const char *suffix_ptr, size_t suffix_len) {
|
||||
std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr,
|
||||
size_t suffix_len) {
|
||||
char buffer[MAX_NAME_WITH_SUFFIX_SIZE];
|
||||
size_t name_len = strlen(name);
|
||||
size_t total_len = name_len + 1 + suffix_len;
|
||||
|
||||
// Silently truncate if needed: prioritize keeping the full suffix
|
||||
if (total_len >= MAX_NAME_WITH_SUFFIX_SIZE) {
|
||||
// NOTE: This calculation could underflow if suffix_len >= MAX_NAME_WITH_SUFFIX_SIZE - 2,
|
||||
// but this is safe because this helper is only called with small suffixes:
|
||||
// MAC suffixes (6-12 bytes), ".local" (5 bytes), etc.
|
||||
name_len = MAX_NAME_WITH_SUFFIX_SIZE - suffix_len - 2; // -2 for separator and null terminator
|
||||
total_len = name_len + 1 + suffix_len;
|
||||
}
|
||||
@@ -257,7 +260,7 @@ std::string make_name_with_suffix(const char *name, char sep, const char *suffix
|
||||
}
|
||||
|
||||
std::string make_name_with_suffix(const std::string &name, char sep, const char *suffix_ptr, size_t suffix_len) {
|
||||
return make_name_with_suffix(name.c_str(), sep, suffix_ptr, suffix_len);
|
||||
return make_name_with_suffix(name.c_str(), name.size(), sep, suffix_ptr, suffix_len);
|
||||
}
|
||||
|
||||
// Parsing & formatting
|
||||
|
||||
@@ -514,12 +514,14 @@ std::string make_name_with_suffix(const std::string &name, char sep, const char
|
||||
|
||||
/// Optimized string concatenation: name + separator + suffix (const char* overload)
|
||||
/// Uses a fixed stack buffer to avoid heap allocations.
|
||||
/// @param name The base name (null-terminated string)
|
||||
/// @param name The base name string
|
||||
/// @param name_len Length of the name
|
||||
/// @param sep Single character separator
|
||||
/// @param suffix_ptr Pointer to the suffix characters
|
||||
/// @param suffix_len Length of the suffix
|
||||
/// @return The concatenated string: name + sep + suffix
|
||||
std::string make_name_with_suffix(const char *name, char sep, const char *suffix_ptr, size_t suffix_len);
|
||||
std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr,
|
||||
size_t suffix_len);
|
||||
|
||||
///@}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user