From bba04acbe0ac6234cb207704fcfb58259196a9af Mon Sep 17 00:00:00 2001 From: Lefucjusz Date: Fri, 11 Oct 2024 10:50:56 +0200 Subject: [PATCH] [BH-2075] Add GET method to time sync endpoint Added GET method returning current Harmony time as a timestamp. This is needed by MC to implement time synchronization functionality. --- .../service-desktop/endpoints/BaseHelper.cpp | 4 +- .../endpoints/backup/BackupEndpoint.cpp | 7 ++- .../endpoints/backup/BackupHelper.cpp | 15 +++--- .../developerMode/DeveloperModeEndpoint.cpp | 6 +-- .../developerMode/DeveloperModeHelper.cpp | 54 +++++++++---------- .../developerMode/Mode/UI_Helper.cpp | 4 +- .../endpoints/filesystem/FS_Helper.cpp | 6 +-- .../filesystem/FilesystemEndpoint.cpp | 7 ++- .../include/endpoints/BaseHelper.hpp | 13 +++-- .../include/endpoints/JsonKeyNames.hpp | 1 + .../developerMode/DeveloperModeHelper.hpp | 12 ++--- .../endpoints/timeSync/TimeSyncHelper.hpp | 1 + .../include/endpoints/update/UpdateHelper.hpp | 2 +- .../endpoints/reboot/RebootEndpoint.cpp | 6 +-- .../endpoints/reboot/RebootHelper.cpp | 8 +-- .../endpoints/restore/RestoreEndpoint.cpp | 6 +-- .../endpoints/restore/RestoreHelper.cpp | 4 +- .../endpoints/security/SecurityEndpoint.cpp | 6 +-- .../security/SecurityEndpointHelper.cpp | 9 ++-- .../endpoints/timeSync/TimeSyncEndpoint.cpp | 6 +-- .../endpoints/timeSync/TimeSyncHelper.cpp | 22 ++++++-- .../endpoints/update/UpdateEndpoint.cpp | 8 ++- .../endpoints/update/UpdateHelper.cpp | 33 ++++++------ 23 files changed, 124 insertions(+), 116 deletions(-) diff --git a/module-services/service-desktop/endpoints/BaseHelper.cpp b/module-services/service-desktop/endpoints/BaseHelper.cpp index 4728aa4b084995c4931d076fc55cc40df8a8c375..cca0fb90cd17db929e88439bcddc3a6108a7dd50 100644 --- a/module-services/service-desktop/endpoints/BaseHelper.cpp +++ b/module-services/service-desktop/endpoints/BaseHelper.cpp @@ -7,7 +7,7 @@ namespace sdesktop::endpoints { auto ret() -> BaseHelper::ProcessResult { - return {sent::no, std::nullopt}; + return {Sent::No, std::nullopt}; } auto BaseHelper::processPut(Context &) -> ProcessResult @@ -44,6 +44,6 @@ namespace sdesktop::endpoints return processPut(context); } postProcess(method, context); - return {sent::no, std::nullopt}; + return {Sent::No, std::nullopt}; } } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/backup/BackupEndpoint.cpp b/module-services/service-desktop/endpoints/backup/BackupEndpoint.cpp index 56d67809b6d709215350b70f2540366208560372..5b681f43dc68f4300b88fc813c42e437fa8dd0b3 100644 --- a/module-services/service-desktop/endpoints/backup/BackupEndpoint.cpp +++ b/module-services/service-desktop/endpoints/backup/BackupEndpoint.cpp @@ -11,10 +11,10 @@ namespace sdesktop::endpoints { auto [sent, response] = helper->process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + if (sent == Sent::No) { if (not response) { LOG_ERROR("Response not sent & response not created : respond with error"); context.setResponseStatus(http::Code::NotAcceptable); @@ -25,9 +25,8 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response) { + if (sent == Sent::Yes and response) { LOG_ERROR("Response set when we already handled response in handler"); } } - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/backup/BackupHelper.cpp b/module-services/service-desktop/endpoints/backup/BackupHelper.cpp index f1b83e04f30e392b8f34c20bd7289ba7009a318d..d748ef512569ac931b87233f5d1d477137aff919 100644 --- a/module-services/service-desktop/endpoints/backup/BackupHelper.cpp +++ b/module-services/service-desktop/endpoints/backup/BackupHelper.cpp @@ -20,7 +20,7 @@ namespace sdesktop::endpoints return checkSyncState(context); } - return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; } auto BackupHelper::processPost(Context &context) -> ProcessResult @@ -33,16 +33,16 @@ namespace sdesktop::endpoints return executeBackupRequest(context); } - return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; } auto BackupHelper::executeBackupRequest([[maybe_unused]] Context &context) -> ProcessResult { if (sys::SystemManagerCommon::RebootToRecovery(owner, sys::RecoveryReason::Backup)) { - return {sent::no, ResponseContext{.status = http::Code::NoContent}}; + return {Sent::No, ResponseContext{.status = http::Code::NoContent}}; } - return {sent::no, ResponseContext{.status = http::Code::InternalServerError}}; + return {Sent::No, ResponseContext{.status = http::Code::InternalServerError}}; } auto BackupHelper::executeSyncRequest([[maybe_unused]] Context &context) -> ProcessResult @@ -52,7 +52,7 @@ namespace sdesktop::endpoints if (ownerServicePtr->getSyncStatus().state == Sync::OperationState::Running) { LOG_DEBUG("Sync already running"); // a sync package preparation is already running, don't start a second task - return {sent::no, ResponseContext{.status = http::Code::NotAcceptable}}; + return {Sent::No, ResponseContext{.status = http::Code::NotAcceptable}}; } else { LOG_DEBUG("Starting a sync package preparation"); @@ -64,7 +64,7 @@ namespace sdesktop::endpoints // return new generated sync package info - return {sent::no, ResponseContext{.status = http::Code::Accepted, .body = json11::Json::object{}}}; + return {Sent::No, ResponseContext{.status = http::Code::Accepted, .body = json11::Json::object{}}}; } } @@ -75,7 +75,6 @@ namespace sdesktop::endpoints const auto status = (syncStatus.state == Sync::OperationState::Finished) ? http::Code::SeeOther : http::Code::NoContent; - return {sent::no, ResponseContext{.status = status, .body = syncStatus}}; + return {Sent::No, ResponseContext{.status = status, .body = syncStatus}}; } - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/developerMode/DeveloperModeEndpoint.cpp b/module-services/service-desktop/endpoints/developerMode/DeveloperModeEndpoint.cpp index 0b93b3324200884ece925f215bd3f73b99bee552..ed09d18902f257784e7f3d788880af7f0ff82496 100644 --- a/module-services/service-desktop/endpoints/developerMode/DeveloperModeEndpoint.cpp +++ b/module-services/service-desktop/endpoints/developerMode/DeveloperModeEndpoint.cpp @@ -13,10 +13,10 @@ namespace sdesktop::endpoints { auto &p = helperSwitcher(context); auto [sent, response] = p.process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + if (sent == Sent::No) { if (not response) { LOG_ERROR("Response not sent & response not created : respond with error"); context.setResponseStatus(http::Code::NotAcceptable); @@ -27,7 +27,7 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response) { + if (sent == Sent::Yes and response) { LOG_ERROR("Response set when we already handled response in handler"); } } diff --git a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp index dc3ea70716cdb3a0009d6141d13bbb4354835c01..a103d83ed47ff76367dbdec7c2a7283f36f4811c 100644 --- a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp +++ b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp @@ -30,21 +30,19 @@ #include #include + namespace { - auto toTetheringState(const std::string &state) -> sys::phone_modes::Tethering { using namespace sdesktop::endpoints::json::developerMode; return state == tetheringOn ? sys::phone_modes::Tethering::On : sys::phone_modes::Tethering::Off; } - } // namespace namespace sdesktop::endpoints { - using sender::putToSendQueue; auto DeveloperModeHelper::processPut(Context &context) -> ProcessResult @@ -56,7 +54,7 @@ namespace sdesktop::endpoints auto state = body[json::developerMode::state].int_value(); sendKeypress(getKeyCode(keyValue), static_cast(state)); app::manager::Controller::preventBlockingDevice(owner); - return {sent::no, std::nullopt}; + return {Sent::No, std::nullopt}; } else if (body[json::developerMode::AT].is_string()) { using namespace sdesktop::developerMode; @@ -66,7 +64,7 @@ namespace sdesktop::endpoints auto event = std::make_unique(cmd, timeout); auto msg = std::make_shared(std::move(event)); code = toCode(owner->bus.sendUnicast(msg, service::name::cellular)); - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } else if (body[json::developerMode::EQ].is_string()) { using namespace sdesktop::developerMode; @@ -94,19 +92,19 @@ namespace sdesktop::endpoints LOG_INFO("File verify OK"); code = toCode(true); } - return {sent::no, ResponseContext{.status = code}}; + return {Sent::No, ResponseContext{.status = code}}; } else if (body[json::developerMode::focus].bool_value()) { auto event = std::make_unique(); auto msg = std::make_shared(std::move(event)); code = toCode(owner->bus.sendUnicast(std::move(msg), service::name::evt_manager)); - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } else if (body[json::developerMode::phoneLocked].bool_value()) { auto event = std::make_unique(); auto msg = std::make_shared(std::move(event)); code = toCode(owner->bus.sendUnicast(std::move(msg), "ApplicationManager")); - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } else if (body[json::developerMode::changeAutoLockTimeout].is_string()) { auto value = body[json::developerMode::changeAutoLockTimeout].string_value(); @@ -118,20 +116,20 @@ namespace sdesktop::endpoints code = owner->bus.sendUnicast(std::move(msg), service::name::db) ? http::Code::NoContent : http::Code::InternalServerError; - return {sent::no, ResponseContext{.status = code}}; + return {Sent::No, ResponseContext{.status = code}}; } else if (body[json::developerMode::changeSim].is_number()) { int simSelected = body[json::developerMode::changeSim].int_value(); requestSimChange(simSelected); code = http::Code::NoContent; - return {sent::no, ResponseContext{.status = code}}; + return {Sent::No, ResponseContext{.status = code}}; } else if (body[json::developerMode::changeCellularStateCmd].is_number()) { int cellularState = body[json::developerMode::changeCellularStateCmd].int_value(); code = requestCellularPowerStateChange(cellularState) ? http::Code::NoContent : http::Code::InternalServerError; - return {sent::no, ResponseContext{.status = code}}; + return {Sent::No, ResponseContext{.status = code}}; } else if (body[json::developerMode::smsCommand].is_string()) { if (body[json::developerMode::smsCommand].string_value() == json::developerMode::smsAdd) { @@ -140,7 +138,7 @@ namespace sdesktop::endpoints return prepareSMS(context); } else { - return {sent::no, ResponseContext{.status = http::Code::NotAcceptable}}; + return {Sent::No, ResponseContext{.status = http::Code::NotAcceptable}}; } } } @@ -148,7 +146,7 @@ namespace sdesktop::endpoints const auto &tetheringState = body[json::developerMode::tethering].string_value(); owner->bus.sendUnicast(std::make_shared(toTetheringState(tetheringState)), service::name::system_manager); - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } else if (body[json::developerMode::phoneLockCodeEnabled].is_bool()) { auto phoneLockState = body[json::developerMode::phoneLockCodeEnabled].bool_value(); @@ -177,7 +175,7 @@ namespace sdesktop::endpoints context.setResponseStatus(http::Code::BadRequest); putToSendQueue(context.createSimpleResponse()); } - return {sent::no, ResponseContext{.status = code}}; + return {Sent::No, ResponseContext{.status = code}}; } auto DeveloperModeHelper::processGet(Context &context) -> ProcessResult @@ -192,30 +190,30 @@ namespace sdesktop::endpoints {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)}; + return {Sent::No, std::move(response)}; } else if (keyValue == json::developerMode::cellularStateInfo) { if (!requestServiceStateInfo(owner)) { - return {sent::no, ResponseContext{.status = http::Code::NotAcceptable}}; + return {Sent::No, ResponseContext{.status = http::Code::NotAcceptable}}; } else { - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } } else if (keyValue == json::developerMode::cellularSleepModeInfo) { if (!requestCellularSleepModeInfo(owner)) { - return {sent::no, ResponseContext{.status = http::Code::NotAcceptable}}; + return {Sent::No, ResponseContext{.status = http::Code::NotAcceptable}}; } else { - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } } else { - return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; } } else { - return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; } } @@ -278,7 +276,7 @@ namespace sdesktop::endpoints }; } - bool DeveloperModeHelper::sendKeypress(bsp::KeyCodes keyCode, gui::InputEvent::State state) + auto DeveloperModeHelper::sendKeypress(bsp::KeyCodes keyCode, gui::InputEvent::State state) -> bool { RawKey key{.state = RawKey::State::Released, .keyCode = keyCode}; @@ -288,14 +286,14 @@ namespace sdesktop::endpoints return owner->bus.sendUnicast(std::move(message), service::name::evt_manager); } - void DeveloperModeHelper::requestSimChange(const int simSelected) + auto DeveloperModeHelper::requestSimChange(const int simSelected) -> void { auto arg = (simSelected == static_cast(cellular::api::SimSlot::SIM2)) ? cellular::api::SimSlot::SIM2 : cellular::api::SimSlot::SIM1; owner->bus.sendUnicast(arg); } - bool DeveloperModeHelper::requestCellularPowerStateChange(const int cellularState) + auto DeveloperModeHelper::requestCellularPowerStateChange(const int cellularState) -> bool { bool res = false; if (cellularState == 1) { @@ -311,6 +309,7 @@ namespace sdesktop::endpoints } return res; } + auto DeveloperModeHelper::smsRecordFromJson(const json11::Json &msgJson) -> SMSRecord { auto record = SMSRecord(); @@ -349,21 +348,20 @@ namespace sdesktop::endpoints DBServiceAPI::AddSMS(owner, record, std::move(listener)); // actual success / fail happens in listener - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } - bool DeveloperModeHelper::requestServiceStateInfo(sys::Service *serv) + auto DeveloperModeHelper::requestServiceStateInfo(sys::Service *serv) -> bool { auto event = std::make_unique(); auto msg = std::make_shared(std::move(event)); return serv->bus.sendUnicast(std::move(msg), service::name::cellular); } - bool DeveloperModeHelper::requestCellularSleepModeInfo(sys::Service *serv) + auto DeveloperModeHelper::requestCellularSleepModeInfo(sys::Service *serv) -> bool { auto event = std::make_unique(); auto msg = std::make_shared(std::move(event)); return serv->bus.sendUnicast(std::move(msg), service::name::cellular); } - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/developerMode/Mode/UI_Helper.cpp b/module-services/service-desktop/endpoints/developerMode/Mode/UI_Helper.cpp index f04200d2abd7dcfed16cc9d538464e4e9661475b..8d31c0b852f4f7366ffcbdaf25772916123e00ed 100644 --- a/module-services/service-desktop/endpoints/developerMode/Mode/UI_Helper.cpp +++ b/module-services/service-desktop/endpoints/developerMode/Mode/UI_Helper.cpp @@ -33,8 +33,8 @@ namespace sdesktop::endpoints std::make_shared(service::name::service_desktop, std::make_unique(std::move(event))), service::name::appmgr); - return {sent::delayed, std::nullopt}; + return {Sent::Delayed, std::nullopt}; } - return {sent::no, std::nullopt}; + return {Sent::No, std::nullopt}; } }; // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp index bae3ff0139c7214f7be5a3c24da3d445e0aed76e..49ebde5dddf4c18b6109e380ed7219cb3c6b982f 100644 --- a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp +++ b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp @@ -68,7 +68,7 @@ namespace sdesktop::endpoints response = {.status = http::Code::BadRequest}; } - return {sent::no, std::move(response)}; + return {Sent::No, std::move(response)}; } auto FS_Helper::processPut(Context &context) -> ProcessResult @@ -96,7 +96,7 @@ namespace sdesktop::endpoints response = ResponseContext{.status = http::Code::BadRequest}; } - return {sent::no, std::move(response)}; + return {Sent::No, std::move(response)}; } auto FS_Helper::processDelete(Context &context) -> ProcessResult @@ -118,7 +118,7 @@ namespace sdesktop::endpoints LOG_ERROR("Bad request, missing or invalid argument"); } - return {sent::no, ResponseContext{.status = code}}; + return {Sent::No, ResponseContext{.status = code}}; } auto FS_Helper::requestLogsFlush() const -> void { diff --git a/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp b/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp index 74892751dadda8fc32537ae767113b828d992c62..253ce64f0dc1cec0c5b6ba983c89f4584227cec7 100644 --- a/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp +++ b/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp @@ -14,10 +14,10 @@ namespace sdesktop::endpoints { const auto &[sent, response] = helper->process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + if (sent == Sent::No) { if (not response) { LOG_ERROR("Response not sent & response not created : respond with error"); context.setResponseStatus(http::Code::NotAcceptable); @@ -28,9 +28,8 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response) { + if (sent == Sent::Yes and response) { LOG_ERROR("Response set when we already handled response in handler"); } } - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/include/endpoints/BaseHelper.hpp b/module-services/service-desktop/endpoints/include/endpoints/BaseHelper.hpp index ac40db5d2355ce611969514e3736b59ab1082fd4..5fbc034222edfaf399e07e0b82d65c7c713f370e 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/BaseHelper.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/BaseHelper.hpp @@ -15,28 +15,28 @@ namespace sys namespace sdesktop::endpoints { - class Context; - enum class sent + + enum class Sent { /// response was sent from Helper class /// we should avoid that - yes, + Yes, /// response wasn't sent from Helper class /// we should send it from endpoint based on processing result - no, + No, /// works on global context /// mechanism: /// - Send message which is delayed /// - send the actual response form ServiceDesktop on `sdesktop::developerMode::DeveloperModeRequest` - delayed, + Delayed, }; /// base helper class to avoid copies class BaseHelper { public: - using ProcessResult = std::pair>; + using ProcessResult = std::pair>; protected: sys::Service *owner = nullptr; @@ -68,5 +68,4 @@ namespace sdesktop::endpoints /// pre and post processing is available on pre/post process method override [[nodiscard]] auto process(http::Method method, Context &context) -> ProcessResult; }; - } // 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 166a2fd717aa7cb3f2b995256f0fee5de4e47f3e..02aa071c285517affa03635a29f32d001ca989ef 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp @@ -120,6 +120,7 @@ namespace sdesktop::endpoints::json namespace timeSync { + inline constexpr auto value = "value"; inline constexpr auto timestamp = "timestamp"; } } // namespace sdesktop::endpoints::json diff --git a/module-services/service-desktop/endpoints/include/endpoints/developerMode/DeveloperModeHelper.hpp b/module-services/service-desktop/endpoints/include/endpoints/developerMode/DeveloperModeHelper.hpp index 7f32ee0ef57a437614ae84c47def58a191d08c54..a6185342be4b7ccbf73506852b8c7ca584cc42d7 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/developerMode/DeveloperModeHelper.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/developerMode/DeveloperModeHelper.hpp @@ -10,17 +10,16 @@ namespace sdesktop::endpoints { - class DeveloperModeHelper : public BaseHelper { static auto getKeyCode(int val) noexcept -> bsp::KeyCodes; - bool sendKeypress(bsp::KeyCodes keyCode, gui::InputEvent::State state); + auto sendKeypress(bsp::KeyCodes keyCode, gui::InputEvent::State state) -> bool; - void requestSimChange(int simSelected); + auto requestSimChange(int simSelected) -> void; auto smsRecordFromJson(const json11::Json &msgJson) -> SMSRecord; - bool requestCellularPowerStateChange(int simSelected); - bool requestServiceStateInfo(sys::Service *serv); - bool requestCellularSleepModeInfo(sys::Service *serv); + auto requestCellularPowerStateChange(int simSelected) -> bool; + auto requestServiceStateInfo(sys::Service *serv) -> bool; + auto requestCellularSleepModeInfo(sys::Service *serv) -> bool; auto prepareSMS(Context &context) -> ProcessResult; public: @@ -73,5 +72,4 @@ namespace sdesktop::endpoints inline constexpr auto tetheringOff = "off"; } // namespace json::developerMode - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/include/endpoints/timeSync/TimeSyncHelper.hpp b/module-services/service-desktop/endpoints/include/endpoints/timeSync/TimeSyncHelper.hpp index 107f8073b675305291a59c156fe9ea7b1ab4c6b0..de206ae81877453ead8fc5ab5478f0322519a004 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/timeSync/TimeSyncHelper.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/timeSync/TimeSyncHelper.hpp @@ -14,6 +14,7 @@ namespace sdesktop::endpoints explicit TimeSyncHelper(sys::Service *p) : BaseHelper(p) {} + auto processGet(Context &context) -> ProcessResult final; auto processPost(Context &context) -> ProcessResult final; private: diff --git a/module-services/service-desktop/endpoints/include/endpoints/update/UpdateHelper.hpp b/module-services/service-desktop/endpoints/include/endpoints/update/UpdateHelper.hpp index 482ca54dfc9d7c36ebe2129ae9dd5e1990ae4f47..efbc2b708a66517536f6d11ce57e1dfadf0e28a8 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/update/UpdateHelper.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/update/UpdateHelper.hpp @@ -15,7 +15,7 @@ namespace sdesktop::endpoints explicit UpdateHelper(sys::Service *p); auto processPost(Context &context) -> ProcessResult final; - void preProcess(http::Method method, Context &context) final; + auto preProcess(http::Method method, Context &context) -> void final; private: std::filesystem::path updatePackagePath; diff --git a/module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp b/module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp index 16686c703ba70184f85921136d526e5bfda63309..08519b69b689aba6000bc76e0fa64638cff6f69f 100644 --- a/module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp +++ b/module-services/service-desktop/endpoints/reboot/RebootEndpoint.cpp @@ -12,10 +12,10 @@ namespace sdesktop::endpoints { const auto &[sent, response] = helper->process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + 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); @@ -26,7 +26,7 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response.has_value()) { + if (sent == Sent::Yes and response.has_value()) { LOG_ERROR("Response set when we already handled response in handler"); } } diff --git a/module-services/service-desktop/endpoints/reboot/RebootHelper.cpp b/module-services/service-desktop/endpoints/reboot/RebootHelper.cpp index 6ee98b5a56ae1181ea52c9debdb00717d9fc2b81..bcd9ce2737e4404a66eb03ac23e6d0d562d69b39 100644 --- a/module-services/service-desktop/endpoints/reboot/RebootHelper.cpp +++ b/module-services/service-desktop/endpoints/reboot/RebootHelper.cpp @@ -16,17 +16,17 @@ namespace sdesktop::endpoints if (rebootType == json::reboot::msc) { sys::SystemManagerCommon::RebootMSC(owner); - return {sent::no, ResponseContext{.status = http::Code::Accepted}}; + 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}}; + 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}}; + return {Sent::No, ResponseContext{.status = http::Code::Accepted}}; } LOG_ERROR("Invalid request: %s", rebootType.c_str()); - return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; } } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/restore/RestoreEndpoint.cpp b/module-services/service-desktop/endpoints/restore/RestoreEndpoint.cpp index a378843905913d651421fe606566ec4b2a31fdac..20cb1386869bc96f1e27bfecfab55bb74b68daf3 100644 --- a/module-services/service-desktop/endpoints/restore/RestoreEndpoint.cpp +++ b/module-services/service-desktop/endpoints/restore/RestoreEndpoint.cpp @@ -11,10 +11,10 @@ namespace sdesktop::endpoints { auto [sent, response] = helper->process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + if (sent == Sent::No) { if (not response) { LOG_ERROR("Response not sent & response not created : respond with error"); context.setResponseStatus(http::Code::NotAcceptable); @@ -25,7 +25,7 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response) { + if (sent == Sent::Yes and response) { LOG_ERROR("Response set when we already handled response in handler"); } } diff --git a/module-services/service-desktop/endpoints/restore/RestoreHelper.cpp b/module-services/service-desktop/endpoints/restore/RestoreHelper.cpp index c98e8b52a0f320d6e8be06fbaf10161ad2d2aea9..af2a1371dd4b8c883f28712cb8a6506201a6bba1 100644 --- a/module-services/service-desktop/endpoints/restore/RestoreHelper.cpp +++ b/module-services/service-desktop/endpoints/restore/RestoreHelper.cpp @@ -10,9 +10,9 @@ namespace sdesktop::endpoints auto RestoreHelper::processPost([[maybe_unused]] Context &context) -> ProcessResult { if (sys::SystemManagerCommon::RebootToRecovery(owner, sys::RecoveryReason::Restore)) { - return {sent::no, ResponseContext{.status = http::Code::NoContent}}; + return {Sent::No, ResponseContext{.status = http::Code::NoContent}}; } - return {sent::no, ResponseContext{.status = http::Code::InternalServerError}}; + return {Sent::No, ResponseContext{.status = http::Code::InternalServerError}}; } } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/security/SecurityEndpoint.cpp b/module-services/service-desktop/endpoints/security/SecurityEndpoint.cpp index 5ab3277c540f2aed4b676df17447040cd17b59c8..eb575f9cec953241d62c90f510e7a656fe0febca 100644 --- a/module-services/service-desktop/endpoints/security/SecurityEndpoint.cpp +++ b/module-services/service-desktop/endpoints/security/SecurityEndpoint.cpp @@ -12,10 +12,10 @@ namespace sdesktop::endpoints { auto [sent, response] = helper->process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + if (sent == Sent::No) { if (not response) { LOG_ERROR("Response not sent & response not created : respond with error"); context.setResponseStatus(http::Code::NotAcceptable); @@ -26,7 +26,7 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response) { + if (sent == Sent::Yes and response) { LOG_ERROR("Response set when we already handled response in handler"); } } diff --git a/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp b/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp index fca174d88ce46b77d9065f05cfae19e91f681563..e2deaa871ae72e76a01df0baae77d8a714886d5a 100644 --- a/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp +++ b/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp @@ -23,13 +23,13 @@ namespace sdesktop::endpoints auto SecurityEndpointHelper::processPut(Context &context) -> ProcessResult { auto code = processConfiguration(context); - return {sent::no, ResponseContext{.status = code}}; + return {Sent::No, ResponseContext{.status = code}}; } auto SecurityEndpointHelper::processGet(Context &context) -> ProcessResult { if (context.getBody()[json::messages::category].string_value() == json::usb::phoneLockStatus) { - return {sent::no, processStatus(context)}; + return {Sent::No, processStatus(context)}; } if (context.getBody()[json::messages::category].string_value() == json::usb::phoneLockTime) { if (auto phoneLockTime = getPhoneLockTime(context); phoneLockTime > std::time(nullptr)) { @@ -43,9 +43,9 @@ namespace sdesktop::endpoints context.setResponseStatus(http::Code::UnprocessableEntity); } putToSendQueue(context.createSimpleResponse()); - return {sent::yes, std::nullopt}; + return {Sent::Yes, std::nullopt}; } - return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; } auto SecurityEndpointHelper::processStatus(Context & /*context*/) -> ResponseContext @@ -119,5 +119,4 @@ namespace sdesktop::endpoints return status; } - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/timeSync/TimeSyncEndpoint.cpp b/module-services/service-desktop/endpoints/timeSync/TimeSyncEndpoint.cpp index d628bf6f2591de8b3d61da14dafa497c4548f07d..196035eaca83d6b1c3e3c55d043f6636f2ba60b3 100644 --- a/module-services/service-desktop/endpoints/timeSync/TimeSyncEndpoint.cpp +++ b/module-services/service-desktop/endpoints/timeSync/TimeSyncEndpoint.cpp @@ -11,10 +11,10 @@ namespace sdesktop::endpoints { const auto &[sent, response] = helper->process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + 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); @@ -25,7 +25,7 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response.has_value()) { + if (sent == Sent::Yes and response.has_value()) { LOG_ERROR("Response set when we already handled response in handler"); } } diff --git a/module-services/service-desktop/endpoints/timeSync/TimeSyncHelper.cpp b/module-services/service-desktop/endpoints/timeSync/TimeSyncHelper.cpp index 4ed16891fb6cad5234f69f21bb91cee4980eac82..dcdc4f2849dcbe7660f4a7d19b89752e01ce02f1 100644 --- a/module-services/service-desktop/endpoints/timeSync/TimeSyncHelper.cpp +++ b/module-services/service-desktop/endpoints/timeSync/TimeSyncHelper.cpp @@ -11,6 +11,22 @@ namespace sdesktop::endpoints { + auto TimeSyncHelper::processGet(Context &context) -> ProcessResult + { + const auto &body = context.getBody(); + + /* Validate */ + if (body[json::timeSync::value] != json::timeSync::timestamp) { + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; + } + + std::time_t currentTimestamp; + std::time(¤tTimestamp); + + json11::Json::object response({{json::timeSync::timestamp, currentTimestamp}}); + return {Sent::No, ResponseContext{.status = http::Code::OK, .body = std::move(response)}}; + } + auto TimeSyncHelper::processPost(Context &context) -> ProcessResult { const auto &jsonTimestamp = context.getBody()[json::timeSync::timestamp]; @@ -18,17 +34,17 @@ namespace sdesktop::endpoints /* Validate */ if (!jsonTimestamp.is_number()) { LOG_ERROR("Timestamp data type not a number!"); - return {sent::no, ResponseContext{.status = http::Code::UnprocessableEntity}}; + return {Sent::No, ResponseContext{.status = http::Code::UnprocessableEntity}}; } const auto timestamp = static_cast(jsonTimestamp.number_value()); logReceivedDateTime(timestamp); if (const auto success = sendTimeUpdateMessage(timestamp); !success) { LOG_ERROR("Failed to send time update message!"); - return {sent::no, ResponseContext{.status = http::Code::InternalServerError}}; + return {Sent::No, ResponseContext{.status = http::Code::InternalServerError}}; } - return {sent::no, ResponseContext{.status = http::Code::OK}}; + return {Sent::No, ResponseContext{.status = http::Code::OK}}; } auto TimeSyncHelper::logReceivedDateTime(std::time_t timestamp) const noexcept -> void diff --git a/module-services/service-desktop/endpoints/update/UpdateEndpoint.cpp b/module-services/service-desktop/endpoints/update/UpdateEndpoint.cpp index f81da42982a2833cecadc69366daa3c5e5a18082..1a8d6b8ab530c4824cf977191cdb680a7c55b92f 100644 --- a/module-services/service-desktop/endpoints/update/UpdateEndpoint.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateEndpoint.cpp @@ -8,15 +8,14 @@ namespace sdesktop::endpoints { - auto UpdateEndpoint::handle(Context &context) -> void { auto &p = helperSwitcher(context); auto [sent, response] = p.process(context.getMethod(), context); - if (sent == sent::delayed) { + if (sent == Sent::Delayed) { LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); } - if (sent == sent::no) { + if (sent == Sent::No) { if (not response) { LOG_ERROR("Response not sent & response not created : respond with error"); context.setResponseStatus(http::Code::NotAcceptable); @@ -27,7 +26,7 @@ namespace sdesktop::endpoints sender::putToSendQueue(context.createSimpleResponse()); } - if (sent == sent::yes and response) { + if (sent == Sent::Yes and response) { LOG_ERROR("Response set when we already handled response in handler"); } } @@ -36,5 +35,4 @@ namespace sdesktop::endpoints { return *updateHelper; } - } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/update/UpdateHelper.cpp b/module-services/service-desktop/endpoints/update/UpdateHelper.cpp index 14813546b9cf751395dd94af9847c8b581ccea8f..6d6f18e9d07c39e36821d672fba25995d5fabd93 100644 --- a/module-services/service-desktop/endpoints/update/UpdateHelper.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateHelper.cpp @@ -43,7 +43,7 @@ namespace sdesktop::endpoints Entry recovery; }; - std::optional fetchVersionJsonFromFile(const std::filesystem::path &path) + auto fetchVersionJsonFromFile(const std::filesystem::path &path) -> std::optional { std::ifstream versionJsonFile{path}; if (not versionJsonFile.is_open()) { @@ -60,7 +60,7 @@ namespace sdesktop::endpoints return versionJson; } - std::optional getUpdatePackageEntries(const std::filesystem::path &path) + auto getUpdatePackageEntries(const std::filesystem::path &path) -> std::optional { if (const auto version = fetchVersionJsonFromFile(path)) { return UpdatePackageEntries{version.value()}; @@ -68,7 +68,7 @@ namespace sdesktop::endpoints return std::nullopt; } - bool validateImageEntry(const std::filesystem::path &path, const std::string &hash) + auto validateImageEntry(const std::filesystem::path &path, const std::string &hash) -> bool { auto fd = std::fopen(path.c_str(), "rb"); if (fd == nullptr) { @@ -88,7 +88,7 @@ namespace sdesktop::endpoints return (md5.getHash() == hash); } - bool removeDirectory(const std::filesystem::path &path) + auto removeDirectory(const std::filesystem::path &path) -> bool { if (std::filesystem::is_directory(path)) { LOG_INFO("Removing directory: %s", path.c_str()); @@ -100,7 +100,7 @@ namespace sdesktop::endpoints return true; } - bool unpackUpdatePackage(const std::filesystem::path &path, const std::filesystem::path &where) + auto unpackUpdatePackage(const std::filesystem::path &path, const std::filesystem::path &where) -> bool { if (not removeDirectory(where)) { LOG_ERROR("Removing '%s' directory failed", path.c_str()); @@ -118,7 +118,8 @@ namespace sdesktop::endpoints return true; } - bool validateUpdatePackage(const std::filesystem::path &packagePath, const std::filesystem::path &binariesPath) + auto validateUpdatePackage(const std::filesystem::path &packagePath, const std::filesystem::path &binariesPath) + -> bool { LOG_INFO("Validating '%s' package", packagePath.c_str()); const auto entries = getUpdatePackageEntries(packagePath / purefs::file::version_json); @@ -132,7 +133,7 @@ namespace sdesktop::endpoints validateImageEntry(prefix / entries->bootloader.first, entries->bootloader.second); } - bool checkAvailableSpace(const std::filesystem::path &path, const std::filesystem::path &updatePackage) + auto checkAvailableSpace(const std::filesystem::path &path, const std::filesystem::path &updatePackage) -> bool { struct statvfs stat {}; @@ -156,13 +157,13 @@ namespace sdesktop::endpoints return freeSpace >= requiredSpace; } - bool checkUpdatePackageFile(const std::filesystem::path &path) + auto checkUpdatePackageFile(const std::filesystem::path &path) -> bool { LOG_INFO("Checking if update package exist, '%s'", path.c_str()); return std::filesystem::exists(path); } - void UpdateHelper::preProcess(http::Method method, [[maybe_unused]] Context &context) + auto UpdateHelper::preProcess(http::Method method, [[maybe_unused]] Context &context) -> void { LOG_INFO("UpdateHelper requesting: %s", magic_enum::enum_name(method).data()); } @@ -172,32 +173,32 @@ namespace sdesktop::endpoints const auto &body = context.getBody(); if (body[json::update::update] != true || body[json::update::reboot] != true) { - return {sent::no, ResponseContext{.status = http::Code::BadRequest}}; + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; } if (not checkUpdatePackageFile(purefs::dir::getTemporaryPath() / sdesktop::paths::updateFilename)) { - return {sent::no, ResponseContext{.status = http::Code::NotFound}}; + return {Sent::No, ResponseContext{.status = http::Code::NotFound}}; } if (not checkAvailableSpace(purefs::dir::getUserDiskPath(), purefs::dir::getTemporaryPath() / sdesktop::paths::updateFilename)) { - return {sent::no, ResponseContext{.status = http::Code::NotAcceptable}}; + return {Sent::No, ResponseContext{.status = http::Code::NotAcceptable}}; } if (not unpackUpdatePackage(purefs::dir::getTemporaryPath() / sdesktop::paths::updateFilename, updatePackagePath)) { - return {sent::no, ResponseContext{.status = http::Code::UnprocessableEntity}}; + return {Sent::No, ResponseContext{.status = http::Code::UnprocessableEntity}}; } if (not validateUpdatePackage(updatePackagePath, binariesPath)) { - return {sent::no, ResponseContext{.status = http::Code::UnprocessableEntity}}; + return {Sent::No, ResponseContext{.status = http::Code::UnprocessableEntity}}; } if (sys::SystemManagerCommon::RebootToRecovery(owner, sys::RecoveryReason::Update)) { - return {sent::no, ResponseContext{.status = http::Code::NoContent}}; + return {Sent::No, ResponseContext{.status = http::Code::NoContent}}; } - return {sent::no, ResponseContext{.status = http::Code::InternalServerError}}; + return {Sent::No, ResponseContext{.status = http::Code::InternalServerError}}; } UpdateHelper::UpdateHelper(sys::Service *p)