mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[wifi] Avoid heap allocation when building AP SSID
This commit is contained in:
@@ -748,28 +748,28 @@ void WiFiComponent::setup_ap_config_() {
|
||||
if (this->ap_.get_ssid().empty()) {
|
||||
// Build AP SSID from app name without heap allocation
|
||||
// WiFi SSID max is 32 bytes, with MAC suffix we keep first 25 + last 7
|
||||
static constexpr size_t max_ssid_len = 32;
|
||||
static constexpr size_t prefix_len = 25;
|
||||
static constexpr size_t suffix_len = 7;
|
||||
static constexpr size_t MAX_SSID_LEN = 32;
|
||||
static constexpr size_t PREFIX_LEN = 25;
|
||||
static constexpr size_t SUFFIX_LEN = 7;
|
||||
|
||||
const std::string &app_name = App.get_name();
|
||||
const char *name_ptr = app_name.c_str();
|
||||
size_t name_len = app_name.length();
|
||||
|
||||
if (name_len <= max_ssid_len) {
|
||||
if (name_len <= MAX_SSID_LEN) {
|
||||
// Name fits, use directly
|
||||
this->ap_.set_ssid(name_ptr);
|
||||
} else {
|
||||
// Name too long, need to truncate into stack buffer
|
||||
char ssid_buf[max_ssid_len + 1];
|
||||
char ssid_buf[MAX_SSID_LEN + 1];
|
||||
if (App.is_name_add_mac_suffix_enabled()) {
|
||||
// Keep first 25 chars and last 7 chars (MAC suffix), remove middle
|
||||
memcpy(ssid_buf, name_ptr, prefix_len);
|
||||
memcpy(ssid_buf + prefix_len, name_ptr + name_len - suffix_len, suffix_len);
|
||||
memcpy(ssid_buf, name_ptr, PREFIX_LEN);
|
||||
memcpy(ssid_buf + PREFIX_LEN, name_ptr + name_len - SUFFIX_LEN, SUFFIX_LEN);
|
||||
} else {
|
||||
memcpy(ssid_buf, name_ptr, max_ssid_len);
|
||||
memcpy(ssid_buf, name_ptr, MAX_SSID_LEN);
|
||||
}
|
||||
ssid_buf[max_ssid_len] = '\0';
|
||||
ssid_buf[MAX_SSID_LEN] = '\0';
|
||||
this->ap_.set_ssid(ssid_buf);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user