Skip to content

Commit

Permalink
Merge branch 'feature/add_new_bluedroid_init' into 'master'
Browse files Browse the repository at this point in the history
Feat(bt/bluedroid): Add new ESP Bluedroid stack init API

Closes BTQABR2023-32

See merge request espressif/esp-idf!25248
  • Loading branch information
wmy-espressif committed Sep 4, 2023
2 parents 6598c6e + 4b35ac5 commit 78af508
Show file tree
Hide file tree
Showing 93 changed files with 819 additions and 519 deletions.
6 changes: 4 additions & 2 deletions components/bt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand Down
8 changes: 0 additions & 8 deletions components/bt/host/bluedroid/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions components/bt/host/bluedroid/api/esp_bt_main.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -199,6 +217,8 @@ esp_err_t esp_bluedroid_deinit(void)

btc_deinit();

bluedriod_config_deinit();

bd_already_init = false;

return ESP_OK;
Expand Down
22 changes: 19 additions & 3 deletions components/bt/host/bluedroid/api/esp_gap_bt_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -307,7 +311,6 @@ esp_err_t esp_bt_gap_pin_reply(esp_bd_addr_t bd_addr, bool accept, uint8_t pin_c
btc_gap_bt_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

#if (BT_SSP_INCLUDED == TRUE)
esp_err_t esp_bt_gap_set_security_param(esp_bt_sp_param_t param_type,
void *value, uint8_t len)
{
Expand All @@ -318,6 +321,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;
Expand All @@ -338,6 +346,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;
Expand All @@ -357,6 +370,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;
Expand All @@ -366,8 +384,6 @@ esp_err_t esp_bt_gap_ssp_confirm_reply(esp_bd_addr_t bd_addr, bool accept)
btc_gap_bt_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

#endif /*(BT_SSP_INCLUDED == TRUE)*/

esp_err_t esp_bt_gap_set_afh_channels(esp_bt_gap_afh_channels channels)
{
btc_msg_t msg;
Expand Down
32 changes: 29 additions & 3 deletions components/bt/host/bluedroid/api/include/api/esp_bt_main.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __ESP_BT_MAIN_H__
#define __ESP_BT_MAIN_H__

#include <stdbool.h>
#include <stdint.h>

#include "esp_err.h"

#ifdef __cplusplus
Expand All @@ -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
*
Expand All @@ -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
Expand All @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions components/bt/host/bluedroid/api/include/api/esp_gap_bt_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@ esp_err_t esp_bt_gap_set_pin(esp_bt_pin_type_t pin_type, uint8_t pin_code_len, e
*/
esp_err_t esp_bt_gap_pin_reply(esp_bd_addr_t bd_addr, bool accept, uint8_t pin_code_len, esp_bt_pin_code_t pin_code);

#if (BT_SSP_INCLUDED == TRUE)
/**
* @brief Set a GAP security parameter value. Overrides the default value.
*
Expand Down Expand Up @@ -758,8 +757,6 @@ esp_err_t esp_bt_gap_ssp_passkey_reply(esp_bd_addr_t bd_addr, bool accept, uint3
*/
esp_err_t esp_bt_gap_ssp_confirm_reply(esp_bd_addr_t bd_addr, bool accept);

#endif /*(BT_SSP_INCLUDED == TRUE)*/

/**
* @brief Set the AFH channels
*
Expand Down
56 changes: 31 additions & 25 deletions components/bt/host/bluedroid/api/include/api/esp_hidd_api.h
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
*
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
*
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions components/bt/host/bluedroid/api/include/api/esp_hidh_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 78af508

Please sign in to comment.