#include "Eth.hpp" #include "esp_log.h" #include "esp_netif.h" #include "esp_eth.h" #include "ethernet_init.h" #include "lwip/ip4_addr.h" static const char* TAG = "Ethernet"; Eth& Eth::instance() { static Eth instance; return instance; } /** Event handler for Ethernet events */ void Eth::eventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { uint8_t mac_addr[6] = {0}; /* we can get the ethernet driver handle from event data */ esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data; switch (event_id) { case ETHERNET_EVENT_CONNECTED: esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr); ESP_LOGI(TAG, "Ethernet Link Up"); ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); break; case ETHERNET_EVENT_DISCONNECTED: ESP_LOGI(TAG, "Ethernet Link Down"); break; case ETHERNET_EVENT_START: ESP_LOGI(TAG, "Ethernet Started"); break; case ETHERNET_EVENT_STOP: ESP_LOGI(TAG, "Ethernet Stopped"); break; default: break; } } /** Event handler for IP_EVENT_ETH_GOT_IP */ void Eth::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; const esp_netif_ip_info_t *ip_info = &event->ip_info; ESP_LOGI(TAG, "Ethernet Got IP Address"); ESP_LOGI(TAG, "~~~~~~~~~~~"); ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); ESP_LOGI(TAG, "~~~~~~~~~~~"); } void Eth::init() { if (initialized_) { return; } ESP_LOGI(TAG, "Initializing Ethernet"); ethEventGroup_ = xEventGroupCreate(); uint8_t eth_port_cnt = 0; esp_eth_handle_t *eth_handles; ESP_ERROR_CHECK(example_eth_init(ð_handles, ð_port_cnt)); // Initialize TCP/IP network interface aka the esp-netif (should be called only once in application) ESP_ERROR_CHECK(esp_netif_init()); // Create default event loop that running in background ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_t *eth_netifs[eth_port_cnt]; esp_eth_netif_glue_handle_t eth_netif_glues[eth_port_cnt]; // Create instance(s) of esp-netif for Ethernet(s) if (eth_port_cnt == 1) { // Use ESP_NETIF_DEFAULT_ETH when just one Ethernet interface is used and you don't need to modify // default esp-netif configuration parameters. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); eth_netifs[0] = esp_netif_new(&cfg); netif_ = eth_netifs[0]; eth_netif_glues[0] = esp_eth_new_netif_glue(eth_handles[0]); // Attach Ethernet driver to TCP/IP stack ESP_ERROR_CHECK(esp_netif_attach(eth_netifs[0], eth_netif_glues[0])); } ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eventHandler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); // Start Ethernet driver state machine for (int i = 0; i < eth_port_cnt; i++) { ESP_ERROR_CHECK(esp_eth_start(eth_handles[i])); } initialized_ = true; } void Eth::applyStaticIp(const StaticIpConfig& cfg) { if (!netif_) { ESP_LOGW(TAG, "applyStaticIp: no netif"); return; } esp_netif_dhcpc_stop(netif_); esp_netif_ip_info_t ip_info = {}; IP4_ADDR(&ip_info.ip, cfg.ip[0], cfg.ip[1], cfg.ip[2], cfg.ip[3]); IP4_ADDR(&ip_info.netmask, cfg.subnet[0], cfg.subnet[1], cfg.subnet[2], cfg.subnet[3]); IP4_ADDR(&ip_info.gw, cfg.gateway[0], cfg.gateway[1], cfg.gateway[2], cfg.gateway[3]); esp_err_t err = esp_netif_set_ip_info(netif_, &ip_info); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to set static IP: %s", esp_err_to_name(err)); esp_netif_dhcpc_start(netif_); return; } esp_netif_dns_info_t dns_info = {}; IP4_ADDR(&dns_info.ip.u_addr.ip4, cfg.dns[0], cfg.dns[1], cfg.dns[2], cfg.dns[3]); dns_info.ip.type = ESP_IPADDR_TYPE_V4; esp_netif_set_dns_info(netif_, ESP_NETIF_DNS_MAIN, &dns_info); ESP_LOGI(TAG, "Static IP set: %d.%d.%d.%d", cfg.ip[0], cfg.ip[1], cfg.ip[2], cfg.ip[3]); } std::string Eth::getIPAddress() { if (!netif_) return ""; esp_netif_ip_info_t ip_info; if (esp_netif_get_ip_info(netif_, &ip_info) == ESP_OK) { char buf[16]; snprintf(buf, sizeof(buf), IPSTR, IP2STR(&ip_info.ip)); return buf; } return ""; }