~aleteoryx/muditaos

ref: c8e65453c5b2ce03a7f87609ed1a729dddc470de muditaos/module-cellular/at/cmd/src/CPBR.cpp -rw-r--r-- 3.6 KiB
c8e65453 — Mateusz Piesta [BH-891] Alarm popup fix 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <at/cmd/CPBR.hpp>

#include <log.hpp>
#include <memory>
#include <string>

namespace at
{
    namespace cmd
    {
        CPBR::CPBR(at::cmd::Modifier mod) noexcept : Cmd("AT+CPBR", mod, at::default_timeout)
        {}

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

        auto CPBR::parseCPBR(const Result &base_result) -> result::CPBR
        {
            auto constexpr responseHeader = "+CPBR: ";

            result::CPBR parsed{base_result};

            if (parsed.Result::operator bool()) {
                if (parsed.response.empty()) {
                    LOG_ERROR("Can't parse - empty response");
                    parsed.code = result::CPBR::Code::PARSING_ERROR;
                }
                else {

                    for (auto token : parsed.response) {

                        if (token == "OK") {
                            parsed.code = result::CPBR::Code::OK;
                            return parsed;
                        }

                        std::string str = token;
                        if (str.find(responseHeader) == std::string::npos) {
                            LOG_ERROR("Can't parse - bad header");
                            parsed.code = result::CPBR::Code::PARSING_ERROR;
                            return parsed;
                        }

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

                        std::vector<std::string> tokens = utils::split(str, ',');
                        if (tokens.size() != cpbr::tokensCount) {
                            LOG_ERROR("Can't parse - invalid tokens count");
                            parsed.code = result::CPBR::Code::PARSING_ERROR;
                            return parsed;
                        }
                        int index = 0;
                        int type  = 0;
                        if (utils::toNumeric(tokens[static_cast<int>(cpbr::Tokens::Index)], index) &&
                            utils::toNumeric(tokens[static_cast<int>(cpbr::Tokens::Type)], type) &&
                            magic_enum::enum_contains<at::cpbr::ContactType>(type)) {

                            utils::findAndReplaceAll(tokens[static_cast<int>(cpbr::Tokens::Number)], "\"", "");
                            utils::findAndReplaceAll(tokens[static_cast<int>(cpbr::Tokens::Name)], "\"", "");

                            cpbr::Contact contact;
                            contact.index  = index;
                            contact.type   = static_cast<at::cpbr::ContactType>(type);
                            contact.number = tokens[static_cast<int>(cpbr::Tokens::Number)];
                            contact.name   = tokens[static_cast<int>(cpbr::Tokens::Name)];
                            parsed.contacts.push_back(contact);
                        }
                        else {
                            LOG_ERROR("Can't parse - bad value");
                            parsed.code = result::CPBR::Code::PARSING_ERROR;
                            return parsed;
                        }
                    }
                }
            }
            return parsed;
        }

        void CPBR::setSimContactsReadRange(int firstIndex, int lastIndex)
        {
            body += utils::to_string(firstIndex) + "," + utils::to_string(lastIndex);
        }
    } // namespace cmd
    namespace result
    {
        CPBR::CPBR(const Result &that) : Result(that)
        {}

    } // namespace result
} // namespace at