1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
21
22
23
24
25
26
27
28
29
30
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
80
81
82
83
84
94
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
117
118
119
120
121
122
123
124
125
126
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
157
158
159
160
161
162
163
164
165
169
170
175
176
177
178
179
180
181
182
183
186
187
188
189
190
191
192
193
194
195
196
197
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* ... */
#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"
#include "esp_mac.h"
#include "ethernet_init.h"11 includes
#if CONFIG_EXAMPLE_BR_WIFI
#include "esp_wifi.h"
#include "nvs_flash.h"/* ... */
#endif
#include "sdkconfig.h"
#include "esp_console.h"
#include "bridge_console_cmd.h"
static const char *TAG = "eth_bridge_example";
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};
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;...
}{...}
}{ ... }
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, "~~~~~~~~~~~");
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));
ESP_LOGI(TAG, "~~~~~~~~~~~");
}{ ... }
#if CONFIG_EXAMPLE_BR_WIFI
static void example_wifi_init(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());
ESP_ERROR_CHECK(nvs_flash_init());
}{...}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = {
.ap = {
.ssid = CONFIG_EXAMPLE_BR_WIFI_SSID,
.ssid_len = strlen(CONFIG_EXAMPLE_BR_WIFI_SSID),
.password = CONFIG_EXAMPLE_BR_WIFI_PASSWORD,
.max_connection = CONFIG_EXAMPLE_BR_MAX_STA_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.channel = CONFIG_EXAMPLE_BR_WIFI_CHANNEL
}{...},
}{...};
if (strlen(CONFIG_EXAMPLE_BR_WIFI_PASSWORD) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}{...}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
}{...}
/* ... */#endif
void app_main(void)
{
uint8_t eth_port_cnt = 0;
esp_eth_handle_t *eth_handles;
ESP_ERROR_CHECK(example_eth_init(ð_handles, ð_port_cnt));
uint8_t common_mac_addr[ETH_ADDR_LEN];
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));
}{...}
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t **eth_netifs = calloc(eth_port_cnt, sizeof(esp_netif_t *));
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];
for (int i = 0; i < eth_port_cnt; i++) {
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;
esp_netif_config.flags = 0;
eth_netifs[i] = esp_netif_new(&netif_cfg);
ESP_ERROR_CHECK(esp_netif_attach(eth_netifs[i], esp_eth_new_netif_glue(eth_handles[i])));
}{...}
uint8_t br_ports = eth_port_cnt;
#if CONFIG_EXAMPLE_BR_WIFI
example_wifi_init();
esp_netif_inherent_config_t esp_netif_config_wifi = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
esp_netif_config_wifi.flags = ESP_NETIF_FLAG_AUTOUP;
esp_netif_config_wifi.ip_info = NULL;
esp_netif_t *wifi_netif = esp_netif_create_wifi(WIFI_IF_AP, &esp_netif_config_wifi);
ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers());
br_ports++;/* ... */
#endif
#if CONFIG_EXAMPLE_BR_DHCPS
esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR_DHCPS();
#else
esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
#endif
esp_netif_config_t netif_br_cfg = {
.base = &esp_netif_br_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
}{...};
bridgeif_config_t bridgeif_config = {
.max_fdb_dyn_entries = 10,
.max_fdb_sta_entries = 2,
.max_ports = br_ports
}{...};
esp_netif_br_config.bridge_info = &bridgeif_config;
memcpy(esp_netif_br_config.mac, common_mac_addr, ETH_ADDR_LEN);
esp_netif_t *br_netif = esp_netif_new(&netif_br_cfg);
esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
for (int i = 0; i < eth_port_cnt; i++) {
ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs[i]));
}{...}
#if CONFIG_EXAMPLE_BR_WIFI
ESP_ERROR_CHECK(esp_netif_br_glue_add_wifi_port(netif_br_glue, wifi_netif));
#endif
ESP_ERROR_CHECK(esp_netif_attach(br_netif, netif_br_glue));
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));
for (int i = 0; i < eth_port_cnt; i++) {
bool promiscuous = true;
esp_eth_ioctl(eth_handles[i], ETH_CMD_S_PROMISCUOUS, &promiscuous);
ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
}{...}
#if CONFIG_EXAMPLE_BR_WIFI
ESP_ERROR_CHECK(esp_wifi_start());
#endif
esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
repl_config.prompt = "bridge>";
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, br_ports);
ESP_ERROR_CHECK(esp_console_start_repl(repl));
}{ ... }