Skip to content

Commit

Permalink
feat(modem): Add support for handling URC
Browse files Browse the repository at this point in the history
Closes #180
  • Loading branch information
david-cermak committed Sep 16, 2024
1 parent d288041 commit 1b6a3b3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
8 changes: 7 additions & 1 deletion components/esp_modem/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ menu "esp-modem"
int "Size in bytes for response from AT commands returning textual values (C-API)"
default 128
help
Some AT commands returns textrual values which C-API copy as c-string to user allocated space,
Some AT commands returns textual values which C-API copy as c-string to user allocated space,
it also truncates the output data to this size. Increase this if some AT answers are truncated.

config ESP_MODEM_URC_HANDLER
bool "Enable support for adding URC handler"
default n
help
If enabled, APIs to add URC handler are available

endmenu
9 changes: 8 additions & 1 deletion components/esp_modem/include/cxx_include/esp_modem_dce.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -89,6 +89,13 @@ class DCE_T {
return dte->recover();
}

#ifdef CONFIG_ESP_MODEM_URC_HANDLER
void set_urc(got_line_cb on_read_cb)
{
dte->set_urc_cb(on_read_cb);
}
#endif

protected:
std::shared_ptr<DTE> dte;
std::shared_ptr<SpecificModule> device;
Expand Down
16 changes: 15 additions & 1 deletion components/esp_modem/include/cxx_include/esp_modem_dte.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -94,6 +94,17 @@ class DTE : public CommandableIf {
*/
void set_error_cb(std::function<void(terminal_error err)> f);

#ifdef CONFIG_ESP_MODEM_URC_HANDLER
/**
* @brief Allow setting a line callback for all incoming data
* @param line_cb
*/
void set_urc_cb(got_line_cb line_cb)
{
command_cb.urc_handler = std::move(line_cb);
}
#endif

/**
* @brief Sets the DTE to desired mode (Command/Data/Cmux)
* @param m Desired operation mode
Expand Down Expand Up @@ -191,6 +202,9 @@ class DTE : public CommandableIf {
* @brief This abstracts command callback processing and implements its locking, signaling of completion and timeouts.
*/
struct command_cb {
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
got_line_cb urc_handler {}; /*!< URC callback if enabled */
#endif
static const size_t GOT_LINE = SignalGroup::bit0; /*!< Bit indicating response available */
got_line_cb got_line; /*!< Supplied command callback */
Lock line_lock{}; /*!< Command callback locking mechanism */
Expand Down
15 changes: 11 additions & 4 deletions components/esp_modem/src/esp_modem_dte.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -50,9 +50,11 @@ void DTE::set_command_callbacks()
{
primary_term->set_read_cb([this](uint8_t *data, size_t len) {
Scoped<Lock> l(command_cb.line_lock);
if (command_cb.got_line == nullptr) {
return false;
#ifndef CONFIG_ESP_MODEM_URC_HANDLER
if (command_cb.got_line == nullptr || command_cb.result != command_result::TIMEOUT) {
return false; // this line has been processed already (got OK or FAIL previously)
}
#endif
if (data) {
// For terminals which post data directly with the callback (CMUX)
// we cannot defragment unless we allocate, but
Expand Down Expand Up @@ -347,9 +349,14 @@ void DTE::on_read(got_line_cb on_read_cb)

bool DTE::command_cb::process_line(uint8_t *data, size_t consumed, size_t len)
{
if (result != command_result::TIMEOUT) {
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
if (urc_handler) {
urc_handler(data, consumed + len);
}
if (result != command_result::TIMEOUT || got_line == nullptr) {
return false; // this line has been processed already (got OK or FAIL previously)
}
#endif
if (memchr(data + consumed, separator, len)) {
result = got_line(data, consumed + len);
if (result == command_result::OK || result == command_result::FAIL) {
Expand Down

0 comments on commit 1b6a3b3

Please sign in to comment.