From c7dac035ae17b3fbc1e2c8f86064f0a9cf9bce76 Mon Sep 17 00:00:00 2001 From: wangmengyang Date: Fri, 24 Feb 2017 17:28:33 +0800 Subject: [PATCH] component/bt: add module btc_gap_bt and port function esp_bt_gap_set_scan_mode --- components/bt/bluedroid/api/esp_gap_bt_api.c | 39 +++++++++++++++ .../bt/bluedroid/api/include/esp_gap_bt_api.h | 10 ++-- components/bt/bluedroid/btc/core/btc_main.c | 4 +- components/bt/bluedroid/btc/core/btc_task.c | 3 +- .../btc/profile/std/gap/btc_gap_bt.c | 50 ++++++++++++++++--- .../btc/profile/std/include/btc_gap_bt.h | 19 ++++++- .../bluedroid_demos/app_project/SampleAV.c | 2 +- 7 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 components/bt/bluedroid/api/esp_gap_bt_api.c diff --git a/components/bt/bluedroid/api/esp_gap_bt_api.c b/components/bt/bluedroid/api/esp_gap_bt_api.c new file mode 100644 index 0000000000..7b74435ad3 --- /dev/null +++ b/components/bt/bluedroid/api/esp_gap_bt_api.c @@ -0,0 +1,39 @@ +// 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. + +#include + +#include "esp_bt_main.h" +#include "esp_gap_bt_api.h" +#include "bt_trace.h" +#include "btc_manage.h" +#include "btc_gap_bt.h" + +esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode) +{ + btc_msg_t msg; + btc_gap_bt_args_t arg; + + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + 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; + + 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 393d33a1d0..6f7df2cad6 100644 --- a/components/bt/bluedroid/api/include/esp_gap_bt_api.h +++ b/components/bt/bluedroid/api/include/esp_gap_bt_api.h @@ -25,10 +25,10 @@ extern "C" { /// Discoverability and Connectability mode typedef enum { - BT_SCAN_MODE_NONE = 0, /*!< Neither discoverable nor connectable */ - BT_SCAN_MODE_CONNECTABLE, /*!< Connectable but not discoverable */ - BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE /*!< both discoverable and connectaable */ -} bt_scan_mode_t; + ESP_BT_SCAN_MODE_NONE = 0, /*!< Neither discoverable nor connectable */ + ESP_BT_SCAN_MODE_CONNECTABLE, /*!< Connectable but not discoverable */ + ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE /*!< both discoverable and connectaable */ +} esp_bt_scan_mode_t; /** * @brief Set discoverability and connectability mode for legacy bluetooth @@ -39,7 +39,7 @@ typedef enum { * - ESP_OK : Succeed * - ESP_ERR_INVALID_ARG: if argument invalid */ -esp_err_t esp_bt_gap_set_scan_mode(bt_scan_mode_t mode); +esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode); /** diff --git a/components/bt/bluedroid/btc/core/btc_main.c b/components/bt/bluedroid/btc/core/btc_main.c index 323a81f75a..d4c98dffe1 100644 --- a/components/bt/bluedroid/btc/core/btc_main.c +++ b/components/bt/bluedroid/btc/core/btc_main.c @@ -43,14 +43,14 @@ static void btc_enable_bluetooth(void) { if (BTA_EnableBluetooth(btc_sec_callback) != BTA_SUCCESS) { future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL); - } + } } static void btc_disable_bluetooth(void) { if (BTA_DisableBluetooth() != BTA_SUCCESS) { future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_FAIL); - } + } } void btc_init_callback(void) diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index a245f54d28..6e88afaabb 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -23,6 +23,7 @@ #include "btc_gatts.h" #include "btc_gattc.h" #include "btc_gap_ble.h" +#include "btc_gap_bt.h" #include "btc_blufi_prf.h" #include "bta_gatt_api.h" @@ -35,7 +36,7 @@ static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_GATTS] = {btc_gatts_call_handler, btc_gatts_cb_handler }, [BTC_PID_GATTC] = {btc_gattc_call_handler, btc_gattc_cb_handler }, [BTC_PID_GAP_BLE] = {btc_gap_ble_call_handler, btc_gap_ble_cb_handler }, - [BTC_PID_GAP_BT] = {NULL, NULL}, // {btc_gap_bt_call_handler, btc_gap_bt_cb_handler }, + [BTC_PID_GAP_BT] = {btc_gap_bt_call_handler, btc_gap_bt_cb_handler }, [BTC_PID_SDP] = {NULL, NULL}, [BTC_PID_BLE_HID] = {NULL, NULL}, [BTC_PID_BT_HID] = {NULL, NULL}, 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 275e600dfa..ee6dd82653 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 @@ -13,42 +13,76 @@ // limitations under the License. #include "esp_gap_bt_api.h" +#include "btc_gap_bt.h" #include "bta_api.h" #include "bt_trace.h" #include -esp_err_t esp_bt_gap_set_scan_mode(bt_scan_mode_t mode) +void btc_gap_bt_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + switch (msg->act) { + default: + // LOG_ERROR("Unhandled deep copy %d\n", msg->act); + break; + } +} + +static void btc_gap_bt_arg_deep_free(btc_msg_t *msg) +{ + LOG_DEBUG("%s \n", __func__); + switch (msg->act) { + default: + // LOG_DEBUG("Unhandled deep free %d\n", msg->act); + break; + } +} + +static void btc_bt_set_scan_mode(esp_bt_scan_mode_t mode) { tBTA_DM_DISC disc_mode; tBTA_DM_CONN conn_mode; switch (mode) { - case BT_SCAN_MODE_NONE: + case ESP_BT_SCAN_MODE_NONE: disc_mode = BTA_DM_NON_DISC; conn_mode = BTA_DM_NON_CONN; break; - case BT_SCAN_MODE_CONNECTABLE: + case ESP_BT_SCAN_MODE_CONNECTABLE: disc_mode = BTA_DM_NON_DISC; conn_mode = BTA_DM_CONN; break; - case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE: + case ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE: disc_mode = BTA_DM_GENERAL_DISC; conn_mode = BTA_DM_CONN; break; default: - // BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode); - return ESP_ERR_INVALID_ARG; + LOG_WARN("invalid scan mode (0x%x)", mode); + return; } - // BTIF_TRACE_EVENT("set property scan mode : %x", mode); BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE); + return; +} - return ESP_OK; +void btc_gap_bt_call_handler(btc_msg_t *msg) +{ + btc_gap_bt_args_t *arg = (btc_gap_bt_args_t *)msg->arg; + 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); + break; + } + default: + break; + } + btc_gap_bt_arg_deep_free(msg); } +/* to be fixed */ esp_err_t esp_bt_gap_set_device_name(const char *name) { if (name == NULL || *name == '\0') { 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 d6f2e5b535..a1e1ee940e 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 @@ -15,8 +15,25 @@ #ifndef __BTC_GAP_BT_H__ #define __BTC_GAP_BT_H__ +#include "esp_bt_defs.h" +#include "esp_gap_bt_api.h" +#include "btc_task.h" +typedef enum { + BTC_GAP_BT_ACT_SET_SCAN_MODE = 0 +} btc_gap_bt_act_t; +/* btc_gap_bt_args_t */ +typedef union { + // BTC_GAP_BT_ACT_SET_SCAN_MODE, + struct scan_mode_args { + esp_bt_scan_mode_t mode; + } scan_mode; +} btc_gap_bt_args_t; +void btc_gap_bt_call_handler(btc_msg_t *msg); +void btc_gap_bt_cb_handler(btc_msg_t *msg); -#define /* __BTC_GAP_BT_H__ */ +void btc_gap_bt_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +#endif /* __BTC_GAP_BT_H__ */ 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 3bf8cf7f89..79c06343ec 100644 --- a/examples/09_a2dp/components/bluedroid_demos/app_project/SampleAV.c +++ b/examples/09_a2dp/components/bluedroid_demos/app_project/SampleAV.c @@ -86,7 +86,7 @@ static void bt_app_handle_evt(uint16_t event, void *p_param) esp_avrc_ct_init(); esp_avrc_ct_register_callback(bt_app_rc_ct_cb); - esp_bt_gap_set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); + esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); break; } case ESP_A2D_CONNECTION_STATE_EVT: { -- 2.40.0