1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-14 01:02:19 +01:00
This commit is contained in:
J. Nick Koston
2025-06-18 14:33:58 +02:00
parent 51e4c45e5c
commit bd50a7f1ab
2 changed files with 15 additions and 20 deletions

View File

@@ -400,19 +400,21 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
case ETHERNET_EVENT_START: case ETHERNET_EVENT_START:
event_name = "ETH started"; event_name = "ETH started";
global_eth_component->started_ = true; global_eth_component->started_ = true;
global_eth_component->enable_loop(); global_eth_component->enable_loop_soon_any_context();
break; break;
case ETHERNET_EVENT_STOP: case ETHERNET_EVENT_STOP:
event_name = "ETH stopped"; event_name = "ETH stopped";
global_eth_component->started_ = false; global_eth_component->started_ = false;
global_eth_component->set_connected_(false); // This will enable the loop global_eth_component->connected_ = false;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
break; break;
case ETHERNET_EVENT_CONNECTED: case ETHERNET_EVENT_CONNECTED:
event_name = "ETH connected"; event_name = "ETH connected";
break; break;
case ETHERNET_EVENT_DISCONNECTED: case ETHERNET_EVENT_DISCONNECTED:
event_name = "ETH disconnected"; event_name = "ETH disconnected";
global_eth_component->set_connected_(false); // This will enable the loop global_eth_component->connected_ = false;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
break; break;
default: default:
return; return;
@@ -428,9 +430,11 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip)); ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
global_eth_component->got_ipv4_address_ = true; global_eth_component->got_ipv4_address_ = true;
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0) #if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->set_connected_(global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT); global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#else #else
global_eth_component->set_connected_(true); global_eth_component->connected_ = true;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif /* USE_NETWORK_IPV6 */ #endif /* USE_NETWORK_IPV6 */
} }
@@ -441,10 +445,12 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip)); ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
global_eth_component->ipv6_count_ += 1; global_eth_component->ipv6_count_ += 1;
#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0) #if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->set_connected_(global_eth_component->got_ipv4_address_ && global_eth_component->connected_ =
(global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT)); global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#else #else
global_eth_component->set_connected_(global_eth_component->got_ipv4_address_); global_eth_component->connected_ = global_eth_component->got_ipv4_address_;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif #endif
} }
#endif /* USE_NETWORK_IPV6 */ #endif /* USE_NETWORK_IPV6 */
@@ -521,15 +527,6 @@ void EthernetComponent::start_connect_() {
bool EthernetComponent::is_connected() { return this->state_ == EthernetComponentState::CONNECTED; } bool EthernetComponent::is_connected() { return this->state_ == EthernetComponentState::CONNECTED; }
void EthernetComponent::set_connected_(bool connected) {
if (this->connected_ != connected) {
this->connected_ = connected;
// Always enable loop when connection state changes
// so the state machine can process the state change
this->enable_loop();
}
}
void EthernetComponent::dump_connect_params_() { void EthernetComponent::dump_connect_params_() {
esp_netif_ip_info_t ip; esp_netif_ip_info_t ip;
esp_netif_get_ip_info(this->eth_netif_, &ip); esp_netif_get_ip_info(this->eth_netif_, &ip);
@@ -633,7 +630,7 @@ bool EthernetComponent::powerdown() {
ESP_LOGE(TAG, "Ethernet PHY not assigned"); ESP_LOGE(TAG, "Ethernet PHY not assigned");
return false; return false;
} }
this->set_connected_(false); this->connected_ = false;
this->started_ = false; this->started_ = false;
// No need to enable_loop() here as this is only called during shutdown/reboot // No need to enable_loop() here as this is only called during shutdown/reboot
if (this->phy_->pwrctl(this->phy_, false) != ESP_OK) { if (this->phy_->pwrctl(this->phy_, false) != ESP_OK) {

View File

@@ -104,8 +104,6 @@ class EthernetComponent : public Component {
void ksz8081_set_clock_reference_(esp_eth_mac_t *mac); void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
/// @brief Set arbitratry PHY registers from config. /// @brief Set arbitratry PHY registers from config.
void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data); void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
/// @brief Safely set connected state and ensure loop is enabled for state machine processing
void set_connected_(bool connected);
std::string use_address_; std::string use_address_;
#ifdef USE_ETHERNET_SPI #ifdef USE_ETHERNET_SPI