From 564c4b644d1d13a06d878fbab79309745c7386d7 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Thu, 12 Jan 2023 10:53:58 +0800 Subject: [PATCH] bluedroid: support get bluetooth device name --- .../bt/host/bluedroid/api/esp_gap_ble_api.c | 13 +++++++ .../api/include/api/esp_gap_ble_api.h | 20 ++++++++++- .../bt/host/bluedroid/bta/dm/bta_dm_act.c | 21 +++++++++++ .../bt/host/bluedroid/bta/dm/bta_dm_api.c | 21 +++++++++++ .../bt/host/bluedroid/bta/dm/bta_dm_main.c | 1 + .../bluedroid/bta/dm/include/bta_dm_int.h | 8 +++++ .../host/bluedroid/bta/include/bta/bta_api.h | 14 ++++++++ .../btc/profile/std/gap/btc_gap_ble.c | 36 +++++++++++++++++++ .../btc/profile/std/include/btc_gap_ble.h | 1 + .../common/include/common/bt_target.h | 1 + .../bluedroid/stack/include/stack/btm_api.h | 1 + 11 files changed, 136 insertions(+), 1 deletion(-) diff --git a/components/bt/host/bluedroid/api/esp_gap_ble_api.c b/components/bt/host/bluedroid/api/esp_gap_ble_api.c index cca3bfcd95..a44d0cdce3 100644 --- a/components/bt/host/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/host/bluedroid/api/esp_gap_ble_api.c @@ -390,6 +390,19 @@ esp_err_t esp_ble_gap_set_device_name(const char *name) return esp_bt_dev_set_device_name(name); } +esp_err_t esp_ble_gap_get_device_name(void) +{ + btc_msg_t msg = {0}; + + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GAP_BLE; + msg.act = BTC_GAP_BLE_ACT_GET_DEV_NAME; + + return (btc_transfer_context(&msg, NULL, 0, NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + esp_err_t esp_ble_gap_get_local_used_addr(esp_bd_addr_t local_used_addr, uint8_t * addr_type) { if(esp_bluedroid_get_status() != (ESP_BLUEDROID_STATUS_ENABLED)) { diff --git a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h index f3dfaa9505..0fb0f119e4 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h @@ -167,6 +167,7 @@ typedef enum { ESP_GAP_BLE_UPDATE_WHITELIST_COMPLETE_EVT, /*!< When adding or removing whitelist complete, the event comes */ ESP_GAP_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_COMPLETE_EVT, /*!< When updating duplicate exceptional list complete, the event comes */ ESP_GAP_BLE_SET_CHANNELS_EVT, /*!< When setting BLE channels complete, the event comes */ + ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT, /*!< When getting BT device name complete, the event comes */ ESP_GAP_BLE_EVT_MAX, } esp_gap_ble_cb_event_t; @@ -636,6 +637,13 @@ typedef uint8_t esp_duplicate_info_t[ESP_BD_ADDR_LEN]; * @brief Gap callback parameters union */ typedef union { + /** + * @brief ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT + */ + struct ble_get_dev_name_cmpl_evt_param { + esp_bt_status_t status; /*!< Indicate the get device name success status */ + char *name; /*!< Name of bluetooth device */ + } get_dev_name_cmpl; /*!< Event parameter of ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT */ /** * @brief ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT */ @@ -1033,7 +1041,17 @@ esp_err_t esp_ble_gap_set_prefer_conn_params(esp_bd_addr_t bd_addr, esp_err_t esp_ble_gap_set_device_name(const char *name); /** - * @brief This function is called to get local used address and adress type. + * @brief Get device name of the local device + * + * @return + * - ESP_OK : success + * - other : failed + * + */ +esp_err_t esp_ble_gap_get_device_name(void); + +/** + * @brief This function is called to get local used address and address type. * uint8_t *esp_bt_dev_get_address(void) get the public address * * @param[in] local_used_addr - current local used ble address (six bytes) diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c index bb9656b3f6..dbda50189f 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c @@ -666,6 +666,27 @@ void bta_dm_set_dev_name (tBTA_DM_MSG *p_data) #endif /// CLASSIC_BT_INCLUDED } +/******************************************************************************* +** +** Function bta_dm_get_dev_name +** +** Description Gets local device name +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_get_dev_name (tBTA_DM_MSG *p_data) +{ + tBTM_STATUS status; + char *name = NULL; + + status = BTM_ReadLocalDeviceName(&name); + if (p_data->get_name.p_cback) { + (*p_data->get_name.p_cback)(status, name); + } +} + /******************************************************************************* ** ** Function bta_dm_set_afh_channels diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_api.c b/components/bt/host/bluedroid/bta/dm/bta_dm_api.c index 07284ff155..6cad02c5e3 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_api.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_api.c @@ -181,6 +181,27 @@ void BTA_DmSetDeviceName(const char *p_name) } } +/******************************************************************************* +** +** Function BTA_DmGetDeviceName +** +** Description This function gets the Bluetooth name of local device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmGetDeviceName(tBTA_GET_DEV_NAME_CBACK *p_cback) +{ + tBTA_DM_API_GET_NAME *p_msg; + + if ((p_msg = (tBTA_DM_API_GET_NAME *) osi_malloc(sizeof(tBTA_DM_API_GET_NAME))) != NULL) { + p_msg->hdr.event = BTA_DM_API_GET_NAME_EVT; + p_msg->p_cback = p_cback; + bta_sys_sendmsg(p_msg); + } +} + void BTA_DmConfigEir(tBTA_DM_EIR_CONF *eir_config) { tBTA_DM_API_CONFIG_EIR *p_msg; diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_main.c b/components/bt/host/bluedroid/bta/dm/bta_dm_main.c index e543192325..73af5714f5 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_main.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_main.c @@ -57,6 +57,7 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = { bta_dm_enable, /* BTA_DM_API_ENABLE_EVT */ bta_dm_disable, /* BTA_DM_API_DISABLE_EVT */ bta_dm_set_dev_name, /* BTA_DM_API_SET_NAME_EVT */ + bta_dm_get_dev_name, /* BTA_DM_API_GET_NAME_EVT */ bta_dm_config_eir, /* BTA_DM_API_CONFIG_EIR_EVT */ bta_dm_set_afh_channels, /* BTA_DM_API_SET_AFH_CHANNELS_EVT */ #if (SDP_INCLUDED == TRUE) diff --git a/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h b/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h index 25f39fe750..c42955bb6c 100644 --- a/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h +++ b/components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h @@ -52,6 +52,7 @@ enum { BTA_DM_API_ENABLE_EVT = BTA_SYS_EVT_START(BTA_ID_DM), BTA_DM_API_DISABLE_EVT, BTA_DM_API_SET_NAME_EVT, + BTA_DM_API_GET_NAME_EVT, BTA_DM_API_CONFIG_EIR_EVT, BTA_DM_API_SET_AFH_CHANNELS_EVT, #if (SDP_INCLUDED == TRUE) @@ -201,6 +202,11 @@ typedef struct { BD_NAME name; /* max 248 bytes name, plus must be Null terminated */ } tBTA_DM_API_SET_NAME; +typedef struct { + BT_HDR hdr; + tBTA_GET_DEV_NAME_CBACK *p_cback; +} tBTA_DM_API_GET_NAME; + /* data type for BTA_DM_API_CONFIG_EIR_EVT */ typedef struct { BT_HDR hdr; @@ -848,6 +854,7 @@ typedef union { tBTA_DM_API_ENABLE enable; tBTA_DM_API_SET_NAME set_name; + tBTA_DM_API_GET_NAME get_name; tBTA_DM_API_CONFIG_EIR config_eir; tBTA_DM_API_SET_AFH_CHANNELS set_afh_channels; @@ -1318,6 +1325,7 @@ extern void bta_dm_search_sm_disable( void ); extern void bta_dm_enable (tBTA_DM_MSG *p_data); extern void bta_dm_disable (tBTA_DM_MSG *p_data); extern void bta_dm_set_dev_name (tBTA_DM_MSG *p_data); +extern void bta_dm_get_dev_name (tBTA_DM_MSG *p_data); extern void bta_dm_config_eir (tBTA_DM_MSG *p_data); extern void bta_dm_set_afh_channels (tBTA_DM_MSG *p_data); extern void bta_dm_read_rmt_name(tBTA_DM_MSG *p_data); diff --git a/components/bt/host/bluedroid/bta/include/bta/bta_api.h b/components/bt/host/bluedroid/bta/include/bta/bta_api.h index 7b7c6cee72..d6ce228a59 100644 --- a/components/bt/host/bluedroid/bta/include/bta/bta_api.h +++ b/components/bt/host/bluedroid/bta/include/bta/bta_api.h @@ -176,6 +176,8 @@ typedef struct { typedef UINT16 tBTA_SEC; +typedef tBTM_GET_DEV_NAME_CBACK tBTA_GET_DEV_NAME_CBACK; + /* Ignore for Discoverable, Connectable, Pairable and Connectable Paired only device modes */ #define BTA_DM_IGNORE 0x00FF @@ -1474,6 +1476,18 @@ extern void BTA_DisableTestMode(void); *******************************************************************************/ extern void BTA_DmSetDeviceName(const char *p_name); +/******************************************************************************* +** +** Function BTA_DmGetDeviceName +** +** Description This function gets the Bluetooth name of the local device. +** +** +** Returns void +** +*******************************************************************************/ +extern void BTA_DmGetDeviceName(tBTA_GET_DEV_NAME_CBACK *p_cback); + /******************************************************************************* ** ** Function BTA_DmGetRemoteName diff --git a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c index 105fc7d619..3ff1c8ae42 100644 --- a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -54,6 +54,32 @@ static inline void btc_gap_ble_cb_to_app(esp_gap_ble_cb_event_t event, esp_ble_g } } +static void btc_gap_ble_get_dev_name_callback(UINT8 status, char *name) +{ + esp_ble_gap_cb_param_t param; + bt_status_t ret; + btc_msg_t msg = {0}; + + memset(¶m, 0, sizeof(esp_ble_gap_cb_param_t)); + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_GAP_BLE; + msg.act = ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT; + + param.get_dev_name_cmpl.status = btc_btm_status_to_esp_status(status); + param.get_dev_name_cmpl.name = (char *)osi_malloc(BTC_MAX_LOC_BD_NAME_LEN + 1); + if (param.get_dev_name_cmpl.name) { + BCM_STRNCPY_S(param.get_dev_name_cmpl.name, name, BTC_MAX_LOC_BD_NAME_LEN + 1); + } else { + param.get_dev_name_cmpl.status = ESP_BT_STATUS_NOMEM; + } + + ret = btc_transfer_context(&msg, ¶m, sizeof(esp_ble_gap_cb_param_t), NULL, NULL); + if (ret != BT_STATUS_SUCCESS) { + BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__); + } +} + static void btc_gap_adv_point_cleanup(void **buf) { if (NULL == *buf) { @@ -1096,6 +1122,13 @@ void btc_gap_ble_cb_deep_free(btc_msg_t *msg) { BTC_TRACE_DEBUG("%s", __func__); switch (msg->act) { + case ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT: { + char *value = ((esp_ble_gap_cb_param_t *)msg->arg)->get_dev_name_cmpl.name; + if (value) { + osi_free(value); + } + break; + } default: BTC_TRACE_DEBUG("Unhandled deep free %d", msg->act); break; @@ -1175,6 +1208,9 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) case BTC_GAP_BLE_ACT_SET_DEV_NAME: BTA_DmSetDeviceName(arg->set_dev_name.device_name); break; + case BTC_GAP_BLE_ACT_GET_DEV_NAME: + BTA_DmGetDeviceName(btc_gap_ble_get_dev_name_callback); + 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/host/bluedroid/btc/profile/std/include/btc_gap_ble.h b/components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h index 54336f1a3d..f27606d10c 100644 --- a/components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h +++ b/components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h @@ -58,6 +58,7 @@ typedef enum { BTC_GAP_BLE_OOB_REQ_REPLY_EVT, BTC_GAP_BLE_UPDATE_DUPLICATE_SCAN_EXCEPTIONAL_LIST, BTC_GAP_BLE_SET_AFH_CHANNELS, + BTC_GAP_BLE_ACT_GET_DEV_NAME, } btc_gap_ble_act_t; /* btc_ble_gap_args_t */ diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index 2675f76f5e..818bc533f1 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -788,6 +788,7 @@ '0' disables storage of the local name in BTM */ #ifndef BTM_MAX_LOC_BD_NAME_LEN #define BTM_MAX_LOC_BD_NAME_LEN 64 +#define BTC_MAX_LOC_BD_NAME_LEN BTM_MAX_LOC_BD_NAME_LEN #endif /* Fixed Default String. When this is defined as null string, the device's diff --git a/components/bt/host/bluedroid/stack/include/stack/btm_api.h b/components/bt/host/bluedroid/stack/include/stack/btm_api.h index 8545b9e584..60845ac77b 100644 --- a/components/bt/host/bluedroid/stack/include/stack/btm_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/btm_api.h @@ -156,6 +156,7 @@ typedef enum{ typedef void (tBTM_DEV_STATUS_CB) (tBTM_DEV_STATUS status); +typedef void (tBTM_GET_DEV_NAME_CBACK) (UINT8 status, char *name); /* Callback function for when a vendor specific event occurs. The length and ** array of returned parameter bytes are included. This asynchronous event