Merge branch 'esp32_ble_name_char' into integration

This commit is contained in:
J. Nick Koston
2025-11-23 21:01:31 -06:00
3 changed files with 35 additions and 13 deletions

View File

@@ -260,29 +260,36 @@ bool ESP32BLE::ble_setup_() {
}
#endif
std::string name;
const char *device_name;
std::string name_with_suffix;
if (this->name_ != nullptr) {
name = this->name_;
if (App.is_name_add_mac_suffix_enabled()) {
// MAC address suffix length (last 6 characters of 12-char MAC address string)
constexpr size_t mac_address_suffix_len = 6;
const std::string mac_addr = get_mac_address();
const char *mac_suffix_ptr = mac_addr.c_str() + mac_address_suffix_len;
name = make_name_with_suffix(name, '-', mac_suffix_ptr, mac_address_suffix_len);
char mac_addr[13];
get_mac_address_into_buffer(mac_addr);
const char *mac_suffix_ptr = mac_addr + mac_address_suffix_len;
name_with_suffix =
make_name_with_suffix(this->name_, strlen(this->name_), '-', mac_suffix_ptr, mac_address_suffix_len);
device_name = name_with_suffix.c_str();
} else {
device_name = this->name_;
}
} else {
name = App.get_name();
if (name.length() > 20) {
name_with_suffix = App.get_name();
if (name_with_suffix.length() > 20) {
if (App.is_name_add_mac_suffix_enabled()) {
// Keep first 13 chars and last 7 chars (MAC suffix), remove middle
name.erase(13, name.length() - 20);
name_with_suffix.erase(13, name_with_suffix.length() - 20);
} else {
name.resize(20);
name_with_suffix.resize(20);
}
}
device_name = name_with_suffix.c_str();
}
err = esp_ble_gap_set_device_name(name.c_str());
err = esp_ble_gap_set_device_name(device_name);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gap_set_device_name failed: %d", err);
return false;

View File

@@ -238,9 +238,9 @@ 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 std::string &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 = name.size();
size_t total_len = name_len + 1 + suffix_len;
// Silently truncate if needed: prioritize keeping the full suffix
@@ -252,13 +252,17 @@ std::string make_name_with_suffix(const std::string &name, char sep, const char
total_len = name_len + 1 + suffix_len;
}
memcpy(buffer, name.c_str(), name_len);
memcpy(buffer, name, name_len);
buffer[name_len] = sep;
memcpy(buffer + name_len + 1, suffix_ptr, suffix_len);
buffer[total_len] = '\0';
return std::string(buffer, total_len);
}
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(), name.size(), sep, suffix_ptr, suffix_len);
}
// Parsing & formatting
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count) {

View File

@@ -515,6 +515,17 @@ std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, .
/// @return The concatenated string: name + sep + suffix
std::string make_name_with_suffix(const std::string &name, char sep, const char *suffix_ptr, size_t suffix_len);
/// Optimized string concatenation: name + separator + suffix (const char* overload)
/// Uses a fixed stack buffer to avoid heap allocations.
/// @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, size_t name_len, char sep, const char *suffix_ptr,
size_t suffix_len);
///@}
/// @name Parsing & formatting