2021-03-29 19:34:45 +02:00
|
|
|
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-02-26 18:32:15 +01:00
|
|
|
#include "cxx_include/esp_modem_dte.hpp"
|
2021-03-30 08:25:16 +02:00
|
|
|
#include <cstring>
|
2021-02-26 18:32:15 +01:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
2021-03-29 19:34:45 +02:00
|
|
|
using namespace esp_modem;
|
|
|
|
|
|
|
|
const int DTE_BUFFER_SIZE = 1024;
|
|
|
|
|
2021-03-04 20:19:18 +01:00
|
|
|
DTE::DTE(std::unique_ptr<Terminal> terminal):
|
2021-03-03 20:35:08 +01:00
|
|
|
buffer_size(DTE_BUFFER_SIZE), consumed(0),
|
|
|
|
buffer(std::make_unique<uint8_t[]>(buffer_size)),
|
2021-04-06 08:33:40 +02:00
|
|
|
term(std::move(terminal)), command_term(term.get()), other_term(nullptr),
|
|
|
|
mode(modem_mode::UNDEF) {}
|
2021-02-26 18:32:15 +01:00
|
|
|
|
2021-04-15 09:46:28 +02:00
|
|
|
command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator)
|
2021-02-26 18:32:15 +01:00
|
|
|
{
|
2021-03-06 16:56:50 +01:00
|
|
|
Scoped<Lock> l(lock);
|
2021-03-03 20:35:08 +01:00
|
|
|
command_result res = command_result::TIMEOUT;
|
2021-04-06 08:33:40 +02:00
|
|
|
command_term->set_read_cb([&](uint8_t *data, size_t len) {
|
|
|
|
if (!data) {
|
|
|
|
auto data_to_read = std::min(len, buffer_size - consumed);
|
|
|
|
data = buffer.get() + consumed;
|
|
|
|
len = command_term->read(data, data_to_read);
|
|
|
|
}
|
|
|
|
consumed += len;
|
2021-04-15 09:46:28 +02:00
|
|
|
if (memchr(data, separator, len)) {
|
2021-04-06 08:33:40 +02:00
|
|
|
res = got_line(data, consumed);
|
2021-03-03 20:35:08 +01:00
|
|
|
if (res == command_result::OK || res == command_result::FAIL) {
|
2021-02-26 18:32:15 +01:00
|
|
|
signal.set(GOT_LINE);
|
2021-03-04 20:19:18 +01:00
|
|
|
return true;
|
2021-02-26 18:32:15 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-04 20:19:18 +01:00
|
|
|
return false;
|
2021-02-26 18:32:15 +01:00
|
|
|
});
|
2021-04-06 08:33:40 +02:00
|
|
|
command_term->write((uint8_t *)command.c_str(), command.length());
|
2021-03-03 20:35:08 +01:00
|
|
|
auto got_lf = signal.wait(GOT_LINE, time_ms);
|
|
|
|
if (got_lf && res == command_result::TIMEOUT) {
|
|
|
|
throw_if_esp_fail(ESP_ERR_INVALID_STATE);
|
|
|
|
}
|
2021-02-26 18:32:15 +01:00
|
|
|
consumed = 0;
|
2021-04-06 08:33:40 +02:00
|
|
|
command_term->set_read_cb(nullptr);
|
2021-02-26 18:32:15 +01:00
|
|
|
return res;
|
2021-04-06 08:33:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DTE::setup_cmux()
|
|
|
|
{
|
|
|
|
auto original_term = std::move(term);
|
|
|
|
auto cmux_term = std::make_shared<CMux>(std::move(original_term), std::move(buffer), buffer_size);
|
|
|
|
buffer_size = 0;
|
|
|
|
cmux_term->init();
|
|
|
|
term = std::make_unique<CMuxInstance>(cmux_term, 0);
|
|
|
|
command_term = term.get(); // use command terminal as previously
|
|
|
|
other_term = std::make_unique<CMuxInstance>(cmux_term, 1);
|
2021-04-15 09:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
command_result DTE::command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms)
|
|
|
|
{
|
|
|
|
return command(cmd, got_line, time_ms, '\n');
|
|
|
|
}
|