Skip to content

Commit

Permalink
Merge branch 'fix/stack_overflow_in_example_spp_initiator_v5.3' into …
Browse files Browse the repository at this point in the history
…'release/v5.3'

Fix/stack overflow in example spp initiator (backport v5.3)

See merge request espressif/esp-idf!32232
  • Loading branch information
jack0c committed Jul 26, 2024
2 parents b81f351 + 7f0be0c commit d390c6f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 20 deletions.
15 changes: 14 additions & 1 deletion examples/bluetooth/bluedroid/classic_bt/a2dp_sink/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -47,6 +47,17 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
/*******************************
* STATIC FUNCTION DEFINITIONS
******************************/
static char *bda2str(uint8_t * bda, char *str, size_t size)
{
if (bda == NULL || str == NULL || size < 18) {
return NULL;
}

uint8_t *p = bda;
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
p[0], p[1], p[2], p[3], p[4], p[5]);
return str;
}

static void bt_app_dev_cb(esp_bt_dev_cb_event_t event, esp_bt_dev_cb_param_t *param)
{
Expand Down Expand Up @@ -176,6 +187,7 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)

void app_main(void)
{
char bda_str[18] = {0};
/* initialize NVS — it is used to store PHY calibration data */
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
Expand Down Expand Up @@ -230,6 +242,7 @@ void app_main(void)
pin_code[3] = '4';
esp_bt_gap_set_pin(pin_type, 4, pin_code);

ESP_LOGI(BT_AV_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
bt_app_task_start_up();
/* bluetooth device name, connection mode and profile set up */
bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param)

void app_main(void)
{
char bda_str[18] = {0};
/* initialize NVS — it is used to store PHY calibration data */
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
Expand Down Expand Up @@ -788,6 +789,7 @@ void app_main(void)
esp_bt_pin_code_t pin_code;
esp_bt_gap_set_pin(pin_type, 0, pin_code);

ESP_LOGI(BT_AV_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
bt_app_task_start_up();
/* Bluetooth device name, connection mode and profile set up */
bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_STACK_UP_EVT, NULL, 0, NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ static void bt_app_gap_start_up(void)

void app_main(void)
{
char bda_str[18] = {0};
/* Initialize NVS — it is used to store PHY calibration data and save key-value pairs in flash memory*/
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
Expand Down Expand Up @@ -304,5 +305,6 @@ void app_main(void)
return;
}

ESP_LOGI(GAP_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
bt_app_gap_start_up();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -71,6 +71,18 @@ uint8_t hid_mouse_descriptor[] = {
0xc0 // END_COLLECTION
};

static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
{
if (bda == NULL || str == NULL || size < 18) {
return NULL;
}

uint8_t *p = bda;
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
p[0], p[1], p[2], p[3], p[4], p[5]);
return str;
}

const int hid_mouse_descriptor_len = sizeof(hid_mouse_descriptor);

/**
Expand Down Expand Up @@ -158,16 +170,6 @@ void mouse_move_task(void *pvParameters)
}
}

static void print_bt_address(void)
{
const char *TAG = "bt_address";
const uint8_t *bd_addr;

bd_addr = esp_bt_dev_get_address();
ESP_LOGI(TAG, "my bluetooth address is %02X:%02X:%02X:%02X:%02X:%02X",
bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
}

void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
{
const char *TAG = "esp_bt_gap_cb";
Expand Down Expand Up @@ -390,6 +392,7 @@ void app_main(void)
{
const char *TAG = "app_main";
esp_err_t ret;
char bda_str[18] = {0};

ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
Expand Down Expand Up @@ -477,6 +480,6 @@ void app_main(void)
esp_bt_pin_code_t pin_code;
esp_bt_gap_set_pin(pin_type, 0, pin_code);

print_bt_address();
ESP_LOGI(TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
ESP_LOGI(TAG, "exiting");
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/

#include "stdlib.h"
#include "driver/uart.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Expand Down Expand Up @@ -31,7 +32,7 @@ extern void spp_msg_args_parser(char *buf, int len);

void spp_msg_handler(char *buf, int len)
{
ESP_LOGE(TAG_CNSL, "Command [%s]", buf);
ESP_LOGI(TAG_CNSL, "Command [%s]", buf);
spp_msg_args_parser(buf, len);
}

Expand All @@ -44,13 +45,18 @@ static void console_uart_task(void *pvParameters)
spp_msg_parser_register_callback(parser, spp_msg_handler);
spp_msg_show_usage();
#define TMP_BUF_LEN 128
uint8_t tmp_buf[128] = {0};
uint8_t *tmp_buf = NULL;

if ((tmp_buf = (uint8_t *)calloc(TMP_BUF_LEN, sizeof(uint8_t))) == NULL) {
ESP_LOGE(TAG_CNSL,"temp buf malloc fail");
return;
}

for (;;) {
//Waiting for UART event.
if (xQueueReceive(uart_queue, (void * )&event, (TickType_t)portMAX_DELAY)) {
switch (event.type) {
//Event of UART receving data
//Event of UART receiving data
case UART_DATA:
{
len = uart_read_bytes(CONSOLE_UART_NUM, tmp_buf, TMP_BUF_LEN, 0);
Expand Down Expand Up @@ -95,6 +101,8 @@ static void console_uart_task(void *pvParameters)
}
}
}

free(tmp_buf);
vTaskDelete(NULL);
}

Expand Down
16 changes: 15 additions & 1 deletion examples/bluetooth/bluedroid/classic_bt/hfp_ag/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -32,6 +32,18 @@ enum {
BT_APP_EVT_STACK_UP = 0,
};

static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
{
if (bda == NULL || str == NULL || size < 18) {
return NULL;
}

uint8_t *p = bda;
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
p[0], p[1], p[2], p[3], p[4], p[5]);
return str;
}

/* handler for bluetooth stack enabled events */
static void bt_hf_hdl_stack_evt(uint16_t event, void *p_param)
{
Expand Down Expand Up @@ -73,6 +85,7 @@ static void bt_hf_hdl_stack_evt(uint16_t event, void *p_param)

void app_main(void)
{
char bda_str[18] = {0};
/* Initialize NVS — it is used to store PHY calibration data */
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
Expand Down Expand Up @@ -101,6 +114,7 @@ void app_main(void)
return;
}

ESP_LOGI(BT_HF_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
/* create application task */
bt_app_task_start_up();

Expand Down
16 changes: 15 additions & 1 deletion examples/bluetooth/bluedroid/classic_bt/hfp_hf/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -32,6 +32,18 @@ static uint8_t peer_bdname_len;

static const char remote_device_name[] = "ESP_HFP_AG";

static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
{
if (bda == NULL || str == NULL || size < 18) {
return NULL;
}

uint8_t *p = bda;
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
p[0], p[1], p[2], p[3], p[4], p[5]);
return str;
}

static bool get_name_from_eir(uint8_t *eir, char *bdname, uint8_t *bdname_len)
{
uint8_t *rmt_bdname = NULL;
Expand Down Expand Up @@ -151,6 +163,7 @@ static void bt_hf_client_hdl_stack_evt(uint16_t event, void *p_param);

void app_main(void)
{
char bda_str[18] = {0};
/* Initialize NVS — it is used to store PHY calibration data */
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
Expand Down Expand Up @@ -186,6 +199,7 @@ void app_main(void)
return;
}

ESP_LOGI(BT_HF_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
/* create application task */
bt_app_task_start_up();

Expand Down
16 changes: 15 additions & 1 deletion examples/bluetooth/esp_hid_host/main/esp_hid_host_main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -51,6 +51,18 @@

static const char *TAG = "ESP_HIDH_DEMO";

static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
{
if (bda == NULL || str == NULL || size < 18) {
return NULL;
}

uint8_t *p = bda;
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
p[0], p[1], p[2], p[3], p[4], p[5]);
return str;
}

void hidh_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data)
{
esp_hidh_event_t event = (esp_hidh_event_t)id;
Expand Down Expand Up @@ -165,6 +177,7 @@ void ble_store_config_init(void);
#endif
void app_main(void)
{
char bda_str[18] = {0};
esp_err_t ret;
#if HID_HOST_MODE == HIDH_IDLE_MODE
ESP_LOGE(TAG, "Please turn on BT HID host or BLE!");
Expand All @@ -188,6 +201,7 @@ void app_main(void)
};
ESP_ERROR_CHECK( esp_hidh_init(&config) );

ESP_LOGI(TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
#if CONFIG_BT_NIMBLE_ENABLED
/* XXX Need to have template for store */
ble_store_config_init();
Expand Down

0 comments on commit d390c6f

Please sign in to comment.