Skip to content

Commit

Permalink
fix(modem): Add support for URC handler into C-API
Browse files Browse the repository at this point in the history
Closes #180
  • Loading branch information
david-cermak committed Nov 20, 2024
1 parent b65cff3 commit 295d99d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions components/esp_modem/include/esp_modem_c_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
* @}
*/
Expand Down
24 changes: 24 additions & 0 deletions components/esp_modem/src/esp_modem_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions components/esp_modem/src/esp_modem_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class UartTerminal : public Terminal {
{
auto t = static_cast<UartTerminal *>(task_param);
t->task();
t->task_handle.task_handle = nullptr;
vTaskDelete(nullptr);
}

Expand Down

0 comments on commit 295d99d

Please sign in to comment.