mirror of
https://github.com/espressif/esp-protocols.git
synced 2026-07-06 00:20:49 +02:00
fix(modem): Truncate C-API out buffer len to API_STR_MAX
And document the use of C-API and min buffer requirements
This commit is contained in:
@@ -75,6 +75,9 @@ menu "esp-modem"
|
||||
help
|
||||
Some AT commands returns textual 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.
|
||||
Callers of esp_modem_get_imsi(), esp_modem_get_imei(), esp_modem_get_operator_name(),
|
||||
esp_modem_get_module_name(), esp_modem_at(), and esp_modem_at_raw() must allocate output
|
||||
buffers of at least this many bytes (see ESP_MODEM_C_API_STR_BUF_SIZE).
|
||||
|
||||
config ESP_MODEM_URC_HANDLER
|
||||
bool "Enable support for adding URC handler"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -21,7 +21,7 @@ extern "C" {
|
||||
esp_err_t esp_modem_sync(esp_modem_dce_t *dce);
|
||||
/**
|
||||
* @brief Reads the operator name
|
||||
* @param[out] name operator name
|
||||
* @param[out] name operator name; must point to a buffer of at least @c ESP_MODEM_C_API_STR_BUF_SIZE bytes
|
||||
* @param[out] act access technology
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/
|
||||
@@ -47,7 +47,7 @@ esp_err_t esp_modem_reset_pin(esp_modem_dce_t *dce, const char *puk, const char
|
||||
/**
|
||||
* @brief Execute the supplied AT command in raw mode (doesn't append '\r' to command, returns everything)
|
||||
* @param[in] cmd String command that's send to DTE
|
||||
* @param[out] out Raw output from DTE
|
||||
* @param[out] out Raw output from DTE; must point to a buffer of at least @c ESP_MODEM_C_API_STR_BUF_SIZE bytes
|
||||
* @param[in] pass Pattern in response for the API to return OK
|
||||
* @param[in] fail Pattern in response for the API to return FAIL
|
||||
* @param[in] timeout AT command timeout in milliseconds
|
||||
@@ -57,7 +57,7 @@ esp_err_t esp_modem_at_raw(esp_modem_dce_t *dce, const char *cmd, char *out, con
|
||||
/**
|
||||
* @brief Execute the supplied AT command
|
||||
* @param[in] cmd AT command
|
||||
* @param[out] out Command output string
|
||||
* @param[out] out Command output string; must point to a buffer of at least @c ESP_MODEM_C_API_STR_BUF_SIZE bytes
|
||||
* @param[in] timeout AT command timeout in milliseconds
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/
|
||||
@@ -121,19 +121,19 @@ esp_err_t esp_modem_set_command_mode(esp_modem_dce_t *dce);
|
||||
esp_err_t esp_modem_set_cmux(esp_modem_dce_t *dce);
|
||||
/**
|
||||
* @brief Reads the IMSI number
|
||||
* @param[out] imsi Module's IMSI number
|
||||
* @param[out] imsi Module's IMSI number; must point to a buffer of at least @c ESP_MODEM_C_API_STR_BUF_SIZE bytes
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/
|
||||
esp_err_t esp_modem_get_imsi(esp_modem_dce_t *dce, char *imsi);
|
||||
/**
|
||||
* @brief Reads the IMEI number
|
||||
* @param[out] imei Module's IMEI number
|
||||
* @param[out] imei Module's IMEI number; must point to a buffer of at least @c ESP_MODEM_C_API_STR_BUF_SIZE bytes
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/
|
||||
esp_err_t esp_modem_get_imei(esp_modem_dce_t *dce, char *imei);
|
||||
/**
|
||||
* @brief Reads the module name
|
||||
* @param[out] name module name
|
||||
* @param[out] name module name; must point to a buffer of at least @c ESP_MODEM_C_API_STR_BUF_SIZE bytes
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/
|
||||
esp_err_t esp_modem_get_module_name(esp_modem_dce_t *dce, char *name);
|
||||
|
||||
@@ -260,7 +260,7 @@ void app_main(void)
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_MODEM_DEVICE_CUSTOM
|
||||
{
|
||||
char time[64];
|
||||
char time[ESP_MODEM_C_API_STR_BUF_SIZE];
|
||||
err = esp_modem_get_time(dce, time);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_modem_get_time failed with %d %s", err, esp_err_to_name(err));
|
||||
@@ -332,7 +332,7 @@ void app_main(void)
|
||||
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err);
|
||||
return;
|
||||
}
|
||||
char imsi[32];
|
||||
char imsi[ESP_MODEM_C_API_STR_BUF_SIZE];
|
||||
err = esp_modem_get_imsi(dce, imsi);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_modem_get_imsi failed with %d", err);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_modem_config.h"
|
||||
#include "esp_netif.h"
|
||||
|
||||
@@ -25,6 +26,15 @@ typedef struct esp_modem_PdpContext_t {
|
||||
const char *apn;
|
||||
} esp_modem_PdpContext_t;
|
||||
|
||||
/**
|
||||
* @brief Minimum byte size of caller-allocated buffers for C-API string output parameters.
|
||||
*
|
||||
* Buffers passed to esp_modem_get_imsi(), esp_modem_get_imei(),
|
||||
* esp_modem_get_operator_name(), esp_modem_get_module_name(), esp_modem_at(), and
|
||||
* esp_modem_at_raw() must be at least this many bytes (including space for a NUL terminator).
|
||||
*/
|
||||
#define ESP_MODEM_C_API_STR_BUF_SIZE CONFIG_ESP_MODEM_C_API_STR_MAX
|
||||
|
||||
/**
|
||||
* @brief SIM PIN status reported by AT+CPIN?
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,14 @@
|
||||
size_t strlcpy(char *dest, const char *src, size_t len);
|
||||
#endif
|
||||
|
||||
static void truncate_to_c_api_max(std::string &s)
|
||||
{
|
||||
const size_t max_len = CONFIG_ESP_MODEM_C_API_STR_MAX - 1;
|
||||
if (s.size() > max_len) {
|
||||
s.resize(max_len);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ESP_MODEM_ADD_CUSTOM_MODULE
|
||||
#include CONFIG_ESP_MODEM_CUSTOM_MODULE_HEADER
|
||||
#endif
|
||||
@@ -239,6 +247,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)) {
|
||||
truncate_to_c_api_max(out);
|
||||
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
|
||||
}
|
||||
return ret;
|
||||
@@ -270,6 +279,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) {
|
||||
truncate_to_c_api_max(imsi);
|
||||
strlcpy(p_imsi, imsi.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
|
||||
}
|
||||
return ret;
|
||||
@@ -283,6 +293,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)) {
|
||||
truncate_to_c_api_max(out);
|
||||
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
|
||||
}
|
||||
return ret;
|
||||
@@ -313,6 +324,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) {
|
||||
truncate_to_c_api_max(imei);
|
||||
strlcpy(p_imei, imei.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
|
||||
}
|
||||
return ret;
|
||||
@@ -328,6 +340,7 @@ extern "C" esp_err_t esp_modem_get_operator_name(esp_modem_dce_t *dce_wrap, char
|
||||
auto ret = command_response_to_esp_err(dce_wrap->dce->get_operator_name(name, act));
|
||||
if (ret == ESP_OK) {
|
||||
if (p_name != nullptr) {
|
||||
truncate_to_c_api_max(name);
|
||||
strlcpy(p_name, name.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
|
||||
}
|
||||
if (p_act != nullptr) {
|
||||
@@ -345,6 +358,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) {
|
||||
truncate_to_c_api_max(name);
|
||||
strlcpy(p_name, name.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
|
||||
}
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user