~aleteoryx/muditaos

ref: 1d2f5cf7a49cf1fb9463d289daa1492a2bec2d1d muditaos/module-cellular/at/src/UrcCusd.cpp -rw-r--r-- 2.4 KiB
1d2f5cf7 — Piotr Tański [EGD-7754] Dates bumped in disclaimers 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

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

using namespace at::urc;

Cusd::Cusd(const std::string &urcBody, const std::string &urcHead) : Urc(urcBody, urcHead)
{
    split(urcBody);
}
auto Cusd::isValid() const noexcept -> bool
{
    return 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;
}
void Cusd::split(const std::string &str)
{
    constexpr auto commaString    = ",";
    constexpr char tokenDelimiter = '\"';

    tokens = utils::split(str, tokenDelimiter);
    for (auto &t : tokens) {
        utils::findAndReplaceAll(t, "\"", "");
        t = utils::trim(t);
    }

    auto dcs       = tokens[Tokens::DCS];
    auto dcsTokens = utils::split(dcs, commaString);

    if (dcsTokens.size() > 1) {
        tokens.pop_back();
        for (auto el : dcsTokens) {
            tokens.push_back(el);
        }
    }
    uint32_t token = 0;
    for (auto &t : tokens) {
        if (token != static_cast<uint32_t>(Tokens::Response)) {
            utils::findAndReplaceAll(t, commaString, "");
            t = utils::trim(t);
        }
        token += 1;
    }
}