From 77f53e8df5f4077850b23d5da2c6fa9ce70bc2a2 Mon Sep 17 00:00:00 2001 From: Mau Abata Date: Thu, 22 Jun 2023 14:56:29 -0500 Subject: [PATCH] Make remote update URL a config entry. --- include/config.h | 11 ++++++++++- include/config_defs.h | 28 ++++++++++++++++++++++++++++ include/update_manager.h | 4 ---- src/config.c | 3 +++ src/update_manager.c | 10 +++++++--- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/include/config.h b/include/config.h index 31b6ba1a..9a8da1d7 100644 --- a/include/config.h +++ b/include/config.h @@ -13,7 +13,7 @@ extern "C" { #define CONFIG_PATH_MAX 64 // This is just a default, others can be loaded after boot. -#define CONFIG_FILENAME "/config.json" +static const char* CONFIG_FILENAME = "/config.json"; // String Lengths #define WIFI_SSID_MAX_LEN 64 @@ -23,6 +23,10 @@ extern "C" { // #define EOM_BETA 1 #define I18N_USE_CJSON_DICT 1 +// System Defaults +static const char* REMOTE_UPDATE_URL = + "http://us-central1-maustec-io.cloudfunctions.net/gh-release-embedded-bridge"; + // Vibration Modes // See vibration_mode_controller.h for more. @@ -136,6 +140,11 @@ struct config { bool edge_menu_lock; // Deny access to menu starting after orgasm detected bool post_orgasm_menu_lock; + + //= Internal System Configuration (Update only if you know what you're doing) + + // Remote update server URL. You may change this to OTA update other versions. + char* remote_update_url; }; typedef struct config config_t; diff --git a/include/config_defs.h b/include/config_defs.h index 48f5214d..06b7e997 100644 --- a/include/config_defs.h +++ b/include/config_defs.h @@ -57,6 +57,34 @@ enum _config_def_operation { } \ } +#define CFG_STRING_PTR(name, default) \ + { \ + if (root != NULL) { \ + if (operation == CFG_SET || operation == CFG_MERGE) { \ + cJSON* item = cJSON_GetObjectItem(root, #name); \ + if (item != NULL || operation == CFG_SET) { \ + /* intentional cstring pointer comparison */ \ + if (cfg->name != default) free(cfg->name); \ + asiprintf(&cfg->name, "%s", item == NULL ? default : item->valuestring); \ + } \ + } else { \ + cJSON_AddStringToObject(root, #name, cfg->name); \ + } \ + } else if (key != NULL && !strcasecmp(key, #name)) { \ + if (operation == CFG_GET) { \ + if (out_val != NULL) strlcpy(out_val, cfg->name, len); \ + return true; \ + } else { \ + if (in_val != NULL) { \ + /* intentional cstring pointer comparison */ \ + if (cfg->name != default) free(cfg->name); \ + asiprintf(&cfg->name, "%s", in_val); \ + } \ + return true; \ + } \ + } \ + } + #define CFG_NUMBER(name, default) \ { \ if (root != NULL) { \ diff --git a/include/update_manager.h b/include/update_manager.h index 4bb760ce..11030ad0 100644 --- a/include/update_manager.h +++ b/include/update_manager.h @@ -8,10 +8,6 @@ extern "C" { #include "esp_err.h" #include "semver.h" -#define REMOTE_UPDATE_URL \ - "http://us-central1-maustec-io.cloudfunctions.net/gh-release-embedded-bridge" -#define UPDATE_FILENAME "update.bin" - enum um_update_status { UM_UPDATE_NOT_CHECKED, UM_UPDATE_AVAILABLE, diff --git a/src/config.c b/src/config.c index bd5cfecb..d543359e 100644 --- a/src/config.c +++ b/src/config.c @@ -62,6 +62,9 @@ CONFIG_DEFS { CFG_BOOL(edge_menu_lock, false); CFG_NUMBER(max_clench_duration, 100); + // Internal system things, only edit if you know what you're doing. + CFG_STRING_PTR(remote_update_url, REMOTE_UPDATE_URL) + // Please just leave this here and don't ask questions. return false; } \ No newline at end of file diff --git a/src/update_manager.c b/src/update_manager.c index f89152de..01d67d63 100644 --- a/src/update_manager.c +++ b/src/update_manager.c @@ -230,7 +230,7 @@ esp_err_t update_manager_update_from_web(um_progress_callback_t* progress_cb, vo }; esp_http_client_config_t data_config = { - .url = REMOTE_UPDATE_URL, + .url = Config.remote_update_url, .event_handler = _http_event_handler, .user_data = &callback_data, }; @@ -324,17 +324,21 @@ esp_err_t update_manager_check_latest_version(semver_t* version) { .size = &size, }; + char* url = NULL; + asiprintf(&url, "%s/%s", Config.remote_update_url, "version.txt"); + esp_http_client_config_t config = { - .url = REMOTE_UPDATE_URL "/version.txt", + .url = url, .user_data = &callback_data, .event_handler = _http_event_handler, }; - ESP_LOGI(TAG, "Checking for OTA updates: %s", REMOTE_UPDATE_URL "/version.txt"); + ESP_LOGI(TAG, "Checking for OTA updates: %s", config.url); esp_http_client_handle_t client = esp_http_client_init(&config); esp_err_t err = esp_http_client_perform(client); + free(url); if (err != ESP_OK) { ESP_LOGE(TAG, "HTTP GET failed: %s", esp_err_to_name(err));