From d086a7cc9edb91369a2f7c8d89e8da73971cf538 Mon Sep 17 00:00:00 2001 From: Ran Bao Date: Sat, 17 Aug 2024 21:51:32 +1200 Subject: [PATCH 1/4] Re-organize the system control structure --- .vscode/settings.json | 3 +- src/app.cpp | 7 ---- src/app.h | 1 - src/eeprom.c | 65 +-------------------------------- src/eeprom.h | 2 - src/html/web_portal.html | 8 ++++ src/menu.cpp | 2 +- src/rest_endpoints.c | 1 + src/system_control.c | 79 ++++++++++++++++++++++++++++++++++++++++ src/system_control.h | 23 ++++++++++++ 10 files changed, 116 insertions(+), 75 deletions(-) create mode 100644 src/system_control.c create mode 100644 src/system_control.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 1020abe..67bb37c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -49,6 +49,7 @@ "clocks.h": "c", "pwm.h": "c", "servo_gate.h": "c", - "semphr.h": "c" + "semphr.h": "c", + "version.h": "c" } } diff --git a/src/app.cpp b/src/app.cpp index 33bfca8..bf94867 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -29,13 +29,6 @@ #include "servo_gate.h" -uint8_t software_reboot() { - watchdog_reboot(0, 0, 0); - - return 0; -} - - int main() { // stdio_init_all(); diff --git a/src/app.h b/src/app.h index bb40fc5..99df2c9 100644 --- a/src/app.h +++ b/src/app.h @@ -35,7 +35,6 @@ extern "C" { #endif bool app_init(); -uint8_t software_reboot(); bool http_app_config(); #ifdef __cplusplus diff --git a/src/eeprom.c b/src/eeprom.c index b8867ef..9d9f809 100644 --- a/src/eeprom.c +++ b/src/eeprom.c @@ -6,7 +6,6 @@ #include #include -#include "hardware/watchdog.h" #include "hardware/regs/rosc.h" #include "hardware/regs/addressmap.h" @@ -19,8 +18,8 @@ #include "app.h" #include "neopixel_led.h" #include "rotary_button.h" -#include "version.h" #include "profile.h" +#include "system_control.h" extern bool cat24c256_eeprom_erase(); @@ -78,7 +77,7 @@ uint8_t eeprom_erase(bool reboot) { cat24c256_eeprom_erase(); if (reboot) { - watchdog_reboot(0, 0, 0); + software_reboot(); } return 37; // Configuration Menu ID @@ -186,63 +185,3 @@ bool eeprom_get_board_id(char ** board_id_buffer, size_t bytes_to_copy) { return true; } - -bool http_rest_system_control(struct fs_file *file, int num_params, char *params[], char *values[]) { - // Mappings - // s0 (str): unique_id - // s1 (str): version_string - // s2 (str): vcs_hash - // s3 (str): build_type - // s4 (bool): save_to_eeprom - // s5 (bool): software_reset - // s6 (bool): erase_eeprom - static char eeprom_config_json_buffer[256]; - - bool save_to_eeprom_flag = false; - bool software_reset_flag = false; - bool erase_eeprom_flag = false; - - // Control - for (int idx = 0; idx < num_params; idx += 1) { - if (strcmp(params[idx], "s4") == 0) { - save_to_eeprom_flag = string_to_boolean(values[idx]); - } - else if (strcmp(params[idx], "s5") == 0) { - software_reset_flag = string_to_boolean(values[idx]); - } - else if (strcmp(params[idx], "s6") == 0) { - erase_eeprom_flag = string_to_boolean(values[idx]); - } - } - - if (save_to_eeprom_flag) { - eeprom_save_all(); - } - - if (erase_eeprom_flag) { - eeprom_erase(software_reset_flag); - } - - if (software_reset_flag) { - software_reboot(); - } - - // Response - snprintf(eeprom_config_json_buffer, - sizeof(eeprom_config_json_buffer), - "%s" - "{\"s0\":\"%s\",\"s1\":\"%s\",\"s2\":\"%s\",\"s3\":\"%s\",\"s4\":%s,\"s5\":%s,\"s6\":%s}", - http_json_header, - metadata.unique_id, version_string, vcs_hash, build_type, - boolean_to_string(save_to_eeprom_flag), - boolean_to_string(erase_eeprom_flag), - boolean_to_string(software_reset_flag)); - - size_t data_length = strlen(eeprom_config_json_buffer); - file->data = eeprom_config_json_buffer; - file->len = data_length; - file->index = data_length; - file->flags = FS_FILE_FLAGS_HEADER_INCLUDED; - - return true; -} diff --git a/src/eeprom.h b/src/eeprom.h index 3e819c3..c9bad04 100644 --- a/src/eeprom.h +++ b/src/eeprom.h @@ -47,8 +47,6 @@ uint8_t eeprom_erase(bool); uint8_t eeprom_save_all(void); void eeprom_register_handler(eeprom_save_handler_t handler); -bool http_rest_system_control(struct fs_file *file, int num_params, char *params[], char *values[]); - #ifdef __cplusplus } diff --git a/src/html/web_portal.html b/src/html/web_portal.html index 593b82a..0ba1a1a 100644 --- a/src/html/web_portal.html +++ b/src/html/web_portal.html @@ -609,6 +609,14 @@ + +
+ + +
+ + +
diff --git a/src/menu.cpp b/src/menu.cpp index f832a3a..183a1e5 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -17,7 +17,7 @@ #include "cleanup_mode.h" #include "eeprom.h" #include "wireless.h" - +#include "system_control.h" // External variables extern muif_t muif_list[]; diff --git a/src/rest_endpoints.c b/src/rest_endpoints.c index 352a297..f93b7f4 100644 --- a/src/rest_endpoints.c +++ b/src/rest_endpoints.c @@ -13,6 +13,7 @@ #include "profile.h" #include "cleanup_mode.h" #include "servo_gate.h" +#include "system_control.h" // Generated headers by html2header.py under scripts #include "display_mirror.html.h" diff --git a/src/system_control.c b/src/system_control.c new file mode 100644 index 0000000..370259b --- /dev/null +++ b/src/system_control.c @@ -0,0 +1,79 @@ +#include + +#include "hardware/watchdog.h" + +#include "system_control.h" +#include "common.h" +#include "eeprom.h" +#include "version.h" + +extern eeprom_metadata_t metadata; + + +int software_reboot() { + watchdog_reboot(0, 0, 0); + + return 0; +} + + + +bool http_rest_system_control(struct fs_file *file, int num_params, char *params[], char *values[]) { + // Mappings + // s0 (str): unique_id + // s1 (str): version_string + // s2 (str): vcs_hash + // s3 (str): build_type + // s4 (bool): save_to_eeprom + // s5 (bool): software_reset + // s6 (bool): erase_eeprom + static char eeprom_config_json_buffer[256]; + + bool save_to_eeprom_flag = false; + bool software_reset_flag = false; + bool erase_eeprom_flag = false; + + // Control + for (int idx = 0; idx < num_params; idx += 1) { + if (strcmp(params[idx], "s4") == 0) { + save_to_eeprom_flag = string_to_boolean(values[idx]); + } + else if (strcmp(params[idx], "s5") == 0) { + software_reset_flag = string_to_boolean(values[idx]); + } + else if (strcmp(params[idx], "s6") == 0) { + erase_eeprom_flag = string_to_boolean(values[idx]); + } + } + + if (save_to_eeprom_flag) { + eeprom_save_all(); + } + + if (erase_eeprom_flag) { + eeprom_erase(software_reset_flag); + } + + if (software_reset_flag) { + software_reboot(); + } + + // Response + snprintf(eeprom_config_json_buffer, + sizeof(eeprom_config_json_buffer), + "%s" + "{\"s0\":\"%s\",\"s1\":\"%s\",\"s2\":\"%s\",\"s3\":\"%s\",\"s4\":%s,\"s5\":%s,\"s6\":%s}", + http_json_header, + metadata.unique_id, version_string, vcs_hash, build_type, + boolean_to_string(save_to_eeprom_flag), + boolean_to_string(erase_eeprom_flag), + boolean_to_string(software_reset_flag)); + + size_t data_length = strlen(eeprom_config_json_buffer); + file->data = eeprom_config_json_buffer; + file->len = data_length; + file->index = data_length; + file->flags = FS_FILE_FLAGS_HEADER_INCLUDED; + + return true; +} diff --git a/src/system_control.h b/src/system_control.h new file mode 100644 index 0000000..1110300 --- /dev/null +++ b/src/system_control.h @@ -0,0 +1,23 @@ +#ifndef SYSTEM_CONTROL_H_ +#define SYSTEM_CONTROL_H_ + +#include +#include +#include +#include "http_rest.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +bool http_rest_system_control(struct fs_file *file, int num_params, char *params[], char *values[]); +int software_reboot(void); + + +#ifdef __cplusplus +} +#endif + + +#endif // \ No newline at end of file From 9b29ca4a7441372a21b02c15bbebaded8e947291 Mon Sep 17 00:00:00 2001 From: Ran Bao Date: Sun, 18 Aug 2024 00:10:15 +1200 Subject: [PATCH 2/4] Add export function --- src/html/web_portal.html | 61 +++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/src/html/web_portal.html b/src/html/web_portal.html index 0ba1a1a..02700f7 100644 --- a/src/html/web_portal.html +++ b/src/html/web_portal.html @@ -609,15 +609,16 @@ + -
+
+ + +
+ + +
- -
- - -
- @@ -1395,6 +1396,52 @@

Error

fetch(uri); } + async function onExportConfigClicked() { + const config = {}; + + const endpoints = [ + "/rest/scale_config", + "/rest/charge_mode_config", + "/rest/coarse_motor_config", + "/rest/fine_motor_config", + "/rest/button_config", + "/rest/wireless_config", + "/rest/neopixel_led_config", + "/rest/profile_config?pf=0", + "/rest/profile_config?pf=1", + "/rest/profile_config?pf=2", + "/rest/profile_config?pf=3", + "/rest/profile_config?pf=4", + "/rest/profile_config?pf=5", + "/rest/profile_config?pf=6", + "/rest/profile_config?pf=7", + "/rest/servo_gate_config", + ] + + // Read metadata + const response = await fetch("/rest/system_control"); + const system_control_data = await response.json(); + config["unique_id"] = system_control_data["s0"]; + config["firmware_version"] = system_control_data["s1"]; + config["vcs_hash"] = system_control_data["s2"]; + + // Fetch data from endpoints + for (const endpoint of endpoints) { + const response = await fetch(endpoint); + const data = await response.json(); + config[endpoint] = data; + } + + // download as a file + const blob = new Blob([JSON.stringify(config, null, "\t")], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = "opentrickler"+config["unique_id"]+"_config.json"; + a.click(); + URL.revokeObjectURL(url); + } + // Start the long polling process when the page loads if (document.readyState != "loading") { onNavButtonClicked('trickler'); From 7272544a68ba2df700bfd65a9135bb16f9adcc8f Mon Sep 17 00:00:00 2001 From: Ran Bao Date: Sun, 18 Aug 2024 12:14:22 +1200 Subject: [PATCH 3/4] Fix several coding error --- src/charge_mode.cpp | 2 +- src/html/web_portal.html | 3 ++- src/wireless.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/charge_mode.cpp b/src/charge_mode.cpp index de8232a..5203fd4 100644 --- a/src/charge_mode.cpp +++ b/src/charge_mode.cpp @@ -622,7 +622,7 @@ bool http_rest_charge_mode_config(struct fs_file *file, int num_params, char *pa sizeof(charge_mode_json_buffer), "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" "{\"c1\":\"#%06lx\",\"c2\":\"#%06lx\",\"c3\":\"#%06lx\",\"c4\":\"#%06lx\"," - "\"c5\":%.3f,\"c6\":%.3f,\"c7\":%.3f,\"c8\":%.3f,\"c9\":%d,\"c10\":\"%s\",\"c11\":%ld,\"c12\":%0.3f}", + "\"c5\":%.3f,\"c6\":%.3f,\"c7\":%.3f,\"c8\":%.3f,\"c9\":%d,\"c10\":%s,\"c11\":%ld,\"c12\":%0.3f}", charge_mode_config.eeprom_charge_mode_data.neopixel_normal_charge_colour, charge_mode_config.eeprom_charge_mode_data.neopixel_under_charge_colour, charge_mode_config.eeprom_charge_mode_data.neopixel_over_charge_colour, diff --git a/src/html/web_portal.html b/src/html/web_portal.html index 02700f7..4a4d88d 100644 --- a/src/html/web_portal.html +++ b/src/html/web_portal.html @@ -1424,12 +1424,13 @@

Error

config["unique_id"] = system_control_data["s0"]; config["firmware_version"] = system_control_data["s1"]; config["vcs_hash"] = system_control_data["s2"]; + config["config"] = {}; // Fetch data from endpoints for (const endpoint of endpoints) { const response = await fetch(endpoint); const data = await response.json(); - config[endpoint] = data; + config["config"][endpoint] = data; } // download as a file diff --git a/src/wireless.c b/src/wireless.c index 5d14be2..2bd39d7 100644 --- a/src/wireless.c +++ b/src/wireless.c @@ -426,7 +426,7 @@ bool http_rest_wireless_config(struct fs_file *file, int num_params, char *param snprintf(wireless_config_json_buffer, sizeof(wireless_config_json_buffer), "%s" - "{\"w0\":\"%s\",\"w2\":\"%d\",\"w3\":%"PRId32",\"w4\":%s}", + "{\"w0\":\"%s\",\"w2\":%d,\"w3\":%"PRId32",\"w4\":%s}", http_json_header, wireless_config.eeprom_wireless_metadata.ssid, // wireless_config.eeprom_wireless_metadata.pw, // No, we don't send the password over anymore From d2b569d07b81885a4eae91fa697016fabfa2ed01 Mon Sep 17 00:00:00 2001 From: Ran Bao Date: Sun, 18 Aug 2024 14:25:31 +1200 Subject: [PATCH 4/4] Add import config --- src/html/web_portal.html | 80 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/src/html/web_portal.html b/src/html/web_portal.html index 4a4d88d..0b50580 100644 --- a/src/html/web_portal.html +++ b/src/html/web_portal.html @@ -608,7 +608,7 @@ - +
@@ -756,7 +756,7 @@

Warning

- + + + + +