From 6e76dc3e5f1381725ee1de3c11c35d74fd9242d2 Mon Sep 17 00:00:00 2001 From: Mateusz Szczesny Date: Tue, 7 Nov 2023 17:47:58 +0100 Subject: [PATCH] [BH-1787] Reboot to MSC endpoint To allow the automation of gathering logs and swapping out OS images during tests a MSC reboot endpoint was added. Together with a change to ecoboot, this allows the sending of a "reboot to MSC" command. The deivce will then reboot and enter MSC mode. --- .../board/rt1051/bsp/lpm/RT1051LPMCommon.cpp | 3 + module-bsp/bsp/lpm/bsp_lpm.hpp | 3 +- .../service-desktop/endpoints/CMakeLists.txt | 4 + .../developerMode/DeveloperModeHelper.cpp | 6 +- .../deviceInfo/DeviceInfoEndpointCommon.cpp | 6 +- .../factoryReset/FactoryResetEndpoint.cpp | 10 +- .../endpoints/filesystem/FS_Helper.cpp | 20 +-- .../endpoints/include/endpoints/Context.hpp | 2 +- .../include/endpoints/EndpointType.hpp | 9 +- .../include/endpoints/JsonKeyNames.hpp | 115 +++++++++--------- .../endpoints/reboot/RebootEndpoint.hpp | 26 ++++ .../include/endpoints/reboot/RebootHelper.hpp | 20 +++ .../endpoints/reboot/RebootEndpoint.cpp | 34 ++++++ .../endpoints/reboot/RebootHelper.cpp | 36 ++++++ .../security/SecurityEndpointHelper.cpp | 2 +- .../endpoints/update/UpdateHelper.cpp | 2 +- .../include/service-desktop/Sync.hpp | 6 +- module-sys/SystemManager/PowerManager.cpp | 13 +- .../SystemManager/SystemManagerCommon.cpp | 32 +++++ .../include/SystemManager/PowerManager.hpp | 7 +- .../SystemManager/SystemManagerCommon.hpp | 12 +- module-sys/common/include/system/Common.hpp | 1 + .../desktop/endpoints/EndpointFactoryBell.cpp | 3 + .../deviceInfo/DeviceInfoEndpoint.cpp | 37 +++--- .../deviceInfo/DeviceInfoEndpoint.cpp | 52 ++++---- 25 files changed, 322 insertions(+), 139 deletions(-) create mode 100644 module-services/service-desktop/endpoints/include/endpoints/reboot/RebootEndpoint.hpp create mode 100644 module-services/service-desktop/endpoints/include/endpoints/reboot/RebootHelper.hpp create mode 100644 module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp create mode 100644 module-services/service-desktop/endpoints/reboot/RebootHelper.cpp diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPMCommon.cpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPMCommon.cpp index c947a4cca3f990556e31bfe81762b2722eca42e7..cd984168c257c7f95de1a19ac462d8708a3f2d7b 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPMCommon.cpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPMCommon.cpp @@ -47,6 +47,9 @@ namespace bsp case RebootType::NormalRestart: set_boot_reason(boot_reason_code_os); break; + case RebootType::GoToMSC: + set_boot_reason(boot_reason_code_usb_mc_mode); + break; default: set_boot_reason(boot_reason_code_unknown); } diff --git a/module-bsp/bsp/lpm/bsp_lpm.hpp b/module-bsp/bsp/lpm/bsp_lpm.hpp index 4b97703a78d6ad83f0ca6e67dd9e1a8d59d8f7c1..0bb0bd0c130133467dcd233a2f680a3ee3a24f66 100644 --- a/module-bsp/bsp/lpm/bsp_lpm.hpp +++ b/module-bsp/bsp/lpm/bsp_lpm.hpp @@ -30,7 +30,8 @@ namespace bsp GoToRecoveryFactoryReset, //! GOto recovery into the factory reset mode GoToRecoveryRecovery, //! Goto to recovery into recovery mode GoToRecoveryBackup, //! Goto to recovery into backup mode - GoToRecoveryRestore //! Goto to recovery into restore mode + GoToRecoveryRestore, //! Goto to recovery into restore mode + GoToMSC //! Goto msc mode }; LowPowerMode() = default; diff --git a/module-services/service-desktop/endpoints/CMakeLists.txt b/module-services/service-desktop/endpoints/CMakeLists.txt index 72493a4e1c674783fcfb9533d2d228d2f6cb5dd4..3500a560523d8964be6000e0934a7f95972ac11b 100644 --- a/module-services/service-desktop/endpoints/CMakeLists.txt +++ b/module-services/service-desktop/endpoints/CMakeLists.txt @@ -63,6 +63,8 @@ target_sources( security/SecurityEndpointHelper.cpp update/UpdateEndpoint.cpp update/UpdateHelper.cpp + reboot/RebootEndpoint.cpp + reboot/RebootHelper.cpp PUBLIC include/endpoints/backup/BackupEndpoint.hpp include/endpoints/backup/BackupHelper.hpp @@ -88,6 +90,8 @@ target_sources( include/endpoints/security/SecurityEndpointHelper.hpp include/endpoints/update/UpdateEndpoint.hpp include/endpoints/update/UpdateHelper.hpp + include/endpoints/reboot/RebootEndpoint.hpp + include/endpoints/reboot/RebootHelper.hpp ) target_include_directories( diff --git a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp index 2c9c030c902fd40e463c43636f0c2fcd6112f60a..cf50fea4f1aafff24fadf48ad77eea423b9d8a44 100644 --- a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp +++ b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp @@ -188,9 +188,9 @@ namespace sdesktop::endpoints if (keyValue == json::developerMode::simStateInfo) { auto response = ResponseContext{ .body = json11::Json::object( - {{json::selectedSim, std::to_string(static_cast(Store::GSM::get()->selected))}, - {json::sim, std::to_string(static_cast(Store::GSM::get()->sim))}, - {json::trayState, std::to_string(static_cast(Store::GSM::get()->tray))}})}; + {{json::deviceInfo::selectedSim, std::to_string(static_cast(Store::GSM::get()->selected))}, + {json::deviceInfo::sim, std::to_string(static_cast(Store::GSM::get()->sim))}, + {json::deviceInfo::trayState, std::to_string(static_cast(Store::GSM::get()->tray))}})}; response.status = http::Code::OK; return {sent::no, std::move(response)}; } diff --git a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp index cd1ddbbcf509ca0027f74e1a1cce4ad4f5951e23..3c8da63d040f4a38f0a8460b0280706bcaeed4ca 100644 --- a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp +++ b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp @@ -26,9 +26,9 @@ namespace sdesktop::endpoints auto DeviceInfoEndpointCommon::handleGet(Context &context) -> http::Code { const auto &requestBody = context.getBody(); - if (not requestBody.object_items().empty() and requestBody[json::fileList].is_number()) { + if (not requestBody.object_items().empty() and requestBody[json::deviceInfo::fileList].is_number()) { - const auto diagFileType = parseDiagnosticFileType(requestBody[json::fileList]); + const auto diagFileType = parseDiagnosticFileType(requestBody[json::deviceInfo::fileList]); if (!magic_enum::enum_contains(diagFileType)) { LOG_ERROR("Bad diagnostic type '%s' requested", magic_enum::enum_name(diagFileType).data()); @@ -100,7 +100,7 @@ namespace sdesktop::endpoints fileArray.push_back(file); } - return json11::Json::object{{json::files, fileArray}}; + return json11::Json::object{{json::deviceInfo::files, fileArray}}; } auto DeviceInfoEndpointCommon::listDirectory(const std::string &path) -> std::vector diff --git a/module-services/service-desktop/endpoints/factoryReset/FactoryResetEndpoint.cpp b/module-services/service-desktop/endpoints/factoryReset/FactoryResetEndpoint.cpp index 10d94d36e80f0515d408423dcdfb1b8f8c4b1684..e17519c2c21bcd48e4c32962a6d42b163ae3de12 100644 --- a/module-services/service-desktop/endpoints/factoryReset/FactoryResetEndpoint.cpp +++ b/module-services/service-desktop/endpoints/factoryReset/FactoryResetEndpoint.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include @@ -19,14 +19,14 @@ namespace sdesktop::endpoints { if (context.getMethod() == http::Method::post) { - if (context.getBody()[json::factoryRequest] == true) { + if (context.getBody()[json::factoryReset::factoryRequest] == true) { auto msg = std::make_shared(); ownerServicePtr->bus.sendUnicast(msg, service::name::service_desktop); - context.setResponseBody(json11::Json::object({{json::factoryRequest, true}})); + context.setResponseBody(json11::Json::object({{json::factoryReset::factoryRequest, true}})); } else { - context.setResponseBody(json11::Json::object({{json::factoryRequest, false}})); + context.setResponseBody(json11::Json::object({{json::factoryReset::factoryRequest, false}})); } sender::putToSendQueue(context.createSimpleResponse()); @@ -34,7 +34,7 @@ namespace sdesktop::endpoints return; } else { - context.setResponseBody(json11::Json::object({{json::factoryRequest, false}})); + context.setResponseBody(json11::Json::object({{json::factoryReset::factoryRequest, false}})); sender::putToSendQueue(context.createSimpleResponse()); diff --git a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp index cf1a451c20b15d535722f015be506fb309c0704b..125bae716ea92a9a370e5ee1c529413a0ec72ad4 100644 --- a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp +++ b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp @@ -138,14 +138,14 @@ namespace sdesktop::endpoints catch (const std::runtime_error &e) { LOG_ERROR("Logs flush exception: %s", e.what()); - json11::Json::object response({{json::reason, e.what()}}); + json11::Json::object response({{json::common::reason, e.what()}}); return ResponseContext{.status = http::Code::InternalServerError, .body = response}; } if (!std::filesystem::exists(filePath)) { LOG_ERROR("File %s not found", filePath.c_str()); - json11::Json::object response({{json::reason, json::fs::fileDoesNotExist}}); + json11::Json::object response({{json::common::reason, json::fs::fileDoesNotExist}}); return ResponseContext{.status = http::Code::NotFound, .body = response}; } @@ -166,13 +166,13 @@ namespace sdesktop::endpoints LOG_ERROR("FileOperations exception: %s", e.what()); code = http::Code::InternalServerError; - response = json11::Json::object({{json::reason, std::string(e.what())}}); + response = json11::Json::object({{json::common::reason, std::string(e.what())}}); } catch (std::exception &e) { LOG_ERROR("FileOperations exception: %s", e.what()); code = http::Code::BadRequest; - response = json11::Json::object({{json::reason, std::string(e.what())}}); + response = json11::Json::object({{json::common::reason, std::string(e.what())}}); } return ResponseContext{.status = code, .body = response}; @@ -190,7 +190,7 @@ namespace sdesktop::endpoints catch (std::exception &e) { LOG_ERROR("Exception during getting data: %s", e.what()); - json11::Json::object response({{json::reason, e.what()}}); + json11::Json::object response({{json::common::reason, e.what()}}); return ResponseContext{.status = http::Code::BadRequest, .body = response}; } @@ -213,7 +213,7 @@ namespace sdesktop::endpoints LOG_ERROR("%s", errorReason.str().c_str()); code = http::Code::BadRequest; - response = json11::Json::object({{json::reason, errorReason.str()}}); + response = json11::Json::object({{json::common::reason, errorReason.str()}}); } return ResponseContext{.status = code, .body = response}; @@ -263,13 +263,13 @@ namespace sdesktop::endpoints LOG_ERROR("FileOperations exception: %s", e.what()); code = http::Code::InternalServerError; - response = json11::Json::object({{json::reason, std::string(e.what())}}); + response = json11::Json::object({{json::common::reason, std::string(e.what())}}); } catch (std::exception &e) { LOG_ERROR("FileOperations exception: %s", e.what()); code = http::Code::BadRequest; - response = json11::Json::object({{json::reason, std::string(e.what())}}); + response = json11::Json::object({{json::common::reason, std::string(e.what())}}); } return ResponseContext{.status = code, .body = response}; @@ -288,7 +288,7 @@ namespace sdesktop::endpoints LOG_ERROR("%s", errorReason.str().c_str()); auto code = http::Code::BadRequest; - auto response = json11::Json::object({{json::reason, errorReason.str()}}); + auto response = json11::Json::object({{json::common::reason, errorReason.str()}}); return ResponseContext{.status = code, .body = response}; } @@ -301,7 +301,7 @@ namespace sdesktop::endpoints catch (std::exception &e) { LOG_ERROR("Exception during sending data: %s", e.what()); auto code = http::Code::NotAcceptable; - auto response = json11::Json::object({{json::reason, e.what()}}); + auto response = json11::Json::object({{json::common::reason, e.what()}}); return ResponseContext{.status = code, .body = response}; } diff --git a/module-services/service-desktop/endpoints/include/endpoints/Context.hpp b/module-services/service-desktop/endpoints/include/endpoints/Context.hpp index 3035e9adb2f5dc80368bbddb69e999213ca28a5c..d32b556f914f45e01f3871d839738612263668d3 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/Context.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/Context.hpp @@ -47,7 +47,7 @@ namespace sdesktop::endpoints if (!body.is_object()) { body = json11::Json(); } - if (static_cast(endpoint) > lastEndpoint) { + if (static_cast(endpoint) > lastEndpoint) { endpoint = EndpointType::invalid; } if (method > http::Method::del) { diff --git a/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp b/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp index 50de5f6d3d3ef5fa8d68f674b9c1c96f89297330..48ebc23779a2f3d70eedde8be7d76ceec43d74b8 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp @@ -1,8 +1,10 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once +#include + namespace sdesktop::endpoints { @@ -22,9 +24,10 @@ namespace sdesktop::endpoints developerMode, bluetooth, usbSecurity, - outbox + outbox, + reboot }; - inline constexpr auto lastEndpoint = static_cast(EndpointType::outbox); + inline constexpr auto lastEndpoint = magic_enum::enum_count() - 1; } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp b/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp index f0cc64a9488e8fdc390284b3ead6ae99cd3453d1..440376d5c2f933369b3d13608f1a631be2e7acd4 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp @@ -5,68 +5,57 @@ namespace sdesktop::endpoints::json { - inline constexpr auto batteryLevel = "batteryLevel"; - inline constexpr auto batteryState = "batteryState"; - inline constexpr auto selectedSim = "selectedSim"; - inline constexpr auto sim = "sim"; - inline constexpr auto trayState = "trayState"; - inline constexpr auto signalStrength = "signalStrength"; - inline constexpr auto deviceSpaceTotal = "deviceSpaceTotal"; - inline constexpr auto systemReservedSpace = "systemReservedSpace"; - inline constexpr auto usedUserSpace = "usedUserSpace"; - inline constexpr auto gitRevision = "gitRevision"; - inline constexpr auto gitBranch = "gitBranch"; - inline constexpr auto gitTag = "gitTag"; - inline constexpr auto currentRTCTime = "currentRTCTime"; - inline constexpr auto updateReady = "updateReady"; - inline constexpr auto updateFileList = "updateFileList"; - inline constexpr auto factoryRequest = "factoryRequest"; - inline constexpr auto networkStatus = "networkStatus"; - inline constexpr auto networkOperatorName = "networkOperatorName"; - inline constexpr auto accessTechnology = "accessTechnology"; + namespace common + { + inline constexpr auto reason = "reason"; + } // namespace common + + namespace deviceInfo + { + inline constexpr auto batteryLevel = "batteryLevel"; + inline constexpr auto batteryState = "batteryState"; + inline constexpr auto selectedSim = "selectedSim"; + inline constexpr auto sim = "sim"; + inline constexpr auto trayState = "trayState"; + inline constexpr auto signalStrength = "signalStrength"; + inline constexpr auto deviceSpaceTotal = "deviceSpaceTotal"; + inline constexpr auto systemReservedSpace = "systemReservedSpace"; + inline constexpr auto usedUserSpace = "usedUserSpace"; + inline constexpr auto gitRevision = "gitRevision"; + inline constexpr auto gitBranch = "gitBranch"; + inline constexpr auto currentRTCTime = "currentRTCTime"; + inline constexpr auto networkStatus = "networkStatus"; + inline constexpr auto networkOperatorName = "networkOperatorName"; + inline constexpr auto accessTechnology = "accessTechnology"; + inline constexpr auto version = "version"; + inline constexpr auto serialNumber = "serialNumber"; + inline constexpr auto caseColour = "caseColour"; + inline constexpr auto fileList = "fileList"; + inline constexpr auto files = "files"; + inline constexpr auto recoveryStatusFilePath = "recoveryStatusFilePath"; + inline constexpr auto updateFilePath = "updateFilePath"; + inline constexpr auto backupFilePath = "backupFilePath"; + inline constexpr auto syncFilePath = "syncFilePath"; + inline constexpr auto mtpPath = "mtpPath"; + inline constexpr auto deviceToken = "deviceToken"; + inline constexpr auto onboardingState = "onboardingState"; + } // namespace deviceInfo + + namespace factoryReset + { + inline constexpr auto factoryRequest = "factoryRequest"; + } // namespace factoryReset - inline constexpr auto update = "update"; - inline constexpr auto updateInfo = "updateInfo"; - inline constexpr auto updateError = "updateError"; - inline constexpr auto errorCode = "errorCode"; - inline constexpr auto statusCode = "statusCode"; - inline constexpr auto updateHistory = "updateHistory"; - inline constexpr auto usbMscMode = "usbMscMode"; - inline constexpr auto versionString = "string"; - inline constexpr auto fileExists = "fileExists"; - inline constexpr auto boot = "boot"; - inline constexpr auto version = "version"; - inline constexpr auto taskId = "id"; - inline constexpr auto state = "state"; - inline constexpr auto success = "success"; - inline constexpr auto reboot = "reboot"; - inline constexpr auto request = "request"; - inline constexpr auto restore = "restore"; - inline constexpr auto finished = "finished"; - inline constexpr auto pending = "pending"; - inline constexpr auto location = "location"; - inline constexpr auto reason = "reason"; - inline constexpr auto serialNumber = "serialNumber"; - inline constexpr auto caseColour = "caseColour"; - inline constexpr auto fileList = "fileList"; - inline constexpr auto files = "files"; - inline constexpr auto recoveryStatusFilePath = "recoveryStatusFilePath"; - inline constexpr auto updateFilePath = "updateFilePath"; - inline constexpr auto backupFilePath = "backupFilePath"; - inline constexpr auto syncFilePath = "syncFilePath"; - inline constexpr auto mtpPath = "mtpPath"; - inline constexpr auto deviceToken = "deviceToken"; - inline constexpr auto onboardingState = "onboardingState"; + namespace update + { + inline constexpr auto update = "update"; + inline constexpr auto reboot = "reboot"; + } // namespace update - namespace updateprocess + namespace sync { - inline constexpr auto command = "command"; - inline constexpr auto updateAborted = "updateAborted"; - namespace commands - { - inline constexpr auto abort = "abort"; - } // namespace commands - } // namespace updateprocess + inline constexpr auto state = "state"; + } // namespace sync namespace messages { @@ -125,4 +114,12 @@ namespace sdesktop::endpoints::json inline constexpr auto timeLeftToNextAttempt = "timeLeftToNextAttempt"; } // namespace usb + namespace reboot + { + inline constexpr auto rebootType = "rebootType"; + inline constexpr auto msc = "msc"; + inline constexpr auto reboot = "reboot"; + inline constexpr auto shutdown = "shutdown"; + } // namespace reboot + } // namespace sdesktop::endpoints::json diff --git a/module-services/service-desktop/endpoints/include/endpoints/reboot/RebootEndpoint.hpp b/module-services/service-desktop/endpoints/include/endpoints/reboot/RebootEndpoint.hpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd3ba85a32f2dbb06c327c7815bdab7f623a1f7 --- /dev/null +++ b/module-services/service-desktop/endpoints/include/endpoints/reboot/RebootEndpoint.hpp @@ -0,0 +1,26 @@ +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include +#include "RebootHelper.hpp" + +namespace sdesktop::endpoints +{ + class RebootEndpoint : public Endpoint + { + public: + explicit RebootEndpoint(sys::Service *ownerServicePtr) + : Endpoint(ownerServicePtr), helper(std::make_unique(ownerServicePtr)) + { + debugName = "RebootEndpoint"; + } + + auto handle(Context &context) -> void override; + + private: + const std::unique_ptr helper; + }; + +} // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/include/endpoints/reboot/RebootHelper.hpp b/module-services/service-desktop/endpoints/include/endpoints/reboot/RebootHelper.hpp new file mode 100644 index 0000000000000000000000000000000000000000..8974b0f41079060281e29a0a902bd248fa136515 --- /dev/null +++ b/module-services/service-desktop/endpoints/include/endpoints/reboot/RebootHelper.hpp @@ -0,0 +1,20 @@ +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include + +namespace sdesktop::endpoints +{ + class RebootHelper : public BaseHelper + { + public: + explicit RebootHelper(sys::Service *p) : BaseHelper(p) + {} + + auto processPost(Context &context) -> ProcessResult final; + + private: + }; +} // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp b/module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7c87db4b6d0f8d81b2ef9fa130991769e2ba60f --- /dev/null +++ b/module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp @@ -0,0 +1,34 @@ +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include +#include +#include +#include + +namespace sdesktop::endpoints +{ + auto RebootEndpoint::handle(Context &context) -> void + { + const auto &[sent, response] = helper->process(context.getMethod(), context); + + if (sent == sent::delayed) { + LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); + } + if (sent == sent::no) { + if (not response.has_value()) { + LOG_ERROR("Response not sent & response not created : respond with error"); + context.setResponseStatus(http::Code::NotAcceptable); + } + else { + context.setResponse(response.value()); + } + + sender::putToSendQueue(context.createSimpleResponse()); + } + if (sent == sent::yes and response.has_value()) { + LOG_ERROR("Response set when we already handled response in handler"); + } + } + +} // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/reboot/RebootHelper.cpp b/module-services/service-desktop/endpoints/reboot/RebootHelper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e0f386ff96b830f6a6374905c43284de1fd98d7 --- /dev/null +++ b/module-services/service-desktop/endpoints/reboot/RebootHelper.cpp @@ -0,0 +1,36 @@ +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include +#include +#include +#include +#include +#include + +#include + +namespace sdesktop::endpoints +{ + using sender::putToSendQueue; + auto RebootHelper::processPost(Context &context) -> ProcessResult + { + const auto rebootType = context.getBody()[json::reboot::rebootType].string_value(); + + if (rebootType == json::reboot::msc) { + sys::SystemManagerCommon::RebootMSC(owner); + return {sent::no, ResponseContext{.status = http::Code::Accepted}}; + } + else if (rebootType == json::reboot::reboot) { + sys::SystemManagerCommon::Reboot(owner); + return {sent::no, ResponseContext{.status = http::Code::Accepted}}; + } + else if (rebootType == json::reboot::shutdown) { + sys::SystemManagerCommon::RegularPowerDown(owner); + return {sent::no, ResponseContext{.status = http::Code::Accepted}}; + } + LOG_ERROR("Invalid request: %s", rebootType.c_str()); + return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + } + +} // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp b/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp index 06d898ee503923ff77a5d5bce2d93bb4dd4f1178..f05f9e221e23fd20acdeac4765a19c3b62667e07 100644 --- a/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp +++ b/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp @@ -68,7 +68,7 @@ namespace sdesktop::endpoints case BlockReason::BatteryCriticalLevel: responseContext.status = http::Code::Locked; responseContext.body = - json11::Json::object({{json::reason, std::to_string(static_cast(security.reason))}}); + json11::Json::object({{json::common::reason, std::to_string(static_cast(security.reason))}}); break; } } diff --git a/module-services/service-desktop/endpoints/update/UpdateHelper.cpp b/module-services/service-desktop/endpoints/update/UpdateHelper.cpp index 2f36174bad2ce92a7ccb46c894d1303c20712eb5..259ec0aebabb561f9a3f287ec2cd1c9a205f81d4 100644 --- a/module-services/service-desktop/endpoints/update/UpdateHelper.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateHelper.cpp @@ -174,7 +174,7 @@ namespace sdesktop::endpoints { const auto &body = context.getBody(); - if (body[json::update] != true || body[json::reboot] != true) { + if (body[json::update::update] != true || body[json::update::reboot] != true) { return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; } diff --git a/module-services/service-desktop/include/service-desktop/Sync.hpp b/module-services/service-desktop/include/service-desktop/Sync.hpp index f2954d6c2cfe2aeaa021520bea3ed83c9ccf0fce..94ded0b54193985b984f0dcc7659d7b63fff8d7a 100644 --- a/module-services/service-desktop/include/service-desktop/Sync.hpp +++ b/module-services/service-desktop/include/service-desktop/Sync.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -71,10 +71,10 @@ class Sync OperationState state = OperationState::Stopped; json11::Json to_json() const { - auto response = json11::Json::object{{sdesktop::endpoints::json::state, opToString(state)}}; + auto response = json11::Json::object{{sdesktop::endpoints::json::sync::state, opToString(state)}}; if (state == OperationState::Error) { - response[sdesktop::endpoints::json::reason] = completionCodeToString(completionCode); + response[sdesktop::endpoints::json::common::reason] = completionCodeToString(completionCode); } return response; diff --git a/module-sys/SystemManager/PowerManager.cpp b/module-sys/SystemManager/PowerManager.cpp index ce8d09c57a4c4f7a2dfbee7c52434a4b3fcd30cf..83ad0d401b3228ff31a85f04525d1223611edbb7 100644 --- a/module-sys/SystemManager/PowerManager.cpp +++ b/module-sys/SystemManager/PowerManager.cpp @@ -80,17 +80,22 @@ namespace sys PowerManager::~PowerManager() {} - int32_t PowerManager::PowerOff() + std::int32_t PowerManager::PowerOff() { return lowPowerControl->PowerOff(); } - int32_t PowerManager::Reboot() + std::int32_t PowerManager::Reboot() { return lowPowerControl->Reboot(bsp::LowPowerMode::RebootType::NormalRestart); } - int32_t PowerManager::RebootToRecovery(RecoveryReason reason) + std::int32_t PowerManager::RebootMSC() + { + return lowPowerControl->Reboot(bsp::LowPowerMode::RebootType::GoToMSC); + } + + std::int32_t PowerManager::RebootToRecovery(RecoveryReason reason) { switch (reason) { case RecoveryReason::FactoryReset: @@ -110,7 +115,7 @@ namespace sys [[nodiscard]] cpu::UpdateResult PowerManager::UpdateCpuFrequency() { - uint32_t cpuLoad = cpuStatistics.GetPercentageCpuLoad(); + std::uint32_t cpuLoad = cpuStatistics.GetPercentageCpuLoad(); cpu::UpdateResult retval; const cpu::AlgorithmData data{ cpuLoad, lowPowerControl->GetCurrentFrequencyLevel(), GetMinimumCpuFrequencyRequested()}; diff --git a/module-sys/SystemManager/SystemManagerCommon.cpp b/module-sys/SystemManager/SystemManagerCommon.cpp index 6664cf7fbd79f6f1a17b17273a199f3d834b1d63..913825fadcef03073c57e350f9401dbb58724395 100644 --- a/module-sys/SystemManager/SystemManagerCommon.cpp +++ b/module-sys/SystemManager/SystemManagerCommon.cpp @@ -115,6 +115,9 @@ namespace sys case SystemManagerCommon::State::Reboot: LOG_INFO(" ---> REBOOT <--- "); break; + case SystemManagerCommon::State::RebootMSC: + LOG_INFO(" ---> REBOOT MSC <--- "); + break; case SystemManagerCommon::State::ShutdownReady: LOG_INFO(" ---> SHUTDOWN <--- "); break; @@ -135,6 +138,9 @@ namespace sys case State::Reboot: powerManager->Reboot(); break; + case State::RebootMSC: + powerManager->RebootMSC(); + break; case State::ShutdownReady: powerManager->PowerOff(); break; @@ -264,6 +270,13 @@ namespace sys service::name::system_manager); } + bool SystemManagerCommon::RegularPowerDown(Service *s) + { + s->bus.sendUnicast(std::make_shared(Code::CloseSystem, CloseReason::RegularPowerDown), + service::name::system_manager); + return true; + } + bool SystemManagerCommon::Reboot(Service *s) { s->bus.sendUnicast(std::make_shared(Code::Reboot, CloseReason::Reboot), @@ -271,6 +284,13 @@ namespace sys return true; } + bool SystemManagerCommon::RebootMSC(Service *s) + { + s->bus.sendUnicast(std::make_shared(Code::RebootMSC, CloseReason::RebootMSC), + service::name::system_manager); + return true; + } + bool SystemManagerCommon::RebootToRecovery(Service *s, RecoveryReason recoveryReason) { s->bus.sendUnicast( @@ -546,6 +566,9 @@ namespace sys case Code::Reboot: RebootHandler(); break; + case Code::RebootMSC: + RebootHandlerMSC(); + break; case Code::RebootToRecovery: case Code::FactoryReset: RebootToRecoveryHandler(data->closeReason, data->recoveryReason); @@ -711,6 +734,10 @@ namespace sys DestroyServices(getWhiteListFor(WhiteListType::RegularClose)); set(State::Reboot); break; + case CloseReason::RebootMSC: + DestroyServices(getWhiteListFor(WhiteListType::RegularClose)); + set(State::RebootMSC); + break; case CloseReason::RebootToRecovery: case CloseReason::FactoryReset: DestroyServices(getWhiteListFor(WhiteListType::Update)); @@ -740,6 +767,11 @@ namespace sys CloseSystemHandler(CloseReason::Reboot); } + void SystemManagerCommon::RebootHandlerMSC() + { + CloseSystemHandler(CloseReason::RebootMSC); + } + void SystemManagerCommon::RebootToRecoveryHandler(CloseReason closeReason, RecoveryReason recoveryReason) { CloseSystemHandler(closeReason); diff --git a/module-sys/SystemManager/include/SystemManager/PowerManager.hpp b/module-sys/SystemManager/include/SystemManager/PowerManager.hpp index 6b45ee5c8db89ba8a60d61a656db056b975222c7..3a93a9bb26a14796d0980babf88ef61ad90d916e 100644 --- a/module-sys/SystemManager/include/SystemManager/PowerManager.hpp +++ b/module-sys/SystemManager/include/SystemManager/PowerManager.hpp @@ -48,9 +48,10 @@ namespace sys explicit PowerManager(CpuStatistics &cpuStats, TaskStatistics &taskStats); ~PowerManager(); - int32_t PowerOff(); - int32_t Reboot(); - int32_t RebootToRecovery(RecoveryReason reason); + std::int32_t PowerOff(); + std::int32_t Reboot(); + std::int32_t RebootMSC(); + std::int32_t RebootToRecovery(RecoveryReason reason); /// called periodically to calculate the CPU requirement /// diff --git a/module-sys/SystemManager/include/SystemManager/SystemManagerCommon.hpp b/module-sys/SystemManager/include/SystemManager/SystemManagerCommon.hpp index 1b76b1237a32ca9766a787ff6a7a44fa834130cd..5600afc509d6ea33bd53e3d6d754aa97219e265d 100644 --- a/module-sys/SystemManager/include/SystemManager/SystemManagerCommon.hpp +++ b/module-sys/SystemManager/include/SystemManager/SystemManagerCommon.hpp @@ -34,6 +34,7 @@ namespace sys CloseSystem, Restore, Reboot, + RebootMSC, RebootToRecovery, FactoryReset, None, @@ -78,7 +79,8 @@ namespace sys Shutdown, ShutdownReady, Reboot, - RebootToRecovery + RebootToRecovery, + RebootMSC } state = State::Running; explicit SystemManagerCommon(std::vector> &&creators); @@ -96,6 +98,10 @@ namespace sys static bool Reboot(Service *s); + static bool RebootMSC(Service *s); + + static bool RegularPowerDown(Service *s); + static bool RebootToRecovery(Service *s, RecoveryReason recoveryReason); static bool SuspendService(const std::string &name, Service *caller); @@ -179,6 +185,8 @@ namespace sys void RebootHandler(); + void RebootHandlerMSC(); + void RebootToRecoveryHandler(CloseReason closeReason, RecoveryReason recoveryReason); void FreqUpdateTick(); @@ -225,6 +233,8 @@ inline const char *c_str(sys::SystemManagerCommon::State state) return "Shutdown"; case sys::SystemManagerCommon::State::Reboot: return "Reboot"; + case sys::SystemManagerCommon::State::RebootMSC: + return "RebootMSC"; case sys::SystemManagerCommon::State::RebootToRecovery: return "RebootToRecovery"; case sys::SystemManagerCommon::State::ShutdownReady: diff --git a/module-sys/common/include/system/Common.hpp b/module-sys/common/include/system/Common.hpp index db655abbfa180c1274f134a9a9d6af8641f1788b..33b086c46645be728df239d84eda7688ad8986e2 100644 --- a/module-sys/common/include/system/Common.hpp +++ b/module-sys/common/include/system/Common.hpp @@ -51,6 +51,7 @@ namespace sys RegularPowerDown, OnboardingPowerDown, Reboot, + RebootMSC, RebootToRecovery, FactoryReset, SystemBrownout, diff --git a/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp b/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp index 0c6b66925cfa432d8ca23ef4f461883370eae071..d6f9e88cf5f91d6dc2c7454441f8a29d25707f2f 100644 --- a/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp +++ b/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace sdesktop::endpoints @@ -33,6 +34,8 @@ namespace sdesktop::endpoints return std::make_unique(ownerServicePtr); case EndpointType::factory: return std::make_unique(ownerServicePtr); + case EndpointType::reboot: + return std::make_unique(ownerServicePtr); default: return std::make_unique(ownerServicePtr); } diff --git a/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp b/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp index 8e6698261007472dff7e0bdd2a289b591649b675..c051c77087d54a26e23ebf425f3a1087fdb2a627 100644 --- a/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp +++ b/products/BellHybrid/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp @@ -37,24 +37,27 @@ namespace sdesktop::endpoints const auto [totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB] = getStorageInfo(); context.setResponseBody(json11::Json::object( - {{json::batteryLevel, std::to_string(Store::Battery::get().level)}, - {json::batteryState, std::to_string(static_cast(Store::Battery::get().state))}, - {json::deviceSpaceTotal, std::to_string(totalDeviceSpaceMiB)}, - {json::systemReservedSpace, std::to_string(reservedSystemSpaceMiB)}, - {json::usedUserSpace, std::to_string(usedUserSpaceMiB)}, - {json::gitRevision, std::string(GIT_REV)}, - {json::gitBranch, std::string(GIT_BRANCH)}, - {json::currentRTCTime, std::to_string(static_cast(std::time(nullptr)))}, - {json::version, std::string(VERSION)}, - {json::serialNumber, getSerialNumber()}, - {json::caseColour, getCaseColour()}, - {json::recoveryStatusFilePath, + {{json::deviceInfo::batteryLevel, std::to_string(Store::Battery::get().level)}, + {json::deviceInfo::batteryState, std::to_string(static_cast(Store::Battery::get().state))}, + {json::deviceInfo::deviceSpaceTotal, std::to_string(totalDeviceSpaceMiB)}, + {json::deviceInfo::systemReservedSpace, std::to_string(reservedSystemSpaceMiB)}, + {json::deviceInfo::usedUserSpace, std::to_string(usedUserSpaceMiB)}, + {json::deviceInfo::gitRevision, std::string(GIT_REV)}, + {json::deviceInfo::gitBranch, std::string(GIT_BRANCH)}, + {json::deviceInfo::currentRTCTime, std::to_string(static_cast(std::time(nullptr)))}, + {json::deviceInfo::version, std::string(VERSION)}, + {json::deviceInfo::serialNumber, getSerialNumber()}, + {json::deviceInfo::caseColour, getCaseColour()}, + {json::deviceInfo::recoveryStatusFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::recoveryStatusFilename).string()}, - {json::updateFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::updateFilename).string()}, - {json::backupFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::backupFilename).string()}, - {json::syncFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::syncFilename).string()}, - {json::mtpPath, getMtpPath().string()}, - {json::onboardingState, std::to_string(static_cast(getOnboardingState()))}})); + {json::deviceInfo::updateFilePath, + (purefs::dir::getTemporaryPath() / sdesktop::paths::updateFilename).string()}, + {json::deviceInfo::backupFilePath, + (purefs::dir::getTemporaryPath() / sdesktop::paths::backupFilename).string()}, + {json::deviceInfo::syncFilePath, + (purefs::dir::getTemporaryPath() / sdesktop::paths::syncFilename).string()}, + {json::deviceInfo::mtpPath, getMtpPath().string()}, + {json::deviceInfo::onboardingState, std::to_string(static_cast(getOnboardingState()))}})); return http::Code::OK; } diff --git a/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp b/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp index 2c126803b20a8d181f85010aa14c6bd163496f57..84be676bf58f2474a0c6735b145468411baea125 100644 --- a/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp +++ b/products/PurePhone/services/desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp @@ -50,32 +50,36 @@ namespace sdesktop::endpoints }(); context.setResponseBody(json11::Json::object( - {{json::batteryLevel, std::to_string(Store::Battery::get().level)}, - {json::batteryState, std::to_string(static_cast(Store::Battery::get().state))}, - {json::selectedSim, std::to_string(static_cast(Store::GSM::get()->selected))}, - {json::trayState, std::to_string(static_cast(Store::GSM::get()->tray))}, - {json::signalStrength, std::to_string(signalStrength)}, - {json::accessTechnology, + {{json::deviceInfo::batteryLevel, std::to_string(Store::Battery::get().level)}, + {json::deviceInfo::batteryState, std::to_string(static_cast(Store::Battery::get().state))}, + {json::deviceInfo::selectedSim, std::to_string(static_cast(Store::GSM::get()->selected))}, + {json::deviceInfo::trayState, std::to_string(static_cast(Store::GSM::get()->tray))}, + {json::deviceInfo::signalStrength, std::to_string(signalStrength)}, + {json::deviceInfo::accessTechnology, std::to_string(static_cast(Store::GSM::get()->getNetwork().accessTechnology))}, - {json::networkStatus, std::to_string(static_cast(Store::GSM::get()->getNetwork().status))}, - {json::networkOperatorName, Store::GSM::get()->getNetworkOperatorName()}, - {json::deviceSpaceTotal, std::to_string(totalDeviceSpaceMiB)}, - {json::systemReservedSpace, std::to_string(reservedSystemSpaceMiB)}, - {json::usedUserSpace, std::to_string(usedUserSpaceMiB)}, - {json::gitRevision, std::string(GIT_REV)}, - {json::gitBranch, std::string(GIT_BRANCH)}, - {json::currentRTCTime, std::to_string(static_cast(std::time(nullptr)))}, - {json::version, std::string(VERSION)}, - {json::serialNumber, getSerialNumber()}, - {json::caseColour, getCaseColour()}, - {json::recoveryStatusFilePath, + {json::deviceInfo::networkStatus, + std::to_string(static_cast(Store::GSM::get()->getNetwork().status))}, + {json::deviceInfo::networkOperatorName, Store::GSM::get()->getNetworkOperatorName()}, + {json::deviceInfo::deviceSpaceTotal, std::to_string(totalDeviceSpaceMiB)}, + {json::deviceInfo::systemReservedSpace, std::to_string(reservedSystemSpaceMiB)}, + {json::deviceInfo::usedUserSpace, std::to_string(usedUserSpaceMiB)}, + {json::deviceInfo::gitRevision, std::string(GIT_REV)}, + {json::deviceInfo::gitBranch, std::string(GIT_BRANCH)}, + {json::deviceInfo::currentRTCTime, std::to_string(static_cast(std::time(nullptr)))}, + {json::deviceInfo::version, std::string(VERSION)}, + {json::deviceInfo::serialNumber, getSerialNumber()}, + {json::deviceInfo::caseColour, getCaseColour()}, + {json::deviceInfo::recoveryStatusFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::recoveryStatusFilename).string()}, - {json::updateFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::updateFilename).string()}, - {json::backupFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::backupFilename).string()}, - {json::syncFilePath, (purefs::dir::getTemporaryPath() / sdesktop::paths::syncFilename).string()}, - {json::mtpPath, getMtpPath().string()}, - {json::deviceToken, getDeviceToken()}, - {json::onboardingState, std::to_string(static_cast(getOnboardingState()))}})); + {json::deviceInfo::updateFilePath, + (purefs::dir::getTemporaryPath() / sdesktop::paths::updateFilename).string()}, + {json::deviceInfo::backupFilePath, + (purefs::dir::getTemporaryPath() / sdesktop::paths::backupFilename).string()}, + {json::deviceInfo::syncFilePath, + (purefs::dir::getTemporaryPath() / sdesktop::paths::syncFilename).string()}, + {json::deviceInfo::mtpPath, getMtpPath().string()}, + {json::deviceInfo::deviceToken, getDeviceToken()}, + {json::deviceInfo::onboardingState, std::to_string(static_cast(getOnboardingState()))}})); return http::Code::OK; }