1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-08 21:03:49 +01:00

[wifi] Optimize logging to reduce flash usage by 284 bytes on ESP8266 (#11022)

This commit is contained in:
J. Nick Koston
2025-10-05 19:03:26 -05:00
committed by GitHub
parent f26e71bae6
commit f62e06104e
3 changed files with 53 additions and 47 deletions

View File

@@ -284,30 +284,34 @@ void WiFiComponent::setup_ap_config_() {
std::string name = App.get_name(); std::string name = App.get_name();
if (name.length() > 32) { if (name.length() > 32) {
if (App.is_name_add_mac_suffix_enabled()) { if (App.is_name_add_mac_suffix_enabled()) {
name.erase(name.begin() + 25, name.end() - 7); // Remove characters between 25 and the mac address // Keep first 25 chars and last 7 chars (MAC suffix), remove middle
name.erase(25, name.length() - 32);
} else { } else {
name = name.substr(0, 32); name.resize(32);
} }
} }
this->ap_.set_ssid(name); this->ap_.set_ssid(name);
} }
this->ap_setup_ = this->wifi_start_ap_(this->ap_);
auto ip_address = this->wifi_soft_ap_ip().str();
ESP_LOGCONFIG(TAG, ESP_LOGCONFIG(TAG,
"Setting up AP:\n" "Setting up AP:\n"
" AP SSID: '%s'\n" " AP SSID: '%s'\n"
" AP Password: '%s'", " AP Password: '%s'\n"
this->ap_.get_ssid().c_str(), this->ap_.get_password().c_str()); " IP Address: %s",
if (this->ap_.get_manual_ip().has_value()) { this->ap_.get_ssid().c_str(), this->ap_.get_password().c_str(), ip_address.c_str());
auto manual = *this->ap_.get_manual_ip();
auto manual_ip = this->ap_.get_manual_ip();
if (manual_ip.has_value()) {
ESP_LOGCONFIG(TAG, ESP_LOGCONFIG(TAG,
" AP Static IP: '%s'\n" " AP Static IP: '%s'\n"
" AP Gateway: '%s'\n" " AP Gateway: '%s'\n"
" AP Subnet: '%s'", " AP Subnet: '%s'",
manual.static_ip.str().c_str(), manual.gateway.str().c_str(), manual.subnet.str().c_str()); manual_ip->static_ip.str().c_str(), manual_ip->gateway.str().c_str(),
manual_ip->subnet.str().c_str());
} }
this->ap_setup_ = this->wifi_start_ap_(this->ap_);
ESP_LOGCONFIG(TAG, " IP Address: %s", this->wifi_soft_ap_ip().str().c_str());
if (!this->has_sta()) { if (!this->has_sta()) {
this->state_ = WIFI_COMPONENT_STATE_AP; this->state_ = WIFI_COMPONENT_STATE_AP;
} }
@@ -330,9 +334,9 @@ void WiFiComponent::set_sta(const WiFiAP &ap) {
} }
void WiFiComponent::clear_sta() { this->sta_.clear(); } void WiFiComponent::clear_sta() { this->sta_.clear(); }
void WiFiComponent::save_wifi_sta(const std::string &ssid, const std::string &password) { void WiFiComponent::save_wifi_sta(const std::string &ssid, const std::string &password) {
SavedWifiSettings save{}; SavedWifiSettings save{}; // zero-initialized - all bytes set to \0, guaranteeing null termination
snprintf(save.ssid, sizeof(save.ssid), "%s", ssid.c_str()); strncpy(save.ssid, ssid.c_str(), sizeof(save.ssid) - 1); // max 32 chars, byte 32 remains \0
snprintf(save.password, sizeof(save.password), "%s", password.c_str()); strncpy(save.password, password.c_str(), sizeof(save.password) - 1); // max 64 chars, byte 64 remains \0
this->pref_.save(&save); this->pref_.save(&save);
// ensure it's written immediately // ensure it's written immediately
global_preferences->sync(); global_preferences->sync();
@@ -349,8 +353,7 @@ void WiFiComponent::start_connecting(const WiFiAP &ap, bool two) {
ESP_LOGV(TAG, "Connection Params:"); ESP_LOGV(TAG, "Connection Params:");
ESP_LOGV(TAG, " SSID: '%s'", ap.get_ssid().c_str()); ESP_LOGV(TAG, " SSID: '%s'", ap.get_ssid().c_str());
if (ap.get_bssid().has_value()) { if (ap.get_bssid().has_value()) {
bssid_t b = *ap.get_bssid(); ESP_LOGV(TAG, " BSSID: %s", format_mac_address_pretty(ap.get_bssid()->data()).c_str());
ESP_LOGV(TAG, " BSSID: %02X:%02X:%02X:%02X:%02X:%02X", b[0], b[1], b[2], b[3], b[4], b[5]);
} else { } else {
ESP_LOGV(TAG, " BSSID: Not Set"); ESP_LOGV(TAG, " BSSID: Not Set");
} }
@@ -457,7 +460,6 @@ void WiFiComponent::print_connect_params_() {
ESP_LOGCONFIG(TAG, " Disabled"); ESP_LOGCONFIG(TAG, " Disabled");
return; return;
} }
ESP_LOGCONFIG(TAG, " SSID: " LOG_SECRET("'%s'"), wifi_ssid().c_str());
for (auto &ip : wifi_sta_ip_addresses()) { for (auto &ip : wifi_sta_ip_addresses()) {
if (ip.is_set()) { if (ip.is_set()) {
ESP_LOGCONFIG(TAG, " IP Address: %s", ip.str().c_str()); ESP_LOGCONFIG(TAG, " IP Address: %s", ip.str().c_str());
@@ -465,24 +467,23 @@ void WiFiComponent::print_connect_params_() {
} }
int8_t rssi = wifi_rssi(); int8_t rssi = wifi_rssi();
ESP_LOGCONFIG(TAG, ESP_LOGCONFIG(TAG,
" BSSID: " LOG_SECRET("%02X:%02X:%02X:%02X:%02X:%02X") "\n" " SSID: " LOG_SECRET("'%s'") "\n"
" Hostname: '%s'\n" " BSSID: " LOG_SECRET("%s") "\n"
" Signal strength: %d dB %s", " Hostname: '%s'\n"
bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], App.get_name().c_str(), rssi, " Signal strength: %d dB %s\n"
LOG_STR_ARG(get_signal_bars(rssi))); " Channel: %" PRId32 "\n"
" Subnet: %s\n"
" Gateway: %s\n"
" DNS1: %s\n"
" DNS2: %s",
wifi_ssid().c_str(), format_mac_address_pretty(bssid.data()).c_str(), App.get_name().c_str(), rssi,
LOG_STR_ARG(get_signal_bars(rssi)), get_wifi_channel(), wifi_subnet_mask_().str().c_str(),
wifi_gateway_ip_().str().c_str(), wifi_dns_ip_(0).str().c_str(), wifi_dns_ip_(1).str().c_str());
#ifdef ESPHOME_LOG_HAS_VERBOSE #ifdef ESPHOME_LOG_HAS_VERBOSE
if (this->selected_ap_.get_bssid().has_value()) { if (this->selected_ap_.get_bssid().has_value()) {
ESP_LOGV(TAG, " Priority: %.1f", this->get_sta_priority(*this->selected_ap_.get_bssid())); ESP_LOGV(TAG, " Priority: %.1f", this->get_sta_priority(*this->selected_ap_.get_bssid()));
} }
#endif #endif
ESP_LOGCONFIG(TAG,
" Channel: %" PRId32 "\n"
" Subnet: %s\n"
" Gateway: %s\n"
" DNS1: %s\n"
" DNS2: %s",
get_wifi_channel(), wifi_subnet_mask_().str().c_str(), wifi_gateway_ip_().str().c_str(),
wifi_dns_ip_(0).str().c_str(), wifi_dns_ip_(1).str().c_str());
#ifdef USE_WIFI_11KV_SUPPORT #ifdef USE_WIFI_11KV_SUPPORT
ESP_LOGCONFIG(TAG, ESP_LOGCONFIG(TAG,
" BTM: %s\n" " BTM: %s\n"
@@ -568,6 +569,25 @@ static void insertion_sort_scan_results(std::vector<WiFiScanResult> &results) {
} }
} }
// Helper function to log scan results - marked noinline to prevent re-inlining into loop
__attribute__((noinline)) static void log_scan_result(const WiFiScanResult &res) {
char bssid_s[18];
auto bssid = res.get_bssid();
format_mac_addr_upper(bssid.data(), bssid_s);
if (res.get_matches()) {
ESP_LOGI(TAG, "- '%s' %s" LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), res.get_is_hidden() ? "(HIDDEN) " : "",
bssid_s, LOG_STR_ARG(get_signal_bars(res.get_rssi())));
ESP_LOGD(TAG,
" Channel: %u\n"
" RSSI: %d dB",
res.get_channel(), res.get_rssi());
} else {
ESP_LOGD(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), bssid_s,
LOG_STR_ARG(get_signal_bars(res.get_rssi())));
}
}
void WiFiComponent::check_scanning_finished() { void WiFiComponent::check_scanning_finished() {
if (!this->scan_done_) { if (!this->scan_done_) {
if (millis() - this->action_started_ > 30000) { if (millis() - this->action_started_ > 30000) {
@@ -602,21 +622,7 @@ void WiFiComponent::check_scanning_finished() {
insertion_sort_scan_results(this->scan_result_); insertion_sort_scan_results(this->scan_result_);
for (auto &res : this->scan_result_) { for (auto &res : this->scan_result_) {
char bssid_s[18]; log_scan_result(res);
auto bssid = res.get_bssid();
format_mac_addr_upper(bssid.data(), bssid_s);
if (res.get_matches()) {
ESP_LOGI(TAG, "- '%s' %s" LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(),
res.get_is_hidden() ? "(HIDDEN) " : "", bssid_s, LOG_STR_ARG(get_signal_bars(res.get_rssi())));
ESP_LOGD(TAG,
" Channel: %u\n"
" RSSI: %d dB",
res.get_channel(), res.get_rssi());
} else {
ESP_LOGD(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), bssid_s,
LOG_STR_ARG(get_signal_bars(res.get_rssi())));
}
} }
if (!this->scan_result_[0].get_matches()) { if (!this->scan_result_[0].get_matches()) {

View File

@@ -301,7 +301,7 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
// if we have certs, this must be EAP-TLS // if we have certs, this must be EAP-TLS
ret = wifi_station_set_enterprise_cert_key((uint8_t *) eap.client_cert, client_cert_len + 1, ret = wifi_station_set_enterprise_cert_key((uint8_t *) eap.client_cert, client_cert_len + 1,
(uint8_t *) eap.client_key, client_key_len + 1, (uint8_t *) eap.client_key, client_key_len + 1,
(uint8_t *) eap.password.c_str(), strlen(eap.password.c_str())); (uint8_t *) eap.password.c_str(), eap.password.length());
if (ret) { if (ret) {
ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_set_cert_key failed: %d", ret); ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_set_cert_key failed: %d", ret);
} }

View File

@@ -408,11 +408,11 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
err = esp_eap_client_set_certificate_and_key((uint8_t *) eap.client_cert, client_cert_len + 1, err = esp_eap_client_set_certificate_and_key((uint8_t *) eap.client_cert, client_cert_len + 1,
(uint8_t *) eap.client_key, client_key_len + 1, (uint8_t *) eap.client_key, client_key_len + 1,
(uint8_t *) eap.password.c_str(), strlen(eap.password.c_str())); (uint8_t *) eap.password.c_str(), eap.password.length());
#else #else
err = esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *) eap.client_cert, client_cert_len + 1, err = esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *) eap.client_cert, client_cert_len + 1,
(uint8_t *) eap.client_key, client_key_len + 1, (uint8_t *) eap.client_key, client_key_len + 1,
(uint8_t *) eap.password.c_str(), strlen(eap.password.c_str())); (uint8_t *) eap.password.c_str(), eap.password.length());
#endif #endif
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGV(TAG, "set_cert_key failed %d", err); ESP_LOGV(TAG, "set_cert_key failed %d", err);