From 8f65322a84ac9aea2704e6a757e75bff1a67d8f1 Mon Sep 17 00:00:00 2001 From: wangmengyang Date: Tue, 28 Feb 2017 11:47:04 +0800 Subject: [PATCH] component/bt: modify the enable/disable function --- components/bt/bluedroid/api/esp_gap_bt_api.c | 23 ++- .../bt/bluedroid/api/include/esp_gap_bt_api.h | 2 + components/bt/bluedroid/btc/core/btc_dm.c | 157 ++++++++++++++++++ components/bt/bluedroid/btc/core/btc_main.c | 3 +- components/bt/bluedroid/btc/core/btc_task.c | 2 + components/bt/bluedroid/btc/include/btc_dm.h | 37 +++++ .../bt/bluedroid/btc/include/btc_task.h | 1 + .../btc/profile/std/gap/btc_gap_bt.c | 19 +-- .../btc/profile/std/include/btc_gap_bt.h | 16 +- components/bt/bluedroid/btif/btif_dm.c | 3 +- .../bluedroid_demos/app_project/SampleAV.c | 19 ++- 11 files changed, 253 insertions(+), 29 deletions(-) create mode 100644 components/bt/bluedroid/btc/core/btc_dm.c create mode 100644 components/bt/bluedroid/btc/include/btc_dm.h diff --git a/components/bt/bluedroid/api/esp_gap_bt_api.c b/components/bt/bluedroid/api/esp_gap_bt_api.c index 7b74435ad3..1de8b6088b 100644 --- a/components/bt/bluedroid/api/esp_gap_bt_api.c +++ b/components/bt/bluedroid/api/esp_gap_bt_api.c @@ -13,7 +13,6 @@ // limitations under the License. #include - #include "esp_bt_main.h" #include "esp_gap_bt_api.h" #include "bt_trace.h" @@ -32,8 +31,28 @@ esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GAP_BT; msg.act = BTC_GAP_BT_ACT_SET_SCAN_MODE; - arg.scan_mode.mode = mode; + arg.set_scan_mode.mode = mode; return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_bt_gap_set_device_name(const char *name) +{ + btc_msg_t msg; + btc_gap_bt_args_t arg; + + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + if (strlen(name) > ESP_BT_GAP_DEVICE_NAME_MAX) { + return ESP_ERR_INVALID_ARG; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GAP_BT; + msg.act = BTC_GAP_BT_ACT_SET_DEV_NAME; + strcpy(arg.set_dev_name.device_name, name); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/bluedroid/api/include/esp_gap_bt_api.h b/components/bt/bluedroid/api/include/esp_gap_bt_api.h index 6f7df2cad6..5ab213570e 100644 --- a/components/bt/bluedroid/api/include/esp_gap_bt_api.h +++ b/components/bt/bluedroid/api/include/esp_gap_bt_api.h @@ -23,6 +23,8 @@ extern "C" { #endif +#define ESP_BT_GAP_DEVICE_NAME_MAX (32) + /// Discoverability and Connectability mode typedef enum { ESP_BT_SCAN_MODE_NONE = 0, /*!< Neither discoverable nor connectable */ diff --git a/components/bt/bluedroid/btc/core/btc_dm.c b/components/bt/bluedroid/btc/core/btc_dm.c new file mode 100644 index 0000000000..13a0424c35 --- /dev/null +++ b/components/bt/bluedroid/btc/core/btc_dm.c @@ -0,0 +1,157 @@ +#include +#include +#include "btc_dm.h" +#include "btc_main.h" +#include "bt_trace.h" +#include "bt_target.h" +#include "btif_storage.h" // TODO: replace with "btc" + +extern void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl); // todo: replace with "btc_xx" + +static void btc_dm_sec_arg_deep_free(btc_msg_t *msg) +{ + btc_dm_sec_args_t *arg = (btc_dm_sec_args_t *)(msg->arg); + if (msg->act == BTA_DM_BLE_KEY_EVT) { + osi_free(arg->sec.ble_key.p_key_value); + } +} + +void btc_dm_sec_arg_deep_copy(btc_msg_t *msg, void *dst, void *src) +{ + tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC *)dst; + tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC *)src; + + if (!src_dm_sec) { + return; + } + + assert(dst_dm_sec); + memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC)); + + if (msg->act == BTA_DM_BLE_KEY_EVT) { + dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE)); + assert(src_dm_sec->ble_key.p_key_value); + assert(dst_dm_sec->ble_key.p_key_value); + memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE)); + } +} + + +/******************************************************************************* +** +** Function btc_dm_evt +** +** Description Switches context from BTE to BTC for all DM events +** +** Returns void +** +*******************************************************************************/ + +void btc_dm_sec_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *data) +{ + btc_msg_t msg; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_DM_SEC; + msg.act = event; + + btc_transfer_context(&msg, (btc_dm_sec_args_t *)data, sizeof(btc_dm_sec_args_t), btc_dm_sec_arg_deep_copy); +} + +static void btc_enable_bluetooth_evt(tBTA_STATUS status) +{ + if (status == BTA_SUCCESS) { + future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_SUCCESS); + } else { + future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL); + } +} + +static void btc_disable_bluetooth_evt(void) +{ + LOG_DEBUG("%s", __FUNCTION__); + + future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_SUCCESS); +} + +void btc_dm_sec_cb_handler(btc_msg_t *msg) +{ + btc_dm_sec_args_t *arg = (btc_dm_sec_args_t *)(msg->arg); + tBTA_DM_SEC *p_data = &(arg->sec); + // tBTA_SERVICE_MASK service_mask; + LOG_DEBUG("btif_dm_upstreams_cback ev: %d\n", msg->act); + + switch (msg->act) { + case BTA_DM_ENABLE_EVT: +#if KARL_NOT_IGNORE + /* for each of the enabled services in the mask, trigger the profile + * enable */ + service_mask = btif_get_enabled_services_mask(); + for (int i = 0; i <= BTA_MAX_SERVICE_ID; i++) { + if (service_mask & + (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) { + btif_in_execute_service_request(i, TRUE); + } + } + btif_enable_bluetooth_evt(p_data->enable.status); +#endif /* KARL_NOT_IGNORE */ + btif_storage_load_bonded_devices(); + btc_enable_bluetooth_evt(p_data->enable.status); + break; + case BTA_DM_DISABLE_EVT: +#if KARL_NOT_IGNORE + /* for each of the enabled services in the mask, trigger the profile + * disable */ + service_mask = btif_get_enabled_services_mask(); + for (int i = 0; i <= BTA_MAX_SERVICE_ID; i++) { + if (service_mask & + (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) { + btif_in_execute_service_request(i, FALSE); + } + } + btif_disable_bluetooth_evt(); +#endif /* KARL_NOT_IGNORE */ + btc_disable_bluetooth_evt(); + break; + case BTA_DM_PIN_REQ_EVT: + break; + case BTA_DM_AUTH_CMPL_EVT: + btif_dm_auth_cmpl_evt(&p_data->auth_cmpl); + break; + case BTA_DM_BOND_CANCEL_CMPL_EVT: + case BTA_DM_SP_CFM_REQ_EVT: + case BTA_DM_SP_KEY_NOTIF_EVT: + + case BTA_DM_DEV_UNPAIRED_EVT: + case BTA_DM_BUSY_LEVEL_EVT: + case BTA_DM_LINK_UP_EVT: + case BTA_DM_LINK_DOWN_EVT: + case BTA_DM_HW_ERROR_EVT: + +#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) + case BTA_DM_BLE_KEY_EVT: + case BTA_DM_BLE_SEC_REQ_EVT: + case BTA_DM_BLE_PASSKEY_NOTIF_EVT: + case BTA_DM_BLE_PASSKEY_REQ_EVT: + case BTA_DM_BLE_NC_REQ_EVT: + case BTA_DM_BLE_OOB_REQ_EVT: + case BTA_DM_BLE_LOCAL_IR_EVT: + case BTA_DM_BLE_LOCAL_ER_EVT: + case BTA_DM_BLE_AUTH_CMPL_EVT: + case BTA_DM_LE_FEATURES_READ: + case BTA_DM_ENER_INFO_READ: +#endif + + case BTA_DM_AUTHORIZE_EVT: + case BTA_DM_SIG_STRENGTH_EVT: + case BTA_DM_SP_RMT_OOB_EVT: + case BTA_DM_SP_KEYPRESS_EVT: + case BTA_DM_ROLE_CHG_EVT: + + default: + LOG_WARN( "btc_dm_sec_cback : unhandled event (%d)\n", msg->act ); + break; + } + + btc_dm_sec_arg_deep_free(msg); +} diff --git a/components/bt/bluedroid/btc/core/btc_main.c b/components/bt/bluedroid/btc/core/btc_main.c index 96a7927bab..89444ad4f5 100644 --- a/components/bt/bluedroid/btc/core/btc_main.c +++ b/components/bt/bluedroid/btc/core/btc_main.c @@ -14,6 +14,7 @@ #include "btc_task.h" #include "btc_main.h" +#include "btc_dm.h" #include "future.h" #include "esp_err.h" #include "btif_config.h" @@ -42,7 +43,7 @@ static void btc_sec_callback(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data) static void btc_enable_bluetooth(void) { - if (BTA_EnableBluetooth(btc_sec_callback) != BTA_SUCCESS) { + if (BTA_EnableBluetooth(btc_dm_sec_evt) != BTA_SUCCESS) { future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL); } } diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index 6e88afaabb..49c42bfdee 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -25,6 +25,7 @@ #include "btc_gap_ble.h" #include "btc_gap_bt.h" #include "btc_blufi_prf.h" +#include "btc_dm.h" #include "bta_gatt_api.h" @@ -43,6 +44,7 @@ static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_SPP] = {NULL, NULL}, [BTC_PID_SPPLIKE] = {NULL, NULL}, [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler }, + [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler } }; /***************************************************************************** diff --git a/components/bt/bluedroid/btc/include/btc_dm.h b/components/bt/bluedroid/btc/include/btc_dm.h new file mode 100644 index 0000000000..28dcd08b50 --- /dev/null +++ b/components/bt/bluedroid/btc/include/btc_dm.h @@ -0,0 +1,37 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef __BTC_DM_H__ +#define __BTC_DM_H__ + +#include "btc_task.h" +#include "esp_bt_defs.h" +#include "bta_api.h" + +typedef enum { + BTC_DM_SEC_ACT +} btc_dm_sec_act_t; + +/* btc_dm_args_t */ +typedef union { + //BTC_DM_SEC_ACT + tBTA_DM_SEC sec; +} btc_dm_sec_args_t; + +// void btc_dm_call_handler(btc_msg_t *msg); +void btc_dm_sec_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *data); +void btc_dm_sec_cb_handler(btc_msg_t *msg); +void btc_dm_sec_arg_deep_copy(btc_msg_t *msg, void *dst, void *src); + +#endif /* __BTC_DM_H__ */ diff --git a/components/bt/bluedroid/btc/include/btc_task.h b/components/bt/bluedroid/btc/include/btc_task.h index 1574dae2f3..4f97740002 100644 --- a/components/bt/bluedroid/btc/include/btc_task.h +++ b/components/bt/bluedroid/btc/include/btc_task.h @@ -45,6 +45,7 @@ typedef enum { BTC_PID_SPP, BTC_PID_SPPLIKE, BTC_PID_BLUFI, + BTC_PID_DM_SEC, BTC_PID_NUM, } btc_pid_t; //btc profile id diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c index ee6dd82653..7ef239ffb2 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c @@ -73,7 +73,11 @@ void btc_gap_bt_call_handler(btc_msg_t *msg) LOG_DEBUG("%s act %d\n", __func__, msg->act); switch (msg->act) { case BTC_GAP_BT_ACT_SET_SCAN_MODE: { - btc_bt_set_scan_mode(arg->scan_mode.mode); + btc_bt_set_scan_mode(arg->set_scan_mode.mode); + break; + } + case BTC_GAP_BT_ACT_SET_DEV_NAME: { + BTA_DmSetDeviceName(arg->set_dev_name.device_name); break; } default: @@ -82,16 +86,7 @@ void btc_gap_bt_call_handler(btc_msg_t *msg) btc_gap_bt_arg_deep_free(msg); } -/* to be fixed */ -esp_err_t esp_bt_gap_set_device_name(const char *name) +void btc_gap_bt_cb_handler(btc_msg_t *msg) { - if (name == NULL || *name == '\0') { - return ESP_ERR_INVALID_ARG; - } - #define ESP_GAP_DEVICE_NAME_MAX (32) - char dev_name[ESP_GAP_DEVICE_NAME_MAX+1]; - strncpy(dev_name, name, ESP_GAP_DEVICE_NAME_MAX); - dev_name[ESP_GAP_DEVICE_NAME_MAX] = '\0'; - BTA_DmSetDeviceName(dev_name); - return ESP_OK; + // todo } diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h index a1e1ee940e..3eff899677 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h @@ -20,15 +20,21 @@ #include "btc_task.h" typedef enum { - BTC_GAP_BT_ACT_SET_SCAN_MODE = 0 + BTC_GAP_BT_ACT_SET_SCAN_MODE = 0, + BTC_GAP_BT_ACT_SET_DEV_NAME } btc_gap_bt_act_t; -/* btc_gap_bt_args_t */ +/* btc_bt_gap_args_t */ typedef union { - // BTC_GAP_BT_ACT_SET_SCAN_MODE, - struct scan_mode_args { + // BTC_BT_GAP_ACT_SET_SCAN_MODE, + struct set_bt_scan_mode_args { esp_bt_scan_mode_t mode; - } scan_mode; + } set_scan_mode; + + // BTC_BT_GAP_ACT_SET_DEV_NAME + struct set_bt_dev_name_args { + char device_name[ESP_BT_GAP_DEVICE_NAME_MAX + 1]; + } set_dev_name; } btc_gap_bt_args_t; void btc_gap_bt_call_handler(btc_msg_t *msg); diff --git a/components/bt/bluedroid/btif/btif_dm.c b/components/bt/bluedroid/btif/btif_dm.c index ba968c9129..fd2ba99df4 100644 --- a/components/bt/bluedroid/btif/btif_dm.c +++ b/components/bt/bluedroid/btif/btif_dm.c @@ -145,7 +145,8 @@ void btif_dm_execute_service_request(UINT16 event, char *p_param) ** Returns void ** *******************************************************************************/ -static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) +// static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) +void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) { /* Save link key, if not temporary */ bt_bdaddr_t bd_addr; diff --git a/examples/09_a2dp/components/bluedroid_demos/app_project/SampleAV.c b/examples/09_a2dp/components/bluedroid_demos/app_project/SampleAV.c index 79c06343ec..5c7faf7c29 100644 --- a/examples/09_a2dp/components/bluedroid_demos/app_project/SampleAV.c +++ b/examples/09_a2dp/components/bluedroid_demos/app_project/SampleAV.c @@ -12,7 +12,7 @@ // #include "EspAudioCom.h" #include "bt_app_common.h" -#include "esp_bt_stack_manager.h" +#include "esp_bt_main.h" #include "esp_gap_bt_api.h" #include "esp_a2dp_api.h" #include "esp_avrc_api.h" @@ -76,15 +76,16 @@ static void bt_app_handle_evt(uint16_t event, void *p_param) switch (event) { case BT_APP_EVT_STACK_ON: { char *dev_name = "ESP_SPEAKER"; + esp_bt_gap_set_device_name(dev_name); - esp_a2d_register_callback(&bt_app_a2d_cb); - esp_a2d_register_data_callback(bt_app_a2d_data_cb); + // esp_a2d_register_callback(&bt_app_a2d_cb); + // esp_a2d_register_data_callback(bt_app_a2d_data_cb); - esp_a2d_sink_init(); + // esp_a2d_sink_init(); - esp_avrc_ct_init(); - esp_avrc_ct_register_callback(bt_app_rc_ct_cb); + // esp_avrc_ct_init(); + // esp_avrc_ct_register_callback(bt_app_rc_ct_cb); esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); break; @@ -143,12 +144,14 @@ void bt_app_handle_rc_evt(uint16_t event, void *p_param) void app_main_entry(void) { esp_err_t init, enable; - init = esp_bt_init_stack(); + // init = esp_bt_init_stack(); + init = esp_bluedroid_init(); if (init != ESP_OK) { return; } - enable = esp_bt_enable_stack(); + // enable = esp_bt_enable_stack(); + enable = esp_bluedroid_enable(); if (enable != ESP_OK) { return; }