feat(esp-modem): Add support for manual CMUX operations

Closes https://github.com/espressif/esp-protocols/issues/168
This commit is contained in:
David Cermak
2022-10-25 15:41:48 +02:00
parent 3f0262dfe5
commit ac5d43882b
7 changed files with 185 additions and 87 deletions

View File

@ -15,24 +15,24 @@ 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(config->dte_buffer_size),
cmux_term(nullptr), command_term(std::move(terminal)), data_term(command_term),
mode(modem_mode::UNDEF) {}
buffer(config->dte_buffer_size),
cmux_term(nullptr), term(std::move(terminal)), other_term(term),
mode(modem_mode::UNDEF) {}
DTE::DTE(std::unique_ptr<Terminal> terminal):
buffer(dte_default_buffer_size),
cmux_term(nullptr), command_term(std::move(terminal)), data_term(command_term),
mode(modem_mode::UNDEF) {}
buffer(dte_default_buffer_size),
cmux_term(nullptr), term(std::move(terminal)), other_term(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(internal_lock);
result = command_result::TIMEOUT;
signal.clear(GOT_LINE);
command_term->set_read_cb([this, got_line, separator](uint8_t *data, size_t len) {
term->set_read_cb([this, got_line, separator](uint8_t *data, size_t len) {
if (!data) {
data = buffer.get();
len = command_term->read(data + buffer.consumed, buffer.size - buffer.consumed);
len = term->read(data + buffer.consumed, buffer.size - buffer.consumed);
} else {
buffer.consumed = 0; // if the underlying terminal contains data, we cannot fragment
}
@ -46,13 +46,13 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
buffer.consumed += len;
return false;
});
command_term->write((uint8_t *)command.c_str(), command.length());
term->write((uint8_t *)command.c_str(), command.length());
auto got_lf = signal.wait(GOT_LINE, time_ms);
if (got_lf && result == command_result::TIMEOUT) {
ESP_MODEM_THROW_IF_ERROR(ESP_ERR_INVALID_STATE);
}
buffer.consumed = 0;
command_term->set_read_cb(nullptr);
term->set_read_cb(nullptr);
return result;
}
@ -68,26 +68,26 @@ bool DTE::exit_cmux()
}
auto ejected = cmux_term->detach();
// return the ejected terminal and buffer back to this DTE
command_term = std::move(ejected.first);
term = std::move(ejected.first);
buffer = std::move(ejected.second);
data_term = command_term;
other_term = term;
return true;
}
bool DTE::setup_cmux()
{
cmux_term = std::make_shared<CMux>(command_term, std::move(buffer));
cmux_term = std::make_shared<CMux>(term, std::move(buffer));
if (cmux_term == nullptr) {
return false;
}
if (!cmux_term->init()) {
return false;
}
command_term = std::make_unique<CMuxInstance>(cmux_term, 0);
if (command_term == nullptr) {
term = std::make_unique<CMuxInstance>(cmux_term, 0);
if (term == nullptr) {
return false;
}
data_term = std::make_unique<CMuxInstance>(cmux_term, 1);
other_term = std::make_unique<CMuxInstance>(cmux_term, 1);
return true;
}
@ -106,14 +106,12 @@ bool DTE::set_mode(modem_mode m)
}
// 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);
if (mode == modem_mode::CMUX_MODE || mode == modem_mode::CMUX_MANUAL_MODE) {
// mode stays the same, but need to swap terminals (as command has been switched)
other_term.swap(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
@ -125,21 +123,46 @@ bool DTE::set_mode(modem_mode m)
}
mode = modem_mode::UNDEF;
return false;
} if (mode == modem_mode::CMUX_MANUAL_MODE) {
return true;
} else {
mode = m;
return true;
}
}
// manual CMUX transitions: Enter CMUX
if (m == modem_mode::CMUX_MANUAL_MODE) {
if (setup_cmux()) {
mode = m;
return true;
}
mode = modem_mode::UNDEF;
return false;
}
// manual CMUX transitions: Exit CMUX
if (m == modem_mode::CMUX_MANUAL_EXIT && mode == modem_mode::CMUX_MANUAL_MODE) {
if (exit_cmux()) {
mode = modem_mode::COMMAND_MODE;
return true;
}
mode = modem_mode::UNDEF;
return false;
}
// manual CMUX transitions: Swap terminals
if (m == modem_mode::CMUX_MANUAL_SWAP && mode == modem_mode::CMUX_MANUAL_MODE) {
other_term.swap(term);
return true;
}
return true;
}
void DTE::set_read_cb(std::function<bool(uint8_t *, size_t)> f)
{
on_data = std::move(f);
data_term->set_read_cb([this](uint8_t *data, size_t len) {
other_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 = other_term->read(buffer.get(), buffer.size);
}
if (on_data) {
return on_data(data, len);
@ -150,22 +173,22 @@ void DTE::set_read_cb(std::function<bool(uint8_t *, size_t)> f)
void DTE::set_error_cb(std::function<void(terminal_error err)> f)
{
data_term->set_error_cb(f);
command_term->set_error_cb(f);
other_term->set_error_cb(f);
term->set_error_cb(f);
}
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 = data_term->read(data, data_to_read);
auto actual_len = other_term->read(data, data_to_read);
*d = data;
return actual_len;
}
int DTE::write(uint8_t *data, size_t len)
{
return data_term->write(data, len);
return other_term->write(data, len);
}
/**