~aleteoryx/muditaos

ref: 84e08c242ff8bd90187890b255a35e6a27662e58 muditaos/module-apps/application-desktop/widgets/PinLockHandler.cpp -rw-r--r-- 9.2 KiB
84e08c24 — tomaszrogala [EGD-3512] Create ICS Parser (#892) 5 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
218
//// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
//// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "PinLockHandler.hpp"
#include "PinHash.hpp"
#include "application-desktop/ApplicationDesktop.hpp"
#include "application-desktop/data/LockPhoneData.hpp"
#include "application-desktop/windows/Names.hpp"

#include <module-utils/common_data/EventStore.hpp>

#include <service-appmgr/service-appmgr/data/SimActionsParams.hpp>
#include <service-cellular/CellularMessage.hpp>

namespace gui
{
    namespace
    {
        constexpr unsigned int default_screen_pin_size = 4;
        constexpr unsigned int screen_nopin_size       = 0;
        constexpr unsigned int default_attempts        = 4;
        constexpr unsigned int blocked_sim_attempts    = 0;
        constexpr unsigned int sim_max_passcode_size   = 8;
        constexpr unsigned int sim_min_passcode_size   = 4;
    } // namespace

    PinLockHandler::PinLockHandler(app::ApplicationDesktop *app, SettingsRecord &settings)
        : app(app), appSettings(settings), screenLock(PinLock::SimCard::NoCard,
                                                      PinLock::LockState::PasscodeRequired,
                                                      PinLock::LockType::Screen,
                                                      default_attempts),
          simLock(PinLock::SimCard::NoCard, PinLock::LockState::Unlocked, PinLock::LockType::SimPin, default_attempts)
    {
        reloadScreenLock();
        screenLock.setAutoActivate(true);

        simLock.setPinSizeBounds(sim_min_passcode_size, sim_max_passcode_size);
    }

    void PinLockHandler::handleScreenPin(const std::vector<unsigned int> &pin)
    {
        if (screenLock.getMaxPinSize() == screen_nopin_size) {
            screenLock.lockState = gui::PinLock::LockState::Unlocked;
        }
        else if (screenLock.value > 0) {
            std::hash<std::vector<unsigned int>> hashEngine;
            uint32_t hash = hashEngine(pin);
            screenLock.value--;
            if (hash == appSettings.lockPassHash) {
                screenLock.lockState = gui::PinLock::LockState::Unlocked;
                screenLock.value     = default_attempts;
            }
            else if (screenLock.value > 0) {
                screenLock.lockState = gui::PinLock::LockState::PasscodeInvalidRetryRequired;
            }
            else {
                screenLock.lockState = gui::PinLock::LockState::Blocked;
            }
        }
    }

    void PinLockHandler::handleScreenConfirm()
    {
        simLock.consumeState();
    }

    void PinLockHandler::reloadScreenLock()
    {
        screenLock.lockState = gui::PinLock::LockState::PasscodeRequired;
        unsigned int pinSize = appSettings.lockPassHash == 0 ? screen_nopin_size : default_screen_pin_size;
        screenLock.setPinSizeBounds(pinSize, pinSize);
        screenLock.value = default_attempts;
    }

    auto PinLockHandler::handlePasscodeParams(app::manager::actions::ActionParamsPtr &&data) const -> gui::PinLock
    {
        auto passcodeData = static_cast<app::manager::actions::PasscodeParams *>(data.get());

        gui::PinLock lock(parseSimCard(passcodeData->getSim()),
                          gui::PinLock::LockState::PasscodeRequired,
                          simLock.getLockType(),
                          passcodeData->getAttempts());

        if (simLock.getLockType() == lock.getLockType() && simLock.getState() == lock.getState() &&
            simLock.getValue() > lock.getValue()) {
            lock.lockState = PinLock::LockState::PasscodeInvalidRetryRequired;
        }
        lock.setPinSizeBounds(sim_min_passcode_size, sim_max_passcode_size);

        return lock;
    }

    void PinLockHandler::handlePinRequest(app::manager::actions::ActionParamsPtr &&data)
    {
        simLock          = handlePasscodeParams(std::move(data));
        simLock.lockType = gui::PinLock::LockType::SimPin;

        auto onActivatedCallback = [this](const std::vector<unsigned int> &data) { handlePasscode(data); };
        switchToPinlockWindow(onActivatedCallback);
    }

    void PinLockHandler::handlePukRequest(app::manager::actions::ActionParamsPtr &&data)
    {
        simLock          = handlePasscodeParams(std::move(data));
        simLock.lockType = gui::PinLock::LockType::SimPuk;

        auto onActivatedCallback = [this](const std::vector<unsigned int> &data) { handlePasscodeChange(data); };
        switchToPinlockWindow(onActivatedCallback);
    }

    void PinLockHandler::switchToPinlockWindow(
        std::function<void(const std::vector<unsigned int> &)> onLockActivatedCallback)
    {
        auto lock = std::make_unique<gui::PinLock>(simLock);

        if (lock->getState() == PinLock::LockState::PasscodeInvalidRetryRequired) {
            lock->onActivatedCallback = [this, onLockActivatedCallback](const std::vector<unsigned int> &) {
                simLock.consumeState();
                switchToPinlockWindow(onLockActivatedCallback);
            };
        }
        else {
            lock->onActivatedCallback = onLockActivatedCallback;
        }

        app->switchWindow(app::window::name::desktop_pin_lock,
                          gui::ShowMode::GUI_SHOW_INIT,
                          std::make_unique<gui::LockPhoneData>(std::move(lock)));
    }

    void PinLockHandler::handlePinChangeRequest(app::manager::actions::ActionParamsPtr &&data)
    {
        simLock          = handlePasscodeParams(std::move(data));
        simLock.lockType = gui::PinLock::LockType::SimPin;

        auto onActivatedCallback = [this](const std::vector<unsigned int> &data) { handlePasscodeChange(data); };
        switchToPinlockWindow(onActivatedCallback);
    }

    void PinLockHandler::handleSimBlocked(app::manager::actions::ActionParamsPtr &&data)
    {
        auto params = static_cast<app::manager::actions::SimStateParams *>(data.get());
        simLock     = PinLock(parseSimCard(params->getSim()),
                          gui::PinLock::LockState::Blocked,
                          gui::PinLock::LockType::SimPin,
                          blocked_sim_attempts);

        auto lock = std::make_unique<gui::PinLock>(simLock);
        app->switchWindow(app::window::name::desktop_pin_lock,
                          gui::ShowMode::GUI_SHOW_INIT,
                          std::make_unique<gui::LockPhoneData>(std::move(lock)));
    }

    void PinLockHandler::handleCMEError(app::manager::actions::ActionParamsPtr &&data) const
    {
        auto params = static_cast<app::manager::actions::UnhandledCMEParams *>(data.get());

        auto lock = std::make_unique<gui::PinLock>(parseSimCard(params->getSim()),
                                                   PinLock::LockState::ErrorOccurred,
                                                   PinLock::LockType::SimPin,
                                                   params->getCMECode());

        app->switchWindow(app::window::name::desktop_pin_lock,
                          gui::ShowMode::GUI_SHOW_INIT,
                          std::make_unique<gui::LockPhoneData>(std::move(lock)));
    }

    void PinLockHandler::handlePasscode(const std::vector<unsigned int> passcode) const
    {
        if (simLock.getLockType() == PinLock::LockType::SimPin) {
            sys::Bus::SendUnicast(
                std::make_shared<CellularSimPinDataMessage>(Store::GSM::SIM::SIM1, passcode), "ServiceCellular", app);
        }
        else if (simLock.getLockType() == PinLock::LockType::SimPuk) {
            handlePasscodeChange(passcode);
        }
    }
    void PinLockHandler::handlePasscodeChange(const std::vector<unsigned int> passcode) const
    {
        auto lock       = std::make_unique<gui::PinLock>(simLock);
        lock->lockState = PinLock::LockState::NewPasscodeRequired;

        lock->onActivatedCallback = [this, passcode](const std::vector<unsigned int> &pin) {
            handleNewPasscodeConfirmed(passcode, pin);
        };

        app->switchWindow(app::window::name::desktop_pin_lock,
                          gui::ShowMode::GUI_SHOW_INIT,
                          std::make_unique<gui::LockPhoneData>(std::move(lock)));
    }

    void PinLockHandler::handleNewPasscodeConfirmed(const std::vector<unsigned int> &passcode,
                                                    const std::vector<unsigned int> &pin) const
    {
        if (simLock.getLockType() == PinLock::LockType::SimPin) {
            sys::Bus::SendUnicast(std::make_shared<CellularSimNewPinDataMessage>(Store::GSM::SIM::SIM1, passcode, pin),
                                  "ServiceCellular",
                                  app);
        }
        else if (simLock.getLockType() == PinLock::LockType::SimPuk) {
            sys::Bus::SendUnicast(std::make_shared<CellularSimPukDataMessage>(Store::GSM::SIM::SIM1, passcode, pin),
                                  "ServiceCellular",
                                  app);
        }
    }

    auto PinLockHandler::parseSimCard(Store::GSM::SIM sim) const noexcept -> PinLock::SimCard
    {
        if (sim == Store::GSM::SIM::SIM1) {
            return gui::PinLock::SimCard::SIM1;
        }
        else if (sim == Store::GSM::SIM::SIM2) {
            return gui::PinLock::SimCard::SIM2;
        }
        return gui::PinLock::SimCard::NoCard;
    }

} // namespace gui