Skip to content

Commit 57f03ed

Browse files
szerwime-no-devlucasssvaz
authored
fix(eth): Del mac and phy resources (#11982)
* fix(eth): Del mac and phy resources - Delete _mac and _phy resources when initialization of ETH fails - Delete _mac and _phy resources when calling ETHClass::end() and ETH is not fully initialized * fx(pr): Remove call to _delMacAndPhy on MAC creation failure --------- Co-authored-by: Me No Dev <[email protected]> Co-authored-by: Lucas Saavedra Vaz <[email protected]>
1 parent 9077b54 commit 57f03ed

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

libraries/Ethernet/src/ETH.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
305305
}
306306
if (_phy == NULL) {
307307
log_e("esp_eth_phy_new failed");
308+
_delMacAndPhy();
308309
return false;
309310
}
310311

@@ -738,15 +739,29 @@ bool ETHClass::beginSPI(
738739
return false;
739740
}
740741

742+
if (_mac == NULL) {
743+
log_e("esp_eth_mac_new failed");
744+
_delMacAndPhy();
745+
return false;
746+
}
747+
748+
if (_phy == NULL) {
749+
log_e("esp_eth_phy_new failed");
750+
_delMacAndPhy();
751+
return false;
752+
}
753+
741754
// Init Ethernet driver to default and install it
742755
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(_mac, _phy);
743756
ret = esp_eth_driver_install(&eth_config, &_eth_handle);
744757
if (ret != ESP_OK) {
745758
log_e("SPI Ethernet driver install failed: %d", ret);
759+
_delMacAndPhy();
746760
return false;
747761
}
748762
if (_eth_handle == NULL) {
749763
log_e("esp_eth_driver_install failed! eth_handle is NULL");
764+
_delMacAndPhy();
750765
return false;
751766
}
752767

@@ -923,6 +938,18 @@ static bool empty_ethDetachBus(void *bus_pointer) {
923938
return true;
924939
}
925940

941+
void ETHClass::_delMacAndPhy() {
942+
if (_mac != NULL) {
943+
_mac->del(_mac);
944+
_mac = NULL;
945+
}
946+
947+
if (_phy != NULL) {
948+
_phy->del(_phy);
949+
_phy = NULL;
950+
}
951+
}
952+
926953
void ETHClass::end(void) {
927954

928955
Network.removeEvent(_eth_connected_event_handle);
@@ -954,18 +981,10 @@ void ETHClass::end(void) {
954981
return;
955982
}
956983
_eth_handle = NULL;
957-
//delete mac
958-
if (_mac != NULL) {
959-
_mac->del(_mac);
960-
_mac = NULL;
961-
}
962-
//delete phy
963-
if (_phy != NULL) {
964-
_phy->del(_phy);
965-
_phy = NULL;
966-
}
967984
}
968985

986+
_delMacAndPhy();
987+
969988
if (_eth_ev_instance != NULL) {
970989
bool do_not_unreg_ev_handler = false;
971990
for (int i = 0; i < NUM_SUPPORTED_ETH_PORTS; ++i) {

libraries/Ethernet/src/ETH.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ class ETHClass : public NetworkInterface {
271271
bool _setLinkSpeed(uint16_t speed);
272272
bool _setAutoNegotiation(bool on);
273273

274+
void _delMacAndPhy();
275+
274276
friend class EthernetClass; // to access beginSPI
275277
};
276278

0 commit comments

Comments
 (0)