From 3b1ab2e9e9c992b42c3cbd50bfa1d37c9312010c Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Mon, 17 Feb 2025 00:12:50 +0100 Subject: [PATCH] Updated shit --- lib/fetchOTA/fetchOTA.cpp | 10 ++++++-- lib/fetchOTA/fetchOTA.h | 10 +++----- lib/fetchOTA/utils.cpp | 9 +++++++ lib/fetchOTA/utils.h | 3 ++- src/global_data/defines.h | 2 +- src/main.cpp | 27 ++++++--------------- src/tools/tools.cpp | 42 +++++++++++++++++++++++++++++++++ src/tools/tools.h | 3 ++- test/otaTests.cpp | 49 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 123 insertions(+), 32 deletions(-) diff --git a/lib/fetchOTA/fetchOTA.cpp b/lib/fetchOTA/fetchOTA.cpp index f8df3da..575416f 100644 --- a/lib/fetchOTA/fetchOTA.cpp +++ b/lib/fetchOTA/fetchOTA.cpp @@ -9,9 +9,9 @@ #include #include -OTA::OTA(String server_url, String currentVersion, String currentDeviceConfiguration) { +OTA::OTA(String server_url, Version currentVersion, String currentDeviceConfiguration) { _serverUrl = server_url; - _currentVersion = parseVersion(currentVersion.c_str()); + _currentVersion = currentVersion; _current_device_configuration = currentDeviceConfiguration; } @@ -59,7 +59,13 @@ Firmware OTA::getLatestVersionOnServer() { return createErrorResponse("No valid configuration found in the JSON response"); } + Configuration latest = getLatestConfiguration(configs.data(), configs.size()); + + if (!isVersionNewer(_currentVersion, latest.version)) { + Log.verbose("No newer version found. Server version: %d.%d.%d", latest.version.major, latest.version.minor, latest.version.patch); + } + return Firmware{ latest.version, latest.url, diff --git a/lib/fetchOTA/fetchOTA.h b/lib/fetchOTA/fetchOTA.h index 2a9312b..58dcb3b 100644 --- a/lib/fetchOTA/fetchOTA.h +++ b/lib/fetchOTA/fetchOTA.h @@ -11,22 +11,18 @@ class OTA { public: - OTA(String server_url, String currentVersion, String currentDeviceConfiguration); + OTA(String server_url, Version currentVersion, String currentDeviceConfiguration); Firmware getLatestVersionOnServer(); - + bool checkForUpdate(); void run_ota_update(String url, std::function callback_started, std::function callback_finished, std::function callback_progress, std::function callback_error); - + private: bool _isHTTPS = false; String _serverUrl; Version _currentVersion; String _current_device_configuration; Firmware createErrorResponse(const String& errorMsg); - void update_started(); - void update_finished(); - void update_progress(int cur, int total); - void update_error(int err); }; #endif \ No newline at end of file diff --git a/lib/fetchOTA/utils.cpp b/lib/fetchOTA/utils.cpp index 878b073..3412d36 100644 --- a/lib/fetchOTA/utils.cpp +++ b/lib/fetchOTA/utils.cpp @@ -30,4 +30,13 @@ Configuration getLatestConfiguration(Configuration *configs, int count) { } } return latest; +} + +bool isVersionNewer(Version oldVersion, Version newVersion) { + if (newVersion.major > oldVersion.major || + (newVersion.major == oldVersion.major && newVersion.minor > oldVersion.minor) || + (newVersion.major == oldVersion.major && newVersion.minor == oldVersion.minor && newVersion.patch > oldVersion.patch)) { + return true; + } + return false; } \ No newline at end of file diff --git a/lib/fetchOTA/utils.h b/lib/fetchOTA/utils.h index 801c143..07df85c 100644 --- a/lib/fetchOTA/utils.h +++ b/lib/fetchOTA/utils.h @@ -28,4 +28,5 @@ struct Configuration { }; Version parseVersion(const char *versionStr); -Configuration getLatestConfiguration(Configuration *configs, int count); \ No newline at end of file +Configuration getLatestConfiguration(Configuration *configs, int count); +bool isVersionNewer(Version oldVersion, Version newVersion); \ No newline at end of file diff --git a/src/global_data/defines.h b/src/global_data/defines.h index 1a7188f..547b13a 100644 --- a/src/global_data/defines.h +++ b/src/global_data/defines.h @@ -7,6 +7,6 @@ #define water_level_min_key "water_level_min" #define water_level_max_key "water_level_max" #define water_volume_key "water_volume" -#define current_software_version Version{0, 0, 7} +#define current_software_version Version{0, 0, 8} #define RESISTOR_VALUE 4 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a13e921..d40a984 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,30 +37,16 @@ extern "C" int rom_phy_get_vdd33(); AsyncWebServer server(80); #define FORMAT_LITTLEFS_IF_FAILED true -void update_started() { - Log.verbose("OTA Update started"); -} -void update_finished() { - Log.verbose("OTA Update finished"); -} - -void update_progress(int cur, int total) { - Log.verbose("OTA Update progress: %d/%d", cur, total); -} - -void update_error(int err) { - Log.error("OTA Update error: %d", err); -} void run_ota(void* parameter) { - OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", "1.1.1", "INA233"); - Firmware fw = ota.getLatestVersionOnServer(); - Log.verbose("we are done"); - Serial.printf("Firmware Info: Valid: %d, Version: %d.%d.%d, URL: %s\n", fw.valid, fw.version.major, fw.version.minor, fw.version.patch, fw.url.c_str()); - Log.verbose("Error message: %s", fw.error.c_str()); - ota.run_ota_update(fw.url, update_started, update_finished, update_progress, update_error); + // OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", "1.1.1", "INA233"); + // Firmware fw = ota.getLatestVersionOnServer(); + // Log.verbose("we are done"); + // Serial.printf("Firmware Info: Valid: %d, Version: %d.%d.%d, URL: %s\n", fw.valid, fw.version.major, fw.version.minor, fw.version.patch, fw.url.c_str()); + // Log.verbose("Error message: %s", fw.error.c_str()); + // ota.run_ota_update(fw.url, update_started, update_finished, update_progress, update_error); vTaskDelete(NULL); } @@ -234,6 +220,7 @@ void setup() xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL); xTaskCreate(read_sensor_task, "ReadSensorTask", 2048, NULL, 1, NULL); xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL); + xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); } void loop() diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index 65d0613..cc8907c 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -3,8 +3,10 @@ #include #include "../global_data/defines.h" #include +#include extern Preferences prefs; +extern OTAStatus ota_status; void printSuffix(Print* _logOutput, int logLevel) { @@ -37,4 +39,44 @@ String processor(const String& var) } return String("Test"); +} + +// OTA Callbacks +void update_started() { + Log.verbose("OTA Update started"); + ota_status.update_progress = 0; +} + +void update_finished() { + Log.verbose("OTA Update finished"); + ota_status.update_progress = -1; +} + +void update_progress(int cur, int total) { + ota_status.update_progress = total/cur; + Log.verbose("OTA Update progress: %d/%d", cur, total); +} + +void update_error(int err) { + Log.error("OTA Update error: %d", err); + ota_status.update_progress = -2; +} + +void check_update_task(void* parameter) { + #ifdef USE_INA226 + OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA226"); + #else + OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA233"); + #endif + while (true) { + Firmware fw = ota.getLatestVersionOnServer(); + if (fw.valid) { + Log.verbose("New firmware available: %d.%d.%d, current versio: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); + ota_status.update_available = true; + ota_status.latest_version = fw.version; + } else { + Log.verbose("No new firmware available"); + } + delay(1000 * 60 * 1); + } } \ No newline at end of file diff --git a/src/tools/tools.h b/src/tools/tools.h index b5f3618..abfdfd4 100644 --- a/src/tools/tools.h +++ b/src/tools/tools.h @@ -4,4 +4,5 @@ void printSuffix(Print* _logOutput, int logLevel); void print_prefix(Print* _logOutput, int logLevel); bool is_error(ActiveErrors active_errors); -String processor(const String& var); \ No newline at end of file +String processor(const String& var); +void check_update_task(void* parameter); \ No newline at end of file diff --git a/test/otaTests.cpp b/test/otaTests.cpp index ae73a07..ddd9a13 100644 --- a/test/otaTests.cpp +++ b/test/otaTests.cpp @@ -84,6 +84,48 @@ void test_parse_version_extra_content() { TEST_ASSERT_EQUAL(0, v.patch); } +void test_is_version_newer_major() { + Version oldVersion = {1, 3, 4}; + Version newVersion = {2, 1, 1}; + TEST_ASSERT_TRUE(isVersionNewer(oldVersion, newVersion)); +} + +void test_is_version_newer_minor() { + Version oldVersion = {1, 0, 8}; + Version newVersion = {1, 1, 0}; + TEST_ASSERT_TRUE(isVersionNewer(oldVersion, newVersion)); +} + +void test_is_version_newer_patch() { + Version oldVersion = {1, 1, 1}; + Version newVersion = {1, 1, 2}; + TEST_ASSERT_TRUE(isVersionNewer(oldVersion, newVersion)); +} + +void test_is_version_not_newer_same() { + Version oldVersion = {1, 1, 1}; + Version newVersion = {1, 1, 1}; + TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion)); +} + +void test_is_version_not_newer_major() { + Version oldVersion = {2, 0, 0}; + Version newVersion = {1, 99, 99}; + TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion)); +} + +void test_is_version_not_newer_minor() { + Version oldVersion = {1, 2, 0}; + Version newVersion = {1, 1, 99}; + TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion)); +} + +void test_is_version_not_newer_patch() { + Version oldVersion = {1, 1, 2}; + Version newVersion = {1, 1, 1}; + TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion)); +} + int main() { UNITY_BEGIN(); @@ -96,5 +138,12 @@ int main() { RUN_TEST(test_parse_version_extra_content); RUN_TEST(test_parse_version_valid_input_2); RUN_TEST(test_parse_version_more_missing_parts); + RUN_TEST(test_is_version_newer_major); + RUN_TEST(test_is_version_newer_minor); + RUN_TEST(test_is_version_newer_patch); + RUN_TEST(test_is_version_not_newer_same); + RUN_TEST(test_is_version_not_newer_major); + RUN_TEST(test_is_version_not_newer_minor); + RUN_TEST(test_is_version_not_newer_patch); UNITY_END(); } \ No newline at end of file