Skip to content

Commit 2208e76

Browse files
authored
Merge pull request #620 from david-cermak/feat/modem_urc
[modem]: Add support for handling URC
2 parents d288041 + c348076 commit 2208e76

File tree

6 files changed

+56
-37
lines changed

6 files changed

+56
-37
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) {

components/esp_modem/test/target/main/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
idf_component_register(SRCS "pppd_test.cpp"
22
"NetworkDCE.cpp"
3-
INCLUDE_DIRS "$ENV{IDF_PATH}/tools/catch"
43
REQUIRES esp_modem)
54

65
set_target_properties(${COMPONENT_LIB} PROPERTIES

components/esp_modem/test/target/main/pppd_test.cpp

Lines changed: 15 additions & 29 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,9 +17,6 @@
1717
#include "freertos/FreeRTOS.h"
1818
#include "freertos/event_groups.h"
1919

20-
#define CATCH_CONFIG_MAIN
21-
#include "catch.hpp"
22-
2320
static const char *TAG = "pppd_test";
2421
static EventGroupHandle_t event_group = NULL;
2522

@@ -76,6 +73,9 @@ esp_err_t modem_init_network(esp_netif_t *netif);
7673
void modem_start_network();
7774
void modem_stop_network();
7875

76+
bool test_connect();
77+
bool test_disconnect();
78+
7979
extern "C" void app_main(void)
8080
{
8181

@@ -99,41 +99,27 @@ extern "C" void app_main(void)
9999
#endif
100100

101101
modem_start_network();
102-
Catch::Session session;
103-
int numFailed = session.run();
104-
if (numFailed > 0) {
105-
ESP_LOGE(TAG, "Test FAILED!");
102+
103+
bool t1 = test_connect();
104+
bool t2 = test_disconnect();
105+
106+
if (t1 && t2) {
107+
ESP_LOGI(TAG, "All tests passed");
106108
} else {
107-
ESP_LOGI(TAG, "Test passed!");
109+
ESP_LOGE(TAG, "Test FAILED!");
108110
}
109111

110112
}
111113

112-
TEST_CASE("Connect test", "[esp_modem]")
114+
bool test_connect() //("Connect test", "[esp_modem]")
113115
{
114116
EventBits_t b = xEventGroupWaitBits(event_group, 1, pdTRUE, pdFALSE, pdMS_TO_TICKS(15000));
115-
CHECK(b == 1);
117+
return b == 1;
116118
}
117119

118-
TEST_CASE("Disconnection test", "[esp_modem]")
120+
bool test_disconnect() //("Disconnection test", "[esp_modem]")
119121
{
120122
modem_stop_network();
121123
EventBits_t b = xEventGroupWaitBits(event_group, 2, pdTRUE, pdFALSE, pdMS_TO_TICKS(15000));
122-
CHECK(b == 2);
123-
}
124-
125-
126-
extern "C" {
127-
128-
static void handle(int nr)
129-
{
130-
ESP_LOGE(TAG, "Signal handler %d", nr);
131-
}
132-
133-
_sig_func_ptr signal (int nr, _sig_func_ptr)
134-
{
135-
return handle;
136-
}
137-
138-
124+
return b == 2;
139125
}

0 commit comments

Comments
 (0)