~aleteoryx/muditaos

3c1067cd87c5b45c12e95a62628cb3c440cd4ffc — Lefucjusz 3 years ago c9b16c3
[MOS-421] Add state refresh when BT automatically turns off

Added code to update BT power state
when it automatically turns off due
to inactivity.
M module-apps/application-settings/ApplicationSettings.cpp => module-apps/application-settings/ApplicationSettings.cpp +17 -0
@@ 66,6 66,7 @@
#include <service-bluetooth/BluetoothMessage.hpp>
#include <service-bluetooth/Constants.hpp>
#include <service-bluetooth/messages/BondedDevices.hpp>
#include <service-bluetooth/messages/BluetoothModeChanged.hpp>
#include <service-bluetooth/messages/Connect.hpp>
#include <service-bluetooth/messages/DeviceName.hpp>
#include <service-bluetooth/messages/InitializationResult.hpp>


@@ 105,6 106,7 @@ namespace app
    {
        bus.channels.push_back(sys::BusChannel::ServiceAudioNotifications);
        bus.channels.push_back(sys::BusChannel::BluetoothNotifications);
        bus.channels.push_back(sys::BusChannel::BluetoothModeChanges);

        CellularServiceAPI::SubscribeForOwnNumber(this, [&](const std::string &number) {
            selectedSimNumber = number;


@@ 158,6 160,21 @@ namespace app
            return sys::MessageNone{};
        });

        connect(typeid(::sys::bluetooth::BluetoothModeChanged), [&](sys::Message *msg) {
            const auto message   = static_cast<::sys::bluetooth::BluetoothModeChanged *>(msg);
            const auto newState  = message->getBluetoothMode();
            const auto oldStatus = bluetoothSettingsModel->getStatus();
            const auto oldState  = oldStatus.state;
            if (newState == sys::bluetooth::BluetoothMode::Disabled && oldState == BluetoothStatus::State::On) {
                const auto isVisible = oldStatus.visibility;
                bluetoothSettingsModel->updateStatus(false, isVisible);
                if (getCurrentWindow()->getName() == gui::window::name::bluetooth) {
                    updateCurrentWindow();
                }
            }
            return sys::MessageNone{};
        });

        connect(typeid(::message::bluetooth::ResponseStatus), [&](sys::Message *msg) {
            auto responseStatusMsg = static_cast<::message::bluetooth::ResponseStatus *>(msg);
            const auto status         = responseStatusMsg->getStatus();

M module-apps/application-settings/models/bluetooth/BluetoothSettingsModel.cpp => module-apps/application-settings/models/bluetooth/BluetoothSettingsModel.cpp +6 -1
@@ 22,10 22,15 @@ void BluetoothSettingsModel::requestStatus()
    service->bus.sendUnicast(std::make_shared<::message::bluetooth::RequestStatus>(), service::name::bluetooth);
}

void BluetoothSettingsModel::setStatus(const bool desiredBluetoothState, const bool desiredVisibility)
void BluetoothSettingsModel::updateStatus(const bool desiredBluetoothState, const bool desiredVisibility)
{
    status.state      = desiredBluetoothState ? BluetoothStatus::State::On : BluetoothStatus::State::Off;
    status.visibility = desiredVisibility;
}

void BluetoothSettingsModel::setStatus(const bool desiredBluetoothState, const bool desiredVisibility)
{
    updateStatus(desiredBluetoothState, desiredVisibility);
    message::bluetooth::SetStatus setStatus(status);
    service->bus.sendUnicast(std::make_shared<::message::bluetooth::SetStatus>(std::move(setStatus)),
                             service::name::bluetooth);

M module-apps/application-settings/models/bluetooth/BluetoothSettingsModel.hpp => module-apps/application-settings/models/bluetooth/BluetoothSettingsModel.hpp +1 -0
@@ 18,6 18,7 @@ class BluetoothSettingsModel
    explicit BluetoothSettingsModel(sys::Service *service);

    void requestStatus();
    void updateStatus(bool desiredBluetoothState, bool desiredVisibility);
    void setStatus(bool desiredBluetoothState, bool desiredVisibility);
    void requestDeviceName();
    void setDeviceName(const UTF8 &deviceName);

M module-apps/application-settings/windows/bluetooth/BluetoothWindow.cpp => module-apps/application-settings/windows/bluetooth/BluetoothWindow.cpp +1 -1
@@ 13,7 13,7 @@ namespace gui

    BluetoothWindow::BluetoothWindow(app::ApplicationCommon *app,
                                     std::shared_ptr<BluetoothSettingsModel> bluetoothSettingsModel)
        : BaseSettingsWindow(app, window::name::bluetooth), bluetoothSettingsModel(bluetoothSettingsModel)
        : BaseSettingsWindow(app, window::name::bluetooth), bluetoothSettingsModel(std::move(bluetoothSettingsModel))
    {
        setTitle(utils::translate("app_settings_bluetooth_main"));
    }

M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +20 -15
@@ 81,8 81,11 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
    auto sentinelRegistrationMsg = std::make_shared<sys::SentinelRegistrationMessage>(cpuSentinel);
    bus.sendUnicast(sentinelRegistrationMsg, service::name::system_manager);

    connectionTimeoutTimer = sys::TimerFactory::createSingleShotTimer(
        this, "btTimeoutTimer", connectionTimeout, [this](sys::Timer &_) { handleTurnOff(); });
    connectionTimeoutTimer =
        sys::TimerFactory::createSingleShotTimer(this, "btTimeoutTimer", connectionTimeout, [this](sys::Timer &_) {
            LOG_INFO("Turning off Bluetooth due to inactivity timeout");
            handleTurnOff();
        });
    startTimeoutTimer();

    connectHandler<BluetoothAddrMessage>();


@@ 191,21 194,16 @@ auto ServiceBluetooth::handle(message::bluetooth::SetStatus *msg) -> std::shared
    auto newBtStatus = msg->getStatus();

    switch (newBtStatus.state) {
    case BluetoothStatus::State::On:
    case BluetoothStatus::State::On: {
        handleTurnOn();

        auto bondedDevicesStr =
            std::visit(bluetooth::StringVisitor(), this->settingsHolder->getValue(bluetooth::Settings::BondedDevices));
        bluetoothDevicesModel->mergeDevicesList(SettingsSerializer::fromString(bondedDevicesStr));
        bluetoothDevicesModel->syncDevicesWithApp();

        cpuSentinel->HoldMinimumFrequency(bsp::CpuFrequencyMHz::Level_3);
        sendWorkerCommand(std::make_unique<bluetooth::event::PowerOn>());
        bus.sendMulticast(
            std::make_shared<sys::bluetooth::BluetoothModeChanged>(sys::bluetooth::BluetoothMode::Enabled),
            sys::BusChannel::BluetoothModeChanges);
        {
            auto bondedDevicesStr = std::visit(bluetooth::StringVisitor(),
                                               this->settingsHolder->getValue(bluetooth::Settings::BondedDevices));
            bluetoothDevicesModel->mergeDevicesList(SettingsSerializer::fromString(bondedDevicesStr));
            bluetoothDevicesModel->syncDevicesWithApp();
        }
        startTimeoutTimer();
        break;
    } break;
    case BluetoothStatus::State::Off:
        stopTimeoutTimer();
        handleTurnOff();


@@ 543,6 541,13 @@ void ServiceBluetooth::handleTurnOff()
    bus.sendMulticast(std::make_shared<sys::bluetooth::BluetoothModeChanged>(sys::bluetooth::BluetoothMode::Disabled),
                      sys::BusChannel::BluetoothModeChanges);
}
void ServiceBluetooth::handleTurnOn()
{
    cpuSentinel->HoldMinimumFrequency(bsp::CpuFrequencyMHz::Level_3);
    sendWorkerCommand(std::make_unique<bluetooth::event::PowerOn>());
    bus.sendMulticast(std::make_shared<sys::bluetooth::BluetoothModeChanged>(sys::bluetooth::BluetoothMode::Enabled),
                      sys::BusChannel::BluetoothModeChanges);
}
auto ServiceBluetooth::handle(message::bluetooth::RequestStatusIndicatorData *msg) -> std::shared_ptr<sys::Message>
{
    bus.sendUnicast(std::make_shared<cellular::RequestCurrentOperatorNameMessage>(), cellular::service::name);

M module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp => module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp +1 -0
@@ 94,6 94,7 @@ class ServiceBluetooth : public sys::Service
    void sendWorkerCommand(std::unique_ptr<bluetooth::event::Base> command);

    void handleTurnOff();
    void handleTurnOn();

    std::shared_ptr<Mailbox<bluetooth::Command, QueueHandle_t, WorkerLock>> workerQueue;
    std::shared_ptr<bluetooth::SettingsHolder> settingsHolder;