From 814bcf40210a418d3816462c3f938d86a0f70cff 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 | 40 +++++++++++++++---- .../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 | 37 +++++++++++++++++ .../btc/profile/std/include/btc_gap_ble.h | 1 + .../bluedroid/stack/include/stack/btm_api.h | 1 + 10 files changed, 149 insertions(+), 8 deletions(-) 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 deafadb428..b25e4c97d1 100644 --- a/components/bt/host/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/host/bluedroid/api/esp_gap_ble_api.c @@ -391,6 +391,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 ec7b848d72..5e4b21e83c 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 @@ -135,7 +135,7 @@ typedef uint8_t esp_ble_io_cap_t; /*!< combination of the io capab /// GAP BLE callback event type typedef enum { -#if (BLE_42_FEATURE_SUPPORT == TRUE) + //BLE_42_FEATURE_SUPPORT ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT = 0, /*!< When advertising data set complete, the event comes */ ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT, /*!< When scan response data set complete, the event comes */ ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT, /*!< When scan parameters set complete, the event comes */ @@ -144,7 +144,7 @@ typedef enum { ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT, /*!< When raw advertising data set complete, the event comes */ ESP_GAP_BLE_ADV_START_COMPLETE_EVT, /*!< When start advertising complete, the event comes */ ESP_GAP_BLE_SCAN_START_COMPLETE_EVT, /*!< When start scan complete, the event comes */ -#endif // #if (BLE_42_FEATURE_SUPPORT == TRUE) + //BLE_INCLUDED ESP_GAP_BLE_AUTH_CMPL_EVT = 8, /*!< Authentication complete indication. */ ESP_GAP_BLE_KEY_EVT, /*!< BLE key event for peer device keys */ ESP_GAP_BLE_SEC_REQ_EVT, /*!< BLE security request */ @@ -154,10 +154,10 @@ typedef enum { ESP_GAP_BLE_LOCAL_IR_EVT, /*!< BLE local IR (identity Root 128-bit random static value used to generate Long Term Key) event */ ESP_GAP_BLE_LOCAL_ER_EVT, /*!< BLE local ER (Encryption Root vakue used to genrate identity resolving key) event */ ESP_GAP_BLE_NC_REQ_EVT, /*!< Numeric Comparison request event */ -#if (BLE_42_FEATURE_SUPPORT == TRUE) + //BLE_42_FEATURE_SUPPORT ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT, /*!< When stop adv complete, the event comes */ ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT, /*!< When stop scan complete, the event comes */ -#endif // #if (BLE_42_FEATURE_SUPPORT == TRUE) + //BLE_INCLUDED ESP_GAP_BLE_SET_STATIC_RAND_ADDR_EVT = 19, /*!< When set the static rand address complete, the event comes */ ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT, /*!< When update connection parameters complete, the event comes */ ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT, /*!< When set pkt length complete, the event comes */ @@ -167,11 +167,11 @@ typedef enum { ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT, /*!< When get the bond device list complete, the event comes */ ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT, /*!< When read the rssi complete, the event comes */ ESP_GAP_BLE_UPDATE_WHITELIST_COMPLETE_EVT, /*!< When add or remove whitelist complete, the event comes */ -#if (BLE_42_FEATURE_SUPPORT == TRUE) + //BLE_42_FEATURE_SUPPORT ESP_GAP_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_COMPLETE_EVT, /*!< When update duplicate exceptional list complete, the event comes */ -#endif //#if (BLE_42_FEATURE_SUPPORT == TRUE) + //BLE_INCLUDED ESP_GAP_BLE_SET_CHANNELS_EVT = 29, /*!< When setting BLE channels complete, the event comes */ -#if (BLE_50_FEATURE_SUPPORT == TRUE) + //BLE_50_FEATURE_SUPPORT ESP_GAP_BLE_READ_PHY_COMPLETE_EVT, /*!< when reading phy complete, this event comes */ ESP_GAP_BLE_SET_PREFERED_DEFAULT_PHY_COMPLETE_EVT, /*!< when preferred default phy complete, this event comes */ ESP_GAP_BLE_SET_PREFERED_PHY_COMPLETE_EVT, /*!< when preferred phy complete , this event comes */ @@ -206,7 +206,10 @@ typedef enum { ESP_GAP_BLE_PERIODIC_ADV_REPORT_EVT, /*!< when periodic report advertising complete, the event comes */ ESP_GAP_BLE_PERIODIC_ADV_SYNC_LOST_EVT, /*!< when periodic advertising sync lost complete, the event comes */ ESP_GAP_BLE_PERIODIC_ADV_SYNC_ESTAB_EVT, /*!< when periodic advertising sync establish complete, the event comes */ -#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE) + //BLE_INCLUDED + ESP_GAP_BLE_SC_OOB_REQ_EVT, /*!< Secure Connection OOB request event */ + ESP_GAP_BLE_SC_CR_LOC_OOB_EVT, /*!< Secure Connection create OOB data complete event */ + ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT, /*!< When getting BT device name complete, the event comes */ ESP_GAP_BLE_EVT_MAX, /*!< when maximum advertising event complete, the event comes */ } esp_gap_ble_cb_event_t; @@ -910,6 +913,13 @@ typedef struct { * @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 */ #if (BLE_42_FEATURE_SUPPORT == TRUE) /** * @brief ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT @@ -1537,7 +1547,21 @@ 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); /** +<<<<<<< HEAD * @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. +>>>>>>> 708ca0181c... bluedroid: support get bluetooth device name * 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 78915cc9ce..43076a1ab3 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c @@ -673,6 +673,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 da63ae4c98..420facef9d 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); + } +} + #if (CLASSIC_BT_INCLUDED == TRUE) void BTA_DmConfigEir(tBTA_DM_EIR_CONF *eir_config) 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 b95a793e23..b8c9ca6e3a 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 */ #if (CLASSIC_BT_INCLUDED == TRUE) bta_dm_config_eir, /* BTA_DM_API_CONFIG_EIR_EVT */ #endif 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 4da7185f2c..6b15a56972 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 @@ -53,6 +53,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, #if (CLASSIC_BT_INCLUDED == TRUE) BTA_DM_API_CONFIG_EIR_EVT, #endif @@ -228,6 +229,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; @@ -1014,6 +1020,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; @@ -1507,6 +1514,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); #if (CLASSIC_BT_INCLUDED == TRUE) extern void bta_dm_config_eir (tBTA_DM_MSG *p_data); #endif 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 0a166100ea..51cee42959 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 @@ -1672,6 +1674,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 01c013beee..26ff138668 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 @@ -70,6 +70,33 @@ static inline void btc_gap_ble_cb_to_app(esp_gap_ble_cb_event_t event, esp_ble_g btc_gap_ble_cb(event, param); } } + +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__); + } +} + #if (BLE_42_FEATURE_SUPPORT == TRUE) static void btc_gap_adv_point_cleanup(void **buf) { @@ -1473,6 +1500,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; @@ -1559,6 +1593,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; #if (BLE_42_FEATURE_SUPPORT == TRUE) case BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW: btc_ble_set_adv_data_raw(arg->cfg_adv_data_raw.raw_adv, 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 37679d8dea..3721f3dccb 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 @@ -91,6 +91,7 @@ typedef enum { BTC_GAP_BLE_STOP_EXT_SCAN, BTC_GAP_BLE_SET_EXT_PEFER_CONNET_PARAMS, #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE) + BTC_GAP_BLE_ACT_GET_DEV_NAME, } btc_gap_ble_act_t; /* btc_ble_gap_args_t */ 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 a987749246..a70714d02c 100644 --- a/components/bt/host/bluedroid/stack/include/stack/btm_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/btm_api.h @@ -157,6 +157,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