Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net): Don't unregister events if there are interfaces still open #9706

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@
#include "esp_netif_defaults.h"
#include "esp_eth_phy.h"

static ETHClass *_ethernets[3] = {NULL, NULL, NULL};
#define NUM_SUPPORTED_ETH_PORTS 3
static ETHClass *_ethernets[NUM_SUPPORTED_ETH_PORTS] = {NULL, NULL, NULL};
static esp_event_handler_instance_t _eth_ev_instance = NULL;

static void _eth_event_cb(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {

if (event_base == ETH_EVENT) {
esp_eth_handle_t eth_handle = *((esp_eth_handle_t *)event_data);
for (int i = 0; i < 3; ++i) {
for (int i = 0; i < NUM_SUPPORTED_ETH_PORTS; ++i) {
if (_ethernets[i] != NULL && _ethernets[i]->handle() == eth_handle) {
_ethernets[i]->_onEthEvent(event_id, event_data);
}
Expand All @@ -60,14 +61,14 @@ static void _eth_event_cb(void *arg, esp_event_base_t event_base, int32_t event_
// This callback needs to be aware of which interface it should match against
static void onEthConnected(arduino_event_id_t event, arduino_event_info_t info) {
if (event == ARDUINO_EVENT_ETH_CONNECTED) {
uint8_t index = 3;
for (int i = 0; i < 3; ++i) {
uint8_t index = NUM_SUPPORTED_ETH_PORTS;
for (int i = 0; i < NUM_SUPPORTED_ETH_PORTS; ++i) {
if (_ethernets[i] != NULL && _ethernets[i]->handle() == info.eth_connected) {
index = i;
break;
}
}
if (index == 3) {
if (index == NUM_SUPPORTED_ETH_PORTS) {
log_e("Could not find ETH interface with that handle!");
return;
}
Expand Down Expand Up @@ -843,8 +844,20 @@ void ETHClass::end(void) {
}

if (_eth_ev_instance != NULL) {
if (esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
_eth_ev_instance = NULL;
bool do_not_unreg_ev_handler = false;
for (int i = 0; i < NUM_SUPPORTED_ETH_PORTS; ++i) {
if (_ethernets[i] != NULL && _ethernets[i]->netif() != NULL && _ethernets[i]->netif() != _esp_netif) {
do_not_unreg_ev_handler = true;
break;
}
}
if (!do_not_unreg_ev_handler) {
if (esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
_eth_ev_instance = NULL;
log_v("Unregistered event handler");
} else {
log_e("Failed to unregister event handler");
}
}
}

Expand Down
23 changes: 21 additions & 2 deletions libraries/Network/src/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,21 @@ int NetworkInterface::waitStatusBits(int bits, uint32_t timeout_ms) const {

void NetworkInterface::destroyNetif() {
if (_ip_ev_instance != NULL) {
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &_ip_event_cb);
_ip_ev_instance = NULL;
bool do_not_unreg_ev_handler = false;
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
if (_interfaces[i] != NULL && _interfaces[i] != this) {
do_not_unreg_ev_handler = true;
break;
}
}
if (!do_not_unreg_ev_handler) {
if (esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &_ip_event_cb) == ESP_OK) {
_ip_ev_instance = NULL;
log_v("Unregistered event handler");
} else {
log_e("Failed to unregister event handler");
}
}
}
if (_esp_netif != NULL) {
esp_netif_destroy(_esp_netif);
Expand All @@ -251,6 +264,12 @@ void NetworkInterface::destroyNetif() {
_interface_event_group = NULL;
_initial_bits = 0;
}
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
if (_interfaces[i] != NULL && _interfaces[i] == this) {
_interfaces[i] = NULL;
break;
}
}
}

bool NetworkInterface::initNetif(Network_Interface_ID interface_id) {
Expand Down
Loading