Compare commits

...

3 Commits

Author SHA1 Message Date
J. Nick Koston
2bacca21c4 tweak 2026-01-22 22:23:10 -10:00
J. Nick Koston
425db688e5 [wifi] Avoid heap allocation when building AP SSID 2026-01-22 22:10:08 -10:00
J. Nick Koston
be5bfe24da [wifi] Avoid heap allocation when building AP SSID 2026-01-22 22:05:50 -10:00

View File

@@ -746,16 +746,32 @@ void WiFiComponent::setup_ap_config_() {
return;
if (this->ap_.get_ssid().empty()) {
std::string name = App.get_name();
if (name.length() > 32) {
// 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 AP_SSID_MAX_LEN = 32;
static constexpr size_t AP_SSID_PREFIX_LEN = 25;
static constexpr size_t AP_SSID_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 <= AP_SSID_MAX_LEN) {
// Name fits, use directly
this->ap_.set_ssid(name_ptr);
} else {
// Name too long, need to truncate into stack buffer
char ssid_buf[AP_SSID_MAX_LEN + 1];
if (App.is_name_add_mac_suffix_enabled()) {
// Keep first 25 chars and last 7 chars (MAC suffix), remove middle
name.erase(25, name.length() - 32);
memcpy(ssid_buf, name_ptr, AP_SSID_PREFIX_LEN);
memcpy(ssid_buf + AP_SSID_PREFIX_LEN, name_ptr + name_len - AP_SSID_SUFFIX_LEN, AP_SSID_SUFFIX_LEN);
} else {
name.resize(32);
memcpy(ssid_buf, name_ptr, AP_SSID_MAX_LEN);
}
ssid_buf[AP_SSID_MAX_LEN] = '\0';
this->ap_.set_ssid(ssid_buf);
}
this->ap_.set_ssid(name);
}
this->ap_setup_ = this->wifi_start_ap_(this->ap_);