~aleteoryx/muditaos

ref: a21736e3a63927001e8cee6cee5b964e238bb8a9 muditaos/module-cellular/at/src/UrcQind.cpp -rw-r--r-- 4.6 KiB
a21736e3 — Lefucjusz [MOS-1050] Add missing Germany HNI codes 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <UrcQind.hpp>
#include <log/debug.hpp>

#include <map>

using namespace at::urc;

auto Qind::isCsq() const noexcept -> bool
{
    if (tokens.size() == magic_enum::enum_count<enum CSQ>()) {
        return tokens[CSQ].find(type_csq) != std::string::npos;
    }
    return false;
}

auto Qind::isFota() const noexcept -> bool
{
    return tokens.size() > 0 && tokens[Fota] == type_fota;
}

auto Qind::isFotaValid() const noexcept -> bool
{
    return isFota() && tokens.size() >= fotaMinTokenSize;
}

auto Qind::getFotaStage() const noexcept -> std::optional<FotaStage>
{
    if (isFotaValid()) {
        for (auto &stage : magic_enum::enum_values<FotaStage>()) {
            if (tokens[Stage] == magic_enum::enum_name(stage)) {
                return stage;
            }
        }
    }
    return std::nullopt;
}

auto Qind::getFotaParameter() const noexcept -> std::string
{
    if (isFotaValid() && tokens.size() > fotaMinTokenSize) {
        return tokens[Param];
    }
    return std::string();
}

auto Qind::isSmsDone() const noexcept -> bool
{
    constexpr auto smsDonePos = 0;
    return tokens.size() > 0 && tokens[smsDonePos] == type_sms_done;
}

auto Qind::validate(enum CSQ check) const noexcept -> bool
{
    try {
        if (isCsq()) {
            int rssi = std::stoi(tokens[RSSI]);
            int ber  = std::stoi(tokens[BER]);
            LOG_DEBUG("> %d %d", rssi, ber);
            switch (check) {
            case RSSI:
                return rssi != invalid_rssi_low && rssi != invalid_rssi_high;
            case BER:
                return ber != invalid_ber;
            default:
                return false;
            }
        }
    }
    catch (const std::exception &ex) {
        LOG_FATAL("exception: %s", ex.what());
    }
    return false;
}

auto Qind::getRSSI() const noexcept -> std::optional<int>
{
    if (isCsq()) {
        int rssi;
        try {
            rssi = std::stoi(tokens[RSSI]);
        }
        catch (const std::exception &e) {
            return std::nullopt;
        }

        if (rssi != invalid_rssi_low && rssi != invalid_rssi_high) {
            return rssi;
        }
    }

    return std::nullopt;
}

auto Qind::getBER() const noexcept -> std::optional<int>
{
    if (isCsq()) {
        int ber;
        try {
            ber = std::stoi(tokens[BER]);
        }
        catch (const std::exception &e) {
            return std::nullopt;
        }

        if (ber != invalid_ber) {
            return ber;
        }
    }

    return std::nullopt;
}

auto Qind::isAct() const noexcept -> bool
{
    if (tokens.size() == magic_enum::enum_count<enum ACT>()) {
        return tokens[ACT].find(type_act) != std::string::npos;
    }
    return false;
}

auto Qind::getAct() const noexcept -> std::string
{
    if (isAct()) {
        return tokens[ACT::ACTVALUE];
    }

    return "";
}

namespace
{
    constexpr auto gsm           = "GSM";
    constexpr auto egprs         = "EGPRS";
    constexpr auto hsdpa         = "HSDPA";
    constexpr auto hsupa         = "HSUPA";
    constexpr auto hsdpaAndHsupa = "HSDPA&HSUPA";
    constexpr auto lte           = "LTE";
    constexpr auto tdScdma       = "TD-SCDMA";
    constexpr auto cdma          = "CDMA";
    constexpr auto wcdma         = "WCDMA";
    constexpr auto hdr           = "HDR";
    constexpr auto evdo          = "EVDO";
    constexpr auto unknown       = "UNKNOWN";

    std::map<std::string_view, Store::Network::AccessTechnology> technologyMap = {
        {gsm, Store::Network::AccessTechnology::Gsm},
        {egprs, Store::Network::AccessTechnology::GsmWEgprs},
        {wcdma, Store::Network::AccessTechnology::Utran},
        {hsdpa, Store::Network::AccessTechnology::UtranWHsdpa},
        {hsupa, Store::Network::AccessTechnology::UtranWHsupa},
        {hsdpaAndHsupa, Store::Network::AccessTechnology::UtranWHsdpaAndWHsupa},
        {lte, Store::Network::AccessTechnology::EUtran},
        {tdScdma, Store::Network::AccessTechnology::Utran},
        {cdma, Store::Network::AccessTechnology::Cdma},
        {wcdma, Store::Network::AccessTechnology::Wcdma},
        {hdr, Store::Network::AccessTechnology::Utran},
        {evdo, Store::Network::AccessTechnology::Utran},
        {unknown, Store::Network::AccessTechnology::Unknown}};
} // namespace
[[nodiscard]] auto Qind::getAccessTechnology() const noexcept -> Store::Network::AccessTechnology
{
    auto act = getAct();

    auto it = technologyMap.find(act);
    if (it != technologyMap.end()) {
        return it->second;
    }
    return Store::Network::AccessTechnology::Unknown;
}