mirror of
https://github.com/esphome/esphome.git
synced 2026-09-16 01:28:39 +00:00
Move ESP8266WiFiSTAState enum to cpp, use uint8_t in header
Keep the enum definition private to wifi_component_esp8266.cpp and store as uint8_t in the class to avoid leaking platform-specific types into the shared header.
This commit is contained in:
@@ -398,17 +398,6 @@ class WiFiPowerSaveListener {
|
||||
virtual void on_wifi_power_save(WiFiPowerSaveMode mode) = 0;
|
||||
};
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
enum class ESP8266WiFiSTAState : uint8_t {
|
||||
IDLE, // Not connecting
|
||||
CONNECTING, // Connection in progress
|
||||
ASSOCIATED, // Associated to AP, waiting for IP
|
||||
CONNECTED, // Successfully connected with IP
|
||||
ERROR_NOT_FOUND, // AP not found (probe failed)
|
||||
ERROR_FAILED, // Connection failed (auth, timeout, etc.)
|
||||
};
|
||||
#endif
|
||||
|
||||
/// This component is responsible for managing the ESP WiFi interface.
|
||||
class WiFiComponent final : public Component {
|
||||
public:
|
||||
@@ -826,7 +815,7 @@ class WiFiComponent final : public Component {
|
||||
#endif /* USE_NETWORK_IPV6 */
|
||||
bool error_from_callback_{false};
|
||||
#ifdef USE_ESP8266
|
||||
ESP8266WiFiSTAState sta_state_{ESP8266WiFiSTAState::IDLE};
|
||||
uint8_t sta_state_{0}; // ESP8266WiFiSTAState, defined in wifi_component_esp8266.cpp
|
||||
#endif
|
||||
RetryHiddenMode retry_hidden_mode_{RetryHiddenMode::BLIND_RETRY};
|
||||
RoamingState roaming_state_{RoamingState::IDLE};
|
||||
|
||||
@@ -44,6 +44,15 @@ namespace esphome::wifi {
|
||||
|
||||
static const char *const TAG = "wifi_esp8266";
|
||||
|
||||
enum class ESP8266WiFiSTAState : uint8_t {
|
||||
IDLE, // Not connecting
|
||||
CONNECTING, // Connection in progress
|
||||
ASSOCIATED, // Associated to AP, waiting for IP
|
||||
CONNECTED, // Successfully connected with IP
|
||||
ERROR_NOT_FOUND, // AP not found (probe failed)
|
||||
ERROR_FAILED, // Connection failed (auth, timeout, etc.)
|
||||
};
|
||||
|
||||
bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
|
||||
uint8_t current_mode = wifi_get_opmode();
|
||||
bool current_sta = current_mode & 0b01;
|
||||
@@ -353,7 +362,7 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
|
||||
|
||||
// Reset flags, do this _before_ wifi_station_connect as the callback method
|
||||
// may be called from wifi_station_connect
|
||||
this->sta_state_ = ESP8266WiFiSTAState::CONNECTING;
|
||||
this->sta_state_ = static_cast<uint8_t>(ESP8266WiFiSTAState::CONNECTING);
|
||||
|
||||
ETS_UART_INTR_DISABLE();
|
||||
ret = wifi_station_connect();
|
||||
@@ -483,7 +492,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
||||
ESP_LOGV(TAG, "Connected ssid='%.*s' bssid=%s channel=%u", it.ssid_len, (const char *) it.ssid, bssid_buf,
|
||||
it.channel);
|
||||
#endif
|
||||
global_wifi_component->sta_state_ = ESP8266WiFiSTAState::ASSOCIATED;
|
||||
global_wifi_component->sta_state_ = static_cast<uint8_t>(ESP8266WiFiSTAState::ASSOCIATED);
|
||||
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
|
||||
// Defer listener notification until state machine reaches STA_CONNECTED
|
||||
// This ensures wifi.connected condition returns true in listener automations
|
||||
@@ -496,13 +505,13 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
||||
if (it.reason == REASON_NO_AP_FOUND) {
|
||||
ESP_LOGW(TAG, "Disconnected ssid='%.*s' reason='Probe Request Unsuccessful'", it.ssid_len,
|
||||
(const char *) it.ssid);
|
||||
global_wifi_component->sta_state_ = ESP8266WiFiSTAState::ERROR_NOT_FOUND;
|
||||
global_wifi_component->sta_state_ = static_cast<uint8_t>(ESP8266WiFiSTAState::ERROR_NOT_FOUND);
|
||||
} else {
|
||||
char bssid_s[18];
|
||||
format_mac_addr_upper(it.bssid, bssid_s);
|
||||
ESP_LOGW(TAG, "Disconnected ssid='%.*s' bssid=" LOG_SECRET("%s") " reason='%s'", it.ssid_len,
|
||||
(const char *) it.ssid, bssid_s, LOG_STR_ARG(get_disconnect_reason_str(it.reason)));
|
||||
global_wifi_component->sta_state_ = ESP8266WiFiSTAState::ERROR_FAILED;
|
||||
global_wifi_component->sta_state_ = static_cast<uint8_t>(ESP8266WiFiSTAState::ERROR_FAILED);
|
||||
}
|
||||
global_wifi_component->error_from_callback_ = true;
|
||||
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
|
||||
@@ -529,7 +538,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
||||
mask_buf[network::IP_ADDRESS_BUFFER_SIZE];
|
||||
ESP_LOGV(TAG, "static_ip=%s gateway=%s netmask=%s", network::IPAddress(&it.ip).str_to(ip_buf),
|
||||
network::IPAddress(&it.gw).str_to(gw_buf), network::IPAddress(&it.mask).str_to(mask_buf));
|
||||
global_wifi_component->sta_state_ = ESP8266WiFiSTAState::CONNECTED;
|
||||
global_wifi_component->sta_state_ = static_cast<uint8_t>(ESP8266WiFiSTAState::CONNECTED);
|
||||
#ifdef USE_WIFI_IP_STATE_LISTENERS
|
||||
// Defer listener callbacks to main loop - system context has limited stack
|
||||
global_wifi_component->pending_.got_ip = true;
|
||||
@@ -628,7 +637,7 @@ WiFiSTAConnectStatus WiFiComponent::wifi_sta_connect_status_() const {
|
||||
// wifi_station_get_connect_status() which queries the SDK every time.
|
||||
// Use if statements with early returns instead of switch to avoid GCC
|
||||
// generating a CSWTCH lookup table in .rodata (flash) on ESP8266.
|
||||
auto state = this->sta_state_;
|
||||
auto state = static_cast<ESP8266WiFiSTAState>(this->sta_state_);
|
||||
if (state == ESP8266WiFiSTAState::CONNECTED)
|
||||
return WiFiSTAConnectStatus::CONNECTED;
|
||||
if (state == ESP8266WiFiSTAState::ERROR_NOT_FOUND)
|
||||
|
||||
Reference in New Issue
Block a user