Fix repeated MDNS use on Pico W

This commit is contained in:
Scott Shawcroft 2023-01-12 14:56:06 -08:00
parent 5c517b7e5a
commit ee2fe993dd
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
2 changed files with 15 additions and 7 deletions

View File

@ -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);
}

View File

@ -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) },
};