From ee2fe993dd86c53f22a03a76414d821588e9148f Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 12 Jan 2023 14:56:06 -0800 Subject: [PATCH] Fix repeated MDNS use on Pico W --- ports/raspberrypi/common-hal/mdns/Server.c | 19 +++++++++++++------ shared-bindings/mdns/Server.c | 3 ++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ports/raspberrypi/common-hal/mdns/Server.c b/ports/raspberrypi/common-hal/mdns/Server.c index e9b08efa33..38f3a70f76 100644 --- a/ports/raspberrypi/common-hal/mdns/Server.c +++ b/ports/raspberrypi/common-hal/mdns/Server.c @@ -36,21 +36,28 @@ #include "lwip/apps/mdns.h" #include "lwip/prot/dns.h" +// Track if we've inited the LWIP MDNS at all. It expects to only init once. +// Subsequent times, we restart it. +STATIC bool inited = false; // Track if we are globally inited. This essentially forces one inited MDNS // object at a time. (But ignores MDNS objects that are deinited.) -STATIC bool inited = false; +STATIC bool object_inited = false; #define NETIF_STA (&cyw43_state.netif[CYW43_ITF_STA]) #define NETIF_AP (&cyw43_state.netif[CYW43_ITF_AP]) void mdns_server_construct(mdns_server_obj_t *self, bool workflow) { - if (inited) { + if (object_inited) { self->inited = false; return; } - mdns_resp_init(); - inited = true; + if (!inited) { + mdns_resp_init(); + inited = true; + } else { + mdns_resp_restart(NETIF_STA); + } self->inited = true; uint8_t mac[6]; @@ -72,7 +79,7 @@ void common_hal_mdns_server_construct(mdns_server_obj_t *self, mp_obj_t network_ mp_raise_ValueError(translate("mDNS only works with built-in WiFi")); return; } - if (inited) { + if (object_inited) { mp_raise_RuntimeError(translate("mDNS already initialized")); } mdns_server_construct(self, false); @@ -83,7 +90,7 @@ void common_hal_mdns_server_deinit(mdns_server_obj_t *self) { return; } self->inited = false; - inited = false; + object_inited = false; mdns_resp_remove_netif(NETIF_STA); } diff --git a/shared-bindings/mdns/Server.c b/shared-bindings/mdns/Server.c index 2f3a0c7085..0d06d67f0d 100644 --- a/shared-bindings/mdns/Server.c +++ b/shared-bindings/mdns/Server.c @@ -64,7 +64,7 @@ STATIC mp_obj_t mdns_server_make_new(const mp_obj_type_t *type, size_t n_args, s } #endif - mdns_server_obj_t *self = m_new_obj(mdns_server_obj_t); + mdns_server_obj_t *self = m_new_obj_with_finaliser(mdns_server_obj_t); self->base.type = &mdns_server_type; common_hal_mdns_server_construct(self, args[ARG_network_interface].u_obj); @@ -205,6 +205,7 @@ STATIC const mp_rom_map_elem_t mdns_server_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_find), MP_ROM_PTR(&mdns_server_find_obj) }, { MP_ROM_QSTR(MP_QSTR_advertise_service), MP_ROM_PTR(&mdns_server_advertise_service_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mdns_server_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&mdns_server_deinit_obj) }, };