~aleteoryx/muditaos

ref: 2cd0e4721405c0bf263f311f27d38aadae6ac89f muditaos/module-cellular/at/src/Cmd.cpp -rw-r--r-- 2.2 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
// 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 "Cmd.hpp"
#include <algorithm>

namespace at
{
    Cmd::Cmd(std::string cmd, cmd::Modifier mod, std::chrono::milliseconds timeout) noexcept
        : cmd(std::move(cmd)), timeout(timeout), mod(mod)
    {}

    Cmd::Cmd(std::string cmd, std::chrono::milliseconds timeout) noexcept : Cmd(cmd, cmd::Modifier::None, timeout)
    {}

    void Cmd::split(const std::string &str, Result &result)
    {
        constexpr char tokenDelimiter = ',';

        result.tokens.push_back(utils::split(str, tokenDelimiter));
        constexpr auto urcStringDelimiter = "\"";
        for (auto &arr : result.tokens) {
            for (auto &t : arr) {
                utils::findAndReplaceAll(t, urcStringDelimiter, "");
                t = utils::trim(t);
            }
        }
    }

    Result Cmd::parseBase(const Result &that)
    {
        auto result = that;
        if (result.code != Result::Code::OK) {
            return result;
        }
        else if (result.response.empty()) {
            LOG_ERROR("Can't parse - empty response");
            result.code = Result::Code::PARSING_ERROR;
            return result;
        }
        else if (result.response.size() == 1) {
            if (result.response[0] == "OK") {
                LOG_INFO("OK response");
                result.code = Result::Code::OK;
                return result;
            }
            LOG_ERROR("Can't parse - response not valid");
            result.code = Result::Code::ERROR;
            return result;
        }
        const char headDelimiter = ':';
        const auto atResponse    = result.response;
        const auto lastPosition  = atResponse.end() - 1;
        for (auto it = atResponse.begin(); it != lastPosition; it++) {
            auto prefixIter = std::find(it->begin(), it->end(), headDelimiter);
            auto head       = std::string(it->begin(), prefixIter);
            auto body       = std::string(prefixIter == it->end() ? it->begin() : prefixIter + 1, it->end());
            split(body, result);
        }

        return result;
    }
} // namespace at