~aleteoryx/muditaos

ref: 25a5d90f4e12ee5d33f4202170f18b59e16a9564 muditaos/module-services/service-cellular/requests/CallForwardingRequest.cpp -rw-r--r-- 4.0 KiB
25a5d90f — rrandomsky [CP-2156] Fixed no response when editing a contact to have the same number as another 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
// Copyright (c) 2017-2021, 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 <at/ATFactory.hpp>

#include "service-cellular/requests/CallForwardingRequest.hpp"
#include <map>

namespace
{
    // according to EC25&EC21_AT_Commands_Manual_V1.3
    const std::map<std::string, std::string> reasonCodes = {{
        {"21", "0"},  // Unconditional
        {"67", "1"},  // MobileBusy
        {"61", "2"},  // NoReply
        {"62", "3"},  // NotReachable
        {"002", "4"}, // AllCallForwarding
        {"004", "5"}, // AllConditionalCallForwarding
    }};
} // namespace

namespace cellular
{

    std::unique_ptr<SupplementaryServicesRequest> CallForwardingRequest::create(const std::string &serviceCode,
                                                                                const std::string &data,
                                                                                GroupMatch matchGroups)
    {
        if (auto it = reasonCodes.find(serviceCode); it != reasonCodes.end()) {
            return std::make_unique<CallForwardingRequest>(it->second, data, matchGroups);
        }

        return nullptr;
    }

    auto CallForwardingRequest::command() -> at::Cmd
    {
        std::vector<commandBuilderFunc> commandParts = {[this]() { return getCommandReason(); },
                                                        [this]() { return getCommandMode(); },
                                                        [this]() { return getCommandNumber(); }};

        bool trimEmpty = true;

        if (!getCommandClass().empty()) {
            commandParts.emplace_back([this]() { return getCommandType(); });
            commandParts.emplace_back([this]() { return getCommandClass(); });
            trimEmpty = false;
        }

        if (!getCommandTime().empty()) {
            commandParts.emplace_back([this]() { return getCommandSubAddr(); });
            commandParts.emplace_back([this]() { return getCommandSatype(); });
            commandParts.emplace_back([this]() { return getCommandTime(); });
            trimEmpty = false;
        }

        return buildCommand(at::AT::CCFC, commandParts, trimEmpty);
    }

    auto CallForwardingRequest::getCommandReason() const -> std::string
    {
        return forwardReason;
    }

    auto CallForwardingRequest::getCommandMode() const -> std::string
    {
        return utils::to_string(magic_enum::enum_integer(procedureType));
    }

    auto CallForwardingRequest::getCommandNumber() const -> std::string
    {
        return phoneNumber.empty() ? std::string() : "\"" + phoneNumber + "\"";
    }

    auto CallForwardingRequest::getCommandType() const -> std::string
    {
        // according to EC25&EC21_AT_Commands_Manual_V1.3
        if (auto pos = phoneNumber.find("+"); pos != std::string::npos) {
            return addressFormatTypeInternational;
        }
        else {
            return addressFormatTypeDefault;
        }
    }

    auto CallForwardingRequest::getCommandClass() const -> std::string
    {
        if (basicServiceGroup.empty()) {
            return std::string();
        }

        return getCommandInformationClass(basicServiceGroup).value_or(std::string());
    }

    auto CallForwardingRequest::getCommandSubAddr() const -> std::string
    {
        return subaddrDefault;
    }

    auto CallForwardingRequest::getCommandSatype() const -> std::string
    {
        return std::string();
    }

    auto CallForwardingRequest::getCommandTime() const -> std::string
    {
        return noReplyConditionTimer;
    }

    auto CallForwardingRequest::isValid() const noexcept -> bool
    {
        if (noReplyConditionTimer.empty()) {
            return true;
        }
        auto time = utils::getNumericValue<int>(noReplyConditionTimer);
        return std::clamp(time, minNoReplyTime, maxNoReplyTime) == time;
    }

    void CallForwardingRequest::handle(RequestHandler &h, at::Result &result)
    {
        h.handle(*this, result);
    }
} // namespace cellular