mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-18 04:52:18 +02:00
fix(exp_modem): DTE should own both command and data terminal
reworks to clear shared_ptr<> of both command and data terminals and having DTE own it. When we swith to CMUX mode the ownership is transfered to CMux terminal
This commit is contained in:
@ -24,18 +24,18 @@ 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)),
|
||||
term(std::move(terminal)), command_term(term.get()), other_term(nullptr),
|
||||
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)),
|
||||
term(std::move(terminal)), command_term(term.get()), other_term(nullptr),
|
||||
cmux_term(nullptr), command_term(std::move(terminal)), data_term(command_term),
|
||||
mode(modem_mode::UNDEF) {}
|
||||
|
||||
command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator)
|
||||
{
|
||||
Scoped<Lock> l(lock);
|
||||
Scoped<Lock> l(internal_lock);
|
||||
command_result res = command_result::TIMEOUT;
|
||||
command_term->set_read_cb([&](uint8_t *data, size_t len) {
|
||||
if (!data) {
|
||||
@ -71,28 +71,21 @@ command_result DTE::command(const std::string &cmd, got_line_cb got_line, uint32
|
||||
|
||||
bool DTE::exit_cmux()
|
||||
{
|
||||
auto cmux_term = static_cast<CMuxInstance*>(term.get())->get_cmux();
|
||||
auto ejected = cmux_term->deinit_and_eject();
|
||||
if (ejected == std::tuple(nullptr, nullptr, 0)) {
|
||||
return false;
|
||||
}
|
||||
// deinit succeeded -> swap the internal terminals with those ejected from cmux
|
||||
auto term_orig = std::move(term);
|
||||
auto other_term_orig = std::move(other_term);
|
||||
term = std::move(std::get<0>(ejected));
|
||||
command_term = std::move(std::get<0>(ejected));
|
||||
buffer = std::move(std::get<1>(ejected));
|
||||
buffer_size = std::get<2>(ejected);
|
||||
command_term = term.get(); // use command terminal as previously
|
||||
data_term = command_term;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DTE::setup_cmux()
|
||||
{
|
||||
auto original_term = std::move(term);
|
||||
if (original_term == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto cmux_term = std::make_shared<CMux>(std::move(original_term), std::move(buffer), buffer_size);
|
||||
cmux_term = std::make_shared<CMux>(command_term, std::move(buffer), buffer_size);
|
||||
if (cmux_term == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@ -100,34 +93,52 @@ bool DTE::setup_cmux()
|
||||
if (!cmux_term->init()) {
|
||||
return false;
|
||||
}
|
||||
term = std::make_unique<CMuxInstance>(cmux_term, 0);
|
||||
if (term == nullptr) {
|
||||
command_term = std::make_unique<CMuxInstance>(cmux_term, 0);
|
||||
if (command_term == nullptr) {
|
||||
return false;
|
||||
}
|
||||
command_term = term.get(); // use command terminal as previously
|
||||
other_term = std::make_unique<CMuxInstance>(cmux_term, 1);
|
||||
data_term = std::make_unique<CMuxInstance>(cmux_term, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DTE::set_mode(modem_mode m)
|
||||
{
|
||||
if (mode == modem_mode::CMUX_MODE && m == modem_mode::COMMAND_MODE) {
|
||||
if (exit_cmux()) {
|
||||
// transitions (COMMAND|UNDEF) -> CMUX
|
||||
if (m == modem_mode::CMUX_MODE) {
|
||||
if (mode == modem_mode::UNDEF || mode == modem_mode::COMMAND_MODE) {
|
||||
if (setup_cmux()) {
|
||||
mode = m;
|
||||
return true;
|
||||
}
|
||||
mode = modem_mode::UNDEF;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// transitions (COMMAND|CMUX|UNDEF) -> DATA
|
||||
if (m == modem_mode::DATA_MODE) {
|
||||
if (mode == modem_mode::CMUX_MODE) {
|
||||
// mode stays the same, but need to swap terminals (as command has been switch)
|
||||
data_term.swap(command_term);
|
||||
} else {
|
||||
mode = m;
|
||||
}
|
||||
// prepare the data terminal's callback to the configured std::function (used by netif)
|
||||
data_term->set_read_cb(on_data);
|
||||
return true;
|
||||
}
|
||||
// transitions (DATA|CMUX|UNDEF) -> COMMAND
|
||||
if (m == modem_mode::COMMAND_MODE) {
|
||||
if (mode == modem_mode::CMUX_MODE) {
|
||||
if (exit_cmux()) {
|
||||
mode = m;
|
||||
return true;
|
||||
}
|
||||
mode = modem_mode::UNDEF;
|
||||
return false;
|
||||
} else {
|
||||
mode = m;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (mode != modem_mode::CMUX_MODE) { // keep CMUX internally, it's CMD+PPP
|
||||
mode = m;
|
||||
}
|
||||
if (m == modem_mode::DATA_MODE) {
|
||||
term->set_read_cb(on_data);
|
||||
if (other_term) { // if we have the other terminal, let's use it for commands
|
||||
command_term = other_term.get();
|
||||
}
|
||||
} else if (m == modem_mode::CMUX_MODE) {
|
||||
return setup_cmux();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -135,10 +146,10 @@ bool DTE::set_mode(modem_mode m)
|
||||
void DTE::set_read_cb(std::function<bool(uint8_t *, size_t)> f)
|
||||
{
|
||||
on_data = std::move(f);
|
||||
term->set_read_cb([this](uint8_t *data, size_t len) {
|
||||
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 = term->read(buffer.get(), buffer_size);
|
||||
len = data_term->read(buffer.get(), buffer_size);
|
||||
}
|
||||
if (on_data) {
|
||||
return on_data(data, len);
|
||||
@ -151,12 +162,12 @@ int DTE::read(uint8_t **d, size_t len)
|
||||
{
|
||||
auto data_to_read = std::min(len, buffer_size);
|
||||
auto data = buffer.get();
|
||||
auto actual_len = term->read(data, data_to_read);
|
||||
auto actual_len = data_term->read(data, data_to_read);
|
||||
*d = data;
|
||||
return actual_len;
|
||||
}
|
||||
|
||||
int DTE::write(uint8_t *data, size_t len)
|
||||
{
|
||||
return term->write(data, len);
|
||||
return data_term->write(data, len);
|
||||
}
|
Reference in New Issue
Block a user