mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 11:56:11 +08: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/automation.h"
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/string_ref.h"
|
||||||
|
|
||||||
|
#include <span>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -274,7 +276,7 @@ class WiFiScanResultsListener {
|
|||||||
*/
|
*/
|
||||||
class WiFiConnectStateListener {
|
class WiFiConnectStateListener {
|
||||||
public:
|
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.
|
/** 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;
|
s_sta_connected = true;
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
for (auto *listener : global_wifi_component->connect_state_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
|
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
|
||||||
#ifdef USE_WIFI_MANUAL_IP
|
#ifdef USE_WIFI_MANUAL_IP
|
||||||
@@ -559,8 +559,9 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
|||||||
s_sta_connected = false;
|
s_sta_connected = false;
|
||||||
s_sta_connecting = false;
|
s_sta_connecting = false;
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
|
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||||
for (auto *listener : global_wifi_component->connect_state_listeners_) {
|
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
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -737,7 +737,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
|||||||
s_sta_connected = true;
|
s_sta_connected = true;
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
for (auto *listener : this->connect_state_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
|
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
|
||||||
#ifdef USE_WIFI_MANUAL_IP
|
#ifdef USE_WIFI_MANUAL_IP
|
||||||
@@ -772,8 +772,9 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
|||||||
s_sta_connecting = false;
|
s_sta_connecting = false;
|
||||||
error_from_callback_ = true;
|
error_from_callback_ = true;
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
|
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||||
for (auto *listener : this->connect_state_listeners_) {
|
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
|
#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));
|
format_mac_address_pretty(it.bssid).c_str(), it.channel, get_auth_mode_str(it.authmode));
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
for (auto *listener : this->connect_state_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
|
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
|
||||||
#ifdef USE_WIFI_MANUAL_IP
|
#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;
|
s_sta_connecting = false;
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
|
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||||
for (auto *listener : this->connect_state_listeners_) {
|
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
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -256,8 +256,10 @@ void WiFiComponent::wifi_loop_() {
|
|||||||
s_sta_was_connected = true;
|
s_sta_was_connected = true;
|
||||||
ESP_LOGV(TAG, "Connected");
|
ESP_LOGV(TAG, "Connected");
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
|
String ssid = WiFi.SSID();
|
||||||
|
bssid_t bssid = this->wifi_bssid();
|
||||||
for (auto *listener : this->connect_state_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(ssid.c_str(), ssid.length()), bssid);
|
||||||
}
|
}
|
||||||
// For static IP configurations, notify IP listeners immediately as the IP is already configured
|
// For static IP configurations, notify IP listeners immediately as the IP is already configured
|
||||||
#ifdef USE_WIFI_MANUAL_IP
|
#ifdef USE_WIFI_MANUAL_IP
|
||||||
@@ -275,8 +277,9 @@ void WiFiComponent::wifi_loop_() {
|
|||||||
s_sta_had_ip = false;
|
s_sta_had_ip = false;
|
||||||
ESP_LOGV(TAG, "Disconnected");
|
ESP_LOGV(TAG, "Disconnected");
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
|
static constexpr uint8_t EMPTY_BSSID[6] = {};
|
||||||
for (auto *listener : this->connect_state_listeners_) {
|
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
|
#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::dump_config() { LOG_TEXT_SENSOR("", "SSID", this); }
|
||||||
|
|
||||||
void SSIDWiFiInfo::on_wifi_connect_state(const std::string &ssid, const wifi::bssid_t &bssid) {
|
void SSIDWiFiInfo::on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) {
|
||||||
this->publish_state(ssid);
|
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::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";
|
char buf[18] = "unknown";
|
||||||
if (mac_address_is_valid(bssid.data())) {
|
if (mac_address_is_valid(bssid.data())) {
|
||||||
format_mac_addr_upper(bssid.data(), buf);
|
format_mac_addr_upper(bssid.data(), buf);
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/string_ref.h"
|
||||||
#include "esphome/components/text_sensor/text_sensor.h"
|
#include "esphome/components/text_sensor/text_sensor.h"
|
||||||
#include "esphome/components/wifi/wifi_component.h"
|
#include "esphome/components/wifi/wifi_component.h"
|
||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
namespace esphome::wifi_info {
|
namespace esphome::wifi_info {
|
||||||
|
|
||||||
@@ -52,7 +54,7 @@ class SSIDWiFiInfo final : public Component, public text_sensor::TextSensor, pub
|
|||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
// WiFiConnectStateListener interface
|
// 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 {
|
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;
|
void dump_config() override;
|
||||||
|
|
||||||
// WiFiConnectStateListener interface
|
// 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,
|
class PowerSaveModeWiFiInfo final : public Component,
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/string_ref.h"
|
||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
#include "esphome/components/wifi/wifi_component.h"
|
#include "esphome/components/wifi/wifi_component.h"
|
||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
|
#include <span>
|
||||||
namespace esphome::wifi_signal {
|
namespace esphome::wifi_signal {
|
||||||
|
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
@@ -28,7 +30,7 @@ class WiFiSignalSensor : public sensor::Sensor, public PollingComponent {
|
|||||||
|
|
||||||
#ifdef USE_WIFI_LISTENERS
|
#ifdef USE_WIFI_LISTENERS
|
||||||
// WiFiConnectStateListener interface - update RSSI immediately on connect
|
// 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
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user