~aleteoryx/muditaos

ref: e4462dce331d56510c794143be8ed7d1c62b4742 muditaos/module-cellular/Modem/ATCommon.cpp -rw-r--r-- 6.1 KiB
e4462dce — Lukasz Skrzypczak [EGD-5298] Microphone Adjust 5 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
173
174
175
176
177
178
179
180
181
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ATCommon.hpp"
#include "time/ScopedTime.hpp"
#include <functional>
#include <log/log.hpp>
#include <string>
#include <ticks.hpp>
#include <vector>
#include <inttypes.h> // for PRIu32
#include <Utils.hpp>

using namespace at;

const std::string Channel::OK         = "OK";
const std::string Channel::ERROR      = "ERROR";
const std::string Channel::NO_CARRIER = "NO CARRIER";
const std::string Channel::BUSY       = "BUSY";
const std::string Channel::NO_ANSWER  = "NO ANSWER";
const std::string Channel::CME_ERROR  = "+CME ERROR:";
const std::string Channel::CMS_ERROR  = "+CMS ERROR:";
// const std::string Channel::CONNECT = "CONNECT";
// const std::string Channel::RING = "RING";
// const std::string Channel::NO_DIALTONE = "NO DIALTONE";

Result::Code Channel::at_check(const std::vector<std::string> &arr)
{
    if (arr.size()) {
        for (auto el : arr) {
            if (el.compare(0, OK.length(), OK) == 0) {
                return Result::Code::OK;
            }
            else if (el.compare(0, ERROR.length(), ERROR) == 0) {
                return Result::Code::ERROR;
            }
        }
    }
    return Result::Code::NONE;
}

bool Channel::at_check_cmx_error(const std::string &CMX, const std::vector<std::string> &arr, uint32_t &errcode)
{
    if (arr.size()) {
        for (auto cmxerr : arr) {
            if (cmxerr.compare(0, CMX.length(), CMX) == 0) {
                auto serr     = utils::trim(cmxerr.substr(CMX.length(), cmxerr.length() - CMX.length()));
                int parsedVal = 0;
                auto ret      = utils::toNumeric(serr, parsedVal);
                errcode       = parsedVal;
                return ret;
            }
        }
    }
    return false;
}

void Channel::cmd_log(std::string cmd, const Result &result, uint32_t timeout)
{
    cmd.erase(std::remove(cmd.begin(), cmd.end(), '\r'), cmd.end());
    cmd.erase(std::remove(cmd.begin(), cmd.end(), '\n'), cmd.end());
    switch (result.code) {
    case Result::Code::TIMEOUT: {
        LOG_ERROR("[AT]: >%s<, timeout %" PRIu32
                  " - please check the value with Quectel_EC25&EC21_AT_Commands_Manual_V1.3.pdf",
                  cmd.c_str(),
                  timeout);
    } break;
    case Result::Code::ERROR: {

        LOG_ERROR("[AT]: >%s<, >%s<", cmd.c_str(), result.response.size() ? result.response.back().c_str() : "");
    } break;
    default:
        LOG_DEBUG("[AT]: >%s<, >%s<", cmd.c_str(), result.response.size() ? result.response.back().c_str() : "");
        break;
    }
#if DEBUG_MODEM_OUTPUT_RESPONSE
    for (auto s : result.response) {
        LOG_DEBUG("[AT] > %s", s.c_str());
    }
#endif
}

std::string Channel::formatCommand(const std::string &cmd) const
{
    bool isTerminatorValid = std::find(validTerm.begin(), validTerm.end(), cmd.back()) != validTerm.end();
    if (isTerminatorValid) {
        return cmd;
    }
    return cmd + cmdSeparator;
}

Result Channel::cmd(const std::string &cmd, std::chrono::milliseconds timeout, size_t rxCount)
{
    Result result;
    blockedTaskHandle = xTaskGetCurrentTaskHandle();

    cmd_init();
    std::string cmdFixed = formatCommand(cmd);
    cmd_send(cmdFixed);

    uint32_t currentTime   = cpp_freertos::Ticks::GetTicks();
    uint32_t timeoutNeeded = ((timeout.count() == UINT32_MAX) ? UINT32_MAX : currentTime + timeout.count());
    uint32_t timeElapsed   = currentTime;

    while (true) {
        if (timeoutNeeded != UINT32_MAX && timeElapsed >= timeoutNeeded) {
            result.code = Result::Code::TIMEOUT;
            break;
        }

        auto ret    = ulTaskNotifyTake(pdTRUE, timeoutNeeded - timeElapsed);
        timeElapsed = cpp_freertos::Ticks::GetTicks();
        if (ret != 0u) {
            std::vector<std::string> ret = cmd_receive();

            result.response.insert(std::end(result.response), std::begin(ret), std::end(ret));

            uint32_t errcode = 0;
            if (at_check_cmx_error(CME_ERROR, ret, errcode)) {
                result.code =
                    Result::Code::ERROR; // setup error but in this case error from +CME ERROR with valid errorCode
                auto tmp_ec = magic_enum::enum_cast<EquipmentErrorCode>(errcode);
                if (tmp_ec.has_value()) {
                    LOG_ERROR("%s", utils::enumToString(tmp_ec.value()).c_str());
                    result.errorCode = tmp_ec.value();
                }
                else {
                    LOG_ERROR("Unknow CME error code %" PRIu32, errcode);
                    result.errorCode = at::EquipmentErrorCode::Unknown;
                }
                break;
            }

            if (at_check_cmx_error(CMS_ERROR, ret, errcode)) {
                result.code =
                    Result::Code::ERROR; // setup error but in this case error from +CME ERROR with valid errorCode

                auto atmp_ec = magic_enum::enum_cast<NetworkErrorCode>(errcode);

                if (atmp_ec.has_value()) {
                    LOG_ERROR("%s", utils::enumToString(atmp_ec.value()).c_str());
                    result.errorCode = atmp_ec.value();
                }
                else {
                    LOG_ERROR("Unknow CMS error code %" PRIu32, errcode);
                    result.errorCode = at::NetworkErrorCode::Unknown;
                }
                break;
            }

            result.code = at_check(ret);
            if (result.code != Result::Code::NONE) {
                break;
            }
            if (rxCount != 0 && result.response.size() >= rxCount) {
                result.code = Result::Code::TOKENS;
                break;
            }
        }
    }

    blockedTaskHandle = nullptr;
    cmd_post();
    cmd_log(cmdFixed, result, timeout.count());

    return result;
}

auto Channel::cmd(const at::Cmd &at) -> Result
{
    auto time = utils::time::Scoped("Time to run at command" + at.getCmd());
    return cmd(at.getCmd(), at.getTimeout());
}

auto Channel::cmd(const at::AT &at) -> Result
{
    auto cmd = at::factory(at);
    auto time = utils::time::Scoped("Time to run at command" + cmd.getCmd());
    return this->cmd(cmd);
}