@@ -202,7 +202,8 @@ STATIC void _update_encoded_ip(void) {
202
202
203
203
mdns_server_obj_t * supervisor_web_workflow_mdns (mp_obj_t network_interface ) {
204
204
#if CIRCUITPY_MDNS
205
- if (network_interface == & common_hal_wifi_radio_obj ) {
205
+ if (network_interface == & common_hal_wifi_radio_obj &&
206
+ mdns .base .type == & mdns_server_type ) {
206
207
return & mdns ;
207
208
}
208
209
#endif
@@ -309,11 +310,6 @@ void supervisor_start_web_workflow(void) {
309
310
310
311
if (first_start ) {
311
312
port_changed = false;
312
- #if CIRCUITPY_MDNS
313
- mdns_server_construct (& mdns , true);
314
- mdns .base .type = & mdns_server_type ;
315
- common_hal_mdns_server_set_instance_name (& mdns , MICROPY_HW_BOARD_NAME );
316
- #endif
317
313
pool .base .type = & socketpool_socketpool_type ;
318
314
common_hal_socketpool_socketpool_construct (& pool , & common_hal_wifi_radio_obj );
319
315
@@ -322,13 +318,26 @@ void supervisor_start_web_workflow(void) {
322
318
323
319
websocket_init ();
324
320
}
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
325
332
if (port_changed ) {
326
333
common_hal_socketpool_socket_close (& listening );
327
334
}
328
335
if (first_start || port_changed ) {
329
336
web_api_port = new_port ;
330
337
#if CIRCUITPY_MDNS
331
- 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
+ }
332
341
#endif
333
342
socketpool_socket (& pool , SOCKETPOOL_AF_INET , SOCKETPOOL_SOCK_STREAM , & listening );
334
343
common_hal_socketpool_socket_settimeout (& listening , 0 );
@@ -453,17 +462,18 @@ static bool _origin_ok(const char *origin) {
453
462
}
454
463
// These are prefix checks up to : so that any port works.
455
464
// TODO: Support DHCP hostname in addition to MDNS.
465
+ const char * end ;
456
466
#if CIRCUITPY_MDNS
457
- const char * local = ".local" ;
458
- const char * hostname = common_hal_mdns_server_get_hostname (& mdns );
459
- const char * end = origin + strlen (http ) + strlen (hostname ) + strlen (local );
460
- if (strncmp (origin + strlen (http ), hostname , strlen (hostname )) == 0 &&
461
- strncmp (origin + strlen (http ) + strlen (hostname ), local , strlen (local )) == 0 &&
462
- (end [0 ] == '\0' || end [0 ] == ':' )) {
463
- 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
+ }
464
476
}
465
- #else
466
- const char * end ;
467
477
#endif
468
478
469
479
_update_encoded_ip ();
@@ -742,12 +752,13 @@ static void _reply_with_file(socketpool_socket_obj_t *socket, _request *request,
742
752
}
743
753
744
754
static void _reply_with_devices_json (socketpool_socket_obj_t * socket , _request * request ) {
755
+ size_t total_results = 0 ;
745
756
#if CIRCUITPY_MDNS
746
757
mdns_remoteservice_obj_t found_devices [32 ];
747
- 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
+ }
748
761
size_t count = MIN (total_results , MP_ARRAY_SIZE (found_devices ));
749
- #else
750
- size_t total_results = 0 ;
751
762
#endif
752
763
socketpool_socket_send (socket , (const uint8_t * )OK_JSON , strlen (OK_JSON ));
753
764
_cors_header (socket , request );
@@ -784,10 +795,11 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request *
784
795
_send_str (socket , "\r\n" );
785
796
mp_print_t _socket_print = {socket , _print_chunk };
786
797
787
- #if CIRCUITPY_MDNS
788
- const char * hostname = common_hal_mdns_server_get_hostname (& mdns );
789
- #else
790
798
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
+ }
791
803
#endif
792
804
_update_encoded_ip ();
793
805
// Note: this leverages the fact that C concats consecutive string literals together.
@@ -1032,7 +1044,13 @@ static void _decode_percents(char *str) {
1032
1044
static bool _reply (socketpool_socket_obj_t * socket , _request * request ) {
1033
1045
if (request -> redirect ) {
1034
1046
#if CIRCUITPY_MDNS
1035
- _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 );
1036
1054
#endif
1037
1055
} else if (strlen (request -> origin ) > 0 && !_origin_ok (request -> origin )) {
1038
1056
_reply_forbidden (socket , request );
0 commit comments