Browse Source

Fix ethernet component hostname handling (#2010)

Co-authored-by: Otto Winter <otto@otto-winter.com>
pull/2018/head
Sean Vig 2 months ago
committed by GitHub
parent
commit
628a94bad3
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .gitignore
  2. 155
      esphome/components/ethernet/ethernet_component.cpp
  3. 10
      esphome/components/ethernet/ethernet_component.h

3
.gitignore

@ -13,6 +13,9 @@ __pycache__/
# Intellij Idea
.idea
# Vim
*.swp
# Hide some OS X stuff
.DS_Store
.AppleDouble

155
esphome/components/ethernet/ethernet_component.cpp

@ -25,6 +25,13 @@ static const char *const TAG = "ethernet";
EthernetComponent *global_eth_component;
#define ESPHL_ERROR_CHECK(err, message) \
if (err != ESP_OK) { \
ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \
this->mark_failed(); \
return; \
}
EthernetComponent::EthernetComponent() { global_eth_component = this; }
void EthernetComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up Ethernet...");
@ -36,7 +43,38 @@ void EthernetComponent::setup() {
this->power_pin_->setup();
}
this->start_connect_();
switch (this->type_) {
case ETHERNET_TYPE_LAN8720: {
memcpy(&this->eth_config, &phy_lan8720_default_ethernet_config, sizeof(eth_config_t));
break;
}
case ETHERNET_TYPE_TLK110: {
memcpy(&this->eth_config, &phy_tlk110_default_ethernet_config, sizeof(eth_config_t));
break;
}
default: {
this->mark_failed();
return;
}
}
this->eth_config.phy_addr = static_cast<eth_phy_base_t>(this->phy_addr_);
this->eth_config.clock_mode = this->clk_mode_;
this->eth_config.gpio_config = EthernetComponent::eth_phy_config_gpio_;
this->eth_config.tcpip_input = tcpip_adapter_eth_input;
if (this->power_pin_ != nullptr) {
this->orig_power_enable_fun_ = this->eth_config.phy_power_enable;
this->eth_config.phy_power_enable = EthernetComponent::eth_phy_power_enable_;
}
tcpipInit();
esp_err_t err;
err = esp_eth_init(&this->eth_config);
ESPHL_ERROR_CHECK(err, "ETH init error");
err = esp_eth_enable();
ESPHL_ERROR_CHECK(err, "ETH enable error");
#ifdef USE_MDNS
network_setup_mdns();
@ -44,30 +82,44 @@ void EthernetComponent::setup() {
}
void EthernetComponent::loop() {
const uint32_t now = millis();
if (!this->connected_ && !this->last_connected_ && now - this->connect_begin_ > 15000) {
ESP_LOGW(TAG, "Connecting via ethernet failed! Re-connecting...");
this->start_connect_();
return;
}
if (this->connected_ == this->last_connected_)
// nothing changed
return;
if (this->connected_) {
// connection established
ESP_LOGI(TAG, "Connected via Ethernet!");
this->dump_connect_params_();
this->status_clear_warning();
} else {
// connection lost
ESP_LOGW(TAG, "Connection via Ethernet lost! Re-connecting...");
this->start_connect_();
switch (this->state_) {
case EthernetComponentState::STOPPED:
if (this->started_) {
ESP_LOGI(TAG, "Starting ethernet connection");
this->state_ = EthernetComponentState::CONNECTING;
this->start_connect_();
}
break;
case EthernetComponentState::CONNECTING:
if (!this->started_) {
ESP_LOGI(TAG, "Stopped ethernet connection");
this->state_ = EthernetComponentState::STOPPED;
} else if (this->connected_) {
// connection established
ESP_LOGI(TAG, "Connected via Ethernet!");
this->state_ = EthernetComponentState::CONNECTED;
this->dump_connect_params_();
this->status_clear_warning();
network_tick_mdns();
} else if (now - this->connect_begin_ > 15000) {
ESP_LOGW(TAG, "Connecting via ethernet failed! Re-connecting...");
this->start_connect_();
}
break;
case EthernetComponentState::CONNECTED:
if (!this->started_) {
ESP_LOGI(TAG, "Stopped ethernet connection");
this->state_ = EthernetComponentState::STOPPED;
} else if (!this->connected_) {
ESP_LOGW(TAG, "Connection via Ethernet lost! Re-connecting...");
this->state_ = EthernetComponentState::CONNECTING;
this->start_connect_();
}
break;
}
this->last_connected_ = this->connected_;
network_tick_mdns();
}
void EthernetComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Ethernet:");
@ -91,9 +143,11 @@ void EthernetComponent::on_wifi_event_(system_event_id_t event, system_event_inf
switch (event) {
case SYSTEM_EVENT_ETH_START:
event_name = "ETH started";
this->started_ = true;
break;
case SYSTEM_EVENT_ETH_STOP:
event_name = "ETH stopped";
this->started_ = false;
this->connected_ = false;
break;
case SYSTEM_EVENT_ETH_CONNECTED:
@ -114,62 +168,13 @@ void EthernetComponent::on_wifi_event_(system_event_id_t event, system_event_inf
ESP_LOGV(TAG, "[Ethernet event] %s (num=%d)", event_name, event);
}
#define ESPHL_ERROR_CHECK(err, message) \
if (err != ESP_OK) { \
ESP_LOGE(TAG, message ": %d", err); \
this->mark_failed(); \
return; \
}
void EthernetComponent::start_connect_() {
this->connect_begin_ = millis();
this->status_set_warning();
esp_err_t err;
if (this->initialized_) {
// already initialized
err = esp_eth_enable();
ESPHL_ERROR_CHECK(err, "ETH enable error");
return;
}
switch (this->type_) {
case ETHERNET_TYPE_LAN8720: {
memcpy(&this->eth_config, &phy_lan8720_default_ethernet_config, sizeof(eth_config_t));
break;
}
case ETHERNET_TYPE_TLK110: {
memcpy(&this->eth_config, &phy_tlk110_default_ethernet_config, sizeof(eth_config_t));
break;
}
default: {
this->mark_failed();
return;
}
}
this->eth_config.phy_addr = static_cast<eth_phy_base_t>(this->phy_addr_);
this->eth_config.clock_mode = this->clk_mode_;
this->eth_config.gpio_config = EthernetComponent::eth_phy_config_gpio_;
this->eth_config.tcpip_input = tcpip_adapter_eth_input;
if (this->power_pin_ != nullptr) {
this->orig_power_enable_fun_ = this->eth_config.phy_power_enable;
this->eth_config.phy_power_enable = EthernetComponent::eth_phy_power_enable_;
}
tcpipInit();
err = esp_eth_init(&this->eth_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ETH init error: %d", err);
this->mark_failed();
return;
}
this->initialized_ = true;
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, App.get_name().c_str());
err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, App.get_name().c_str());
ESPHL_ERROR_CHECK(err, "ETH set hostname error");
tcpip_adapter_ip_info_t info;
if (this->manual_ip_.has_value()) {
@ -220,7 +225,7 @@ void EthernetComponent::eth_phy_power_enable_(bool enable) {
delay(1);
global_eth_component->orig_power_enable_fun_(enable);
}
bool EthernetComponent::is_connected() { return this->connected_ && this->last_connected_; }
bool EthernetComponent::is_connected() { return this->state_ == EthernetComponentState::CONNECTED; }
void EthernetComponent::dump_connect_params_() {
tcpip_adapter_ip_info_t ip;
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);

10
esphome/components/ethernet/ethernet_component.h

@ -26,6 +26,12 @@ struct ManualIP {
IPAddress dns2; ///< The second DNS server. 0.0.0.0 for default.
};
enum class EthernetComponentState {
STOPPED,
CONNECTING,
CONNECTED,
};
class EthernetComponent : public Component {
public:
EthernetComponent();
@ -65,9 +71,9 @@ class EthernetComponent : public Component {
eth_clock_mode_t clk_mode_{ETH_CLOCK_GPIO0_IN};
optional<ManualIP> manual_ip_{};
bool initialized_{false};
bool started_{false};
bool connected_{false};
bool last_connected_{false};
EthernetComponentState state_{EthernetComponentState::STOPPED};
uint32_t connect_begin_;
eth_config_t eth_config;
eth_phy_power_enable_func orig_power_enable_fun_;

Loading…
Cancel
Save