Skip to content

Commit 295d99d

Browse files
committed
fix(modem): Add support for URC handler into C-API
Closes #180
1 parent b65cff3 commit 295d99d

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

components/esp_modem/include/esp_modem_c_api_types.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ esp_err_t esp_modem_command(esp_modem_dce_t *dce, const char *command, esp_err_t
141141
*/
142142
esp_err_t esp_modem_set_apn(esp_modem_dce_t *dce, const char *apn);
143143

144+
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
145+
/**
146+
* @brief Sets a handler for unsolicited result codes (URCs) from the modem
147+
*
148+
* This function registers a callback that is triggered whenever an unsolicited
149+
* result code (URC) is received from the modem. URCs are typically sent by the
150+
* modem without a prior command to notify the host about certain events or status changes.
151+
*
152+
* @param dce Modem DCE handle
153+
* @param got_line_cb Callback function which is called whenever a URC line is received
154+
* @return ESP_OK on success, ESP_FAIL on failure
155+
*/
156+
esp_err_t esp_modem_set_urc(esp_modem_dce_t *dce, esp_err_t(*got_line_cb)(uint8_t *data, size_t len));
157+
#endif
158+
144159
/**
145160
* @}
146161
*/

components/esp_modem/src/esp_modem_c_api.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,27 @@ extern "C" esp_err_t esp_modem_set_apn(esp_modem_dce_t *dce_wrap, const char *ap
451451
dce_wrap->dce->get_module()->configure_pdp_context(std::move(new_pdp));
452452
return ESP_OK;
453453
}
454+
455+
#ifdef CONFIG_ESP_MODEM_URC_HANDLER
456+
extern "C" esp_err_t esp_modem_set_urc(esp_modem_dce_t *dce_wrap, esp_err_t(*got_line_fn)(uint8_t *data, size_t len))
457+
{
458+
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
459+
return ESP_ERR_INVALID_ARG;
460+
}
461+
if (got_line_fn == nullptr) {
462+
dce_wrap->dce->set_urc(nullptr);
463+
return ESP_OK;
464+
}
465+
dce_wrap->dce->set_urc([got_line_fn](uint8_t *data, size_t len) {
466+
switch (got_line_fn(data, len)) {
467+
case ESP_OK:
468+
return command_result::OK;
469+
case ESP_FAIL:
470+
return command_result::FAIL;
471+
default:
472+
return command_result::TIMEOUT;
473+
}
474+
});
475+
return ESP_OK;
476+
}
477+
#endif

components/esp_modem/src/esp_modem_uart.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class UartTerminal : public Terminal {
7272
{
7373
auto t = static_cast<UartTerminal *>(task_param);
7474
t->task();
75+
t->task_handle.task_handle = nullptr;
7576
vTaskDelete(nullptr);
7677
}
7778

0 commit comments

Comments
 (0)