~aleteoryx/muditaos

ref: bd90e575167d8170ad989ef8d6842d7db57bb0df muditaos/module-cellular/at/cmd/src/CPBS.cpp -rw-r--r-- 2.6 KiB
bd90e575 — Marcin Smoczyński [BH-907] Split utils module into components 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
// 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/CPBS.hpp>

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

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

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

        auto CPBS::parseCPBS(const Result &base_result) -> result::CPBS
        {

            auto constexpr responseHeader = "+CPBS: ";

            result::CPBS parsed{base_result};

            if (parsed.Result::operator bool()) {
                if (parsed.response.empty()) {
                    LOG_ERROR("Can't parse - empty response");
                    parsed.code = result::CPBS::Code::PARSING_ERROR;
                }
                else {
                    std::string str = parsed.response[0];
                    if (str.find(responseHeader) == std::string::npos) {
                        LOG_ERROR("Can't parse - bad header");
                        parsed.code = result::CPBS::Code::PARSING_ERROR;
                        return parsed;
                    }
                    utils::findAndReplaceAll(str, responseHeader, "");
                    utils::trim(str);

                    std::vector<std::string> tokens = utils::split(str, ',');

                    if (tokens.size() != cpbs::tokensCount) {
                        LOG_ERROR("Can't parse - invalid tokens count");
                        parsed.code = result::CPBS::Code::PARSING_ERROR;
                        return parsed;
                    }

                    int used  = 0;
                    int total = 0;
                    if (utils::toNumeric(tokens[static_cast<int>(cpbs::tokens::Used)], used) &&
                        utils::toNumeric(tokens[static_cast<int>(cpbs::tokens::Total)], total)) {
                        parsed.used    = used;
                        parsed.total   = total;
                        parsed.storage = tokens[static_cast<int>(cpbs::tokens::Storage)];
                    }
                    else {
                        LOG_ERROR("Can't parse - bad value");
                        parsed.code = result::CPBS::Code::PARSING_ERROR;
                    }
                }
            }
            return parsed;
        }

        void CPBS::set(const std::string &storage)
        {
            body += storage;
        }
    } // namespace cmd
    namespace result
    {
        CPBS::CPBS(const Result &that) : Result(that)
        {}

    } // namespace result
} // namespace at