Skip to content

Commit a4ee652

Browse files
authored
fix(net): Don't unregister events if there are interfaces still open (espressif#9706)
* fix(net): Don't unreg events if there are netifs Unregister IP events only if all other netifs are stopped. * fix(eth): Delete mac and phy on end * fix(net): Update pin naming and log levels
1 parent 10a48f5 commit a4ee652

File tree

6 files changed

+84
-30
lines changed

6 files changed

+84
-30
lines changed

Diff for: libraries/Ethernet/src/ETH.cpp

+55-24
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
}
@@ -118,7 +119,7 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
118119
}
119120

120121
ETHClass::ETHClass(uint8_t eth_index)
121-
: _eth_handle(NULL), _eth_index(eth_index), _phy_type(ETH_PHY_MAX), _glue_handle(NULL)
122+
: _eth_handle(NULL), _eth_index(eth_index), _phy_type(ETH_PHY_MAX), _glue_handle(NULL), _mac(NULL), _phy(NULL)
122123
#if ETH_SPI_SUPPORTS_CUSTOM
123124
,
124125
_spi(NULL)
@@ -534,7 +535,11 @@ bool ETHClass::beginSPI(
534535
if (_spi != NULL) {
535536
pinMode(_pin_cs, OUTPUT);
536537
digitalWrite(_pin_cs, HIGH);
537-
perimanSetPinBusExtraType(_pin_cs, "ETH_CS");
538+
char cs_num_str[3];
539+
itoa(_eth_index, cs_num_str, 10);
540+
strcat(strcpy(_cs_str, "ETH_CS["), cs_num_str);
541+
strcat(_cs_str, "]");
542+
perimanSetPinBusExtraType(_pin_cs, _cs_str);
538543
}
539544
#endif
540545

@@ -586,8 +591,6 @@ bool ETHClass::beginSPI(
586591
spi_devcfg.spics_io_num = _pin_cs;
587592
spi_devcfg.queue_size = 20;
588593

589-
esp_eth_mac_t *mac = NULL;
590-
esp_eth_phy_t *phy = NULL;
591594
#if CONFIG_ETH_SPI_ETHERNET_W5500
592595
if (type == ETH_PHY_W5500) {
593596
eth_w5500_config_t mac_config = ETH_W5500_DEFAULT_CONFIG(spi_host, &spi_devcfg);
@@ -606,8 +609,8 @@ bool ETHClass::beginSPI(
606609
mac_config.custom_spi_driver.write = _eth_spi_write;
607610
}
608611
#endif
609-
mac = esp_eth_mac_new_w5500(&mac_config, &eth_mac_config);
610-
phy = esp_eth_phy_new_w5500(&phy_config);
612+
_mac = esp_eth_mac_new_w5500(&mac_config, &eth_mac_config);
613+
_phy = esp_eth_phy_new_w5500(&phy_config);
611614
} else
612615
#endif
613616
#if CONFIG_ETH_SPI_ETHERNET_DM9051
@@ -623,8 +626,8 @@ bool ETHClass::beginSPI(
623626
mac_config.custom_spi_driver.write = _eth_spi_write;
624627
}
625628
#endif
626-
mac = esp_eth_mac_new_dm9051(&mac_config, &eth_mac_config);
627-
phy = esp_eth_phy_new_dm9051(&phy_config);
629+
_mac = esp_eth_mac_new_dm9051(&mac_config, &eth_mac_config);
630+
_phy = esp_eth_phy_new_dm9051(&phy_config);
628631
} else
629632
#endif
630633
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
@@ -640,8 +643,8 @@ bool ETHClass::beginSPI(
640643
mac_config.custom_spi_driver.write = _eth_spi_write;
641644
}
642645
#endif
643-
mac = esp_eth_mac_new_ksz8851snl(&mac_config, &eth_mac_config);
644-
phy = esp_eth_phy_new_ksz8851snl(&phy_config);
646+
_mac = esp_eth_mac_new_ksz8851snl(&mac_config, &eth_mac_config);
647+
_phy = esp_eth_phy_new_ksz8851snl(&phy_config);
645648
} else
646649
#endif
647650
{
@@ -650,7 +653,7 @@ bool ETHClass::beginSPI(
650653
}
651654

652655
// Init Ethernet driver to default and install it
653-
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
656+
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(_mac, _phy);
654657
ret = esp_eth_driver_install(&eth_config, &_eth_handle);
655658
if (ret != ESP_OK) {
656659
log_e("SPI Ethernet driver install failed: %d", ret);
@@ -743,40 +746,46 @@ bool ETHClass::beginSPI(
743746
#if ETH_SPI_SUPPORTS_CUSTOM
744747
if (_spi == NULL) {
745748
#endif
746-
if (!perimanSetPinBus(_pin_cs, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), -1, -1)) {
749+
if (!perimanSetPinBus(_pin_cs, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), _eth_index, -1)) {
747750
goto err;
748751
}
752+
perimanSetPinBusExtraType(_pin_cs, "ETH_SPI_CS");
749753
#if ETH_SPI_SUPPORTS_CUSTOM
750754
}
751755
#endif
752756
#if ETH_SPI_SUPPORTS_NO_IRQ
753757
if (_pin_irq != -1) {
754758
#endif
755-
if (!perimanSetPinBus(_pin_irq, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), -1, -1)) {
759+
if (!perimanSetPinBus(_pin_irq, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), _eth_index, -1)) {
756760
goto err;
757761
}
762+
perimanSetPinBusExtraType(_pin_irq, "ETH_IRQ");
758763
#if ETH_SPI_SUPPORTS_NO_IRQ
759764
}
760765
#endif
761766
if (_pin_sck != -1) {
762-
if (!perimanSetPinBus(_pin_sck, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), -1, -1)) {
767+
if (!perimanSetPinBus(_pin_sck, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), _eth_index, -1)) {
763768
goto err;
764769
}
770+
perimanSetPinBusExtraType(_pin_sck, "ETH_SPI_SCK");
765771
}
766772
if (_pin_miso != -1) {
767-
if (!perimanSetPinBus(_pin_miso, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), -1, -1)) {
773+
if (!perimanSetPinBus(_pin_miso, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), _eth_index, -1)) {
768774
goto err;
769775
}
776+
perimanSetPinBusExtraType(_pin_miso, "ETH_SPI_MISO");
770777
}
771778
if (_pin_mosi != -1) {
772-
if (!perimanSetPinBus(_pin_mosi, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), -1, -1)) {
779+
if (!perimanSetPinBus(_pin_mosi, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), _eth_index, -1)) {
773780
goto err;
774781
}
782+
perimanSetPinBusExtraType(_pin_mosi, "ETH_SPI_MOSI");
775783
}
776784
if (_pin_rst != -1) {
777-
if (!perimanSetPinBus(_pin_rst, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), -1, -1)) {
785+
if (!perimanSetPinBus(_pin_rst, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), _eth_index, -1)) {
778786
goto err;
779787
}
788+
perimanSetPinBusExtraType(_pin_rst, "ETH_RST");
780789
}
781790

782791
Network.onSysEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
@@ -840,11 +849,33 @@ void ETHClass::end(void) {
840849
return;
841850
}
842851
_eth_handle = NULL;
852+
//delete mac
853+
if (_mac != NULL) {
854+
_mac->del(_mac);
855+
_mac = NULL;
856+
}
857+
//delete phy
858+
if (_phy != NULL) {
859+
_phy->del(_phy);
860+
_phy = NULL;
861+
}
843862
}
844863

845864
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;
865+
bool do_not_unreg_ev_handler = false;
866+
for (int i = 0; i < NUM_SUPPORTED_ETH_PORTS; ++i) {
867+
if (_ethernets[i] != NULL && _ethernets[i]->netif() != NULL && _ethernets[i]->netif() != _esp_netif) {
868+
do_not_unreg_ev_handler = true;
869+
break;
870+
}
871+
}
872+
if (!do_not_unreg_ev_handler) {
873+
if (esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
874+
_eth_ev_instance = NULL;
875+
log_v("Unregistered event handler");
876+
} else {
877+
log_e("Failed to unregister event handler");
878+
}
848879
}
849880
}
850881

Diff for: libraries/Ethernet/src/ETH.h

+3
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ class ETHClass : public NetworkInterface {
184184
uint8_t _eth_index;
185185
eth_phy_type_t _phy_type;
186186
esp_eth_netif_glue_handle_t _glue_handle;
187+
esp_eth_mac_t *_mac;
188+
esp_eth_phy_t *_phy;
187189
#if ETH_SPI_SUPPORTS_CUSTOM
188190
SPIClass *_spi;
191+
char _cs_str[10];
189192
#endif
190193
uint8_t _spi_freq_mhz;
191194
int8_t _pin_cs;

Diff for: libraries/Network/src/NetworkInterface.cpp

+21-2
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) {

Diff for: libraries/PPP/src/PPP.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ bool PPPClass::begin(ppp_modem_model_t model, uint8_t uart_num, int baud_rate) {
283283
} else {
284284
pinMode(_pin_rst, OUTPUT);
285285
}
286+
perimanSetPinBusExtraType(_pin_rst, "PPP_MODEM_RST");
286287
digitalWrite(_pin_rst, !_pin_rst_act_low);
287288
delay(200);
288289
digitalWrite(_pin_rst, _pin_rst_act_low);

Diff for: libraries/WiFi/src/AP.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void _onApArduinoEvent(arduino_event_t *ev) {
8585
if (_ap_network_if == NULL || ev->event_id < ARDUINO_EVENT_WIFI_AP_START || ev->event_id > ARDUINO_EVENT_WIFI_AP_GOT_IP6) {
8686
return;
8787
}
88-
log_d("Arduino AP Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
88+
log_v("Arduino AP Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
8989
if (ev->event_id == ARDUINO_EVENT_WIFI_AP_START) {
9090
if (_ap_network_if->getStatusBits() & ESP_NETIF_WANT_IP6_BIT) {
9191
esp_err_t err = esp_netif_create_ip6_linklocal(_ap_network_if->netif());

Diff for: libraries/WiFi/src/STA.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void _onStaArduinoEvent(arduino_event_t *ev) {
107107
return;
108108
}
109109
static bool first_connect = true;
110-
log_d("Arduino STA Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
110+
log_v("Arduino STA Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
111111

112112
if (ev->event_id == ARDUINO_EVENT_WIFI_STA_START) {
113113
_sta_network_if->_setStatus(WL_DISCONNECTED);
@@ -162,11 +162,11 @@ static void _onStaArduinoEvent(arduino_event_t *ev) {
162162
_sta_network_if->connect();
163163
}
164164
} else if (ev->event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
165-
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
165+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
166166
uint8_t *ip = (uint8_t *)&(ev->event_info.got_ip.ip_info.ip.addr);
167167
uint8_t *mask = (uint8_t *)&(ev->event_info.got_ip.ip_info.netmask.addr);
168168
uint8_t *gw = (uint8_t *)&(ev->event_info.got_ip.ip_info.gw.addr);
169-
log_d(
169+
log_v(
170170
"STA IP: %u.%u.%u.%u, MASK: %u.%u.%u.%u, GW: %u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2], mask[3], gw[0], gw[1], gw[2], gw[3]
171171
);
172172
#endif

0 commit comments

Comments
 (0)