42
42
#include " esp_netif_defaults.h"
43
43
#include " esp_eth_phy.h"
44
44
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 };
46
47
static esp_event_handler_instance_t _eth_ev_instance = NULL ;
47
48
48
49
static void _eth_event_cb (void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
49
50
50
51
if (event_base == ETH_EVENT) {
51
52
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) {
53
54
if (_ethernets[i] != NULL && _ethernets[i]->handle () == eth_handle) {
54
55
_ethernets[i]->_onEthEvent (event_id, event_data);
55
56
}
@@ -60,14 +61,14 @@ static void _eth_event_cb(void *arg, esp_event_base_t event_base, int32_t event_
60
61
// This callback needs to be aware of which interface it should match against
61
62
static void onEthConnected (arduino_event_id_t event, arduino_event_info_t info) {
62
63
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) {
65
66
if (_ethernets[i] != NULL && _ethernets[i]->handle () == info.eth_connected ) {
66
67
index = i;
67
68
break ;
68
69
}
69
70
}
70
- if (index == 3 ) {
71
+ if (index == NUM_SUPPORTED_ETH_PORTS ) {
71
72
log_e (" Could not find ETH interface with that handle!" );
72
73
return ;
73
74
}
@@ -843,8 +844,20 @@ void ETHClass::end(void) {
843
844
}
844
845
845
846
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
+ }
848
861
}
849
862
}
850
863
0 commit comments