~aleteoryx/muditaos

ref: 2cd0e4721405c0bf263f311f27d38aadae6ac89f muditaos/module-cellular/at/src/UrcCusd.cpp -rw-r--r-- 4.0 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "UrcCusd.hpp"
#include <magic_enum.hpp>
#include <re2/re2.h>

using namespace at::urc;

auto Cusd::isURC(const std::string &uHead) -> bool
{
    return uHead == head;
}

auto Cusd::Handle(UrcHandler &h) -> void
{
    h.Handle(*this);
}

Cusd::Cusd(const std::string &urcBody, const std::string &urcHead) : Urc(urcBody, urcHead)
{
    try {
        split(urcBody);

        // MOS-858: decide whether to try to display anything or to bail out
        int constexpr supportedAlphabets[]{0, 15};
        if (auto dcs = getDCS();
            dcs.has_value() && std::find(std::begin(supportedAlphabets), std::end(supportedAlphabets), *dcs) ==
                                   std::end(supportedAlphabets)) {
            throw std::runtime_error("unsupported CUSD alphabet");
        }

        if (auto status = getStatus();
            status != StatusType::FurtherUserActionRequired && status != StatusType::NoFurtherUserActionRequired) {
            LOG_WARN("Unsupported CUSD status: %d", magic_enum::enum_integer(status));
        }
    }
    catch (std::runtime_error const &exc) {
        valid_ = false;
        LOG_ERROR("%s", exc.what());
    }
}

auto Cusd::isValid() const noexcept -> bool
{
    return valid_ && Urc::isValid();
}

auto Cusd::isActionNeeded() const noexcept -> bool
{
    return getStatus() == StatusType::FurtherUserActionRequired;
}

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

    if (auto const &messageToken = tokens[Tokens::Response]; !messageToken.empty()) {
        return std::make_optional(messageToken);
    }

    return std::nullopt;
}

auto Cusd::getStatus() const noexcept -> StatusType
{
    return *magic_enum::enum_cast<StatusType>(std::stoi(tokens[Tokens::Status]));
}

auto Cusd::getDCS() const noexcept -> std::optional<int>
{
    if (auto const &dcsToken = tokens[Tokens::DCS]; !dcsToken.empty()) {
        return std::make_optional(std::stoi(dcsToken));
    }

    return std::nullopt;
}

auto Cusd::split(const std::string &str) -> void
{
    auto constexpr maxNumberOfDcsTokens = 3;
    tokens.resize(maxNumberOfDcsTokens);

    using namespace re2;
    re2::StringPiece input(str);

    auto constexpr numberOfStatusTypes = magic_enum::enum_count<StatusType>();
    static_assert(numberOfStatusTypes <= 9,
                  "StatusType: too many enum entries to handle - please revise regex/algorithm");
    std::string const regexForStatus(" ([0-" + std::to_string(numberOfStatusTypes) + "])");

    if (!RE2::Consume(&input, regexForStatus, &tokens[Tokens::Status])) {
        throw std::runtime_error("unrecognized CUSD status field or corrupted CUSD format");
    }

    auto noFurtherInput = [&input]() { return input == "\r\n"; };

    if (noFurtherInput()) {
        return;
    }

    auto const startQuotationMarkPosition = input.find('"');
    // must give the size to rfind() because called in the default way is broken
    auto const endQuotationMarkPosition = input.rfind('"', input.size());
    if (startQuotationMarkPosition == StringPiece::npos || endQuotationMarkPosition == StringPiece::npos) {
        throw std::runtime_error("cannot locate message - corrupted CUSD format");
    }

    if (endQuotationMarkPosition == startQuotationMarkPosition + 1) {
        LOG_WARN("Empty CUSD message");
    }
    else {
        auto messageStartPosition = startQuotationMarkPosition + 1;
        auto messageEndPosition   = endQuotationMarkPosition;
        auto messageLength        = messageEndPosition - messageStartPosition;
        tokens[Tokens::Response]  = input.substr(messageStartPosition, messageLength).as_string();
    }

    input.remove_prefix(endQuotationMarkPosition + 1);
    if (noFurtherInput()) {
        return;
    }

    if (!RE2::FullMatch(input, ",([0-9]+)\r\n", &tokens[Tokens::DCS])) {
        throw std::runtime_error("corrupted CUSD format");
    }
}