mirror of
https://github.com/esphome/esphome.git
synced 2025-10-13 23:33:48 +01:00
adjust
This commit is contained in:
@@ -213,7 +213,11 @@ bool ESP32BLE::ble_setup_() {
|
||||
if (this->name_.has_value()) {
|
||||
name = this->name_.value();
|
||||
if (App.is_name_add_mac_suffix_enabled()) {
|
||||
name = make_name_with_suffix(name, '-', get_mac_address().substr(6));
|
||||
// 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);
|
||||
}
|
||||
} else {
|
||||
name = App.get_name();
|
||||
|
@@ -691,7 +691,9 @@ void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_
|
||||
|
||||
std::string EthernetComponent::get_use_address() const {
|
||||
if (this->use_address_.empty()) {
|
||||
return make_name_with_suffix(App.get_name(), '.', "local");
|
||||
// ".local" suffix length for mDNS hostnames
|
||||
constexpr size_t MDNS_LOCAL_SUFFIX_LEN = 5;
|
||||
return make_name_with_suffix(App.get_name(), '.', "local", MDNS_LOCAL_SUFFIX_LEN);
|
||||
}
|
||||
return this->use_address_;
|
||||
}
|
||||
|
@@ -29,7 +29,8 @@ static const char *const TAG = "mqtt";
|
||||
|
||||
MQTTClientComponent::MQTTClientComponent() {
|
||||
global_mqtt_client = this;
|
||||
this->credentials_.client_id = make_name_with_suffix(App.get_name(), '-', get_mac_address());
|
||||
const std::string mac_addr = get_mac_address();
|
||||
this->credentials_.client_id = make_name_with_suffix(App.get_name(), '-', mac_addr.c_str(), mac_addr.size());
|
||||
}
|
||||
|
||||
// Connection
|
||||
|
@@ -267,7 +267,9 @@ network::IPAddress WiFiComponent::get_dns_address(int num) {
|
||||
}
|
||||
std::string WiFiComponent::get_use_address() const {
|
||||
if (this->use_address_.empty()) {
|
||||
return make_name_with_suffix(App.get_name(), '.', "local");
|
||||
// ".local" suffix length for mDNS hostnames
|
||||
constexpr size_t MDNS_LOCAL_SUFFIX_LEN = 5;
|
||||
return make_name_with_suffix(App.get_name(), '.', "local", MDNS_LOCAL_SUFFIX_LEN);
|
||||
}
|
||||
return this->use_address_;
|
||||
}
|
||||
|
@@ -102,10 +102,14 @@ class Application {
|
||||
arch_init();
|
||||
this->name_add_mac_suffix_ = name_add_mac_suffix;
|
||||
if (name_add_mac_suffix) {
|
||||
const std::string mac_suffix = get_mac_address().substr(6);
|
||||
this->name_ = make_name_with_suffix(name, '-', mac_suffix);
|
||||
// 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();
|
||||
// Use pointer + offset to avoid substr() allocation
|
||||
const char *mac_suffix_ptr = mac_addr.c_str() + MAC_ADDRESS_SUFFIX_LEN;
|
||||
this->name_ = make_name_with_suffix(name, '-', mac_suffix_ptr, MAC_ADDRESS_SUFFIX_LEN);
|
||||
if (!friendly_name.empty()) {
|
||||
this->friendly_name_ = make_name_with_suffix(friendly_name, ' ', mac_suffix);
|
||||
this->friendly_name_ = make_name_with_suffix(friendly_name, ' ', mac_suffix_ptr, MAC_ADDRESS_SUFFIX_LEN);
|
||||
}
|
||||
} else {
|
||||
this->name_ = name;
|
||||
|
@@ -237,11 +237,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;
|
||||
// MAC address suffix length (last 6 characters of 12-char MAC address string)
|
||||
static constexpr size_t MAC_ADDRESS_SUFFIX_LEN = 6;
|
||||
// Full MAC address string length (lowercase hex without separators)
|
||||
static constexpr size_t MAC_ADDRESS_LEN = 12;
|
||||
// ".local" suffix length for mDNS hostnames
|
||||
static constexpr size_t MDNS_LOCAL_SUFFIX_LEN = 5;
|
||||
|
||||
std::string make_name_with_suffix(const std::string &name, char sep, const std::string &suffix) {
|
||||
std::string make_name_with_suffix(const std::string &name, 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 suffix_len = suffix.size();
|
||||
size_t total_len = name_len + 1 + suffix_len;
|
||||
|
||||
// Silently truncate if needed: prioritize keeping the full suffix
|
||||
@@ -255,7 +260,7 @@ std::string make_name_with_suffix(const std::string &name, char sep, const std::
|
||||
|
||||
memcpy(buffer, name.c_str(), name_len);
|
||||
buffer[name_len] = sep;
|
||||
memcpy(buffer + name_len + 1, suffix.c_str(), suffix_len);
|
||||
memcpy(buffer + name_len + 1, suffix_ptr, suffix_len);
|
||||
buffer[total_len] = '\0';
|
||||
return std::string(buffer, total_len);
|
||||
}
|
||||
|
@@ -311,9 +311,10 @@ std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, .
|
||||
/// Maximum name length supported is 120 characters for friendly names.
|
||||
/// @param name The base name string
|
||||
/// @param sep The separator character (e.g., '-', ' ', or '.')
|
||||
/// @param suffix The suffix to append (e.g., MAC address suffix or ".local")
|
||||
/// @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 std::string &name, char sep, const std::string &suffix);
|
||||
std::string make_name_with_suffix(const std::string &name, char sep, const char *suffix_ptr, size_t suffix_len);
|
||||
|
||||
///@}
|
||||
|
||||
|
Reference in New Issue
Block a user