bridge_example_main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_netif.h"
  11. #include "esp_netif_br_glue.h"
  12. #include "esp_eth.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "esp_mac.h"
  16. #include "ethernet_init.h"
  17. #if CONFIG_EXAMPLE_BR_WIFI
  18. #include "esp_wifi.h"
  19. #include "nvs_flash.h"
  20. #endif // CONFIG_EXAMPLE_BR_WIFI
  21. #include "sdkconfig.h"
  22. #include "esp_console.h"
  23. #include "bridge_console_cmd.h"
  24. static const char *TAG = "eth_bridge_example";
  25. /** Event handler for Ethernet events */
  26. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  27. int32_t event_id, void *event_data)
  28. {
  29. uint8_t mac_addr[6] = {0};
  30. /* we can get the ethernet driver handle from event data */
  31. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  32. switch (event_id) {
  33. case ETHERNET_EVENT_CONNECTED:
  34. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  35. ESP_LOGI(TAG, "Ethernet (%p) Link Up", eth_handle);
  36. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  37. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  38. break;
  39. case ETHERNET_EVENT_DISCONNECTED:
  40. ESP_LOGI(TAG, "Ethernet (%p) Link Down", eth_handle);
  41. break;
  42. case ETHERNET_EVENT_START:
  43. ESP_LOGI(TAG, "Ethernet (%p) Started", eth_handle);
  44. break;
  45. case ETHERNET_EVENT_STOP:
  46. ESP_LOGI(TAG, "Ethernet (%p) Stopped", eth_handle);
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. /** Event handler for IP_EVENT_ETH_GOT_IP */
  53. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  54. int32_t event_id, void *event_data)
  55. {
  56. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  57. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  58. ESP_LOGI(TAG, "Ethernet Got IP Address");
  59. ESP_LOGI(TAG, "~~~~~~~~~~~");
  60. ESP_LOGI(TAG, "ETHIP:" IPSTR "\r", IP2STR(&ip_info->ip));
  61. ESP_LOGI(TAG, "ETHMASK:" IPSTR "\r", IP2STR(&ip_info->netmask));
  62. ESP_LOGI(TAG, "ETHGW:" IPSTR "\r", IP2STR(&ip_info->gw));
  63. ESP_LOGI(TAG, "~~~~~~~~~~~");
  64. }
  65. #if CONFIG_EXAMPLE_BR_WIFI
  66. static void example_wifi_init(void)
  67. {
  68. esp_err_t ret = nvs_flash_init();
  69. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  70. ESP_ERROR_CHECK(nvs_flash_erase());
  71. ESP_ERROR_CHECK(nvs_flash_init());
  72. }
  73. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  74. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  75. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  76. wifi_config_t wifi_config = {
  77. .ap = {
  78. .ssid = CONFIG_EXAMPLE_BR_WIFI_SSID,
  79. .ssid_len = strlen(CONFIG_EXAMPLE_BR_WIFI_SSID),
  80. .password = CONFIG_EXAMPLE_BR_WIFI_PASSWORD,
  81. .max_connection = CONFIG_EXAMPLE_BR_MAX_STA_CONN,
  82. .authmode = WIFI_AUTH_WPA_WPA2_PSK,
  83. .channel = CONFIG_EXAMPLE_BR_WIFI_CHANNEL
  84. },
  85. };
  86. if (strlen(CONFIG_EXAMPLE_BR_WIFI_PASSWORD) == 0) {
  87. wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  88. }
  89. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  90. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
  91. }
  92. #endif //CONFIG_EXAMPLE_BR_WIFI
  93. void app_main(void)
  94. {
  95. // Initialize Ethernet driver
  96. uint8_t eth_port_cnt = 0;
  97. esp_eth_handle_t *eth_handles;
  98. ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt));
  99. // The same MAC address will be used for all Ethernet ports since the bridge acts as one device
  100. uint8_t common_mac_addr[ETH_ADDR_LEN];
  101. // If internal Ethernet is not supported by ESP32x SoC, Locally Administered OUI address might be returned.
  102. // Note that Locally Administered OUI range should be used only when testing on a LAN under your control!
  103. ESP_ERROR_CHECK(esp_read_mac(common_mac_addr, ESP_MAC_ETH));
  104. for (int i = 0; i < eth_port_cnt; i++) {
  105. ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_S_MAC_ADDR, common_mac_addr));
  106. }
  107. // Initialize TCP/IP network interface
  108. ESP_ERROR_CHECK(esp_netif_init());
  109. // Create default event loop that running in background
  110. ESP_ERROR_CHECK(esp_event_loop_create_default());
  111. // Create instances of esp-netif for Ethernet ports
  112. esp_netif_t **eth_netifs = calloc(eth_port_cnt, sizeof(esp_netif_t *));
  113. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  114. esp_netif_config_t netif_cfg = {
  115. .base = &esp_netif_config,
  116. .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  117. };
  118. char if_key_str[10];
  119. char if_desc_str[10];
  120. char num_str[3];
  121. for (int i = 0; i < eth_port_cnt; i++) {
  122. itoa(i, num_str, 10);
  123. strcat(strcpy(if_key_str, "ETH_"), num_str);
  124. strcat(strcpy(if_desc_str, "eth"), num_str);
  125. esp_netif_config.if_key = if_key_str;
  126. esp_netif_config.if_desc = if_desc_str;
  127. esp_netif_config.route_prio = 50 - i;
  128. esp_netif_config.flags = 0; // esp-netif flags need to be zero when port's to be bridged
  129. eth_netifs[i] = esp_netif_new(&netif_cfg);
  130. // Attach Ethernet driver to TCP/IP stack
  131. ESP_ERROR_CHECK(esp_netif_attach(eth_netifs[i], esp_eth_new_netif_glue(eth_handles[i])));
  132. }
  133. uint8_t br_ports = eth_port_cnt;
  134. #if CONFIG_EXAMPLE_BR_WIFI
  135. example_wifi_init();
  136. esp_netif_inherent_config_t esp_netif_config_wifi = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
  137. 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
  138. esp_netif_config_wifi.ip_info = NULL; // no IP address for physical interface
  139. esp_netif_t *wifi_netif = esp_netif_create_wifi(WIFI_IF_AP, &esp_netif_config_wifi);
  140. ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers());
  141. br_ports++;
  142. #endif
  143. // Create instance of esp-netif for bridge interface
  144. #if CONFIG_EXAMPLE_BR_DHCPS
  145. esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR_DHCPS();
  146. #else
  147. esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
  148. #endif // CONFIG_EXAMPLE_BR_DHCPS
  149. esp_netif_config_t netif_br_cfg = {
  150. .base = &esp_netif_br_config,
  151. .stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
  152. };
  153. // Bridge configuration
  154. bridgeif_config_t bridgeif_config = {
  155. .max_fdb_dyn_entries = 10, // maximum number of address entries in dynamic forwarding database
  156. .max_fdb_sta_entries = 2, // maximum number of address entries in static forwarding database
  157. .max_ports = br_ports // maximum number of ports the bridge can consist of
  158. };
  159. esp_netif_br_config.bridge_info = &bridgeif_config;
  160. // Set MAC address of bridge interface the same as the Ethernet interface
  161. memcpy(esp_netif_br_config.mac, common_mac_addr, ETH_ADDR_LEN);
  162. esp_netif_t *br_netif = esp_netif_new(&netif_br_cfg);
  163. // Create new esp netif bridge glue instance
  164. esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
  165. // Add Ethernet port interfaces to that esp netif bridge glue instance
  166. for (int i = 0; i < eth_port_cnt; i++) {
  167. ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs[i]));
  168. }
  169. #if CONFIG_EXAMPLE_BR_WIFI
  170. ESP_ERROR_CHECK(esp_netif_br_glue_add_wifi_port(netif_br_glue, wifi_netif));
  171. #endif // CONFIG_EXAMPLE_BR_WIFI
  172. // Attach esp netif bridge glue instance with added ports to bridge netif
  173. ESP_ERROR_CHECK(esp_netif_attach(br_netif, netif_br_glue));
  174. // Register user defined event handers
  175. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  176. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  177. for (int i = 0; i < eth_port_cnt; i++) {
  178. // Since the MAC forwarding is performed in lwIP bridge, we need to pass all addresses through the Ethernet MACs
  179. bool promiscuous = true;
  180. esp_eth_ioctl(eth_handles[i], ETH_CMD_S_PROMISCUOUS, &promiscuous);
  181. // Start Ethernet driver state machine
  182. ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
  183. }
  184. #if CONFIG_EXAMPLE_BR_WIFI
  185. ESP_ERROR_CHECK(esp_wifi_start());
  186. #endif
  187. // --- Initialize Console ---
  188. esp_console_repl_t *repl = NULL;
  189. esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
  190. repl_config.prompt = "bridge>";
  191. // install console REPL environment
  192. esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  193. ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
  194. example_register_br_config_commands(br_netif, br_ports);
  195. // start console REPL
  196. ESP_ERROR_CHECK(esp_console_start_repl(repl));
  197. }