Select one of the symbols to view example projects that use it.
 
Outline
#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"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
#include "esp_console.h"
#include "bridge_console_cmd.h"
TAG
eth_event_handler(void *, esp_event_base_t, int32_t, void *)
got_ip_event_handler(void *, esp_event_base_t, int32_t, void *)
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesbridge samplemain/bridge_example_main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2022-2023 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" #include "esp_mac.h" #include "ethernet_init.h"11 includes #if CONFIG_EXAMPLE_BR_WIFI #include "esp_wifi.h" #include "nvs_flash.h"/* ... */ #endif // CONFIG_EXAMPLE_BR_WIFI #include "sdkconfig.h" #include "esp_console.h" #include "bridge_console_cmd.h" 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, "~~~~~~~~~~~"); 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 //CONFIG_EXAMPLE_BR_WIFI void app_main(void) { // Initialize Ethernet driver uint8_t eth_port_cnt = 0; esp_eth_handle_t *eth_handles; ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt)); // 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)); }{...} // Initialize TCP/IP network interface 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 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; // esp-netif flags need to be zero when port's to be bridged eth_netifs[i] = esp_netif_new(&netif_cfg); // Attach Ethernet driver to TCP/IP stack 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 flags need to be zero when port's to be bridged except for AP's ESP_NETIF_FLAG_AUTOUP esp_netif_config_wifi.ip_info = NULL; // no IP address for physical interface 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 // Create instance of esp-netif for bridge interface #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 // CONFIG_EXAMPLE_BR_DHCPS esp_netif_config_t netif_br_cfg = { .base = &esp_netif_br_config, .stack = ESP_NETIF_NETSTACK_DEFAULT_BR, }{...}; // Bridge configuration bridgeif_config_t bridgeif_config = { .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 .max_ports = br_ports // maximum number of ports the bridge can consist of }{...}; 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 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 // CONFIG_EXAMPLE_BR_WIFI // 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, &eth_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++) { // 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])); }{...} #if CONFIG_EXAMPLE_BR_WIFI ESP_ERROR_CHECK(esp_wifi_start()); #endif // --- 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, br_ports); // start console REPL ESP_ERROR_CHECK(esp_console_start_repl(repl)); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.