~aleteoryx/muditaos

ref: 9752788304eae313b3e4deef08a951a3c6455e31 muditaos/products/PurePhone/sys/SystemManager.cpp -rw-r--r-- 8.9 KiB
97527883 — Lukasz Mastalerz [BH-1412] Whitelist update 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 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>
#include <service-desktop/ServiceDesktopName.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 RegularClose
        {
            const std::vector<std::string> whitelist{service::name::evt_manager, service::name::cellular};
        } // namespace RegularClose
        namespace Update
        {
            const std::vector<std::string> whitelist{service::name::evt_manager, service::name::cellular};
        } // namespace Update
        namespace Restore
        {
            const std::vector<std::string> whitelist{
                service::name::service_desktop, // Handle restore procedure
                service::name::evt_manager, // Workaround for charging battery after shutting down and turn on the phone
                service::name::appmgr,
                service::name::cellular};
        } // namespace Restore
    }     // 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);
            DestroySystemService(service::name::evt_manager, this);
        }
        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);
        }
    }

    const std::vector<std::string> &SystemManager::getWhiteListFor(WhiteListType type)
    {
        switch (type) {
        case WhiteListType::RegularClose: {
            return RegularClose::whitelist;
        }
        case WhiteListType::Update: {
            return Update::whitelist;
        }
        case WhiteListType::Restore: {
            return Restore::whitelist;
        }
        }
        return RegularClose::whitelist;
    }
} // namespace sys