feat(bt/bluedroid): Add new version of API for Bluedroid host stack initialization

This commit is contained in:
liqigan
2023-08-29 16:49:49 +08:00
committed by BOT
parent d9da8f1953
commit 0df585dc35
81 changed files with 756 additions and 370 deletions

View File

@@ -108,7 +108,8 @@ if(CONFIG_BT_ENABLED)
host/bluedroid/stack/a2dp/include
host/bluedroid/stack/rfcomm/include
host/bluedroid/stack/include
host/bluedroid/common/include)
host/bluedroid/common/include
host/bluedroid/config/include)
list(APPEND include_dirs host/bluedroid/api/include/api)
@@ -364,7 +365,8 @@ if(CONFIG_BT_ENABLED)
"host/bluedroid/stack/smp/smp_keys.c"
"host/bluedroid/stack/smp/smp_l2c.c"
"host/bluedroid/stack/smp/smp_main.c"
"host/bluedroid/stack/smp/smp_utils.c")
"host/bluedroid/stack/smp/smp_utils.c"
"host/bluedroid/config/stack_config.c")
list(APPEND srcs "common/btc/profile/esp/blufi/bluedroid_host/esp_blufi.c")

View File

@@ -135,14 +135,6 @@ choice BT_HID_ROLE
This enables the BT HID Device
endchoice
config BT_SSP_ENABLED
bool "Secure Simple Pairing"
depends on BT_CLASSIC_ENABLED
default y
help
This enables the Secure Simple Pairing. If disable this option,
Bluedroid will only support Legacy Pairing
config BT_BLE_ENABLED
bool "Bluetooth Low Energy"
depends on BT_BLUEDROID_ENABLED

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -11,6 +11,7 @@
#include "esp_bt.h"
#include "osi/future.h"
#include "osi/allocator.h"
#include "config/stack_config.h"
static bool bd_already_enable = false;
static bool bd_already_init = false;
@@ -106,11 +107,22 @@ esp_err_t esp_bluedroid_disable(void)
}
esp_err_t esp_bluedroid_init(void)
{
esp_bluedroid_config_t cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();
return esp_bluedroid_init_with_cfg(&cfg);
}
esp_err_t esp_bluedroid_init_with_cfg(esp_bluedroid_config_t *cfg)
{
btc_msg_t msg;
future_t **future_p;
bt_status_t ret;
if (!cfg) {
LOG_ERROR("%s cfg is NULL", __func__);
return ESP_ERR_INVALID_ARG;
}
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
LOG_ERROR("Controller not initialised\n");
return ESP_ERR_INVALID_STATE;
@@ -125,9 +137,15 @@ esp_err_t esp_bluedroid_init(void)
osi_mem_dbg_init();
#endif
ret = bluedriod_config_init(cfg);
if (ret != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid stack initialize fail, ret:%d", ret);
return ESP_FAIL;
}
/*
* BTC Init
*/
* BTC Init
*/
ret = btc_init();
if (ret != BT_STATUS_SUCCESS) {
LOG_ERROR("Bluedroid Initialize Fail");
@@ -199,6 +217,8 @@ esp_err_t esp_bluedroid_deinit(void)
btc_deinit();
bluedriod_config_deinit();
bd_already_init = false;
return ESP_OK;

View File

@@ -8,14 +8,18 @@
#include <string.h>
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_log.h"
#include "common/bt_trace.h"
#include "bta/bta_api.h"
#include "btc/btc_manage.h"
#include "btc_gap_bt.h"
#include "btc/btc_storage.h"
#include "config/stack_config.h"
#if (BTC_GAP_BT_INCLUDED == TRUE)
#define TAG "BT_GAP"
esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback)
{
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
@@ -318,6 +322,11 @@ esp_err_t esp_bt_gap_set_security_param(esp_bt_sp_param_t param_type,
return ESP_ERR_INVALID_STATE;
}
if (!(bluedriod_config_get()->get_ssp_enabled())) {
ESP_LOGE(TAG, "%s is not supported when `ssp_en` in `esp_bluedroid_config_t` is disabled!", __func__);
return ESP_ERR_NOT_SUPPORTED;
}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_ACT_SET_SECURITY_PARAM;
@@ -338,6 +347,11 @@ esp_err_t esp_bt_gap_ssp_passkey_reply(esp_bd_addr_t bd_addr, bool accept, uint3
return ESP_ERR_INVALID_STATE;
}
if (!(bluedriod_config_get()->get_ssp_enabled())) {
ESP_LOGE(TAG, "%s is not supported when `ssp_en` in `esp_bluedroid_config_t` is disabled!", __func__);
return ESP_ERR_NOT_SUPPORTED;
}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_ACT_PASSKEY_REPLY;
@@ -357,6 +371,11 @@ esp_err_t esp_bt_gap_ssp_confirm_reply(esp_bd_addr_t bd_addr, bool accept)
return ESP_ERR_INVALID_STATE;
}
if (!(bluedriod_config_get()->get_ssp_enabled())) {
ESP_LOGE(TAG, "%s is not supported when `ssp_en` in `esp_bluedroid_config_t` is disabled!", __func__);
return ESP_ERR_NOT_SUPPORTED;
}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_ACT_CONFIRM_REPLY;

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -7,6 +7,9 @@
#ifndef __ESP_BT_MAIN_H__
#define __ESP_BT_MAIN_H__
#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
#ifdef __cplusplus
@@ -22,6 +25,18 @@ typedef enum {
ESP_BLUEDROID_STATUS_ENABLED /*!< Bluetooth initialized and enabled */
} esp_bluedroid_status_t;
/**
* @brief Bluetooth stack configuration
*/
typedef struct {
bool ssp_en; /*!< Whether SSP(secure simple pairing) or legacy pairing is used for Classic Bluetooth */
} esp_bluedroid_config_t;
#define BT_BLUEDROID_INIT_CONFIG_DEFAULT() \
{ \
.ssp_en = true, \
}
/**
* @brief Get bluetooth stack status
*
@@ -31,7 +46,7 @@ typedef enum {
esp_bluedroid_status_t esp_bluedroid_get_status(void);
/**
* @brief Enable bluetooth, must after esp_bluedroid_init().
* @brief Enable bluetooth, must after esp_bluedroid_init()/esp_bluedroid_init_with_cfg().
*
* @return
* - ESP_OK : Succeed
@@ -55,7 +70,18 @@ esp_err_t esp_bluedroid_disable(void);
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_bluedroid_init(void);
esp_err_t esp_bluedroid_init(void) __attribute__((deprecated("Please use esp_bluedroid_init_with_cfg")));
/**
* @brief Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff.
*
* @param cfg Initial configuration of ESP Bluedroid stack.
*
* @return
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_bluedroid_init_with_cfg(esp_bluedroid_config_t *cfg);
/**
* @brief Deinit and free the resource for bluetooth, must be after every bluetooth stuff.

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -277,9 +277,10 @@ typedef void (*esp_hd_cb_t)(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *para
esp_err_t esp_bt_hid_device_register_callback(esp_hd_cb_t callback);
/**
* @brief Initializes HIDD interface. This function should be called after esp_bluedroid_init() and
* esp_bluedroid_enable() success, and should be called after esp_bt_hid_device_register_callback.
* When the operation is complete, the callback function will be called with ESP_HIDD_INIT_EVT.
* @brief Initializes HIDD interface. This function should be called after
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success, and should be
* called after esp_bt_hid_device_register_callback. When the operation is complete, the callback
* function will be called with ESP_HIDD_INIT_EVT.
*
* @return
* - ESP_OK: success
@@ -288,9 +289,10 @@ esp_err_t esp_bt_hid_device_register_callback(esp_hd_cb_t callback);
esp_err_t esp_bt_hid_device_init(void);
/**
* @brief De-initializes HIDD interface. This function should be called after esp_bluedroid_init() and
* esp_bluedroid_enable() success, and should be called after esp_bt_hid_device_init(). When the
* operation is complete, the callback function will be called with ESP_HIDD_DEINIT_EVT.
* @brief De-initializes HIDD interface. This function should be called after
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success, and should be
* called after esp_bt_hid_device_init(). When the operation is complete, the callback function will be
* called with ESP_HIDD_DEINIT_EVT.
*
* @return
* - ESP_OK: success
@@ -300,9 +302,9 @@ esp_err_t esp_bt_hid_device_deinit(void);
/**
* @brief Registers HIDD parameters with SDP and sets l2cap Quality of Service. This function should be
* called after esp_bluedroid_init() and esp_bluedroid_enable() success, and should be called after
* esp_bt_hid_device_init(). When the operation is complete, the callback function will be called
* with ESP_HIDD_REGISTER_APP_EVT.
* called after esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success,
* and should be called after esp_bt_hid_device_init(). When the operation is complete, the callback
* function will be called with ESP_HIDD_REGISTER_APP_EVT.
*
* @param[in] app_param: HIDD parameters
* @param[in] in_qos: incoming QoS parameters
@@ -317,9 +319,9 @@ esp_err_t esp_bt_hid_device_register_app(esp_hidd_app_param_t *app_param, esp_hi
/**
* @brief Removes HIDD parameters from SDP and resets l2cap Quality of Service. This function should be
* called after esp_bluedroid_init() and esp_bluedroid_enable() success, and should be called after
* esp_bt_hid_device_init(). When the operation is complete, the callback function will be called
* with ESP_HIDD_UNREGISTER_APP_EVT.
* called after esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success,
* and should be called after esp_bt_hid_device_init(). When the operation is complete, the callback
* function will be called with ESP_HIDD_UNREGISTER_APP_EVT.
*
* @return
* - ESP_OK: success
@@ -329,8 +331,9 @@ esp_err_t esp_bt_hid_device_unregister_app(void);
/**
* @brief Connects to the peer HID Host with virtual cable. This function should be called after
* esp_bluedroid_init() and esp_bluedroid_enable() success, and should be called after esp_bt_hid_device_init().
* When the operation is complete, the callback function will be called with ESP_HIDD_OPEN_EVT.
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success, and should be
* called after esp_bt_hid_device_init(). When the operation is complete, the callback function will
* be called with ESP_HIDD_OPEN_EVT.
*
* @param[in] bd_addr: Remote host bluetooth device address.
*
@@ -342,8 +345,9 @@ esp_err_t esp_bt_hid_device_connect(esp_bd_addr_t bd_addr);
/**
* @brief Disconnects from the currently connected HID Host. This function should be called after
* esp_bluedroid_init() and esp_bluedroid_enable() success, and should be called after esp_bt_hid_device_init().
* When the operation is complete, the callback function will be called with ESP_HIDD_CLOSE_EVT.
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success, and should be
* called after esp_bt_hid_device_init(). When the operation is complete, the callback function will
* be called with ESP_HIDD_CLOSE_EVT.
*
* @note The disconnect operation will not remove the virtually cabled device. If the connect request from the
* different HID Host, it will reject the request.
@@ -356,8 +360,9 @@ esp_err_t esp_bt_hid_device_disconnect(void);
/**
* @brief Sends HID report to the currently connected HID Host. This function should be called after
* esp_bluedroid_init() and esp_bluedroid_enable() success, and should be called after esp_bt_hid_device_init().
* When the operation is complete, the callback function will be called with ESP_HIDD_SEND_REPORT_EVT.
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success, and should be
* called after esp_bt_hid_device_init(). When the operation is complete, the callback function will
* be called with ESP_HIDD_SEND_REPORT_EVT.
*
* @param[in] type: type of report
* @param[in] id: report id as defined by descriptor
@@ -372,9 +377,9 @@ esp_err_t esp_bt_hid_device_send_report(esp_hidd_report_type_t type, uint8_t id,
/**
* @brief Sends HID Handshake with error info for invalid set_report to the currently connected HID Host.
* This function should be called after esp_bluedroid_init() and esp_bluedroid_enable() success, and
* should be called after esp_bt_hid_device_init(). When the operation is complete, the callback
* function will be called with ESP_HIDD_REPORT_ERR_EVT.
* This function should be called after esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and
* esp_bluedroid_enable() success, and should be called after esp_bt_hid_device_init(). When the
* operation is complete, the callback function will be called with ESP_HIDD_REPORT_ERR_EVT.
*
* @param[in] error: type of error
*
@@ -385,9 +390,10 @@ esp_err_t esp_bt_hid_device_send_report(esp_hidd_report_type_t type, uint8_t id,
esp_err_t esp_bt_hid_device_report_error(esp_hidd_handshake_error_t error);
/**
* @brief Remove the virtually cabled device. This function should be called after esp_bluedroid_init()
* and esp_bluedroid_enable() success, and should be called after esp_bt_hid_device_init(). When the
* operation is complete, the callback function will be called with ESP_HIDD_VC_UNPLUG_EVT.
* @brief Remove the virtually cabled device. This function should be called after
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() and esp_bluedroid_enable() success, and should be
* called after esp_bt_hid_device_init(). When the operation is complete, the callback function will be
* called with ESP_HIDD_VC_UNPLUG_EVT.
*
* @note If the connection exists, then HID Device will send a `VIRTUAL_CABLE_UNPLUG` control command to
* the peer HID Host, and the connection will be destroyed. If the connection does not exist, then HID

View File

@@ -318,8 +318,9 @@ esp_err_t esp_bt_hid_host_register_callback(esp_hh_cb_t callback);
/**
* @brief This function initializes HID host. This function should be called after esp_bluedroid_enable() and
* esp_bluedroid_init() success, and should be called after esp_bt_hid_host_register_callback().
* When the operation is complete the callback function will be called with ESP_HIDH_INIT_EVT.
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() success, and should be called after
* esp_bt_hid_host_register_callback(). When the operation is complete the callback function will be called
* with ESP_HIDH_INIT_EVT.
*
* @return
* - ESP_OK: success
@@ -329,7 +330,7 @@ esp_err_t esp_bt_hid_host_init(void);
/**
* @brief Closes the interface. This function should be called after esp_bluedroid_enable() and
* esp_bluedroid_init() success, and should be called after esp_bt_hid_host_init().
* esp_bluedroid_init()/esp_bluedroid_init_with_cfg() success, and should be called after esp_bt_hid_host_init().
* When the operation is complete the callback function will be called with ESP_HIDH_DEINIT_EVT.
*
* @return - ESP_OK: success

View File

@@ -88,13 +88,6 @@
#define UC_BT_HID_DEVICE_ENABLED FALSE
#endif
//SSP
#ifdef CONFIG_BT_SSP_ENABLED
#define UC_BT_SSP_ENABLED CONFIG_BT_SSP_ENABLED
#else
#define UC_BT_SSP_ENABLED FALSE
#endif
//BQB(BT)
#ifdef CONFIG_BT_CLASSIC_BQB_ENABLED
#define UC_BT_CLASSIC_BQB_ENABLED CONFIG_BT_CLASSIC_BQB_ENABLED

View File

@@ -52,6 +52,7 @@
******************************************************************************/
#if (UC_BT_CLASSIC_ENABLED == TRUE)
#define CLASSIC_BT_INCLUDED TRUE
#define BT_SSP_INCLUDED TRUE
#define BTC_SM_INCLUDED TRUE
#define BTC_PRF_QUEUE_INCLUDED TRUE
#define BTC_GAP_BT_INCLUDED TRUE
@@ -134,10 +135,6 @@
#endif
#endif /* UC_BT_HFP_CLIENT_ENABLED */
#if UC_BT_SSP_ENABLED
#define BT_SSP_INCLUDED TRUE
#endif /* UC_BT_SSP_ENABLED */
#if UC_BT_HID_ENABLED
#define BT_HID_INCLUDED TRUE
#endif /* UC_BT_HID_ENABLED */

View File

@@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdbool.h>
#include "bt_common.h"
struct bluedroid_config {
bool (*get_ssp_enabled)(void);
};
bt_status_t bluedriod_config_init(esp_bluedroid_config_t *cfg);
void bluedriod_config_deinit(void);
const struct bluedroid_config *bluedriod_config_get(void);

View File

@@ -0,0 +1,55 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "osi/allocator.h"
#include "esp_bt_main.h"
#include "config/stack_config.h"
struct stack_config_env_tag {
esp_bluedroid_config_t cfg;
struct bluedroid_config interface;
};
static struct stack_config_env_tag *s_stack_config_env = NULL;
static bool get_ssp_enabled(void)
{
assert(s_stack_config_env);
esp_bluedroid_config_t *cfg = &s_stack_config_env->cfg;
return cfg->ssp_en;
}
bt_status_t bluedriod_config_init(esp_bluedroid_config_t *cfg)
{
s_stack_config_env = osi_calloc(sizeof(struct stack_config_env_tag));
if (!s_stack_config_env) {
return BT_STATUS_NOMEM;
}
memcpy(&s_stack_config_env->cfg, cfg, sizeof(esp_bluedroid_config_t));
struct bluedroid_config *interface = &s_stack_config_env->interface;
interface->get_ssp_enabled = get_ssp_enabled;
return BT_STATUS_SUCCESS;
}
void bluedriod_config_deinit(void)
{
if (s_stack_config_env) {
osi_free(s_stack_config_env);
s_stack_config_env = NULL;
}
}
const struct bluedroid_config *bluedriod_config_get(void)
{
assert(s_stack_config_env);
return &s_stack_config_env->interface;
}

View File

@@ -29,6 +29,7 @@
#include "stack/btm_ble_api.h"
#include "device/version.h"
#include "osi/future.h"
#include "config/stack_config.h"
#if (BLE_50_FEATURE_SUPPORT == TRUE)
const bt_event_mask_t BLE_EVENT_MASK = { "\x00\x00\x00\x00\x00\xff\xff\xff" };
#else
@@ -172,9 +173,11 @@ static void start_up(void)
// Inform the controller what page 0 features we support, based on what
// it told us it supports. We need to do this first before we request the
// next page, because the controller's response for page 1 may be
// dependent on what we configure from page 0
// dependent on what we configure from page 0 and host SSP configuration
#if (BT_SSP_INCLUDED == TRUE)
controller_param.simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(controller_param.features_classic[0].as_array);
controller_param.simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(
controller_param.features_classic[0].as_array) &&
(bluedriod_config_get()->get_ssp_enabled());
#else
controller_param.simple_pairing_supported = false;
#endif

View File

@@ -233,7 +233,8 @@ esp_err_t simple_ble_start(simple_ble_cfg_t *cfg)
return ret;
}
ret = esp_bluedroid_init();
esp_bluedroid_config_t bluedroid_cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();
ret = esp_bluedroid_init_with_cfg(&bluedroid_cfg);
if (ret) {
ESP_LOGE(TAG, "%s init bluetooth failed %d", __func__, ret);
return ret;