diff --git a/.github/actions/build-native-binary/action.yaml b/.github/actions/build-native-binary/action.yaml index f81283f..ce7ef57 100644 --- a/.github/actions/build-native-binary/action.yaml +++ b/.github/actions/build-native-binary/action.yaml @@ -135,7 +135,7 @@ runs: if: ${{ inputs.arch == 'amd64' }} run: | cd dist_${{ inputs.arch }}/utest - LD_LIBRARY_PATH=../lib ./TestSelfUpdateAgent > ../../unit_tests_report_${{ inputs.arch }}.txt --gtest_output=xml:../../unit_tests_report_${{ inputs.arch }}.xml + LD_LIBRARY_PATH=../lib SUA_MESSAGE_DELAY=0 ./TestSelfUpdateAgent > ../../unit_tests_report_${{ inputs.arch }}.txt --gtest_output=xml:../../unit_tests_report_${{ inputs.arch }}.xml shell: bash - uses: uraimo/run-on-arch-action@v2 @@ -157,13 +157,13 @@ runs: echo "\nextra/selfupdateagent.crt" | tee -a /etc/ca-certificates.conf > /dev/null update-ca-certificates cd /sua/dist_arm64/utest - LD_LIBRARY_PATH=../lib ./TestSelfUpdateAgent > ../../unit_tests_report_arm64.txt --gtest_output=xml:../../unit_tests_report_arm64.xml + LD_LIBRARY_PATH=../lib SUA_MESSAGE_DELAY=0 ./TestSelfUpdateAgent > ../../unit_tests_report_arm64.txt --gtest_output=xml:../../unit_tests_report_arm64.xml - name: Collect code-coverage for amd64 if: ${{ inputs.arch == 'amd64' }} run: | cd dist_${{ inputs.arch }}_codecov/utest - LD_LIBRARY_PATH=../lib ./TestSelfUpdateAgent + LD_LIBRARY_PATH=../lib SUA_MESSAGE_DELAY=0 ./TestSelfUpdateAgent shell: bash - name: Generate code-coverage report diff --git a/src/Context.h b/src/Context.h index 791ec7d..56f4d9e 100644 --- a/src/Context.h +++ b/src/Context.h @@ -62,6 +62,7 @@ namespace sua { std::string caFilepath = SUA_DEFAULT_CA_FILEPATH; bool downloadMode = true; bool fallbackMode = false; + int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds DesiredState desiredState; CurrentState currentState; diff --git a/src/Defaults.cpp b/src/Defaults.cpp index fdb1cf6..af0208f 100644 --- a/src/Defaults.cpp +++ b/src/Defaults.cpp @@ -16,11 +16,12 @@ #include "Defaults.h" -const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp"; -const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto"; -const int SUA_DEFAULT_MQTT_PORT = 1883; -const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883"; -const std::string SUA_DEFAULT_MODE = "download"; -const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates"; -const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs"; -const std::string SUA_DEFAULT_CA_FILEPATH = ""; +const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp"; +const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto"; +const int SUA_DEFAULT_MQTT_PORT = 1883; +const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883"; +const std::string SUA_DEFAULT_MODE = "download"; +const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates"; +const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs"; +const std::string SUA_DEFAULT_CA_FILEPATH = ""; +const int SUA_DEFAULT_FEEDBACK_INTERVAL = 5; diff --git a/src/Defaults.h b/src/Defaults.h index 192b746..a0a7215 100644 --- a/src/Defaults.h +++ b/src/Defaults.h @@ -27,5 +27,6 @@ extern const std::string SUA_DEFAULT_MODE; extern const std::string SUA_DEFAULT_TEMP_DIRECTORY; extern const std::string SUA_DEFAULT_CA_DIRECTORY; extern const std::string SUA_DEFAULT_CA_FILEPATH; +extern const int SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds #endif diff --git a/src/FSM/States/Downloading.cpp b/src/FSM/States/Downloading.cpp index eb2d305..bbdc8c6 100644 --- a/src/FSM/States/Downloading.cpp +++ b/src/FSM/States/Downloading.cpp @@ -22,6 +22,8 @@ #include "FotaEvent.h" #include "Logger.h" +#include + namespace sua { Downloading::Downloading() @@ -49,8 +51,15 @@ namespace sua { ctx.desiredState.downloadBytesDownloaded = std::stoll(payload.at("downloaded")); ctx.desiredState.downloadProgressPercentage = std::stoi(payload.at("percentage")); + const auto now = std::chrono::system_clock::now().time_since_epoch(); + const auto now_in_seconds = std::chrono::duration_cast(now).count(); + Logger::info("Download progress: {}%", ctx.desiredState.downloadProgressPercentage); - send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Downloading); + + if((ctx.desiredState.downloadProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) { + send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Downloading); + _timeLastUpdate = now_in_seconds; + } }); if (true == ctx.downloadMode) { diff --git a/src/FSM/States/Downloading.h b/src/FSM/States/Downloading.h index 8d9deba..f637240 100644 --- a/src/FSM/States/Downloading.h +++ b/src/FSM/States/Downloading.h @@ -32,6 +32,9 @@ namespace sua { void onEnter(Context& ctx) override; FotaEvent body(Context& ctx) override; + + private: + long long _timeLastUpdate = 0; }; } // namespace sua diff --git a/src/FSM/States/Installing.cpp b/src/FSM/States/Installing.cpp index 17c3a21..bebb4ef 100644 --- a/src/FSM/States/Installing.cpp +++ b/src/FSM/States/Installing.cpp @@ -21,6 +21,8 @@ #include "Context.h" #include "Logger.h" +#include + namespace sua { Installing::Installing() @@ -37,7 +39,14 @@ namespace sua { subscribe(Installer::EVENT_INSTALLING, [this, &ctx](const std::map& payload) { ctx.desiredState.installProgressPercentage = std::stoi(payload.at("percentage")); Logger::info("Install progress: {}%", ctx.desiredState.installProgressPercentage); - send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Installing); + + const auto now = std::chrono::system_clock::now().time_since_epoch(); + const auto now_in_seconds = std::chrono::duration_cast(now).count(); + + if((ctx.desiredState.installProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) { + send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Installing); + _timeLastUpdate = now_in_seconds; + } }); std::string install_input; diff --git a/src/FSM/States/Installing.h b/src/FSM/States/Installing.h index 059531f..cac374f 100644 --- a/src/FSM/States/Installing.h +++ b/src/FSM/States/Installing.h @@ -32,6 +32,9 @@ namespace sua { void onEnter(Context& ctx) override; FotaEvent body(Context& ctx) override; + + private: + long long _timeLastUpdate = 0; }; } // namespace sua diff --git a/src/main.cpp b/src/main.cpp index a1c5b5c..ba56ecf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,12 +68,18 @@ int main(int argc, char* argv[]) std::string caDirectory = SUA_DEFAULT_CA_DIRECTORY; std::string caFilepath = SUA_DEFAULT_CA_FILEPATH; std::string help_string = fmt::format(help_string_template, server, installer, hostPathToSelfupdateDir, server, caDirectory, caFilepath); + int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL; const char * env_server = std::getenv("SUA_SERVER"); if(env_server) { server = env_server; } + const char * env_interval = std::getenv("SUA_MESSAGE_DELAY"); + if(env_interval) { + feedbackInterval = std::stoi(env_interval); + } + if(argc > 1) { for(int i = 1; i < argc; i++) { const std::string opt(argv[i]); @@ -223,6 +229,7 @@ int main(int argc, char* argv[]) ctx.messagingProtocol = std::make_shared(); ctx.bundleChecker = std::make_shared(); ctx.mqttProcessor = std::make_shared(ctx); + ctx.feedbackInterval = feedbackInterval; sua::Logger::info("SUA build number : '{}'", SUA_BUILD_NUMBER ); sua::Logger::info("SUA commit hash : '{}'", SUA_COMMIT_HASH ); diff --git a/utest/TestSelfUpdateScenarios.cpp b/utest/TestSelfUpdateScenarios.cpp index b7c8184..ca4fe9d 100644 --- a/utest/TestSelfUpdateScenarios.cpp +++ b/utest/TestSelfUpdateScenarios.cpp @@ -191,6 +191,8 @@ namespace { ctx().messagingProtocol = std::make_shared(); ctx().bundleChecker = std::make_shared(); + + ctx().feedbackInterval = 0; } void TearDown() override {