4242#include " esp_netif_defaults.h"
4343#include " esp_eth_phy.h"
4444
45- static ETHClass *_ethernets[3 ] = {NULL , NULL , NULL };
45+ #define NUM_SUPPORTED_ETH_PORTS 3
46+ static ETHClass *_ethernets[NUM_SUPPORTED_ETH_PORTS] = {NULL , NULL , NULL };
4647static esp_event_handler_instance_t _eth_ev_instance = NULL ;
4748
4849static void _eth_event_cb (void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
4950
5051 if (event_base == ETH_EVENT) {
5152 esp_eth_handle_t eth_handle = *((esp_eth_handle_t *)event_data);
52- for (int i = 0 ; i < 3 ; ++i) {
53+ for (int i = 0 ; i < NUM_SUPPORTED_ETH_PORTS ; ++i) {
5354 if (_ethernets[i] != NULL && _ethernets[i]->handle () == eth_handle) {
5455 _ethernets[i]->_onEthEvent (event_id, event_data);
5556 }
@@ -60,14 +61,14 @@ static void _eth_event_cb(void *arg, esp_event_base_t event_base, int32_t event_
6061// This callback needs to be aware of which interface it should match against
6162static void onEthConnected (arduino_event_id_t event, arduino_event_info_t info) {
6263 if (event == ARDUINO_EVENT_ETH_CONNECTED) {
63- uint8_t index = 3 ;
64- for (int i = 0 ; i < 3 ; ++i) {
64+ uint8_t index = NUM_SUPPORTED_ETH_PORTS ;
65+ for (int i = 0 ; i < NUM_SUPPORTED_ETH_PORTS ; ++i) {
6566 if (_ethernets[i] != NULL && _ethernets[i]->handle () == info.eth_connected ) {
6667 index = i;
6768 break ;
6869 }
6970 }
70- if (index == 3 ) {
71+ if (index == NUM_SUPPORTED_ETH_PORTS ) {
7172 log_e (" Could not find ETH interface with that handle!" );
7273 return ;
7374 }
@@ -843,8 +844,20 @@ void ETHClass::end(void) {
843844 }
844845
845846 if (_eth_ev_instance != NULL ) {
846- if (esp_event_handler_unregister (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
847- _eth_ev_instance = NULL ;
847+ bool do_not_unreg_ev_handler = false ;
848+ for (int i = 0 ; i < NUM_SUPPORTED_ETH_PORTS; ++i) {
849+ if (_ethernets[i] != NULL && _ethernets[i]->netif () != NULL && _ethernets[i]->netif () != _esp_netif) {
850+ do_not_unreg_ev_handler = true ;
851+ break ;
852+ }
853+ }
854+ if (!do_not_unreg_ev_handler) {
855+ if (esp_event_handler_unregister (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
856+ _eth_ev_instance = NULL ;
857+ log_v (" Unregistered event handler" );
858+ } else {
859+ log_e (" Failed to unregister event handler" );
860+ }
848861 }
849862 }
850863
0 commit comments