mirror of
https://github.com/espressif/esp-idf.git
synced 2026-08-18 06:35:35 +03:00
Merge branch 'feat/show_how_to_use_smp_in_blufi_example' into 'master'
feat(ble/blufi): Support setting BLE encryption for blufi Closes BLERP-2232 and DOC-13032 See merge request espressif/esp-idf!41779
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "esp_bt_device.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_blufi.h"
|
||||
#include <esp_gap_ble_api.h>
|
||||
|
||||
#if (BLUFI_INCLUDED == TRUE)
|
||||
|
||||
@@ -70,12 +71,155 @@ static esp_ble_adv_params_t blufi_adv_params = {
|
||||
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
||||
};
|
||||
|
||||
#ifdef CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
static char *esp_auth_req_to_str(esp_ble_auth_req_t auth_req)
|
||||
{
|
||||
char *auth_str = NULL;
|
||||
switch(auth_req) {
|
||||
case ESP_LE_AUTH_NO_BOND:
|
||||
auth_str = "ESP_LE_AUTH_NO_BOND";
|
||||
break;
|
||||
case ESP_LE_AUTH_BOND:
|
||||
auth_str = "ESP_LE_AUTH_BOND";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_MITM:
|
||||
auth_str = "ESP_LE_AUTH_REQ_MITM";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_BOND_MITM:
|
||||
auth_str = "ESP_LE_AUTH_REQ_BOND_MITM";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_ONLY:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_ONLY";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_BOND:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_BOND";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_MITM:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_MITM";
|
||||
break;
|
||||
case ESP_LE_AUTH_REQ_SC_MITM_BOND:
|
||||
auth_str = "ESP_LE_AUTH_REQ_SC_MITM_BOND";
|
||||
break;
|
||||
default:
|
||||
auth_str = "INVALID BLE AUTH REQ";
|
||||
break;
|
||||
}
|
||||
|
||||
return auth_str;
|
||||
}
|
||||
|
||||
static char *esp_key_type_to_str(esp_ble_key_type_t key_type)
|
||||
{
|
||||
char *key_str = NULL;
|
||||
switch(key_type) {
|
||||
case ESP_LE_KEY_NONE:
|
||||
key_str = "ESP_LE_KEY_NONE";
|
||||
break;
|
||||
case ESP_LE_KEY_PENC:
|
||||
key_str = "ESP_LE_KEY_PENC";
|
||||
break;
|
||||
case ESP_LE_KEY_PID:
|
||||
key_str = "ESP_LE_KEY_PID";
|
||||
break;
|
||||
case ESP_LE_KEY_PCSRK:
|
||||
key_str = "ESP_LE_KEY_PCSRK";
|
||||
break;
|
||||
case ESP_LE_KEY_PLK:
|
||||
key_str = "ESP_LE_KEY_PLK";
|
||||
break;
|
||||
case ESP_LE_KEY_LLK:
|
||||
key_str = "ESP_LE_KEY_LLK";
|
||||
break;
|
||||
case ESP_LE_KEY_LENC:
|
||||
key_str = "ESP_LE_KEY_LENC";
|
||||
break;
|
||||
case ESP_LE_KEY_LID:
|
||||
key_str = "ESP_LE_KEY_LID";
|
||||
break;
|
||||
case ESP_LE_KEY_LCSRK:
|
||||
key_str = "ESP_LE_KEY_LCSRK";
|
||||
break;
|
||||
default:
|
||||
key_str = "INVALID BLE KEY TYPE";
|
||||
break;
|
||||
}
|
||||
return key_str;
|
||||
}
|
||||
#endif
|
||||
|
||||
void esp_blufi_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
BLUFI_TRACE_DEBUG("GAP_EVT, event %d", event);
|
||||
switch (event) {
|
||||
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
|
||||
esp_ble_gap_start_advertising(&blufi_adv_params);
|
||||
break;
|
||||
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
|
||||
//advertising start complete event to indicate advertising start successfully or failed
|
||||
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
|
||||
BTC_TRACE_ERROR("Advertising start failed, status %x", param->adv_start_cmpl.status);
|
||||
break;
|
||||
}
|
||||
BLUFI_TRACE_API("Advertising start successfully");
|
||||
break;
|
||||
#ifdef CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
case ESP_GAP_BLE_PASSKEY_REQ_EVT: /* passkey request event */
|
||||
BLUFI_TRACE_API("Passkey request");
|
||||
break;
|
||||
case ESP_GAP_BLE_OOB_REQ_EVT: {
|
||||
BLUFI_TRACE_API("OOB request");
|
||||
uint8_t tk[16] = {1}; //If you paired with OOB, both devices need to use the same tk
|
||||
esp_ble_oob_req_reply(param->ble_security.ble_req.bd_addr, tk, sizeof(tk));
|
||||
break;
|
||||
}
|
||||
case ESP_GAP_BLE_LOCAL_IR_EVT: /* BLE local IR event */
|
||||
BLUFI_TRACE_API("Local identity root");
|
||||
break;
|
||||
case ESP_GAP_BLE_LOCAL_ER_EVT: /* BLE local ER event */
|
||||
BLUFI_TRACE_API("Local encryption root");
|
||||
break;
|
||||
case ESP_GAP_BLE_NC_REQ_EVT:
|
||||
/* The app will receive this evt when the IO has DisplayYesNO capability and the peer device IO also has DisplayYesNo capability.
|
||||
show the passkey number to the user to confirm it with the number displayed by peer device. */
|
||||
esp_ble_confirm_reply(param->ble_security.ble_req.bd_addr, true);
|
||||
BLUFI_TRACE_WARNING("Numeric Comparison request, passkey %" PRIu32, param->ble_security.key_notif.passkey);
|
||||
break;
|
||||
case ESP_GAP_BLE_SEC_REQ_EVT:
|
||||
/* send the positive(true) security response to the peer device to accept the security request.
|
||||
If not accept the security request, should send the security response with negative(false) accept value*/
|
||||
esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
|
||||
break;
|
||||
case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: ///the app will receive this evt when the IO has Output capability and the peer device IO has Input capability.
|
||||
///show the passkey number to the user to input it in the peer device.
|
||||
BLUFI_TRACE_WARNING("Passkey notify, passkey %06" PRIu32, param->ble_security.key_notif.passkey);
|
||||
break;
|
||||
case ESP_GAP_BLE_KEY_EVT:
|
||||
//shows the ble key info share with peer device to the user.
|
||||
BLUFI_TRACE_API("Key exchanged, key_type %s", esp_key_type_to_str(param->ble_security.ble_key.key_type));
|
||||
break;
|
||||
case ESP_GAP_BLE_AUTH_CMPL_EVT: {
|
||||
esp_bd_addr_t bd_addr;
|
||||
memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t));
|
||||
BLUFI_TRACE_API("Authentication complete, addr_type %u, addr "ESP_BD_ADDR_STR"",
|
||||
param->ble_security.auth_cmpl.addr_type, ESP_BD_ADDR_HEX(bd_addr));
|
||||
if(!param->ble_security.auth_cmpl.success) {
|
||||
BLUFI_TRACE_WARNING("Pairing failed, reason 0x%x",param->ble_security.auth_cmpl.fail_reason);
|
||||
} else {
|
||||
BLUFI_TRACE_WARNING("Pairing successfully, auth_mode %s",esp_auth_req_to_str(param->ble_security.auth_cmpl.auth_mode));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT: {
|
||||
BLUFI_TRACE_DEBUG("Bond device remove, status %d, device "ESP_BD_ADDR_STR"",
|
||||
param->remove_bond_dev_cmpl.status, ESP_BD_ADDR_HEX(param->remove_bond_dev_cmpl.bd_addr));
|
||||
break;
|
||||
}
|
||||
case ESP_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT:
|
||||
if (param->local_privacy_cmpl.status != ESP_BT_STATUS_SUCCESS){
|
||||
BLUFI_TRACE_WARNING("Local privacy config failed, status %x", param->local_privacy_cmpl.status);
|
||||
}
|
||||
break;
|
||||
#endif // CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -251,10 +395,16 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
|
||||
break;
|
||||
case BTA_GATTS_CREATE_EVT:
|
||||
blufi_env.handle_srvc = p_data->create.service_id;
|
||||
|
||||
#if CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
BLUFI_TRACE_WARNING("BLE SMP support in BLUFI is ENABLED!");
|
||||
#endif // CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
//add the first blufi characteristic --> write characteristic
|
||||
BTA_GATTS_AddCharacteristic(blufi_env.handle_srvc, &blufi_char_uuid_p2e,
|
||||
(GATT_PERM_WRITE),
|
||||
#if CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
GATT_PERM_WRITE_ENC_MITM,
|
||||
#else
|
||||
GATT_PERM_WRITE,
|
||||
#endif
|
||||
(GATT_CHAR_PROP_BIT_WRITE),
|
||||
NULL, NULL);
|
||||
break;
|
||||
@@ -398,6 +548,16 @@ void esp_blufi_adv_stop(void)
|
||||
esp_ble_gap_stop_advertising();
|
||||
}
|
||||
|
||||
|
||||
esp_err_t esp_blufi_start_security_request(esp_blufi_bd_addr_t remote_bda)
|
||||
{
|
||||
#ifdef CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
return esp_ble_set_encryption(remote_bda, ESP_BLE_SEC_ENCRYPT_MITM);
|
||||
#else
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
#endif // CONFIG_BT_BLUFI_BLE_SMP_ENABLE
|
||||
}
|
||||
|
||||
void esp_blufi_send_encap(void *arg)
|
||||
{
|
||||
struct blufi_hdr *hdr = (struct blufi_hdr *)arg;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -90,6 +90,22 @@ void esp_blufi_adv_start_with_name(const char *name);
|
||||
|
||||
void esp_blufi_send_encap(void *arg);
|
||||
|
||||
/*
|
||||
* @brief Initiate BLE security request with the connected peer device.
|
||||
*
|
||||
* This function triggers the BLE Security Manager Protocol (SMP) procedure
|
||||
* to establish a secure, encrypted connection with the specified remote device.
|
||||
* It should be called after a BLE connection is established.
|
||||
*
|
||||
* @param[in] remote_bda Bluetooth device address of the connected peer.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Security request initiated successfully
|
||||
* - ESP_FAIL: Security request failed
|
||||
* - ESP_ERR_INVALID_STATE: BluFi BLE SMP is not enabled
|
||||
*/
|
||||
esp_err_t esp_blufi_start_security_request(esp_blufi_bd_addr_t remote_bda);
|
||||
|
||||
#ifdef CONFIG_BT_NIMBLE_ENABLED
|
||||
/**
|
||||
* @brief Handle gap event for BluFi.
|
||||
|
||||
@@ -291,6 +291,16 @@ config BT_BLE_BLUFI_ENABLE
|
||||
help
|
||||
This option can be close when the app does not require blufi function.
|
||||
|
||||
config BT_BLUFI_BLE_SMP_ENABLE
|
||||
bool "Enable BLE SMP support for BluFi"
|
||||
depends on BT_BLE_BLUFI_ENABLE && BT_BLE_SMP_ENABLE
|
||||
default n
|
||||
help
|
||||
Enable BLE Security Manager Protocol (SMP) for BluFi.
|
||||
When enabled, BluFi will support BLE pairing and encryption
|
||||
before Wi-Fi provisioning, providing a more secure provisioning process.
|
||||
This feature is only supported with the Bluedroid host.
|
||||
|
||||
config BT_GATT_MAX_SR_PROFILES
|
||||
int "Max GATT Server Profiles"
|
||||
depends on BT_GATTS_ENABLE && BT_BLUEDROID_ENABLED
|
||||
|
||||
Reference in New Issue
Block a user