From f161c867c70f1cfc38325d57c0f15265a5932b41 Mon Sep 17 00:00:00 2001 From: Jin Cheng Date: Mon, 31 Oct 2022 10:22:40 +0800 Subject: [PATCH 1/2] fixed build errors with Secure Simple Paring disabled --- examples/bluetooth/esp_hid_device/main/esp_hid_gap.c | 3 +++ examples/bluetooth/esp_hid_host/main/esp_hid_gap.c | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/examples/bluetooth/esp_hid_device/main/esp_hid_gap.c b/examples/bluetooth/esp_hid_device/main/esp_hid_gap.c index 2c3ff904d5..ecf52360a2 100644 --- a/examples/bluetooth/esp_hid_device/main/esp_hid_gap.c +++ b/examples/bluetooth/esp_hid_device/main/esp_hid_gap.c @@ -403,9 +403,12 @@ static void bt_gap_event_handler(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_para static esp_err_t init_bt_gap(void) { esp_err_t ret; +#if (CONFIG_BT_SSP_ENABLED) + /* Set default parameters for Secure Simple Pairing */ esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE; esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO; esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t)); +#endif /* * Set default parameters for Legacy Pairing * Use fixed pin code diff --git a/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c b/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c index 2c3ff904d5..ee160ee269 100644 --- a/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c +++ b/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c @@ -391,9 +391,16 @@ static void bt_gap_event_handler(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_para handle_bt_device_result(¶m->disc_res); break; } +#if (CONFIG_BT_SSP_ENABLED) case ESP_BT_GAP_KEY_NOTIF_EVT: ESP_LOGI(TAG, "BT GAP KEY_NOTIF passkey:%d", param->key_notif.passkey); break; + case ESP_BT_GAP_CFM_REQ_EVT: { + ESP_LOGI(TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val); + esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true); + break; + } +#endif default: ESP_LOGV(TAG, "BT GAP EVENT %s", bt_gap_evt_str(event)); break; @@ -403,9 +410,12 @@ static void bt_gap_event_handler(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_para static esp_err_t init_bt_gap(void) { esp_err_t ret; +#if (CONFIG_BT_SSP_ENABLED) + /* Set default parameters for Secure Simple Pairing */ esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE; esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO; esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t)); +#endif /* * Set default parameters for Legacy Pairing * Use fixed pin code From 0f3c7470d7f67c6a80cc5c0caff2bf7eb8877146 Mon Sep 17 00:00:00 2001 From: Jin Cheng Date: Mon, 31 Oct 2022 10:36:20 +0800 Subject: [PATCH 2/2] 1. changed pin_type of Legacy Paring from FIXED to VARIABLE to avoid authentication failure 2. provide compatibilities with devices that do not support Secure Simple Paring Closes https://github.com/espressif/esp-idf/issues/10069 Closes https://github.com/espressif/esp-idf/issues/10005 --- .../bluetooth/esp_hid_host/main/esp_hid_gap.c | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c b/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c index ee160ee269..70aabb1305 100644 --- a/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c +++ b/examples/bluetooth/esp_hid_host/main/esp_hid_gap.c @@ -401,8 +401,25 @@ static void bt_gap_event_handler(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_para break; } #endif + case ESP_BT_GAP_PIN_REQ_EVT: { + ESP_LOGI(TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit); + if (param->pin_req.min_16_digit) { + ESP_LOGI(TAG, "Input pin code: 0000 0000 0000 0000"); + esp_bt_pin_code_t pin_code = {0}; + esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code); + } else { + ESP_LOGI(TAG, "Input pin code: 1234"); + esp_bt_pin_code_t pin_code; + pin_code[0] = '1'; + pin_code[1] = '2'; + pin_code[2] = '3'; + pin_code[3] = '4'; + esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code); + } + break; + } default: - ESP_LOGV(TAG, "BT GAP EVENT %s", bt_gap_evt_str(event)); + ESP_LOGW(TAG, "BT GAP EVENT %s", bt_gap_evt_str(event)); break; } } @@ -418,15 +435,11 @@ static esp_err_t init_bt_gap(void) #endif /* * Set default parameters for Legacy Pairing - * Use fixed pin code + * Use variable pin, input pin code when pairing */ - esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED; + esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE; esp_bt_pin_code_t pin_code; - pin_code[0] = '1'; - pin_code[1] = '2'; - pin_code[2] = '3'; - pin_code[3] = '4'; - esp_bt_gap_set_pin(pin_type, 4, pin_code); + esp_bt_gap_set_pin(pin_type, 0, pin_code); if ((ret = esp_bt_gap_register_callback(bt_gap_event_handler)) != ESP_OK) { ESP_LOGE(TAG, "esp_bt_gap_register_callback failed: %d", ret);