~aleteoryx/muditaos

ref: 2cf455dc130a0df575e95cff47ae797353e304e6 muditaos/module-cellular/at/cmd/src/CSQ.cpp -rw-r--r-- 2.3 KiB
2cf455dc — Alex0vSky [MOS-1046] Update module-services/service-cellular/call/doc/README.md 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log/log.hpp>
#include <memory>
#include <string>
#include <type_traits>
#include <at/cmd/CSQ.hpp>
#include <chrono>

namespace at
{
    namespace cmd
    {
        using namespace std::chrono_literals;
        CSQ::CSQ(at::cmd::Modifier mod) noexcept : Cmd("AT+CSQ", mod, 300ms)
        {}

        CSQ::CSQ() noexcept : CSQ(at::cmd::Modifier::None)
        {}

        result::CSQ CSQ::parseCSQ(const Result &base_result)
        {
            auto constexpr responseHeader = "+CSQ: ";

            result::CSQ p{base_result};
            // use parent operator bool
            if (p.Result::operator bool()) {
                if (p.response.empty()) {
                    LOG_ERROR("Can't parse - empty response");
                    p.code = result::CSQ::Code::PARSING_ERROR;

                    return p;
                }

                std::string str = p.response[0];
                if (str.find(responseHeader) == std::string::npos) {
                    LOG_ERROR("Can't parse - bad header");
                    p.code = result::CSQ::Code::PARSING_ERROR;
                    return p;
                }

                utils::findAndReplaceAll(str, responseHeader, "");
                utils::trim(str);

                std::vector<std::string> tokens = utils::split(str, ',');
                if (tokens.size() != csq::tokensCount) {
                    LOG_ERROR("Can't parse - wrong token count");
                    p.code = result::CSQ::Code::PARSING_ERROR;
                    return p;
                }

                auto csq = 0;
                auto ber = 0;
                if (utils::toNumeric(tokens.at(magic_enum::enum_integer(csq::Tokens::Csq)), csq) &&
                    utils::toNumeric(tokens.at(magic_enum::enum_integer(csq::Tokens::Ber)), ber)) {
                    p.csq = csq;
                    p.ber = ber;
                }
                else {
                    LOG_ERROR("Can't parse - bad value");
                    p.code = result::CSQ::Code::PARSING_ERROR;
                }
            }

            return p;
        }

    } // namespace cmd

    namespace result
    {
        CSQ::CSQ(const Result &that) : Result(that)
        {}

    } // namespace result
} // namespace at