bridge_example_main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * SPDX-FileCopyrightText: 2022 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. #include "sdkconfig.h"
  18. #include "esp_console.h"
  19. #include "bridge_console_cmd.h"
  20. static const char *TAG = "eth_bridge_example";
  21. /** Event handler for Ethernet events */
  22. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  23. int32_t event_id, void *event_data)
  24. {
  25. uint8_t mac_addr[6] = {0};
  26. /* we can get the ethernet driver handle from event data */
  27. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  28. switch (event_id) {
  29. case ETHERNET_EVENT_CONNECTED:
  30. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  31. ESP_LOGI(TAG, "Ethernet (%p) Link Up", eth_handle);
  32. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  33. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  34. break;
  35. case ETHERNET_EVENT_DISCONNECTED:
  36. ESP_LOGI(TAG, "Ethernet (%p) Link Down", eth_handle);
  37. break;
  38. case ETHERNET_EVENT_START:
  39. ESP_LOGI(TAG, "Ethernet (%p) Started", eth_handle);
  40. break;
  41. case ETHERNET_EVENT_STOP:
  42. ESP_LOGI(TAG, "Ethernet (%p) Stopped", eth_handle);
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. /** Event handler for IP_EVENT_ETH_GOT_IP */
  49. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  50. int32_t event_id, void *event_data)
  51. {
  52. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  53. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  54. ESP_LOGI(TAG, "Ethernet Got IP Address");
  55. ESP_LOGI(TAG, "~~~~~~~~~~~");
  56. ESP_LOGI(TAG, "ETHIP:" IPSTR "\r", IP2STR(&ip_info->ip));
  57. ESP_LOGI(TAG, "ETHMASK:" IPSTR "\r", IP2STR(&ip_info->netmask));
  58. ESP_LOGI(TAG, "ETHGW:" IPSTR "\r", IP2STR(&ip_info->gw));
  59. ESP_LOGI(TAG, "~~~~~~~~~~~");
  60. }
  61. void app_main(void)
  62. {
  63. // Initialize Ethernet driver
  64. uint8_t eth_port_cnt = 0;
  65. esp_eth_handle_t *eth_handles;
  66. ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt));
  67. // The same MAC address will be used for all Ethernet ports since the bridge acts as one device
  68. uint8_t common_mac_addr[ETH_ADDR_LEN];
  69. // If internal Ethernet is not supported by ESP32x SoC, Locally Administered OUI address might be returned.
  70. // Note that Locally Administered OUI range should be used only when testing on a LAN under your control!
  71. ESP_ERROR_CHECK(esp_read_mac(common_mac_addr, ESP_MAC_ETH));
  72. for (int i = 0; i < eth_port_cnt; i++) {
  73. ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_S_MAC_ADDR, common_mac_addr));
  74. }
  75. // Initialize TCP/IP network interface
  76. ESP_ERROR_CHECK(esp_netif_init());
  77. // Create default event loop that running in background
  78. ESP_ERROR_CHECK(esp_event_loop_create_default());
  79. // Create instances of esp-netif for Ethernet ports
  80. esp_netif_t **eth_netifs = calloc(eth_port_cnt, sizeof(esp_netif_t *));
  81. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  82. esp_netif_config_t netif_cfg = {
  83. .base = &esp_netif_config,
  84. .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  85. };
  86. char if_key_str[10];
  87. char if_desc_str[10];
  88. char num_str[3];
  89. for (int i = 0; i < eth_port_cnt; i++) {
  90. itoa(i, num_str, 10);
  91. strcat(strcpy(if_key_str, "ETH_"), num_str);
  92. strcat(strcpy(if_desc_str, "eth"), num_str);
  93. esp_netif_config.if_key = if_key_str;
  94. esp_netif_config.if_desc = if_desc_str;
  95. esp_netif_config.route_prio = 50 - i;
  96. esp_netif_config.flags = 0; // esp-netif flags need to be zero when port's to be bridged
  97. eth_netifs[i] = esp_netif_new(&netif_cfg);
  98. // Attach Ethernet driver to TCP/IP stack
  99. ESP_ERROR_CHECK(esp_netif_attach(eth_netifs[i], esp_eth_new_netif_glue(eth_handles[i])));
  100. }
  101. // Create instance of esp-netif for bridge interface
  102. esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
  103. esp_netif_config_t netif_br_cfg = {
  104. .base = &esp_netif_br_config,
  105. .stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
  106. };
  107. // Bridge configuration
  108. bridgeif_config_t bridgeif_config = {
  109. .max_fdb_dyn_entries = 10, // maximum number of address entries in dynamic forwarding database
  110. .max_fdb_sta_entries = 2, // maximum number of address entries in static forwarding database
  111. .max_ports = eth_port_cnt // maximum number of ports the bridge can consist of
  112. };
  113. esp_netif_br_config.bridge_info = &bridgeif_config;
  114. // Set MAC address of bridge interface the same as the Ethernet interface
  115. memcpy(esp_netif_br_config.mac, common_mac_addr, ETH_ADDR_LEN);
  116. esp_netif_t *br_netif = esp_netif_new(&netif_br_cfg);
  117. // Create new esp netif bridge glue instance
  118. esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
  119. // Add Ethernet port interfaces to that esp netif bridge glue instance
  120. for (int i = 0; i < eth_port_cnt; i++) {
  121. ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs[i]));
  122. }
  123. // Attach esp netif bridge glue instance with added ports to bridge netif
  124. ESP_ERROR_CHECK(esp_netif_attach(br_netif, netif_br_glue));
  125. // Register user defined event handers
  126. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  127. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  128. for (int i = 0; i < eth_port_cnt; i++) {
  129. // Since the MAC forwarding is performed in lwIP bridge, we need to pass all addresses through the Ethernet MACs
  130. bool promiscuous = true;
  131. esp_eth_ioctl(eth_handles[i], ETH_CMD_S_PROMISCUOUS, &promiscuous);
  132. // Start Ethernet driver state machine
  133. ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
  134. }
  135. // --- Initialize Console ---
  136. esp_console_repl_t *repl = NULL;
  137. esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
  138. repl_config.prompt = "bridge>";
  139. // install console REPL environment
  140. esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  141. ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
  142. example_register_br_config_commands(br_netif, eth_port_cnt);
  143. // start console REPL
  144. ESP_ERROR_CHECK(esp_console_start_repl(repl));
  145. }