mirror of
https://github.com/esphome/esphome.git
synced 2026-01-09 19:50:49 -07:00
[wifi] Use StringRef and std::span in WiFiConnectStateListener to avoid allocations (#12672)
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -274,7 +276,7 @@ class WiFiScanResultsListener {
|
||||
*/
|
||||
class WiFiConnectStateListener {
|
||||
public:
|
||||
virtual void on_wifi_connect_state(const std::string &ssid, const bssid_t &bssid) = 0;
|
||||
virtual void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) = 0;
|
||||
};
|
||||
|
||||
/** Listener interface for WiFi power save mode changes.
|
||||
|
||||
@@ -526,7 +526,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
||||
s_sta_connected = true;
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
for (auto *listener : global_wifi_component->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state(global_wifi_component->wifi_ssid(), global_wifi_component->wifi_bssid());
|
||||
listener->on_wifi_connect_state(StringRef(buf, it.ssid_len), it.bssid);
|
||||
}
|
||||
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
|
||||
#ifdef USE_WIFI_MANUAL_IP
|
||||
@@ -559,8 +559,9 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
||||
s_sta_connected = false;
|
||||
s_sta_connecting = false;
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||
for (auto *listener : global_wifi_component->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state("", bssid_t({0, 0, 0, 0, 0, 0}));
|
||||
listener->on_wifi_connect_state(StringRef(), EMPTY_BSSID);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
@@ -737,7 +737,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
||||
s_sta_connected = true;
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
for (auto *listener : this->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state(this->wifi_ssid(), this->wifi_bssid());
|
||||
listener->on_wifi_connect_state(StringRef(buf, it.ssid_len), it.bssid);
|
||||
}
|
||||
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
|
||||
#ifdef USE_WIFI_MANUAL_IP
|
||||
@@ -772,8 +772,9 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
||||
s_sta_connecting = false;
|
||||
error_from_callback_ = true;
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||
for (auto *listener : this->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state("", bssid_t({0, 0, 0, 0, 0, 0}));
|
||||
listener->on_wifi_connect_state(StringRef(), EMPTY_BSSID);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -303,7 +303,7 @@ void WiFiComponent::wifi_event_callback_(esphome_wifi_event_id_t event, esphome_
|
||||
format_mac_address_pretty(it.bssid).c_str(), it.channel, get_auth_mode_str(it.authmode));
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
for (auto *listener : this->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state(this->wifi_ssid(), this->wifi_bssid());
|
||||
listener->on_wifi_connect_state(StringRef(buf, it.ssid_len), it.bssid);
|
||||
}
|
||||
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
|
||||
#ifdef USE_WIFI_MANUAL_IP
|
||||
@@ -357,8 +357,9 @@ void WiFiComponent::wifi_event_callback_(esphome_wifi_event_id_t event, esphome_
|
||||
|
||||
s_sta_connecting = false;
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||
for (auto *listener : this->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state("", bssid_t({0, 0, 0, 0, 0, 0}));
|
||||
listener->on_wifi_connect_state(StringRef(), EMPTY_BSSID);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
@@ -256,8 +256,10 @@ void WiFiComponent::wifi_loop_() {
|
||||
s_sta_was_connected = true;
|
||||
ESP_LOGV(TAG, "Connected");
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
String ssid = WiFi.SSID();
|
||||
bssid_t bssid = this->wifi_bssid();
|
||||
for (auto *listener : this->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state(this->wifi_ssid(), this->wifi_bssid());
|
||||
listener->on_wifi_connect_state(StringRef(ssid.c_str(), ssid.length()), bssid);
|
||||
}
|
||||
// For static IP configurations, notify IP listeners immediately as the IP is already configured
|
||||
#ifdef USE_WIFI_MANUAL_IP
|
||||
@@ -275,8 +277,9 @@ void WiFiComponent::wifi_loop_() {
|
||||
s_sta_had_ip = false;
|
||||
ESP_LOGV(TAG, "Disconnected");
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||
for (auto *listener : this->connect_state_listeners_) {
|
||||
listener->on_wifi_connect_state("", bssid_t({0, 0, 0, 0, 0, 0}));
|
||||
listener->on_wifi_connect_state(StringRef(), EMPTY_BSSID);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -103,8 +103,8 @@ void SSIDWiFiInfo::setup() { wifi::global_wifi_component->add_connect_state_list
|
||||
|
||||
void SSIDWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "SSID", this); }
|
||||
|
||||
void SSIDWiFiInfo::on_wifi_connect_state(const std::string &ssid, const wifi::bssid_t &bssid) {
|
||||
this->publish_state(ssid);
|
||||
void SSIDWiFiInfo::on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) {
|
||||
this->publish_state(ssid.str());
|
||||
}
|
||||
|
||||
/****************
|
||||
@@ -115,7 +115,7 @@ void BSSIDWiFiInfo::setup() { wifi::global_wifi_component->add_connect_state_lis
|
||||
|
||||
void BSSIDWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "BSSID", this); }
|
||||
|
||||
void BSSIDWiFiInfo::on_wifi_connect_state(const std::string &ssid, const wifi::bssid_t &bssid) {
|
||||
void BSSIDWiFiInfo::on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) {
|
||||
char buf[18] = "unknown";
|
||||
if (mac_address_is_valid(bssid.data())) {
|
||||
format_mac_addr_upper(bssid.data(), buf);
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
#include "esphome/components/text_sensor/text_sensor.h"
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
#ifdef USE_WIFI
|
||||
#include <array>
|
||||
#include <span>
|
||||
|
||||
namespace esphome::wifi_info {
|
||||
|
||||
@@ -52,7 +54,7 @@ class SSIDWiFiInfo final : public Component, public text_sensor::TextSensor, pub
|
||||
void dump_config() override;
|
||||
|
||||
// WiFiConnectStateListener interface
|
||||
void on_wifi_connect_state(const std::string &ssid, const wifi::bssid_t &bssid) override;
|
||||
void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) override;
|
||||
};
|
||||
|
||||
class BSSIDWiFiInfo final : public Component, public text_sensor::TextSensor, public wifi::WiFiConnectStateListener {
|
||||
@@ -61,7 +63,7 @@ class BSSIDWiFiInfo final : public Component, public text_sensor::TextSensor, pu
|
||||
void dump_config() override;
|
||||
|
||||
// WiFiConnectStateListener interface
|
||||
void on_wifi_connect_state(const std::string &ssid, const wifi::bssid_t &bssid) override;
|
||||
void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) override;
|
||||
};
|
||||
|
||||
class PowerSaveModeWiFiInfo final : public Component,
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
#ifdef USE_WIFI
|
||||
#include <span>
|
||||
namespace esphome::wifi_signal {
|
||||
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
@@ -28,7 +30,7 @@ class WiFiSignalSensor : public sensor::Sensor, public PollingComponent {
|
||||
|
||||
#ifdef USE_WIFI_LISTENERS
|
||||
// WiFiConnectStateListener interface - update RSSI immediately on connect
|
||||
void on_wifi_connect_state(const std::string &ssid, const wifi::bssid_t &bssid) override { this->update(); }
|
||||
void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) override { this->update(); }
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user