diff --git a/components/esp_modem/include/esp_modem_c_api_types.h b/components/esp_modem/include/esp_modem_c_api_types.h index 521161da7b..c8086b8af1 100644 --- a/components/esp_modem/include/esp_modem_c_api_types.h +++ b/components/esp_modem/include/esp_modem_c_api_types.h @@ -141,6 +141,21 @@ esp_err_t esp_modem_command(esp_modem_dce_t *dce, const char *command, esp_err_t */ esp_err_t esp_modem_set_apn(esp_modem_dce_t *dce, const char *apn); +#ifdef CONFIG_ESP_MODEM_URC_HANDLER +/** + * @brief Sets a handler for unsolicited result codes (URCs) from the modem + * + * This function registers a callback that is triggered whenever an unsolicited + * result code (URC) is received from the modem. URCs are typically sent by the + * modem without a prior command to notify the host about certain events or status changes. + * + * @param dce Modem DCE handle + * @param got_line_cb Callback function which is called whenever a URC line is received + * @return ESP_OK on success, ESP_FAIL on failure + */ +esp_err_t esp_modem_set_urc(esp_modem_dce_t *dce, esp_err_t(*got_line_cb)(uint8_t *data, size_t len)); +#endif + /** * @} */ diff --git a/components/esp_modem/src/esp_modem_c_api.cpp b/components/esp_modem/src/esp_modem_c_api.cpp index f25b936ed0..9b64e83c0a 100644 --- a/components/esp_modem/src/esp_modem_c_api.cpp +++ b/components/esp_modem/src/esp_modem_c_api.cpp @@ -451,3 +451,27 @@ extern "C" esp_err_t esp_modem_set_apn(esp_modem_dce_t *dce_wrap, const char *ap dce_wrap->dce->get_module()->configure_pdp_context(std::move(new_pdp)); return ESP_OK; } + +#ifdef CONFIG_ESP_MODEM_URC_HANDLER +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)) +{ + if (dce_wrap == nullptr || dce_wrap->dce == nullptr) { + return ESP_ERR_INVALID_ARG; + } + if (got_line_fn == nullptr) { + dce_wrap->dce->set_urc(nullptr); + return ESP_OK; + } + dce_wrap->dce->set_urc([got_line_fn](uint8_t *data, size_t len) { + switch (got_line_fn(data, len)) { + case ESP_OK: + return command_result::OK; + case ESP_FAIL: + return command_result::FAIL; + default: + return command_result::TIMEOUT; + } + }); + return ESP_OK; +} +#endif diff --git a/components/esp_modem/src/esp_modem_uart.cpp b/components/esp_modem/src/esp_modem_uart.cpp index df7b4882bc..945fb2889f 100644 --- a/components/esp_modem/src/esp_modem_uart.cpp +++ b/components/esp_modem/src/esp_modem_uart.cpp @@ -72,6 +72,7 @@ class UartTerminal : public Terminal { { auto t = static_cast(task_param); t->task(); + t->task_handle.task_handle = nullptr; vTaskDelete(nullptr); }