From 763143a89a161a4fb87de7981a1d310074cd0385 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Thu, 11 Aug 2022 19:41:35 +0800 Subject: [PATCH 1/3] fix esp hid crash when auth failed --- components/esp_hid/src/ble_hidh.c | 2 ++ components/esp_hid/src/esp_hidh.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/components/esp_hid/src/ble_hidh.c b/components/esp_hid/src/ble_hidh.c index 5fe54f2fa7..9563d3a0b7 100644 --- a/components/esp_hid/src/ble_hidh.c +++ b/components/esp_hid/src/ble_hidh.c @@ -112,6 +112,8 @@ static void read_device_services(esp_gatt_if_t gattc_if, esp_hidh_dev_t *dev) ESP_LOGE(TAG, "malloc report maps failed"); return; } + /* read characteristic value may failed, so we should init report maps */ + memset(dev->config.report_maps, 0, dev->config.report_maps_len * sizeof(esp_hid_raw_report_map_t)); for (uint16_t s = 0; s < dcount; s++) { suuid = service_result[s].uuid.uuid.uuid16; diff --git a/components/esp_hid/src/esp_hidh.c b/components/esp_hid/src/esp_hidh.c index 69bd0c5532..128547045f 100644 --- a/components/esp_hid/src/esp_hidh.c +++ b/components/esp_hid/src/esp_hidh.c @@ -370,7 +370,10 @@ static void esp_hidh_dev_resources_free(esp_hidh_dev_t *dev) free((void *)dev->config.manufacturer_name); free((void *)dev->config.serial_number); for (uint8_t d = 0; d < dev->config.report_maps_len; d++) { - free((void *)dev->config.report_maps[d].data); + /* data of report map maybe is NULL */ + if (dev->config.report_maps[d].data) { + free((void *)dev->config.report_maps[d].data); + } } free((void *)dev->config.report_maps); esp_hidh_dev_report_t *r; From c5dc8389ed31c563c8cfcb5d35e2c5fe9dec094f Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Wed, 17 Aug 2022 16:58:54 +0800 Subject: [PATCH 2/3] bluedroid: fix ble rpa generate and update by host --- .../host/bluedroid/stack/btm/btm_ble_addr.c | 20 +------ .../bt/host/bluedroid/stack/btm/btm_ble_gap.c | 53 +++++++++++++++++++ .../bluedroid/stack/btm/include/btm_ble_int.h | 1 + 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c b/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c index 730d7895bc..54742754b4 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c @@ -56,8 +56,9 @@ static void btm_gen_resolve_paddr_cmpl(tSMP_ENC *p) p_cb->private_addr[5] = p->param_buf[0]; p_cb->private_addr[4] = p->param_buf[1]; p_cb->private_addr[3] = p->param_buf[2]; + /* set it to controller */ - btsnd_hcic_ble_set_random_addr(p_cb->private_addr); + btm_ble_set_random_addr(p_cb->private_addr); p_cb->exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RESOLVABLE; memcpy(p_cb->resolvale_addr, p_cb->private_addr, BD_ADDR_LEN); @@ -66,23 +67,6 @@ static void btm_gen_resolve_paddr_cmpl(tSMP_ENC *p) p_cb->set_local_privacy_cback = NULL; } - if (btm_cb.ble_ctr_cb.inq_var.adv_mode == BTM_BLE_ADV_ENABLE){ - BTM_TRACE_DEBUG("Advertise with new resolvable private address, now."); - /** - * Restart advertising, using new resolvable private address - */ - btm_ble_stop_adv(); - btm_ble_start_adv(); - } - if (btm_cb.ble_ctr_cb.inq_var.state == BTM_BLE_SCANNING){ - BTM_TRACE_DEBUG("Scan with new resolvable private address, now."); - /** - * Restart scaning, using new resolvable private address - */ - btm_ble_stop_scan(); - btm_ble_start_scan(); - } - /* start a periodical timer to refresh random addr */ btu_stop_timer_oneshot(&p_cb->raddr_timer_ent); #if (BTM_BLE_CONFORMANCE_TESTING == TRUE) diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c index 6ab129e572..1ff0a356ce 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c @@ -4023,6 +4023,59 @@ tBTM_STATUS btm_ble_stop_adv(void) return rt; } +tBTM_STATUS btm_ble_set_random_addr(BD_ADDR random_bda) +{ + tBTM_STATUS rt = BTM_SUCCESS; + + osi_mutex_lock(&adv_enable_lock, OSI_MUTEX_MAX_TIMEOUT); + osi_mutex_lock(&scan_enable_lock, OSI_MUTEX_MAX_TIMEOUT); + + if (btm_cb.ble_ctr_cb.inq_var.adv_mode == BTM_BLE_ADV_ENABLE) { + if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE)) { + osi_sem_take(&adv_enable_sem, OSI_SEM_MAX_TIMEOUT); + rt = adv_enable_status; + } else { + rt = BTM_BAD_VALUE_RET; + } + } + + if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) { + if (btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_DISABLE, BTM_BLE_SCAN_DUPLICATE_DISABLE)) { + osi_sem_take(&scan_enable_sem, OSI_SEM_MAX_TIMEOUT); + rt = scan_enable_status; + } else { + rt = BTM_BAD_VALUE_RET; + } + } + + if (rt == BTM_SUCCESS) { + btsnd_hcic_ble_set_random_addr(random_bda); + } + + if (btm_cb.ble_ctr_cb.inq_var.adv_mode == BTM_BLE_ADV_ENABLE) { + if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE)) { + osi_sem_take(&adv_enable_sem, OSI_SEM_MAX_TIMEOUT); + rt = adv_enable_status; + } else { + rt = BTM_BAD_VALUE_RET; + } + } + + if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) { + if (btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_ENABLE, btm_cb.ble_ctr_cb.inq_var.scan_duplicate_filter)) { + osi_sem_take(&scan_enable_sem, OSI_SEM_MAX_TIMEOUT); + rt = scan_enable_status; + } else { + rt = BTM_BAD_VALUE_RET; + } + } + + osi_mutex_unlock(&adv_enable_lock); + osi_mutex_unlock(&scan_enable_lock); + + return rt; +} + /******************************************************************************* ** diff --git a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h index aea722448a..bbc532de47 100644 --- a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h +++ b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h @@ -404,6 +404,7 @@ tBTM_STATUS btm_ble_stop_adv(void); tBTM_STATUS btm_ble_start_scan(void); void btm_ble_create_ll_conn_complete (UINT8 status); void btm_ble_create_conn_cancel_complete (UINT8 *p); +tBTM_STATUS btm_ble_set_random_addr(BD_ADDR random_bda); /* LE security function from btm_sec.c */ #if SMP_INCLUDED == TRUE From c912fcba45ae651a68789f3da98ed3a7d2d7ef7b Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Mon, 19 Sep 2022 11:23:05 +0800 Subject: [PATCH 3/3] fix ble adv tx power map --- .../host/bluedroid/common/include/common/bt_target.h | 12 ++++++++++++ .../host/bluedroid/stack/include/stack/btm_ble_api.h | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index 1891981f39..2675f76f5e 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -1126,15 +1126,27 @@ #endif #ifndef BTM_BLE_ADV_TX_POWER +#ifdef CONFIG_IDF_TARGET_ESP32 #define BTM_BLE_ADV_TX_POWER {-12, -9, -6, -3, 0, 3, 6, 9} +#else +#define BTM_BLE_ADV_TX_POWER {-24, -21, -18, -15, -12, -9, -6, -3, 0, 3, 6, 9, 12, 15, 18, 21} +#endif #endif #ifndef BTM_TX_POWER +#ifdef CONFIG_IDF_TARGET_ESP32 #define BTM_TX_POWER {-12, -9, -6, -3, 0, 3, 6, 9} +#else +#define BTM_TX_POWER {-24, -21, -18, -15, -12, -9, -6, -3, 0, 3, 6, 9, 12, 15, 18, 21} +#endif #endif #ifndef BTM_TX_POWER_LEVEL_MAX +#ifdef CONFIG_IDF_TARGET_ESP32 #define BTM_TX_POWER_LEVEL_MAX 7 +#else +#define BTM_TX_POWER_LEVEL_MAX 15 +#endif #endif diff --git a/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h b/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h index 45f6e2e510..1fbbbcab12 100644 --- a/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h @@ -377,7 +377,7 @@ typedef UINT8 tBTM_BLE_AD_TYPE; /* adv tx power level */ #define BTM_BLE_ADV_TX_POWER_MIN 0 /* minimum tx power */ -#define BTM_BLE_ADV_TX_POWER_MAX 7 /* maximum tx power */ +#define BTM_BLE_ADV_TX_POWER_MAX BTM_TX_POWER_LEVEL_MAX /* maximum tx power */ typedef UINT8 tBTM_BLE_ADV_TX_POWER; /* adv tx power in dBm */