diff --git a/components/esp_modem/include/esp_modem_c_api_types.h b/components/esp_modem/include/esp_modem_c_api_types.h index 66591df435e..f47e2a064c2 100644 --- a/components/esp_modem/include/esp_modem_c_api_types.h +++ b/components/esp_modem/include/esp_modem_c_api_types.h @@ -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); + /** * @} */ diff --git a/components/esp_modem/src/esp_modem_c_api.cpp b/components/esp_modem/src/esp_modem_c_api.cpp index c9ebdda2e6c..fa1bbdbb2b9 100644 --- a/components/esp_modem/src/esp_modem_c_api.cpp +++ b/components/esp_modem/src/esp_modem_c_api.cpp @@ -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; } @@ -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)); +} diff --git a/components/esp_modem/src/esp_modem_dce.cpp b/components/esp_modem/src/esp_modem_dce.cpp index 5cd5895e6b1..afd41ffa6e0 100644 --- a/components/esp_modem/src/esp_modem_dce.cpp +++ b/components/esp_modem/src/esp_modem_dce.cpp @@ -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); diff --git a/components/esp_modem/src/esp_modem_dte.cpp b/components/esp_modem/src/esp_modem_dte.cpp index 0da7d35b87c..ea71182cb5d 100644 --- a/components/esp_modem/src/esp_modem_dte.cpp +++ b/components/esp_modem/src/esp_modem_dte.cpp @@ -15,14 +15,14 @@ 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): - 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), term(std::move(terminal)), other_term(term), + mode(modem_mode::UNDEF) {} DTE::DTE(std::unique_ptr 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), 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) { @@ -94,7 +94,7 @@ bool DTE::setup_cmux() bool DTE::set_mode(modem_mode m) { if (mode == modem_mode::CMUX_MANUAL_MODE && - (m == modem_mode::DATA_MODE || m == modem_mode::COMMAND_MODE )) { + (m == modem_mode::DATA_MODE || m == modem_mode::COMMAND_MODE )) { // no operation for this DTE when switching DATA/COMMAND in manual CMUX return true; }