From 1cf5a56896deaae8f2359f923e68e0e10ffef99f Mon Sep 17 00:00:00 2001 From: wangmengyang Date: Fri, 17 Mar 2017 19:16:52 +0800 Subject: [PATCH] component/bt: move API to set device name that can be used by both bt and ble applications --- components/bt/bluedroid/api/esp_bt_device.c | 26 ++++++++++++- components/bt/bluedroid/api/esp_gap_ble_api.c | 20 +--------- components/bt/bluedroid/api/esp_gap_bt_api.c | 28 -------------- .../bt/bluedroid/api/include/esp_bt_device.h | 17 +++++++++ .../bt/bluedroid/api/include/esp_gap_bt_api.h | 18 --------- components/bt/bluedroid/btc/core/btc_dev.c | 34 +++++++++++++++++ components/bt/bluedroid/btc/core/btc_task.c | 2 + components/bt/bluedroid/btc/include/btc_dev.h | 38 +++++++++++++++++++ .../bt/bluedroid/btc/include/btc_task.h | 1 + .../btc/profile/std/gap/btc_gap_ble.c | 3 -- .../btc/profile/std/gap/btc_gap_bt.c | 4 -- .../btc/profile/std/include/btc_gap_ble.h | 6 --- .../btc/profile/std/include/btc_gap_bt.h | 6 --- 13 files changed, 119 insertions(+), 84 deletions(-) create mode 100644 components/bt/bluedroid/btc/core/btc_dev.c create mode 100644 components/bt/bluedroid/btc/include/btc_dev.h diff --git a/components/bt/bluedroid/api/esp_bt_device.c b/components/bt/bluedroid/api/esp_bt_device.c index dffcbe80b2..b4885cc0ef 100644 --- a/components/bt/bluedroid/api/esp_bt_device.c +++ b/components/bt/bluedroid/api/esp_bt_device.c @@ -12,10 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. - +#include +#include #include "esp_bt_device.h" #include "esp_bt_main.h" #include "controller.h" +#include "btc_task.h" +#include "btc_dev.h" const uint8_t *esp_bt_dev_get_address(void) { @@ -24,3 +27,24 @@ const uint8_t *esp_bt_dev_get_address(void) } return controller_get_interface()->get_address()->address; } + +esp_err_t esp_bt_dev_set_device_name(const char *name) +{ + btc_msg_t msg; + btc_dev_args_t arg; + + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + if (strlen(name) > ESP_DEV_DEVICE_NAME_MAX) { + return ESP_ERR_INVALID_ARG; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_DEV; + msg.act = BTC_DEV_ACT_SET_DEVICE_NAME; + strcpy(arg.set_dev_name.device_name, name); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_dev_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/bluedroid/api/esp_gap_ble_api.c b/components/bt/bluedroid/api/esp_gap_ble_api.c index 2807281aed..a89b7da2cd 100644 --- a/components/bt/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/bluedroid/api/esp_gap_ble_api.c @@ -13,7 +13,7 @@ // limitations under the License. #include - +#include "esp_bt_device.h" #include "esp_bt_main.h" #include "esp_gap_ble_api.h" #include "bta_api.h" @@ -217,23 +217,7 @@ esp_err_t esp_ble_gap_config_local_privacy (bool privacy_enable) esp_err_t esp_ble_gap_set_device_name(const char *name) { - btc_msg_t msg; - btc_ble_gap_args_t arg; - - if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_ERR_INVALID_STATE; - } - - if (strlen(name) > ESP_GAP_DEVICE_NAME_MAX) { - return ESP_ERR_INVALID_ARG; - } - - msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_GAP_BLE; - msg.act = BTC_GAP_BLE_ACT_SET_DEV_NAME; - strcpy(arg.set_dev_name.device_name, name); - - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); + return esp_bt_dev_set_device_name(name); } uint8_t *esp_ble_resolve_adv_data( uint8_t *adv_data, uint8_t type, uint8_t *length) diff --git a/components/bt/bluedroid/api/esp_gap_bt_api.c b/components/bt/bluedroid/api/esp_gap_bt_api.c index 7c4855e27b..1f68879663 100644 --- a/components/bt/bluedroid/api/esp_gap_bt_api.c +++ b/components/bt/bluedroid/api/esp_gap_bt_api.c @@ -39,32 +39,4 @@ esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t 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 (name != NULL) { - size_t len = strlen(name); - if (len > 0 || len <= ESP_BT_GAP_DEVICE_NAME_MAX) { - strcpy(arg.set_dev_name.device_name, name); - } else { - return ESP_ERR_INVALID_ARG; - } - } else { - 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; - - - return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); -} - #endif /* #if BTC_GAP_BT_INCLUDED */ diff --git a/components/bt/bluedroid/api/include/esp_bt_device.h b/components/bt/bluedroid/api/include/esp_bt_device.h index c84d042d66..d3c3f79c55 100644 --- a/components/bt/bluedroid/api/include/esp_bt_device.h +++ b/components/bt/bluedroid/api/include/esp_bt_device.h @@ -17,6 +17,8 @@ #include #include +#include "esp_err.h" +#include "esp_bt_defs.h" #ifdef __cplusplus extern "C" { @@ -30,6 +32,21 @@ extern "C" { */ const uint8_t *esp_bt_dev_get_address(void); + +/** + * @brief Set bluetooth device name. This function should be called after esp_bluedroid_enable() + * completes successfully + * + * @param[in] name : device name to be set + * + * @return + * - ESP_OK : Succeed + * - ESP_ERR_INVALID_ARG : if name is NULL pointer or empty, or string length out of limit + * - ESP_INVALID_STATE : if bluetooth stack is not yet enabled + * - ESP_FAIL : others + */ +esp_err_t esp_bt_dev_set_device_name(const char *name); + #ifdef __cplusplus } #endif 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 47b669a76d..5efdd63326 100644 --- a/components/bt/bluedroid/api/include/esp_gap_bt_api.h +++ b/components/bt/bluedroid/api/include/esp_gap_bt_api.h @@ -24,8 +24,6 @@ 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 */ @@ -47,22 +45,6 @@ typedef enum { */ esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode); - -/** - * @brief Set bluetooth device name. This function should be called after esp_bluedroid_enable() - * completes successfully - * - * @param[in] name : device name to be set, string length should not exceed ESP_BT_GAP_DEVICE_NAME_MAX - * - * @return - * - ESP_OK : Succeed - * - ESP_ERR_INVALID_ARG : if name is NULL pointer or empty, or string length out of limit - * - ESP_INVALID_STATE : if bluetooth stack is not yet enabled - * - ESP_FAIL : others - */ -esp_err_t esp_bt_gap_set_device_name(const char *name); - - #ifdef __cplusplus } #endif diff --git a/components/bt/bluedroid/btc/core/btc_dev.c b/components/bt/bluedroid/btc/core/btc_dev.c new file mode 100644 index 0000000000..b75100055a --- /dev/null +++ b/components/bt/bluedroid/btc/core/btc_dev.c @@ -0,0 +1,34 @@ +// 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 "bta_api.h" +#include "btc_task.h" +#include "btc_manage.h" +#include "btc_dev.h" + +void btc_dev_call_handler(btc_msg_t *msg) +{ + btc_dev_args_t *arg = (btc_dev_args_t *)msg->arg; + + LOG_DEBUG("%s act %d\n", __FUNCTION__, msg->act); + + switch (msg->act) { + case BTC_DEV_ACT_SET_DEVICE_NAME: + BTA_DmSetDeviceName(arg->set_dev_name.device_name); + break; + default: + break; + } +} diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index 699a8e8d99..f44d12439c 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -20,6 +20,7 @@ #include "gki.h" #include "bt_defs.h" #include "btc_main.h" +#include "btc_dev.h" #include "btc_gatts.h" #include "btc_gattc.h" #include "btc_gap_ble.h" @@ -39,6 +40,7 @@ static xQueueHandle xBtcQueue = 0; static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL }, + [BTC_PID_DEV] = {btc_dev_call_handler, NULL }, [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 }, diff --git a/components/bt/bluedroid/btc/include/btc_dev.h b/components/bt/bluedroid/btc/include/btc_dev.h new file mode 100644 index 0000000000..dd2e8663a9 --- /dev/null +++ b/components/bt/bluedroid/btc/include/btc_dev.h @@ -0,0 +1,38 @@ +// 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_DEV_H__ +#define __BTC_DEV_H__ + +#include "esp_bt_defs.h" +#include "esp_bt_device.h" +#include "btc_task.h" + +typedef enum { + BTC_DEV_ACT_SET_DEVICE_NAME +} btc_dev_act_t; + +/* btc_dev_args_t */ +typedef union { + // BTC_BT_GAP_ACT_SET_DEV_NAME + struct set_bt_dev_name_args { +#define ESP_DEV_DEVICE_NAME_MAX (32) + char device_name[ESP_DEV_DEVICE_NAME_MAX + 1]; + } set_dev_name; +} btc_dev_args_t; + +void btc_dev_call_handler(btc_msg_t *msg); + +#endif /* __BTC_DEV_H__ */ + diff --git a/components/bt/bluedroid/btc/include/btc_task.h b/components/bt/bluedroid/btc/include/btc_task.h index 25d2cbe561..bb89969746 100644 --- a/components/bt/bluedroid/btc/include/btc_task.h +++ b/components/bt/bluedroid/btc/include/btc_task.h @@ -36,6 +36,7 @@ typedef enum { typedef enum { BTC_PID_MAIN_INIT = 0, + BTC_PID_DEV, BTC_PID_GATTS, BTC_PID_GATTC, BTC_PID_GAP_BLE, diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c index 9e158c592e..4cb05116c7 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -768,9 +768,6 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) case BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY: btc_ble_config_local_privacy(arg->cfg_local_privacy.privacy_enable); break; - case BTC_GAP_BLE_ACT_SET_DEV_NAME: - BTA_DmSetDeviceName(arg->set_dev_name.device_name); - break; case BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW: btc_ble_set_adv_data_raw(arg->cfg_adv_data_raw.raw_adv, arg->cfg_adv_data_raw.raw_adv_len, 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 a9a461679b..07737b7d33 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 @@ -78,10 +78,6 @@ void btc_gap_bt_call_handler(btc_msg_t *msg) 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: break; } diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h index ea639bcde9..cc32eb76bc 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h @@ -31,7 +31,6 @@ typedef enum { BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN, BTC_GAP_BLE_ACT_SET_RAND_ADDRESS, BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY, - BTC_GAP_BLE_ACT_SET_DEV_NAME, BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW, BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW, } btc_gap_ble_act_t; @@ -73,11 +72,6 @@ typedef union { struct cfg_local_privacy_args { bool privacy_enable; } cfg_local_privacy; - //BTC_GAP_BLE_ACT_SET_DEV_NAME, - struct set_dev_name_args { -#define ESP_GAP_DEVICE_NAME_MAX (32) - char device_name[ESP_GAP_DEVICE_NAME_MAX + 1]; - } set_dev_name; //BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW, struct config_adv_data_raw_args { uint8_t *raw_adv; 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 6c74522b27..d77a4b9f65 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 @@ -21,7 +21,6 @@ typedef enum { BTC_GAP_BT_ACT_SET_SCAN_MODE = 0, - BTC_GAP_BT_ACT_SET_DEV_NAME } btc_gap_bt_act_t; /* btc_bt_gap_args_t */ @@ -30,11 +29,6 @@ typedef union { struct set_bt_scan_mode_args { esp_bt_scan_mode_t 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);