@@ -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,21 +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 );
317-
318- const size_t api_password_len = sizeof (_api_password ) - 1 ;
319- result = common_hal_os_getenv_str ("CIRCUITPY_WEB_API_PASSWORD" , _api_password + 1 , api_password_len );
320- if (result == GETENV_OK ) {
321- _api_password [0 ] = ':' ;
322- _base64_in_place (_api_password , strlen (_api_password ), sizeof (_api_password ) - 1 );
323- } else {
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 ) {
324308 return false;
325309 }
326310
327- bool first_start = pool .base .type != & socketpool_socketpool_type ;
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 );
321+
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+ }
328330
329- if (first_start ) {
330331 pool .base .type = & socketpool_socketpool_type ;
331332 common_hal_socketpool_socketpool_construct (& pool , & common_hal_wifi_radio_obj );
332333
@@ -336,36 +337,42 @@ bool supervisor_start_web_workflow(void) {
336337 websocket_init ();
337338 }
338339
339- if (!common_hal_socketpool_socket_get_closed (& active )) {
340- common_hal_socketpool_socket_close (& active );
341- }
340+ initialized = pool .base .type == & socketpool_socketpool_type ;
342341
343- #if CIRCUITPY_MDNS
344- // Try to start MDNS if the user deinited it.
345- if (mdns .base .type != & mdns_server_type ||
346- common_hal_mdns_server_deinited (& mdns )) {
347- mdns_server_construct (& mdns , true);
348- mdns .base .type = & mdns_server_type ;
342+ if (initialized ){
343+ if (!common_hal_socketpool_socket_get_closed (& active )) {
344+ common_hal_socketpool_socket_close (& active );
345+ }
346+
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+ }
349358 if (!common_hal_mdns_server_deinited (& mdns )) {
350- common_hal_mdns_server_set_instance_name (& mdns , web_instance_name );
359+ common_hal_mdns_server_advertise_service (& mdns , "_circuitpython" , "_tcp" , web_api_port );
351360 }
352- }
353- if (!common_hal_mdns_server_deinited (& mdns )) {
354- common_hal_mdns_server_advertise_service (& mdns , "_circuitpython" , "_tcp" , web_api_port );
355- }
356- #endif
361+ #endif
357362
358- if (common_hal_socketpool_socket_get_closed (& listening )) {
359- socketpool_socket (& pool , SOCKETPOOL_AF_INET , SOCKETPOOL_SOCK_STREAM , & listening );
360- common_hal_socketpool_socket_settimeout (& listening , 0 );
361- // Bind to any ip. (Not checking for failures)
362- common_hal_socketpool_socket_bind (& listening , "" , 0 , web_api_port );
363- 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;
364374 }
365- // Wake polling thread (maybe)
366- socketpool_socket_poll_resume ();
367- #endif
368- return true;
375+ return false;
369376}
370377
371378void web_workflow_send_raw (socketpool_socket_obj_t * socket , const uint8_t * buf , int len ) {
0 commit comments