bridge_example_main.c 6.0 KB

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