Skip to content

Commit ae38110

Browse files
committed
feat(modem): Added support for at_raw() command
at_raw() sends raw string and supports custom pass/fail phrases for the API to return OK/FAIL. It also returns raw, unprocessed string as output. This allows sending custom requests, such as: * dce->at_raw("", resp, "RDY", "...", 20000) -- waiting for reset * esp_modem_at_raw(dce, "+++", resp, "DISCONNECTED", "ERROR", 5000) -- exits PPP mode Closes #471 Partially addresses #468
1 parent 5ab699d commit ae38110

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

components/esp_modem/include/generate/esp_modem_command_declare.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ ESP_MODEM_DECLARE_DCE_COMMAND(store_profile, command_result, 0) \
4444
*/\
4545
ESP_MODEM_DECLARE_DCE_COMMAND(set_pin, command_result, 1, STRING_IN(p1, pin)) \
4646
\
47+
/**
48+
* @brief Execute the supplied AT command in raw mode (doesn't append '\r' to command, returns everything)
49+
* @param[in] cmd String command that's send to DTE
50+
* @param[out] out Raw output from DTE
51+
* @param[in] pass Pattern in response for the API to return OK
52+
* @param[in] fail Pattern in response for the API to return FAIL
53+
* @param[in] cmd String command that's send to DTE
54+
* @param[in] timeout AT command timeout in milliseconds
55+
* @return OK, FAIL or TIMEOUT
56+
*/\
57+
ESP_MODEM_DECLARE_DCE_COMMAND(at_raw, command_result, 5, STRING_IN(p1, cmd), STRING_OUT(p2, out), STRING_IN(p3, pass), STRING_IN(p4, fail), INT_IN(p5, timeout)) \
58+
\
4759
/**
4860
* @brief Execute the supplied AT command
4961
* @param[in] cmd AT command

components/esp_modem/src/esp_modem_c_api.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -17,7 +17,7 @@
1717
#include "esp_private/c_api_wrapper.hpp"
1818

1919
#ifndef ESP_MODEM_C_API_STR_MAX
20-
#define ESP_MODEM_C_API_STR_MAX 64
20+
#define ESP_MODEM_C_API_STR_MAX 128
2121
#endif
2222

2323
#ifndef HAVE_STRLCPY
@@ -206,6 +206,20 @@ extern "C" esp_err_t esp_modem_get_imsi(esp_modem_dce_t *dce_wrap, char *p_imsi)
206206
return ret;
207207
}
208208

209+
extern "C" esp_err_t esp_modem_at_raw(esp_modem_dce_t *dce_wrap, const char *cmd, char *p_out, const char *pass, const char *fail, int timeout)
210+
{
211+
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
212+
return ESP_ERR_INVALID_ARG;
213+
}
214+
std::string out;
215+
auto ret = command_response_to_esp_err(dce_wrap->dce->at_raw(cmd, out, pass, fail, timeout));
216+
if ((p_out != NULL) && (!out.empty())) {
217+
strlcpy(p_out, out.c_str(), ESP_MODEM_C_API_STR_MAX);
218+
}
219+
return ret;
220+
}
221+
222+
209223
extern "C" esp_err_t esp_modem_set_flow_control(esp_modem_dce_t *dce_wrap, int dce_flow, int dte_flow)
210224
{
211225
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {

components/esp_modem/src/esp_modem_command_library.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -394,6 +394,22 @@ command_result at(CommandableIf *t, const std::string &cmd, std::string &out, in
394394
return generic_get_string(t, at_command, out, timeout);
395395
}
396396

397+
command_result at_raw(CommandableIf *t, const std::string &cmd, std::string &out, const std::string &pass, const std::string &fail, int timeout = 500)
398+
{
399+
ESP_LOGV(TAG, "%s", __func__ );
400+
return t->command(cmd, [&](uint8_t *data, size_t len) {
401+
out.assign(reinterpret_cast<char *>(data), len);
402+
403+
if (out.find(pass) != std::string::npos) {
404+
return command_result::OK;
405+
} else if (out.find(fail) != std::string::npos) {
406+
return command_result::FAIL;
407+
}
408+
409+
return command_result::TIMEOUT;
410+
}, timeout);
411+
}
412+
397413
command_result get_signal_quality(CommandableIf *t, int &rssi, int &ber)
398414
{
399415
ESP_LOGV(TAG, "%s", __func__ );

0 commit comments

Comments
 (0)