~aleteoryx/muditaos

ref: adb6a640857b297d3e67b035d5ccc813565236a2 muditaos/module-services/service-cellular/SimCard.cpp -rw-r--r-- 5.4 KiB
adb6a640 — breichel [EGD-4063] Handling PUK/PIN (#1014) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SimCard.hpp"
#include <log/log.hpp>
#include <variant>
#include "Result.hpp"
#include "UrcCpin.hpp" //for Cpin parseState
#include "UrcFactory.hpp"

SimCardResult SimCard::convertErrorFromATResult(const at::Result atres) const
{
    if (std::holds_alternative<at::EquipmentErrorCode>(atres.errorCode)) {

        auto cerr = static_cast<int>(std::get<at::EquipmentErrorCode>(atres.errorCode));
        if ((cerr > static_cast<int>(SimCardResult::AT_ERROR_Begin)) &&
            (cerr < static_cast<int>(SimCardResult::AT_ERROR_End))) {
            return static_cast<SimCardResult>(cerr);
        }
    }
    return SimCardResult::Unknown;
}

std::optional<at::response::qpinc::AttemptsCounters> SimCard::getAttemptsCounters() const
{
    auto channel = cellularService.cmux->get(TS0710::Channel::Commands);
    if (channel) {
        auto resp = channel->cmd(at::factory(at::AT::QPINC) + "\"SC\"");
        at::response::qpinc::AttemptsCounters ret;
        if (at::response::parseQPINC(resp, ret)) {
            return ret;
        }
    }

    return std::nullopt;
}

SimCardResult SimCard::supplyPin(const std::string pin) const
{
    if (auto pc = getAttemptsCounters(); pc) {
        if (pc.value().PinCounter > 0) {
            if (auto channel = cellularService.cmux->get(TS0710::Channel::Commands); channel) {
                auto resp = channel->cmd(at::factory(at::AT::CPIN) + "\"" + pin + "\"");

                if (resp.code == at::Result::Code::OK) {
                    return SimCardResult::OK;
                }
                else {
                    return convertErrorFromATResult(resp);
                }
            }
        }
        else {
            if (pc.value().PukCounter > 0) {
                return SimCardResult::SIM_PUKRequired;
            }
            else {
                return SimCardResult::Locked;
            }
        }
    }
    return SimCardResult::Unknown;
}

SimCardResult SimCard::supplyPuk(const std::string puk, const std::string pin) const
{
    if (auto pc = getAttemptsCounters(); pc) {
        if (pc.value().PukCounter != 0) {
            if (auto channel = cellularService.cmux->get(TS0710::Channel::Commands); channel) {
                auto resp = channel->cmd(at::factory(at::AT::CPIN) + "\"" + puk + "\"" + ",\"" + pin + "\"");
                if (resp.code == at::Result::Code::OK) {
                    return SimCardResult::OK;
                }
                else {
                    return convertErrorFromATResult(resp);
                }
            }
        }
        else {
            return SimCardResult::Locked;
        }
    }

    return SimCardResult::Unknown;
}

bool SimCard::isPinLocked() const
{
    if (auto channel = cellularService.cmux->get(TS0710::Channel::Commands); channel) {
        auto resp = channel->cmd(at::factory(at::AT::CLCK) + "\"SC\",2\r");
        int val   = 0;
        if (at::response::parseCLCK(resp, val)) {
            return val != 0;
        }
    }
    return true;
}

SimCardResult SimCard::setPinLock(bool lock, const std::string pin) const
{
    if (auto pc = getAttemptsCounters(); pc) {
        if (pc.value().PukCounter != 0) {
            auto channel = cellularService.cmux->get(TS0710::Channel::Commands);
            if (channel) {
                auto resp =
                    channel->cmd(at::factory(at::AT::CLCK) + "\"SC\"," + (lock ? "1" : "0") + ",\"" + pin + "\"");
                if (resp.code == at::Result::Code::OK) {
                    return SimCardResult::OK;
                }
                else {
                    return convertErrorFromATResult(resp);
                }
            }
        }
        else {
            return SimCardResult::Locked;
        }
    }

    return SimCardResult::Unknown;
}

SimCardResult SimCard::changePin(const std::string oldPin, const std::string newPin) const
{
    if (auto pc = getAttemptsCounters(); pc) {
        if (pc.value().PukCounter != 0) {
            auto channel = cellularService.cmux->get(TS0710::Channel::Commands);
            if (channel) {
                auto resp = channel->cmd(at::factory(at::AT::CPWD) + "\"SC\", \"" + oldPin + "\",\"" + newPin + "\"");
                if (resp.code == at::Result::Code::OK) {
                    return SimCardResult::OK;
                }
                else {
                    return convertErrorFromATResult(resp);
                }
            }
        }
        else {
            return SimCardResult::Locked;
        }
    }

    return SimCardResult::Unknown;
}

std::optional<at::SimState> SimCard::simState() const
{
    std::string buf;
    return simStateWithMessage(buf);
}

std::optional<at::SimState> SimCard::simStateWithMessage(std::string &message) const
{
    if (auto channel = cellularService.cmux->get(TS0710::Channel::Commands); channel) {
        auto resp = channel->cmd(at::factory(at::AT::GET_CPIN));
        if (resp.code == at::Result::Code::OK) {
            if (resp.response.size()) {
                for (auto el : resp.response) {
                    auto urc  = at::urc::UrcFactory::Create(el);
                    auto cpin = std::unique_ptr<at::urc::Cpin>{static_cast<at::urc::Cpin *>(urc.release())};
                    if (cpin) {
                        return cpin->getState();
                    }
                }
            }
        }
    }
    return at::SimState::Unknown;
}