From f8c83ce25c99b8e2d425908415f80ca662c91c60 Mon Sep 17 00:00:00 2001 From: SP2FET Date: Mon, 21 Dec 2020 12:27:43 +0100 Subject: [PATCH] [EGD-4869] Add bluetooth message status to application settings This change demonstrates how to work with Bluetooth messages in application settings windows. Responses to bluetooth status messages are mocked because settings storage and message handlers in Service Bluetooth are not ready yet. --- .../ApplicationSettings.cpp | 10 ++++- .../windows/BluetoothWindow.cpp | 41 ++++++++++--------- .../windows/BluetoothWindow.hpp | 27 ++++++++++-- .../service-bluetooth/ServiceBluetooth.cpp | 16 +++++++- .../service-bluetooth/ServiceBluetooth.hpp | 2 + 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/module-apps/application-settings-new/ApplicationSettings.cpp b/module-apps/application-settings-new/ApplicationSettings.cpp index 4b2d8fc39e0fa9bbeb268907e4f163eb8120e901..3ebc8c69ae97eecf3660175220ab82132bdb0a30 100644 --- a/module-apps/application-settings-new/ApplicationSettings.cpp +++ b/module-apps/application-settings-new/ApplicationSettings.cpp @@ -30,8 +30,7 @@ #include #include #include -#include -#include +#include namespace app { @@ -91,6 +90,13 @@ namespace app currentWindow->rebuild(); } } + else if (auto responseStatusMsg = dynamic_cast(msgl); + nullptr != responseStatusMsg) { + if (gui::window::name::bluetooth == getCurrentWindow()->getName()) { + auto btStatusData = std::make_unique(responseStatusMsg->getStatus()); + switchWindow(gui::window::name::bluetooth, std::move(btStatusData)); + } + } return std::make_shared(); } diff --git a/module-apps/application-settings-new/windows/BluetoothWindow.cpp b/module-apps/application-settings-new/windows/BluetoothWindow.cpp index 7645e93a96616d3e169606de291264eea4183a5f..d3fbc8a258a35250724e2af491a47b28b8e5fac6 100644 --- a/module-apps/application-settings-new/windows/BluetoothWindow.cpp +++ b/module-apps/application-settings-new/windows/BluetoothWindow.cpp @@ -6,8 +6,9 @@ #include "OptionSetting.hpp" -#include -#include +#include +#include +#include namespace gui { @@ -16,10 +17,17 @@ namespace gui { topBar->setActive(TopBar::Elements::BATTERY, false); topBar->setActive(TopBar::Elements::SIM, false); + sys::Bus::SendUnicast( + std::make_shared(), service::name::bluetooth, application); } void BluetoothWindow::onBeforeShow(ShowMode mode, SwitchData *data) { + if (data != nullptr) { + const auto newData = static_cast(data); + isBluetoothSwitchOn = newData->getState(); + isPhoneVisibilitySwitchOn = newData->getVisibility(); + } rebuildOptionList(); } @@ -30,7 +38,7 @@ namespace gui optionsList.emplace_back(std::make_unique( utils::translateI18("app_settings_bluetooth_main"), [=](gui::Item &item) { - bluetoothSwitchHandler(isBluetoothSwitchOn); + switchHandler(isBluetoothSwitchOn); return true; }, [=](gui::Item &item) { @@ -62,7 +70,7 @@ namespace gui optionsList.emplace_back(std::make_unique( utils::translateI18("app_settings_bluetooth_phone_visibility"), [=](gui::Item &item) { - phoneVisibilitySwitchHandler(isPhoneVisibilitySwitchOn); + switchHandler(isPhoneVisibilitySwitchOn); return true; }, [=](gui::Item &item) { @@ -96,27 +104,22 @@ namespace gui return optionsList; } - void BluetoothWindow::bluetoothSwitchHandler(bool &switchState) + void BluetoothWindow::switchHandler(bool &switchState) { - if (switchState) { - sys::Bus::SendUnicast( - std::make_shared(BluetoothMessage::Request::Stop), "ServiceBluetooth", application); + switchState = !switchState; + BluetoothStatus btStatus; + + if (isBluetoothSwitchOn) { + btStatus.state = BluetoothStatus::BluetoothState::On; } else { - sys::Bus::SendUnicast( - std::make_shared(BluetoothMessage::Request::Start), "ServiceBluetooth", application); + btStatus.state = BluetoothStatus::BluetoothState::Off; } - switchState = !switchState; - rebuildOptionList(); - } + btStatus.visibility = isPhoneVisibilitySwitchOn; + message::bluetooth::SetStatus setStatus(btStatus); - void BluetoothWindow::phoneVisibilitySwitchHandler(bool &switchState) - { sys::Bus::SendUnicast( - std::make_shared(BluetoothMessage::Request::Visible), "ServiceBluetooth", application); - - switchState = !switchState; - rebuildOptionList(); + std::make_shared(setStatus), service::name::bluetooth, application); } void BluetoothWindow::rebuildOptionList() diff --git a/module-apps/application-settings-new/windows/BluetoothWindow.hpp b/module-apps/application-settings-new/windows/BluetoothWindow.hpp index 158182e5f7ee799836c8f82425c11c3dfa61bf9e..e2de64e9c81e52097e217f4d57c5794f88b3e721 100644 --- a/module-apps/application-settings-new/windows/BluetoothWindow.hpp +++ b/module-apps/application-settings-new/windows/BluetoothWindow.hpp @@ -4,6 +4,7 @@ #pragma once #include +#include namespace gui { @@ -17,8 +18,28 @@ namespace gui bool isBluetoothSwitchOn = false; bool isPhoneVisibilitySwitchOn = false; auto bluetoothOptionsList() -> std::list; - void bluetoothSwitchHandler(bool &switchState); - void phoneVisibilitySwitchHandler(bool &switchState); + void switchHandler(bool &switchState); void rebuildOptionList(); }; -}; // namespace gui + + class BluetoothStatusData : public SwitchData + { + public: + explicit BluetoothStatusData(BluetoothStatus status) : SwitchData(), status(std::move(status)) + {} + [[nodiscard]] auto getState() const noexcept -> bool + { + if (status.state == BluetoothStatus::BluetoothState::On) { + return true; + } + return false; + } + [[nodiscard]] auto getVisibility() const noexcept -> bool + { + return status.visibility; + } + + private: + BluetoothStatus status; + }; +} // namespace gui diff --git a/module-services/service-bluetooth/ServiceBluetooth.cpp b/module-services/service-bluetooth/ServiceBluetooth.cpp index adc15c9d8579e7f5720337cd00578e98bef4f5f3..1ed19649866716c6405ef45898cea40536b7e227 100644 --- a/module-services/service-bluetooth/ServiceBluetooth.cpp +++ b/module-services/service-bluetooth/ServiceBluetooth.cpp @@ -13,6 +13,8 @@ #include #include #include +#include "service-bluetooth/messages/Status.hpp" +#include "service-bluetooth/messages/SetStatus.hpp" #include @@ -37,7 +39,8 @@ sys::ReturnCodes ServiceBluetooth::InitHandler() { LOG_ERROR("Bluetooth experimental!"); worker = std::make_unique(this); - + btStatus.state = BluetoothStatus::BluetoothState::On; + btStatus.visibility = true; return sys::ReturnCodes::Success; } @@ -49,6 +52,17 @@ sys::ReturnCodes ServiceBluetooth::DeinitHandler() sys::MessagePointer ServiceBluetooth::DataReceivedHandler(sys::DataMessage *msg, sys::ResponseMessage *resp) { + // mock response on message::bluetooth::RequestStatus + if (auto requestStatusMsg = dynamic_cast(msg); nullptr != requestStatusMsg) { + sys::Bus::SendUnicast(std::make_shared(btStatus), msg->sender, this); + } + + // temporary solution for handling message::bluetooth::SetStatus + if (auto setStatusMsg = dynamic_cast(msg); nullptr != setStatusMsg) { + btStatus = setStatusMsg->getStatus(); + sys::Bus::SendBroadcast(std::make_shared(btStatus), this); + } + try { switch (static_cast(msg->messageType)) { case MessageType::BluetoothRequest: { diff --git a/module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp b/module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp index 916ce4067530496c6bce36a129f4eca94f967830..77b1d4b45c1d5ff587309fd97161f832410024bb 100644 --- a/module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp +++ b/module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp @@ -8,6 +8,7 @@ #include #include #include "service-bluetooth/SettingsHolder.hpp" +#include "BluetoothMessage.hpp" #include // for unique_ptr @@ -32,4 +33,5 @@ class ServiceBluetooth : public sys::Service private: std::unique_ptr worker; std::unique_ptr settingsHolder; + BluetoothStatus btStatus; // will be replaced with settings storage introduced in [EGD-4579] };