~aleteoryx/muditaos

ref: d5de12f7cea071555de4058ebf62b160a86a36af muditaos/module-apps/application-desktop/widgets/PinLockHandler.cpp -rw-r--r-- 4.9 KiB
d5de12f7 — Radosław Wicik [EGD-3852] clean include in service (#928) 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
// 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 <service-cellular/ServiceCellular.hpp>

namespace gui
{
    constexpr unsigned int default_screen_pin_size = 4;
    constexpr unsigned int default_screen_attempts = 4;

    PinLockHandler::PinLockHandler(app::ApplicationDesktop *app, SettingsRecord &settings)
        : app(app), appSettings(settings), lock(this)
    {
        reloadScreenLock();
    }

    auto PinLockHandler::handle(CellularSimResponseMessage *msg) -> bool
    {
        assert(msg);

        parseSimCard(msg);
        parseSimState(msg);
        parseAttemptsAndPinSize(msg);
        lock.additionalLockInfo[gui::PinLock::InfoName::PhoneNum] = msg->getPhoneNumber().getFormatted();

        app->getWindow(app::window::name::desktop_pin_lock)->rebuild();
        app->switchWindow(gui::name::window::main_window);
        return true;
    }

    void PinLockHandler::handle(const std::vector<unsigned int> &pin)
    {
        if (lock.getLockType() == gui::PinLock::LockType::Screen) {
            handleScreenPin(pin);
        }
        else {
            handleSimPinOrPuk(pin);
        }
    }

    void PinLockHandler::parseSimState(CellularSimResponseMessage *msg)
    {
        assert(msg);
        switch (msg->getSimState()) {
        case CellularSimResponseMessage::SimState::SIMUnlocked:
            lock.type  = gui::PinLock::LockType::Screen;
            lock.state = gui::PinLock::State::EnterPin;
            break;
        case CellularSimResponseMessage::SimState::PINRequired:
            lock.type  = gui::PinLock::LockType::SIM;
            lock.state = gui::PinLock::State::EnterPin;
            break;
        case CellularSimResponseMessage::SimState::PINInvalidRetryPossible:
            lock.type  = gui::PinLock::LockType::SIM;
            lock.state = gui::PinLock::State::InvalidPin;
            break;
        case CellularSimResponseMessage::SimState::PUKRequired:
            lock.type  = gui::PinLock::LockType::PUK;
            lock.state = gui::PinLock::State::EnterPin;
            break;
        case CellularSimResponseMessage::SimState::PUKInvalidRetryPossible:
            lock.type  = gui::PinLock::LockType::PUK;
            lock.state = gui::PinLock::State::InvalidPin;
            break;
        case CellularSimResponseMessage::SimState::SIMBlocked:
            lock.type  = gui::PinLock::LockType::PUK;
            lock.state = gui::PinLock::State::Blocked;
            break;
        }
    }

    void PinLockHandler::parseSimCard(CellularSimResponseMessage *msg)
    {
        assert(msg);
        switch (msg->getSimCard()) {
        case CellularSimMessage::SimCard::SIM1:
            lock.additionalLockInfo[gui::PinLock::InfoName::LockName] = "SIM1";
            break;
        case CellularSimMessage::SimCard::SIM2:
            lock.additionalLockInfo[gui::PinLock::InfoName::LockName] = "SIM2";
            break;
        }
    }

    void PinLockHandler::parseAttemptsAndPinSize(CellularSimResponseMessage *msg)
    {
        if (msg->getSimState() == CellularSimResponseMessage::SimState::SIMUnlocked) {
            reloadScreenLock();
        }
        else {
            lock.pinValue          = std::vector<unsigned int>(msg->getPinSize(), 0);
            lock.remainingAttempts = msg->getAttemptsLeft();
        }
    }

    void PinLockHandler::handleScreenPin(const std::vector<unsigned int> &pin)
    {
        if (lock.remainingAttempts > 0) {
            std::hash<std::vector<unsigned int>> hashEngine;
            uint32_t hash = hashEngine(pin);
            lock.remainingAttempts--;
            if (hash == appSettings.lockPassHash) {
                lock.remainingAttempts = default_screen_attempts;
                lock.state             = gui::PinLock::State::VerifiedPin;
            }
            else if (lock.remainingAttempts > 0) {
                lock.state = gui::PinLock::State::InvalidPin;
            }
            else {
                lock.state = gui::PinLock::State::Blocked;
            }

            app->switchWindow(app::window::name::desktop_pin_lock);
        }
    }

    void PinLockHandler::handleSimPinOrPuk(const std::vector<unsigned int> &pin)
    {
        auto sim = CellularSimMessage::SimCard::SIM1;
        sys::Bus::SendUnicast(
            std::make_shared<CellularSimVerifyPinRequestMessage>(sim, pin), ServiceCellular::serviceName, app);
    }

    void PinLockHandler::reloadScreenLock()
    {
        lock.type  = gui::PinLock::LockType::Screen;
        lock.state = gui::PinLock::State::EnterPin;

        unsigned int pinSize   = appSettings.lockPassHash == 0 ? 0 : default_screen_pin_size;
        lock.pinValue          = std::vector<unsigned int>(pinSize, 0);
        lock.remainingAttempts = default_screen_attempts;
    }

} // namespace gui