~aleteoryx/muditaos

ref: 462a7393d9395b9c776e14676c97988b7058637a muditaos/products/PurePhone/sys/SystemManager.cpp -rw-r--r-- 7.6 KiB
462a7393 — Lukasz Mastalerz [BH-1412] Fix system shutdown procedure 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <sys/SystemManager.hpp>
#include <sys/messages/PhoneModeRequest.hpp>
#include <sys/messages/TetheringPhoneModeChangeProhibitedMessage.hpp>

#include <evtmgr/EVMessages.hpp>
#include <system/messages/SystemManagerMessage.hpp>
#include <system/messages/TetheringQuestionRequest.hpp>
#include <system/messages/TetheringStateRequest.hpp>
#include <service-appmgr/ServiceApplicationManagerName.hpp>
#include <service-cellular/CellularServiceAPI.hpp>
#include <service-evtmgr/ServiceEventManagerName.hpp>
#include <service-evtmgr/EventManagerServiceAPI.hpp>

namespace sys
{
    namespace
    {
        const std::map<bsp::KeyCodes, phone_modes::PhoneMode> SliderStateToPhoneModeMapping = {
            {bsp::KeyCodes::SSwitchUp, phone_modes::PhoneMode::Connected},
            {bsp::KeyCodes::SSwitchMid, phone_modes::PhoneMode::DoNotDisturb},
            {bsp::KeyCodes::SSwitchDown, phone_modes::PhoneMode::Offline}};
    } // namespace

    SystemManager::SystemManager(std::vector<std::unique_ptr<BaseServiceCreator>> &&creators)
        : SystemManagerCommon(std::move(creators))
    {}

    void SystemManager::StartSystem(InitFunction sysInit, InitFunction appSpaceInit, DeinitFunction sysDeinit)
    {
        auto activeSimSelected = []() {
            const auto storeGsm = Store::GSM::get();
            return ((storeGsm->selected == Store::GSM::SelectedSIM::SIM1 ||
                     storeGsm->selected == Store::GSM::SelectedSIM::SIM2) &&
                    storeGsm->simCardInserted());
        };
        auto isCallOngoing = [this]() {
            auto request =
                async_call<cellular::IsCallActive, cellular::IsCallActiveResponse>(::service::name::cellular);
            sync(request);
            return request.getResult().active;
        };
        phoneModeSubject =
            std::make_unique<phone_modes::Subject>(this, std::move(activeSimSelected), std::move(isCallOngoing));
        SystemManagerCommon::StartSystem(std::move(sysInit), std::move(appSpaceInit), std::move(sysDeinit));
    }

    ReturnCodes SystemManager::InitHandler()
    {
        SystemManagerCommon::InitHandler();

        connect(typeid(PhoneModeRequest), [this](sys::Message *message) -> sys::MessagePointer {
            auto request = static_cast<PhoneModeRequest *>(message);
            return handlePhoneModeRequest(request);
        });

        connect(typeid(TetheringStateRequest), [this](sys::Message *message) -> sys::MessagePointer {
            auto request = static_cast<TetheringStateRequest *>(message);
            return handleTetheringStateRequest(request);
        });

        connect(typeid(TetheringEnabledResponse), [this](sys::Message *message) -> sys::MessagePointer {
            auto response = static_cast<TetheringEnabledResponse *>(message);
            return enableTethering(response);
        });

        connect(cellular::CheckIfStartAllowedMessage(), [&](Message *) {
            switch (Store::Battery::get().levelState) {
            case Store::Battery::LevelState::Normal:
                CellularServiceAPI::ChangeModulePowerState(this, cellular::service::State::PowerState::On);
                break;
            case Store::Battery::LevelState::CriticalCharging:
                [[fallthrough]];
            case Store::Battery::LevelState::CriticalNotCharging:
                CellularServiceAPI::ChangeModulePowerState(this, cellular::service::State::PowerState::Off);
                break;
            case Store::Battery::LevelState::Shutdown:
                break;
            }
            return MessageNone{};
        });

        return ReturnCodes::Success;
    }

    phone_modes::PhoneMode SystemManager::translateSliderState(const RawKey &key)
    {
        const auto code = key.keyCode;
        if (code != bsp::KeyCodes::SSwitchUp && code != bsp::KeyCodes::SSwitchMid &&
            code != bsp::KeyCodes::SSwitchDown) {
            throw std::invalid_argument{"Invalid key code passed."};
        }
        return SliderStateToPhoneModeMapping.at(code);
    }

    MessagePointer SystemManager::handlePhoneModeRequest(PhoneModeRequest *request)
    {
        LOG_INFO("Phone mode change requested.");
        if (phoneModeSubject->isTetheringEnabled()) {
            LOG_WARN("Changing phone mode when tethering is enabled!");
            // display popup
            bus.sendUnicast(std::make_shared<TetheringPhoneModeChangeProhibitedMessage>(), service::name::appmgr);
            return MessageNone{};
        }
        phoneModeSubject->setPhoneMode(request->getPhoneMode());
        return MessageNone{};
    }

    MessagePointer SystemManager::handleTetheringStateRequest(TetheringStateRequest *request)
    {
        LOG_INFO("Tethering state change requested");

        if (Store::Battery::get().levelState != Store::Battery::LevelState::Normal) {
            LOG_INFO("Tethering state change refused - battery too low");
            return MessageNone{};
        }

        if (const auto requestedState = request->getTetheringState(); requestedState == phone_modes::Tethering::On) {
            if (phoneModeSubject->isTetheringPossible()) {
                bus.sendUnicast(std::make_shared<TetheringQuestionRequest>(), service::name::appmgr);
            }
        }
        else {
            if (const auto tetheringChanged = phoneModeSubject->setTetheringMode(phone_modes::Tethering::Off);
                !tetheringChanged) {
                bus.sendUnicast(std::make_shared<TetheringQuestionAbort>(), service::name::appmgr);
            }
            else {
                // Turned on, disabling...
                LOG_INFO("Disabling tethering");
                bus.sendUnicast(std::make_shared<sevm::RequestPhoneModeForceUpdate>(), service::name::evt_manager);
            }
        }
        return MessageNone{};
    }

    MessagePointer SystemManager::enableTethering([[maybe_unused]] TetheringEnabledResponse *response)
    {
        phoneModeSubject->setTetheringMode(phone_modes::Tethering::On);
        return MessageNone{};
    }

    void SystemManager::batteryNormalLevelAction()
    {
        SystemManagerCommon::batteryNormalLevelAction();
        CellularServiceAPI::ChangeModulePowerState(this, cellular::service::State::PowerState::On);
        auto msg = std::make_shared<CriticalBatteryLevelNotification>(
            false, Store::Battery::get().state == Store::Battery::State::Charging);
        bus.sendUnicast(std::move(msg), service::name::appmgr);
    }

    void SystemManager::batteryCriticalLevelAction(bool charging)
    {
        SystemManagerCommon::batteryCriticalLevelAction(charging);
        CellularServiceAPI::ChangeModulePowerState(this, cellular::service::State::PowerState::Off);
        EventManagerServiceAPI::turnOffTorch(this);
        auto msg = std::make_shared<CriticalBatteryLevelNotification>(true, charging);
        bus.sendUnicast(std::move(msg), service::name::appmgr);
    }

    void SystemManager::handleShutdown()
    {
        // check if we are discharging - if so -> shutdown
        if (Store::Battery::get().state == Store::Battery::State::Discharging) {
            set(State::ShutdownReady);
        }
        else {
            // await from EvtManager for info that red key was pressed / timeout
            auto msg = mailbox.pop();
            if (!msg) {
                return;
            }
            if (msg->sender != service::name::evt_manager) {
                LOG_ERROR("Ignored msg from: %s on shutdown", msg->sender.c_str());
                return;
            }
            msg->Execute(this);
        }
    }
} // namespace sys