Skip to content

Commit 5b47272

Browse files
committed
fix(net): Don't unreg events if there are netifs
Unregister IP events only if all other netifs are stopped.
1 parent d45f35a commit 5b47272

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

libraries/Ethernet/src/ETH.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@
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};
4647
static esp_event_handler_instance_t _eth_ev_instance = NULL;
4748

4849
static 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
6162
static 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

libraries/Network/src/NetworkInterface.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,21 @@ int NetworkInterface::waitStatusBits(int bits, uint32_t timeout_ms) const {
239239

240240
void NetworkInterface::destroyNetif() {
241241
if (_ip_ev_instance != NULL) {
242-
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &_ip_event_cb);
243-
_ip_ev_instance = NULL;
242+
bool do_not_unreg_ev_handler = false;
243+
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
244+
if (_interfaces[i] != NULL && _interfaces[i] != this) {
245+
do_not_unreg_ev_handler = true;
246+
break;
247+
}
248+
}
249+
if (!do_not_unreg_ev_handler) {
250+
if (esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &_ip_event_cb) == ESP_OK) {
251+
_ip_ev_instance = NULL;
252+
log_v("Unregistered event handler");
253+
} else {
254+
log_e("Failed to unregister event handler");
255+
}
256+
}
244257
}
245258
if (_esp_netif != NULL) {
246259
esp_netif_destroy(_esp_netif);
@@ -251,6 +264,12 @@ void NetworkInterface::destroyNetif() {
251264
_interface_event_group = NULL;
252265
_initial_bits = 0;
253266
}
267+
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
268+
if (_interfaces[i] != NULL && _interfaces[i] == this) {
269+
_interfaces[i] = NULL;
270+
break;
271+
}
272+
}
254273
}
255274

256275
bool NetworkInterface::initNetif(Network_Interface_ID interface_id) {

0 commit comments

Comments
 (0)