fix(esp_modem): Implement movable unique_buffer to bundle data, size, ptr

Also improves and fixes tests
This commit is contained in:
David Cermak
2022-06-10 18:04:10 +02:00
parent f3ff98bb82
commit 66e6d4cbf8
10 changed files with 176 additions and 87 deletions

View File

@ -78,23 +78,19 @@ uint8_t CMux::fcs_crc(const uint8_t frame[6])
return crc;
}
void CMux::close_down()
void CMux::send_disconnect(size_t i)
{
uint8_t frame[] = {
SOF_MARKER, 0x3, 0xFF, 0x5, 0xC3, 0x1, 0xE7, SOF_MARKER };
term->write(frame, 8);
}
void CMux::send_disc(size_t i)
{
uint8_t frame[6];
frame[0] = SOF_MARKER;
frame[1] = (i << 2) | 0x3;
frame[2] = FT_DISC | PF;
frame[3] = 1;
frame[4] = 0xFF - fcs_crc(frame);
frame[5] = SOF_MARKER;
term->write(frame, 6);
if (i == 0) { // control terminal
uint8_t frame[] = {
SOF_MARKER, 0x3, 0xFF, 0x5, 0xC3, 0x1, 0xE7, SOF_MARKER };
term->write(frame, 8);
} else { // separate virtual terminal
uint8_t frame[] = {
SOF_MARKER, 0x3, FT_DISC | PF, 0x1, 0, SOF_MARKER };
frame[1] |= i << 2;
frame[4] = 0xFF - fcs_crc(frame);
term->write(frame, sizeof(frame));
}
}
void CMux::send_sabm(size_t i)
@ -146,6 +142,9 @@ void CMux::data_available(uint8_t *data, size_t len)
read_cb[virtual_term](payload_start, total_payload_size);
#endif
}
} else if (type == 0xFF && dlci == 0) { // notify the internal DISC command
Scoped<Lock> l(lock);
sabm_ack = dlci;
}
}
@ -277,7 +276,7 @@ bool CMux::on_cmux_data(uint8_t *data, size_t actual_len)
{
if (!data) {
#ifdef DEFRAGMENT_CMUX_PAYLOAD
auto data_to_read = buffer_size - 128; // keep 128 (max CMUX payload) backup buffer)
auto data_to_read = buffer.size - 128; // keep 128 (max CMUX payload) backup buffer)
if (payload_start) {
data = payload_start + total_payload_size;
data_to_read = payload_len + 2;
@ -324,12 +323,13 @@ bool CMux::on_cmux_data(uint8_t *data, size_t actual_len)
return true;
}
bool CMux::exit_cmux_protocol()
bool CMux::deinit()
{
int timeout = 0;
sabm_ack = -1;
// First disconnect all (2) virtual terminals
for (size_t i = 1; i < 3; i++) {
int timeout = 0;
send_disc(i);
send_disconnect(i);
while (true) {
usleep(10'000);
Scoped<Lock> l(lock);
@ -342,11 +342,21 @@ bool CMux::exit_cmux_protocol()
}
}
}
close_down();
usleep(100'000);
sabm_ack = -1;
// Then disconnect the control terminal
send_disconnect(0);
while (true) {
usleep(10'000);
Scoped<Lock> l(lock);
if (sabm_ack == 0) {
break;
}
if (timeout++ > 100) {
return false;
}
}
term->set_read_cb(nullptr);
return true;
}
bool CMux::init()
@ -415,10 +425,7 @@ void CMux::set_read_cb(int inst, std::function<bool(uint8_t *, size_t)> f)
}
}
std::tuple<std::shared_ptr<Terminal>, std::unique_ptr<uint8_t[]>, size_t> esp_modem::CMux::deinit_and_eject()
std::pair<std::shared_ptr<Terminal>, unique_buffer> CMux::detach()
{
if (exit_cmux_protocol()) {
return std::make_tuple(std::move(term), std::move(buffer), buffer_size);
}
return std::tuple(nullptr, nullptr, 0);
return std::make_pair(std::move(term), std::move(buffer));
}

View File

@ -59,22 +59,24 @@ bool DCE_Mode::set_unsafe(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m
return true;
}
netif.stop();
SignalGroup signal;
dte->set_read_cb([&signal](uint8_t *data, size_t len) -> bool {
auto signal = std::make_shared<SignalGroup>();
std::weak_ptr<SignalGroup> weak_signal = signal;
dte->set_read_cb([weak_signal](uint8_t *data, size_t len) -> bool {
if (memchr(data, '\n', len)) {
ESP_LOG_BUFFER_HEXDUMP("esp-modem: debug_data", data, len, ESP_LOG_DEBUG);
const auto pass = std::list<std::string_view>({"NO CARRIER", "DISCONNECTED"});
std::string_view response((char *) data, len);
for (auto &it : pass)
if (response.find(it) != std::string::npos) {
signal.set(1);
if (auto signal = weak_signal.lock())
signal->set(1);
return true;
}
}
return false;
});
netif.wait_until_ppp_exits();
if (!signal.wait(1, 2000)) {
if (!signal->wait(1, 2000)) {
if (!device->set_mode(modem_mode::COMMAND_MODE)) {
mode = modem_mode::UNDEF;
return false;
@ -110,7 +112,7 @@ bool DCE_Mode::set_unsafe(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m
return false;
}
device->set_mode(modem_mode::CMUX_MODE); // switch the device into CMUX mode
usleep(100'000); // some devices need a few ms to switch
usleep(100'000); // some devices need a few ms to switch
if (!dte->set_mode(modem_mode::CMUX_MODE)) {
return false;

View File

@ -15,6 +15,7 @@
#include <cstring>
#include "esp_log.h"
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_cmux.hpp"
#include "esp_modem_config.h"
using namespace esp_modem;
@ -22,14 +23,12 @@ using namespace esp_modem;
static const size_t dte_default_buffer_size = 1000;
DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr<Terminal> terminal):
buffer_size(config->dte_buffer_size), consumed(0),
buffer(std::make_unique<uint8_t[]>(buffer_size)),
buffer(config->dte_buffer_size),
cmux_term(nullptr), command_term(std::move(terminal)), data_term(command_term),
mode(modem_mode::UNDEF) {}
DTE::DTE(std::unique_ptr<Terminal> terminal):
buffer_size(dte_default_buffer_size), consumed(0),
buffer(std::make_unique<uint8_t[]>(buffer_size)),
buffer(dte_default_buffer_size),
cmux_term(nullptr), command_term(std::move(terminal)), data_term(command_term),
mode(modem_mode::UNDEF) {}
@ -40,18 +39,18 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
command_term->set_read_cb([&](uint8_t *data, size_t len) {
if (!data) {
data = buffer.get();
len = command_term->read(data + consumed, buffer_size - consumed);
len = command_term->read(data + buffer.consumed, buffer.size - buffer.consumed);
} else {
consumed = 0; // if the underlying terminal contains data, we cannot fragment
buffer.consumed = 0; // if the underlying terminal contains data, we cannot fragment
}
if (memchr(data + consumed, separator, len)) {
res = got_line(data, consumed + len);
if (memchr(data + buffer.consumed, separator, len)) {
res = got_line(data, buffer.consumed + len);
if (res == command_result::OK || res == command_result::FAIL) {
signal.set(GOT_LINE);
return true;
}
}
consumed += len;
buffer.consumed += len;
return false;
});
command_term->write((uint8_t *)command.c_str(), command.length());
@ -59,7 +58,7 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
if (got_lf && res == command_result::TIMEOUT) {
throw_if_esp_fail(ESP_ERR_INVALID_STATE);
}
consumed = 0;
buffer.consumed = 0;
command_term->set_read_cb(nullptr);
return res;
}
@ -71,25 +70,23 @@ command_result DTE::command(const std::string &cmd, got_line_cb got_line, uint32
bool DTE::exit_cmux()
{
auto ejected = cmux_term->deinit_and_eject();
if (ejected == std::tuple(nullptr, nullptr, 0)) {
if (!cmux_term->deinit()) {
return false;
}
// deinit succeeded -> swap the internal terminals with those ejected from cmux
command_term = std::move(std::get<0>(ejected));
buffer = std::move(std::get<1>(ejected));
buffer_size = std::get<2>(ejected);
auto ejected = cmux_term->detach();
// return the ejected terminal and buffer back to this DTE
command_term = std::move(ejected.first);
buffer = std::move(ejected.second);
data_term = command_term;
return true;
}
bool DTE::setup_cmux()
{
cmux_term = std::make_shared<CMux>(command_term, std::move(buffer), buffer_size);
cmux_term = std::make_shared<CMux>(command_term, std::move(buffer));
if (cmux_term == nullptr) {
return false;
}
buffer_size = 0;
if (!cmux_term->init()) {
return false;
}
@ -149,7 +146,7 @@ void DTE::set_read_cb(std::function<bool(uint8_t *, size_t)> f)
data_term->set_read_cb([this](uint8_t *data, size_t len) {
if (!data) { // if no data available from terminal callback -> need to explicitly read some
data = buffer.get();
len = data_term->read(buffer.get(), buffer_size);
len = data_term->read(buffer.get(), buffer.size);
}
if (on_data) {
return on_data(data, len);
@ -160,7 +157,7 @@ void DTE::set_read_cb(std::function<bool(uint8_t *, size_t)> f)
int DTE::read(uint8_t **d, size_t len)
{
auto data_to_read = std::min(len, buffer_size);
auto data_to_read = std::min(len, buffer.size);
auto data = buffer.get();
auto actual_len = data_term->read(data, data_to_read);
*d = data;
@ -170,4 +167,10 @@ int DTE::read(uint8_t **d, size_t len)
int DTE::write(uint8_t *data, size_t len)
{
return data_term->write(data, len);
}
}
/**
* Implemented here to keep all headers C++11 compliant
*/
unique_buffer::unique_buffer(size_t size):
data(std::make_unique<uint8_t[]>(size)), size(size), consumed(0) {}

View File

@ -54,13 +54,16 @@ void Netif::start()
signal.set(PPP_STARTED);
}
void Netif::stop() {}
void Netif::stop()
{
ppp_dte->set_read_cb(nullptr);
signal.clear(PPP_STARTED);
}
Netif::~Netif() = default;
void Netif::wait_until_ppp_exits()
{
}
} // namespace esp_modem