@@ -258,19 +258,9 @@ void supervisor_web_workflow_status(void) {
258258}
259259#endif
260260
261- bool supervisor_start_web_workflow (void ) {
261+ bool supervisor_start_web_workflow (bool reload ) {
262262 #if CIRCUITPY_WEB_WORKFLOW && CIRCUITPY_WIFI && CIRCUITPY_OS_GETENV
263263
264- // Skip starting the workflow if we're not starting from power on or reset.
265- const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason ();
266- if (reset_reason != RESET_REASON_POWER_ON &&
267- reset_reason != RESET_REASON_RESET_PIN &&
268- reset_reason != RESET_REASON_DEEP_SLEEP_ALARM &&
269- reset_reason != RESET_REASON_UNKNOWN &&
270- reset_reason != RESET_REASON_SOFTWARE ) {
271- return false;
272- }
273-
274264 char ssid [33 ];
275265 char password [64 ];
276266
@@ -287,11 +277,6 @@ bool supervisor_start_web_workflow(void) {
287277 return false;
288278 }
289279
290- result = common_hal_os_getenv_str ("CIRCUITPY_WEB_INSTANCE_NAME" , web_instance_name , sizeof (web_instance_name ));
291- if (result != GETENV_OK || web_instance_name [0 ] == '\0' ) {
292- strcpy (web_instance_name , MICROPY_HW_BOARD_NAME );
293- }
294-
295280 if (!common_hal_wifi_radio_get_enabled (& common_hal_wifi_radio_obj )) {
296281 common_hal_wifi_init (false);
297282 common_hal_wifi_radio_set_enabled (& common_hal_wifi_radio_obj , true);
@@ -303,6 +288,7 @@ bool supervisor_start_web_workflow(void) {
303288 // We can all connect again because it will return early if we're already connected to the
304289 // network. If we are connected to a different network, then it will disconnect before
305290 // attempting to connect to the given network.
291+
306292 _wifi_status = common_hal_wifi_radio_connect (
307293 & common_hal_wifi_radio_obj , (uint8_t * )ssid , strlen (ssid ), (uint8_t * )password , strlen (password ),
308294 0 , 8 , NULL , 0 );
@@ -312,12 +298,36 @@ bool supervisor_start_web_workflow(void) {
312298 return false;
313299 }
314300
315- // (leaves new_port unchanged on any failure)
316- (void )common_hal_os_getenv_int ("CIRCUITPY_WEB_API_PORT" , & web_api_port );
301+ // Skip starting the workflow if we're not starting from power on or reset.
302+ const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason ();
303+ if (reset_reason != RESET_REASON_POWER_ON &&
304+ reset_reason != RESET_REASON_RESET_PIN &&
305+ reset_reason != RESET_REASON_DEEP_SLEEP_ALARM &&
306+ reset_reason != RESET_REASON_UNKNOWN &&
307+ reset_reason != RESET_REASON_SOFTWARE ) {
308+ return false;
309+ }
310+
311+ bool initialized = pool .base .type == & socketpool_socketpool_type ;
312+
313+ if (!initialized && !reload ) {
314+ result = common_hal_os_getenv_str ("CIRCUITPY_WEB_INSTANCE_NAME" , web_instance_name , sizeof (web_instance_name ));
315+ if (result != GETENV_OK || web_instance_name [0 ] == '\0' ) {
316+ strcpy (web_instance_name , MICROPY_HW_BOARD_NAME );
317+ }
318+
319+ // (leaves new_port unchanged on any failure)
320+ (void )common_hal_os_getenv_int ("CIRCUITPY_WEB_API_PORT" , & web_api_port );
317321
318- bool first_start = pool .base .type != & socketpool_socketpool_type ;
322+ const size_t api_password_len = sizeof (_api_password ) - 1 ;
323+ result = common_hal_os_getenv_str ("CIRCUITPY_WEB_API_PASSWORD" , _api_password + 1 , api_password_len );
324+ if (result == GETENV_OK ) {
325+ _api_password [0 ] = ':' ;
326+ _base64_in_place (_api_password , strlen (_api_password ), sizeof (_api_password ) - 1 );
327+ } else { // Skip starting web-workflow when no password is passed.
328+ return false;
329+ }
319330
320- if (first_start ) {
321331 pool .base .type = & socketpool_socketpool_type ;
322332 common_hal_socketpool_socketpool_construct (& pool , & common_hal_wifi_radio_obj );
323333
@@ -327,43 +337,42 @@ bool supervisor_start_web_workflow(void) {
327337 websocket_init ();
328338 }
329339
330- if (!common_hal_socketpool_socket_get_closed (& active )) {
331- common_hal_socketpool_socket_close (& active );
332- }
340+ initialized = pool .base .type == & socketpool_socketpool_type ;
333341
334- #if CIRCUITPY_MDNS
335- // Try to start MDNS if the user deinited it.
336- if (mdns .base .type != & mdns_server_type ||
337- common_hal_mdns_server_deinited (& mdns )) {
338- mdns_server_construct (& mdns , true);
339- mdns .base .type = & mdns_server_type ;
340- if (!common_hal_mdns_server_deinited (& mdns )) {
341- common_hal_mdns_server_set_instance_name (& mdns , web_instance_name );
342+ if (initialized ){
343+ if (!common_hal_socketpool_socket_get_closed (& active )) {
344+ common_hal_socketpool_socket_close (& active );
342345 }
343- }
344- if (!common_hal_mdns_server_deinited (& mdns )) {
345- common_hal_mdns_server_advertise_service (& mdns , "_circuitpython" , "_tcp" , web_api_port );
346- }
347- #endif
348346
349- const size_t api_password_len = sizeof (_api_password ) - 1 ;
350- result = common_hal_os_getenv_str ("CIRCUITPY_WEB_API_PASSWORD" , _api_password + 1 , api_password_len );
351- if (result == GETENV_OK ) {
352- _api_password [0 ] = ':' ;
353- _base64_in_place (_api_password , strlen (_api_password ), sizeof (_api_password ) - 1 );
354- }
347+ #if CIRCUITPY_MDNS
348+ // Try to start MDNS if the user deinited it.
349+ if (mdns .base .type != & mdns_server_type ||
350+ common_hal_mdns_server_deinited (& mdns ) ||
351+ reload ) { // Always reconstruct on reload, since we don't know if the net changed.
352+ mdns_server_construct (& mdns , true);
353+ mdns .base .type = & mdns_server_type ;
354+ if (!common_hal_mdns_server_deinited (& mdns )) {
355+ common_hal_mdns_server_set_instance_name (& mdns , web_instance_name );
356+ }
357+ }
358+ if (!common_hal_mdns_server_deinited (& mdns )) {
359+ common_hal_mdns_server_advertise_service (& mdns , "_circuitpython" , "_tcp" , web_api_port );
360+ }
361+ #endif
355362
356- if (common_hal_socketpool_socket_get_closed (& listening )) {
357- socketpool_socket (& pool , SOCKETPOOL_AF_INET , SOCKETPOOL_SOCK_STREAM , & listening );
358- common_hal_socketpool_socket_settimeout (& listening , 0 );
359- // Bind to any ip. (Not checking for failures)
360- common_hal_socketpool_socket_bind (& listening , "" , 0 , web_api_port );
361- common_hal_socketpool_socket_listen (& listening , 1 );
363+ if (common_hal_socketpool_socket_get_closed (& listening )) {
364+ socketpool_socket (& pool , SOCKETPOOL_AF_INET , SOCKETPOOL_SOCK_STREAM , & listening );
365+ common_hal_socketpool_socket_settimeout (& listening , 0 );
366+ // Bind to any ip. (Not checking for failures)
367+ common_hal_socketpool_socket_bind (& listening , "" , 0 , web_api_port );
368+ common_hal_socketpool_socket_listen (& listening , 1 );
369+ }
370+ // Wake polling thread (maybe)
371+ socketpool_socket_poll_resume ();
372+ #endif
373+ return true;
362374 }
363- // Wake polling thread (maybe)
364- socketpool_socket_poll_resume ();
365- #endif
366- return true;
375+ return false;
367376}
368377
369378void web_workflow_send_raw (socketpool_socket_obj_t * socket , const uint8_t * buf , int len ) {
0 commit comments