This commit is contained in:
kbx81
2025-11-24 01:30:29 -06:00
parent 66d6c85aa7
commit deb8ffafa8

View File

@@ -1,4 +1,3 @@
#include "wifi_component.h"
#ifdef USE_WIFI
@@ -20,6 +19,10 @@ namespace wifi {
static const char *const TAG = "wifi_pico_w";
// Track previous state for detecting changes
static bool s_sta_was_connected = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static bool s_sta_had_ip = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
if (sta.has_value()) {
if (sta.value()) {
@@ -219,11 +222,49 @@ network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
}
void WiFiComponent::wifi_loop_() {
// Handle scan completion
if (this->state_ == WIFI_COMPONENT_STATE_STA_SCANNING && !cyw43_wifi_scan_active(&cyw43_state)) {
this->scan_done_ = true;
ESP_LOGV(TAG, "Scan done");
this->wifi_scan_state_callback_.call(this->scan_result_);
}
// Poll for connection state changes
// The arduino-pico WiFi library doesn't have event callbacks like ESP8266/ESP32,
// so we need to poll the link status to detect state changes
auto status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
bool is_connected = (status == CYW43_LINK_UP);
// Detect connection state change
if (is_connected && !s_sta_was_connected) {
// Just connected
s_sta_was_connected = true;
ESP_LOGV(TAG, "Connected");
this->wifi_connect_state_callback_.call(this->wifi_ssid(), this->wifi_bssid());
} else if (!is_connected && s_sta_was_connected) {
// Just disconnected
s_sta_was_connected = false;
s_sta_had_ip = false;
ESP_LOGV(TAG, "Disconnected");
this->wifi_connect_state_callback_.call("", bssid_t({0, 0, 0, 0, 0, 0}));
}
// Detect IP address changes (only when connected)
if (is_connected) {
bool has_ip = false;
// Check for any IP address (IPv4 or IPv6)
for (auto addr : addrList) {
has_ip = true;
break;
}
if (has_ip && !s_sta_had_ip) {
// Just got IP address
s_sta_had_ip = true;
ESP_LOGV(TAG, "Got IP address");
this->ip_state_callback_.call(this->wifi_sta_ip_addresses(), this->get_dns_address(0), this->get_dns_address(1));
}
}
}
void WiFiComponent::wifi_pre_setup_() {}