8000 Wifi fully seperate from web-workflow · n3o59hf/circuitpython@5869af3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5869af3

Browse files
committed
Wifi fully seperate from web-workflow
1 parent 9eaae37 commit 5869af3

File tree

4 files changed

+71
-61
lines changed

4 files changed

+71
-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: 59 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,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

371378
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)
0