Files
esp-protocols/esp_modem/src/esp_modem_dte.cpp

38 lines
1.4 KiB
C++
Raw Normal View History

2021-02-26 18:32:15 +01:00
#include "cxx_include/esp_modem_dte.hpp"
#include <string.h>
#include "esp_log.h"
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)),
term(std::move(terminal)), mode(modem_mode::UNDEF) {}
2021-02-26 18:32:15 +01:00
command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms)
2021-02-26 18:32:15 +01:00
{
// assert(term_id < term->max_virtual_terms());
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-02-26 18:32:15 +01:00
term->write((uint8_t *)command.c_str(), command.length());
term->set_data_cb([&](size_t len){
auto data_to_read = std::min(len, buffer_size - consumed);
auto data = buffer.get() + consumed;
auto actual_len = term->read(data, data_to_read);
consumed += actual_len;
if (memchr(data, '\n', actual_len)) {
2021-03-04 20:19:18 +01:00
// ESP_LOGE("GOT", "LINE");
2021-03-03 20:35:08 +01:00
res = got_line(buffer.get(), consumed);
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-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-03-03 20:35:08 +01:00
term->set_data_cb(nullptr);
2021-02-26 18:32:15 +01:00
return res;
2021-03-03 20:35:08 +01:00
}