Skip to content

Commit

Permalink
Merge pull request #649 from embedcat/master
Browse files Browse the repository at this point in the history
[modem]: Add ability to change ESP_MODEM_C_API_STR_MAX from Kconfig (IDFGH-13668)
  • Loading branch information
david-cermak authored Sep 12, 2024
2 parents 571a741 + 1790989 commit 8e4d43e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
7 changes: 7 additions & 0 deletions components/esp_modem/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ menu "esp-modem"
dce_factory::Factory::create_unique_dce_from<CustomModule, DCE*>(dce_config, std::move(dte), netif)
Please refer to the pppos_client example for more details.

config ESP_MODEM_C_API_STR_MAX
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,
it also truncates the output data to this size. Increase this if some AT answers are truncated.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ DCE *esp_modem_create_custom_dce(const esp_modem_dce_config_t *dce_config, std::
/**
* @brief This API is only needed for extending standard C-API, since we added get_time() method to our CustomModule
*
* @note This header is included from esp_modem_c_api.cpp, so it could use ESP_MODEM_C_API_STR_MAX macro
* @note This header is included from esp_modem_c_api.cpp, so it could use CONFIG_ESP_MODEM_C_API_STR_MAX macro
* indicating maximum C-API string size
*
* @note In order to access the newly added API get_time(), we have to static_cast<> the GenericModule from DCE
Expand All @@ -70,10 +70,10 @@ extern "C" esp_err_t esp_modem_get_time(esp_modem_dce_t *dce_wrap, char *p_time)
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
return ESP_ERR_INVALID_ARG;
}
std::string time{ESP_MODEM_C_API_STR_MAX};
std::string time{CONFIG_ESP_MODEM_C_API_STR_MAX};
auto ret = command_response_to_esp_err(static_cast<SIM7600_WITH_TIME *>(dce_wrap->dce->get_module())->get_time(time));
if (ret == ESP_OK && !time.empty()) {
strlcpy(p_time, time.c_str(), ESP_MODEM_C_API_STR_MAX);
strlcpy(p_time, time.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
}
return ret;
}
16 changes: 6 additions & 10 deletions components/esp_modem/src/esp_modem_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
#include "exception_stub.hpp"
#include "esp_private/c_api_wrapper.hpp"

#ifndef ESP_MODEM_C_API_STR_MAX
#define ESP_MODEM_C_API_STR_MAX 128
#endif

#ifndef HAVE_STRLCPY
size_t strlcpy(char *dest, const char *src, size_t len);
#endif
Expand Down Expand Up @@ -180,7 +176,7 @@ extern "C" esp_err_t esp_modem_at(esp_modem_dce_t *dce_wrap, const char *at, cha
std::string at_str(at);
auto ret = command_response_to_esp_err(dce_wrap->dce->at(at_str, out, timeout));
if ((p_out != NULL) && (!out.empty())) {
strlcpy(p_out, out.c_str(), ESP_MODEM_C_API_STR_MAX);
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
}
return ret;
}
Expand All @@ -201,7 +197,7 @@ extern "C" esp_err_t esp_modem_get_imsi(esp_modem_dce_t *dce_wrap, char *p_imsi)
std::string imsi;
auto ret = command_response_to_esp_err(dce_wrap->dce->get_imsi(imsi));
if (ret == ESP_OK && !imsi.empty()) {
strlcpy(p_imsi, imsi.c_str(), ESP_MODEM_C_API_STR_MAX);
strlcpy(p_imsi, imsi.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
}
return ret;
}
Expand All @@ -214,7 +210,7 @@ extern "C" esp_err_t esp_modem_at_raw(esp_modem_dce_t *dce_wrap, const char *cmd
std::string out;
auto ret = command_response_to_esp_err(dce_wrap->dce->at_raw(cmd, out, pass, fail, timeout));
if ((p_out != NULL) && (!out.empty())) {
strlcpy(p_out, out.c_str(), ESP_MODEM_C_API_STR_MAX);
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
}
return ret;
}
Expand Down Expand Up @@ -244,7 +240,7 @@ extern "C" esp_err_t esp_modem_get_imei(esp_modem_dce_t *dce_wrap, char *p_imei)
std::string imei;
auto ret = command_response_to_esp_err(dce_wrap->dce->get_imei(imei));
if (ret == ESP_OK && !imei.empty()) {
strlcpy(p_imei, imei.c_str(), ESP_MODEM_C_API_STR_MAX);
strlcpy(p_imei, imei.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
}
return ret;
}
Expand All @@ -258,7 +254,7 @@ extern "C" esp_err_t esp_modem_get_operator_name(esp_modem_dce_t *dce_wrap, char
int act;
auto ret = command_response_to_esp_err(dce_wrap->dce->get_operator_name(name, act));
if (ret == ESP_OK && !name.empty()) {
strlcpy(p_name, name.c_str(), ESP_MODEM_C_API_STR_MAX);
strlcpy(p_name, name.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
*p_act = act;
}
return ret;
Expand All @@ -272,7 +268,7 @@ extern "C" esp_err_t esp_modem_get_module_name(esp_modem_dce_t *dce_wrap, char *
std::string name;
auto ret = command_response_to_esp_err(dce_wrap->dce->get_module_name(name));
if (ret == ESP_OK && !name.empty()) {
strlcpy(p_name, name.c_str(), ESP_MODEM_C_API_STR_MAX);
strlcpy(p_name, name.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/esp_modem/en/api_docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ These functions are the actual commands to communicate with the modem using AT c

Note that the functions which implement AT commands returning textual values use plain ``char *``
pointer as the return value. The API expects the output data to point to user allocated space of at least
``ESP_MODEM_C_API_STR_MAX`` (64 by default) bytes, it also truncates the output data to this size.
``CONFIG_ESP_MODEM_C_API_STR_MAX`` (128 by default) bytes, it also truncates the output data to this size.

.. doxygenfile:: esp_modem_api_commands.h

Expand Down

0 comments on commit 8e4d43e

Please sign in to comment.