Skip to content

Commit

Permalink
fix(esp-modem): Support AT with callback in C-API
Browse files Browse the repository at this point in the history
Closes #143
  • Loading branch information
david-cermak committed Nov 21, 2022
1 parent 4111640 commit a6fc861
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 44 deletions.
4 changes: 2 additions & 2 deletions components/esp_modem/include/cxx_include/esp_modem_dte.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class DTE : public CommandableIf {
Lock internal_lock{}; /*!< Locks DTE operations */
unique_buffer buffer; /*!< DTE buffer */
std::shared_ptr<CMux> cmux_term; /*!< Primary terminal for this DTE */
std::shared_ptr<Terminal> term; /*!< Reference to the primary terminal (mostly for sending commands) */
std::shared_ptr<Terminal> other_term; /*!< Secondary terminal for this DTE */
std::shared_ptr<Terminal> primary_term; /*!< Reference to the primary terminal (mostly for sending commands) */
std::shared_ptr<Terminal> secondary_term; /*!< Secondary terminal for this DTE */
modem_mode mode; /*!< DTE operation mode */
SignalGroup signal; /*!< Event group used to signal request-response operations */
command_result result; /*!< Command result of the currently exectuted command */
Expand Down
2 changes: 2 additions & 0 deletions components/esp_modem/include/esp_modem_c_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ esp_err_t esp_modem_set_error_cb(esp_modem_dce_t *dce, esp_modem_terminal_error_
*/
esp_err_t esp_modem_set_mode(esp_modem_dce_t *dce, esp_modem_dce_mode_t mode);

esp_err_t esp_modem_command(esp_modem_dce_t *dce, const char *command, esp_err_t(*got_line_cb)(uint8_t *data, size_t len), uint32_t timeout_ms);

/**
* @}
*/
Expand Down
50 changes: 34 additions & 16 deletions components/esp_modem/src/esp_modem_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@ extern "C" esp_err_t esp_modem_set_mode(esp_modem_dce_t *dce_wrap, esp_modem_dce
return ESP_ERR_INVALID_ARG;
}
switch (mode) {
case ESP_MODEM_MODE_DATA:
return dce_wrap->dce->set_mode(modem_mode::DATA_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_COMMAND:
return dce_wrap->dce->set_mode(modem_mode::COMMAND_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_EXIT:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_EXIT) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_SWAP:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_SWAP) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_DATA:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_DATA) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_COMMAND:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_COMMAND) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_DATA:
return dce_wrap->dce->set_mode(modem_mode::DATA_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_COMMAND:
return dce_wrap->dce->set_mode(modem_mode::COMMAND_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_EXIT:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_EXIT) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_SWAP:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_SWAP) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_DATA:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_DATA) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_COMMAND:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_COMMAND) ? ESP_OK : ESP_FAIL;
}
return ESP_ERR_NOT_SUPPORTED;
}
Expand Down Expand Up @@ -400,3 +400,21 @@ extern "C" esp_err_t esp_modem_set_pdp_context(esp_modem_dce_t *dce_wrap, esp_mo
pdp.protocol_type = c_api_pdp->protocol_type;
return command_response_to_esp_err(dce_wrap->dce->set_pdp_context(pdp));
}

extern "C" esp_err_t esp_modem_command(esp_modem_dce_t *dce_wrap, const char *command, esp_err_t(*got_line_fn)(uint8_t *data, size_t len), uint32_t timeout_ms)
{
if (dce_wrap == nullptr || dce_wrap->dce == nullptr || command == nullptr || got_line_fn == nullptr) {
return ESP_ERR_INVALID_ARG;
}
std::string cmd(command);
return command_response_to_esp_err(dce_wrap->dce->command(cmd, [got_line_fn](uint8_t *data, size_t len) {
switch (got_line_fn(data, len)) {
case ESP_OK:
return command_result::OK;
case ESP_FAIL:
return command_result::FAIL;
default:
return command_result::TIMEOUT;
}
}, timeout_ms));
}
2 changes: 1 addition & 1 deletion components/esp_modem/src/esp_modem_dce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool DCE_Mode::set_unsafe(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m
mode = modem_mode::CMUX_MODE;
return transitions::enter_data(*dte, *device, netif);
case modem_mode::CMUX_MANUAL_MODE:
if (mode != modem_mode::COMMAND_MODE) {
if (mode != modem_mode::COMMAND_MODE && mode != modem_mode::UNDEF) {
return false;
}
device->set_mode(modem_mode::CMUX_MODE);
Expand Down
51 changes: 26 additions & 25 deletions components/esp_modem/src/esp_modem_dte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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), term(std::move(terminal)), other_term(term),
mode(modem_mode::UNDEF) {}
buffer(config->dte_buffer_size),
cmux_term(nullptr), primary_term(std::move(terminal)), secondary_term(primary_term),
mode(modem_mode::UNDEF) {}

DTE::DTE(std::unique_ptr<Terminal> terminal):
buffer(dte_default_buffer_size),
cmux_term(nullptr), term(std::move(terminal)), other_term(term),
mode(modem_mode::UNDEF) {}
buffer(dte_default_buffer_size),
cmux_term(nullptr), primary_term(std::move(terminal)), secondary_term(primary_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);
term->set_read_cb([this, got_line, separator](uint8_t *data, size_t len) {
primary_term->set_read_cb([this, got_line, separator](uint8_t *data, size_t len) {
if (!data) {
data = buffer.get();
len = term->read(data + buffer.consumed, buffer.size - buffer.consumed);
len = primary_term->read(data + buffer.consumed, buffer.size - buffer.consumed);
} else {
buffer.consumed = 0; // if the underlying terminal contains data, we cannot fragment
}
Expand All @@ -46,13 +46,13 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
buffer.consumed += len;
return false;
});
term->write((uint8_t *)command.c_str(), command.length());
primary_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;
term->set_read_cb(nullptr);
primary_term->set_read_cb(nullptr);
return result;
}

Expand All @@ -68,26 +68,26 @@ bool DTE::exit_cmux()
}
auto ejected = cmux_term->detach();
// return the ejected terminal and buffer back to this DTE
term = std::move(ejected.first);
primary_term = std::move(ejected.first);
buffer = std::move(ejected.second);
other_term = term;
secondary_term = primary_term;
return true;
}

bool DTE::setup_cmux()
{
cmux_term = std::make_shared<CMux>(term, std::move(buffer));
cmux_term = std::make_shared<CMux>(primary_term, std::move(buffer));
if (cmux_term == nullptr) {
return false;
}
if (!cmux_term->init()) {
return false;
}
term = std::make_unique<CMuxInstance>(cmux_term, 0);
if (term == nullptr) {
primary_term = std::make_unique<CMuxInstance>(cmux_term, 0);
if (primary_term == nullptr) {
return false;
}
other_term = std::make_unique<CMuxInstance>(cmux_term, 1);
secondary_term = std::make_unique<CMuxInstance>(cmux_term, 1);
return true;
}

Expand All @@ -108,7 +108,7 @@ bool DTE::set_mode(modem_mode m)
if (m == modem_mode::DATA_MODE) {
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);
secondary_term.swap(primary_term);
} else {
mode = m;
}
Expand Down Expand Up @@ -150,19 +150,20 @@ bool DTE::set_mode(modem_mode m)
}
// manual CMUX transitions: Swap terminals
if (m == modem_mode::CMUX_MANUAL_SWAP && mode == modem_mode::CMUX_MANUAL_MODE) {
other_term.swap(term);
secondary_term.swap(primary_term);
return true;
}
return true;
mode = modem_mode::UNDEF;
return false;
}

void DTE::set_read_cb(std::function<bool(uint8_t *, size_t)> f)
{
on_data = std::move(f);
other_term->set_read_cb([this](uint8_t *data, size_t len) {
secondary_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 = other_term->read(buffer.get(), buffer.size);
len = secondary_term->read(buffer.get(), buffer.size);
}
if (on_data) {
return on_data(data, len);
Expand All @@ -173,22 +174,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)
{
other_term->set_error_cb(f);
term->set_error_cb(f);
secondary_term->set_error_cb(f);
primary_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 = other_term->read(data, data_to_read);
auto actual_len = secondary_term->read(data, data_to_read);
*d = data;
return actual_len;
}

int DTE::write(uint8_t *data, size_t len)
{
return other_term->write(data, len);
return secondary_term->write(data, len);
}

/**
Expand Down

0 comments on commit a6fc861

Please sign in to comment.