From 868d311a2361c9125d70833b8757a859a539e8cc Mon Sep 17 00:00:00 2001 From: Glenn Moloney Date: Sun, 30 Jun 2024 22:01:32 +1000 Subject: [PATCH] esp32/network_lan: Make LAN.active(state) succeed if already in state. This PR ensures that `network.LAN.active(True/False)` will succeed if the LAN is already in the desired state. Currently, `lan.active(True)` will raise an `OSError` exception if the LAN is already in the desired state. This is inconsistent with `network.WLAN.active(True/False)` and causes `lan.active(True)` to raise an exception after a soft reset (causing common network startup scripts to fail for LAN interfaces). Signed-off-by: Glenn Moloney --- ports/esp32/network_lan.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/esp32/network_lan.c b/ports/esp32/network_lan.c index 7e7ebcc92..b957c5f89 100644 --- a/ports/esp32/network_lan.c +++ b/ports/esp32/network_lan.c @@ -312,13 +312,14 @@ static mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) { lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); if (n_args > 1) { - if (mp_obj_is_true(args[1])) { + bool make_active = mp_obj_is_true(args[1]); + if (make_active && !self->base.active) { esp_netif_set_hostname(self->base.netif, mod_network_hostname_data); self->base.active = (esp_eth_start(self->eth_handle) == ESP_OK); if (!self->base.active) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed")); } - } else { + } else if (!make_active && self->base.active) { self->base.active = !(esp_eth_stop(self->eth_handle) == ESP_OK); if (self->base.active) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));