From 17909892427354dc03d795ddd5a5d9ff116ee106 Mon Sep 17 00:00:00 2001 From: embedcat Date: Wed, 11 Sep 2024 11:40:09 +0300 Subject: [PATCH] feat(modem): add ability to change ESP_MODEM_C_API_STR_MAX from Kconfig --- components/esp_modem/Kconfig | 7 +++++++ .../examples/pppos_client/main/custom_module.hpp | 6 +++--- components/esp_modem/src/esp_modem_c_api.cpp | 16 ++++++---------- docs/esp_modem/en/api_docs.rst | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/components/esp_modem/Kconfig b/components/esp_modem/Kconfig index 6896428d0..88cc4a21c 100644 --- a/components/esp_modem/Kconfig +++ b/components/esp_modem/Kconfig @@ -63,4 +63,11 @@ menu "esp-modem" dce_factory::Factory::create_unique_dce_from(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 diff --git a/components/esp_modem/examples/pppos_client/main/custom_module.hpp b/components/esp_modem/examples/pppos_client/main/custom_module.hpp index 13125ba7d..aa0012bb1 100644 --- a/components/esp_modem/examples/pppos_client/main/custom_module.hpp +++ b/components/esp_modem/examples/pppos_client/main/custom_module.hpp @@ -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 @@ -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(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; } diff --git a/components/esp_modem/src/esp_modem_c_api.cpp b/components/esp_modem/src/esp_modem_c_api.cpp index 033e6f72a..f25b936ed 100644 --- a/components/esp_modem/src/esp_modem_c_api.cpp +++ b/components/esp_modem/src/esp_modem_c_api.cpp @@ -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 @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; @@ -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; } diff --git a/docs/esp_modem/en/api_docs.rst b/docs/esp_modem/en/api_docs.rst index 4ace0e1ac..3b15b6994 100644 --- a/docs/esp_modem/en/api_docs.rst +++ b/docs/esp_modem/en/api_docs.rst @@ -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