Skip to content

Commit e776f38

Browse files
authored
Merge pull request #8367 from bill88t/web_workflow_disable
Web workflow skip init when no password, freeing socket. Also reconnect wifi on reload.
2 parents aad6e74 + 5869af3 commit e776f38

File tree

4 files changed

+73
-61
lines changed

4 files changed

+73
-61
lines changed

docs/workflows.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ conflicts with user created NUS services.
6868
Read-only characteristic that returns the UTF-8 encoded version string.
6969

7070
## Web
71+
If the keys `CIRCUITPY_WIFI_SSID` and `CIRCUITPY_WIFI_PASSWORD` are set in `settings.toml`,
72+
CircuitPython will automatically connect to the given Wi-Fi network on boot and upon reload.
7173

72-
The web workflow is depends on adding Wi-Fi credentials into the `settings.toml` file. The keys are
73-
`CIRCUITPY_WIFI_SSID` and `CIRCUITPY_WIFI_PASSWORD`. Once these are defined, CircuitPython will
74-
automatically connect to the network and start the webserver used for the workflow. The webserver
75-
is on port 80 unless overridden by `CIRCUITPY_WEB_API_PORT`. It also enables MDNS. The name
76-
of the board as advertised to the network can be overridden by `CIRCUITPY_WEB_INSTANCE_NAME`.
74+
If `CIRCUITPY_WEB_API_PASSWORD` is also set, the web workflow will also start.
75+
The web workflow will only be enabled if the Wi-Fi connection succeeds upon boot.
76+
77+
The webserver is on port 80 unless overridden by `CIRCUITPY_WEB_API_PORT`. It also enables MDNS.
78+
The name of the board as advertised to the network can be overridden by `CIRCUITPY_WEB_INSTANCE_NAME`.
7779

7880
Here is an example `/settings.toml`:
7981

@@ -82,7 +84,7 @@ Here is an example `/settings.toml`:
8284
CIRCUITPY_WIFI_SSID="scottswifi"
8385
CIRCUITPY_WIFI_PASSWORD="secretpassword"
8486

85-
# To enable modifying files from the web. Change this too!
87+
# To enable the the webserver. Change this too!
8688
# Leave the User field blank in the browser.
8789
CIRCUITPY_WEB_API_PASSWORD="passw0rd"
8890

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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

369378
void web_workflow_send_raw(socketpool_socket_obj_t *socket, const uint8_t *buf, int len) {

supervisor/shared/web_workflow/web_workflow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
void supervisor_web_workflow_background(void *data);
3737
bool supervisor_web_workflow_status_dirty(void);
3838
void supervisor_web_workflow_status(void);
39-
bool supervisor_start_web_workflow(void);
39+
bool supervisor_start_web_workflow(bool);
4040
void supervisor_stop_web_workflow(void);
4141

4242
// Share the MDNS object with user code.

supervisor/shared/workflow.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ void supervisor_workflow_reset(void) {
5656
#endif
5757

5858
#if CIRCUITPY_WEB_WORKFLOW
59+
bool result = supervisor_start_web_workflow(true);
5960
if (workflow_background_cb.fun) {
60-
if (supervisor_start_web_workflow()) {
61+
if (result) {
6162
supervisor_workflow_request_background();
6263
}
6364
}
@@ -105,7 +106,7 @@ void supervisor_workflow_start(void) {
105106
#endif
106107

107108
#if CIRCUITPY_WEB_WORKFLOW
108-
if (supervisor_start_web_workflow()) {
109+
if (supervisor_start_web_workflow(false)) {
109110
// Enable background callbacks if web_workflow startup successful
110111
memset(&workflow_background_cb, 0, sizeof(workflow_background_cb));
111112
workflow_background_cb.fun = supervisor_web_workflow_background;

0 commit comments

Comments
 (0)