ESP32: Wifi to Ethernet bridge with DHCP server

iMad_idf
Posts: 3
Joined: Sun Aug 02, 2020 9:52 pm

ESP32: Wifi to Ethernet bridge with DHCP server

Postby iMad_idf » Sun Aug 02, 2020 10:04 pm

Hello guys,

I am quite advanced by now with ESP-IDF, but I run into the issue I really need some suggestion on. My application is as follows:
1. On Ethernet, there's a client that needs IP
2. On WifiAP, there's also a client who needs an IP
3. Both need to be in single subnet, and transparently process traffic.

I tried everything possible with ESP eth2ap example. I've created flows to enable/disable transparent transmission based on events. I tried to have DHCP server enabled only on WIFI, and setting zero IP config to ETHERNET, so it would become kind of a "switch port" to the WIFI. But I can't get the thing work the way I need.. Any help is highly appreciated.
I do realize that I can't perform MAC level transparency, while having level 3 protocol operable, hence I am here looking for suggestions..

Here's the code I have at the moment:

Code: Select all

#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_eth.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "esp_private/wifi.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

static const char *TAG = "eth_example";
static esp_eth_handle_t s_eth_handle = NULL;
static esp_eth_handle_t v_eth_handle = NULL;
static xQueueHandle flow_control_queue = NULL;
static bool s_sta_is_connected = false;
static bool s_ethernet_is_connected = false;
static esp_netif_t *beth_netif = NULL;
static esp_netif_t *ceth_netif = NULL;
static uint8_t s_eth_mac[6];

static void initialize_wifi(void);
static void initialize_ethernet(void);

#define FLOW_CONTROL_QUEUE_TIMEOUT_MS (100)
#define FLOW_CONTROL_QUEUE_LENGTH (40)
#define FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS (100)

typedef struct {
    void *packet;
    uint16_t length;
} flow_control_msg_t;

static esp_err_t pkt_wifi2eth(void *buffer, uint16_t len, void *eb)
{
    if (s_ethernet_is_connected) {
        if (esp_eth_transmit(v_eth_handle, buffer, len) != ESP_OK) {
            ESP_LOGE(TAG, "Ethernet send packet failed");
        }
    }
    esp_wifi_internal_free_rx_buffer(eb);
    return ESP_OK;
}

static esp_err_t pkt_eth2wifi(esp_eth_handle_t v_eth_handle, uint8_t *buffer, uint32_t len, void* priv)
{
    esp_err_t ret = ESP_OK;
    flow_control_msg_t msg = {
        .packet = buffer,
        .length = len
    };
    if (xQueueSend(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) != pdTRUE) {
        ESP_LOGE(TAG, "send flow control message failed or timeout");
        free(buffer);
        ret = ESP_FAIL;
    }
    return ret;
}

static void eth2wifi_flow_control_task(void *args)
{
    flow_control_msg_t msg;
    int res = 0;
    uint32_t timeout = 0;
    while (1) {
        if (xQueueReceive(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) == pdTRUE) {
            timeout = 0;
            if (s_sta_is_connected && msg.length) {
                do {
                    vTaskDelay(pdMS_TO_TICKS(timeout));
                    timeout += 2;
                    res = esp_wifi_internal_tx(ESP_IF_WIFI_AP, msg.packet, msg.length);
                } while (res && timeout < FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS);
                if (res != ESP_OK) {
                    ESP_LOGE(TAG, "WiFi send packet failed: %d", res);
                } else {
                	ESP_LOGE(TAG, "Sent data from ETH to WIFI %i", msg.length);
                }
            }
            free(msg.packet);
        }
    }
    vTaskDelete(NULL);
}

static void eth_event_handler(void *arg, esp_event_base_t event_base,
                              int32_t event_id, void *event_data)
{
    switch (event_id) {
    case ETHERNET_EVENT_CONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Up");
        s_ethernet_is_connected = true;
        esp_eth_ioctl(v_eth_handle, ETH_CMD_G_MAC_ADDR, s_eth_mac);
        esp_wifi_set_mac(WIFI_IF_AP, s_eth_mac);
        break;
    case ETHERNET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Down");
        s_ethernet_is_connected = false;
        break;
    case ETHERNET_EVENT_START:
        ESP_LOGI(TAG, "Ethernet Started");
        break;
    case ETHERNET_EVENT_STOP:
        ESP_LOGI(TAG, "Ethernet Stopped");
        break;
    case IP_EVENT_ETH_GOT_IP:
    	ESP_LOGI(TAG, "Got ENET Client Connected.");
    	break;
    default:
        break;
    }
}

static void wifi_event_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    switch (event_id) {
    case WIFI_EVENT_AP_STACONNECTED:
        ESP_LOGI(TAG, "Wi-Fi AP got a station connected");
        s_sta_is_connected = true;
        vTaskDelay(500);
        initialize_ethernet();
        vTaskDelay(500);
        esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, pkt_wifi2eth);
        break;
    case WIFI_EVENT_AP_STADISCONNECTED:
        ESP_LOGI(TAG, "Wi-Fi AP got a station disconnected");
        s_sta_is_connected = false;
        esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL);
        break;
    default:
        break;
    }
}

static void initialize_ethernet(void) {
	
    const esp_netif_inherent_config_t eth_behav_cfg = {
            .lost_ip_event = 0,
			.flags = ESP_NETIF_FLAG_AUTOUP,
            .if_key = "ETH_DHCPS",
            .if_desc = "eth1d",
            .route_prio = 10
    };

    esp_netif_config_t eth_as_dhcps_cfg = { .base = &eth_behav_cfg, .stack =  ESP_NETIF_NETSTACK_DEFAULT_ETH};
    ceth_netif = esp_netif_new(&eth_as_dhcps_cfg);
    
    esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, esp_netif_action_start, ceth_netif);
    esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_STOP, esp_netif_action_stop, ceth_netif);
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &eth_event_handler, NULL));
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
    phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
    phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
    mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
    mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
    esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
    config.stack_input = pkt_eth2wifi;
    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &v_eth_handle));
    ESP_ERROR_CHECK(esp_netif_attach(ceth_netif, esp_eth_new_netif_glue(v_eth_handle)));
    esp_eth_ioctl(v_eth_handle, ETH_CMD_S_PROMISCUOUS, (void *)true);
    ESP_ERROR_CHECK(esp_eth_start(v_eth_handle));
}


static void initialize_wifi(void)
{
	const esp_netif_ip_info_t wifi_ap_ip = {
	        .ip = { .addr = esp_ip4addr_aton("192.168.4.1") },
	        .gw = { .addr = esp_ip4addr_aton("192.168.4.1") },
	        .netmask = { .addr = esp_ip4addr_aton("255.255.255.0") },

	};
    const esp_netif_inherent_config_t wifi_behav_cfg = {
            .get_ip_event =   IP_EVENT_ETH_GOT_IP,
            .lost_ip_event = 0,
            .flags = ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP,
            .ip_info =  (esp_netif_ip_info_t*) &wifi_ap_ip,
            .if_key = "WIFI_DHCPS",
            .if_desc = "wf1",
            .route_prio = 50
    };
    esp_netif_config_t wf_as_dhcps_cfg = { .base = &wifi_behav_cfg, .stack =  ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP};
	beth_netif = esp_netif_new(&wf_as_dhcps_cfg);
	
    esp_netif_create_default_wifi_ap();
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    wifi_config_t wifi_config = {
        .ap = {
            .ssid = CONFIG_EXAMPLE_WIFI_SSID,
            .ssid_len = strlen(CONFIG_EXAMPLE_WIFI_SSID),
            .password = CONFIG_EXAMPLE_WIFI_PASSWORD,
            .max_connection = CONFIG_EXAMPLE_MAX_STA_CONN,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK
        },
    };
    if (strlen(CONFIG_EXAMPLE_WIFI_PASSWORD) == 0) {
        wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    }
    ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(beth_netif));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());

    ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
    		CONFIG_EXAMPLE_WIFI_SSID, CONFIG_EXAMPLE_WIFI_PASSWORD, 1);
}

static esp_err_t initialize_flow_control(void)
{
    flow_control_queue = xQueueCreate(FLOW_CONTROL_QUEUE_LENGTH, sizeof(flow_control_msg_t));
    if (!flow_control_queue) {
        ESP_LOGE(TAG, "create flow control queue failed");
        return ESP_FAIL;
    }
    BaseType_t ret = xTaskCreate(eth2wifi_flow_control_task, "flow_ctl", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL);
    if (ret != pdTRUE) {
        ESP_LOGE(TAG, "create flow control task failed");
        return ESP_FAIL;
    }
    return ESP_OK;
}

void app_main(void)
{
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    ESP_ERROR_CHECK(initialize_flow_control());
    ESP_ERROR_CHECK(esp_netif_init());
    
    initialize_wifi();
}

theGtknerd
Posts: 6
Joined: Tue May 25, 2021 1:48 pm

Re: ESP32: Wifi to Ethernet bridge with DHCP server

Postby theGtknerd » Tue Aug 06, 2024 11:13 am

Hi, any more progress on this?

I am also looking to create a minimal router that only transmits packets from ethernet to WiFi as fast as possible.

ESP_ondrej
Posts: 207
Joined: Fri May 07, 2021 10:35 am

Re: ESP32: Wifi to Ethernet bridge with DHCP server

Postby ESP_ondrej » Thu Aug 08, 2024 10:26 am

Could you please try https://github.com/espressif/esp-idf/tr ... ork/bridge if it meets your use case?

Who is online

Users browsing this forum: No registered users and 86 guests