@@ -200,6 +200,16 @@ STATIC void _update_encoded_ip(void) {
200200 }
201201}
202202
203+ mdns_server_obj_t * supervisor_web_workflow_mdns (mp_obj_t network_interface ) {
204+ #if CIRCUITPY_MDNS
205+ if (network_interface == & common_hal_wifi_radio_obj &&
206+ mdns .base .type == & mdns_server_type ) {
207+ return & mdns ;
208+ }
209+ #endif
210+ return NULL ;
211+ }
212+
203213#if CIRCUITPY_STATUS_BAR
204214bool supervisor_web_workflow_status_dirty (void ) {
205215 return common_hal_wifi_radio_get_enabled (& common_hal_wifi_radio_obj ) != _last_enabled ||
@@ -300,11 +310,6 @@ void supervisor_start_web_workflow(void) {
300310
301311 if (first_start ) {
302312 port_changed = false;
303- #if CIRCUITPY_MDNS
304- mdns_server_construct (& mdns , true);
305- mdns .base .type = & mdns_server_type ;
306- common_hal_mdns_server_set_instance_name (& mdns , MICROPY_HW_BOARD_NAME );
307- #endif
308313 pool .base .type = & socketpool_socketpool_type ;
309314 common_hal_socketpool_socketpool_construct (& pool , & common_hal_wifi_radio_obj );
310315
@@ -313,13 +318,26 @@ void supervisor_start_web_workflow(void) {
313318
314319 websocket_init ();
315320 }
321+ #if CIRCUITPY_MDNS
322+ // Try to start MDNS if the user deinited it.
323+ if (mdns .base .type != & mdns_server_type ||
324+ common_hal_mdns_server_deinited (& mdns )) {
325+ mdns_server_construct (& mdns , true);
326+ mdns .base .type = & mdns_server_type ;
327+ if (!common_hal_mdns_server_deinited (& mdns )) {
328+ common_hal_mdns_server_set_instance_name (& mdns , MICROPY_HW_BOARD_NAME );
329+ }
330+ }
331+ #endif
316332 if (port_changed ) {
317333 common_hal_socketpool_socket_close (& listening );
318334 }
319335 if (first_start || port_changed ) {
320336 web_api_port = new_port ;
321337 #if CIRCUITPY_MDNS
322- common_hal_mdns_server_advertise_service (& mdns , "_circuitpython" , "_tcp" , web_api_port );
338+ if (!common_hal_mdns_server_deinited (& mdns )) {
339+ common_hal_mdns_server_advertise_service (& mdns , "_circuitpython" , "_tcp" , web_api_port );
340+ }
323341 #endif
324342 socketpool_socket (& pool , SOCKETPOOL_AF_INET , SOCKETPOOL_SOCK_STREAM , & listening );
325343 common_hal_socketpool_socket_settimeout (& listening , 0 );
@@ -444,17 +462,18 @@ static bool _origin_ok(const char *origin) {
444462 }
445463 // These are prefix checks up to : so that any port works.
446464 // TODO: Support DHCP hostname in addition to MDNS.
465+ const char * end ;
447466 #if CIRCUITPY_MDNS
448- const char * local = ".local" ;
449- const char * hostname = common_hal_mdns_server_get_hostname (& mdns );
450- const char * end = origin + strlen (http ) + strlen (hostname ) + strlen (local );
451- if (strncmp (origin + strlen (http ), hostname , strlen (hostname )) == 0 &&
452- strncmp (origin + strlen (http ) + strlen (hostname ), local , strlen (local )) == 0 &&
453- (end [0 ] == '\0' || end [0 ] == ':' )) {
454- return true;
467+ if (!common_hal_mdns_server_deinited (& mdns )) {
468+ const char * local = ".local" ;
469+ const char * hostname = common_hal_mdns_server_get_hostname (& mdns );
470+ end = origin + strlen (http ) + strlen (hostname ) + strlen (local );
471+ if (strncmp (origin + strlen (http ), hostname , strlen (hostname )) == 0 &&
472+ strncmp (origin + strlen (http ) + strlen (hostname ), local , strlen (local )) == 0 &&
473+ (end [0 ] == '\0' || end [0 ] == ':' )) {
474+ return true;
475+ }
455476 }
456- #else
457- const char * end ;
458477 #endif
459478
460479 _update_encoded_ip ();
@@ -733,12 +752,13 @@ static void _reply_with_file(socketpool_socket_obj_t *socket, _request *request,
733752}
734753
735754static void _reply_with_devices_json (socketpool_socket_obj_t * socket , _request * request ) {
755+ size_t total_results = 0 ;
736756 #if CIRCUITPY_MDNS
737757 mdns_remoteservice_obj_t found_devices [32 ];
738- size_t total_results = mdns_server_find (& mdns , "_circuitpython" , "_tcp" , 1 , found_devices , MP_ARRAY_SIZE (found_devices ));
758+ if (!common_hal_mdns_server_deinited (& mdns )) {
759+ total_results = mdns_server_find (& mdns , "_circuitpython" , "_tcp" , 1 , found_devices , MP_ARRAY_SIZE (found_devices ));
760+ }
739761 size_t count = MIN (total_results , MP_ARRAY_SIZE (found_devices ));
740- #else
741- size_t total_results = 0 ;
742762 #endif
743763 socketpool_socket_send (socket , (const uint8_t * )OK_JSON , strlen (OK_JSON ));
744764 _cors_header (socket , request );
@@ -775,10 +795,11 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request *
775795 _send_str (socket , "\r\n" );
776796 mp_print_t _socket_print = {socket , _print_chunk };
777797
778- #if CIRCUITPY_MDNS
779- const char * hostname = common_hal_mdns_server_get_hostname (& mdns );
780- #else
781798 const char * hostname = "" ;
799+ #if CIRCUITPY_MDNS
800+ if (!common_hal_mdns_server_deinited (& mdns )) {
801+ hostname = common_hal_mdns_server_get_hostname (& mdns );
802+ }
782803 #endif
783804 _update_encoded_ip ();
784805 // Note: this leverages the fact that C concats consecutive string literals together.
@@ -1023,7 +1044,13 @@ static void _decode_percents(char *str) {
10231044static bool _reply (socketpool_socket_obj_t * socket , _request * request ) {
10241045 if (request -> redirect ) {
10251046 #if CIRCUITPY_MDNS
1026- _reply_redirect (socket , request , request -> path );
1047+ if (!common_hal_mdns_server_deinited (& mdns )) {
1048+ _reply_redirect (socket , request , request -> path );
1049+ } else {
1050+ _reply_missing (socket , request );
1051+ }
1052+ #else
1053+ _reply_missing (socket , request );
10271054 #endif
10281055 } else if (strlen (request -> origin ) > 0 && !_origin_ok (request -> origin )) {
10291056 _reply_forbidden (socket , request );
0 commit comments