Skip to content

Commit 1b6a3b3

Browse files
committed
feat(modem): Add support for handling URC
Closes #180
1 parent d288041 commit 1b6a3b3

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

components/esp_modem/Kconfig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ menu "esp-modem"
6767
int "Size in bytes for response from AT commands returning textual values (C-API)"
6868
default 128
6969
help
70-
Some AT commands returns textrual values which C-API copy as c-string to user allocated space,
70+
Some AT commands returns textual values which C-API copy as c-string to user allocated space,
7171
it also truncates the output data to this size. Increase this if some AT answers are truncated.
7272

73+
config ESP_MODEM_URC_HANDLER
74+
bool "Enable support for adding URC handler"
75+
default n
76+
help
77+
If enabled, APIs to add URC handler are available
78+
7379
endmenu

components/esp_modem/include/cxx_include/esp_modem_dce.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -89,6 +89,13 @@ class DCE_T {
8989
return dte->recover();
9090
}
9191

92+
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
93+
void set_urc(got_line_cb on_read_cb)
94+
{
95+
dte->set_urc_cb(on_read_cb);
96+
}
97+
#endif
98+
9299
protected:
93100
std::shared_ptr<DTE> dte;
94101
std::shared_ptr<SpecificModule> device;

components/esp_modem/include/cxx_include/esp_modem_dte.hpp

Lines changed: 15 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
*/
@@ -94,6 +94,17 @@ class DTE : public CommandableIf {
9494
*/
9595
void set_error_cb(std::function<void(terminal_error err)> f);
9696

97+
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
98+
/**
99+
* @brief Allow setting a line callback for all incoming data
100+
* @param line_cb
101+
*/
102+
void set_urc_cb(got_line_cb line_cb)
103+
{
104+
command_cb.urc_handler = std::move(line_cb);
105+
}
106+
#endif
107+
97108
/**
98109
* @brief Sets the DTE to desired mode (Command/Data/Cmux)
99110
* @param m Desired operation mode
@@ -191,6 +202,9 @@ class DTE : public CommandableIf {
191202
* @brief This abstracts command callback processing and implements its locking, signaling of completion and timeouts.
192203
*/
193204
struct command_cb {
205+
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
206+
got_line_cb urc_handler {}; /*!< URC callback if enabled */
207+
#endif
194208
static const size_t GOT_LINE = SignalGroup::bit0; /*!< Bit indicating response available */
195209
got_line_cb got_line; /*!< Supplied command callback */
196210
Lock line_lock{}; /*!< Command callback locking mechanism */

components/esp_modem/src/esp_modem_dte.cpp

Lines changed: 11 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
*/
@@ -50,9 +50,11 @@ void DTE::set_command_callbacks()
5050
{
5151
primary_term->set_read_cb([this](uint8_t *data, size_t len) {
5252
Scoped<Lock> l(command_cb.line_lock);
53-
if (command_cb.got_line == nullptr) {
54-
return false;
53+
#ifndef CONFIG_ESP_MODEM_URC_HANDLER
54+
if (command_cb.got_line == nullptr || command_cb.result != command_result::TIMEOUT) {
55+
return false; // this line has been processed already (got OK or FAIL previously)
5556
}
57+
#endif
5658
if (data) {
5759
// For terminals which post data directly with the callback (CMUX)
5860
// we cannot defragment unless we allocate, but
@@ -347,9 +349,14 @@ void DTE::on_read(got_line_cb on_read_cb)
347349

348350
bool DTE::command_cb::process_line(uint8_t *data, size_t consumed, size_t len)
349351
{
350-
if (result != command_result::TIMEOUT) {
352+
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
353+
if (urc_handler) {
354+
urc_handler(data, consumed + len);
355+
}
356+
if (result != command_result::TIMEOUT || got_line == nullptr) {
351357
return false; // this line has been processed already (got OK or FAIL previously)
352358
}
359+
#endif
353360
if (memchr(data + consumed, separator, len)) {
354361
result = got_line(data, consumed + len);
355362
if (result == command_result::OK || result == command_result::FAIL) {

0 commit comments

Comments
 (0)