2022-05-06 10:09:24 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "esp_netif_br_glue.h"
|
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
2022-07-22 11:13:04 -04:00
|
|
|
#include "esp_mac.h"
|
|
|
|
#include "ethernet_init.h"
|
2022-05-06 10:09:24 -04:00
|
|
|
#include "sdkconfig.h"
|
2022-11-08 09:16:21 -05:00
|
|
|
#include "esp_console.h"
|
|
|
|
#include "bridge_console_cmd.h"
|
2022-05-06 10:09:24 -04:00
|
|
|
|
|
|
|
static const char *TAG = "eth_bridge_example";
|
|
|
|
|
|
|
|
/** Event handler for Ethernet events */
|
|
|
|
static void eth_event_handler(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 (%p) Link Up", eth_handle);
|
|
|
|
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 (%p) Link Down", eth_handle);
|
|
|
|
break;
|
|
|
|
case ETHERNET_EVENT_START:
|
|
|
|
ESP_LOGI(TAG, "Ethernet (%p) Started", eth_handle);
|
|
|
|
break;
|
|
|
|
case ETHERNET_EVENT_STOP:
|
|
|
|
ESP_LOGI(TAG, "Ethernet (%p) Stopped", eth_handle);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Event handler for IP_EVENT_ETH_GOT_IP */
|
|
|
|
static void 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, "~~~~~~~~~~~");
|
2022-11-08 09:16:21 -05:00
|
|
|
ESP_LOGI(TAG, "ETHIP:" IPSTR "\r", IP2STR(&ip_info->ip));
|
|
|
|
ESP_LOGI(TAG, "ETHMASK:" IPSTR "\r", IP2STR(&ip_info->netmask));
|
|
|
|
ESP_LOGI(TAG, "ETHGW:" IPSTR "\r", IP2STR(&ip_info->gw));
|
2022-05-06 10:09:24 -04:00
|
|
|
ESP_LOGI(TAG, "~~~~~~~~~~~");
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
2022-07-22 11:13:04 -04:00
|
|
|
// Initialize Ethernet driver
|
|
|
|
uint8_t eth_port_cnt = 0;
|
|
|
|
esp_eth_handle_t *eth_handles;
|
|
|
|
ESP_ERROR_CHECK(example_eth_init(ð_handles, ð_port_cnt));
|
2022-05-06 10:09:24 -04:00
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
// The same MAC address will be used for all Ethernet ports since the bridge acts as one device
|
|
|
|
uint8_t common_mac_addr[ETH_ADDR_LEN];
|
|
|
|
// If internal Ethernet is not supported by ESP32x SoC, Locally Administered OUI address might be returned.
|
|
|
|
// Note that Locally Administered OUI range should be used only when testing on a LAN under your control!
|
|
|
|
ESP_ERROR_CHECK(esp_read_mac(common_mac_addr, ESP_MAC_ETH));
|
|
|
|
for (int i = 0; i < eth_port_cnt; i++) {
|
|
|
|
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_S_MAC_ADDR, common_mac_addr));
|
|
|
|
}
|
2022-05-06 10:09:24 -04:00
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
// Initialize TCP/IP network interface
|
2022-05-06 10:09:24 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
// Create default event loop that running in background
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
// Create instances of esp-netif for Ethernet ports
|
2022-07-22 11:13:04 -04:00
|
|
|
esp_netif_t **eth_netifs = calloc(eth_port_cnt, sizeof(esp_netif_t *));
|
2022-05-06 10:09:24 -04:00
|
|
|
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
|
|
|
|
esp_netif_config_t netif_cfg = {
|
|
|
|
.base = &esp_netif_config,
|
|
|
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
|
|
|
|
};
|
|
|
|
char if_key_str[10];
|
|
|
|
char if_desc_str[10];
|
|
|
|
char num_str[3];
|
2022-07-22 11:13:04 -04:00
|
|
|
for (int i = 0; i < eth_port_cnt; i++) {
|
2022-05-06 10:09:24 -04:00
|
|
|
itoa(i, num_str, 10);
|
|
|
|
strcat(strcpy(if_key_str, "ETH_"), num_str);
|
|
|
|
strcat(strcpy(if_desc_str, "eth"), num_str);
|
|
|
|
esp_netif_config.if_key = if_key_str;
|
|
|
|
esp_netif_config.if_desc = if_desc_str;
|
|
|
|
esp_netif_config.route_prio = 50 - i;
|
2022-07-22 11:13:04 -04:00
|
|
|
esp_netif_config.flags = 0; // esp-netif flags need to be zero when port's to be bridged
|
2022-05-06 10:09:24 -04:00
|
|
|
|
|
|
|
eth_netifs[i] = esp_netif_new(&netif_cfg);
|
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
// Attach Ethernet driver to TCP/IP stack
|
2022-05-06 10:09:24 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(eth_netifs[i], esp_eth_new_netif_glue(eth_handles[i])));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create instance of esp-netif for bridge interface
|
|
|
|
esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
|
|
|
|
esp_netif_config_t netif_br_cfg = {
|
|
|
|
.base = &esp_netif_br_config,
|
|
|
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
|
|
|
|
};
|
2022-06-09 09:47:57 -04:00
|
|
|
// Bridge configuration
|
2022-05-06 10:09:24 -04:00
|
|
|
bridgeif_config_t bridgeif_config = {
|
2022-06-09 09:47:57 -04:00
|
|
|
.max_fdb_dyn_entries = 10, // maximum number of address entries in dynamic forwarding database
|
|
|
|
.max_fdb_sta_entries = 2, // maximum number of address entries in static forwarding database
|
2022-07-22 11:13:04 -04:00
|
|
|
.max_ports = eth_port_cnt // maximum number of ports the bridge can consist of
|
2022-05-06 10:09:24 -04:00
|
|
|
};
|
|
|
|
esp_netif_br_config.bridge_info = &bridgeif_config;
|
|
|
|
// Set MAC address of bridge interface the same as the Ethernet interface
|
|
|
|
memcpy(esp_netif_br_config.mac, common_mac_addr, ETH_ADDR_LEN);
|
|
|
|
esp_netif_t *br_netif = esp_netif_new(&netif_br_cfg);
|
|
|
|
|
|
|
|
// Create new esp netif bridge glue instance
|
|
|
|
esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
|
|
|
|
// Add Ethernet port interfaces to that esp netif bridge glue instance
|
2022-07-22 11:13:04 -04:00
|
|
|
for (int i = 0; i < eth_port_cnt; i++) {
|
2022-05-06 10:09:24 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs[i]));
|
|
|
|
}
|
|
|
|
// Attach esp netif bridge glue instance with added ports to bridge netif
|
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(br_netif, netif_br_glue));
|
|
|
|
|
|
|
|
// Register user defined event handers
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
|
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
for (int i = 0; i < eth_port_cnt; i++) {
|
2022-05-06 10:09:24 -04:00
|
|
|
// Since the MAC forwarding is performed in lwIP bridge, we need to pass all addresses through the Ethernet MACs
|
|
|
|
bool promiscuous = true;
|
|
|
|
esp_eth_ioctl(eth_handles[i], ETH_CMD_S_PROMISCUOUS, &promiscuous);
|
|
|
|
// Start Ethernet driver state machine
|
|
|
|
ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
|
|
|
|
}
|
2022-11-08 09:16:21 -05:00
|
|
|
|
|
|
|
// --- Initialize Console ---
|
|
|
|
esp_console_repl_t *repl = NULL;
|
|
|
|
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
|
|
|
|
repl_config.prompt = "bridge>";
|
|
|
|
|
|
|
|
// install console REPL environment
|
|
|
|
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
|
|
|
|
example_register_br_config_commands(br_netif, eth_port_cnt);
|
|
|
|
// start console REPL
|
|
|
|
ESP_ERROR_CHECK(esp_console_start_repl(repl));
|
2022-05-06 10:09:24 -04:00
|
|
|
}
|