diff --git a/ports/espressif/common-hal/mdns/Server.c b/ports/espressif/common-hal/mdns/Server.c index 38789b0610..18c0f0f5e4 100644 --- a/ports/espressif/common-hal/mdns/Server.c +++ b/ports/espressif/common-hal/mdns/Server.c @@ -33,14 +33,19 @@ #include "components/mdns/include/mdns.h" -STATIC bool inited = false; +// Track whether the underlying IDF mdns has been started so that we only +// create a single inited MDNS object to CircuitPython. (After deinit, another +// could be created.) +STATIC bool mdns_started = false; void mdns_server_construct(mdns_server_obj_t *self, bool workflow) { - if (inited) { + if (mdns_started) { + // Mark this object as deinited because another is already using MDNS. self->inited = false; return; } mdns_init(); + mdns_started = true; uint8_t mac[6]; esp_netif_get_mac(common_hal_wifi_radio_obj.netif, mac); @@ -81,7 +86,7 @@ void common_hal_mdns_server_deinit(mdns_server_obj_t *self) { return; } self->inited = false; - inited = false; + mdns_started = false; mdns_free(); } diff --git a/ports/espressif/common-hal/mdns/Server.h b/ports/espressif/common-hal/mdns/Server.h index 795a13a7a8..ee463657f1 100644 --- a/ports/espressif/common-hal/mdns/Server.h +++ b/ports/espressif/common-hal/mdns/Server.h @@ -32,7 +32,7 @@ typedef struct { mp_obj_base_t base; const char *hostname; const char *instance_name; - // "cpy-" "XXXXXX" "\0" - char default_hostname[4 + 6 + 1]; + char default_hostname[sizeof("cpy-XXXXXX")]; + // Track if this object owns access to the underlying MDNS service. bool inited; } mdns_server_obj_t; diff --git a/ports/raspberrypi/common-hal/mdns/Server.c b/ports/raspberrypi/common-hal/mdns/Server.c index 38f3a70f76..ab0a7079af 100644 --- a/ports/raspberrypi/common-hal/mdns/Server.c +++ b/ports/raspberrypi/common-hal/mdns/Server.c @@ -48,6 +48,7 @@ STATIC bool object_inited = false; void mdns_server_construct(mdns_server_obj_t *self, bool workflow) { if (object_inited) { + // Mark the object as deinit since another is already using MDNS. self->inited = false; return; } diff --git a/ports/raspberrypi/common-hal/mdns/Server.h b/ports/raspberrypi/common-hal/mdns/Server.h index f2440d7a64..a4dab8aa06 100644 --- a/ports/raspberrypi/common-hal/mdns/Server.h +++ b/ports/raspberrypi/common-hal/mdns/Server.h @@ -34,8 +34,8 @@ typedef struct { mp_obj_base_t base; const char *hostname; const char *instance_name; - // "cpy-" "XXXXXX" "\0" - char default_hostname[4 + 6 + 1]; + char default_hostname[sizeof("cpy-XXXXXX")]; const char *service_type[MDNS_MAX_SERVICES]; + // Track if this object owns access to the underlying MDNS service. bool inited; } mdns_server_obj_t;