From 241ba1a6bbb2f817e7cccbd375d0b1a4541739f9 Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Thu, 28 Feb 2019 20:39:12 +0800 Subject: [PATCH] Component/bt: fix iOS and win10 compatibility error for HID --- components/bt/Kconfig | 7 ++++++ .../common/include/common/bt_target.h | 10 ++++++++ components/bt/bluedroid/stack/btm/btm_ble.c | 3 ++- .../bt/bluedroid/stack/btm/include/btm_int.h | 2 ++ components/bt/bluedroid/stack/smp/smp_act.c | 25 ++++++++++++------- components/bt/bluedroid/stack/smp/smp_utils.c | 2 ++ .../main/ble_hidd_demo_main.c | 13 +++++++++- .../main/hid_device_le_prf.c | 19 +++++++++++--- .../main/hidd_le_prf_int.h | 5 +++- 9 files changed, 70 insertions(+), 16 deletions(-) diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 53967ddf59..860c4e1d0c 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -434,6 +434,13 @@ config BLE_SMP_ENABLE help This option can be close when the app not used the ble security connect. +config SMP_SLAVE_CON_PARAMS_UPD_ENABLE + bool "Slave enable connection parameters update during pairing" + depends on BLE_SMP_ENABLE + default n + help + In order to reduce the pairing time, slave actively initiates connection parameters update during pairing. + config BT_STACK_NO_LOG bool "Disable BT debug logs (minimize bin size)" depends on BLUEDROID_ENABLED diff --git a/components/bt/bluedroid/common/include/common/bt_target.h b/components/bt/bluedroid/common/include/common/bt_target.h index 02e00088ef..411c69cb4b 100644 --- a/components/bt/bluedroid/common/include/common/bt_target.h +++ b/components/bt/bluedroid/common/include/common/bt_target.h @@ -130,6 +130,16 @@ #define BLE_PRIVACY_SPT FALSE #endif /* CONFIG_SMP_ENABLE */ +#ifdef CONFIG_SMP_SLAVE_CON_PARAMS_UPD_ENABLE +#if(CONFIG_SMP_SLAVE_CON_PARAMS_UPD_ENABLE) +#define SMP_SLAVE_CON_PARAMS_UPD_ENABLE TRUE +#else +#define SMP_SLAVE_CON_PARAMS_UPD_ENABLE FALSE +#endif +#else +#define SMP_SLAVE_CON_PARAMS_UPD_ENABLE FALSE +#endif + #ifndef CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED #define BLE_ADV_REPORT_FLOW_CONTROL FALSE #else diff --git a/components/bt/bluedroid/stack/btm/btm_ble.c b/components/bt/bluedroid/stack/btm/btm_ble.c index 0a5ad832c9..5e4c9ee71c 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble.c +++ b/components/bt/bluedroid/stack/btm/btm_ble.c @@ -1468,13 +1468,14 @@ tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 lin break; } } - +#if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE) // already have encrypted information, do not need to update connection parameters if(link_role == BTM_ROLE_SLAVE && (p_rec->ble.key_type & BTM_LE_KEY_PENC)) { p_rec->ble.skip_update_conn_param = true; } else { p_rec->ble.skip_update_conn_param = false; } +#endif if (SMP_Pair(bd_addr) == SMP_STARTED) { cmd = BTM_CMD_STARTED; p_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING; diff --git a/components/bt/bluedroid/stack/btm/include/btm_int.h b/components/bt/bluedroid/stack/btm/include/btm_int.h index dcf6ecfc0e..a79423c712 100644 --- a/components/bt/bluedroid/stack/btm/include/btm_int.h +++ b/components/bt/bluedroid/stack/btm/include/btm_int.h @@ -488,7 +488,9 @@ typedef struct { #if SMP_INCLUDED == TRUE tBTM_LE_KEY_TYPE key_type; /* bit mask of valid key types in record */ tBTM_SEC_BLE_KEYS keys; /* LE device security info in slave rode */ +#if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE) bool skip_update_conn_param; /* skip update connection paraams or not*/ +#endif UINT16 auth_mode; /* Authentication mode */ #endif #if (BLE_PRIVACY_SPT == TRUE) diff --git a/components/bt/bluedroid/stack/smp/smp_act.c b/components/bt/bluedroid/stack/smp/smp_act.c index 63b9cb10e5..dcd3495f25 100644 --- a/components/bt/bluedroid/stack/smp/smp_act.c +++ b/components/bt/bluedroid/stack/smp/smp_act.c @@ -1478,16 +1478,23 @@ void smp_idle_terminate(tSMP_CB *p_cb, tSMP_INT_DATA *p_data) *******************************************************************************/ void smp_fast_conn_param(tSMP_CB *p_cb, tSMP_INT_DATA *p_data) { - tBTM_SEC_DEV_REC *p_rec = btm_find_dev (p_cb->pairing_bda); - if(p_rec && p_rec->ble.skip_update_conn_param) { - //do nothing - return; + if(p_cb->role == BTM_ROLE_MASTER) { + L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE); + } +#if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE) + else { + tBTM_SEC_DEV_REC *p_rec = btm_find_dev (p_cb->pairing_bda); + if(p_rec && p_rec->ble.skip_update_conn_param) { + //do nothing + return; + } + /* Disable L2CAP connection parameter updates while bonding since + some peripherals are not able to revert to fast connection parameters + during the start of service discovery. Connection paramter updates + get enabled again once service discovery completes. */ + L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE); } - /* Disable L2CAP connection parameter updates while bonding since - some peripherals are not able to revert to fast connection parameters - during the start of service discovery. Connection paramter updates - get enabled again once service discovery completes. */ - L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE); +#endif } /******************************************************************************* diff --git a/components/bt/bluedroid/stack/smp/smp_utils.c b/components/bt/bluedroid/stack/smp/smp_utils.c index 79a8b1dd59..19c2cde99b 100644 --- a/components/bt/bluedroid/stack/smp/smp_utils.c +++ b/components/bt/bluedroid/stack/smp/smp_utils.c @@ -996,6 +996,7 @@ void smp_proc_pairing_cmpl(tSMP_CB *p_cb) memcpy (pairing_bda, p_cb->pairing_bda, BD_ADDR_LEN); +#if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE) if (p_cb->role == HCI_ROLE_SLAVE) { if(p_rec && p_rec->ble.skip_update_conn_param) { //clear flag @@ -1004,6 +1005,7 @@ void smp_proc_pairing_cmpl(tSMP_CB *p_cb) L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, TRUE); } } +#endif smp_reset_control_value(p_cb); if (p_callback) { diff --git a/examples/bluetooth/ble_hid_device_demo/main/ble_hidd_demo_main.c b/examples/bluetooth/ble_hid_device_demo/main/ble_hidd_demo_main.c index c37b5d4416..7b39f51d0f 100644 --- a/examples/bluetooth/ble_hid_device_demo/main/ble_hidd_demo_main.c +++ b/examples/bluetooth/ble_hid_device_demo/main/ble_hidd_demo_main.c @@ -44,6 +44,17 @@ * */ +/** + * Note: + * 1. Win10 does not support vendor report , So SUPPORT_REPORT_VENDOR is always set to FALSE, it defines in hidd_le_prf_int.h + * 2. Update connection parameters are not allowed during iPhone HID encryption, slave turns + * off the ability to automatically update connection parameters during encryption. + * 3. After our HID device is connected, the iPhones write 1 to the Report Characteristic Configuration Descriptor, + * even if the HID encryption is not completed. This should actually be written 1 after the HID encryption is completed. + * we modify the permissions of the Report Characteristic Configuration Descriptor to `ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE_ENCRYPTED`. + * if you got `GATT_INSUF_ENCRYPTION` error, please ignore. + */ + #define HID_DEMO_TAG "HID_DEMO" @@ -324,7 +335,7 @@ void app_main() //init the gpio pin gpio_demo_init(); - xTaskCreate(&hid_demo_task, "hid_task", 2048, NULL, 5, NULL); + xTaskCreate(&hid_demo_task, "hid_task", 2048, NULL, 5, NULL); } diff --git a/examples/bluetooth/ble_hid_device_demo/main/hid_device_le_prf.c b/examples/bluetooth/ble_hid_device_demo/main/hid_device_le_prf.c index 8d112fad87..8041e7b5e6 100644 --- a/examples/bluetooth/ble_hid_device_demo/main/hid_device_le_prf.c +++ b/examples/bluetooth/ble_hid_device_demo/main/hid_device_le_prf.c @@ -168,7 +168,9 @@ static const uint8_t hidReportMap[] = { 0x81, 0x00, // Input (Data, Ary, Abs) 0xC0, // End Collection 0x81, 0x03, // Input (Const, Var, Abs) - 0xC0, // End Collection + 0xC0, // End Collectionq + +#if (SUPPORT_REPORT_VENDOR == true) 0x06, 0xFF, 0xFF, // Usage Page(Vendor defined) 0x09, 0xA5, // Usage(Vendor Defined) 0xA1, 0x01, // Collection(Application) @@ -179,6 +181,8 @@ static const uint8_t hidReportMap[] = { 0x95, 0x7F, // Report Count = 127 Btyes 0x91, 0x02, // Output(Data, Variable, Absolute) 0xC0, // End Collection +#endif + }; /// Battery Service Attributes Indexes @@ -239,8 +243,11 @@ static uint8_t hidReportRefKeyIn[HID_REPORT_REF_LEN] = static uint8_t hidReportRefLedOut[HID_REPORT_REF_LEN] = { HID_RPT_ID_LED_OUT, HID_REPORT_TYPE_OUTPUT }; +#if (SUPPORT_REPORT_VENDOR == true) + static uint8_t hidReportRefVendorOut[HID_REPORT_REF_LEN] = {HID_RPT_ID_VENDOR_OUT, HID_REPORT_TYPE_OUTPUT}; +#endif // HID Report Reference characteristic descriptor, Feature static uint8_t hidReportRefFeature[HID_REPORT_REF_LEN] = @@ -436,6 +443,7 @@ static esp_gatts_attr_db_t hidd_le_gatt_db[HIDD_LE_IDX_NB] = ESP_GATT_PERM_READ, sizeof(hidReportRefLedOut), sizeof(hidReportRefLedOut), hidReportRefLedOut}}, +#if (SUPPORT_REPORT_VENDOR == true) // Report Characteristic Declaration [HIDD_LE_IDX_REPORT_VENDOR_OUT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, @@ -449,8 +457,8 @@ static esp_gatts_attr_db_t hidd_le_gatt_db[HIDD_LE_IDX_NB] = ESP_GATT_PERM_READ, sizeof(hidReportRefVendorOut), sizeof(hidReportRefVendorOut), hidReportRefVendorOut}}, - - // Report Characteristic Declaration +#endif + // Report Characteristic Declaration [HIDD_LE_IDX_REPORT_CC_IN_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, @@ -462,7 +470,7 @@ static esp_gatts_attr_db_t hidd_le_gatt_db[HIDD_LE_IDX_NB] = NULL}}, // Report KEY INPUT Characteristic - Client Characteristic Configuration Descriptor [HIDD_LE_IDX_REPORT_CC_IN_CCC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, - (ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE), + (ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE_ENCRYPTED), sizeof(uint16_t), 0, NULL}}, // Report Characteristic - Report Reference Descriptor @@ -538,6 +546,7 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, { switch(event) { case ESP_GATTS_REG_EVT: { + esp_ble_gap_config_local_icon (ESP_BLE_APPEARANCE_GENERIC_HID); esp_hidd_cb_param_t hidd_param; hidd_param.init_finish.state = param->reg.status; if(param->reg.app_id == HIDD_APP_ID) { @@ -584,6 +593,7 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, case ESP_GATTS_CLOSE_EVT: break; case ESP_GATTS_WRITE_EVT: { +#if (SUPPORT_REPORT_VENDOR == true) esp_hidd_cb_param_t cb_param = {0}; if (param->write.handle == hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_VENDOR_OUT_VAL] && hidd_le_env.hidd_cb != NULL) { @@ -593,6 +603,7 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, cb_param.vendor_write.data = param->write.value; (hidd_le_env.hidd_cb)(ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT, &cb_param); } +#endif break; } case ESP_GATTS_CREAT_ATTR_TAB_EVT: { diff --git a/examples/bluetooth/ble_hid_device_demo/main/hidd_le_prf_int.h b/examples/bluetooth/ble_hid_device_demo/main/hidd_le_prf_int.h index 4482be55ae..89e7cc20df 100644 --- a/examples/bluetooth/ble_hid_device_demo/main/hidd_le_prf_int.h +++ b/examples/bluetooth/ble_hid_device_demo/main/hidd_le_prf_int.h @@ -16,12 +16,14 @@ #ifndef __HID_DEVICE_LE_PRF__ #define __HID_DEVICE_LE_PRF__ +#include #include "esp_gatts_api.h" #include "esp_gatt_defs.h" #include "esp_hidd_prf_api.h" #include "esp_gap_ble_api.h" #include "hid_dev.h" +#define SUPPORT_REPORT_VENDOR false //HID BLE profile log tag #define HID_LE_PRF_TAG "HID_LE_PRF" @@ -141,11 +143,12 @@ enum { HIDD_LE_IDX_REPORT_LED_OUT_VAL, HIDD_LE_IDX_REPORT_LED_OUT_REP_REF, +#if (SUPPORT_REPORT_VENDOR == true) /// Report Vendor HIDD_LE_IDX_REPORT_VENDOR_OUT_CHAR, HIDD_LE_IDX_REPORT_VENDOR_OUT_VAL, HIDD_LE_IDX_REPORT_VENDOR_OUT_REP_REF, - +#endif HIDD_LE_IDX_REPORT_CC_IN_CHAR, HIDD_LE_IDX_REPORT_CC_IN_VAL, HIDD_LE_IDX_REPORT_CC_IN_CCC,