Skip to content

Commit

Permalink
Fix ethernet initialization order (esphome#504)
Browse files Browse the repository at this point in the history
ethernet interface must be started before TCP settings
  • Loading branch information
OttoWinter authored Feb 16, 2019
1 parent d2a1dc5 commit f5f663b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
This is the source of the C++ library that powers [ESPHome](https://esphome.io/)

For issues, please go to [the issue tracker](https://github.com/esphome/issues/issues).

For feature requests, please see [feature requests](https://github.com/esphome/feature-requests/issues).

> Note: Starting with 1.10.0 using ESPHome-Core directly through C++ is no longer officially
Expand Down
66 changes: 36 additions & 30 deletions src/esphome/ethernet_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,36 +133,6 @@ void EthernetComponent::start_connect_() {
return;
}

if (this->manual_ip_.has_value()) {
tcpip_adapter_ip_info_t info;
info.ip.addr = static_cast<uint32_t>(this->manual_ip_->static_ip);
info.gw.addr = static_cast<uint32_t>(this->manual_ip_->gateway);
info.netmask.addr = static_cast<uint32_t>(this->manual_ip_->subnet);

err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
ESPHL_ERROR_CHECK(err, "DHCPC stop error");
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");

if (!is_empty(this->manual_ip_->dns1)) {
ip_addr_t d;
d.type = IPADDR_TYPE_V4;
d.u_addr.ip4.addr = static_cast<uint32_t>(this->manual_ip_->dns1);
dns_setserver(0, &d);
}
if (!is_empty(this->manual_ip_->dns1)) {
ip_addr_t d;
d.type = IPADDR_TYPE_V4;
d.u_addr.ip4.addr = static_cast<uint32_t>(this->manual_ip_->dns2);
dns_setserver(1, &d);
}
} else {
err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
if (err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED) {
ESPHL_ERROR_CHECK(err, "DHCPC start error");
}
}

switch (this->type_) {
case ETHERNET_TYPE_LAN8720: {
memcpy(&this->eth_config, &phy_lan8720_default_ethernet_config, sizeof(eth_config_t));
Expand Down Expand Up @@ -201,6 +171,42 @@ void EthernetComponent::start_connect_() {

tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, get_app_name().c_str());

tcpip_adapter_ip_info_t info;
if (this->manual_ip_.has_value()) {
info.ip.addr = static_cast<uint32_t>(this->manual_ip_->static_ip);
info.gw.addr = static_cast<uint32_t>(this->manual_ip_->gateway);
info.netmask.addr = static_cast<uint32_t>(this->manual_ip_->subnet);
} else {
info.ip.addr = 0;
info.gw.addr = 0;
info.netmask.addr = 0;
}

err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
ESPHL_ERROR_CHECK(err, "DHCPC stop error");
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");

if (this->manual_ip_.has_value()) {
if (!is_empty(this->manual_ip_->dns1)) {
ip_addr_t d;
d.type = IPADDR_TYPE_V4;
d.u_addr.ip4.addr = static_cast<uint32_t>(this->manual_ip_->dns1);
dns_setserver(0, &d);
}
if (!is_empty(this->manual_ip_->dns1)) {
ip_addr_t d;
d.type = IPADDR_TYPE_V4;
d.u_addr.ip4.addr = static_cast<uint32_t>(this->manual_ip_->dns2);
dns_setserver(1, &d);
}
} else {
err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
if (err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED) {
ESPHL_ERROR_CHECK(err, "DHCPC start error");
}
}

this->connect_begin_ = millis();
this->status_set_warning();
}
Expand Down

0 comments on commit f5f663b

Please sign in to comment.