trying to get ethernet to serve as DHCP server
Posted: Tue Oct 31, 2017 4:12 pm
Hi guys,
Many attempts trying to get ethernet to work as DHCP server (along with wifi as DHCP server AP as well). I think it's kinda odd but it's our requirement. Doesn't seem to work. Ethernet always get assigned an IP. I used tcpip_adapter DHCP calls in event handlers to set it up. Code is as follows.
Any ideas will be much much appreciated!
Noted that tcpip_adapter_dhcps_start() doesn't support TCPIP_ADAPTER_IF_ETH so I commented it out.
Many attempts trying to get ethernet to work as DHCP server (along with wifi as DHCP server AP as well). I think it's kinda odd but it's our requirement. Doesn't seem to work. Ethernet always get assigned an IP. I used tcpip_adapter DHCP calls in event handlers to set it up. Code is as follows.
Any ideas will be much much appreciated!
Code: Select all
esp_err_t IRAM_ATTR user_event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_ETH_START:
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
ESP_LOGD(tag, "stopping dhcp client");
tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_ETH);
ESP_LOGD(tag, "dhcp server start successfully");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
ESP_LOGD(tag, "stopping dhcp client");
tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_ETH);
ESP_LOGD(tag, "dhcp server start successfully");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
eth_dump();
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
eth_dump();
break;
case SYSTEM_EVENT_STA_START:
break;
case SYSTEM_EVENT_STA_GOT_IP:
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
break;
default:
break;
}
return ESP_OK;
}
Noted that tcpip_adapter_dhcps_start() doesn't support TCPIP_ADAPTER_IF_ETH so I commented it out.
Code: Select all
esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if)
{
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcps_start_api);
/* only support ap now */
// if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
// ESP_LOGD(TAG, "dhcp server invalid if=%d", tcpip_if);
// return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
// }
if (dhcps_status != TCPIP_ADAPTER_DHCP_STARTED) {
struct netif *p_netif = esp_netif[tcpip_if];
if (p_netif != NULL && netif_is_up(p_netif)) {
tcpip_adapter_ip_info_t default_ip;
tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &default_ip);
dhcps_start(p_netif, default_ip.ip);
dhcps_status = TCPIP_ADAPTER_DHCP_STARTED;
ESP_LOGD(TAG, "dhcp server start successfully");
return ESP_OK;
} else {
ESP_LOGD(TAG, "dhcp server re init");
dhcps_status = TCPIP_ADAPTER_DHCP_INIT;
return ESP_OK;
}
}
ESP_LOGD(TAG, "dhcp server already start");
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED;
}