@@ -200,6 +200,16 @@ STATIC void _update_encoded_ip(void) {
200
200
}
201
201
}
202
202
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
+
203
213
#if CIRCUITPY_STATUS_BAR
204
214
bool supervisor_web_workflow_status_dirty (void ) {
205
215
return common_hal_wifi_radio_get_enabled (& common_hal_wifi_radio_obj ) != _last_enabled ||
@@ -300,11 +310,6 @@ void supervisor_start_web_workflow(void) {
300
310
301
311
if (first_start ) {
302
312
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
308
313
pool .base .type = & socketpool_socketpool_type ;
309
314
common_hal_socketpool_socketpool_construct (& pool , & common_hal_wifi_radio_obj );
310
315
@@ -313,13 +318,26 @@ void supervisor_start_web_workflow(void) {
313
318
314
319
websocket_init ();
315
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
316
332
if (port_changed ) {
317
333
common_hal_socketpool_socket_close (& listening );
318
334
}
319
335
if (first_start || port_changed ) {
320
336
web_api_port = new_port ;
321
337
#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
+ }
323
341
#endif
324
342
socketpool_socket (& pool , SOCKETPOOL_AF_INET , SOCKETPOOL_SOCK_STREAM , & listening );
325
343
common_hal_socketpool_socket_settimeout (& listening , 0 );
@@ -444,17 +462,18 @@ static bool _origin_ok(const char *origin) {
444
462
}
445
463
// These are prefix checks up to : so that any port works.
446
464
// TODO: Support DHCP hostname in addition to MDNS.
465
+ const char * end ;
447
466
#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
+ }
455
476
}
456
- #else
457
- const char * end ;
458
477
#endif
459
478
460
479
_update_encoded_ip ();
@@ -733,12 +752,13 @@ static void _reply_with_file(socketpool_socket_obj_t *socket, _request *request,
733
752
}
734
753
735
754
static void _reply_with_devices_json (socketpool_socket_obj_t * socket , _request * request ) {
755
+ size_t total_results = 0 ;
736
756
#if CIRCUITPY_MDNS
737
757
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
+ }
739
761
size_t count = MIN (total_results , MP_ARRAY_SIZE (found_devices ));
740
- #else
741
- size_t total_results = 0 ;
742
762
#endif
743
763
socketpool_socket_send (socket , (const uint8_t * )OK_JSON , strlen (OK_JSON ));
744
764
_cors_header (socket , request );
@@ -775,10 +795,11 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request *
775
795
_send_str (socket , "\r\n" );
776
796
mp_print_t _socket_print = {socket , _print_chunk };
777
797
778
- #if CIRCUITPY_MDNS
779
- const char * hostname = common_hal_mdns_server_get_hostname (& mdns );
780
- #else
781
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
+ }
782
803
#endif
783
804
_update_encoded_ip ();
784
805
// Note: this leverages the fact that C concats consecutive string literals together.
@@ -1023,7 +1044,13 @@ static void _decode_percents(char *str) {
1023
1044
static bool _reply (socketpool_socket_obj_t * socket , _request * request ) {
1024
1045
if (request -> redirect ) {
1025
1046
#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 );
1027
1054
#endif
1028
1055
} else if (strlen (request -> origin ) > 0 && !_origin_ok (request -> origin )) {
1029
1056
_reply_forbidden (socket , request );
0 commit comments