~aleteoryx/muditaos

ref: 65a8cc18592d99c90405b3b07ca393eeccce9855 muditaos/module-services/service-cellular/requests/PinChangeRequest.cpp -rw-r--r-- 3.1 KiB
65a8cc18 — Marcin Smoczyński changelog: update changelog for v0.47.1 (#1056) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <string>

#include <at/Commands.hpp>
#include <Utils.hpp>

#include "service-cellular/requests/PinChangeRequest.hpp"

namespace
{
    //(according to 3GPP TS 22.030 V16.0.0):
    const std::map<std::string, cellular::PinChangeRequest::PassChangeType> serviceCodeToChangeType = {{
        {"04", cellular::PinChangeRequest::PassChangeType::ChangePin},
        {"042", cellular::PinChangeRequest::PassChangeType::ChangePin2},
        {"05", cellular::PinChangeRequest::PassChangeType::ChangePinByPuk},
    }};

    constexpr inline std::string_view changeNetworkPasswordServiceCode = "03";
} // namespace

namespace cellular
{

    PinChangeRequest::PinChangeRequest(const std::string &data, GroupMatch matchGroups)
        : Request(data), requestOldPinOrPuk(matchGroups[magic_enum::enum_integer(PinChangeRegexGroups::OldPassword)]),
          requestNewPin(matchGroups[magic_enum::enum_integer(PinChangeRegexGroups::NewPassword)]),
          requestNewPinRepeat(matchGroups[magic_enum::enum_integer(PinChangeRegexGroups::NewPasswordRepeat)])
    {
        auto &serviceCode = matchGroups[magic_enum::enum_integer(PinChangeRegexGroups::ServiceCode)];
        if (auto it = serviceCodeToChangeType.find(serviceCode); it == serviceCodeToChangeType.end()) {
            throw std::runtime_error(std::string(__FUNCTION__) + ": input service code not supported");
        }
        else {
            passChangeType = it->second;
        }
    }

    std::unique_ptr<PinChangeRequest> PinChangeRequest::create(const std::string &data, GroupMatch matchGroups)
    {
        return std::make_unique<PinChangeRequest>(data, matchGroups);
    }

    auto PinChangeRequest::command() -> std::string
    {
        std::array<std::function<std::string()>, 2> commandParts = {
            [this]() { return this->getOldPinOrPuk(); },
            [this]() { return "," + this->getNewPin(); },
        };

        std::string cmd;
        switch (passChangeType) {
        case PassChangeType::ChangePin:
            cmd.append(at::factory(at::AT::CPWD) + "\"SC\",");
            break;
        case PassChangeType::ChangePin2:
            cmd.append(at::factory(at::AT::CPWD) + "\"P2\",");
            break;
        case PassChangeType::ChangePinByPuk:
            cmd.append(at::factory(at::AT::CPIN));
            break;
        };

        for (auto &cmdPart : commandParts) {
            cmd.append(cmdPart());
        }

        return cmd;
    }

    auto PinChangeRequest::getOldPinOrPuk() const noexcept -> std::string
    {
        return "\"" + requestOldPinOrPuk + "\"";
    }

    auto PinChangeRequest::getNewPin() const noexcept -> std::string
    {
        return "\"" + requestNewPin + "\"";
    }

    void PinChangeRequest::handle(RequestHandler &h, at::Result &result)
    {
        h.handle(*this, result);
    }

    auto PinChangeRequest::isValid() const noexcept -> bool
    {
        return requestNewPin == requestNewPinRepeat && !requestNewPin.empty() && !requestNewPinRepeat.empty() &&
               !requestOldPinOrPuk.empty();
    }
} // namespace cellular