~aleteoryx/muditaos

ref: ff7af9601a43e1c1cb2985eec502698e1f35f176 muditaos/module-cellular/at/src/URC_CUSD.cpp -rw-r--r-- 1.7 KiB
ff7af960 — Radosław Wicik [EGD-4313] Fix usb_stack submodule path (#969) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <URC_CUSD.hpp>
#include <Utils.hpp>
#include <magic_enum.hpp>

using namespace at::urc;

CUSD::CUSD(const std::string &val) : Any(val)
{}

auto CUSD::what() const noexcept -> std::string
{
    return urc_name;
}

auto CUSD::isValid() const noexcept -> bool
{
    return is() && tokens.size() == magic_enum::enum_count<Tokens>();
}

auto CUSD::isActionNeeded() const noexcept -> bool
{
    if (isValid()) {
        auto status = getStatus();
        if (status) {
            return *status == StatusType::FurtherUserActionRequired;
        }
    }
    return false;
}

auto CUSD::getMessage() const noexcept -> std::optional<std::string>
{
    if (!isValid()) {
        return std::nullopt;
    }

    auto message = tokens[Tokens::Response];
    utils::findAndReplaceAll(message, "\"", "");

    return utils::trim(message);
}

auto CUSD::getStatus() const noexcept -> std::optional<StatusType>
{
    if (isValid()) {
        int statusInt;
        try {
            statusInt = std::stoi(tokens[Tokens::Status]);
        }
        catch (const std::exception &e) {
            return std::nullopt;
        }

        auto status = magic_enum::enum_cast<StatusType>(statusInt);
        if (status.has_value()) {
            return status.value();
        }
    }

    return std::nullopt;
}

auto CUSD::getDCS() const noexcept -> std::optional<int>
{
    if (!isValid()) {
        return std::nullopt;
    }
    int dcs;
    try {
        dcs = std::stoi(tokens[Tokens::DCS]);
    }
    catch (const std::exception &e) {
        return std::nullopt;
    }

    return dcs;
}