Skip to content

Commit 1393764

Browse files
authored
Merge pull request #477 from david-cermak/feat/modem_at_raw
feat(modem): Added support for at_raw() command
2 parents 5ab699d + 0998f3d commit 1393764

File tree

9 files changed

+80
-38
lines changed

9 files changed

+80
-38
lines changed

components/esp_modem/examples/modem_console/main/my_module_dce.hpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -17,26 +17,20 @@
1717
#include "cxx_include/esp_modem_dce_module.hpp"
1818

1919
/**
20-
* @brief Definition of a custom modem which inherits from the GenericModule, uses all its methods
21-
* and could override any of them. Here, for demonstration purposes only, we redefine just `get_module_name()`
20+
* @brief Definition of a custom DCE uses GenericModule and all its methods
21+
* but could override command processing. Here, for demonstration purposes only,
22+
* we "inject" URC handler to the actual command processing.
23+
* This is possible since we inherit from `CommandableIf` and redefine `command()` method.
24+
* Then we're able to use declare all common methods from the command library
25+
* to be processed using "our" `command()` method (with custom URC handler).
2226
*/
23-
class MyShinyModem: public esp_modem::GenericModule {
24-
using GenericModule::GenericModule;
25-
public:
26-
esp_modem::command_result get_module_name(std::string &name) override
27-
{
28-
name = "Custom Shiny Module";
29-
return esp_modem::command_result::OK;
30-
}
31-
};
32-
3327
namespace Shiny {
3428

3529
using namespace esp_modem;
3630

37-
class DCE : public esp_modem::DCE_T<MyShinyModem>, public CommandableIf {
31+
class DCE : public esp_modem::DCE_T<GenericModule>, public CommandableIf {
3832
public:
39-
using DCE_T<MyShinyModem>::DCE_T;
33+
using DCE_T<GenericModule>::DCE_T;
4034

4135
command_result
4236
command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms) override
@@ -97,7 +91,7 @@ class Factory: public ::esp_modem::dce_factory::Factory {
9791
std::shared_ptr<esp_modem::DTE> dte,
9892
esp_netif_t *netif)
9993
{
100-
return build_generic_DCE<MyShinyModem, DCE, std::unique_ptr<DCE>>(config, std::move(dte), netif);
94+
return build_generic_DCE<GenericModule, DCE, std::unique_ptr<DCE>>(config, std::move(dte), netif);
10195
}
10296

10397
};

components/esp_modem/include/cxx_include/esp_modem_command_library.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ command_result power_down_sim76xx(CommandableIf *t);
5454
command_result power_down_sim70xx(CommandableIf *t);
5555
command_result set_network_bands_sim76xx(CommandableIf *t, const std::string &mode, const int *bands, int size);
5656
command_result power_down_sim8xx(CommandableIf *t);
57-
command_result set_data_mode_sim8xx(CommandableIf *t);
57+
command_result set_data_mode_alt(CommandableIf *t);
5858
command_result set_pdp_context(CommandableIf *t, PdpContext &pdp, uint32_t timeout_ms);
5959

6060
/**

components/esp_modem/include/cxx_include/esp_modem_dce_module.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class SIM7070: public GenericModule {
144144
using GenericModule::GenericModule;
145145
public:
146146
command_result power_down() override;
147+
command_result set_data_mode() override;
148+
147149
};
148150

149151
/**
@@ -162,7 +164,6 @@ class SIM800: public GenericModule {
162164
using GenericModule::GenericModule;
163165
public:
164166
command_result power_down() override;
165-
command_result set_data_mode() override;
166167
};
167168

168169
/**

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: 20 additions & 4 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
*/
@@ -277,13 +277,13 @@ command_result set_pdp_context(CommandableIf *t, PdpContext &pdp)
277277
command_result set_data_mode(CommandableIf *t)
278278
{
279279
ESP_LOGV(TAG, "%s", __func__ );
280-
return generic_command(t, "ATD*99##\r", "CONNECT", "ERROR", 5000);
280+
return generic_command(t, "ATD*99#\r", "CONNECT", "ERROR", 5000);
281281
}
282282

283-
command_result set_data_mode_sim8xx(CommandableIf *t)
283+
command_result set_data_mode_alt(CommandableIf *t)
284284
{
285285
ESP_LOGV(TAG, "%s", __func__ );
286-
return generic_command(t, "ATD*99#\r", "CONNECT", "ERROR", 5000);
286+
return generic_command(t, "ATD*99##\r", "CONNECT", "ERROR", 5000);
287287
}
288288

289289
command_result resume_data_mode(CommandableIf *t)
@@ -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__ );

components/esp_modem/src/esp_modem_modules.cpp

Lines changed: 6 additions & 6 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
*/
@@ -67,6 +67,11 @@ command_result SIM7070::power_down()
6767
return dce_commands::power_down_sim70xx(dte.get());
6868
}
6969

70+
command_result SIM7070::set_data_mode()
71+
{
72+
return dce_commands::set_data_mode_alt(dte.get());
73+
}
74+
7075
command_result SIM7000::power_down()
7176
{
7277
return dce_commands::power_down_sim70xx(dte.get());
@@ -77,11 +82,6 @@ command_result SIM800::power_down()
7782
return dce_commands::power_down_sim8xx(dte.get());
7883
}
7984

80-
command_result SIM800::set_data_mode()
81-
{
82-
return dce_commands::set_data_mode_sim8xx(dte.get());
83-
}
84-
8585
command_result BG96::set_pdp_context(esp_modem::PdpContext &pdp)
8686
{
8787
return dce_commands::set_pdp_context(dte.get(), pdp, 300);

components/esp_modem/test/host_test/main/LoopbackTerm.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -36,7 +36,7 @@ int LoopbackTerm::write(uint8_t *data, size_t len)
3636
} else if (command == "ATO\r") {
3737
response = "ERROR\r\n";
3838
} else if (command.find("ATD") != std::string::npos) {
39-
response = "CONNECT\r\n";
39+
response = "CONNECT\n";
4040
} else if (command.find("AT+CSQ\r") != std::string::npos) {
4141
response = "+CSQ: 123,456\n\r\nOK\r\n";
4242
} else if (command.find("AT+CGMM\r") != std::string::npos) {
@@ -74,6 +74,9 @@ int LoopbackTerm::write(uint8_t *data, size_t len)
7474
}
7575
if (len > 2 && data[0] == 0xf9) { // Simple CMUX responder
7676
// turn the request into a reply -> implements CMUX loopback
77+
// Note: This simple CMUX responder only updates CMUX headers and replaces payload.
78+
// It means that all responses (that we test) must be shorter or equal to the requests
79+
// For example ATD (dial command): sizeof("ATD*99#") >= sizeof("CONNECT");
7780
if (data[2] == 0x3f || data[2] == 0x53) { // SABM command
7881
data[2] = 0x73;
7982
} else if (data[2] == 0xef) { // Generic request

docs/esp_modem/en/advanced_api.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ Create custom module
2626

2727
Creating a custom module is necessary if the application needs to use a specific device that is not supported
2828
and their commands differ from any of the supported devices. In this case it is recommended to define a new class
29-
representing this specific device and derive from the :cpp:class:`esp_modem::GenericModule`. In order to instantiate
30-
the appropriate DCE of this module, application could use :ref:`the DCE factory<dce_factory>`, and build the DCE with
31-
the specific module, using :cpp:func:`esp_modem::dce_factory::Factory::build`.
29+
representing this specific device and derive from the :cpp:class:`esp_modem::GenericModule` (or any other available
30+
module, that's close to your custom device). Then you can create the DCE using :ref:`the DCE factory<dce_factory>`
31+
public method :cpp:func:`esp_modem::dce_factory::Factory::create_unique_dce_from`.
3232

33-
Please refer to the implementation of the existing modules.
34-
35-
Please note that the ``modem_console`` example defines a trivial custom modem DCE which overrides one command,
33+
Please note that the ``pppos_client`` example defines a trivial custom DCE which overrides one command, and adds a new command
3634
for demonstration purposes only.
3735

36+
It is also possible to create a specific DCE class that would conform to the generic ``DCE`` API and use all generic commands,
37+
work with commands differently. This might be useful to add some custom preprocessing of commands or replies.
38+
Please check the ``modem_console`` example with ``CONFIG_EXAMPLE_MODEM_DEVICE_SHINY=y`` configuration which demonstrates
39+
overriding default ``command()`` method to implement URC processing in user space.
3840

3941
Create new communication interface
4042
----------------------------------

0 commit comments

Comments
 (0)