From 819e2ecb84a22d6e03ec9ed67b3260c0dd7e8aba Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Mon, 10 Jul 2017 09:56:09 -0700 Subject: [PATCH] Use one type for UUID (1/5) Currently, we have few different representations for UUID in stack: tBT_UUID, tSDP_UUID, bt_uuid_t, bluetooth:UUID, or uint8_t*. Additionally, tBT_UUID and bt_uuid_t are used to hold UUID as 128bit as Little Endian or Big Endian, depending on which part of stack (GATT or SDP) is using it. This patch is creating one type, bluetooth::Uuid, that will replace all other types. Bug: 66912853 Test: all sl4a tests for GATT and RFCOMM Merged-In: Ia42d3233146db0488728ed6f878f99b368fe8838 Change-Id: Ia42d3233146db0488728ed6f878f99b368fe8838 --- bta/ag/bta_ag_sdp.cc | 13 +- bta/dm/bta_dm_act.cc | 160 ++++++------ bta/dm/bta_dm_api.cc | 33 +-- bta/dm/bta_dm_int.h | 12 +- bta/gatt/bta_gattc_act.cc | 27 +-- bta/gatt/bta_gattc_api.cc | 30 +-- bta/gatt/bta_gattc_cache.cc | 201 ++++++++-------- bta/gatt/bta_gattc_int.h | 16 +- bta/gatt/bta_gattc_main.cc | 8 +- bta/gatt/bta_gattc_utils.cc | 70 ------ bta/gatt/bta_gatts_act.cc | 10 +- bta/gatt/bta_gatts_api.cc | 11 +- bta/gatt/bta_gatts_int.h | 7 +- bta/gatt/bta_gatts_utils.cc | 61 ----- bta/hf_client/bta_hf_client_sdp.cc | 10 +- bta/hh/bta_hh_le.cc | 60 ++--- bta/hl/bta_hl_act.cc | 6 +- bta/include/bta_api.h | 9 +- bta/include/bta_gatt_api.h | 34 +-- bta/include/bta_jv_api.h | 3 +- bta/include/bta_sdp_api.h | 6 +- bta/jv/bta_jv_act.cc | 48 +--- bta/jv/bta_jv_api.cc | 6 +- bta/jv/bta_jv_int.h | 4 +- bta/mce/bta_mce_act.cc | 12 +- bta/sdp/bta_sdp_act.cc | 169 +++++-------- bta/sdp/bta_sdp_api.cc | 6 +- bta/sdp/bta_sdp_int.h | 2 +- btcore/Android.bp | 2 - btcore/include/property.h | 7 +- btcore/include/uuid.h | 75 ------ btcore/src/property.cc | 15 +- btcore/src/uuid.cc | 163 ------------- btcore/test/property_test.cc | 26 +- btcore/test/uuid_test.cc | 162 ------------- btif/include/btif_api.h | 8 +- btif/include/btif_gatt_util.h | 9 +- btif/include/btif_sock_rfc.h | 8 +- btif/include/btif_sock_sdp.h | 31 +-- btif/include/btif_storage.h | 3 +- btif/include/btif_util.h | 10 - btif/src/bluetooth.cc | 3 +- btif/src/btif_ble_scanner.cc | 13 +- btif/src/btif_core.cc | 9 +- btif/src/btif_dm.cc | 74 ++---- btif/src/btif_gatt_client.cc | 41 ++-- btif/src/btif_gatt_server.cc | 20 +- btif/src/btif_gatt_test.cc | 57 ++--- btif/src/btif_gatt_util.cc | 152 +----------- btif/src/btif_sdp.cc | 18 +- btif/src/btif_sock.cc | 16 +- btif/src/btif_sock_rfc.cc | 53 ++-- btif/src/btif_sock_sdp.cc | 49 ++-- btif/src/btif_storage.cc | 57 +++-- btif/src/btif_util.cc | 81 ------- btif/test/btif_storage_test.cc | 28 +-- service/Android.bp | 3 +- service/bluetooth_instance.h | 10 +- service/client/main.cc | 25 +- service/common/Android.bp | 4 +- .../bluetooth/bluetooth_gatt_characteristic.cc | 2 +- .../android/bluetooth/bluetooth_gatt_descriptor.cc | 2 +- .../bluetooth/bluetooth_gatt_included_service.cc | 2 +- .../bluetooth/bluetooth_gatt_included_service.h | 6 +- .../android/bluetooth/bluetooth_gatt_service.cc | 2 +- service/common/android/bluetooth/scan_filter.cc | 6 +- service/common/android/bluetooth/uuid.cc | 7 +- service/common/android/bluetooth/uuid.h | 13 +- service/common/bluetooth/advertise_data.h | 2 +- service/common/bluetooth/characteristic.h | 6 +- service/common/bluetooth/descriptor.h | 6 +- service/common/bluetooth/low_energy_constants.h | 12 +- service/common/bluetooth/scan_filter.cc | 22 +- service/common/bluetooth/scan_filter.h | 22 +- service/common/bluetooth/service.h | 6 +- service/common/bluetooth/uuid.cc | 160 ------------ service/common/bluetooth/uuid.h | 109 --------- service/example/heart_rate/constants.h | 11 +- service/example/heart_rate/heart_rate_server.cc | 42 ++-- service/gatt_client.cc | 19 +- service/gatt_client.h | 14 +- service/gatt_server.cc | 37 +-- service/gatt_server.h | 16 +- service/gatt_server_old.cc | 67 +++--- service/gatt_server_old.h | 16 +- service/hal/bluetooth_gatt_interface.cc | 8 +- service/hal/bluetooth_gatt_interface.h | 4 +- service/hal/fake_bluetooth_gatt_interface.cc | 8 +- service/hal/fake_bluetooth_gatt_interface.h | 18 +- service/ipc/binder/bluetooth_binder_server.h | 2 +- .../ipc/binder/interface_with_instances_base.cc | 35 +-- service/ipc/binder/interface_with_instances_base.h | 8 +- service/ipc/linux_ipc_host.cc | 35 +-- service/ipc/linux_ipc_host.h | 2 +- service/low_energy_advertiser.cc | 12 +- service/low_energy_advertiser.h | 14 +- service/low_energy_client.cc | 21 +- service/low_energy_client.h | 14 +- service/low_energy_scanner.cc | 18 +- service/low_energy_scanner.h | 14 +- service/test/gatt_client_unittest.cc | 27 +-- service/test/gatt_server_unittest.cc | 53 ++-- service/test/low_energy_advertiser_unittest.cc | 42 ++-- service/test/low_energy_client_unittest.cc | 34 ++- service/test/low_energy_scanner_unittest.cc | 20 +- service/test/parcelable_unittest.cc | 46 ++-- service/test/uuid_unittest.cc | 160 ------------ stack/a2dp/a2dp_api.cc | 8 +- stack/avrc/avrc_sdp.cc | 7 +- stack/bnep/bnep_api.cc | 35 ++- stack/bnep/bnep_int.h | 9 +- stack/bnep/bnep_main.cc | 9 +- stack/bnep/bnep_utils.cc | 145 +++++------ stack/btm/btm_ble_adv_filter.cc | 45 ++-- stack/btm/btm_inq.cc | 57 ++--- stack/gap/gap_ble.cc | 23 +- stack/gatt/att_protocol.cc | 5 +- stack/gatt/gatt_api.cc | 140 +++-------- stack/gatt/gatt_attr.cc | 22 +- stack/gatt/gatt_cl.cc | 79 +++--- stack/gatt/gatt_db.cc | 244 +++++++------------ stack/gatt/gatt_int.h | 54 ++--- stack/gatt/gatt_sr.cc | 126 +++++----- stack/gatt/gatt_utils.cc | 268 ++++----------------- stack/hid/hidh_api.cc | 13 +- stack/include/bnep_api.h | 10 +- stack/include/bt_types.h | 17 -- stack/include/btm_api.h | 3 +- stack/include/btm_ble_api.h | 2 +- stack/include/btm_ble_api_types.h | 2 +- stack/include/gatt_api.h | 25 +- stack/include/sdp_api.h | 21 +- stack/pan/pan_api.cc | 40 ++- stack/pan/pan_int.h | 3 +- stack/pan/pan_main.cc | 149 ++++-------- stack/rfcomm/port_api.cc | 20 +- stack/sdp/sdp_api.cc | 54 ++--- stack/sdp/sdp_discovery.cc | 25 +- stack/sdp/sdp_utils.cc | 85 ++----- stack/sdp/sdpint.h | 18 +- stack/srvc/srvc_battery.cc | 21 +- stack/srvc/srvc_dis.cc | 17 +- stack/srvc/srvc_eng.cc | 5 +- test/rootcanal/Android.bp | 1 + test/suite/gatt/gatt_test.cc | 4 +- test/suite/gatt/gatt_test.h | 4 +- test/suite/gatt/gatt_unittest.cc | 31 +-- test/suite/rfcomm/rfcomm_test.cc | 14 +- test/suite/rfcomm/rfcomm_test.h | 2 +- test/suite/rfcomm/rfcomm_unittest.cc | 6 +- tools/bdtool/adapter.c | 2 +- tools/bdtool/bdtool.c | 12 +- types/Android.bp | 2 + types/bluetooth/uuid.cc | 170 +++++++++++++ types/bluetooth/uuid.h | 134 +++++++++++ types/test/bluetooth/uuid_unittest.cc | 166 +++++++++++++ 156 files changed, 2189 insertions(+), 3633 deletions(-) delete mode 100644 btcore/include/uuid.h delete mode 100644 btcore/src/uuid.cc delete mode 100644 btcore/test/uuid_test.cc delete mode 100644 service/common/bluetooth/uuid.cc delete mode 100644 service/common/bluetooth/uuid.h delete mode 100644 service/test/uuid_unittest.cc create mode 100644 types/bluetooth/uuid.cc create mode 100644 types/bluetooth/uuid.h create mode 100644 types/test/bluetooth/uuid_unittest.cc diff --git a/bta/ag/bta_ag_sdp.cc b/bta/ag/bta_ag_sdp.cc index ae3c97391..2bfe145dd 100644 --- a/bta/ag/bta_ag_sdp.cc +++ b/bta/ag/bta_ag_sdp.cc @@ -35,6 +35,8 @@ #include "sdp_api.h" #include "utl.h" +using bluetooth::Uuid; + /* Number of protocol elements in protocol element list. */ #define BTA_AG_NUM_PROTO_ELEMS 2 @@ -374,7 +376,7 @@ bool bta_ag_sdp_find_attr(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) { * ******************************************************************************/ void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) { - tSDP_UUID uuid_list[1]; + Uuid uuid_list[1]; uint16_t num_uuid = 1; uint16_t attr_list[4]; uint8_t num_attr; @@ -387,7 +389,7 @@ void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) { attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST; attr_list[3] = ATTR_ID_SUPPORTED_FEATURES; num_attr = 4; - uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE; + uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_HF_HANDSFREE); } /* HFP acceptor; get features */ else if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_ACP) { @@ -395,7 +397,7 @@ void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) { attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST; attr_list[2] = ATTR_ID_SUPPORTED_FEATURES; num_attr = 3; - uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE; + uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_HF_HANDSFREE); } /* HSP initiator; get proto list */ else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT) { @@ -409,9 +411,9 @@ void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) { // UUID_SERVCLASS_HEADSET (0x1108) to store its service record. However, // most of such devices are HSP 1.0 devices. if (p_scb->hsp_version >= HSP_VERSION_1_2) { - uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HEADSET_HS; + uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_HEADSET_HS); } else { - uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HEADSET; + uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_HEADSET); } } /* HSP acceptor; no discovery */ @@ -422,7 +424,6 @@ void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) { /* allocate buffer for sdp database */ p_scb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BTA_AG_DISC_BUF_SIZE); /* set up service discovery database; attr happens to be attr_list len */ - uuid_list[0].len = LEN_UUID_16; db_inited = SDP_InitDiscoveryDb(p_scb->p_disc_db, BTA_AG_DISC_BUF_SIZE, num_uuid, uuid_list, num_attr, attr_list); diff --git a/bta/dm/bta_dm_act.cc b/bta/dm/bta_dm_act.cc index 088e871b7..9de0d2be7 100644 --- a/bta/dm/bta_dm_act.cc +++ b/bta/dm/bta_dm_act.cc @@ -52,6 +52,8 @@ #include "gap_api.h" #endif +using bluetooth::Uuid; + static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir, uint16_t eir_len); static void bta_dm_inq_cmpl_cb(void* p_result); @@ -1103,7 +1105,7 @@ void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG* p_data) { void bta_dm_search_start(tBTA_DM_MSG* p_data) { tBTM_INQUIRY_CMPL result; - size_t len = sizeof(tBT_UUID) * p_data->search.num_uuid; + size_t len = sizeof(Uuid) * p_data->search.num_uuid; bta_dm_gattc_register(); APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__, @@ -1125,8 +1127,8 @@ void bta_dm_search_start(tBTA_DM_MSG* p_data) { if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 && p_data->search.p_uuid != NULL) { - bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len); - memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->search.p_uuid, len); + bta_dm_search_cb.p_srvc_uuid = (Uuid*)osi_malloc(len); + *bta_dm_search_cb.p_srvc_uuid = *p_data->search.p_uuid; } result.status = BTM_StartInquiry((tBTM_INQ_PARMS*)&p_data->search.inq_params, bta_dm_inq_results_cb, @@ -1196,7 +1198,7 @@ void bta_dm_search_cancel(UNUSED_ATTR tBTA_DM_MSG* p_data) { * ******************************************************************************/ void bta_dm_discover(tBTA_DM_MSG* p_data) { - size_t len = sizeof(tBT_UUID) * p_data->discover.num_uuid; + size_t len = sizeof(Uuid) * p_data->discover.num_uuid; APPL_TRACE_EVENT("%s services_to_search=0x%04X, sdp_search=%d", __func__, p_data->discover.services, p_data->discover.sdp_search); @@ -1207,8 +1209,8 @@ void bta_dm_discover(tBTA_DM_MSG* p_data) { osi_free_and_reset((void**)&bta_dm_search_cb.p_srvc_uuid); if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 && p_data->discover.p_uuid != NULL) { - bta_dm_search_cb.p_srvc_uuid = (tBT_UUID*)osi_malloc(len); - memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->discover.p_uuid, len); + bta_dm_search_cb.p_srvc_uuid = (Uuid*)osi_malloc(len); + *bta_dm_search_cb.p_srvc_uuid = *p_data->discover.p_uuid; } bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid; @@ -1223,7 +1225,7 @@ void bta_dm_discover(tBTA_DM_MSG* p_data) { bta_dm_search_cb.transport = p_data->discover.transport; bta_dm_search_cb.name_discover_done = false; - memcpy(&bta_dm_search_cb.uuid, &p_data->discover.uuid, sizeof(tSDP_UUID)); + bta_dm_search_cb.uuid = p_data->discover.uuid; bta_dm_discover_device(p_data->discover.bd_addr); } @@ -1475,12 +1477,10 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data) { uint16_t service = 0xFFFF; tSDP_PROTOCOL_ELEM pe; - tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid; + Uuid* p_uuid = bta_dm_search_cb.p_srvc_uuid; tBTA_DM_SEARCH result; - tBT_UUID service_uuid; - uint32_t num_uuids = 0; - uint8_t uuid_list[32][MAX_UUID_SIZE]; // assuming a max of 32 services + std::vector uuid_list; if ((p_data->sdp_event.sdp_result == SDP_SUCCESS) || (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) || @@ -1490,7 +1490,7 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data) { p_sdp_rec = NULL; if (bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1)) { p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db, - &bta_dm_search_cb.uuid, p_sdp_rec); + bta_dm_search_cb.uuid, p_sdp_rec); if (p_sdp_rec && SDP_FindProtocolListElemInRec( p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) { @@ -1510,7 +1510,7 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data) { p_uuid += (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search); /* only support 16 bits UUID for now */ - service = p_uuid->uu.uuid16; + service = p_uuid->As16Bit(); } /* all GATT based services */ do { @@ -1518,14 +1518,14 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data) { p_sdp_rec = SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, 0, p_sdp_rec); if (p_sdp_rec) { + Uuid service_uuid; if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) { /* send result back to app now, one by one */ result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr; strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(), BD_NAME_LEN); - result.disc_ble_res.service.len = service_uuid.len; - result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16; + result.disc_ble_res.service = service_uuid; bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result); } } @@ -1540,16 +1540,14 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data) { bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) || (p_sdp_rec != NULL)) { if (service != UUID_SERVCLASS_PNP_INFORMATION) { - uint16_t tmp_svc = 0xFFFF; bta_dm_search_cb.services_found |= (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK( bta_dm_search_cb.service_index - 1)); - tmp_svc = + uint16_t tmp_svc = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1]; /* Add to the list of UUIDs */ - sdpu_uuid16_to_uuid128(tmp_svc, uuid_list[num_uuids]); - num_uuids++; + uuid_list.push_back(Uuid::From16Bit(tmp_svc)); } } } @@ -1575,14 +1573,14 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data) { if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) { p_sdp_rec = NULL; do { - tBT_UUID temp_uuid; /* find a service record, report it */ p_sdp_rec = SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec); if (p_sdp_rec) { + // SDP_FindServiceUUIDInRec_128bit is used only once, refactor? + Uuid temp_uuid; if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) { - memcpy(uuid_list[num_uuids], temp_uuid.uu.uuid128, MAX_UUID_SIZE); - num_uuids++; + uuid_list.push_back(temp_uuid); } } } while (p_sdp_rec); @@ -1603,13 +1601,15 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data) { p_msg->disc_result.result.disc_res.result = BTA_SUCCESS; p_msg->disc_result.result.disc_res.p_raw_data = NULL; p_msg->disc_result.result.disc_res.raw_data_size = 0; - p_msg->disc_result.result.disc_res.num_uuids = num_uuids; + p_msg->disc_result.result.disc_res.num_uuids = uuid_list.size(); p_msg->disc_result.result.disc_res.p_uuid_list = NULL; - if (num_uuids > 0) { + if (uuid_list.size() > 0) { + // TODO(jpawlowski): make p_uuid_list into vector, and just copy + // vectors, but first get rid of bta_sys_sendmsg below. p_msg->disc_result.result.disc_res.p_uuid_list = - (uint8_t*)osi_malloc(num_uuids * MAX_UUID_SIZE); - memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list, - num_uuids * MAX_UUID_SIZE); + (Uuid*)osi_malloc(uuid_list.size() * sizeof(Uuid)); + memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list.data(), + uuid_list.size() * sizeof(Uuid)); } // Copy the raw_data to the discovery result structure if (bta_dm_search_cb.p_sdp_db != NULL && @@ -1904,11 +1904,9 @@ void bta_dm_search_cancel_notify(UNUSED_ATTR tBTA_DM_MSG* p_data) { * ******************************************************************************/ static void bta_dm_find_services(const RawAddress& bd_addr) { - tSDP_UUID uuid; - - memset(&uuid, 0, sizeof(tSDP_UUID)); while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) { + Uuid uuid = Uuid::kEmpty; if (bta_dm_search_cb.services_to_search & (tBTA_SERVICE_MASK)( BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) { @@ -1921,10 +1919,10 @@ static void bta_dm_find_services(const RawAddress& bd_addr) { LOG_INFO(LOG_TAG, "%s services_to_search=%08x", __func__, bta_dm_search_cb.services_to_search); if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) { - uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[0]; + uuid = Uuid::From16Bit(bta_service_id_to_uuid_lkup_tbl[0]); bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK; } else { - uuid.uu.uuid16 = UUID_PROTOCOL_L2CAP; + uuid = Uuid::From16Bit(UUID_PROTOCOL_L2CAP); bta_dm_search_cb.services_to_search = 0; } } else { @@ -1932,15 +1930,14 @@ static void bta_dm_find_services(const RawAddress& bd_addr) { if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) { if (bta_dm_search_cb.uuid_to_search > 0 && bta_dm_search_cb.p_srvc_uuid) { - memcpy(&uuid, (const void*)(bta_dm_search_cb.p_srvc_uuid + - bta_dm_search_cb.num_uuid - - bta_dm_search_cb.uuid_to_search), - sizeof(tBT_UUID)); + uuid = *(bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid - + bta_dm_search_cb.uuid_to_search); bta_dm_search_cb.uuid_to_search--; } else { - uuid.uu.uuid16 = - bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index]; + uuid = Uuid::From16Bit( + bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb + .service_index]); } /* last one? clear the BLE service bit if all discovery has been done @@ -1954,18 +1951,17 @@ static void bta_dm_find_services(const RawAddress& bd_addr) { /* remove the service from services to be searched */ bta_dm_search_cb.services_to_search &= (tBTA_SERVICE_MASK)(~( BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))); - uuid.uu.uuid16 = - bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index]; + uuid = Uuid::From16Bit( + bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index]); } } - if (uuid.len == 0) uuid.len = LEN_UUID_16; - if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) { - memcpy(&uuid, &bta_dm_search_cb.uuid, sizeof(tSDP_UUID)); + uuid = bta_dm_search_cb.uuid; } - LOG_INFO(LOG_TAG, "%s search UUID = %04x", __func__, uuid.uu.uuid16); + LOG_INFO(LOG_TAG, "%s search UUID = %s", __func__, + uuid.ToString().c_str()); SDP_InitDiscoveryDb(bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1, &uuid, 0, NULL); @@ -3461,16 +3457,16 @@ static void bta_dm_set_eir(char* local_name) { if (local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len) { /* get number of UUID 16-bit list */ #if (BTA_EIR_CANNED_UUID_LIST == TRUE) - num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16; + num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / Uuid::kNumBytes16; #else // BTA_EIR_CANNED_UUID_LIST - max_num_uuid = (free_eir_length - 2) / LEN_UUID_16; + max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes16; data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p, max_num_uuid, &num_uuid); p = (uint8_t*)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */ #endif // BTA_EIR_CANNED_UUID_LIST /* if UUID doesn't fit remaing space, shorten local name */ - if (local_name_len > (free_eir_length - 4 - num_uuid * LEN_UUID_16)) { + if (local_name_len > (free_eir_length - 4 - num_uuid * Uuid::kNumBytes16)) { local_name_len = find_utf8_char_boundary( local_name, p_bta_dm_eir_cfg->bta_dm_eir_min_name_len); APPL_TRACE_WARNING("%s local name is shortened (%d)", __func__, @@ -3494,23 +3490,24 @@ static void bta_dm_set_eir(char* local_name) { /* if UUID list is provided as static data in configuration */ if ((p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0) && (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) { - if (free_eir_length > LEN_UUID_16 + 2) { + if (free_eir_length > Uuid::kNumBytes16 + 2) { free_eir_length -= 2; if (free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) { - num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16; + num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / Uuid::kNumBytes16; data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE; } else /* not enough room for all UUIDs */ { APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated"); - num_uuid = free_eir_length / LEN_UUID_16; + num_uuid = free_eir_length / Uuid::kNumBytes16; data_type = BTM_EIR_MORE_16BITS_UUID_TYPE; } - UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1); + UINT8_TO_STREAM(p, num_uuid * Uuid::kNumBytes16 + 1); UINT8_TO_STREAM(p, data_type); - memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16); - p += num_uuid * LEN_UUID_16; - free_eir_length -= num_uuid * LEN_UUID_16; + memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, + num_uuid * Uuid::kNumBytes16); + p += num_uuid * Uuid::kNumBytes16; + free_eir_length -= num_uuid * Uuid::kNumBytes16; } } #else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */ @@ -3520,7 +3517,7 @@ static void bta_dm_set_eir(char* local_name) { p_type = p++; num_uuid = 0; - max_num_uuid = (free_eir_length - 2) / LEN_UUID_16; + max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes16; data_type = BTM_GetEirSupportedServices(bta_dm_cb.eir_uuid, &p, max_num_uuid, &num_uuid); @@ -3532,10 +3529,10 @@ static void bta_dm_set_eir(char* local_name) { for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) { - if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) { + const Uuid& curr = bta_dm_cb.custom_uuid[custom_uuid_idx]; + if (curr.GetShortestRepresentationSize() == Uuid::kNumBytes16) { if (num_uuid < max_num_uuid) { - UINT16_TO_STREAM(p, - bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16); + UINT16_TO_STREAM(p, curr.As16Bit()); num_uuid++; } else { data_type = BTM_EIR_MORE_16BITS_UUID_TYPE; @@ -3547,9 +3544,9 @@ static void bta_dm_set_eir(char* local_name) { } #endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */ - UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1); + UINT8_TO_STREAM(p_length, num_uuid * Uuid::kNumBytes16 + 1); UINT8_TO_STREAM(p_type, data_type); - free_eir_length -= num_uuid * LEN_UUID_16 + 2; + free_eir_length -= num_uuid * Uuid::kNumBytes16 + 2; } #endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */ @@ -3561,13 +3558,14 @@ static void bta_dm_set_eir(char* local_name) { num_uuid = 0; data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE; - max_num_uuid = (free_eir_length - 2) / LEN_UUID_32; + max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes32; for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) { - if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) { + const Uuid& curr = bta_dm_cb.custom_uuid[custom_uuid_idx]; + if (curr.GetShortestRepresentationSize() == Uuid::kNumBytes32) { if (num_uuid < max_num_uuid) { - UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32); + UINT32_TO_STREAM(p, curr.As32Bit()); num_uuid++; } else { data_type = BTM_EIR_MORE_32BITS_UUID_TYPE; @@ -3577,9 +3575,9 @@ static void bta_dm_set_eir(char* local_name) { } } - UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1); + UINT8_TO_STREAM(p_length, num_uuid * Uuid::kNumBytes32 + 1); UINT8_TO_STREAM(p_type, data_type); - free_eir_length -= num_uuid * LEN_UUID_32 + 2; + free_eir_length -= num_uuid * Uuid::kNumBytes32 + 2; } /* Adding 128-bit UUID list */ @@ -3589,14 +3587,14 @@ static void bta_dm_set_eir(char* local_name) { num_uuid = 0; data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE; - max_num_uuid = (free_eir_length - 2) / LEN_UUID_128; + max_num_uuid = (free_eir_length - 2) / Uuid::kNumBytes128; for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) { - if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) { + const Uuid& curr = bta_dm_cb.custom_uuid[custom_uuid_idx]; + if (curr.GetShortestRepresentationSize() == Uuid::kNumBytes128) { if (num_uuid < max_num_uuid) { - ARRAY16_TO_STREAM(p, - bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128); + ARRAY16_TO_STREAM(p, curr.To128BitBE().data()); num_uuid++; } else { data_type = BTM_EIR_MORE_128BITS_UUID_TYPE; @@ -3606,9 +3604,9 @@ static void bta_dm_set_eir(char* local_name) { } } - UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1); + UINT8_TO_STREAM(p_length, num_uuid * Uuid::kNumBytes128 + 1); UINT8_TO_STREAM(p_type, data_type); - free_eir_length -= num_uuid * LEN_UUID_128 + 2; + free_eir_length -= num_uuid * Uuid::kNumBytes128 + 2; } #endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE \ )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */ @@ -4443,11 +4441,8 @@ static void bta_dm_gattc_register(void) { * ******************************************************************************/ static void btm_dm_start_disc_gatt_services(uint16_t conn_id) { - tBT_UUID* p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid - - bta_dm_search_cb.uuid_to_search; - - p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid - - bta_dm_search_cb.uuid_to_search; + Uuid* p_uuid = bta_dm_search_cb.p_srvc_uuid + bta_dm_search_cb.num_uuid - + bta_dm_search_cb.uuid_to_search; /* always search for all services */ BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid); @@ -4474,11 +4469,14 @@ static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) { if (bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) < bta_dm_search_cb.ble_raw_size) { APPL_TRACE_DEBUG( - "ADDING BLE SERVICE uuid=0x%x, ble_ptr = 0x%x, ble_raw_used = 0x%x", - service_id.uuid.uu.uuid16, bta_dm_search_cb.p_ble_rawdata, + "ADDING BLE SERVICE uuid=%s, ble_ptr = 0x%x, ble_raw_used = 0x%x", + service_id.uuid.ToString().c_str(), bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used); if (bta_dm_search_cb.p_ble_rawdata) { + // TODO(jpawlowski): the p_ble_raw data is only sent to btif_dm.cc, but is + // never used there. Get rid of this code completly, or implement the + // TODOs from btif_dm.cc memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used), &service_id, sizeof(service_id)); @@ -4494,14 +4492,14 @@ static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) { __func__, bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used); } - LOG_INFO(LOG_TAG, "%s service_id_uuid_len=%d ", __func__, - service_id.uuid.len); + LOG_INFO(LOG_TAG, "%s service_id_uuid_len=%zu", __func__, + service_id.uuid.GetShortestRepresentationSize()); if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) { /* send result back to app now, one by one */ result.disc_ble_res.bd_addr = bta_dm_search_cb.peer_bdaddr; strlcpy((char*)result.disc_ble_res.bd_name, bta_dm_get_remname(), BD_NAME_LEN); - memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID)); + result.disc_ble_res.service = service_id.uuid; bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result); } diff --git a/bta/dm/bta_dm_api.cc b/bta/dm/bta_dm_api.cc index c06de6050..92688c243 100644 --- a/bta/dm/bta_dm_api.cc +++ b/bta/dm/bta_dm_api.cc @@ -35,6 +35,8 @@ #include "osi/include/osi.h" #include "utl.h" +using bluetooth::Uuid; + /***************************************************************************** * Constants ****************************************************************************/ @@ -260,7 +262,7 @@ void BTA_DmDiscover(const RawAddress& bd_addr, tBTA_SERVICE_MASK services, * Returns void * ******************************************************************************/ -void BTA_DmDiscoverUUID(const RawAddress& bd_addr, tSDP_UUID* uuid, +void BTA_DmDiscoverUUID(const RawAddress& bd_addr, const Uuid& uuid, tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) { tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER)); @@ -273,8 +275,7 @@ void BTA_DmDiscoverUUID(const RawAddress& bd_addr, tSDP_UUID* uuid, p_msg->num_uuid = 0; p_msg->p_uuid = NULL; - - memcpy(&p_msg->uuid, uuid, sizeof(tSDP_UUID)); + p_msg->uuid = uuid; bta_sys_sendmsg(p_msg); } @@ -500,11 +501,11 @@ void BTA_GetEirService(uint8_t* p_eir, size_t eir_len, tBTA_SERVICE_MASK* p_services) { uint8_t xx, yy; uint8_t num_uuid, max_num_uuid = 32; - uint8_t uuid_list[32 * LEN_UUID_16]; + uint8_t uuid_list[32 * Uuid::kNumBytes16]; uint16_t* p_uuid16 = (uint16_t*)uuid_list; tBTA_SERVICE_MASK mask; - BTM_GetEirUuidList(p_eir, eir_len, LEN_UUID_16, &num_uuid, uuid_list, + BTM_GetEirUuidList(p_eir, eir_len, Uuid::kNumBytes16, &num_uuid, uuid_list, max_num_uuid); for (xx = 0; xx < num_uuid; xx++) { mask = 1; @@ -815,9 +816,10 @@ static void bta_dm_discover_send_msg(const RawAddress& bd_addr, tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search, tBTA_TRANSPORT transport) { - const size_t len = p_services ? (sizeof(tBTA_DM_API_DISCOVER) + - sizeof(tBT_UUID) * p_services->num_uuid) - : sizeof(tBTA_DM_API_DISCOVER); + const size_t len = + p_services + ? (sizeof(tBTA_DM_API_DISCOVER) + sizeof(Uuid) * p_services->num_uuid) + : sizeof(tBTA_DM_API_DISCOVER); tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_calloc(len); p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT; @@ -830,9 +832,9 @@ static void bta_dm_discover_send_msg(const RawAddress& bd_addr, p_msg->services = p_services->srvc_mask; p_msg->num_uuid = p_services->num_uuid; if (p_services->num_uuid != 0) { - p_msg->p_uuid = (tBT_UUID*)(p_msg + 1); + p_msg->p_uuid = (Uuid*)(p_msg + 1); memcpy(p_msg->p_uuid, p_services->p_uuid, - sizeof(tBT_UUID) * p_services->num_uuid); + sizeof(Uuid) * p_services->num_uuid); } } @@ -906,9 +908,10 @@ void BTA_DmDiscoverExt(const RawAddress& bd_addr, ******************************************************************************/ void BTA_DmSearchExt(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK_EXT* p_services, tBTA_DM_SEARCH_CBACK* p_cback) { - const size_t len = p_services ? (sizeof(tBTA_DM_API_SEARCH) + - sizeof(tBT_UUID) * p_services->num_uuid) - : sizeof(tBTA_DM_API_SEARCH); + const size_t len = + p_services + ? (sizeof(tBTA_DM_API_SEARCH) + sizeof(Uuid) * p_services->num_uuid) + : sizeof(tBTA_DM_API_SEARCH); tBTA_DM_API_SEARCH* p_msg = (tBTA_DM_API_SEARCH*)osi_calloc(len); p_msg->hdr.event = BTA_DM_API_SEARCH_EVT; @@ -921,9 +924,9 @@ void BTA_DmSearchExt(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK_EXT* p_services, p_msg->num_uuid = p_services->num_uuid; if (p_services->num_uuid != 0) { - p_msg->p_uuid = (tBT_UUID*)(p_msg + 1); + p_msg->p_uuid = (Uuid*)(p_msg + 1); memcpy(p_msg->p_uuid, p_services->p_uuid, - sizeof(tBT_UUID) * p_services->num_uuid); + sizeof(Uuid) * p_services->num_uuid); } else { p_msg->p_uuid = NULL; } diff --git a/bta/dm/bta_dm_int.h b/bta/dm/bta_dm_int.h index 12a407457..5a4c3e8f4 100644 --- a/bta/dm/bta_dm_int.h +++ b/bta/dm/bta_dm_int.h @@ -151,7 +151,7 @@ typedef struct { tBTA_DM_SEARCH_CBACK* p_cback; tBTA_DM_RS_RES rs_res; uint8_t num_uuid; - tBT_UUID* p_uuid; + bluetooth::Uuid* p_uuid; } tBTA_DM_API_SEARCH; /* data type for BTA_DM_API_DISCOVER_EVT */ @@ -163,8 +163,8 @@ typedef struct { bool sdp_search; tBTA_TRANSPORT transport; uint8_t num_uuid; - tBT_UUID* p_uuid; - tSDP_UUID uuid; + bluetooth::Uuid* p_uuid; + bluetooth::Uuid uuid; } tBTA_DM_API_DISCOVER; /* data type for BTA_DM_API_DI_DISC_EVT */ @@ -641,7 +641,7 @@ typedef struct { /* store UUID list for EIR */ uint32_t eir_uuid[BTM_EIR_SERVICE_ARRAY_SIZE]; #if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) - tBT_UUID custom_uuid[BTA_EIR_SERVER_NUM_CUSTOM_UUID]; + bluetooth::Uuid custom_uuid[BTA_EIR_SERVER_NUM_CUSTOM_UUID]; #endif #endif @@ -669,7 +669,7 @@ typedef struct { cancel stored here */ bool wait_disc; bool sdp_results; - tSDP_UUID uuid; + bluetooth::Uuid uuid; uint8_t peer_scn; bool sdp_search; bool cancel_pending; /* inquiry cancel is pending */ @@ -677,7 +677,7 @@ typedef struct { tBTA_DM_SEARCH_CBACK* p_scan_cback; tBTA_GATTC_IF client_if; uint8_t num_uuid; - tBT_UUID* p_srvc_uuid; + bluetooth::Uuid* p_srvc_uuid; uint8_t uuid_to_search; bool gatt_disc_active; uint16_t conn_id; diff --git a/bta/gatt/bta_gattc_act.cc b/bta/gatt/bta_gattc_act.cc index d12d681ad..f9a90a85e 100644 --- a/bta/gatt/bta_gattc_act.cc +++ b/bta/gatt/bta_gattc_act.cc @@ -44,6 +44,8 @@ #include "bta_hh_int.h" #endif +using bluetooth::Uuid; + /***************************************************************************** * Constants ****************************************************************************/ @@ -169,7 +171,7 @@ void bta_gattc_start_if(uint8_t client_if) { } /** Register a GATT client application with BTA */ -void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_cback, +void bta_gattc_register(const Uuid& app_uuid, tBTA_GATTC_CBACK* p_cback, BtaAppRegisterCallback cb) { tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES; uint8_t client_if = 0; @@ -182,15 +184,14 @@ void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_cback, /* todo need to check duplicate uuid */ for (uint8_t i = 0; i < BTA_GATTC_CL_MAX; i++) { if (!bta_gattc_cb.cl_rcb[i].in_use) { - if ((p_app_uuid == NULL) || - (bta_gattc_cb.cl_rcb[i].client_if = - GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0) { + if ((bta_gattc_cb.cl_rcb[i].client_if = + GATT_Register(app_uuid, &bta_gattc_cl_cback)) == 0) { APPL_TRACE_ERROR("Register with GATT stack failed."); status = BTA_GATT_ERROR; } else { bta_gattc_cb.cl_rcb[i].in_use = true; bta_gattc_cb.cl_rcb[i].p_cback = p_cback; - memcpy(&bta_gattc_cb.cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID)); + bta_gattc_cb.cl_rcb[i].app_uuid = app_uuid; /* BTA use the same client interface as BTE GATT statck */ client_if = bta_gattc_cb.cl_rcb[i].client_if; @@ -1427,21 +1428,15 @@ bool bta_gattc_process_srvc_chg_ind(uint16_t conn_id, tBTA_GATTC_RCB* p_clrcb, tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_NOTIFY* p_notify, tGATT_VALUE* att_value) { - tBT_UUID gattp_uuid, srvc_chg_uuid; bool processed = false; - uint8_t i; - - gattp_uuid.len = 2; - gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER; - srvc_chg_uuid.len = 2; - srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD; + Uuid gattp_uuid = Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER); + Uuid srvc_chg_uuid = Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD); const tBTA_GATTC_CHARACTERISTIC* p_char = bta_gattc_get_characteristic_srcb(p_srcb, p_notify->handle); - if (p_char && - bta_gattc_uuid_compare(&p_char->service->uuid, &gattp_uuid, true) && - bta_gattc_uuid_compare(&p_char->uuid, &srvc_chg_uuid, true)) { + if (p_char && p_char->service->uuid == gattp_uuid && + p_char->uuid == srvc_chg_uuid) { if (att_value->len != BTA_GATTC_SERVICE_CHANGED_LEN) { APPL_TRACE_ERROR( "%s: received malformed service changed indication, skipping", @@ -1466,7 +1461,7 @@ bool bta_gattc_process_srvc_chg_ind(uint16_t conn_id, tBTA_GATTC_RCB* p_clrcb, /* not an opened connection; or connection busy */ /* search for first available clcb and start discovery */ if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL)) { - for (i = 0; i < BTA_GATTC_CLCB_MAX; i++) { + for (size_t i = 0; i < BTA_GATTC_CLCB_MAX; i++) { if (bta_gattc_cb.clcb[i].in_use && bta_gattc_cb.clcb[i].p_srcb == p_srcb && bta_gattc_cb.clcb[i].p_q_cmd == NULL) { diff --git a/bta/gatt/bta_gattc_api.cc b/bta/gatt/bta_gattc_api.cc index 4815f245d..143db2d33 100644 --- a/bta/gatt/bta_gattc_api.cc +++ b/bta/gatt/bta_gattc_api.cc @@ -36,6 +36,8 @@ #include "bta_sys.h" #include "device/include/controller.h" +using bluetooth::Uuid; + /***************************************************************************** * Constants ****************************************************************************/ @@ -64,14 +66,6 @@ void BTA_GATTC_Disable(void) { bta_sys_deregister(BTA_ID_GATTC); } -static void create_random_uuid(tBT_UUID* uuid) { - uuid->len = LEN_UUID_128; - - for (int i = 0; i < 16; ++i) { - uuid->uu.uuid128[i] = (uint8_t)(rand() % 256); - } -} - /** * This function is called to register application callbacks with BTA GATTC * module. |client_cb| pointer to the application callback function. @@ -82,10 +76,7 @@ void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb, if (bta_sys_is_register(BTA_ID_GATTC) == false) bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg); - // base::Owned will own and free app_uuid - tBT_UUID* uuid = new tBT_UUID; - create_random_uuid(uuid); - do_in_bta_thread(FROM_HERE, base::Bind(&bta_gattc_register, base::Owned(uuid), + do_in_bta_thread(FROM_HERE, base::Bind(&bta_gattc_register, Uuid::GetRandom(), p_client_cb, std::move(cb))); } @@ -239,15 +230,15 @@ void BTA_GATTC_ConfigureMTU(uint16_t conn_id, uint16_t mtu) { * Returns None * ******************************************************************************/ -void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id, tBT_UUID* p_srvc_uuid) { - const size_t len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID); +void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id, Uuid* p_srvc_uuid) { + const size_t len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(Uuid); tBTA_GATTC_API_SEARCH* p_buf = (tBTA_GATTC_API_SEARCH*)osi_calloc(len); p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT; p_buf->hdr.layer_specific = conn_id; if (p_srvc_uuid) { - p_buf->p_srvc_uuid = (tBT_UUID*)(p_buf + 1); - memcpy(p_buf->p_srvc_uuid, p_srvc_uuid, sizeof(tBT_UUID)); + p_buf->p_srvc_uuid = (Uuid*)(p_buf + 1); + *p_buf->p_srvc_uuid = *p_srvc_uuid; } else { p_buf->p_srvc_uuid = NULL; } @@ -255,11 +246,12 @@ void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id, tBT_UUID* p_srvc_uuid) { bta_sys_sendmsg(p_buf); } -void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id, tBT_UUID* p_srvc_uuid) { +void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id, + const Uuid& p_srvc_uuid) { tGATT_DISC_PARAM* param = new tGATT_DISC_PARAM; param->s_handle = 0x0001; param->e_handle = 0xFFFF; - param->service = *p_srvc_uuid; + param->service = p_srvc_uuid; do_in_bta_thread(FROM_HERE, base::Bind(base::IgnoreResult(&GATTC_Discover), conn_id, GATT_DISC_SRVC_BY_UUID, base::Owned(param))); @@ -367,7 +359,7 @@ void BTA_GATTC_ReadCharacteristic(uint16_t conn_id, uint16_t handle, * This function is called to read a value of characteristic with uuid equal to * |uuid| */ -void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, tBT_UUID uuid, +void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, const Uuid& uuid, uint16_t s_handle, uint16_t e_handle, tBTA_GATT_AUTH_REQ auth_req, GATT_READ_OP_CB callback, void* cb_data) { diff --git a/bta/gatt/bta_gattc_cache.cc b/bta/gatt/bta_gattc_cache.cc index bfc03c2e0..f99a46f50 100644 --- a/bta/gatt/bta_gattc_cache.cc +++ b/bta/gatt/bta_gattc_cache.cc @@ -44,13 +44,14 @@ #include "sdpdefs.h" #include "utl.h" +using bluetooth::Uuid; + static void bta_gattc_cache_write(const RawAddress& server_bda, uint16_t num_attr, tBTA_GATTC_NV_ATTR* attr); static void bta_gattc_char_dscpt_disc_cmpl(uint16_t conn_id, tBTA_GATTC_SERV* p_srvc_cb); static tBTA_GATT_STATUS bta_gattc_sdp_service_disc( uint16_t conn_id, tBTA_GATTC_SERV* p_server_cb); -extern void bta_to_btif_uuid(bt_uuid_t* p_dest, tBT_UUID* p_src); tBTA_GATTC_SERVICE* bta_gattc_find_matching_service(const list_t* services, uint16_t handle); tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb(tBTA_GATTC_SERV* p_srcb, @@ -80,32 +81,46 @@ typedef struct { } tBTA_GATTC_CB_DATA; #if (BTA_GATT_DEBUG == TRUE) -static char* bta_gattc_attr_type[] = { +static const char* bta_gattc_attr_type[] = { "I", /* Included Service */ "C", /* Characteristic */ "D" /* Characteristic Descriptor */ }; /* utility functions */ +bool display_descriptor(void* data, void* context) { + tBTA_GATTC_DESCRIPTOR* p_desc = (tBTA_GATTC_DESCRIPTOR*)data; + APPL_TRACE_ERROR("\t Descriptor handle[%d] uuid[%s]", p_desc->handle, + p_desc->uuid.ToString().c_str()); + return true; +} + bool display_cache_attribute(void* data, void* context) { - tBTA_GATTC_CACHE_ATTR* p_attr = data; - APPL_TRACE_ERROR("\t Attr handle[%d] uuid[0x%04x] type[%s] prop[0x%1x]", - p_attr->handle, p_attr->uuid.uu.uuid16, - bta_gattc_attr_type[p_attr->attr_type], p_attr->property); + tBTA_GATTC_CHARACTERISTIC* p_attr = (tBTA_GATTC_CHARACTERISTIC*)data; + APPL_TRACE_ERROR("\t Characteristic handle[%d] uuid[%s] prop[0x%1x]", + p_attr->handle, p_attr->uuid.ToString().c_str(), + p_attr->properties); + if (!p_attr->descriptors) { + APPL_TRACE_ERROR("No descriptors"); + return true; + } + + list_foreach(p_attr->descriptors, display_descriptor, NULL); return true; } bool display_cache_service(void* data, void* context) { - tBTA_GATTC_SERVICE* p_cur_srvc = data; - APPL_TRACE_ERROR("Service: handle[%d ~ %d] %s[0x%04x] inst[%d]", - p_cur_srvc->s_handle, p_cur_srvc->e_handle, - ((p_cur_srvc->uuid.len == 2) ? "uuid16" : "uuid128"), - p_cur_srvc->uuid.uu.uuid16, p_cur_srvc->handle); - - if (p_cur_srvc->characteristics != NULL) { - list_foreach(p_cur_srvc->characteristics, display_cache_attribute, NULL); + tBTA_GATTC_SERVICE* p_cur_srvc = (tBTA_GATTC_SERVICE*)data; + APPL_TRACE_ERROR("Service: handle[%d ~ %d] %s inst[%d]", p_cur_srvc->s_handle, + p_cur_srvc->e_handle, p_cur_srvc->uuid.ToString().c_str(), + p_cur_srvc->handle); + + if (!p_cur_srvc->characteristics) { + APPL_TRACE_ERROR("No characteristics"); + return true; } + list_foreach(p_cur_srvc->characteristics, display_cache_attribute, NULL); return true; } @@ -142,8 +157,9 @@ static void bta_gattc_display_explore_record(tBTA_GATTC_ATTR_REC* p_rec, APPL_TRACE_ERROR("<================Start Explore Queue =============>"); for (i = 0; i < num_rec; i++, pp++) { APPL_TRACE_ERROR( - "\t rec[%d] uuid[0x%04x] s_handle[%d] e_handle[%d] is_primary[%d]", - i + 1, pp->uuid.uu.uuid16, pp->s_handle, pp->e_handle, pp->is_primary); + "\t rec[%d] uuid[%s] s_handle[%d] e_handle[%d] is_primary[%d]", i + 1, + pp->uuid.ToString().c_str(), pp->s_handle, pp->e_handle, + pp->is_primary); } APPL_TRACE_ERROR("<================ End Explore Queue =============>"); APPL_TRACE_ERROR(" "); @@ -202,7 +218,7 @@ static void service_free(void* ptr) { static tBTA_GATT_STATUS bta_gattc_add_srvc_to_cache(tBTA_GATTC_SERV* p_srvc_cb, uint16_t s_handle, uint16_t e_handle, - tBT_UUID* p_uuid, + const Uuid& uuid, bool is_primary) { #if (BTA_GATT_DEBUG == TRUE) APPL_TRACE_DEBUG("Add a service into Service"); @@ -215,7 +231,7 @@ static tBTA_GATT_STATUS bta_gattc_add_srvc_to_cache(tBTA_GATTC_SERV* p_srvc_cb, p_new_srvc->s_handle = s_handle; p_new_srvc->e_handle = e_handle; p_new_srvc->is_primary = is_primary; - memcpy(&p_new_srvc->uuid, p_uuid, sizeof(tBT_UUID)); + p_new_srvc->uuid = uuid; p_new_srvc->handle = s_handle; p_new_srvc->characteristics = list_new(characteristic_free); p_new_srvc->included_svc = list_new(osi_free); @@ -231,12 +247,12 @@ static tBTA_GATT_STATUS bta_gattc_add_srvc_to_cache(tBTA_GATTC_SERV* p_srvc_cb, static tBTA_GATT_STATUS bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb, uint16_t attr_handle, uint16_t value_handle, - tBT_UUID* p_uuid, + const Uuid& uuid, uint8_t property) { #if (BTA_GATT_DEBUG == TRUE) APPL_TRACE_DEBUG("%s: Add a characteristic into Service", __func__); - APPL_TRACE_DEBUG("handle=%d uuid16=0x%x property=0x%x", value_handle, - p_uuid->uu.uuid16, property); + APPL_TRACE_DEBUG("handle=%d uuid16=%s property=0x%x", value_handle, + uuid.ToString().c_str(), property); #endif tBTA_GATTC_SERVICE* service = @@ -259,7 +275,7 @@ static tBTA_GATT_STATUS bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb, characteristic->handle = value_handle; characteristic->properties = property; characteristic->descriptors = list_new(osi_free); - memcpy(&characteristic->uuid, p_uuid, sizeof(tBT_UUID)); + characteristic->uuid = uuid; characteristic->service = service; list_append(service->characteristics, characteristic); @@ -277,13 +293,13 @@ static tBTA_GATT_STATUS bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb, * ******************************************************************************/ static tBTA_GATT_STATUS bta_gattc_add_attr_to_cache( - tBTA_GATTC_SERV* p_srvc_cb, uint16_t handle, tBT_UUID* p_uuid, + tBTA_GATTC_SERV* p_srvc_cb, uint16_t handle, const Uuid& uuid, uint8_t property, uint16_t incl_srvc_s_handle, tBTA_GATTC_ATTR_TYPE type) { #if (BTA_GATT_DEBUG == TRUE) APPL_TRACE_DEBUG("%s: Add a [%s] into Service", __func__, bta_gattc_attr_type[type]); - APPL_TRACE_DEBUG("handle=%d uuid16=0x%x property=0x%x type=%d", handle, - p_uuid->uu.uuid16, property, type); + APPL_TRACE_DEBUG("handle=%d uuid=%s property=0x%x type=%d", handle, + uuid.ToString().c_str(), property, type); #endif tBTA_GATTC_SERVICE* service = @@ -299,7 +315,7 @@ static tBTA_GATT_STATUS bta_gattc_add_attr_to_cache( (tBTA_GATTC_INCLUDED_SVC*)osi_malloc(sizeof(tBTA_GATTC_INCLUDED_SVC)); isvc->handle = handle; - memcpy(&isvc->uuid, p_uuid, sizeof(tBT_UUID)); + isvc->uuid = uuid; isvc->owning_service = service; isvc->included_service = bta_gattc_find_matching_service( @@ -317,7 +333,7 @@ static tBTA_GATT_STATUS bta_gattc_add_attr_to_cache( (tBTA_GATTC_DESCRIPTOR*)osi_malloc(sizeof(tBTA_GATTC_DESCRIPTOR)); descriptor->handle = handle; - memcpy(&descriptor->uuid, p_uuid, sizeof(tBT_UUID)); + descriptor->uuid = uuid; if (service->characteristics == NULL || list_is_empty(service->characteristics)) { @@ -494,7 +510,7 @@ static void bta_gattc_explore_srvc(uint16_t conn_id, if (p_srvc_cb->cur_srvc_idx < p_srvc_cb->total_srvc) { /* add the first service into cache */ if (bta_gattc_add_srvc_to_cache(p_srvc_cb, p_rec->s_handle, p_rec->e_handle, - &p_rec->uuid, p_rec->is_primary) == 0) { + p_rec->uuid, p_rec->is_primary) == 0) { /* start discovering included services */ bta_gattc_start_disc_include_srvc(conn_id, p_srvc_cb); return; @@ -548,7 +564,7 @@ static void bta_gattc_char_disc_cmpl(uint16_t conn_id, if (p_srvc_cb->total_char > 0) { /* add the first characteristic into cache */ bta_gattc_add_char_to_cache(p_srvc_cb, p_rec->char_decl_handle, - p_rec->s_handle, &p_rec->uuid, p_rec->property); + p_rec->s_handle, p_rec->uuid, p_rec->property); /* start discoverying characteristic descriptor , if failed, disc for next * char*/ @@ -577,7 +593,7 @@ static void bta_gattc_char_dscpt_disc_cmpl(uint16_t conn_id, p_rec = p_srvc_cb->p_srvc_list + (++p_srvc_cb->cur_char_idx); /* add the next characteristic into cache */ bta_gattc_add_char_to_cache(p_srvc_cb, p_rec->char_decl_handle, - p_rec->s_handle, &p_rec->uuid, p_rec->property); + p_rec->s_handle, p_rec->uuid, p_rec->property); /* start discoverying next characteristic for char descriptor */ bta_gattc_start_disc_char_dscp(conn_id, p_srvc_cb); @@ -591,9 +607,9 @@ static void bta_gattc_char_dscpt_disc_cmpl(uint16_t conn_id, bta_gattc_explore_srvc(conn_id, p_srvc_cb); } } + static bool bta_gattc_srvc_in_list(tBTA_GATTC_SERV* p_srvc_cb, - uint16_t s_handle, uint16_t e_handle, - UNUSED_ATTR tBT_UUID uuid) { + uint16_t s_handle, uint16_t e_handle, Uuid) { tBTA_GATTC_ATTR_REC* p_rec = NULL; uint8_t i; bool exist_srvc = false; @@ -628,7 +644,7 @@ static bool bta_gattc_srvc_in_list(tBTA_GATTC_SERV* p_srvc_cb, static tBTA_GATT_STATUS bta_gattc_add_srvc_to_list(tBTA_GATTC_SERV* p_srvc_cb, uint16_t s_handle, uint16_t e_handle, - tBT_UUID uuid, + const Uuid& uuid, bool is_primary) { tBTA_GATTC_ATTR_REC* p_rec = NULL; tBTA_GATT_STATUS status = BTA_GATT_OK; @@ -637,13 +653,13 @@ static tBTA_GATT_STATUS bta_gattc_add_srvc_to_list(tBTA_GATTC_SERV* p_srvc_cb, p_srvc_cb->next_avail_idx < BTA_GATTC_MAX_CACHE_CHAR) { p_rec = p_srvc_cb->p_srvc_list + p_srvc_cb->next_avail_idx; - APPL_TRACE_DEBUG("%s handle=%d, service type=0x%04x", __func__, s_handle, - uuid.uu.uuid16); + APPL_TRACE_DEBUG("%s handle=%d, service type=%s", __func__, s_handle, + uuid.ToString().c_str()); p_rec->s_handle = s_handle; p_rec->e_handle = e_handle; p_rec->is_primary = is_primary; - memcpy(&p_rec->uuid, &uuid, sizeof(tBT_UUID)); + p_rec->uuid = uuid; p_srvc_cb->total_srvc++; p_srvc_cb->next_avail_idx++; @@ -666,7 +682,7 @@ static tBTA_GATT_STATUS bta_gattc_add_srvc_to_list(tBTA_GATTC_SERV* p_srvc_cb, static tBTA_GATT_STATUS bta_gattc_add_char_to_list(tBTA_GATTC_SERV* p_srvc_cb, uint16_t decl_handle, uint16_t value_handle, - tBT_UUID uuid, + const Uuid& uuid, uint8_t property) { tBTA_GATTC_ATTR_REC* p_rec = NULL; tBTA_GATT_STATUS status = BTA_GATT_OK; @@ -684,7 +700,7 @@ static tBTA_GATT_STATUS bta_gattc_add_char_to_list(tBTA_GATTC_SERV* p_srvc_cb, p_rec->property = property; p_rec->e_handle = (p_srvc_cb->p_srvc_list + p_srvc_cb->cur_srvc_idx)->e_handle; - memcpy(&p_rec->uuid, &uuid, sizeof(tBT_UUID)); + p_rec->uuid = uuid; /* update the endind handle of pervious characteristic if available */ if (p_srvc_cb->total_char > 1) { @@ -710,29 +726,28 @@ static tBTA_GATT_STATUS bta_gattc_add_char_to_list(tBTA_GATTC_SERV* p_srvc_cb, * ******************************************************************************/ void bta_gattc_sdp_callback(uint16_t sdp_status, void* user_data) { - tSDP_DISC_REC* p_sdp_rec = NULL; - tBT_UUID service_uuid; tSDP_PROTOCOL_ELEM pe; - uint16_t start_handle = 0, end_handle = 0; tBTA_GATTC_CB_DATA* cb_data = (tBTA_GATTC_CB_DATA*)user_data; tBTA_GATTC_SERV* p_srvc_cb = bta_gattc_find_scb_by_cid(cb_data->sdp_conn_id); if (((sdp_status == SDP_SUCCESS) || (sdp_status == SDP_DB_FULL)) && p_srvc_cb != NULL) { + tSDP_DISC_REC* p_sdp_rec = NULL; do { /* find a service record, report it */ p_sdp_rec = SDP_FindServiceInDb(cb_data->p_sdp_db, 0, p_sdp_rec); if (p_sdp_rec) { + Uuid service_uuid; if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) { if (SDP_FindProtocolListElemInRec(p_sdp_rec, UUID_PROTOCOL_ATT, &pe)) { - start_handle = (uint16_t)pe.params[0]; - end_handle = (uint16_t)pe.params[1]; + uint16_t start_handle = (uint16_t)pe.params[0]; + uint16_t end_handle = (uint16_t)pe.params[1]; #if (BTA_GATT_DEBUG == TRUE) - APPL_TRACE_EVENT( - "Found ATT service [0x%04x] handle[0x%04x ~ 0x%04x]", - service_uuid.uu.uuid16, start_handle, end_handle); + APPL_TRACE_EVENT("Found ATT service [%s] handle[0x%04x ~ 0x%04x]", + service_uuid.ToString().c_str(), start_handle, + end_handle); #endif if (GATT_HANDLE_IS_VALID(start_handle) && @@ -772,14 +787,9 @@ void bta_gattc_sdp_callback(uint16_t sdp_status, void* user_data) { ******************************************************************************/ static tBTA_GATT_STATUS bta_gattc_sdp_service_disc( uint16_t conn_id, tBTA_GATTC_SERV* p_server_cb) { - tSDP_UUID uuid; uint16_t num_attrs = 2; uint16_t attr_list[2]; - memset(&uuid, 0, sizeof(tSDP_UUID)); - - uuid.len = LEN_UUID_16; - uuid.uu.uuid16 = UUID_PROTOCOL_ATT; /* * On success, cb_data will be freed inside bta_gattc_sdp_callback, @@ -792,6 +802,7 @@ static tBTA_GATT_STATUS bta_gattc_sdp_service_disc( attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST; attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST; + Uuid uuid = Uuid::From16Bit(UUID_PROTOCOL_ATT); SDP_InitDiscoveryDb(cb_data->p_sdp_db, BTA_GATT_SDP_DB_SIZE, 1, &uuid, num_attrs, attr_list); @@ -856,7 +867,7 @@ void bta_gattc_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, p_data->value.incl_service.service_type, false); /* add into database */ bta_gattc_add_attr_to_cache( - p_srvc_cb, p_data->handle, &p_data->value.incl_service.service_type, + p_srvc_cb, p_data->handle, p_data->value.incl_service.service_type, pri_srvc, p_data->value.incl_service.s_handle, BTA_GATTC_ATTR_TYPE_INCL_SRVC); break; @@ -870,16 +881,16 @@ void bta_gattc_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, break; case GATT_DISC_CHAR_DSCPT: - bta_gattc_add_attr_to_cache(p_srvc_cb, p_data->handle, &p_data->type, 0, + bta_gattc_add_attr_to_cache(p_srvc_cb, p_data->handle, p_data->type, 0, 0 /* incl_srvc_handle */, BTA_GATTC_ATTR_TYPE_CHAR_DESCR); break; } } } + void bta_gattc_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status) { - tBTA_GATTC_SERV* p_srvc_cb; tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); if (p_clcb && (status != GATT_SUCCESS || p_clcb->status != GATT_SUCCESS)) { @@ -887,36 +898,35 @@ void bta_gattc_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, bta_gattc_sm_execute(p_clcb, BTA_GATTC_DISCOVER_CMPL_EVT, NULL); return; } - p_srvc_cb = bta_gattc_find_scb_by_cid(conn_id); - if (p_srvc_cb != NULL) { - switch (disc_type) { - case GATT_DISC_SRVC_ALL: - case GATT_DISC_SRVC_BY_UUID: + tBTA_GATTC_SERV* p_srvc_cb = bta_gattc_find_scb_by_cid(conn_id); + if (!p_srvc_cb) return; + + switch (disc_type) { + case GATT_DISC_SRVC_ALL: + case GATT_DISC_SRVC_BY_UUID: #if (BTA_GATT_DEBUG == TRUE) - bta_gattc_display_explore_record(p_srvc_cb->p_srvc_list, - p_srvc_cb->next_avail_idx); + bta_gattc_display_explore_record(p_srvc_cb->p_srvc_list, + p_srvc_cb->next_avail_idx); #endif - bta_gattc_explore_srvc(conn_id, p_srvc_cb); - break; - - case GATT_DISC_INC_SRVC: - bta_gattc_incl_srvc_disc_cmpl(conn_id, p_srvc_cb); + bta_gattc_explore_srvc(conn_id, p_srvc_cb); + break; - break; + case GATT_DISC_INC_SRVC: + bta_gattc_incl_srvc_disc_cmpl(conn_id, p_srvc_cb); + break; - case GATT_DISC_CHAR: + case GATT_DISC_CHAR: #if (BTA_GATT_DEBUG == TRUE) - bta_gattc_display_explore_record(p_srvc_cb->p_srvc_list, - p_srvc_cb->next_avail_idx); + bta_gattc_display_explore_record(p_srvc_cb->p_srvc_list, + p_srvc_cb->next_avail_idx); #endif - bta_gattc_char_disc_cmpl(conn_id, p_srvc_cb); - break; + bta_gattc_char_disc_cmpl(conn_id, p_srvc_cb); + break; - case GATT_DISC_CHAR_DSCPT: - bta_gattc_char_dscpt_disc_cmpl(conn_id, p_srvc_cb); - break; - } + case GATT_DISC_CHAR_DSCPT: + bta_gattc_char_dscpt_disc_cmpl(conn_id, p_srvc_cb); + break; } } @@ -929,32 +939,28 @@ void bta_gattc_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, * Returns false if map can not be found. * ******************************************************************************/ -void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, tBT_UUID* p_uuid) { - tBTA_GATTC cb_data; - - if (!p_clcb->p_srcb->p_srvc_cache || - list_is_empty(p_clcb->p_srcb->p_srvc_cache)) - return; +void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, Uuid* p_uuid) { + auto cache = p_clcb->p_srcb->p_srvc_cache; + if (!cache || list_is_empty(cache)) return; - for (list_node_t* sn = list_begin(p_clcb->p_srcb->p_srvc_cache); - sn != list_end(p_clcb->p_srcb->p_srvc_cache); sn = list_next(sn)) { + for (list_node_t* sn = list_begin(cache); sn != list_end(cache); + sn = list_next(sn)) { tBTA_GATTC_SERVICE* p_cache = (tBTA_GATTC_SERVICE*)list_node(sn); - if (!bta_gattc_uuid_compare(p_uuid, &p_cache->uuid, false)) continue; + if (p_uuid && *p_uuid != p_cache->uuid) continue; #if (BTA_GATT_DEBUG == TRUE) - APPL_TRACE_DEBUG("found service [0x%04x], inst[%d] handle [%d]", - p_cache->uuid.uu.uuid16, p_cache->handle, + APPL_TRACE_DEBUG("found service %s, inst[%d] handle [%d]", + p_cache->uuid.ToString().c_str(), p_cache->handle, p_cache->s_handle); #endif if (!p_clcb->p_rcb->p_cback) continue; + tBTA_GATTC cb_data; memset(&cb_data, 0, sizeof(tBTA_GATTC)); - cb_data.srvc_res.conn_id = p_clcb->bta_conn_id; cb_data.srvc_res.service_uuid.inst_id = p_cache->handle; - memcpy(&cb_data.srvc_res.service_uuid.uuid, &p_cache->uuid, - sizeof(tBTA_GATT_ID)); + cb_data.srvc_res.service_uuid.uuid = p_cache->uuid; (*p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_RES_EVT, &cb_data); } @@ -1078,7 +1084,7 @@ tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(uint16_t conn_id, void bta_gattc_fill_gatt_db_el(btgatt_db_element_t* p_attr, bt_gatt_db_attribute_type_t type, uint16_t att_handle, uint16_t s_handle, - uint16_t e_handle, uint16_t id, tBT_UUID uuid, + uint16_t e_handle, uint16_t id, const Uuid& uuid, uint8_t prop) { p_attr->type = type; p_attr->attribute_handle = att_handle; @@ -1090,7 +1096,7 @@ void bta_gattc_fill_gatt_db_el(btgatt_db_element_t* p_attr, // Permissions are not discoverable using the attribute protocol. // Core 5.0, Part F, 3.2.5 Attribute Permissions p_attr->permissions = 0; - bta_to_btif_uuid(&p_attr->uuid, &uuid); + p_attr->uuid = uuid; } /******************************************************************************* @@ -1295,20 +1301,20 @@ void bta_gattc_rebuild_cache(tBTA_GATTC_SERV* p_srvc_cb, uint16_t num_attr, switch (p_attr->attr_type) { case BTA_GATTC_ATTR_TYPE_SRVC: bta_gattc_add_srvc_to_cache(p_srvc_cb, p_attr->s_handle, - p_attr->e_handle, &p_attr->uuid, + p_attr->e_handle, p_attr->uuid, p_attr->is_primary); break; case BTA_GATTC_ATTR_TYPE_CHAR: // TODO(jpawlowski): store decl_handle properly. bta_gattc_add_char_to_cache(p_srvc_cb, p_attr->s_handle, - p_attr->s_handle, &p_attr->uuid, + p_attr->s_handle, p_attr->uuid, p_attr->prop); break; case BTA_GATTC_ATTR_TYPE_CHAR_DESCR: case BTA_GATTC_ATTR_TYPE_INCL_SRVC: - bta_gattc_add_attr_to_cache(p_srvc_cb, p_attr->s_handle, &p_attr->uuid, + bta_gattc_add_attr_to_cache(p_srvc_cb, p_attr->s_handle, p_attr->uuid, p_attr->prop, p_attr->incl_srvc_handle, p_attr->attr_type); break; @@ -1328,7 +1334,7 @@ void bta_gattc_rebuild_cache(tBTA_GATTC_SERV* p_srvc_cb, uint16_t num_attr, * ******************************************************************************/ void bta_gattc_fill_nv_attr(tBTA_GATTC_NV_ATTR* p_attr, uint8_t type, - uint16_t s_handle, uint16_t e_handle, tBT_UUID uuid, + uint16_t s_handle, uint16_t e_handle, Uuid uuid, uint8_t prop, uint16_t incl_srvc_handle, bool is_primary) { p_attr->s_handle = s_handle; @@ -1338,8 +1344,7 @@ void bta_gattc_fill_nv_attr(tBTA_GATTC_NV_ATTR* p_attr, uint8_t type, p_attr->id = 0; p_attr->prop = prop; p_attr->incl_srvc_handle = incl_srvc_handle; - - memcpy(&p_attr->uuid, &uuid, sizeof(tBT_UUID)); + p_attr->uuid = uuid; } /******************************************************************************* diff --git a/bta/gatt/bta_gattc_int.h b/bta/gatt/bta_gattc_int.h index 8055090f6..fc950c039 100644 --- a/bta/gatt/bta_gattc_int.h +++ b/bta/gatt/bta_gattc_int.h @@ -101,7 +101,7 @@ typedef struct { uint16_t handle; // read by UUID data - tBT_UUID uuid; + bluetooth::Uuid uuid; uint16_t s_handle; uint16_t e_handle; @@ -143,7 +143,7 @@ typedef struct { typedef struct { BT_HDR hdr; - tBT_UUID* p_srvc_uuid; + bluetooth::Uuid* p_srvc_uuid; } tBTA_GATTC_API_SEARCH; typedef struct { @@ -185,7 +185,7 @@ typedef union { /* GATT server cache on the client */ typedef struct { - tBT_UUID uuid; + bluetooth::Uuid uuid; uint16_t s_handle; uint16_t e_handle; // this field is set only for characteristic @@ -257,7 +257,7 @@ typedef struct { client_if; /* client interface with BTE stack for this application */ uint8_t num_clcb; /* number of associated CLCB */ bool dereg_pending; - tBT_UUID app_uuid; + bluetooth::Uuid app_uuid; tBTA_GATTC_NOTIF_REG notif_reg[BTA_GATTC_NOTIF_REG_MAX]; } tBTA_GATTC_RCB; @@ -338,7 +338,8 @@ extern bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event, /* function processed outside SM */ extern void bta_gattc_disable(); -extern void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_data, +extern void bta_gattc_register(const bluetooth::Uuid& app_uuid, + tBTA_GATTC_CBACK* p_data, BtaAppRegisterCallback cb); extern void bta_gattc_process_api_open(tBTA_GATTC_DATA* p_msg); extern void bta_gattc_process_api_open_cancel(tBTA_GATTC_DATA* p_msg); @@ -421,8 +422,6 @@ extern tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg); extern bool bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data); -extern bool bta_gattc_uuid_compare(const tBT_UUID* p_src, const tBT_UUID* p_tar, - bool is_precise); extern bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg, tBTA_GATTC_SERV* p_srcb, tBTA_GATTC_NOTIFY* p_notify); @@ -448,7 +447,8 @@ extern tBTA_GATT_STATUS bta_gattc_discover_procedure( uint16_t conn_id, tBTA_GATTC_SERV* p_server_cb, uint8_t disc_type); extern tBTA_GATT_STATUS bta_gattc_discover_pri_service( uint16_t conn_id, tBTA_GATTC_SERV* p_server_cb, uint8_t disc_type); -extern void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, tBT_UUID* p_uuid); +extern void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, + bluetooth::Uuid* p_uuid); extern const list_t* bta_gattc_get_services(uint16_t conn_id); extern const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle( uint16_t conn_id, uint16_t handle); diff --git a/bta/gatt/bta_gattc_main.cc b/bta/gatt/bta_gattc_main.cc index 9a2deb6ed..fdea6effd 100644 --- a/bta/gatt/bta_gattc_main.cc +++ b/bta/gatt/bta_gattc_main.cc @@ -278,8 +278,8 @@ const tBTA_GATTC_ST_TBL bta_gattc_st_tbl[] = { tBTA_GATTC_CB bta_gattc_cb; #if (BTA_GATT_DEBUG == TRUE) -static char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code); -static char* gattc_state_code(tBTA_GATTC_STATE state_code); +static const char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code); +static const char* gattc_state_code(tBTA_GATTC_STATE state_code); #endif /******************************************************************************* @@ -403,7 +403,7 @@ bool bta_gattc_hdl_event(BT_HDR* p_msg) { * Returns void * ******************************************************************************/ -static char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code) { +static const char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code) { switch (evt_code) { case BTA_GATTC_API_OPEN_EVT: return "BTA_GATTC_API_OPEN_EVT"; @@ -453,7 +453,7 @@ static char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code) { * Returns void * ******************************************************************************/ -static char* gattc_state_code(tBTA_GATTC_STATE state_code) { +static const char* gattc_state_code(tBTA_GATTC_STATE state_code) { switch (state_code) { case BTA_GATTC_IDLE_ST: return "GATTC_IDLE_ST"; diff --git a/bta/gatt/bta_gattc_utils.cc b/bta/gatt/bta_gattc_utils.cc index f14f46907..71e3e0266 100644 --- a/bta/gatt/bta_gattc_utils.cc +++ b/bta/gatt/bta_gattc_utils.cc @@ -35,76 +35,6 @@ #include "l2c_api.h" #include "utl.h" -/***************************************************************************** - * Constants - ****************************************************************************/ - -static const uint8_t base_uuid[LEN_UUID_128] = { - 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -/******************************************************************************* - * - * Function bta_gatt_convert_uuid16_to_uuid128 - * - * Description Convert a 16 bits UUID to be an standard 128 bits one. - * - * Returns true if two uuid match; false otherwise. - * - ******************************************************************************/ -void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128], - uint16_t uuid_16) { - uint8_t* p = &uuid_128[LEN_UUID_128 - 4]; - - memcpy(uuid_128, base_uuid, LEN_UUID_128); - - UINT16_TO_STREAM(p, uuid_16); -} -/******************************************************************************* - * - * Function bta_gattc_uuid_compare - * - * Description Compare two UUID to see if they are the same. - * - * Returns true if two uuid match; false otherwise. - * - ******************************************************************************/ -bool bta_gattc_uuid_compare(const tBT_UUID* p_src, const tBT_UUID* p_tar, - bool is_precise) { - uint8_t su[LEN_UUID_128], tu[LEN_UUID_128]; - const uint8_t *ps, *pt; - - /* any of the UUID is unspecified */ - if (p_src == 0 || p_tar == 0) { - if (is_precise) - return false; - else - return true; - } - - /* If both are 16-bit, we can do a simple compare */ - if (p_src->len == 2 && p_tar->len == 2) { - return p_src->uu.uuid16 == p_tar->uu.uuid16; - } - - /* One or both of the UUIDs is 128-bit */ - if (p_src->len == LEN_UUID_16) { - /* convert a 16 bits UUID to 128 bits value */ - bta_gatt_convert_uuid16_to_uuid128(su, p_src->uu.uuid16); - ps = su; - } else - ps = p_src->uu.uuid128; - - if (p_tar->len == LEN_UUID_16) { - /* convert a 16 bits UUID to 128 bits value */ - bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16); - pt = tu; - } else - pt = p_tar->uu.uuid128; - - return (memcmp(ps, pt, LEN_UUID_128) == 0); -} - /******************************************************************************* * * Function bta_gattc_cl_get_regcb diff --git a/bta/gatt/bta_gatts_act.cc b/bta/gatt/bta_gatts_act.cc index b328093b4..fb8a808a8 100644 --- a/bta/gatt/bta_gatts_act.cc +++ b/bta/gatt/bta_gatts_act.cc @@ -184,8 +184,7 @@ void bta_gatts_register(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) { for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) { if (p_cb->rcb[i].in_use) { - if (bta_gatts_uuid_compare(p_cb->rcb[i].app_uuid, - p_msg->api_reg.app_uuid)) { + if (p_cb->rcb[i].app_uuid == p_msg->api_reg.app_uuid) { APPL_TRACE_ERROR("application already registered."); status = BTA_GATT_DUP_REG; break; @@ -202,17 +201,16 @@ void bta_gatts_register(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) { } cb_data.reg_oper.server_if = BTA_GATTS_INVALID_IF; - memcpy(&cb_data.reg_oper.uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID)); + cb_data.reg_oper.uuid = p_msg->api_reg.app_uuid; if (first_unuse != 0xff) { APPL_TRACE_ERROR("register application first_unuse rcb_idx = %d", first_unuse); p_cb->rcb[first_unuse].in_use = true; p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback; - memcpy(&p_cb->rcb[first_unuse].app_uuid, &p_msg->api_reg.app_uuid, - sizeof(tBT_UUID)); + p_cb->rcb[first_unuse].app_uuid = p_msg->api_reg.app_uuid; cb_data.reg_oper.server_if = p_cb->rcb[first_unuse].gatt_if = - GATT_Register(&p_msg->api_reg.app_uuid, &bta_gatts_cback); + GATT_Register(p_msg->api_reg.app_uuid, &bta_gatts_cback); if (!p_cb->rcb[first_unuse].gatt_if) { status = BTA_GATT_NO_RESOURCES; } else { diff --git a/bta/gatt/bta_gatts_api.cc b/bta/gatt/bta_gatts_api.cc index 7d4504300..28d4691d4 100644 --- a/bta/gatt/bta_gatts_api.cc +++ b/bta/gatt/bta_gatts_api.cc @@ -31,8 +31,6 @@ #include "bta_gatts_int.h" #include "bta_sys.h" -void btif_to_bta_uuid(tBT_UUID* p_dest, const bt_uuid_t* p_src); - /***************************************************************************** * Constants ****************************************************************************/ @@ -76,7 +74,8 @@ void BTA_GATTS_Disable(void) { * Returns None * ******************************************************************************/ -void BTA_GATTS_AppRegister(tBT_UUID* p_app_uuid, tBTA_GATTS_CBACK* p_cback) { +void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid, + tBTA_GATTS_CBACK* p_cback) { tBTA_GATTS_API_REG* p_buf = (tBTA_GATTS_API_REG*)osi_malloc(sizeof(tBTA_GATTS_API_REG)); @@ -85,8 +84,7 @@ void BTA_GATTS_AppRegister(tBT_UUID* p_app_uuid, tBTA_GATTS_CBACK* p_cback) { bta_sys_register(BTA_ID_GATTS, &bta_gatts_reg); p_buf->hdr.event = BTA_GATTS_API_REG_EVT; - if (p_app_uuid != NULL) - memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID)); + p_buf->app_uuid = app_uuid; p_buf->p_cback = p_cback; bta_sys_sendmsg(p_buf); @@ -143,8 +141,7 @@ extern uint16_t BTA_GATTS_AddService(tBTA_GATTS_IF server_if, uint16_t status = GATTS_AddService(server_if, service.data(), service.size()); if (status == GATT_SERVICE_STARTED) { - btif_to_bta_uuid(&bta_gatts_cb.srvc_cb[srvc_idx].service_uuid, - &service[0].uuid); + bta_gatts_cb.srvc_cb[srvc_idx].service_uuid = service[0].uuid; // service_id is equal to service start handle bta_gatts_cb.srvc_cb[srvc_idx].service_id = service[0].attribute_handle; diff --git a/bta/gatt/bta_gatts_int.h b/bta/gatt/bta_gatts_int.h index df5d706db..7df9426b7 100644 --- a/bta/gatt/bta_gatts_int.h +++ b/bta/gatt/bta_gatts_int.h @@ -59,7 +59,7 @@ typedef uint16_t tBTA_GATTS_INT_EVT; /* internal strucutre for GATTC register API */ typedef struct { BT_HDR hdr; - tBT_UUID app_uuid; + bluetooth::Uuid app_uuid; tBTA_GATTS_CBACK* p_cback; } tBTA_GATTS_API_REG; @@ -124,14 +124,14 @@ typedef union { /* application registration control block */ typedef struct { bool in_use; - tBT_UUID app_uuid; + bluetooth::Uuid app_uuid; tBTA_GATTS_CBACK* p_cback; tBTA_GATTS_IF gatt_if; } tBTA_GATTS_RCB; /* service registration control block */ typedef struct { - tBT_UUID service_uuid; /* service UUID */ + bluetooth::Uuid service_uuid; /* service UUID */ uint16_t service_id; /* service start handle */ uint8_t rcb_idx; uint8_t idx; /* self index of serviec CB */ @@ -175,7 +175,6 @@ extern void bta_gatts_open(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg); extern void bta_gatts_cancel_open(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg); extern void bta_gatts_close(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg); -extern bool bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src); extern tBTA_GATTS_RCB* bta_gatts_find_app_rcb_by_app_if( tBTA_GATTS_IF server_if); extern uint8_t bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB* p_cb, diff --git a/bta/gatt/bta_gatts_utils.cc b/bta/gatt/bta_gatts_utils.cc index 23cc7c753..ed86f3aa8 100644 --- a/bta/gatt/bta_gatts_utils.cc +++ b/bta/gatt/bta_gatts_utils.cc @@ -31,27 +31,6 @@ #include "bta_sys.h" #include "utl.h" -static const uint8_t base_uuid[LEN_UUID_128] = { - 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -/******************************************************************************* - * - * Function bta_gatt_convert_uuid16_to_uuid128 - * - * Description Convert a 16 bits UUID to be an standard 128 bits one. - * - * Returns true if two uuid match; false otherwise. - * - ******************************************************************************/ -static void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128], - uint16_t uuid_16) { - uint8_t* p = &uuid_128[LEN_UUID_128 - 4]; - - memcpy(uuid_128, base_uuid, LEN_UUID_128); - - UINT16_TO_STREAM(p, uuid_16); -} /******************************************************************************* * * Function bta_gatts_alloc_srvc_cb @@ -168,43 +147,3 @@ tBTA_GATTS_SRVC_CB* bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB* p_cb, } return NULL; } -/******************************************************************************* - * - * Function bta_gatts_uuid_compare - * - * Description Compare two UUID to see if they are the same. - * - * Returns true if two uuid match; false otherwise. - * - ******************************************************************************/ -bool bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src) { - uint8_t su[LEN_UUID_128], tu[LEN_UUID_128]; - uint8_t *ps, *pt; - - /* any of the UUID is unspecified */ - if (src.len == 0 || tar.len == 0) { - return true; - } - - /* If both are 16-bit, we can do a simple compare */ - if (src.len == 2 && tar.len == 2) { - return src.uu.uuid16 == tar.uu.uuid16; - } - - /* One or both of the UUIDs is 128-bit */ - if (src.len == LEN_UUID_16) { - /* convert a 16 bits UUID to 128 bits value */ - bta_gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16); - ps = su; - } else - ps = src.uu.uuid128; - - if (tar.len == LEN_UUID_16) { - /* convert a 16 bits UUID to 128 bits value */ - bta_gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16); - pt = tu; - } else - pt = tar.uu.uuid128; - - return (memcmp(ps, pt, LEN_UUID_128) == 0); -} diff --git a/bta/hf_client/bta_hf_client_sdp.cc b/bta/hf_client/bta_hf_client_sdp.cc index 475ce1654..6feb1885f 100644 --- a/bta/hf_client/bta_hf_client_sdp.cc +++ b/bta/hf_client/bta_hf_client_sdp.cc @@ -33,6 +33,8 @@ #include "bta_sys.h" #include "osi/include/osi.h" +using bluetooth::Uuid; + /* Number of protocol elements in protocol element list. */ #define BTA_HF_CLIENT_NUM_PROTO_ELEMS 2 @@ -289,7 +291,7 @@ bool bta_hf_client_sdp_find_attr(tBTA_HF_CLIENT_CB* client_cb) { * ******************************************************************************/ void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb) { - tSDP_UUID uuid_list[2]; + Uuid uuid_list[1]; uint16_t num_uuid = 1; uint16_t attr_list[4]; uint8_t num_attr; @@ -302,7 +304,7 @@ void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb) { attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST; attr_list[3] = ATTR_ID_SUPPORTED_FEATURES; num_attr = 4; - uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE; + uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_AG_HANDSFREE); } /* acceptor; get features */ else { @@ -310,15 +312,13 @@ void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb) { attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST; attr_list[2] = ATTR_ID_SUPPORTED_FEATURES; num_attr = 3; - uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE; + uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_AG_HANDSFREE); } /* allocate buffer for sdp database */ client_cb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BT_DEFAULT_BUFFER_SIZE); /* set up service discovery database; attr happens to be attr_list len */ - uuid_list[0].len = LEN_UUID_16; - uuid_list[1].len = LEN_UUID_16; db_inited = SDP_InitDiscoveryDb(client_cb->p_disc_db, BT_DEFAULT_BUFFER_SIZE, num_uuid, uuid_list, num_attr, attr_list); diff --git a/bta/hh/bta_hh_le.cc b/bta/hh/bta_hh_le.cc index b4827a2b4..ceb6a2795 100644 --- a/bta/hh/bta_hh_le.cc +++ b/bta/hh/bta_hh_le.cc @@ -44,6 +44,7 @@ #include "stack/include/l2c_api.h" #include "utl.h" +using bluetooth::Uuid; using std::vector; #ifndef BTA_HH_LE_RECONN @@ -597,8 +598,7 @@ static tBTA_GATTC_DESCRIPTOR* find_descriptor_by_short_uuid( dn != list_end(p_char->descriptors); dn = list_next(dn)) { tBTA_GATTC_DESCRIPTOR* p_desc = (tBTA_GATTC_DESCRIPTOR*)list_node(dn); - if (p_char->uuid.len == LEN_UUID_16 && p_desc->uuid.uu.uuid16 == short_uuid) - return p_desc; + if (p_desc->uuid == Uuid::From16Bit(short_uuid)) return p_desc; } return NULL; @@ -826,7 +826,7 @@ static void write_rpt_ctl_cfg_cb(uint16_t conn_id, tGATT_STATUS status, const tBTA_GATTC_DESCRIPTOR* p_desc = BTA_GATTC_GetDescriptor(conn_id, handle); - uint16_t char_uuid = p_desc->characteristic->uuid.uu.uuid16; + uint16_t char_uuid = p_desc->characteristic->uuid.As16Bit(); srvc_inst_id = p_desc->characteristic->service->handle; hid_inst_id = srvc_inst_id; @@ -1071,8 +1071,6 @@ void bta_hh_le_dis_cback(const RawAddress& addr, tDIS_VALUE* p_dis_value) { * ******************************************************************************/ void bta_hh_le_pri_service_discovery(tBTA_HH_DEV_CB* p_cb) { - tBT_UUID pri_srvc; - bta_hh_le_co_reset_rpt_cache(p_cb->addr, p_cb->app_id); p_cb->disc_active |= (BTA_HH_LE_DISC_HIDS | BTA_HH_LE_DISC_DIS); @@ -1085,8 +1083,7 @@ void bta_hh_le_pri_service_discovery(tBTA_HH_DEV_CB* p_cb) { /* in parallel */ /* start primary service discovery for HID service */ - pri_srvc.len = LEN_UUID_16; - pri_srvc.uu.uuid16 = UUID_SERVCLASS_LE_HID; + Uuid pri_srvc = Uuid::From16Bit(UUID_SERVCLASS_LE_HID); BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &pri_srvc); return; } @@ -1414,7 +1411,7 @@ static void read_ext_rpt_ref_desc_cb(uint16_t conn_id, tGATT_STATUS status, /* if the length of the descriptor value is right, parse it assume it's a 16 * bits UUID */ - if (len != LEN_UUID_16) { + if (len != Uuid::kNumBytes16) { APPL_TRACE_ERROR("%s: we support only 16bit UUID: %d", __func__, len); return; } @@ -1515,13 +1512,13 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb, tBTA_GATTC_CHARACTERISTIC* p_char = (tBTA_GATTC_CHARACTERISTIC*)list_node(cn); - if (p_char->uuid.len != LEN_UUID_16) continue; + if (!p_char->uuid.Is16Bit()) continue; - LOG_DEBUG(LOG_TAG, "%s: %s 0x%04d", __func__, - bta_hh_uuid_to_str(p_char->uuid.uu.uuid16), - p_char->uuid.uu.uuid16); + uint16_t uuid16 = p_char->uuid.As16Bit(); + LOG_DEBUG(LOG_TAG, "%s: %s %s", __func__, bta_hh_uuid_to_str(uuid16), + p_char->uuid.ToString().c_str()); - switch (p_char->uuid.uu.uuid16) { + switch (uuid16) { case GATT_UUID_HID_CONTROL_POINT: p_dev_cb->hid_srvc.control_point_handle = p_char->handle; break; @@ -1560,8 +1557,7 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb, case GATT_UUID_HID_BT_KB_OUTPUT: case GATT_UUID_HID_BT_MOUSE_INPUT: case GATT_UUID_HID_BT_KB_INPUT: - if (bta_hh_le_find_alloc_report_entry(p_dev_cb, service->handle, - p_char->uuid.uu.uuid16, + if (bta_hh_le_find_alloc_report_entry(p_dev_cb, service->handle, uuid16, p_char->handle) == NULL) APPL_TRACE_ERROR("%s: Add report entry failed !!!", __func__); @@ -1569,8 +1565,7 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb, default: APPL_TRACE_DEBUG("%s: not processing %s 0x%04d", __func__, - bta_hh_uuid_to_str(p_char->uuid.uu.uuid16), - p_char->uuid.uu.uuid16); + bta_hh_uuid_to_str(uuid16), uuid16); } } @@ -1580,8 +1575,7 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb, tBTA_GATTC_CHARACTERISTIC* p_char = (tBTA_GATTC_CHARACTERISTIC*)list_node(cn); - if (p_char->uuid.len == LEN_UUID_16 && - p_char->uuid.uu.uuid16 == GATT_UUID_HID_PROTO_MODE) { + if (p_char->uuid == Uuid::From16Bit(GATT_UUID_HID_PROTO_MODE)) { p_dev_cb->hid_srvc.proto_mode_handle = p_char->handle; bta_hh_le_set_protocol_mode(p_dev_cb, p_dev_cb->mode); break; @@ -1618,7 +1612,7 @@ void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) { sn = list_next(sn)) { tBTA_GATTC_SERVICE* service = (tBTA_GATTC_SERVICE*)list_node(sn); - if (service->uuid.uu.uuid16 == UUID_SERVCLASS_LE_HID && + if (service->uuid == Uuid::From16Bit(UUID_SERVCLASS_LE_HID) && service->is_primary && !have_hid) { have_hid = true; @@ -1632,15 +1626,14 @@ void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) { APPL_TRACE_DEBUG("%s: have HID service inst_id= %d", __func__, p_dev_cb->hid_srvc.srvc_inst_id); - } else if (service->uuid.uu.uuid16 == UUID_SERVCLASS_SCAN_PARAM) { + } else if (service->uuid == Uuid::From16Bit(UUID_SERVCLASS_SCAN_PARAM)) { p_dev_cb->scan_refresh_char_handle = 0; for (list_node_t* cn = list_begin(service->characteristics); cn != list_end(service->characteristics); cn = list_next(cn)) { tBTA_GATTC_CHARACTERISTIC* p_char = (tBTA_GATTC_CHARACTERISTIC*)list_node(cn); - if (p_char->uuid.len == LEN_UUID_16 && - p_char->uuid.uu.uuid16 == GATT_UUID_SCAN_REFRESH) { + if (p_char->uuid == Uuid::From16Bit(GATT_UUID_SCAN_REFRESH)) { p_dev_cb->scan_refresh_char_handle = p_char->handle; if (p_char->properties & BTA_GATT_CHAR_PROP_BIT_NOTIFY) @@ -1651,15 +1644,14 @@ void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) { break; } } - } else if (service->uuid.uu.uuid16 == UUID_SERVCLASS_GAP_SERVER) { + } else if (service->uuid == Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER)) { // TODO(jpawlowski): this should be done by GAP profile, remove when GAP // is fixed. for (list_node_t* cn = list_begin(service->characteristics); cn != list_end(service->characteristics); cn = list_next(cn)) { tBTA_GATTC_CHARACTERISTIC* p_char = (tBTA_GATTC_CHARACTERISTIC*)list_node(cn); - if (p_char->uuid.len == LEN_UUID_16 && - p_char->uuid.uu.uuid16 == GATT_UUID_GAP_PREF_CONN_PARAM) { + if (p_char->uuid == Uuid::From16Bit(GATT_UUID_GAP_PREF_CONN_PARAM)) { /* read the char value */ gatt_queue_read_op(GATT_READ_CHAR, p_dev_cb->conn_id, p_char->handle, read_pref_conn_params_cb, p_dev_cb); @@ -1708,18 +1700,18 @@ void bta_hh_le_input_rpt_notify(tBTA_GATTC_NOTIFY* p_data) { app_id = p_dev_cb->app_id; p_rpt = bta_hh_le_find_report_entry(p_dev_cb, p_dev_cb->hid_srvc.srvc_inst_id, - p_char->uuid.uu.uuid16, p_char->handle); + p_char->uuid.As16Bit(), p_char->handle); if (p_rpt == NULL) { APPL_TRACE_ERROR( - "%s: notification received for Unknown Report, uuid: 0x%04x, handle: " + "%s: notification received for Unknown Report, uuid: %s, handle: " "0x%04x", - __func__, p_char->uuid.uu.uuid16, p_char->handle); + __func__, p_char->uuid.ToString().c_str(), p_char->handle); return; } - if (p_char->uuid.uu.uuid16 == GATT_UUID_HID_BT_MOUSE_INPUT) + if (p_char->uuid == Uuid::From16Bit(GATT_UUID_HID_BT_MOUSE_INPUT)) app_id = BTA_HH_APP_ID_MI; - else if (p_char->uuid.uu.uuid16 == GATT_UUID_HID_BT_KB_INPUT) + else if (p_char->uuid == Uuid::From16Bit(GATT_UUID_HID_BT_KB_INPUT)) app_id = BTA_HH_APP_ID_KB; APPL_TRACE_DEBUG("Notification received on report ID: %d", p_rpt->rpt_id); @@ -1853,7 +1845,7 @@ static void read_report_cb(uint16_t conn_id, tGATT_STATUS status, if (p_char == NULL) return; - uint16_t char_uuid = p_char->uuid.uu.uuid16; + uint16_t char_uuid = p_char->uuid.As16Bit(); if (char_uuid != GATT_UUID_HID_REPORT && char_uuid != GATT_UUID_HID_BT_KB_INPUT && @@ -1882,7 +1874,7 @@ static void read_report_cb(uint16_t conn_id, tGATT_STATUS status, if (status == BTA_GATT_OK) { p_rpt = bta_hh_le_find_report_entry(p_dev_cb, p_char->service->handle, - p_char->uuid.uu.uuid16, p_char->handle); + char_uuid, p_char->handle); if (p_rpt != NULL && len) { p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + len + 1); @@ -1946,7 +1938,7 @@ static void write_report_cb(uint16_t conn_id, tGATT_STATUS status, const tBTA_GATTC_CHARACTERISTIC* p_char = BTA_GATTC_GetCharacteristic(conn_id, handle); - uint16_t uuid = p_char->uuid.uu.uuid16; + uint16_t uuid = p_char->uuid.As16Bit(); if (uuid != GATT_UUID_HID_REPORT && uuid != GATT_UUID_HID_BT_KB_INPUT && uuid != GATT_UUID_HID_BT_MOUSE_INPUT && uuid != GATT_UUID_HID_BT_KB_OUTPUT) { diff --git a/bta/hl/bta_hl_act.cc b/bta/hl/bta_hl_act.cc index 6c896f1fd..e7986450b 100644 --- a/bta/hl/bta_hl_act.cc +++ b/bta/hl/bta_hl_act.cc @@ -39,6 +39,8 @@ #include "sdp_api.h" #include "utl.h" +using bluetooth::Uuid; + /***************************************************************************** * Local Function prototypes ****************************************************************************/ @@ -1817,7 +1819,6 @@ tSDP_DISC_CMPL_CB* bta_hl_allocate_spd_cback(tBTA_HL_SDP_OPER sdp_oper, tBTA_HL_STATUS bta_hl_init_sdp(tBTA_HL_SDP_OPER sdp_oper, uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx) { tBTA_HL_MCL_CB* p_cb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - tSDP_UUID uuid_list; uint16_t attr_list[BTA_HL_NUM_SRCH_ATTR]; uint16_t num_attrs = BTA_HL_NUM_SRCH_ATTR; tBTA_HL_STATUS status; @@ -1843,8 +1844,7 @@ tBTA_HL_STATUS bta_hl_init_sdp(tBTA_HL_SDP_OPER sdp_oper, uint8_t app_idx, attr_list[8] = ATTR_ID_HDP_DATA_EXCH_SPEC; attr_list[9] = ATTR_ID_HDP_MCAP_SUP_PROC; - uuid_list.len = LEN_UUID_16; - uuid_list.uu.uuid16 = UUID_SERVCLASS_HDP_PROFILE; + Uuid uuid_list = Uuid::From16Bit(UUID_SERVCLASS_HDP_PROFILE); SDP_InitDiscoveryDb(p_cb->p_db, BTA_HL_DISC_SIZE, 1, &uuid_list, num_attrs, attr_list); diff --git a/bta/include/bta_api.h b/bta/include/bta_api.h index 814038cd8..c5f05123b 100644 --- a/bta/include/bta_api.h +++ b/bta/include/bta_api.h @@ -143,7 +143,7 @@ typedef uint32_t tBTA_SERVICE_MASK; typedef struct { tBTA_SERVICE_MASK srvc_mask; uint8_t num_uuid; - tBT_UUID* p_uuid; + bluetooth::Uuid* p_uuid; } tBTA_SERVICE_MASK_EXT; /* Security Setting Mask */ @@ -762,7 +762,7 @@ typedef struct { uint32_t raw_data_size; /* size of raw data */ tBT_DEVICE_TYPE device_type; /* device type in case it is BLE device */ uint32_t num_uuids; - uint8_t* p_uuid_list; + bluetooth::Uuid* p_uuid_list; tBTA_STATUS result; } tBTA_DM_DISC_RES; @@ -770,7 +770,7 @@ typedef struct { typedef struct { RawAddress bd_addr; /* BD address peer device. */ BD_NAME bd_name; /* Name of peer device. */ - tBT_UUID service; /* GATT based Services UUID found on peer device. */ + bluetooth::Uuid service; /* GATT based Services UUID found on peer device. */ } tBTA_DM_DISC_BLE_RES; /* Union of all search callback structures */ @@ -1172,7 +1172,8 @@ extern void BTA_DmDiscover(const RawAddress& bd_addr, * Returns void * ******************************************************************************/ -extern void BTA_DmDiscoverUUID(const RawAddress& bd_addr, tSDP_UUID* uuid, +extern void BTA_DmDiscoverUUID(const RawAddress& bd_addr, + const bluetooth::Uuid& uuid, tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search); /******************************************************************************* diff --git a/bta/include/bta_gatt_api.h b/bta/include/bta_gatt_api.h index 8034dc752..558c20eeb 100644 --- a/bta/include/bta_gatt_api.h +++ b/bta/include/bta_gatt_api.h @@ -46,7 +46,7 @@ using std::vector; **************************/ /* GATT ID */ typedef struct { - tBT_UUID uuid; /* uuid of the attribute */ + bluetooth::Uuid uuid; /* uuid of the attribute */ uint8_t inst_id; /* instance ID */ } __attribute__((packed)) tBTA_GATT_ID; @@ -210,7 +210,7 @@ enum { typedef uint8_t tBTA_GATTC_ATTR_TYPE; typedef struct { - tBT_UUID uuid; + bluetooth::Uuid uuid; uint16_t s_handle; uint16_t e_handle; /* used for service only */ uint8_t attr_type; @@ -224,7 +224,6 @@ typedef struct { typedef struct { tBTA_GATT_STATUS status; tBTA_GATTC_IF client_if; - tBT_UUID app_uuid; } tBTA_GATTC_REG; typedef struct { @@ -419,8 +418,8 @@ typedef uint8_t tBTA_GATT_CHAR_PROP; /*********************** NV callback Data Definitions ********************** */ typedef struct { - tBT_UUID app_uuid128; - tBT_UUID svc_uuid; + bluetooth::Uuid app_uuid128; + bluetooth::Uuid svc_uuid; uint16_t svc_inst; uint16_t s_handle; uint16_t e_handle; @@ -474,7 +473,7 @@ typedef struct { typedef struct { tBTA_GATTS_IF server_if; tBTA_GATT_STATUS status; - tBT_UUID uuid; + bluetooth::Uuid uuid; } tBTA_GATTS_REG_OPER; typedef struct { @@ -483,7 +482,7 @@ typedef struct { uint16_t svc_instance; bool is_primary; tBTA_GATT_STATUS status; - tBT_UUID uuid; + bluetooth::Uuid uuid; } tBTA_GATTS_CREATE; typedef struct { @@ -549,31 +548,32 @@ typedef void(tBTA_GATTS_ENB_CBACK)(tBTA_GATT_STATUS status); typedef void(tBTA_GATTS_CBACK)(tBTA_GATTS_EVT event, tBTA_GATTS* p_data); typedef struct { - tBT_UUID uuid; + bluetooth::Uuid uuid; bool is_primary; uint16_t handle; uint16_t s_handle; uint16_t e_handle; list_t* characteristics; /* list of tBTA_GATTC_CHARACTERISTIC */ list_t* included_svc; /* list of tBTA_GATTC_INCLUDED_SVC */ -} __attribute__((packed, aligned(alignof(tBT_UUID)))) tBTA_GATTC_SERVICE; +} __attribute__((packed, aligned(alignof(bluetooth::Uuid)))) tBTA_GATTC_SERVICE; typedef struct { - tBT_UUID uuid; + bluetooth::Uuid uuid; uint16_t handle; tBTA_GATT_CHAR_PROP properties; tBTA_GATTC_SERVICE* service; /* owning service*/ list_t* descriptors; /* list of tBTA_GATTC_DESCRIPTOR */ -} __attribute__((packed, aligned(alignof(tBT_UUID)))) tBTA_GATTC_CHARACTERISTIC; +} __attribute__((packed, aligned(alignof(bluetooth::Uuid)))) +tBTA_GATTC_CHARACTERISTIC; typedef struct { - tBT_UUID uuid; + bluetooth::Uuid uuid; uint16_t handle; tBTA_GATTC_CHARACTERISTIC* characteristic; /* owning characteristic */ } __attribute__((packed)) tBTA_GATTC_DESCRIPTOR; typedef struct { - tBT_UUID uuid; + bluetooth::Uuid uuid; uint16_t handle; tBTA_GATTC_SERVICE* owning_service; /* owning service*/ tBTA_GATTC_SERVICE* included_service; @@ -693,14 +693,14 @@ extern void BTA_GATTC_Close(uint16_t conn_id); * ******************************************************************************/ extern void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id, - tBT_UUID* p_srvc_uuid); + bluetooth::Uuid* p_srvc_uuid); /** * This function is called to send "Find service by UUID" request. Used only for * PTS tests. */ extern void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id, - tBT_UUID* p_srvc_uuid); + const bluetooth::Uuid& p_srvc_uuid); /******************************************************************************* * @@ -790,7 +790,7 @@ void BTA_GATTC_ReadCharacteristic(uint16_t conn_id, uint16_t handle, * This function is called to read a value of characteristic with uuid equal to * |uuid| */ -void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, tBT_UUID uuid, +void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, const bluetooth::Uuid& uuid, uint16_t s_handle, uint16_t e_handle, tBTA_GATT_AUTH_REQ auth_req, GATT_READ_OP_CB callback, void* cb_data); @@ -1020,7 +1020,7 @@ extern void BTA_GATTS_Disable(void); * Returns None * ******************************************************************************/ -extern void BTA_GATTS_AppRegister(tBT_UUID* p_app_uuid, +extern void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid, tBTA_GATTS_CBACK* p_cback); /******************************************************************************* diff --git a/bta/include/bta_jv_api.h b/bta/include/bta_jv_api.h index 54824c401..dfa4f1381 100644 --- a/bta/include/bta_jv_api.h +++ b/bta/include/bta_jv_api.h @@ -475,7 +475,8 @@ tBTA_JV_STATUS BTA_JvFreeChannel(uint16_t channel, int conn_type); * ******************************************************************************/ tBTA_JV_STATUS BTA_JvStartDiscovery(const RawAddress& bd_addr, - uint16_t num_uuid, tSDP_UUID* p_uuid_list, + uint16_t num_uuid, + const bluetooth::Uuid* p_uuid_list, uint32_t rfcomm_slot_id); /******************************************************************************* diff --git a/bta/include/bta_sdp_api.h b/bta/include/bta_sdp_api.h index 4dc5885d2..ec0e692a2 100644 --- a/bta/include/bta_sdp_api.h +++ b/bta/include/bta_sdp_api.h @@ -31,6 +31,8 @@ #include "bta_api.h" #include "btm_api.h" +using bluetooth::Uuid; + /* status values */ #define BTA_SDP_SUCCESS 0 /* Successful operation. */ #define BTA_SDP_FAILURE 1 /* Generic failure. */ @@ -55,7 +57,7 @@ typedef uint16_t tBTA_SDP_EVT; typedef struct { tBTA_SDP_STATUS status; RawAddress remote_addr; - tBT_UUID uuid; + bluetooth::Uuid uuid; int record_count; bluetooth_sdp_record records[BTA_SDP_MAX_RECORDS]; } tBTA_SDP_SEARCH_COMP; @@ -104,7 +106,7 @@ extern tBTA_SDP_STATUS BTA_SdpEnable(tBTA_SDP_DM_CBACK* p_cback); * ******************************************************************************/ extern tBTA_SDP_STATUS BTA_SdpSearch(const RawAddress& bd_addr, - tSDP_UUID* uuid); + const bluetooth::Uuid& uuid); /******************************************************************************* * diff --git a/bta/jv/bta_jv_act.cc b/bta/jv/bta_jv_act.cc index deba61132..408b9d2d2 100644 --- a/bta/jv/bta_jv_act.cc +++ b/bta/jv/bta_jv_act.cc @@ -22,6 +22,7 @@ * ******************************************************************************/ #include +#include #include #include #include @@ -48,6 +49,8 @@ #include "osi/include/osi.h" +using bluetooth::Uuid; + /* one of these exists for each client */ struct fc_client { struct fc_client* next_all_list; @@ -81,13 +84,6 @@ static void fcchan_conn_chng_cbk(uint16_t chan, const RawAddress& bd_addr, static void fcchan_data_cbk(uint16_t chan, const RawAddress& bd_addr, BT_HDR* p_buf); -extern void uuid_to_string_legacy(bt_uuid_t* p_uuid, char* str, size_t str_len); -static inline void logu(const char* title, const uint8_t* p_uuid) { - char uuids[128]; - uuid_to_string_legacy((bt_uuid_t*)p_uuid, uuids, sizeof(uuids)); - APPL_TRACE_DEBUG("%s: %s", title, uuids); -} - static tBTA_JV_PCB* bta_jv_add_rfc_port(tBTA_JV_RFC_CB* p_cb, tBTA_JV_PCB* p_pcb_open); static tBTA_JV_STATUS bta_jv_free_set_pm_profile_cb(uint32_t jv_handle); @@ -772,36 +768,6 @@ void bta_jv_free_scn(tBTA_JV_MSG* p_data) { break; } } -static inline tBT_UUID shorten_sdp_uuid(const tBT_UUID* u) { - static uint8_t bt_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; - - logu("in, uuid:", u->uu.uuid128); - APPL_TRACE_DEBUG("uuid len:%d", u->len); - if (u->len == 16) { - if (memcmp(&u->uu.uuid128[4], &bt_base_uuid[4], 12) == 0) { - tBT_UUID su; - memset(&su, 0, sizeof(su)); - if (u->uu.uuid128[0] == 0 && u->uu.uuid128[1] == 0) { - su.len = 2; - uint16_t u16; - memcpy(&u16, &u->uu.uuid128[2], sizeof(u16)); - su.uu.uuid16 = ntohs(u16); - APPL_TRACE_DEBUG("shorten to 16 bits uuid: %x", su.uu.uuid16); - } else { - su.len = 4; - uint32_t u32; - memcpy(&u32, &u->uu.uuid128[0], sizeof(u32)); - su.uu.uuid32 = ntohl(u32); - APPL_TRACE_DEBUG("shorten to 32 bits uuid: %x", su.uu.uuid32); - } - return su; - } - } - APPL_TRACE_DEBUG("cannot shorten none-reserved 128 bits uuid"); - return *u; -} /******************************************************************************* * @@ -826,11 +792,9 @@ static void bta_jv_start_discovery_cback(uint16_t result, void* user_data) { if (result == SDP_SUCCESS || result == SDP_DB_FULL) { tSDP_DISC_REC* p_sdp_rec = NULL; tSDP_PROTOCOL_ELEM pe; - logu("bta_jv_cb.uuid", bta_jv_cb.uuid.uu.uuid128); - tBT_UUID su = shorten_sdp_uuid(&bta_jv_cb.uuid); - logu("shorten uuid:", su.uu.uuid128); - p_sdp_rec = - SDP_FindServiceUUIDInDb(p_bta_jv_cfg->p_sdp_db, &su, p_sdp_rec); + APPL_TRACE_DEBUG("bta_jv_cb.uuid: %s", bta_jv_cb.uuid.ToString().c_str()); + p_sdp_rec = SDP_FindServiceUUIDInDb(p_bta_jv_cfg->p_sdp_db, + bta_jv_cb.uuid, p_sdp_rec); APPL_TRACE_DEBUG("p_sdp_rec:%p", p_sdp_rec); if (p_sdp_rec && SDP_FindProtocolListElemInRec(p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) { diff --git a/bta/jv/bta_jv_api.cc b/bta/jv/bta_jv_api.cc index 0478a3b02..f4db9d8a4 100644 --- a/bta/jv/bta_jv_api.cc +++ b/bta/jv/bta_jv_api.cc @@ -34,6 +34,8 @@ #include "sdp_api.h" #include "utl.h" +using bluetooth::Uuid; + /***************************************************************************** * Constants ****************************************************************************/ @@ -214,7 +216,7 @@ tBTA_JV_STATUS BTA_JvFreeChannel(uint16_t channel, int conn_type) { * ******************************************************************************/ tBTA_JV_STATUS BTA_JvStartDiscovery(const RawAddress& bd_addr, - uint16_t num_uuid, tSDP_UUID* p_uuid_list, + uint16_t num_uuid, const Uuid* p_uuid_list, uint32_t rfcomm_slot_id) { tBTA_JV_API_START_DISCOVERY* p_msg = (tBTA_JV_API_START_DISCOVERY*)osi_malloc( sizeof(tBTA_JV_API_START_DISCOVERY)); @@ -224,7 +226,7 @@ tBTA_JV_STATUS BTA_JvStartDiscovery(const RawAddress& bd_addr, p_msg->hdr.event = BTA_JV_API_START_DISCOVERY_EVT; p_msg->bd_addr = bd_addr; p_msg->num_uuid = num_uuid; - memcpy(p_msg->uuid_list, p_uuid_list, num_uuid * sizeof(tSDP_UUID)); + memcpy(p_msg->uuid_list, p_uuid_list, num_uuid * sizeof(Uuid)); p_msg->num_attr = 0; p_msg->rfcomm_slot_id = rfcomm_slot_id; diff --git a/bta/jv/bta_jv_int.h b/bta/jv/bta_jv_int.h index 94ee08584..1a0d988be 100644 --- a/bta/jv/bta_jv_int.h +++ b/bta/jv/bta_jv_int.h @@ -80,7 +80,7 @@ typedef struct { BT_HDR hdr; RawAddress bd_addr; uint16_t num_uuid; - tSDP_UUID uuid_list[BTA_JV_MAX_UUIDS]; + bluetooth::Uuid uuid_list[BTA_JV_MAX_UUIDS]; uint16_t num_attr; uint16_t attr_list[BTA_JV_MAX_ATTRS]; uint32_t rfcomm_slot_id; @@ -361,7 +361,7 @@ typedef struct { uint16_t free_psm_list[BTA_JV_MAX_L2C_CONN]; /* PSMs freed by java (can be reused) */ uint8_t sdp_active; /* see BTA_JV_SDP_ACT_* */ - tSDP_UUID uuid; /* current uuid of sdp discovery*/ + bluetooth::Uuid uuid; /* current uuid of sdp discovery*/ tBTA_JV_PM_CB pm_cb[BTA_JV_PM_MAX_NUM]; /* PM on a per JV handle bases */ } tBTA_JV_CB; diff --git a/bta/mce/bta_mce_act.cc b/bta/mce/bta_mce_act.cc index cd9cdbab7..3c82dd6d4 100644 --- a/bta/mce/bta_mce_act.cc +++ b/bta/mce/bta_mce_act.cc @@ -38,12 +38,14 @@ #include "sdp_api.h" #include "utl.h" +using bluetooth::Uuid; + /***************************************************************************** * Constants ****************************************************************************/ -static const tBT_UUID bta_mce_mas_uuid = { - .len = 2, .uu.uuid16 = UUID_SERVCLASS_MESSAGE_ACCESS}; +static const Uuid bta_mce_mas_uuid = + Uuid::From16Bit(UUID_SERVCLASS_MESSAGE_ACCESS); /******************************************************************************* * @@ -75,8 +77,8 @@ static void bta_mce_search_cback(uint16_t result, void* user_data) { tSDP_DISC_ATTR* p_attr; tSDP_PROTOCOL_ELEM pe; - p_rec = SDP_FindServiceUUIDInDb(p_bta_mce_cfg->p_sdp_db, - (tBT_UUID*)&bta_mce_mas_uuid, p_rec); + p_rec = SDP_FindServiceUUIDInDb(p_bta_mce_cfg->p_sdp_db, bta_mce_mas_uuid, + p_rec); APPL_TRACE_DEBUG("p_rec:%p", p_rec); @@ -162,7 +164,7 @@ void bta_mce_get_remote_mas_instances(tBTA_MCE_MSG* p_data) { bta_mce_cb.remote_addr = p_data->get_rmt_mas.bd_addr; SDP_InitDiscoveryDb(p_bta_mce_cfg->p_sdp_db, p_bta_mce_cfg->sdp_db_size, 1, - (tBT_UUID*)&bta_mce_mas_uuid, 0, NULL); + &bta_mce_mas_uuid, 0, NULL); if (!SDP_ServiceSearchAttributeRequest2(p_data->get_rmt_mas.bd_addr, p_bta_mce_cfg->p_sdp_db, diff --git a/bta/sdp/bta_sdp_act.cc b/bta/sdp/bta_sdp_act.cc index 89e9f6b7e..128c07146 100644 --- a/bta/sdp/bta_sdp_act.cc +++ b/bta/sdp/bta_sdp_act.cc @@ -42,55 +42,11 @@ * Constants ****************************************************************************/ -static const uint8_t UUID_OBEX_OBJECT_PUSH[] = { - 0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_PBAP_PSE[] = {0x00, 0x00, 0x11, 0x2F, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_MAP_MAS[] = {0x00, 0x00, 0x11, 0x32, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_MAP_MNS[] = {0x00, 0x00, 0x11, 0x33, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_SAP[] = {0x00, 0x00, 0x11, 0x2D, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; -// TODO: -// Both the fact that the UUIDs are declared in multiple places, plus the fact -// that there is a mess of UUID comparison and shortening methods will have to -// be fixed. -// The btcore->uuid module should be used for all instances. - -#define UUID_MAX_LENGTH 16 -#define IS_UUID(u1, u2) !memcmp(u1, u2, UUID_MAX_LENGTH) - -static inline tBT_UUID shorten_sdp_uuid(const tBT_UUID* u) { - static uint8_t bt_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; - - APPL_TRACE_DEBUG("%s() - uuid len:%d", __func__, u->len); - if (u->len != 16) return *u; - - if (memcmp(&u->uu.uuid128[4], &bt_base_uuid[4], 12) != 0) return *u; - - tBT_UUID su; - memset(&su, 0, sizeof(su)); - if (u->uu.uuid128[0] == 0 && u->uu.uuid128[1] == 0) { - su.len = 2; - uint16_t u16; - memcpy(&u16, &u->uu.uuid128[2], sizeof(u16)); - su.uu.uuid16 = ntohs(u16); - } else { - su.len = 4; - uint32_t u32; - memcpy(&u32, &u->uu.uuid128[0], sizeof(u32)); - su.uu.uuid32 = ntohl(u32); - } - return su; -} +static const Uuid UUID_OBEX_OBJECT_PUSH = Uuid::From16Bit(0x1105); +static const Uuid UUID_PBAP_PSE = Uuid::From16Bit(0x112F); +static const Uuid UUID_MAP_MAS = Uuid::From16Bit(0x1132); +static const Uuid UUID_MAP_MNS = Uuid::From16Bit(0x1133); +static const Uuid UUID_SAP = Uuid::From16Bit(0x112D); static void bta_create_mns_sdp_record(bluetooth_sdp_record* record, tSDP_DISC_REC* p_rec) { @@ -373,74 +329,63 @@ static void bta_create_raw_sdp_record(bluetooth_sdp_record* record, record->hdr.user1_ptr = p_bta_sdp_cfg->p_sdp_db->raw_data; } -/******************************************************************************* - * - * Function bta_sdp_search_cback - * - * Description Callback from btm after search is completed - * - * Returns void - * - ******************************************************************************/ +/** Callback from btm after search is completed */ static void bta_sdp_search_cback(uint16_t result, void* user_data) { - tSDP_DISC_REC* p_rec = NULL; - tBTA_SDP_SEARCH_COMP evt_data; tBTA_SDP_STATUS status = BTA_SDP_FAILURE; int count = 0; - tBT_UUID su; APPL_TRACE_DEBUG("%s() - res: 0x%x", __func__, result); - memset(&evt_data, 0, sizeof(evt_data)); bta_sdp_cb.sdp_active = BTA_SDP_ACTIVE_NONE; if (bta_sdp_cb.p_dm_cback == NULL) return; + Uuid& uuid = *(reinterpret_cast(user_data)); + + tBTA_SDP_SEARCH_COMP evt_data; + memset(&evt_data, 0, sizeof(evt_data)); evt_data.remote_addr = bta_sdp_cb.remote_addr; - tBT_UUID* uuid = (tBT_UUID*)user_data; - memcpy(&evt_data.uuid, uuid, sizeof(tBT_UUID)); - su = shorten_sdp_uuid(uuid); + evt_data.uuid = uuid; if (result == SDP_SUCCESS || result == SDP_DB_FULL) { + tSDP_DISC_REC* p_rec = NULL; do { - p_rec = SDP_FindServiceUUIDInDb(p_bta_sdp_cfg->p_sdp_db, &su, p_rec); + p_rec = SDP_FindServiceUUIDInDb(p_bta_sdp_cfg->p_sdp_db, uuid, p_rec); /* generate the matching record data pointer */ - if (p_rec != NULL) { - status = BTA_SDP_SUCCESS; - if (IS_UUID(UUID_MAP_MAS, uuid->uu.uuid128)) { - APPL_TRACE_DEBUG("%s() - found MAP (MAS) uuid", __func__); - bta_create_mas_sdp_record(&evt_data.records[count], p_rec); - } else if (IS_UUID(UUID_MAP_MNS, uuid->uu.uuid128)) { - APPL_TRACE_DEBUG("%s() - found MAP (MNS) uuid", __func__); - bta_create_mns_sdp_record(&evt_data.records[count], p_rec); - } else if (IS_UUID(UUID_PBAP_PSE, uuid->uu.uuid128)) { - APPL_TRACE_DEBUG("%s() - found PBAP (PSE) uuid", __func__); - bta_create_pse_sdp_record(&evt_data.records[count], p_rec); - } else if (IS_UUID(UUID_OBEX_OBJECT_PUSH, uuid->uu.uuid128)) { - APPL_TRACE_DEBUG("%s() - found Object Push Server (OPS) uuid", - __func__); - bta_create_ops_sdp_record(&evt_data.records[count], p_rec); - } else if (IS_UUID(UUID_SAP, uuid->uu.uuid128)) { - APPL_TRACE_DEBUG("%s() - found SAP uuid", __func__); - bta_create_sap_sdp_record(&evt_data.records[count], p_rec); - } else { - /* we do not have specific structure for this */ - APPL_TRACE_DEBUG("%s() - profile not identified. using raw data", - __func__); - bta_create_raw_sdp_record(&evt_data.records[count], p_rec); - p_rec = NULL; // Terminate loop - /* For raw, we only extract the first entry, and then return the - entire - raw data chunk. - TODO: Find a way to split the raw data into record chunks, and - iterate - to extract generic data for each chunk - e.g. rfcomm channel - and - service name. */ - } - count++; - } else { + if (!p_rec) { APPL_TRACE_DEBUG("%s() - UUID not found", __func__); + continue; } + + status = BTA_SDP_SUCCESS; + if (uuid == UUID_MAP_MAS) { + APPL_TRACE_DEBUG("%s() - found MAP (MAS) uuid", __func__); + bta_create_mas_sdp_record(&evt_data.records[count], p_rec); + } else if (uuid == UUID_MAP_MNS) { + APPL_TRACE_DEBUG("%s() - found MAP (MNS) uuid", __func__); + bta_create_mns_sdp_record(&evt_data.records[count], p_rec); + } else if (uuid == UUID_PBAP_PSE) { + APPL_TRACE_DEBUG("%s() - found PBAP (PSE) uuid", __func__); + bta_create_pse_sdp_record(&evt_data.records[count], p_rec); + } else if (uuid == UUID_OBEX_OBJECT_PUSH) { + APPL_TRACE_DEBUG("%s() - found Object Push Server (OPS) uuid", + __func__); + bta_create_ops_sdp_record(&evt_data.records[count], p_rec); + } else if (uuid == UUID_SAP) { + APPL_TRACE_DEBUG("%s() - found SAP uuid", __func__); + bta_create_sap_sdp_record(&evt_data.records[count], p_rec); + } else { + /* we do not have specific structure for this */ + APPL_TRACE_DEBUG("%s() - profile not identified. using raw data", + __func__); + bta_create_raw_sdp_record(&evt_data.records[count], p_rec); + p_rec = NULL; // Terminate loop + /* For raw, we only extract the first entry, and then return the + entire raw data chunk. + TODO: Find a way to split the raw data into record chunks, and + iterate to extract generic data for each chunk - e.g. rfcomm + channel and service name. */ + } + count++; } while (p_rec != NULL && count < BTA_SDP_MAX_RECORDS); evt_data.record_count = count; @@ -448,7 +393,7 @@ static void bta_sdp_search_cback(uint16_t result, void* user_data) { evt_data.status = status; bta_sdp_cb.p_dm_cback(BTA_SDP_SEARCH_COMP_EVT, (tBTA_SDP*)&evt_data, - (void*)&uuid->uu.uuid128); + (void*)&uuid); osi_free(user_data); // We no longer need the user data to track the search } @@ -486,13 +431,14 @@ void bta_sdp_search(tBTA_SDP_MSG* p_data) { APPL_TRACE_DEBUG("%s in, sdp_active:%d", __func__, bta_sdp_cb.sdp_active); + const Uuid& uuid = p_data->get_search.uuid; if (bta_sdp_cb.sdp_active != BTA_SDP_ACTIVE_NONE) { /* SDP is still in progress */ status = BTA_SDP_BUSY; if (bta_sdp_cb.p_dm_cback) { tBTA_SDP_SEARCH_COMP result; memset(&result, 0, sizeof(result)); - result.uuid = p_data->get_search.uuid; + result.uuid = uuid; result.remote_addr = p_data->get_search.bd_addr; result.status = status; bta_sdp_cb.p_dm_cback(BTA_SDP_SEARCH_COMP_EVT, (tBTA_SDP*)&result, NULL); @@ -502,20 +448,15 @@ void bta_sdp_search(tBTA_SDP_MSG* p_data) { bta_sdp_cb.sdp_active = BTA_SDP_ACTIVE_YES; bta_sdp_cb.remote_addr = p_data->get_search.bd_addr; - /* set the uuid used in the search */ - tBT_UUID* bta_sdp_search_uuid = - static_cast(osi_malloc(sizeof(tBT_UUID))); - memcpy(bta_sdp_search_uuid, &(p_data->get_search.uuid), sizeof(tBT_UUID)); /* initialize the search for the uuid */ - APPL_TRACE_DEBUG("%s init discovery with UUID(len: %d):", __func__, - bta_sdp_search_uuid->len); - for (int x = 0; x < bta_sdp_search_uuid->len; x++) { - APPL_TRACE_DEBUG("%X", bta_sdp_search_uuid->uu.uuid128[x]); - } + APPL_TRACE_DEBUG("%s init discovery with UUID: %s", __func__, + uuid.ToString().c_str()); SDP_InitDiscoveryDb(p_bta_sdp_cfg->p_sdp_db, p_bta_sdp_cfg->sdp_db_size, 1, - bta_sdp_search_uuid, 0, NULL); + &uuid, 0, NULL); + Uuid* bta_sdp_search_uuid = (Uuid*)osi_malloc(sizeof(Uuid)); + *bta_sdp_search_uuid = uuid; if (!SDP_ServiceSearchAttributeRequest2( p_data->get_search.bd_addr, p_bta_sdp_cfg->p_sdp_db, bta_sdp_search_cback, (void*)bta_sdp_search_uuid)) { @@ -525,7 +466,7 @@ void bta_sdp_search(tBTA_SDP_MSG* p_data) { if (bta_sdp_cb.p_dm_cback) { tBTA_SDP_SEARCH_COMP result; memset(&result, 0, sizeof(result)); - result.uuid = p_data->get_search.uuid; + result.uuid = uuid; result.remote_addr = p_data->get_search.bd_addr; result.status = status; bta_sdp_cb.p_dm_cback(BTA_SDP_SEARCH_COMP_EVT, (tBTA_SDP*)&result, NULL); diff --git a/bta/sdp/bta_sdp_api.cc b/bta/sdp/bta_sdp_api.cc index bc6a9b931..0e01616e8 100644 --- a/bta/sdp/bta_sdp_api.cc +++ b/bta/sdp/bta_sdp_api.cc @@ -87,7 +87,8 @@ tBTA_SDP_STATUS BTA_SdpEnable(tBTA_SDP_DM_CBACK* p_cback) { * BTA_SDP_FAILURE, otherwise. * ******************************************************************************/ -tBTA_SDP_STATUS BTA_SdpSearch(const RawAddress& bd_addr, tSDP_UUID* uuid) { +tBTA_SDP_STATUS BTA_SdpSearch(const RawAddress& bd_addr, + const bluetooth::Uuid& uuid) { tBTA_SDP_API_SEARCH* p_msg = (tBTA_SDP_API_SEARCH*)osi_malloc(sizeof(tBTA_SDP_API_SEARCH)); @@ -95,8 +96,7 @@ tBTA_SDP_STATUS BTA_SdpSearch(const RawAddress& bd_addr, tSDP_UUID* uuid) { p_msg->hdr.event = BTA_SDP_API_SEARCH_EVT; p_msg->bd_addr = bd_addr; - // p_msg->uuid = uuid; - memcpy(&(p_msg->uuid), uuid, sizeof(tSDP_UUID)); + p_msg->uuid = uuid; bta_sys_sendmsg(p_msg); diff --git a/bta/sdp/bta_sdp_int.h b/bta/sdp/bta_sdp_int.h index 0cb5b93ca..c2bf7c73c 100644 --- a/bta/sdp/bta_sdp_int.h +++ b/bta/sdp/bta_sdp_int.h @@ -59,7 +59,7 @@ typedef struct { typedef struct { BT_HDR hdr; RawAddress bd_addr; - tSDP_UUID uuid; + bluetooth::Uuid uuid; } tBTA_SDP_API_SEARCH; /* data type for BTA_SDP_API_SEARCH_EVT */ diff --git a/btcore/Android.bp b/btcore/Android.bp index 398409830..b91091c59 100644 --- a/btcore/Android.bp +++ b/btcore/Android.bp @@ -11,7 +11,6 @@ cc_library_static { "src/module.cc", "src/osi_module.cc", "src/property.cc", - "src/uuid.cc", ], shared_libs: [ "liblog", @@ -40,7 +39,6 @@ cc_test { srcs: [ "test/device_class_test.cc", "test/property_test.cc", - "test/uuid_test.cc", ], shared_libs: [ "liblog", diff --git a/btcore/include/property.h b/btcore/include/property.h index 40e2f2af2..26b287fa8 100644 --- a/btcore/include/property.h +++ b/btcore/include/property.h @@ -18,6 +18,7 @@ #pragma once +#include #include #include #include @@ -48,7 +49,7 @@ bt_property_t* property_new_discovery_timeout(const uint32_t timeout); bt_property_t* property_new_name(const char* name); bt_property_t* property_new_rssi(const int8_t rssi); bt_property_t* property_new_scan_mode(bt_scan_mode_t scan_mode); -bt_property_t* property_new_uuids(const bt_uuid_t* uuid, size_t count); +bt_property_t* property_new_uuids(const bluetooth::Uuid* uuid, size_t count); // Property resource frees both property and value. void property_free(bt_property_t* property); @@ -76,5 +77,5 @@ uint32_t property_as_discovery_timeout(const bt_property_t* property); const bt_bdname_t* property_as_name(const bt_property_t* property); int8_t property_as_rssi(const bt_property_t* property); bt_scan_mode_t property_as_scan_mode(const bt_property_t* property); -const bt_uuid_t* property_as_uuids(const bt_property_t* property, - size_t* count); +const bluetooth::Uuid* property_as_uuids(const bt_property_t* property, + size_t* count); diff --git a/btcore/include/uuid.h b/btcore/include/uuid.h deleted file mode 100644 index 6114ab384..000000000 --- a/btcore/include/uuid.h +++ /dev/null @@ -1,75 +0,0 @@ -/****************************************************************************** - * - * Copyright (C) 2014 Google, Inc. - * - * 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. - * - ******************************************************************************/ - -#pragma once - -#include -#include - -typedef struct uuid_string_t uuid_string_t; - -// Creates uuid string structure to hold a well formed UUID -// string. Must release resources with |uuid_string_free|. -// Returns NULL if no memory. -uuid_string_t* uuid_string_new(void); - -// Frees a uuid string structure created from |uuid_string_new|. -// |uuid_string| may be NULL. -void uuid_string_free(uuid_string_t* uuid_string); - -// Returns a string pointer to the well formed UUID string -// entry. |uuid_string| must not be NULL. -const char* uuid_string_data(const uuid_string_t* uuid_string); - -// Creates uuid structure from a well formed UUID string -// |uuid_string|. The caller takes ownership of the uuid -// structure and must release resources with |uuid_free|. -// |uuid_string| must not be NULL. -// -// Returns NULL if |uuid_string| is malformed or no memory. -// -// A well formed UUID string is structured like this: -// "00112233-4455-6677-8899-aabbccddeeff" -bt_uuid_t* uuid_new(const char* uuid_string); - -// Frees a uuid structure created from |uuid_new| and friends. -// |uuid| may be NULL. -void uuid_free(bt_uuid_t* uuid); - -// Returns true if the UUID is all zeros, false otherwise. -// |uuid| may not be NULL. -bool uuid_is_empty(const bt_uuid_t* uuid); - -// Returns true if the two UUIDs are equal, false otherwise. -// |first| and |second| may not be NULL. -bool uuid_is_equal(const bt_uuid_t* first, const bt_uuid_t* second); - -// Copies uuid |src| into |dest| and returns a pointer to |dest|. -// |src| and |dest| must not be NULL. -bt_uuid_t* uuid_copy(bt_uuid_t* dest, const bt_uuid_t* src); - -// Converts contents of |uuid| to a well formed UUID string -// |uuid_string| using numbers and lower case letter. |uuid| -// and |uuid_string| must not be NULL. -void uuid_to_string(const bt_uuid_t* uuid, uuid_string_t* uuid_string); - -// Converts contents of |uuid| to a short uuid if possible. Returns -// true if conversion is possible, false otherwise. -// |uuid|, |uuid16| and |uuid32| must not be NULL. -bool uuid_128_to_16(const bt_uuid_t* uuid, uint16_t* uuid16); -bool uuid_128_to_32(const bt_uuid_t* uuid, uint32_t* uuid32); diff --git a/btcore/src/property.cc b/btcore/src/property.cc index ad375e183..be0885660 100644 --- a/btcore/src/property.cc +++ b/btcore/src/property.cc @@ -20,10 +20,11 @@ #include #include #include "btcore/include/device_class.h" -#include "btcore/include/uuid.h" #include "osi/include/allocator.h" #include "osi/include/compat.h" +using bluetooth::Uuid; + static bt_property_t* property_new_(void* val, size_t len, bt_property_type_t type); @@ -111,10 +112,9 @@ bt_property_t* property_new_scan_mode(bt_scan_mode_t scan_mode) { BT_PROPERTY_ADAPTER_SCAN_MODE); } -bt_property_t* property_new_uuids(const bt_uuid_t* uuid, size_t count) { +bt_property_t* property_new_uuids(const Uuid* uuid, size_t count) { CHECK(uuid != NULL); - return property_new_((void*)uuid, sizeof(bt_uuid_t) * count, - BT_PROPERTY_UUIDS); + return property_new_((void*)uuid, sizeof(Uuid) * count, BT_PROPERTY_UUIDS); } void property_free(bt_property_t* property) { @@ -208,11 +208,10 @@ bt_scan_mode_t property_as_scan_mode(const bt_property_t* property) { return *(const bt_scan_mode_t*)property->val; } -const bt_uuid_t* property_as_uuids(const bt_property_t* property, - size_t* count) { +const Uuid* property_as_uuids(const bt_property_t* property, size_t* count) { CHECK(property_is_uuids(property)); - *count = sizeof(bt_uuid_t) / property->len; - return (const bt_uuid_t*)property->val; + *count = sizeof(Uuid) / property->len; + return (const Uuid*)property->val; } static bt_property_t* property_new_(void* val, size_t len, diff --git a/btcore/src/uuid.cc b/btcore/src/uuid.cc deleted file mode 100644 index 9985fe6cd..000000000 --- a/btcore/src/uuid.cc +++ /dev/null @@ -1,163 +0,0 @@ -/****************************************************************************** - * - * Copyright (C) 2014 Google, Inc. - * - * 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 -#include -#include - -#include "btcore/include/uuid.h" -#include "osi/include/allocator.h" - -static const size_t UUID_WELL_FORMED_STRING_LEN = 36; -static const size_t UUID_WELL_FORMED_STRING_LEN_WITH_NULL = 36 + 1; - -typedef struct uuid_string_t { char string[0]; } uuid_string_t; - -static const bt_uuid_t empty_uuid = {{0}}; - -// The base UUID is used for calculating 128-bit UUIDs from 16 and -// 32 bit UUIDs as described in the SDP specification. -static const bt_uuid_t base_uuid = {{ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5f, 0x9b, 0x34, 0xfb, -}}; - -static bool uuid_is_base(const bt_uuid_t* uuid); - -uuid_string_t* uuid_string_new(void) { - return static_cast( - osi_calloc(UUID_WELL_FORMED_STRING_LEN_WITH_NULL)); -} - -void uuid_string_free(uuid_string_t* uuid_string) { osi_free(uuid_string); } - -const char* uuid_string_data(const uuid_string_t* uuid_string) { - CHECK(uuid_string != NULL); - return (const char*)uuid_string->string; -} - -bt_uuid_t* uuid_new(const char* uuid_string) { - CHECK(uuid_string != NULL); - - if (strlen(uuid_string) < UUID_WELL_FORMED_STRING_LEN) return NULL; - if (uuid_string[8] != '-' || uuid_string[13] != '-' || - uuid_string[18] != '-' || uuid_string[23] != '-') - return NULL; - - bt_uuid_t* uuid = static_cast(osi_calloc(sizeof(bt_uuid_t))); - - const char* s = uuid_string; - for (size_t i = 0; i < sizeof(bt_uuid_t); ++i, s += 2) { - char buf[3] = {0}; - buf[0] = s[0]; - buf[1] = s[1]; - uuid->uu[i] = strtoul(buf, NULL, 16); - // Adjust by skipping the dashes - switch (i) { - case 3: - case 5: - case 7: - case 9: - s++; - break; - } - } - return uuid; -} - -void uuid_free(bt_uuid_t* uuid) { osi_free(uuid); } - -bool uuid_is_empty(const bt_uuid_t* uuid) { - return !uuid || !memcmp(uuid, &empty_uuid, sizeof(bt_uuid_t)); -} - -bool uuid_is_equal(const bt_uuid_t* first, const bt_uuid_t* second) { - CHECK(first != NULL); - CHECK(second != NULL); - return !memcmp(first, second, sizeof(bt_uuid_t)); -} - -bt_uuid_t* uuid_copy(bt_uuid_t* dest, const bt_uuid_t* src) { - CHECK(dest != NULL); - CHECK(src != NULL); - return (bt_uuid_t*)memcpy(dest, src, sizeof(bt_uuid_t)); -} - -bool uuid_128_to_16(const bt_uuid_t* uuid, uint16_t* uuid16) { - CHECK(uuid != NULL); - CHECK(uuid16 != NULL); - - if (!uuid_is_base(uuid)) return false; - - *uuid16 = (uuid->uu[2] << 8) + uuid->uu[3]; - return true; -} - -bool uuid_128_to_32(const bt_uuid_t* uuid, uint32_t* uuid32) { - CHECK(uuid != NULL); - CHECK(uuid32 != NULL); - - if (!uuid_is_base(uuid)) return false; - - *uuid32 = (uuid->uu[0] << 24) + (uuid->uu[1] << 16) + (uuid->uu[2] << 8) + - uuid->uu[3]; - return true; -} - -void uuid_to_string(const bt_uuid_t* uuid, uuid_string_t* uuid_string) { - CHECK(uuid != NULL); - CHECK(uuid_string != NULL); - - char* string = uuid_string->string; - char* end = string + UUID_WELL_FORMED_STRING_LEN_WITH_NULL; - - // XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX - for (int i = 0; i < 4; i++) { - string += snprintf(string, end - string, "%02x", uuid->uu[i]); - } - *string = '-'; - ++string; - for (int i = 4; i < 6; i++) { - string += snprintf(string, end - string, "%02x", uuid->uu[i]); - } - *string = '-'; - ++string; - for (int i = 6; i < 8; i++) { - string += snprintf(string, end - string, "%02x", uuid->uu[i]); - } - *string = '-'; - ++string; - for (int i = 8; i < 10; i++) { - string += snprintf(string, end - string, "%02x", uuid->uu[i]); - } - *string = '-'; - ++string; - for (int i = 10; i < 16; i++) { - string += snprintf(string, end - string, "%02x", uuid->uu[i]); - } -} - -static bool uuid_is_base(const bt_uuid_t* uuid) { - if (!uuid) return false; - - for (int i = 4; i < 16; i++) { - if (uuid->uu[i] != base_uuid.uu[i]) return false; - } - return true; -} diff --git a/btcore/test/property_test.cc b/btcore/test/property_test.cc index e87c1865b..c1517d94a 100644 --- a/btcore/test/property_test.cc +++ b/btcore/test/property_test.cc @@ -22,6 +22,8 @@ #include "btcore/include/property.h" +using bluetooth::Uuid; + class PropertyTest : public AllocationTestHarness {}; TEST_F(PropertyTest, addr) { @@ -131,38 +133,38 @@ TEST_F(PropertyTest, scan_mode) { } TEST_F(PropertyTest, uuids) { - bt_uuid_t uuid0 = {{ + Uuid uuid0 = Uuid::From128BitBE({{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, - }}; + }}); bt_property_t* property = property_new_uuids(&uuid0, 1); - EXPECT_EQ(0, strcmp((const char*)uuid0.uu, (char*)property->val)); + EXPECT_EQ(0, memcmp(uuid0.To128BitBE().data(), property->val, sizeof(Uuid))); EXPECT_EQ(BT_PROPERTY_UUIDS, property->type); - EXPECT_EQ((int)sizeof(bt_uuid_t), property->len); + EXPECT_EQ((int)sizeof(Uuid), property->len); size_t uuid_cnt1; - const bt_uuid_t* uuid1 = property_as_uuids(property, &uuid_cnt1); - EXPECT_EQ(0, memcmp(uuid1->uu, uuid1->uu, sizeof(bt_uuid_t))); + const Uuid* uuid1 = property_as_uuids(property, &uuid_cnt1); + EXPECT_EQ(uuid0, *uuid1); property_free(property); } TEST_F(PropertyTest, copy) { { - bt_uuid_t uuids[] = { - {{ + Uuid uuids[] = { + Uuid::From128BitBE({{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, - }}, - {{ + }}), + Uuid::From128BitBE({{ 0xf0, 0xe1, 0xd2, 0xc3, 0xf4, 0xe5, 0xd6, 0xc7, 0xf8, 0xe9, 0xda, 0xcb, 0xfc, 0xed, 0xde, 0xcf, - }}, + }}), }; bt_property_t* property0 = - property_new_uuids(uuids, sizeof(bt_uuid_t) / sizeof(uuids)); + property_new_uuids(uuids, sizeof(uuids) / sizeof(Uuid)); bt_property_t property1; property_copy(&property1, property0); diff --git a/btcore/test/uuid_test.cc b/btcore/test/uuid_test.cc deleted file mode 100644 index e9a40edc8..000000000 --- a/btcore/test/uuid_test.cc +++ /dev/null @@ -1,162 +0,0 @@ -/****************************************************************************** - * - * Copyright (C) 2014 Google, Inc. - * - * 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 "osi/test/AllocationTestHarness.h" - -#include "btcore/include/uuid.h" - -static const char* UUID_EMPTY = "00000000-0000-0000-0000-000000000000"; -static const char* UUID_ONES = "11111111-1111-1111-1111-111111111111"; -static const char* UUID_SEQUENTIAL = "01234567-89ab-cdef-ABCD-EF0123456789"; -static const char* UUID_BASE = "00000000-0000-1000-8000-00805f9b34fb"; - -class UuidTest : public AllocationTestHarness { - protected: - virtual void SetUp() {} - - virtual void TearDown() {} -}; - -TEST_F(UuidTest, new_from_string) { - bt_uuid_t* uuid; - - uuid = uuid_new("incorrect length"); - EXPECT_EQ(NULL, uuid); - - uuid = uuid_new("correct length but missing dashes --"); - EXPECT_EQ(NULL, uuid); - - uuid = uuid_new(UUID_ONES); - ASSERT_TRUE(uuid != NULL); - for (int i = 0; i < 16; i++) { - EXPECT_EQ(0x11, uuid->uu[i]); - } - uuid_free(uuid); - - uuid = uuid_new(UUID_SEQUENTIAL); - EXPECT_EQ(0x01, uuid->uu[0]); - EXPECT_EQ(0x23, uuid->uu[1]); - EXPECT_EQ(0x45, uuid->uu[2]); - EXPECT_EQ(0x67, uuid->uu[3]); - EXPECT_EQ(0x89, uuid->uu[4]); - EXPECT_EQ(0xAB, uuid->uu[5]); - EXPECT_EQ(0xCD, uuid->uu[6]); - EXPECT_EQ(0xEF, uuid->uu[7]); - EXPECT_EQ(0xab, uuid->uu[8]); - EXPECT_EQ(0xcd, uuid->uu[9]); - EXPECT_EQ(0xef, uuid->uu[10]); - EXPECT_EQ(0x01, uuid->uu[11]); - EXPECT_EQ(0x23, uuid->uu[12]); - EXPECT_EQ(0x45, uuid->uu[13]); - EXPECT_EQ(0x67, uuid->uu[14]); - EXPECT_EQ(0x89, uuid->uu[15]); - uuid_free(uuid); - - uuid = uuid_new(UUID_BASE); - EXPECT_EQ(0x00, uuid->uu[0]); - EXPECT_EQ(0x00, uuid->uu[1]); - EXPECT_EQ(0x00, uuid->uu[2]); - EXPECT_EQ(0x00, uuid->uu[3]); - EXPECT_EQ(0x00, uuid->uu[4]); - EXPECT_EQ(0x00, uuid->uu[5]); - EXPECT_EQ(0x10, uuid->uu[6]); - EXPECT_EQ(0x00, uuid->uu[7]); - EXPECT_EQ(0x80, uuid->uu[8]); - EXPECT_EQ(0x00, uuid->uu[9]); - EXPECT_EQ(0x00, uuid->uu[10]); - EXPECT_EQ(0x80, uuid->uu[11]); - EXPECT_EQ(0x5f, uuid->uu[12]); - EXPECT_EQ(0x9b, uuid->uu[13]); - EXPECT_EQ(0x34, uuid->uu[14]); - EXPECT_EQ(0xfb, uuid->uu[15]); - uuid_free(uuid); -} - -TEST_F(UuidTest, uuid_is_empty) { - bt_uuid_t* uuid = NULL; - - uuid = uuid_new(UUID_EMPTY); - ASSERT_TRUE(uuid != NULL); - EXPECT_TRUE(uuid_is_empty(uuid)); - uuid_free(uuid); - - uuid = uuid_new(UUID_BASE); - ASSERT_TRUE(uuid != NULL); - EXPECT_FALSE(uuid_is_empty(uuid)); - uuid_free(uuid); -} - -TEST_F(UuidTest, uuid_128_to_16) { - bt_uuid_t* uuid = NULL; - uint16_t uuid16 = 0xffff; - - uuid = uuid_new(UUID_ONES); - EXPECT_FALSE(uuid_128_to_16(uuid, &uuid16)); - uuid_free(uuid); - EXPECT_EQ((uint16_t)0xffff, uuid16); - - uuid = uuid_new(UUID_BASE); - EXPECT_TRUE(uuid_128_to_16(uuid, &uuid16)); - uuid_free(uuid); - EXPECT_NE((uint16_t)0xffff, uuid16); - EXPECT_EQ((uint16_t)0, uuid16); -} - -TEST_F(UuidTest, uuid_128_to_32) { - bt_uuid_t* uuid = NULL; - uint32_t uuid32 = 0xffffffff; - - uuid = uuid_new(UUID_ONES); - EXPECT_FALSE(uuid_128_to_32(uuid, &uuid32)); - uuid_free(uuid); - EXPECT_EQ((uint32_t)0xffffffff, uuid32); - - uuid = uuid_new(UUID_BASE); - EXPECT_TRUE(uuid_128_to_32(uuid, &uuid32)); - uuid_free(uuid); - EXPECT_NE((uint32_t)0xffffffff, uuid32); - EXPECT_EQ((uint32_t)0, uuid32); -} - -TEST_F(UuidTest, uuid_to_string) { - bt_uuid_t* uuid = NULL; - - uuid_string_t* uuid_string = uuid_string_new(); - EXPECT_TRUE(uuid_string != NULL); - - uuid = uuid_new(UUID_BASE); - EXPECT_TRUE(uuid != NULL); - uuid_to_string(uuid, uuid_string); - uuid_free(uuid); - - EXPECT_TRUE(!strcmp(UUID_BASE, uuid_string_data(uuid_string))); - - uuid = uuid_new(UUID_SEQUENTIAL); - EXPECT_TRUE(uuid != NULL); - - uuid_to_string(uuid, uuid_string); - uuid_free(uuid); - - char lower_case_buf[36 + 1]; - for (int i = 0; i < 36 + 1; i++) { - lower_case_buf[i] = tolower(UUID_SEQUENTIAL[i]); - } - EXPECT_TRUE(!strcmp(lower_case_buf, uuid_string_data(uuid_string))); - uuid_string_free(uuid_string); -} diff --git a/btif/include/btif_api.h b/btif/include/btif_api.h index 739eabebd..dcd01e4c6 100644 --- a/btif/include/btif_api.h +++ b/btif/include/btif_api.h @@ -184,8 +184,8 @@ bt_status_t btif_set_remote_device_property(RawAddress* remote_addr, * Returns bt_status_t * ******************************************************************************/ -bt_status_t btif_get_remote_service_record(RawAddress* remote_addr, - bt_uuid_t* uuid); +bt_status_t btif_get_remote_service_record(const RawAddress& remote_addr, + const bluetooth::Uuid& uuid); /******************************************************************************* * BTIF DM API @@ -329,8 +329,8 @@ bt_status_t btif_dm_get_adapter_property(bt_property_t* prop); * Returns bt_status_t * ******************************************************************************/ -bt_status_t btif_dm_get_remote_service_record(RawAddress* remote_addr, - bt_uuid_t* uuid); +bt_status_t btif_dm_get_remote_service_record(const RawAddress& remote_addr, + const bluetooth::Uuid& uuid); /******************************************************************************* * diff --git a/btif/include/btif_gatt_util.h b/btif/include/btif_gatt_util.h index 731f51938..159d62d8a 100644 --- a/btif/include/btif_gatt_util.h +++ b/btif/include/btif_gatt_util.h @@ -24,15 +24,10 @@ #include "bta/include/bta_gatt_api.h" -void btif_to_bta_uuid(tBT_UUID* p_dest, const bt_uuid_t* p_src); void btif_to_bta_response(tBTA_GATTS_RSP* p_dest, btgatt_response_t* p_src); void btif_to_bta_uuid_mask(tBTM_BLE_PF_COND_MASK* p_mask, - const bt_uuid_t* p_src, const bt_uuid_t* svc_uuid); - -void bta_to_btif_uuid(bt_uuid_t* p_dest, tBT_UUID* p_src); - -uint16_t set_read_value(btgatt_read_params_t* p_dest, tBTA_GATTC_READ* p_src); -uint16_t get_uuid16(tBT_UUID* p_uuid); + const bluetooth::Uuid& p_src, + const bluetooth::Uuid& svc_uuid); void btif_gatt_check_encrypted_link(RawAddress bd_addr, tBTA_GATT_TRANSPORT transport); diff --git a/btif/include/btif_sock_rfc.h b/btif/include/btif_sock_rfc.h index 5c588b503..8e9660867 100644 --- a/btif/include/btif_sock_rfc.h +++ b/btif/include/btif_sock_rfc.h @@ -31,12 +31,12 @@ bt_status_t btsock_rfc_init(int handle, uid_set_t* set); bt_status_t btsock_rfc_cleanup(); -bt_status_t btsock_rfc_listen(const char* name, const uint8_t* uuid, +bt_status_t btsock_rfc_listen(const char* name, const bluetooth::Uuid* uuid, int channel, int* sock_fd, int flags, int app_uid); -bt_status_t btsock_rfc_connect(const RawAddress* bd_addr, const uint8_t* uuid, - int channel, int* sock_fd, int flags, - int app_uid); +bt_status_t btsock_rfc_connect(const RawAddress* bd_addr, + const bluetooth::Uuid* uuid, int channel, + int* sock_fd, int flags, int app_uid); void btsock_rfc_signaled(int fd, int flags, uint32_t user_id); #endif diff --git a/btif/include/btif_sock_sdp.h b/btif/include/btif_sock_sdp.h index 7672c7cdf..2b1467299 100644 --- a/btif/include/btif_sock_sdp.h +++ b/btif/include/btif_sock_sdp.h @@ -19,34 +19,21 @@ #ifndef BTIF_SOCK_SDP_H #define BTIF_SOCK_SDP_H +#include #include #include #include -static const uint8_t UUID_OBEX_OBJECT_PUSH[] = { - 0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_PBAP_PSE[] = {0x00, 0x00, 0x11, 0x2F, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_MAP_MAS[] = {0x00, 0x00, 0x11, 0x32, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_SAP[] = {0x00, 0x00, 0x11, 0x2D, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_SPP[] = {0x00, 0x00, 0x11, 0x01, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; +static const bluetooth::Uuid UUID_OBEX_OBJECT_PUSH = + bluetooth::Uuid::From16Bit(0x1105); +static const bluetooth::Uuid UUID_PBAP_PSE = bluetooth::Uuid::From16Bit(0x112F); +static const bluetooth::Uuid UUID_MAP_MAS = bluetooth::Uuid::From16Bit(0x1132); +static const bluetooth::Uuid UUID_SAP = bluetooth::Uuid::From16Bit(0x112D); +static const bluetooth::Uuid UUID_SPP = bluetooth::Uuid::From16Bit(0x1101); -static inline bool is_uuid_empty(const uint8_t* uuid) { - static uint8_t empty_uuid[16]; - return uuid == NULL || memcmp(uuid, empty_uuid, sizeof(empty_uuid)) == 0; -} - -int add_rfc_sdp_rec(const char* name, const uint8_t* uuid, int scn); +int add_rfc_sdp_rec(const char* name, bluetooth::Uuid uuid, int scn); void del_rfc_sdp_rec(int handle); bool is_reserved_rfc_channel(int channel); -int get_reserved_rfc_channel(const uint8_t* uuid); +int get_reserved_rfc_channel(const bluetooth::Uuid& uuid); #endif diff --git a/btif/include/btif_storage.h b/btif/include/btif_storage.h index 1f7efd3ff..5affe2b50 100644 --- a/btif/include/btif_storage.h +++ b/btif/include/btif_storage.h @@ -19,6 +19,7 @@ #ifndef BTIF_STORAGE_H #define BTIF_STORAGE_H +#include #include #include "bt_target.h" @@ -272,7 +273,7 @@ bool btif_storage_get_stored_remote_name(const RawAddress& bd_addr, char* name); /****************************************************************************** * Exported for unit tests *****************************************************************************/ -size_t btif_split_uuids_string(const char* str, bt_uuid_t* p_uuid, +size_t btif_split_uuids_string(const char* str, bluetooth::Uuid* p_uuid, size_t max_uuids); #endif /* BTIF_STORAGE_H */ diff --git a/btif/include/btif_util.h b/btif/include/btif_util.h index a1a6492e3..b7d9a82e9 100644 --- a/btif/include/btif_util.h +++ b/btif/include/btif_util.h @@ -65,17 +65,7 @@ const char* dump_rc_pdu(uint8_t pdu); uint32_t devclass2uint(DEV_CLASS dev_class); void uint2devclass(uint32_t dev, DEV_CLASS dev_class); -void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128); - -// Takes a |str| containing a 128-bit GUID formatted UUID and stores the -// result in |p_uuid|. |str| must be formatted in this format: -// "12345678-1234-1234-1234-123456789012" -// |p_uuid| cannot be null. Returns true if parsing was successful, false -// otherwise. Returns false if |str| is null. -bool string_to_uuid(const char* str, bt_uuid_t* p_uuid); int ascii_2_hex(const char* p_ascii, int len, uint8_t* p_hex); -void uuid_to_string_legacy(bt_uuid_t* p_uuid, char* str, size_t str_len); - #endif /* BTIF_UTIL_H */ diff --git a/btif/src/bluetooth.cc b/btif/src/bluetooth.cc index 1d2c66a34..9565a7f1a 100644 --- a/btif/src/bluetooth.cc +++ b/btif/src/bluetooth.cc @@ -212,7 +212,8 @@ int set_remote_device_property(RawAddress* remote_addr, return btif_set_remote_device_property(remote_addr, property); } -int get_remote_service_record(RawAddress* remote_addr, bt_uuid_t* uuid) { +int get_remote_service_record(const RawAddress& remote_addr, + const bluetooth::Uuid& uuid) { /* sanity check */ if (interface_ready() == false) return BT_STATUS_NOT_READY; diff --git a/btif/src/btif_ble_scanner.cc b/btif/src/btif_ble_scanner.cc index 02538c6e5..98d4b1940 100644 --- a/btif/src/btif_ble_scanner.cc +++ b/btif/src/btif_ble_scanner.cc @@ -282,8 +282,8 @@ class BleScannerInterfaceImpl : public BleScannerInterface { void ScanFilterAddRemove(int action, int filt_type, int filt_index, int company_id, int company_id_mask, - const bt_uuid_t* p_uuid, - const bt_uuid_t* p_uuid_mask, + const bluetooth::Uuid* p_uuid, + const bluetooth::Uuid* p_uuid_mask, const RawAddress* bd_addr, char addr_type, vector data, vector mask, FilterConfigCallback cb) override { @@ -314,24 +314,21 @@ class BleScannerInterfaceImpl : public BleScannerInterface { case BTM_BLE_PF_SRVC_UUID: case BTM_BLE_PF_SRVC_SOL_UUID: { - tBT_UUID bt_uuid; - btif_to_bta_uuid(&bt_uuid, p_uuid); - if (p_uuid_mask == NULL) { do_in_bta_thread( FROM_HERE, base::Bind(&BTM_LE_PF_uuid_filter, action, filt_index, filt_type, - bt_uuid, BTM_BLE_PF_LOGIC_AND, nullptr, + *p_uuid, BTM_BLE_PF_LOGIC_AND, nullptr, jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type)))); return; } tBTM_BLE_PF_COND_MASK* mask = new tBTM_BLE_PF_COND_MASK; - btif_to_bta_uuid_mask(mask, p_uuid_mask, p_uuid); + btif_to_bta_uuid_mask(mask, *p_uuid_mask, *p_uuid); do_in_bta_thread( FROM_HERE, base::Bind(&BTM_LE_PF_uuid_filter, action, filt_index, filt_type, - bt_uuid, BTM_BLE_PF_LOGIC_AND, base::Owned(mask), + *p_uuid, BTM_BLE_PF_LOGIC_AND, base::Owned(mask), jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type)))); return; } diff --git a/btif/src/btif_core.cc b/btif/src/btif_core.cc index febf94579..e7a4ca182 100644 --- a/btif/src/btif_core.cc +++ b/btif/src/btif_core.cc @@ -65,6 +65,7 @@ #include "osi/include/thread.h" #include "stack_manager.h" +using bluetooth::Uuid; /******************************************************************************* * Constants & Macros ******************************************************************************/ @@ -608,7 +609,7 @@ static bt_status_t btif_in_get_adapter_properties(void) { bt_scan_mode_t mode; uint32_t disc_timeout; RawAddress bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS]; - bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; + Uuid local_uuids[BT_MAX_NUM_UUIDS]; bt_status_t status; /* RawAddress */ @@ -666,7 +667,7 @@ static bt_status_t btif_in_get_remote_device_properties(RawAddress* bd_addr) { bt_bdname_t name, alias; uint32_t cod, devtype; - bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS]; + Uuid remote_uuids[BT_MAX_NUM_UUIDS]; memset(remote_properties, 0, sizeof(remote_properties)); BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME, @@ -1095,8 +1096,8 @@ bt_status_t btif_set_remote_device_property(RawAddress* remote_addr, * Returns bt_status_t * ******************************************************************************/ -bt_status_t btif_get_remote_service_record(RawAddress* remote_addr, - bt_uuid_t* uuid) { +bt_status_t btif_get_remote_service_record(const RawAddress& remote_addr, + const Uuid& uuid) { if (!btif_is_enabled()) return BT_STATUS_NOT_READY; return btif_dm_get_remote_service_record(remote_addr, uuid); diff --git a/btif/src/btif_dm.cc b/btif/src/btif_dm.cc index eceab109d..aeb396303 100644 --- a/btif/src/btif_dm.cc +++ b/btif/src/btif_dm.cc @@ -41,6 +41,7 @@ #include +#include #include #include "advertise_data_parser.h" @@ -67,6 +68,7 @@ #include "stack/btm/btm_int.h" #include "stack_config.h" +using bluetooth::Uuid; /****************************************************************************** * Constants & Macros *****************************************************************************/ @@ -182,8 +184,6 @@ typedef struct { #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id)) -#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb" - #define MAX_BTIF_BOND_EVENT_ENTRIES 15 static skip_sdp_entry_t sdp_blacklist[] = {{76}}; // Apple Mouse and Keyboard @@ -247,8 +247,6 @@ extern bt_status_t btif_hh_execute_service(bool b_enable); extern bt_status_t btif_hf_client_execute_service(bool b_enable); extern bt_status_t btif_sdp_execute_service(bool b_enable); extern int btif_hh_connect(const RawAddress* bd_addr); -extern void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128], - uint16_t uuid_16); extern void btif_av_move_idle(RawAddress bd_addr); extern bt_status_t btif_hd_execute_service(bool b_enable); @@ -802,10 +800,10 @@ static void search_services_copy_cb(uint16_t event, char* p_dest, char* p_src) { if (p_src_data->disc_res.result == BTA_SUCCESS) { if (p_src_data->disc_res.num_uuids > 0) { p_dest_data->disc_res.p_uuid_list = - (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH)); + (Uuid*)(p_dest + sizeof(tBTA_DM_SEARCH)); memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list, - p_src_data->disc_res.num_uuids * MAX_UUID_SIZE); + p_src_data->disc_res.num_uuids * sizeof(Uuid)); osi_free_and_reset((void**)&p_src_data->disc_res.p_uuid_list); } osi_free_and_reset((void**)&p_src_data->disc_res.p_raw_data); @@ -1091,14 +1089,11 @@ static void btif_dm_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl) { LOG_WARN(LOG_TAG, "%s: Incoming HID Connection", __func__); bt_property_t prop; RawAddress bd_addr; - bt_uuid_t uuid; - char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE; - - string_to_uuid(uuid_str, &uuid); + Uuid uuid = Uuid::From16Bit(UUID_SERVCLASS_HUMAN_INTERFACE); prop.type = BT_PROPERTY_UUIDS; - prop.val = uuid.uu; - prop.len = MAX_UUID_SIZE; + prop.val = &uuid; + prop.len = Uuid::kNumBytes128; /* Send the event to the BTIF */ HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS, @@ -1392,13 +1387,10 @@ static void btif_dm_search_services_evt(uint16_t event, char* p_param) { if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) { prop.val = p_data->disc_res.p_uuid_list; - prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE; + prop.len = p_data->disc_res.num_uuids * Uuid::kNumBytes128; for (i = 0; i < p_data->disc_res.num_uuids; i++) { - char temp[256]; - uuid_to_string_legacy( - (bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i * MAX_UUID_SIZE)), - temp, sizeof(temp)); - LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp); + std::string temp = ((p_data->disc_res.p_uuid_list + i))->ToString(); + LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp.c_str()); } } @@ -1442,36 +1434,20 @@ static void btif_dm_search_services_evt(uint16_t event, char* p_param) { break; case BTA_DM_DISC_BLE_RES_EVT: { - BTIF_TRACE_DEBUG("%s:, services 0x%x)", __func__, - p_data->disc_ble_res.service.uu.uuid16); - bt_uuid_t uuid; - int i = 0; - int j = 15; + BTIF_TRACE_DEBUG("%s: service %s", __func__, + p_data->disc_ble_res.service.ToString().c_str()); int num_properties = 0; - if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID) { + if (p_data->disc_ble_res.service.As16Bit() == UUID_SERVCLASS_LE_HID) { BTIF_TRACE_DEBUG("%s: Found HOGP UUID", __func__); bt_property_t prop[2]; - char temp[256]; bt_status_t ret; - bta_gatt_convert_uuid16_to_uuid128( - uuid.uu, p_data->disc_ble_res.service.uu.uuid16); - - while (i < j) { - unsigned char c = uuid.uu[j]; - uuid.uu[j] = uuid.uu[i]; - uuid.uu[i] = c; - i++; - j--; - } - - uuid_to_string_legacy(&uuid, temp, sizeof(temp)); - LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp); + const auto& arr = p_data->disc_ble_res.service.To128BitBE(); RawAddress& bd_addr = p_data->disc_ble_res.bd_addr; prop[0].type = BT_PROPERTY_UUIDS; - prop[0].val = uuid.uu; - prop[0].len = MAX_UUID_SIZE; + prop[0].val = (void*)arr.data(); + prop[0].len = Uuid::kNumBytes128; /* Also write this to the NVRAM */ ret = btif_storage_set_remote_device_property(&bd_addr, &prop[0]); @@ -2039,7 +2015,7 @@ static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, case BTA_DM_DISC_RES_EVT: { if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) { - param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE); + param_len += (p_data->disc_res.num_uuids * Uuid::kNumBytes128); } } break; } @@ -2501,16 +2477,10 @@ bt_status_t btif_dm_get_remote_services_by_transport(RawAddress* remote_addr, * * Returns bt_status_t ******************************************************************************/ -bt_status_t btif_dm_get_remote_service_record(RawAddress* remote_addr, - bt_uuid_t* uuid) { - BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, remote_addr->ToString().c_str()); - - tSDP_UUID sdp_uuid; - sdp_uuid.len = MAX_UUID_SIZE; - memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE); - - BTA_DmDiscoverUUID(*remote_addr, &sdp_uuid, bte_dm_remote_service_record_evt, - true); +bt_status_t btif_dm_get_remote_service_record(const RawAddress& remote_addr, + const Uuid& uuid) { + BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, remote_addr.ToString().c_str()); + BTA_DmDiscoverUUID(remote_addr, uuid, bte_dm_remote_service_record_evt, true); return BT_STATUS_SUCCESS; } @@ -2525,7 +2495,7 @@ void btif_dm_execute_service_request(uint16_t event, char* p_param) { btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable); if (status == BT_STATUS_SUCCESS) { bt_property_t property; - bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; + Uuid local_uuids[BT_MAX_NUM_UUIDS]; /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */ BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS, diff --git a/btif/src/btif_gatt_client.cc b/btif/src/btif_gatt_client.cc index 262234b19..7877f47cb 100644 --- a/btif/src/btif_gatt_client.cc +++ b/btif/src/btif_gatt_client.cc @@ -53,6 +53,7 @@ using base::Bind; using base::Owned; +using bluetooth::Uuid; using std::vector; extern bt_status_t btif_gattc_test_command_impl( @@ -214,30 +215,25 @@ void btm_read_rssi_cb(tBTM_RSSI_RESULT* p_result) { * Client API Functions ******************************************************************************/ -bt_status_t btif_gattc_register_app(const bt_uuid_t& uuid) { +bt_status_t btif_gattc_register_app(const Uuid& uuid) { CHECK_BTGATT_INIT(); - tBT_UUID bt_uuid; - btif_to_bta_uuid(&bt_uuid, &uuid); - return do_in_jni_thread(Bind( - [](tBT_UUID bt_uuid) { + [](const Uuid& uuid) { BTA_GATTC_AppRegister( bta_gattc_cback, base::Bind( - [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) { + [](const Uuid& uuid, uint8_t client_id, uint8_t status) { do_in_jni_thread(Bind( - [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) { - bt_uuid_t app_uuid; - bta_to_btif_uuid(&app_uuid, &bt_uuid); + [](const Uuid& uuid, uint8_t client_id, uint8_t status) { HAL_CBACK(bt_gatt_callbacks, client->register_client_cb, - status, client_id, app_uuid); + status, client_id, uuid); }, - bt_uuid, client_id, status)); + uuid, client_id, status)); }, - bt_uuid)); + uuid)); }, - bt_uuid)); + uuid)); } void btif_gattc_unregister_app_impl(int client_if) { @@ -343,13 +339,11 @@ bt_status_t btif_gattc_refresh(int client_if, const RawAddress& bd_addr) { return do_in_jni_thread(Bind(&BTA_GATTC_Refresh, bd_addr)); } -bt_status_t btif_gattc_search_service(int conn_id, - const bt_uuid_t* filter_uuid) { +bt_status_t btif_gattc_search_service(int conn_id, const Uuid* filter_uuid) { CHECK_BTGATT_INIT(); if (filter_uuid) { - tBT_UUID* uuid = new tBT_UUID; - btif_to_bta_uuid(uuid, filter_uuid); + Uuid* uuid = new Uuid(*filter_uuid); return do_in_jni_thread( Bind(&BTA_GATTC_ServiceSearchRequest, conn_id, base::Owned(uuid))); } else { @@ -358,11 +352,8 @@ bt_status_t btif_gattc_search_service(int conn_id, } } -void btif_gattc_discover_service_by_uuid(int conn_id, const bt_uuid_t& p_uuid) { - tBT_UUID* uuid = new tBT_UUID; - btif_to_bta_uuid(uuid, &p_uuid); - do_in_jni_thread( - Bind(&BTA_GATTC_DiscoverServiceByUuid, conn_id, base::Owned(uuid))); +void btif_gattc_discover_service_by_uuid(int conn_id, const Uuid& uuid) { + do_in_jni_thread(Bind(&BTA_GATTC_DiscoverServiceByUuid, conn_id, uuid)); } void btif_gattc_get_gatt_db_impl(int conn_id) { @@ -418,13 +409,11 @@ void read_using_char_uuid_cb(uint16_t conn_id, tGATT_STATUS status, base::Owned(params)); } -bt_status_t btif_gattc_read_using_char_uuid(int conn_id, const bt_uuid_t& uuid, +bt_status_t btif_gattc_read_using_char_uuid(int conn_id, const Uuid& uuid, uint16_t s_handle, uint16_t e_handle, int auth_req) { CHECK_BTGATT_INIT(); - tBT_UUID bt_uuid; - btif_to_bta_uuid(&bt_uuid, &uuid); - return do_in_jni_thread(Bind(&BTA_GATTC_ReadUsingCharUuid, conn_id, bt_uuid, + return do_in_jni_thread(Bind(&BTA_GATTC_ReadUsingCharUuid, conn_id, uuid, s_handle, e_handle, auth_req, read_using_char_uuid_cb, nullptr)); } diff --git a/btif/src/btif_gatt_server.cc b/btif/src/btif_gatt_server.cc index bd3a94ce0..5a7b85092 100644 --- a/btif/src/btif_gatt_server.cc +++ b/btif/src/btif_gatt_server.cc @@ -51,6 +51,7 @@ using base::Bind; using base::Owned; +using bluetooth::Uuid; using std::vector; /******************************************************************************* @@ -128,10 +129,9 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) { tBTA_GATTS* p_data = (tBTA_GATTS*)p_param; switch (event) { case BTA_GATTS_REG_EVT: { - bt_uuid_t app_uuid; - bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.uuid); HAL_CBACK(bt_gatt_callbacks, server->register_server_cb, - p_data->reg_oper.status, p_data->reg_oper.server_if, app_uuid); + p_data->reg_oper.status, p_data->reg_oper.server_if, + p_data->reg_oper.uuid); break; } @@ -266,13 +266,11 @@ static void btapp_gatts_cback(tBTA_GATTS_EVT event, tBTA_GATTS* p_data) { /******************************************************************************* * Server API Functions ******************************************************************************/ -static bt_status_t btif_gatts_register_app(const bt_uuid_t& bt_uuid) { +static bt_status_t btif_gatts_register_app(const Uuid& bt_uuid) { CHECK_BTGATT_INIT(); - tBT_UUID* uuid = new tBT_UUID; - btif_to_bta_uuid(uuid, &bt_uuid); return do_in_jni_thread( - Bind(&BTA_GATTS_AppRegister, base::Owned(uuid), &btapp_gatts_cback)); + Bind(&BTA_GATTS_AppRegister, bt_uuid, &btapp_gatts_cback)); } static bt_status_t btif_gatts_unregister_app(int server_if) { @@ -350,15 +348,11 @@ static bt_status_t btif_gatts_close(int server_if, const RawAddress& bd_addr, static void add_service_impl(int server_if, vector service) { - bt_uuid_t restricted_uuid1, restricted_uuid2; - uuid_128_from_16(&restricted_uuid1, UUID_SERVCLASS_GATT_SERVER); - uuid_128_from_16(&restricted_uuid2, UUID_SERVCLASS_GAP_SERVER); - // TODO(jpawlowski): btif should be a pass through layer, and no checks should // be made here. This exception is added only until GATT server code is // refactored, and one can distinguish stack-internal aps from external apps - if (memcmp(&service[0].uuid, &restricted_uuid1, sizeof(bt_uuid_t)) == 0 || - memcmp(&service[0].uuid, &restricted_uuid2, sizeof(bt_uuid_t)) == 0) { + if (service[0].uuid == Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER) || + service[0].uuid == Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER)) { LOG_ERROR(LOG_TAG, "%s: Attept to register restricted service", __func__); HAL_CBACK(bt_gatt_callbacks, server->service_added_cb, BT_STATUS_FAIL, server_if, std::move(service)); diff --git a/btif/src/btif_gatt_test.cc b/btif/src/btif_gatt_test.cc index 4312533ba..b6a526606 100644 --- a/btif/src/btif_gatt_test.cc +++ b/btif/src/btif_gatt_test.cc @@ -39,6 +39,7 @@ #include "osi/include/log.h" #include "osi/include/osi.h" +using bluetooth::Uuid; /******************************************************************************* * Typedefs & Macros ******************************************************************************/ @@ -65,27 +66,6 @@ static btif_test_cb_t test_cb; * Callback functions ******************************************************************************/ -static char* format_uuid(tBT_UUID bt_uuid, char* str_buf, size_t buf_size) { - if (bt_uuid.len == LEN_UUID_16) { - snprintf(str_buf, buf_size, "0x%04x", bt_uuid.uu.uuid16); - } else if (bt_uuid.len == LEN_UUID_128) { - int x = snprintf(str_buf, buf_size, "%02x%02x%02x%02x-%02x%02x-%02x%02x", - bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14], - bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12], - bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10], - bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]); - snprintf(&str_buf[x], buf_size - x, "%02x%02x-%02x%02x%02x%02x%02x%02x", - bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6], - bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4], - bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2], - bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]); - } else { - snprintf(str_buf, buf_size, "Unknown (len=%d)", bt_uuid.len); - } - - return str_buf; -} - static void btif_test_connect_cback(tGATT_IF, const RawAddress&, uint16_t conn_id, bool connected, tGATT_DISCONN_REASON, tBT_TRANSPORT) { @@ -121,8 +101,6 @@ static void btif_test_command_complete_cback(uint16_t conn_id, tGATTC_OPTYPE op, static void btif_test_discovery_result_cback(UNUSED_ATTR uint16_t conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES* p_data) { - char str_buf[50]; - LOG_DEBUG(LOG_TAG, "------ GATT Discovery result %-22s -------", disc_name[disc_type]); LOG_DEBUG(LOG_TAG, " Attribute handle: 0x%04x (%d)", p_data->handle, @@ -130,7 +108,7 @@ static void btif_test_discovery_result_cback(UNUSED_ATTR uint16_t conn_id, if (disc_type != GATT_DISC_CHAR_DSCPT) { LOG_DEBUG(LOG_TAG, " Attribute type: %s", - format_uuid(p_data->type, str_buf, sizeof(str_buf))); + p_data->type.ToString().c_str()); } switch (disc_type) { @@ -139,8 +117,7 @@ static void btif_test_discovery_result_cback(UNUSED_ATTR uint16_t conn_id, p_data->handle, p_data->value.group_value.e_handle, p_data->handle, p_data->value.group_value.e_handle); LOG_DEBUG(LOG_TAG, " Service UUID: %s", - format_uuid(p_data->value.group_value.service_type, str_buf, - sizeof(str_buf))); + p_data->value.group_value.service_type.ToString().c_str()); break; case GATT_DISC_SRVC_BY_UUID: @@ -156,21 +133,19 @@ static void btif_test_discovery_result_cback(UNUSED_ATTR uint16_t conn_id, p_data->value.incl_service.s_handle, p_data->value.incl_service.e_handle); LOG_DEBUG(LOG_TAG, " Service UUID: %s", - format_uuid(p_data->value.incl_service.service_type, str_buf, - sizeof(str_buf))); + p_data->value.incl_service.service_type.ToString().c_str()); break; case GATT_DISC_CHAR: LOG_DEBUG(LOG_TAG, " Properties: 0x%02x", p_data->value.dclr_value.char_prop); LOG_DEBUG(LOG_TAG, " Characteristic UUID: %s", - format_uuid(p_data->value.dclr_value.char_uuid, str_buf, - sizeof(str_buf))); + p_data->value.dclr_value.char_uuid.ToString().c_str()); break; case GATT_DISC_CHAR_DSCPT: LOG_DEBUG(LOG_TAG, " Descriptor UUID: %s", - format_uuid(p_data->type, str_buf, sizeof(str_buf))); + p_data->type.ToString().c_str()); break; } @@ -205,8 +180,10 @@ bt_status_t btif_gattc_test_command_impl(int command, { LOG_DEBUG(LOG_TAG, "%s: ENABLE - enable=%d", __func__, params->u1); if (params->u1) { - tBT_UUID app_uuid = {LEN_UUID_128, {0xAE}}; - test_cb.gatt_if = GATT_Register(&app_uuid, &btif_test_callbacks); + std::array tmp; + tmp.fill(0xAE); + test_cb.gatt_if = GATT_Register(bluetooth::Uuid::From128BitBE(tmp), + &btif_test_callbacks); GATT_StartIf(test_cb.gatt_if); } else { GATT_Deregister(test_cb.gatt_if); @@ -217,12 +194,8 @@ bt_status_t btif_gattc_test_command_impl(int command, case 0x02: /* Connect */ { - LOG_DEBUG(LOG_TAG, - "%s: CONNECT - device=%02x:%02x:%02x:%02x:%02x:%02x " - "(dev_type=%d, addr_type=%d)", - __func__, params->bda1->address[0], params->bda1->address[1], - params->bda1->address[2], params->bda1->address[3], - params->bda1->address[4], params->bda1->address[5], params->u1, + LOG_DEBUG(LOG_TAG, "%s: CONNECT - device=%s (dev_type=%d, addr_type=%d)", + __func__, params->bda1->ToString().c_str(), params->u1, params->u2); if (params->u1 == BT_DEVICE_TYPE_BLE) @@ -246,7 +219,6 @@ bt_status_t btif_gattc_test_command_impl(int command, case 0x04: /* Discover */ { - char buf[50] = {0}; tGATT_DISC_PARAM param; memset(¶m, 0, sizeof(tGATT_DISC_PARAM)); @@ -258,13 +230,12 @@ bt_status_t btif_gattc_test_command_impl(int command, param.s_handle = params->u2; param.e_handle = params->u3; - btif_to_bta_uuid(¶m.service, params->uuid1); + param.service = *params->uuid1; LOG_DEBUG(LOG_TAG, "%s: DISCOVER (%s), conn_id=%d, uuid=%s, handles=0x%04x-0x%04x", __func__, disc_name[params->u1], test_cb.conn_id, - format_uuid(param.service, buf, sizeof(buf)), params->u2, - params->u3); + param.service.ToString().c_str(), params->u2, params->u3); GATTC_Discover(test_cb.conn_id, params->u1, ¶m); break; } diff --git a/btif/src/btif_gatt_util.cc b/btif/src/btif_gatt_util.cc index 97459179b..15d67e898 100644 --- a/btif/src/btif_gatt_util.cc +++ b/btif/src/btif_gatt_util.cc @@ -40,62 +40,11 @@ #include "btif_util.h" #include "osi/include/osi.h" -#define GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */ -#define GATTC_READ_VALUE_TYPE_AGG_FORMAT \ - 0x2905 /* Characteristic Aggregate Format*/ - -static unsigned char BASE_UUID[16] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, - 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - -int uuidType(const unsigned char* p_uuid) { - int i = 0; - int match = 0; - int all_zero = 1; - - for (i = 0; i != 16; ++i) { - if (i == 12 || i == 13) continue; - - if (p_uuid[i] == BASE_UUID[i]) ++match; - - if (p_uuid[i] != 0) all_zero = 0; - } - if (all_zero) return 0; - if (match == 12) return LEN_UUID_32; - if (match == 14) return LEN_UUID_16; - return LEN_UUID_128; -} +using bluetooth::Uuid; /******************************************************************************* * BTIF -> BTA conversion functions ******************************************************************************/ - -void btif_to_bta_uuid(tBT_UUID* p_dest, const bt_uuid_t* p_src) { - char* p_byte = (char*)p_src; - int i = 0; - - p_dest->len = uuidType(p_src->uu); - - switch (p_dest->len) { - case LEN_UUID_16: - p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12]; - break; - - case LEN_UUID_32: - p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12]; - p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16); - break; - - case LEN_UUID_128: - for (i = 0; i != 16; ++i) p_dest->uu.uuid128[i] = p_byte[i]; - break; - - default: - LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_dest->len); - break; - } -} - void btif_to_bta_response(tBTA_GATTS_RSP* p_dest, btgatt_response_t* p_src) { p_dest->attr_value.auth_req = p_src->attr_value.auth_req; p_dest->attr_value.handle = p_src->attr_value.handle; @@ -105,25 +54,23 @@ void btif_to_bta_response(tBTA_GATTS_RSP* p_dest, btgatt_response_t* p_src) { } void btif_to_bta_uuid_mask(tBTM_BLE_PF_COND_MASK* p_mask, - const bt_uuid_t* uuid_mask, - const bt_uuid_t* svc_uuid) { - char* p_byte = (char*)uuid_mask; - int uuid_len = uuidType(svc_uuid->uu); - int i = 0; + const bluetooth::Uuid& uuid_mask, + const bluetooth::Uuid& svc_uuid) { + // we use svc_uuid for uuid_mask length picking ? + int uuid_len = svc_uuid.GetShortestRepresentationSize(); switch (uuid_len) { - case LEN_UUID_16: - p_mask->uuid16_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; + case Uuid::kNumBytes16: + p_mask->uuid16_mask = uuid_mask.As16Bit(); break; - case LEN_UUID_32: - p_mask->uuid32_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; - p_mask->uuid32_mask += - (uuid_mask->uu[15] << 24) + (uuid_mask->uu[14] << 16); + case Uuid::kNumBytes32: + p_mask->uuid32_mask = uuid_mask.As32Bit(); break; - case LEN_UUID_128: - for (i = 0; i != 16; ++i) p_mask->uuid128_mask[i] = p_byte[i]; + case Uuid::kNumBytes128: + memcpy(p_mask->uuid128_mask, uuid_mask.To128BitLE().data(), + Uuid::kNumBytes128); break; default: @@ -132,81 +79,6 @@ void btif_to_bta_uuid_mask(tBTM_BLE_PF_COND_MASK* p_mask, } /******************************************************************************* - * BTA -> BTIF conversion functions - ******************************************************************************/ - -void bta_to_btif_uuid(bt_uuid_t* p_dest, tBT_UUID* p_src) { - int i = 0; - - if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32) { - for (i = 0; i != 16; ++i) p_dest->uu[i] = BASE_UUID[i]; - } - - switch (p_src->len) { - case 0: - break; - - case LEN_UUID_16: - p_dest->uu[12] = p_src->uu.uuid16 & 0xff; - p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; - break; - - case LEN_UUID_32: - p_dest->uu[12] = p_src->uu.uuid16 & 0xff; - p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; - p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff; - p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff; - break; - - case LEN_UUID_128: - for (i = 0; i != 16; ++i) p_dest->uu[i] = p_src->uu.uuid128[i]; - break; - - default: - LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_src->len); - break; - } -} - -/******************************************************************************* - * Utility functions - ******************************************************************************/ - -uint16_t get_uuid16(tBT_UUID* p_uuid) { - if (p_uuid->len == LEN_UUID_16) { - return p_uuid->uu.uuid16; - } else if (p_uuid->len == LEN_UUID_128) { - uint16_t u16; - uint8_t* p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4]; - STREAM_TO_UINT16(u16, p); - return u16; - } else /* p_uuid->len == LEN_UUID_32 */ - { - return (uint16_t)p_uuid->uu.uuid32; - } -} - -uint16_t set_read_value(btgatt_read_params_t* p_dest, tBTA_GATTC_READ* p_src) { - uint16_t len = 0; - - p_dest->status = p_src->status; - p_dest->handle = p_src->handle; - - if ((p_src->status == BTA_GATT_OK) && (p_src->len != 0)) { - LOG_INFO(LOG_TAG, "%s len = %d ", __func__, p_src->len); - p_dest->value.len = p_src->len; - memcpy(p_dest->value.value, p_src->value, p_src->len); - - len += p_src->len; - } else { - p_dest->value.len = 0; - } - - p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE; - return len; -} - -/******************************************************************************* * Encrypted link map handling ******************************************************************************/ diff --git a/btif/src/btif_sdp.cc b/btif/src/btif_sdp.cc index de39c42c7..145ad8201 100644 --- a/btif/src/btif_sdp.cc +++ b/btif/src/btif_sdp.cc @@ -40,6 +40,8 @@ #include "btif_profile_queue.h" #include "btif_util.h" +using bluetooth::Uuid; + /***************************************************************************** * Functions implemented in sdp_server.c *****************************************************************************/ @@ -64,16 +66,13 @@ static btsdp_callbacks_t* bt_sdp_callbacks = NULL; static void btif_sdp_search_comp_evt(uint16_t event, char* p_param) { tBTA_SDP_SEARCH_COMP* evt_data = (tBTA_SDP_SEARCH_COMP*)p_param; - RawAddress addr; BTIF_TRACE_DEBUG("%s: event = %d", __func__, event); if (event != BTA_SDP_SEARCH_COMP_EVT) return; - addr = evt_data->remote_addr; - HAL_CBACK(bt_sdp_callbacks, sdp_search_cb, (bt_status_t)evt_data->status, - &addr, (uint8_t*)(evt_data->uuid.uu.uuid128), - evt_data->record_count, evt_data->records); + evt_data->remote_addr, evt_data->uuid, evt_data->record_count, + evt_data->records); } static void sdp_search_comp_copy_cb(uint16_t event, char* p_dest, char* p_src) { @@ -137,13 +136,8 @@ static bt_status_t deinit() { return BT_STATUS_SUCCESS; } -static bt_status_t search(RawAddress* bd_addr, const uint8_t* uuid) { - tSDP_UUID sdp_uuid; - sdp_uuid.len = 16; - memcpy(sdp_uuid.uu.uuid128, uuid, sizeof(sdp_uuid.uu.uuid128)); - - BTA_SdpSearch(*bd_addr, &sdp_uuid); - +static bt_status_t search(RawAddress* bd_addr, const Uuid& uuid) { + BTA_SdpSearch(*bd_addr, uuid); return BT_STATUS_SUCCESS; } diff --git a/btif/src/btif_sock.cc b/btif/src/btif_sock.cc index a41ab13e7..6c216b397 100644 --- a/btif/src/btif_sock.cc +++ b/btif/src/btif_sock.cc @@ -36,12 +36,14 @@ #include "btif_util.h" #include "osi/include/thread.h" +using bluetooth::Uuid; + static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, - const uint8_t* uuid, int channel, int* sock_fd, + const Uuid* uuid, int channel, int* sock_fd, int flags, int app_uid); static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type, - const uint8_t* uuid, int channel, - int* sock_fd, int flags, int app_uid); + const Uuid* uuid, int channel, int* sock_fd, + int flags, int app_uid); static void btsock_signaled(int fd, int type, int flags, uint32_t user_id); @@ -120,10 +122,9 @@ void btif_sock_cleanup(void) { } static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, - const uint8_t* service_uuid, int channel, + const Uuid* service_uuid, int channel, int* sock_fd, int flags, int app_uid) { if ((flags & BTSOCK_FLAG_NO_SDP) == 0) { - CHECK(service_uuid != NULL || channel > 0); CHECK(sock_fd != NULL); } @@ -154,9 +155,8 @@ static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, } static bt_status_t btsock_connect(const RawAddress* bd_addr, btsock_type_t type, - const uint8_t* uuid, int channel, - int* sock_fd, int flags, int app_uid) { - CHECK(uuid != NULL || channel > 0); + const Uuid* uuid, int channel, int* sock_fd, + int flags, int app_uid) { CHECK(bd_addr != NULL); CHECK(sock_fd != NULL); diff --git a/btif/src/btif_sock_rfc.cc b/btif/src/btif_sock_rfc.cc index 2ea2a15f9..b3378b566 100644 --- a/btif/src/btif_sock_rfc.cc +++ b/btif/src/btif_sock_rfc.cc @@ -59,6 +59,8 @@ * L2CAP functions from this file. */ #include "btif_sock_l2cap.h" +using bluetooth::Uuid; + // Maximum number of RFCOMM channels (1-30 inclusive). #define MAX_RFC_CHANNEL 30 @@ -82,7 +84,7 @@ typedef struct { int scn_notified; RawAddress addr; int is_service_uuid_valid; - uint8_t service_uuid[16]; + Uuid service_uuid; char service_name[256]; int fd; int app_fd; // Temporary storage for the half of the socketpair that's sent @@ -181,7 +183,7 @@ static bool is_requesting_sdp(void) { } static rfc_slot_t* alloc_rfc_slot(const RawAddress* addr, const char* name, - const uint8_t* uuid, int channel, int flags, + const Uuid& uuid, int channel, int flags, bool server) { int security = 0; if (flags & BTSOCK_FLAG_ENCRYPT) @@ -215,13 +217,9 @@ static rfc_slot_t* alloc_rfc_slot(const RawAddress* addr, const char* name, slot->scn = channel; slot->app_uid = -1; - if (!is_uuid_empty(uuid)) { - memcpy(slot->service_uuid, uuid, sizeof(slot->service_uuid)); - slot->is_service_uuid_valid = true; - } else { - memset(slot->service_uuid, 0, sizeof(slot->service_uuid)); - slot->is_service_uuid_valid = false; - } + slot->is_service_uuid_valid = !uuid.IsEmpty(); + slot->service_uuid = uuid; + if (name && *name) { strlcpy(slot->service_name, name, sizeof(slot->service_name)); } else { @@ -269,7 +267,7 @@ static rfc_slot_t* create_srv_accept_rfc_slot(rfc_slot_t* srv_rs, } bt_status_t btsock_rfc_listen(const char* service_name, - const uint8_t* service_uuid, int channel, + const Uuid* service_uuid, int channel, int* sock_fd, int flags, int app_uid) { CHECK(sock_fd != NULL); CHECK((service_uuid != NULL) || @@ -285,15 +283,16 @@ bt_status_t btsock_rfc_listen(const char* service_name, if (!is_init_done()) return BT_STATUS_NOT_READY; if ((flags & BTSOCK_FLAG_NO_SDP) == 0) { - if (is_uuid_empty(service_uuid)) { + if (!service_uuid || service_uuid->IsEmpty()) { APPL_TRACE_DEBUG( - "BTA_JvGetChannelId: service_uuid not set AND " - "BTSOCK_FLAG_NO_SDP is not set - changing to SPP"); - service_uuid = - UUID_SPP; // Use serial port profile to listen to specified channel + "%s: service_uuid not set AND BTSOCK_FLAG_NO_SDP is not set - " + "changing to SPP", + __func__); + // Use serial port profile to listen to specified channel + service_uuid = &UUID_SPP; } else { // Check the service_uuid. overwrite the channel # if reserved - int reserved_channel = get_reserved_rfc_channel(service_uuid); + int reserved_channel = get_reserved_rfc_channel(*service_uuid); if (reserved_channel > 0) { channel = reserved_channel; } @@ -303,7 +302,7 @@ bt_status_t btsock_rfc_listen(const char* service_name, std::unique_lock lock(slot_lock); rfc_slot_t* slot = - alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true); + alloc_rfc_slot(NULL, service_name, *service_uuid, channel, flags, true); if (!slot) { LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__); return BT_STATUS_FAIL; @@ -329,10 +328,10 @@ bt_status_t btsock_rfc_listen(const char* service_name, } bt_status_t btsock_rfc_connect(const RawAddress* bd_addr, - const uint8_t* service_uuid, int channel, + const Uuid* service_uuid, int channel, int* sock_fd, int flags, int app_uid) { CHECK(sock_fd != NULL); - CHECK(service_uuid != NULL || (channel >= 1 && channel <= MAX_RFC_CHANNEL)); + CHECK((service_uuid != NULL) || (channel >= 1 && channel <= MAX_RFC_CHANNEL)); *sock_fd = INVALID_FD; @@ -345,13 +344,13 @@ bt_status_t btsock_rfc_connect(const RawAddress* bd_addr, std::unique_lock lock(slot_lock); rfc_slot_t* slot = - alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, false); + alloc_rfc_slot(bd_addr, NULL, *service_uuid, channel, flags, false); if (!slot) { LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__); return BT_STATUS_FAIL; } - if (is_uuid_empty(service_uuid)) { + if (!service_uuid || service_uuid->IsEmpty()) { tBTA_JV_STATUS ret = BTA_JvRfcommConnect(slot->security, slot->role, slot->scn, slot->addr, rfcomm_cback, slot->id); @@ -368,12 +367,8 @@ bt_status_t btsock_rfc_connect(const RawAddress* bd_addr, return BT_STATUS_FAIL; } } else { - tSDP_UUID sdp_uuid; - sdp_uuid.len = 16; - memcpy(sdp_uuid.uu.uuid128, service_uuid, sizeof(sdp_uuid.uu.uuid128)); - if (!is_requesting_sdp()) { - BTA_JvStartDiscovery(*bd_addr, 1, &sdp_uuid, slot->id); + BTA_JvStartDiscovery(*bd_addr, 1, service_uuid, slot->id); slot->f.pending_sdp_request = false; slot->f.doing_sdp_request = true; } else { @@ -732,11 +727,7 @@ static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV* p_data, uint32_t id) { // Find the next slot that needs to perform an SDP request and service it. slot = find_rfc_slot_by_pending_sdp(); if (slot) { - tSDP_UUID sdp_uuid; - sdp_uuid.len = 16; - memcpy(sdp_uuid.uu.uuid128, slot->service_uuid, - sizeof(sdp_uuid.uu.uuid128)); - BTA_JvStartDiscovery(slot->addr, 1, &sdp_uuid, slot->id); + BTA_JvStartDiscovery(slot->addr, 1, &slot->service_uuid, slot->id); slot->f.pending_sdp_request = false; slot->f.doing_sdp_request = true; } diff --git a/btif/src/btif_sock_sdp.cc b/btif/src/btif_sock_sdp.cc index afbfab450..cb50f740b 100644 --- a/btif/src/btif_sock_sdp.cc +++ b/btif/src/btif_sock_sdp.cc @@ -43,6 +43,8 @@ #include "sdp_api.h" #include "utl.h" +using bluetooth::Uuid; + // This module provides an abstraction on top of the lower-level SDP database // code for registration and discovery of various bluetooth sockets. // @@ -85,7 +87,6 @@ static const tBTA_OP_FMT bta_ops_obj_fmt[OBEX_PUSH_NUM_FORMATS] = { #define RESERVED_SCN_OPS 12 #define UUID_MAX_LENGTH 16 -#define UUID_MATCHES(u1, u2) !memcmp(u1, u2, UUID_MAX_LENGTH) // Adds a protocol list and service name (if provided) to an SDP record given by // |sdp_handle|, and marks it as browseable. This is a shortcut for defining a @@ -152,15 +153,15 @@ error: // Registers a service with the given |name|, |uuid|, and |channel| in the SDP // database as a generic L2CAP RFCOMM protocol, storing its |uuid| as a service // class sequence. -static int add_sdp_by_uuid(const char* name, const uint8_t* uuid, +static int add_sdp_by_uuid(const char* name, const Uuid& uuid, const uint16_t channel) { - APPL_TRACE_DEBUG("add_sdp_by_uuid: scn: %d, service_name: %s", channel, name); + APPL_TRACE_DEBUG("%s: uuid: %s, scn: %d, service_name: %s", __func__, + uuid.ToString().c_str(), channel, name); uint32_t handle = SDP_CreateRecord(); if (handle == 0) { APPL_TRACE_ERROR( - "add_sdp_by_uuid: failed to create sdp record, " - "scn: %d, service_name: %s", + "%s: failed to create sdp record, scn: %d, service_name: %s", __func__, channel, name); return 0; } @@ -183,7 +184,8 @@ static int add_sdp_by_uuid(const char* name, const uint8_t* uuid, // Do the conversion to big-endian -- tmp is only used to iterate through the // UUID array in the macro and serves no other purpose as the conversion // macros are not hygenic. - { ARRAY_TO_BE_STREAM(tmp, uuid, UUID_MAX_LENGTH); } + + { ARRAY_TO_BE_STREAM(tmp, uuid.To128BitBE().data(), UUID_MAX_LENGTH); } stage = "service_class_sequence"; if (!SDP_AddSequence(handle, (uint16_t)ATTR_ID_SERVICE_CLASS_ID_LIST, 1, @@ -191,17 +193,14 @@ static int add_sdp_by_uuid(const char* name, const uint8_t* uuid, goto error; APPL_TRACE_DEBUG( - "add_sdp_by_uuid: service registered successfully, " - "service_name: %s, handle: 0x%08x", - name, handle); + "%s: service registered successfully, service_name: %s, handle: 0x%08x", + __func__, name, handle); return handle; error: SDP_DeleteRecord(handle); - APPL_TRACE_ERROR( - "add_sdp_by_uuid: failed to register service " - "stage: %s, service_name: %s", - stage, name); + APPL_TRACE_ERROR("%s: failed to register service stage: %s, service_name: %s", + __func__, stage, name); return 0; } @@ -377,10 +376,10 @@ error: // |channel|. This function attempts to identify the type of the service based // upon its |uuid|, and will override the |channel| with a reserved channel // number if the |uuid| matches one of the preregistered bluez SDP records. -static int add_rfc_sdp_by_uuid(const char* name, const uint8_t* uuid, +static int add_rfc_sdp_by_uuid(const char* name, const Uuid& uuid, const int channel) { - APPL_TRACE_DEBUG("add_rfc_sdp_by_uuid: service_name: %s, channel: %d", name, - channel); + APPL_TRACE_DEBUG("%s: uuid: %s, service_name: %s, channel: %d", __func__, + uuid.ToString().c_str(), name, channel); /* * Bluetooth Socket API relies on having preregistered bluez sdp records for @@ -402,14 +401,14 @@ static int add_rfc_sdp_by_uuid(const char* name, const uint8_t* uuid, int handle = 0; - if (UUID_MATCHES(UUID_OBEX_OBJECT_PUSH, uuid)) { + if (uuid == UUID_OBEX_OBJECT_PUSH) { handle = add_ops_sdp(name, final_channel); - } else if (UUID_MATCHES(UUID_PBAP_PSE, uuid)) { + } else if (uuid == UUID_PBAP_PSE) { // PBAP Server is always channel 19 handle = add_pbap_sdp(name, final_channel); - } else if (UUID_MATCHES(UUID_SPP, uuid)) { + } else if (uuid == UUID_SPP) { handle = add_spp_sdp(name, final_channel); - } else if (UUID_MATCHES(UUID_MAP_MAS, uuid)) { + } else if (uuid == UUID_MAP_MAS) { // Record created by new SDP create record interface handle = 0xff; } else { @@ -429,10 +428,10 @@ bool is_reserved_rfc_channel(const int channel) { return false; } -int get_reserved_rfc_channel(const uint8_t* uuid) { - if (UUID_MATCHES(UUID_PBAP_PSE, uuid)) { +int get_reserved_rfc_channel(const bluetooth::Uuid& uuid) { + if (uuid == UUID_PBAP_PSE) { return RESERVED_SCN_PBS; - } else if (UUID_MATCHES(UUID_OBEX_OBJECT_PUSH, uuid)) { + } else if (uuid == UUID_OBEX_OBJECT_PUSH) { return RESERVED_SCN_OPS; } @@ -442,8 +441,8 @@ int get_reserved_rfc_channel(const uint8_t* uuid) { // Adds an SDP record to the SDP database using the given |name|, |uuid|, and // |channel|. Note that if the |uuid| is empty, the |uuid| will be set based // upon the |channel| passed in. -int add_rfc_sdp_rec(const char* name, const uint8_t* uuid, const int channel) { - if (is_uuid_empty(uuid)) { +int add_rfc_sdp_rec(const char* name, Uuid uuid, const int channel) { + if (uuid.IsEmpty()) { switch (channel) { case RESERVED_SCN_PBS: // PBAP Reserved port uuid = UUID_PBAP_PSE; diff --git a/btif/src/btif_storage.cc b/btif/src/btif_storage.cc index e455b5fb4..1692901cd 100644 --- a/btif/src/btif_storage.cc +++ b/btif/src/btif_storage.cc @@ -54,6 +54,8 @@ #include "osi/include/log.h" #include "osi/include/osi.h" +using bluetooth::Uuid; + /******************************************************************************* * Constants & Macros ******************************************************************************/ @@ -228,18 +230,12 @@ static int prop2cfg(const RawAddress* remote_bd_addr, bt_property_t* prop) { *(int*)prop->val); break; case BT_PROPERTY_UUIDS: { - uint32_t i; - char buf[64]; - value[0] = 0; - for (i = 0; i < (prop->len) / sizeof(bt_uuid_t); i++) { - bt_uuid_t* p_uuid = (bt_uuid_t*)prop->val + i; - memset(buf, 0, sizeof(buf)); - uuid_to_string_legacy(p_uuid, buf, sizeof(buf)); - strcat(value, buf); - // strcat(value, ";"); - strcat(value, " "); + std::string val; + size_t cnt = (prop->len) / sizeof(Uuid); + for (size_t i = 0; i < cnt; i++) { + val += (reinterpret_cast(prop->val) + i)->ToString() + " "; } - btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value); + btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, val.c_str()); break; } case BT_PROPERTY_REMOTE_VERSION_INFO: { @@ -344,10 +340,10 @@ static int cfg2prop(const RawAddress* remote_bd_addr, bt_property_t* prop) { int size = sizeof(value); if (btif_config_get_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size)) { - bt_uuid_t* p_uuid = (bt_uuid_t*)prop->val; + Uuid* p_uuid = reinterpret_cast(prop->val); size_t num_uuids = btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS); - prop->len = num_uuids * sizeof(bt_uuid_t); + prop->len = num_uuids * sizeof(Uuid); ret = true; } else { prop->val = NULL; @@ -518,15 +514,21 @@ static void btif_read_le_key(const uint8_t key_type, const size_t key_len, * Returns Number of UUIDs parsed from the supplied string * ******************************************************************************/ -size_t btif_split_uuids_string(const char* str, bt_uuid_t* p_uuid, +size_t btif_split_uuids_string(const char* str, bluetooth::Uuid* p_uuid, size_t max_uuids) { CHECK(str); CHECK(p_uuid); size_t num_uuids = 0; while (str && num_uuids < max_uuids) { - bool rc = string_to_uuid(str, p_uuid++); - if (!rc) break; + bool is_valid; + bluetooth::Uuid tmp = + Uuid::FromString(std::string(str, Uuid::kString128BitLen), &is_valid); + if (!is_valid) break; + + *p_uuid = tmp; + p_uuid++; + num_uuids++; str = strchr(str, ' '); if (str) str++; @@ -585,7 +587,7 @@ bt_status_t btif_storage_get_adapter_property(bt_property_t* property) { return BT_STATUS_SUCCESS; } else if (property->type == BT_PROPERTY_UUIDS) { /* publish list of local supported services */ - bt_uuid_t* p_uuid = (bt_uuid_t*)property->val; + Uuid* p_uuid = reinterpret_cast(property->val); uint32_t num_uuids = 0; uint32_t i; @@ -597,32 +599,35 @@ bt_status_t btif_storage_get_adapter_property(bt_property_t* property) { if (service_mask & (tBTA_SERVICE_MASK)(1 << i)) { switch (i) { case BTA_HFP_SERVICE_ID: { - uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE, p_uuid + num_uuids); + *(p_uuid + num_uuids) = + Uuid::From16Bit(UUID_SERVCLASS_AG_HANDSFREE); num_uuids++; } /* intentional fall through: Send both BFP & HSP UUIDs if HFP is * enabled */ case BTA_HSP_SERVICE_ID: { - uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, - p_uuid + num_uuids); + *(p_uuid + num_uuids) = + Uuid::From16Bit(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY); num_uuids++; } break; case BTA_A2DP_SOURCE_SERVICE_ID: { - uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE, p_uuid + num_uuids); + *(p_uuid + num_uuids) = + Uuid::From16Bit(UUID_SERVCLASS_AUDIO_SOURCE); num_uuids++; } break; case BTA_A2DP_SINK_SERVICE_ID: { - uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SINK, p_uuid + num_uuids); + *(p_uuid + num_uuids) = Uuid::From16Bit(UUID_SERVCLASS_AUDIO_SINK); num_uuids++; } break; case BTA_HFP_HS_SERVICE_ID: { - uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE, p_uuid + num_uuids); + *(p_uuid + num_uuids) = + Uuid::From16Bit(UUID_SERVCLASS_HF_HANDSFREE); num_uuids++; } break; } } } - property->len = (num_uuids) * sizeof(bt_uuid_t); + property->len = (num_uuids) * sizeof(Uuid); return BT_STATUS_SUCCESS; } @@ -804,8 +809,8 @@ bt_status_t btif_storage_load_bonded_devices(void) { bt_bdname_t name, alias; bt_scan_mode_t mode; uint32_t disc_timeout; - bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; - bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS]; + Uuid local_uuids[BT_MAX_NUM_UUIDS]; + Uuid remote_uuids[BT_MAX_NUM_UUIDS]; bt_status_t status; btif_in_fetch_bonded_devices(&bonded_devices, 1); diff --git a/btif/src/btif_util.cc b/btif/src/btif_util.cc index 932c55a4d..12e58d227 100644 --- a/btif/src/btif_util.cc +++ b/btif/src/btif_util.cc @@ -56,29 +56,6 @@ * Constants & Macros ******************************************************************************/ #define ISDIGIT(a) (((a) >= '0') && ((a) <= '9')) -#define ISXDIGIT(a) \ - ((((a) >= '0') && ((a) <= '9')) || (((a) >= 'A') && ((a) <= 'F')) || \ - (((a) >= 'a') && ((a) <= 'f'))) - -/******************************************************************************* - * Local type definitions - ******************************************************************************/ - -/******************************************************************************* - * Static variables - ******************************************************************************/ - -/******************************************************************************* - * Static functions - ******************************************************************************/ - -/******************************************************************************* - * Externs - ******************************************************************************/ - -/******************************************************************************* - * Functions - ******************************************************************************/ /***************************************************************************** * Logging helper functions @@ -100,64 +77,6 @@ void uint2devclass(uint32_t cod, DEV_CLASS dev_class) { dev_class[0] = (uint8_t)(cod >> 16); } -static const uint8_t sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}; - -void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128) { - uint16_t uuid16_bo; - memset(uuid128, 0, sizeof(bt_uuid_t)); - - memcpy(uuid128->uu, sdp_base_uuid, MAX_UUID_SIZE); - uuid16_bo = ntohs(uuid16); - memcpy(uuid128->uu + 2, &uuid16_bo, sizeof(uint16_t)); -} - -bool string_to_uuid(const char* str, bt_uuid_t* p_uuid) { - CHECK(p_uuid); - if (str == NULL) return false; - - uint32_t uuid0, uuid4; - uint16_t uuid1, uuid2, uuid3, uuid5; - - int rc = sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx", &uuid0, &uuid1, - &uuid2, &uuid3, &uuid4, &uuid5); - if (rc != 6) return false; - - uuid0 = htonl(uuid0); - uuid1 = htons(uuid1); - uuid2 = htons(uuid2); - uuid3 = htons(uuid3); - uuid4 = htonl(uuid4); - uuid5 = htons(uuid5); - - memcpy(&(p_uuid->uu[0]), &uuid0, 4); - memcpy(&(p_uuid->uu[4]), &uuid1, 2); - memcpy(&(p_uuid->uu[6]), &uuid2, 2); - memcpy(&(p_uuid->uu[8]), &uuid3, 2); - memcpy(&(p_uuid->uu[10]), &uuid4, 4); - memcpy(&(p_uuid->uu[14]), &uuid5, 2); - - return true; -} - -void uuid_to_string_legacy(bt_uuid_t* p_uuid, char* str, size_t str_len) { - uint32_t uuid0, uuid4; - uint16_t uuid1, uuid2, uuid3, uuid5; - - memcpy(&uuid0, &(p_uuid->uu[0]), 4); - memcpy(&uuid1, &(p_uuid->uu[4]), 2); - memcpy(&uuid2, &(p_uuid->uu[6]), 2); - memcpy(&uuid3, &(p_uuid->uu[8]), 2); - memcpy(&uuid4, &(p_uuid->uu[10]), 4); - memcpy(&uuid5, &(p_uuid->uu[14]), 2); - - snprintf(str, str_len, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", ntohl(uuid0), - ntohs(uuid1), ntohs(uuid2), ntohs(uuid3), ntohl(uuid4), - ntohs(uuid5)); - return; -} - /***************************************************************************** * Function ascii_2_hex * diff --git a/btif/test/btif_storage_test.cc b/btif/test/btif_storage_test.cc index 988e23fd9..70b2026cd 100644 --- a/btif/test/btif_storage_test.cc +++ b/btif/test/btif_storage_test.cc @@ -21,25 +21,7 @@ #include "btif/include/btif_storage.h" #include "btif/include/btif_util.h" -TEST(BtifStorageTest, test_string_to_uuid) { - const char* s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22"; - const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, - 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22}; - - bt_uuid_t uuid; - memset(&uuid, 0, sizeof(uuid)); - EXPECT_FALSE(memcmp(&uuid, u1, sizeof(u1)) == 0); - - bool rc = string_to_uuid(s1, &uuid); - EXPECT_TRUE(rc); - EXPECT_TRUE(memcmp(&uuid, u1, sizeof(u1)) == 0); -} - -TEST(BtifStorageTest, test_string_to_uuid_invalid) { - bt_uuid_t uuid; - bool rc = string_to_uuid("This is not a UUID", &uuid); - EXPECT_FALSE(rc); -} +using bluetooth::Uuid; TEST(BtifStorageTest, test_uuid_split_multiple) { const char* s1 = @@ -50,11 +32,11 @@ TEST(BtifStorageTest, test_uuid_split_multiple) { const uint8_t u2[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x23}; - bt_uuid_t uuids[2]; + Uuid uuids[2]; size_t num_uuids = btif_split_uuids_string(s1, uuids, 2); EXPECT_EQ(num_uuids, 2u); - EXPECT_TRUE(memcmp(&uuids[0], u1, sizeof(u1)) == 0); - EXPECT_TRUE(memcmp(&uuids[1], u2, sizeof(u2)) == 0); + EXPECT_TRUE(memcmp(uuids[0].To128BitBE().data(), u1, sizeof(u1)) == 0); + EXPECT_TRUE(memcmp(uuids[1].To128BitBE().data(), u2, sizeof(u2)) == 0); } TEST(BtifStorageTest, test_uuid_split_partial) { @@ -62,7 +44,7 @@ TEST(BtifStorageTest, test_uuid_split_partial) { "e39c6285-867f-4b1d-9db0-35fbd9aebf22 " "e39c6285-867f-4b1d-9db0-35fbd9aebf23"; - bt_uuid_t uuids[2]; + Uuid uuids[2]; size_t num_uuids = btif_split_uuids_string(s1, uuids, 1); EXPECT_EQ(num_uuids, 1u); } diff --git a/service/Android.bp b/service/Android.bp index 39aec179f..c9baaf33a 100644 --- a/service/Android.bp +++ b/service/Android.bp @@ -59,7 +59,6 @@ btserviceBaseTestSrc = [ "test/low_energy_client_unittest.cc", "test/low_energy_scanner_unittest.cc", "test/settings_unittest.cc", - "test/uuid_unittest.cc", ] // Native system service for target @@ -75,6 +74,7 @@ cc_binary { static_libs: [ "libbluetooth-binder-common", "libbtcore", + "libbluetooth-types", ], header_libs: [ "libhardware_headers" ], @@ -108,6 +108,7 @@ cc_test { "libbluetooth-common", "libgmock", "liblog", + "libbluetooth-types", ], host_supported: true, target: { diff --git a/service/bluetooth_instance.h b/service/bluetooth_instance.h index 868f425e2..fa82883cc 100644 --- a/service/bluetooth_instance.h +++ b/service/bluetooth_instance.h @@ -20,21 +20,21 @@ #include #include +#include #include "service/common/bluetooth/low_energy_constants.h" -#include "service/common/bluetooth/uuid.h" namespace bluetooth { // A BluetoothInstance represents an application's handle to an instance -// that is registered with the underlying Bluetooth stack using a UUID and has a +// that is registered with the underlying Bluetooth stack using a Uuid and has a // stack-assigned integer "instance_id" ID associated with it. class BluetoothInstance { public: virtual ~BluetoothInstance() = default; // Returns the app-specific unique ID used while registering this instance. - virtual const UUID& GetAppIdentifier() const = 0; + virtual const Uuid& GetAppIdentifier() const = 0; // Returns the HAL "interface ID" assigned to this instance by the stack. virtual int GetInstanceId() const = 0; @@ -58,14 +58,14 @@ class BluetoothInstanceFactory { // Callback invoked as a result of a call to RegisterInstance. using RegisterCallback = - std::function instance)>; // Registers an instance for the given unique identifier |app_uuid|. // On success, this asynchronously invokes |callback| with a unique pointer // to a BluetoothInstance whose ownership can be taken by the caller. In // the case of an error, the pointer will contain nullptr. - virtual bool RegisterInstance(const UUID& app_uuid, + virtual bool RegisterInstance(const Uuid& app_uuid, const RegisterCallback& callback) = 0; private: diff --git a/service/client/main.cc b/service/client/main.cc index be6b15e72..38dc48e58 100644 --- a/service/client/main.cc +++ b/service/client/main.cc @@ -593,7 +593,7 @@ void HandleStartAdv(IBluetooth* bt_iface, const vector& args) { bool connectable = false; bool set_manufacturer_data = false; bool set_uuid = false; - bluetooth::UUID uuid; + bluetooth::Uuid uuid; for (auto iter = args.begin(); iter != args.end(); ++iter) { const std::string& arg = *iter; @@ -609,14 +609,15 @@ void HandleStartAdv(IBluetooth* bt_iface, const vector& args) { // This flag has a single argument. ++iter; if (iter == args.end()) { - PrintError("Expected a UUID after -u"); + PrintError("Expected a Uuid after -u"); return; } std::string uuid_str = *iter; - uuid = bluetooth::UUID(uuid_str); - if (!uuid.is_valid()) { - PrintError("Invalid UUID: " + uuid_str); + bool is_valid = false; + uuid = bluetooth::Uuid::FromString(uuid_str, &is_valid); + if (!is_valid) { + PrintError("Invalid Uuid: " + uuid_str); return; } @@ -661,19 +662,19 @@ void HandleStartAdv(IBluetooth* bt_iface, const vector& args) { // Determine the type and length bytes. int uuid_size = uuid.GetShortestRepresentationSize(); uint8_t type; - if (uuid_size == bluetooth::UUID::kNumBytes128) - type = bluetooth::kEIRTypeComplete128BitUUIDs; - else if (uuid_size == bluetooth::UUID::kNumBytes32) - type = bluetooth::kEIRTypeComplete32BitUUIDs; - else if (uuid_size == bluetooth::UUID::kNumBytes16) - type = bluetooth::kEIRTypeComplete16BitUUIDs; + if (uuid_size == bluetooth::Uuid::kNumBytes128) + type = bluetooth::kEIRTypeComplete128BitUuids; + else if (uuid_size == bluetooth::Uuid::kNumBytes32) + type = bluetooth::kEIRTypeComplete32BitUuids; + else if (uuid_size == bluetooth::Uuid::kNumBytes16) + type = bluetooth::kEIRTypeComplete16BitUuids; else NOTREACHED() << "Unexpected size: " << uuid_size; data.push_back(uuid_size + 1); data.push_back(type); - auto uuid_bytes = uuid.GetFullLittleEndian(); + auto uuid_bytes = uuid.To128BitLE(); int index = (uuid_size == 16) ? 0 : 12; data.insert(data.end(), uuid_bytes.data() + index, uuid_bytes.data() + index + uuid_size); diff --git a/service/common/Android.bp b/service/common/Android.bp index e85d353d4..64b911ad6 100644 --- a/service/common/Android.bp +++ b/service/common/Android.bp @@ -18,7 +18,6 @@ cc_library_static { "bluetooth/scan_settings.cc", "bluetooth/service.cc", "bluetooth/util/atomic_string.cc", - "bluetooth/uuid.cc", ], export_include_dirs: ["./"], include_dirs: ["system/bt"], @@ -72,4 +71,7 @@ cc_library_static { "libbase", "libbinder", ], + static_libs: [ + "libbluetooth-types", + ] } diff --git a/service/common/android/bluetooth/bluetooth_gatt_characteristic.cc b/service/common/android/bluetooth/bluetooth_gatt_characteristic.cc index 6ed9e1a7f..627950f19 100644 --- a/service/common/android/bluetooth/bluetooth_gatt_characteristic.cc +++ b/service/common/android/bluetooth/bluetooth_gatt_characteristic.cc @@ -59,7 +59,7 @@ status_t BluetoothGattCharacteristic::readFromParcel(const Parcel* parcel) { UUID uuid; status = parcel->readParcelable(&uuid); if (status != OK) return status; - uuid_ = uuid; + uuid_ = uuid.uuid; status = parcel->readInt32(&tmp); if (status != OK) return status; diff --git a/service/common/android/bluetooth/bluetooth_gatt_descriptor.cc b/service/common/android/bluetooth/bluetooth_gatt_descriptor.cc index ecec0e0d4..f1246dfc9 100644 --- a/service/common/android/bluetooth/bluetooth_gatt_descriptor.cc +++ b/service/common/android/bluetooth/bluetooth_gatt_descriptor.cc @@ -42,7 +42,7 @@ status_t BluetoothGattDescriptor::readFromParcel(const Parcel* parcel) { UUID uuid; status_t status = parcel->readParcelable(&uuid); if (status != OK) return status; - uuid_ = (bluetooth::UUID)uuid; + uuid_ = uuid.uuid; int32_t tmp; status = parcel->readInt32(&tmp); diff --git a/service/common/android/bluetooth/bluetooth_gatt_included_service.cc b/service/common/android/bluetooth/bluetooth_gatt_included_service.cc index c66e0d2fa..6c57d31ca 100644 --- a/service/common/android/bluetooth/bluetooth_gatt_included_service.cc +++ b/service/common/android/bluetooth/bluetooth_gatt_included_service.cc @@ -43,7 +43,7 @@ status_t BluetoothGattIncludedService::readFromParcel(const Parcel* parcel) { UUID uuid; status_t status = parcel->readParcelable(&uuid); if (status != OK) return status; - uuid_ = uuid; + uuid_ = uuid.uuid; int32_t tmp; status = parcel->readInt32(&tmp); diff --git a/service/common/android/bluetooth/bluetooth_gatt_included_service.h b/service/common/android/bluetooth/bluetooth_gatt_included_service.h index 3a00a19cd..c66a42e38 100644 --- a/service/common/android/bluetooth/bluetooth_gatt_included_service.h +++ b/service/common/android/bluetooth/bluetooth_gatt_included_service.h @@ -25,7 +25,7 @@ using android::Parcel; using android::Parcelable; using android::status_t; -using ::bluetooth::UUID; +using ::bluetooth::Uuid; namespace android { namespace bluetooth { @@ -56,11 +56,11 @@ class BluetoothGattIncludedService : public Parcelable { uint16_t handle() const { return handle_; } bool primary() const { return primary_; } - UUID uuid() const { return uuid_; } + Uuid uuid() const { return uuid_; } protected: uint16_t handle_; - UUID uuid_; + Uuid uuid_; bool primary_; }; } // namespace bluetooth diff --git a/service/common/android/bluetooth/bluetooth_gatt_service.cc b/service/common/android/bluetooth/bluetooth_gatt_service.cc index 0c0daeb27..2d4c56d54 100644 --- a/service/common/android/bluetooth/bluetooth_gatt_service.cc +++ b/service/common/android/bluetooth/bluetooth_gatt_service.cc @@ -64,7 +64,7 @@ status_t BluetoothGattService::readFromParcel(const Parcel* parcel) { UUID uuid; status = parcel->readParcelable(&uuid); if (status != OK) return status; - uuid_ = uuid; + uuid_ = uuid.uuid; std::vector characteristics; status = parcel->readParcelableVector(&characteristics); diff --git a/service/common/android/bluetooth/scan_filter.cc b/service/common/android/bluetooth/scan_filter.cc index 6bb61191b..bf85e5082 100644 --- a/service/common/android/bluetooth/scan_filter.cc +++ b/service/common/android/bluetooth/scan_filter.cc @@ -77,14 +77,14 @@ status_t ScanFilter::readFromParcel(const Parcel* parcel) { if (status != OK) return status; device_address_ = std::string(String8(addr).string()); - std::unique_ptr uuid; + UUID uuid; status = parcel->readParcelable(&uuid); if (status != OK) return status; - service_uuid_ = std::move(uuid); + service_uuid_.reset(new ::bluetooth::Uuid(uuid.uuid)); status = parcel->readParcelable(&uuid); if (status != OK) return status; - service_uuid_mask_ = std::move(uuid); + service_uuid_mask_.reset(new ::bluetooth::Uuid(uuid.uuid)); return status; } diff --git a/service/common/android/bluetooth/uuid.cc b/service/common/android/bluetooth/uuid.cc index 6cfc6db27..6243ccc81 100644 --- a/service/common/android/bluetooth/uuid.cc +++ b/service/common/android/bluetooth/uuid.cc @@ -33,7 +33,7 @@ status_t UUID::writeToParcel(Parcel* parcel) const { // a second 64-bit integer. This is the same as writing the raw-bytes in // sequence, but we don't want to assume any host-endianness here. So follow // the same scheme and use the same Parcel APIs. - UUID::UUID128Bit bytes = GetFullBigEndian(); + ::bluetooth::Uuid::UUID128Bit bytes = uuid.To128BitBE(); uint64_t most_sig_bits = ((((uint64_t)bytes[0]) << 56) | (((uint64_t)bytes[1]) << 48) | @@ -55,7 +55,7 @@ status_t UUID::writeToParcel(Parcel* parcel) const { } status_t UUID::readFromParcel(const Parcel* parcel) { - UUID::UUID128Bit bytes; + ::bluetooth::Uuid::UUID128Bit bytes; uint64_t most_sig_bits, least_sig_bits; status_t status = parcel->readUint64(&most_sig_bits); @@ -82,8 +82,7 @@ status_t UUID::readFromParcel(const Parcel* parcel) { bytes[14] = (least_sig_bits >> 8) & 0xFF; bytes[15] = least_sig_bits & 0xFF; - id_ = bytes; - is_valid_ = true; + uuid = ::bluetooth::Uuid::From128BitBE(bytes); return status; } diff --git a/service/common/android/bluetooth/uuid.h b/service/common/android/bluetooth/uuid.h index c0a09e06b..302889cfa 100644 --- a/service/common/android/bluetooth/uuid.h +++ b/service/common/android/bluetooth/uuid.h @@ -28,11 +28,11 @@ using android::status_t; namespace android { namespace bluetooth { -class UUID : public Parcelable, public ::bluetooth::UUID { +class UUID : public Parcelable { public: UUID() = default; // NOLINT, implicit converter - UUID(const ::bluetooth::UUID& uuid) : ::bluetooth::UUID(uuid){}; // NOLINT + UUID(const ::bluetooth::Uuid& uuid) : uuid(uuid){}; // NOLINT ~UUID() = default; // Write |this| parcelable to the given |parcel|. Keep in mind that @@ -48,6 +48,15 @@ class UUID : public Parcelable, public ::bluetooth::UUID { // // Returns android::OK on success and an appropriate error otherwise. status_t readFromParcel(const Parcel* parcel) override; + + bool operator==(::bluetooth::Uuid rhs) const { return uuid == rhs; } + + ::bluetooth::Uuid uuid; }; } // namespace bluetooth } // namespace android + +inline bool operator==(const ::bluetooth::Uuid& lhs, + const android::bluetooth::UUID& rhs) { + return lhs == rhs.uuid; +} diff --git a/service/common/bluetooth/advertise_data.h b/service/common/bluetooth/advertise_data.h index 0eb2bc176..054e8f83f 100644 --- a/service/common/bluetooth/advertise_data.h +++ b/service/common/bluetooth/advertise_data.h @@ -30,7 +30,7 @@ namespace bluetooth { class AdvertiseData { public: // Constructs an AdvertiseData with the given parameters. |data| can only - // contain the "Service UUIDs", "Service Data", "Manufacturer Data", + // contain the "Service Uuids", "Service Data", "Manufacturer Data", // "Tx Power" and "Device name" fields as specified in the Core Specification // Supplement. |data| must be properly formatted according to the supplement // and contains the data as it will be sent over the wire. diff --git a/service/common/bluetooth/characteristic.h b/service/common/bluetooth/characteristic.h index ba7328a3f..fc5d66315 100644 --- a/service/common/bluetooth/characteristic.h +++ b/service/common/bluetooth/characteristic.h @@ -26,7 +26,7 @@ class Characteristic { public: Characteristic() = default; Characteristic(const Characteristic& other); - Characteristic(uint16_t handle, const UUID& uuid, uint8_t properties, + Characteristic(uint16_t handle, const Uuid& uuid, uint8_t properties, uint16_t permissions, const std::vector& descriptors) : handle_(handle), @@ -43,7 +43,7 @@ class Characteristic { bool operator!=(const Characteristic& rhs) const; uint16_t handle() const { return handle_; } - const UUID& uuid() const { return uuid_; } + const Uuid& uuid() const { return uuid_; } uint8_t properties() const { return properties_; } uint16_t permissions() const { return permissions_; } const std::vector& descriptors() const { return descriptors_; } @@ -51,7 +51,7 @@ class Characteristic { protected: uint16_t handle_; - UUID uuid_; + Uuid uuid_; uint8_t properties_; uint16_t permissions_; std::vector descriptors_; diff --git a/service/common/bluetooth/descriptor.h b/service/common/bluetooth/descriptor.h index 35fe53e1a..8483067a1 100644 --- a/service/common/bluetooth/descriptor.h +++ b/service/common/bluetooth/descriptor.h @@ -24,7 +24,7 @@ class Descriptor { Descriptor() = default; Descriptor(const Descriptor& other); Descriptor& operator=(const Descriptor& other); - Descriptor(uint16_t handle, const UUID& uuid, uint16_t permissions) + Descriptor(uint16_t handle, const Uuid& uuid, uint16_t permissions) : handle_(handle), uuid_(uuid), permissions_(permissions){}; virtual ~Descriptor() = default; @@ -35,11 +35,11 @@ class Descriptor { uint16_t handle() const { return handle_; } uint16_t permissions() const { return permissions_; } - const UUID& uuid() const { return uuid_; } + const Uuid& uuid() const { return uuid_; } protected: uint16_t handle_; - UUID uuid_; + Uuid uuid_; uint16_t permissions_; }; } diff --git a/service/common/bluetooth/low_energy_constants.h b/service/common/bluetooth/low_energy_constants.h index 348fa603d..27cb5caf3 100644 --- a/service/common/bluetooth/low_energy_constants.h +++ b/service/common/bluetooth/low_energy_constants.h @@ -110,12 +110,12 @@ const int kAdvertisingChannelAll = // Various Extended Inquiry Response fields types that are used for advertising // data fields as defined in the Core Specification Supplement. const uint8_t kEIRTypeFlags = 0x01; -const uint8_t kEIRTypeIncomplete16BitUUIDs = 0x02; -const uint8_t kEIRTypeComplete16BitUUIDs = 0x03; -const uint8_t kEIRTypeIncomplete32BitUUIDs = 0x04; -const uint8_t kEIRTypeComplete32BitUUIDs = 0x05; -const uint8_t kEIRTypeIncomplete128BitUUIDs = 0x06; -const uint8_t kEIRTypeComplete128BitUUIDs = 0x07; +const uint8_t kEIRTypeIncomplete16BitUuids = 0x02; +const uint8_t kEIRTypeComplete16BitUuids = 0x03; +const uint8_t kEIRTypeIncomplete32BitUuids = 0x04; +const uint8_t kEIRTypeComplete32BitUuids = 0x05; +const uint8_t kEIRTypeIncomplete128BitUuids = 0x06; +const uint8_t kEIRTypeComplete128BitUuids = 0x07; const uint8_t kEIRTypeShortenedLocalName = 0x08; const uint8_t kEIRTypeCompleteLocalName = 0x09; const uint8_t kEIRTypeTxPower = 0x0A; diff --git a/service/common/bluetooth/scan_filter.cc b/service/common/bluetooth/scan_filter.cc index 3226c5095..da5fa36ed 100644 --- a/service/common/bluetooth/scan_filter.cc +++ b/service/common/bluetooth/scan_filter.cc @@ -16,16 +16,18 @@ #include "bluetooth/scan_filter.h" +#include + namespace bluetooth { ScanFilter::ScanFilter(const ScanFilter& other) { device_name_ = other.device_name_; device_address_ = other.device_address_; - if (other.service_uuid_) service_uuid_.reset(new UUID(*other.service_uuid_)); + if (other.service_uuid_) service_uuid_.reset(new Uuid(*other.service_uuid_)); if (other.service_uuid_mask_) - service_uuid_mask_.reset(new UUID(*other.service_uuid_mask_)); + service_uuid_mask_.reset(new Uuid(*other.service_uuid_mask_)); } ScanFilter& ScanFilter::operator=(const ScanFilter& other) { @@ -33,12 +35,12 @@ ScanFilter& ScanFilter::operator=(const ScanFilter& other) { device_address_ = other.device_address_; if (other.service_uuid_) - service_uuid_.reset(new UUID(*other.service_uuid_)); + service_uuid_.reset(new Uuid(*other.service_uuid_)); else service_uuid_ = nullptr; if (other.service_uuid_mask_) - service_uuid_mask_.reset(new UUID(*other.service_uuid_mask_)); + service_uuid_mask_.reset(new Uuid(*other.service_uuid_mask_)); else service_uuid_mask_ = nullptr; @@ -52,15 +54,15 @@ bool ScanFilter::SetDeviceAddress(const std::string& device_address) { return true; } -void ScanFilter::SetServiceUuid(const UUID& service_uuid) { - service_uuid_.reset(new UUID(service_uuid)); +void ScanFilter::SetServiceUuid(const Uuid& service_uuid) { + service_uuid_.reset(new Uuid(service_uuid)); service_uuid_mask_.reset(); } -void ScanFilter::SetServiceUuidWithMask(const UUID& service_uuid, - const UUID& mask) { - service_uuid_.reset(new UUID(service_uuid)); - service_uuid_mask_.reset(new UUID(mask)); +void ScanFilter::SetServiceUuidWithMask(const Uuid& service_uuid, + const Uuid& mask) { + service_uuid_.reset(new Uuid(service_uuid)); + service_uuid_mask_.reset(new Uuid(mask)); } bool ScanFilter::operator==(const ScanFilter& rhs) const { diff --git a/service/common/bluetooth/scan_filter.h b/service/common/bluetooth/scan_filter.h index a8c30ee57..db40e6e92 100644 --- a/service/common/bluetooth/scan_filter.h +++ b/service/common/bluetooth/scan_filter.h @@ -45,21 +45,21 @@ class ScanFilter { // |device_address| is in an illegal format. bool SetDeviceAddress(const std::string& device_address); - // The service UUID and its mask used while filtering scan results. See + // The service Uuid and its mask used while filtering scan results. See // SetServiceUuidWithMask for what this mask does. The raw pointer returned // from these getters belongs to the ScanFilter object. nullptr will be // returned if these fields have not been set on this filter. - const UUID* service_uuid() const { return service_uuid_.get(); } - const UUID* service_uuid_mask() const { return service_uuid_mask_.get(); } + const Uuid* service_uuid() const { return service_uuid_.get(); } + const Uuid* service_uuid_mask() const { return service_uuid_mask_.get(); } - // Sets the service UUID for this filter. - void SetServiceUuid(const UUID& service_uuid); + // Sets the service Uuid for this filter. + void SetServiceUuid(const Uuid& service_uuid); - // Sets the service UUID for this filter with a 128-bit mask. The mask allows - // the caller to partially filter scanned service UUIDs. For any of the - // 128-bits of a UUID, set the corresponding bit in the mask to 1 to match the + // Sets the service Uuid for this filter with a 128-bit mask. The mask allows + // the caller to partially filter scanned service Uuids. For any of the + // 128-bits of a Uuid, set the corresponding bit in the mask to 1 to match the // advertised value, and 0 to ignore that bit. - void SetServiceUuidWithMask(const UUID& service_uuid, const UUID& mask); + void SetServiceUuidWithMask(const Uuid& service_uuid, const Uuid& mask); // Comparison operator. bool operator==(const ScanFilter& rhs) const; @@ -68,8 +68,8 @@ class ScanFilter { std::string device_name_; std::string device_address_; - std::unique_ptr service_uuid_; - std::unique_ptr service_uuid_mask_; + std::unique_ptr service_uuid_; + std::unique_ptr service_uuid_mask_; // TODO(armansito): Add service and manufacturer data filter fields. }; diff --git a/service/common/bluetooth/service.h b/service/common/bluetooth/service.h index b3ce0ea44..f8e069e17 100644 --- a/service/common/bluetooth/service.h +++ b/service/common/bluetooth/service.h @@ -26,7 +26,7 @@ class Service { public: Service() = default; Service(const Service& other); - Service(uint16_t handle, bool primary, const UUID& uuid, + Service(uint16_t handle, bool primary, const Uuid& uuid, const std::vector& characteristics, const std::vector& included_services) : handle_(handle), @@ -44,7 +44,7 @@ class Service { uint16_t handle() const { return handle_; } bool primary() const { return primary_; } - const UUID& uuid() const { return uuid_; } + const Uuid& uuid() const { return uuid_; } const std::vector& characteristics() const { return characteristics_; } @@ -56,7 +56,7 @@ class Service { protected: uint16_t handle_; bool primary_; - UUID uuid_; + Uuid uuid_; std::vector characteristics_; std::vector included_services_; }; diff --git a/service/common/bluetooth/uuid.cc b/service/common/bluetooth/uuid.cc deleted file mode 100644 index 16547aec1..000000000 --- a/service/common/bluetooth/uuid.cc +++ /dev/null @@ -1,160 +0,0 @@ -// -// Copyright (C) 2015 Google, Inc. -// -// 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 "bluetooth/uuid.h" - -#include -#include -#include -#include - -#include -#include -#include -#include - -namespace bluetooth { - -namespace { - -const UUID::UUID128Bit kSigBaseUUID = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5f, 0x9b, 0x34, 0xfb}}; - -} // namespace - -// static -UUID UUID::GetRandom() { - UUID128Bit bytes; - base::RandBytes(bytes.data(), bytes.size()); - return UUID(bytes); -} - -// static -UUID UUID::GetNil() { - UUID128Bit bytes; - bytes.fill(0); - return UUID(bytes); -} - -// static -UUID UUID::GetMax() { - UUID128Bit bytes; - bytes.fill(1); - return UUID(bytes); -} - -void UUID::InitializeDefault() { - // Initialize to Bluetooth SIG base UUID. - id_ = kSigBaseUUID; - is_valid_ = true; -} - -UUID::UUID() { InitializeDefault(); } - -UUID::UUID(std::string uuid) { - InitializeDefault(); - is_valid_ = false; - - if (uuid.empty()) return; - - if (uuid.size() < 11 && uuid.find("0x") == 0) uuid = uuid.substr(2); - - if (uuid.size() != 4 && uuid.size() != 8 && uuid.size() != 36) return; - - if (uuid.size() == 36) { - if (uuid[8] != '-') return; - if (uuid[13] != '-') return; - if (uuid[18] != '-') return; - if (uuid[23] != '-') return; - - std::vector tokens = base::SplitString( - uuid, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); - - if (tokens.size() != 5) return; - - uuid = base::JoinString(tokens, ""); - } - - const int start_index = uuid.size() == 4 ? 2 : 0; - const size_t copy_size = std::min(id_.size(), uuid.size() / 2); - for (size_t i = 0; i < copy_size; ++i) { - std::string octet_text(uuid, i * 2, 2); - char* temp = nullptr; - id_[start_index + i] = strtol(octet_text.c_str(), &temp, 16); - if (*temp != '\0') return; - } - - is_valid_ = true; -} - -UUID::UUID(const bt_uuid_t& uuid) { - std::reverse_copy(uuid.uu, uuid.uu + sizeof(uuid.uu), id_.begin()); - is_valid_ = true; -} - -UUID::UUID(const UUID16Bit& uuid) { - InitializeDefault(); - std::copy(uuid.begin(), uuid.end(), id_.begin() + kNumBytes16); -} - -UUID::UUID(const UUID32Bit& uuid) { - InitializeDefault(); - std::copy(uuid.begin(), uuid.end(), id_.begin()); -} - -UUID::UUID(const UUID128Bit& uuid) : id_(uuid), is_valid_(true) {} - -UUID::UUID128Bit UUID::GetFullBigEndian() const { return id_; } - -UUID::UUID128Bit UUID::GetFullLittleEndian() const { - UUID::UUID128Bit ret; - std::reverse_copy(id_.begin(), id_.end(), ret.begin()); - return ret; -} - -bt_uuid_t UUID::GetBlueDroid() const { - bt_uuid_t ret; - std::reverse_copy(id_.begin(), id_.end(), ret.uu); - return ret; -} - -std::string UUID::ToString() const { - return base::StringPrintf( - "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - id_[0], id_[1], id_[2], id_[3], id_[4], id_[5], id_[6], id_[7], id_[8], - id_[9], id_[10], id_[11], id_[12], id_[13], id_[14], id_[15]); -} - -size_t UUID::GetShortestRepresentationSize() const { - if (memcmp(id_.data() + 4, kSigBaseUUID.data() + 4, id_.size() - 4) != 0) - return kNumBytes128; - - if (id_[0] == 0 && id_[1] == 0) return kNumBytes16; - - return kNumBytes32; -} - -bool UUID::operator<(const UUID& rhs) const { - return std::lexicographical_compare(id_.begin(), id_.end(), rhs.id_.begin(), - rhs.id_.end()); -} - -bool UUID::operator==(const UUID& rhs) const { - return std::equal(id_.begin(), id_.end(), rhs.id_.begin()); -} - -} // namespace bluetooth diff --git a/service/common/bluetooth/uuid.h b/service/common/bluetooth/uuid.h deleted file mode 100644 index 034654c11..000000000 --- a/service/common/bluetooth/uuid.h +++ /dev/null @@ -1,109 +0,0 @@ -// -// Copyright (C) 2015 Google, Inc. -// -// 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. -// - -#pragma once - -#include -#include - -// TODO: Find places that break include what you use and remove this. -#include -#include - -namespace bluetooth { - -class UUID { - public: - static constexpr size_t kNumBytes128 = 16; - static constexpr size_t kNumBytes32 = 4; - static constexpr size_t kNumBytes16 = 2; - - typedef std::array UUID16Bit; - typedef std::array UUID32Bit; - typedef std::array UUID128Bit; - - // Creates and returns a random 128-bit UUID. - static UUID GetRandom(); - - // Creates and returns a UUID in which all 128 bits are equal to 0. - static UUID GetNil(); - - // Creates and returns a UUID in which all 128 bits are equal to 1. - static UUID GetMax(); - - // Construct a Bluetooth 'base' UUID. - UUID(); - virtual ~UUID() = default; - - // BlueDroid constructor. - explicit UUID(const bt_uuid_t& uuid); - - // String constructor. Only hex ASCII accepted. - explicit UUID(std::string uuid); - - // std::array variants constructors. - explicit UUID(const UUID16Bit& uuid); - explicit UUID(const UUID32Bit& uuid); - explicit UUID(const UUID128Bit& uuid); - - // Provide the full network-byte-ordered blob. - UUID128Bit GetFullBigEndian() const; - - // Provide blob in Little endian (BlueDroid expects this). - UUID128Bit GetFullLittleEndian() const; - - // Helper for bluedroid LE type. - bt_uuid_t GetBlueDroid() const; - - // Returns a string representation for the UUID. - std::string ToString() const; - - // Returns whether or not this UUID was initialized correctly. - bool is_valid() const { return is_valid_; } - - // Returns the shortest possible representation of this UUID in bytes. - size_t GetShortestRepresentationSize() const; - - bool operator<(const UUID& rhs) const; - bool operator==(const UUID& rhs) const; - inline bool operator!=(const UUID& rhs) const { return !(*this == rhs); } - - protected: - void InitializeDefault(); - - // Network-byte-ordered ID. - UUID128Bit id_; - - // True if this UUID was initialized with a correct representation. - bool is_valid_; -}; - -} // namespace bluetooth - -// Custom std::hash specialization so that bluetooth::UUID can be used as a key -// in std::unordered_map. -namespace std { - -template <> -struct hash { - std::size_t operator()(const bluetooth::UUID& key) const { - const auto& uuid_bytes = key.GetFullBigEndian(); - std::hash hash_fn; - return hash_fn(std::string((char*)uuid_bytes.data(), uuid_bytes.size())); - } -}; - -} // namespace std diff --git a/service/example/heart_rate/constants.h b/service/example/heart_rate/constants.h index 6159997cf..8871a0c19 100644 --- a/service/example/heart_rate/constants.h +++ b/service/example/heart_rate/constants.h @@ -28,11 +28,12 @@ const uint8_t kHRValueFormat16Bit = (1 << 0); const uint16_t kHRSensorContactDetected = (3 << 1); const uint8_t kHREnergyExpendedPresent = (1 << 3); -const bluetooth::UUID kCCCDescriptorUUID("2902"); -const bluetooth::UUID kHRServiceUUID("180D"); -const bluetooth::UUID kHRMeasurementUUID("2A37"); -const bluetooth::UUID kBodySensorLocationUUID("2A38"); -const bluetooth::UUID kHRControlPointUUID("2A39"); +const bluetooth::Uuid kCCCDescriptorUuid = bluetooth::Uuid::FromString("2902"); +const bluetooth::Uuid kHRServiceUuid = bluetooth::Uuid::FromString("180D"); +const bluetooth::Uuid kHRMeasurementUuid = bluetooth::Uuid::FromString("2A37"); +const bluetooth::Uuid kBodySensorLocationUuid = + bluetooth::Uuid::FromString("2A38"); +const bluetooth::Uuid kHRControlPointUuid = bluetooth::Uuid::FromString("2A39"); const uint8_t kHRBodyLocationOther = 0; const uint8_t kHRBodyLocationChest = 1; diff --git a/service/example/heart_rate/heart_rate_server.cc b/service/example/heart_rate/heart_rate_server.cc index a7bfe520c..07e5ea863 100644 --- a/service/example/heart_rate/heart_rate_server.cc +++ b/service/example/heart_rate/heart_rate_server.cc @@ -55,8 +55,8 @@ class CLIBluetoothLeAdvertiserCallback bt_->GetName(&name_param); std::string name(String8(name_param).string()); - /* Advertising data: 16-bit Service UUID: Heart Rate Service, Tx power*/ - std::vector data{0x03, bluetooth::kEIRTypeComplete16BitUUIDs, + /* Advertising data: 16-bit Service Uuid: Heart Rate Service, Tx power*/ + std::vector data{0x03, bluetooth::kEIRTypeComplete16BitUuids, 0x0D, 0x18, 0x02, bluetooth::kEIRTypeTxPower, 0x00}; @@ -239,25 +239,25 @@ Status HeartRateServer::OnServerRegistered(int status, int server_if) { LOG(INFO) << "Heart Rate server registered - server_if: " << server_if_; - bluetooth::Service hrService( - 0, true, kHRServiceUUID, - {{0, - kHRMeasurementUUID, - bluetooth::kCharacteristicPropertyNotify, - 0, - {{0, kCCCDescriptorUUID, (bluetooth::kAttributePermissionRead | - bluetooth::kAttributePermissionWrite)}}}, - {0, - kBodySensorLocationUUID, - bluetooth::kCharacteristicPropertyRead, - bluetooth::kAttributePermissionRead, - {}}, - {0, - kHRControlPointUUID, - bluetooth::kCharacteristicPropertyWrite, - bluetooth::kAttributePermissionWrite, - {}}}, - {}); + bluetooth::Service hrService(0, true, kHRServiceUuid, + {{0, + kHRMeasurementUuid, + bluetooth::kCharacteristicPropertyNotify, + 0, + {{0, kCCCDescriptorUuid, + (bluetooth::kAttributePermissionRead | + bluetooth::kAttributePermissionWrite)}}}, + {0, + kBodySensorLocationUuid, + bluetooth::kCharacteristicPropertyRead, + bluetooth::kAttributePermissionRead, + {}}, + {0, + kHRControlPointUuid, + bluetooth::kCharacteristicPropertyWrite, + bluetooth::kAttributePermissionWrite, + {}}}, + {}); bool op_status = true; diff --git a/service/gatt_client.cc b/service/gatt_client.cc index 80f5ebf06..966ed2f18 100644 --- a/service/gatt_client.cc +++ b/service/gatt_client.cc @@ -26,7 +26,7 @@ namespace bluetooth { // GattClient implementation // ======================================================== -GattClient::GattClient(const UUID& uuid, int client_id) +GattClient::GattClient(const Uuid& uuid, int client_id) : app_identifier_(uuid), client_id_(client_id) {} GattClient::~GattClient() { @@ -38,7 +38,7 @@ GattClient::~GattClient() { ->unregister_client(client_id_); } -const UUID& GattClient::GetAppIdentifier() const { return app_identifier_; } +const Uuid& GattClient::GetAppIdentifier() const { return app_identifier_; } int GattClient::GetInstanceId() const { return client_id_; } @@ -53,22 +53,21 @@ GattClientFactory::~GattClientFactory() { hal::BluetoothGattInterface::Get()->RemoveClientObserver(this); } -bool GattClientFactory::RegisterInstance(const UUID& uuid, +bool GattClientFactory::RegisterInstance(const Uuid& uuid, const RegisterCallback& callback) { - VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); + VLOG(1) << __func__ << " - Uuid: " << uuid.ToString(); lock_guard lock(pending_calls_lock_); if (pending_calls_.find(uuid) != pending_calls_.end()) { - LOG(ERROR) << "GATT client with given UUID already registered - " - << "UUID: " << uuid.ToString(); + LOG(ERROR) << "GATT client with given Uuid already registered - " + << "Uuid: " << uuid.ToString(); return false; } const btgatt_client_interface_t* hal_iface = hal::BluetoothGattInterface::Get()->GetClientHALInterface(); - bt_uuid_t app_uuid = uuid.GetBlueDroid(); - if (hal_iface->register_client(app_uuid) != BT_STATUS_SUCCESS) return false; + if (hal_iface->register_client(uuid) != BT_STATUS_SUCCESS) return false; pending_calls_[uuid] = callback; @@ -77,8 +76,8 @@ bool GattClientFactory::RegisterInstance(const UUID& uuid, void GattClientFactory::RegisterClientCallback( hal::BluetoothGattInterface* /* gatt_iface */, int status, int client_id, - const bt_uuid_t& app_uuid) { - UUID uuid(app_uuid); + const Uuid& app_uuid) { + Uuid uuid(app_uuid); auto iter = pending_calls_.find(uuid); if (iter == pending_calls_.end()) { diff --git a/service/gatt_client.h b/service/gatt_client.h index fd83ffe9f..c42349af4 100644 --- a/service/gatt_client.h +++ b/service/gatt_client.h @@ -20,9 +20,9 @@ #include #include +#include #include "service/bluetooth_instance.h" -#include "service/common/bluetooth/uuid.h" #include "service/hal/bluetooth_gatt_interface.h" namespace bluetooth { @@ -35,7 +35,7 @@ class GattClient : public BluetoothInstance { ~GattClient() override; // BluetoothClientInstace overrides: - const UUID& GetAppIdentifier() const override; + const Uuid& GetAppIdentifier() const override; int GetInstanceId() const override; private: @@ -43,10 +43,10 @@ class GattClient : public BluetoothInstance { // Constructor shouldn't be called directly as instances are meant to be // obtained from the factory. - GattClient(const UUID& uuid, int client_id); + GattClient(const Uuid& uuid, int client_id); // See getters above for documentation. - UUID app_identifier_; + Uuid app_identifier_; int client_id_; DISALLOW_COPY_AND_ASSIGN(GattClient); @@ -64,18 +64,18 @@ class GattClientFactory : public BluetoothInstanceFactory, ~GattClientFactory() override; // BluetoothInstanceFactory override: - bool RegisterInstance(const UUID& uuid, + bool RegisterInstance(const Uuid& uuid, const RegisterCallback& callback) override; private: // hal::BluetoothGattInterface::ClientObserver override: void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface, int status, int client_id, - const bt_uuid_t& app_uuid) override; + const Uuid& app_uuid) override; // Map of pending calls to register. std::mutex pending_calls_lock_; - std::unordered_map pending_calls_; + std::unordered_map pending_calls_; DISALLOW_COPY_AND_ASSIGN(GattClientFactory); }; diff --git a/service/gatt_server.cc b/service/gatt_server.cc index 63bacbb8f..d3703bac0 100644 --- a/service/gatt_server.cc +++ b/service/gatt_server.cc @@ -16,6 +16,8 @@ #include "service/gatt_server.h" +#include + #include "service/logging_helpers.h" #include "stack/include/bt_types.h" @@ -27,7 +29,7 @@ namespace bluetooth { // GattServer implementation // ======================================================== -GattServer::GattServer(const UUID& uuid, int server_id) +GattServer::GattServer(const Uuid& uuid, int server_id) : app_identifier_(uuid), server_id_(server_id), delegate_(nullptr) {} GattServer::~GattServer() { @@ -50,7 +52,7 @@ void GattServer::SetDelegate(Delegate* delegate) { delegate_ = delegate; } -const UUID& GattServer::GetAppIdentifier() const { return app_identifier_; } +const Uuid& GattServer::GetAppIdentifier() const { return app_identifier_; } int GattServer::GetInstanceId() const { return server_id_; } @@ -68,16 +70,16 @@ bool GattServer::AddService(const bluetooth::Service& service, svc.push_back({.type = (service.primary() ? BTGATT_DB_PRIMARY_SERVICE : BTGATT_DB_SECONDARY_SERVICE), - .uuid = service.uuid().GetBlueDroid()}); + .uuid = service.uuid()}); for (const auto& characteristic : service.characteristics()) { svc.push_back({.type = BTGATT_DB_CHARACTERISTIC, - .uuid = characteristic.uuid().GetBlueDroid(), + .uuid = characteristic.uuid(), .properties = characteristic.properties(), .permissions = characteristic.permissions()}); for (const auto& descriptor : characteristic.descriptors()) svc.push_back({.type = BTGATT_DB_DESCRIPTOR, - .uuid = descriptor.uuid().GetBlueDroid(), + .uuid = descriptor.uuid(), .permissions = descriptor.permissions()}); } @@ -292,10 +294,10 @@ void GattServer::ServiceAddedCallback(hal::BluetoothGattInterface* gatt_iface, VLOG(1) << __func__ << " - status: " << status << " server_id: " << server_id << " first handle: " << svc[0].attribute_handle - << " service UUID: " << UUID(svc[0].uuid).ToString() + << " service Uuid: " << Uuid(svc[0].uuid).ToString() << " count: " << svc.size(); - Service service(svc[0].attribute_handle, true, UUID(svc[0].uuid), {}, {}); + Service service(svc[0].attribute_handle, true, Uuid(svc[0].uuid), {}, {}); for (size_t i = 1; i < svc.size(); i++) { const btgatt_db_element_t& curr = svc[i]; @@ -303,13 +305,13 @@ void GattServer::ServiceAddedCallback(hal::BluetoothGattInterface* gatt_iface, << " handle: " << curr.attribute_handle; if (curr.type == BTGATT_DB_CHARACTERISTIC) { service.characteristics().push_back({curr.attribute_handle, - UUID(curr.uuid), + Uuid(curr.uuid), curr.properties, curr.permissions, {}}); } else if (curr.type == BTGATT_DB_DESCRIPTOR) { service.characteristics().back().descriptors().push_back( - {curr.attribute_handle, UUID(curr.uuid), curr.permissions}); + {curr.attribute_handle, Uuid(curr.uuid), curr.permissions}); } else if (svc[i].type == BTGATT_DB_INCLUDED_SERVICE) { } } @@ -567,22 +569,21 @@ GattServerFactory::~GattServerFactory() { hal::BluetoothGattInterface::Get()->RemoveServerObserver(this); } -bool GattServerFactory::RegisterInstance(const UUID& uuid, +bool GattServerFactory::RegisterInstance(const Uuid& uuid, const RegisterCallback& callback) { - VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); + VLOG(1) << __func__ << " - Uuid: " << uuid.ToString(); lock_guard lock(pending_calls_lock_); if (pending_calls_.find(uuid) != pending_calls_.end()) { - LOG(ERROR) << "GATT-server client with given UUID already being registered " - << " - UUID: " << uuid.ToString(); + LOG(ERROR) << "GATT-server client with given Uuid already being registered " + << " - Uuid: " << uuid.ToString(); return false; } const btgatt_server_interface_t* hal_iface = hal::BluetoothGattInterface::Get()->GetServerHALInterface(); - bt_uuid_t app_uuid = uuid.GetBlueDroid(); - if (hal_iface->register_server(app_uuid) != BT_STATUS_SUCCESS) return false; + if (hal_iface->register_server(uuid) != BT_STATUS_SUCCESS) return false; pending_calls_[uuid] = callback; @@ -591,10 +592,10 @@ bool GattServerFactory::RegisterInstance(const UUID& uuid, void GattServerFactory::RegisterServerCallback( hal::BluetoothGattInterface* gatt_iface, int status, int server_id, - const bt_uuid_t& app_uuid) { - UUID uuid(app_uuid); + const Uuid& app_uuid) { + Uuid uuid(app_uuid); - VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); + VLOG(1) << __func__ << " - Uuid: " << uuid.ToString(); lock_guard lock(pending_calls_lock_); auto iter = pending_calls_.find(uuid); diff --git a/service/gatt_server.h b/service/gatt_server.h index e17e862be..8db518e54 100644 --- a/service/gatt_server.h +++ b/service/gatt_server.h @@ -25,10 +25,10 @@ #include #include +#include #include "service/bluetooth_instance.h" #include "service/common/bluetooth/service.h" -#include "service/common/bluetooth/uuid.h" #include "service/hal/bluetooth_gatt_interface.h" namespace bluetooth { @@ -116,7 +116,7 @@ class GattServer : public BluetoothInstance, void SetDelegate(Delegate* delegate); // BluetoothClientInstace overrides: - const UUID& GetAppIdentifier() const override; + const Uuid& GetAppIdentifier() const override; int GetInstanceId() const override; // Callback type used to report the status of an asynchronous GATT server @@ -134,7 +134,7 @@ class GattServer : public BluetoothInstance, // we can add other services to this server instance? Do we need to clean up // all the entries or does the upper-layer need to remove the service? Or are // we in a stuck-state where the service declaration hasn't ended? - bool AddService(const bluetooth::Service&, const ResultCallback& callback); + bool AddService(const Service&, const ResultCallback& callback); // Sends a response for a pending notification. |request_id| and // |device_address| should match those that were received through one of the @@ -189,7 +189,7 @@ class GattServer : public BluetoothInstance, // Constructor shouldn't be called directly as instances are meant to be // obtained from the factory. - GattServer(const UUID& uuid, int server_id); + GattServer(const Uuid& uuid, int server_id); // hal::BluetoothGattInterface::ServerObserver overrides: void ConnectionCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id, @@ -237,7 +237,7 @@ class GattServer : public BluetoothInstance, int request_id); // See getters for documentation. - UUID app_identifier_; + Uuid app_identifier_; int server_id_; // Mutex that synchronizes access to the entries below. @@ -277,18 +277,18 @@ class GattServerFactory : public BluetoothInstanceFactory, ~GattServerFactory() override; // BluetoothInstanceFactory override: - bool RegisterInstance(const UUID& uuid, + bool RegisterInstance(const Uuid& uuid, const RegisterCallback& callback) override; private: // hal::BluetoothGattInterface::ServerObserver override: void RegisterServerCallback(hal::BluetoothGattInterface* gatt_iface, int status, int server_id, - const bt_uuid_t& app_uuid) override; + const Uuid& app_uuid) override; // Map of pending calls to register. std::mutex pending_calls_lock_; - std::unordered_map pending_calls_; + std::unordered_map pending_calls_; DISALLOW_COPY_AND_ASSIGN(GattServerFactory); }; diff --git a/service/gatt_server_old.cc b/service/gatt_server_old.cc index f6099ef26..0f1b699df 100644 --- a/service/gatt_server_old.cc +++ b/service/gatt_server_old.cc @@ -83,7 +83,7 @@ namespace bluetooth { namespace gatt { struct Characteristic { - UUID uuid; + Uuid uuid; int blob_section; std::vector blob; @@ -97,11 +97,11 @@ struct ServerInternals { ServerInternals(); ~ServerInternals(); int Initialize(); - bt_status_t AddCharacteristic(const UUID& uuid, uint8_t properties, + bt_status_t AddCharacteristic(const Uuid& uuid, uint8_t properties, uint16_t permissions); - // This maps API attribute UUIDs to BlueDroid handles. - std::map uuid_to_attribute; + // This maps API attribute Uuids to BlueDroid handles. + std::map uuid_to_attribute; // The attribute cache, indexed by BlueDroid handles. std::unordered_map characteristics; @@ -111,7 +111,7 @@ struct ServerInternals { ScanResults scan_results; - UUID last_write; + Uuid last_write; const btgatt_interface_t* gatt; int server_if; int client_if; @@ -130,11 +130,12 @@ namespace { /** Callback invoked in response to register_server */ void RegisterServerCallback(int status, int server_if, - const bt_uuid_t& app_uuid) { + const bluetooth::Uuid& app_uuid) { LOG_INFO(LOG_TAG, "%s: status:%d server_if:%d app_uuid:%p", __func__, status, server_if, &app_uuid); g_internal->server_if = server_if; + pending_svc_decl.push_back( {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = app_uuid}); } @@ -157,7 +158,7 @@ void ServiceAddedCallback(int status, int server_if, if (el.type == BTGATT_DB_DESCRIPTOR) { LOG_INFO(LOG_TAG, "%s: descr_handle:%d", __func__, el.attribute_handle); } else if (el.type == BTGATT_DB_CHARACTERISTIC) { - bluetooth::UUID id(el.uuid); + bluetooth::Uuid id(el.uuid); uint16_t char_handle = el.attribute_handle; LOG_INFO(LOG_TAG, "%s: char_handle:%d", __func__, char_handle); @@ -192,11 +193,10 @@ void ServiceAddedCallback(int status, int server_if, pending_svc_decl.clear(); blob_index.clear(); - // The UUID provided here is unimportant, and is only used to satisfy + // The Uuid provided here is unimportant, and is only used to satisfy // BlueDroid. - // It must be different than any other registered UUID. - bt_uuid_t client_id = service[0].uuid; - ++client_id.uu[15]; + // It must be different than any other registered Uuid. + bluetooth::Uuid client_id = bluetooth::Uuid::GetRandom(); bt_status_t btstat = g_internal->gatt->client->register_client(client_id); if (btstat != BT_STATUS_SUCCESS) { @@ -277,7 +277,7 @@ void RequestWriteCallback(int conn_id, int trans_id, const RawAddress& bda, } else if (!is_prep) { // This is a single frame characteristic write. // Notify upwards because we're done now. - const bluetooth::UUID::UUID128Bit& attr_uuid = ch.uuid.GetFullBigEndian(); + const bluetooth::Uuid::UUID128Bit& attr_uuid = ch.uuid.To128BitBE(); ssize_t status; OSI_NO_INTR(status = write(g_internal->pipefd[kPipeWriteEnd], attr_uuid.data(), attr_uuid.size())); @@ -317,9 +317,8 @@ void RequestExecWriteCallback(int conn_id, int trans_id, const RawAddress& bda, if (!exec_write) return; std::lock_guard lock(g_internal->lock); - // Communicate the attribute UUID as notification of a write update. - const bluetooth::UUID::UUID128Bit uuid = - g_internal->last_write.GetFullBigEndian(); + // Communicate the attribute Uuid as notification of a write update. + const bluetooth::Uuid::UUID128Bit uuid = g_internal->last_write.To128BitBE(); ssize_t status; OSI_NO_INTR(status = write(g_internal->pipefd[kPipeWriteEnd], uuid.data(), uuid.size())); @@ -347,9 +346,9 @@ void EnableAdvertisingCallback(uint8_t status) { } void RegisterClientCallback(int status, int client_if, - const bt_uuid_t& app_uuid) { - LOG_INFO(LOG_TAG, "%s: status:%d client_if:%d uuid[0]:%u", __func__, status, - client_if, app_uuid.uu[0]); + const bluetooth::Uuid& app_uuid) { + LOG_INFO(LOG_TAG, "%s: status:%d client_if:%d uuid[0]:%s", __func__, status, + client_if, app_uuid.ToString().c_str()); g_internal->client_if = client_if; // Setup our advertisement. This has no callback. @@ -500,13 +499,11 @@ int ServerInternals::Initialize() { return 0; } -bt_status_t ServerInternals::AddCharacteristic(const UUID& uuid, +bt_status_t ServerInternals::AddCharacteristic(const Uuid& uuid, uint8_t properties, uint16_t permissions) { - bt_uuid_t c_uuid = uuid.GetBlueDroid(); - pending_svc_decl.push_back({.type = BTGATT_DB_CHARACTERISTIC, - .uuid = c_uuid, + .uuid = uuid, .properties = properties, .permissions = permissions}); return BT_STATUS_SUCCESS; @@ -532,7 +529,7 @@ Server::Server() : internal_(nullptr) {} Server::~Server() {} -bool Server::Initialize(const UUID& service_id, int* gatt_pipe) { +bool Server::Initialize(const Uuid& service_id, int* gatt_pipe) { internal_.reset(new ServerInternals); if (!internal_) { LOG_ERROR(LOG_TAG, "Error creating internals"); @@ -547,9 +544,7 @@ bool Server::Initialize(const UUID& service_id, int* gatt_pipe) { return false; } - bt_uuid_t uuid = service_id.GetBlueDroid(); - - bt_status_t btstat = internal_->gatt->server->register_server(uuid); + bt_status_t btstat = internal_->gatt->server->register_server(service_id); if (btstat != BT_STATUS_SUCCESS) { LOG_ERROR(LOG_TAG, "Failed to register server"); return false; @@ -567,7 +562,7 @@ bool Server::Initialize(const UUID& service_id, int* gatt_pipe) { return true; } -bool Server::SetAdvertisement(const std::vector& ids, +bool Server::SetAdvertisement(const std::vector& ids, const std::vector& service_data, const std::vector& manufacturer_data, bool transmit_name) { @@ -575,8 +570,8 @@ bool Server::SetAdvertisement(const std::vector& ids, // const auto& mutable_manufacturer_data = manufacturer_data; // const auto& mutable_service_data = service_data; - // for (const UUID &id : ids) { - // const auto le_id = id.GetFullLittleEndian(); + // for (const Uuid &id : ids) { + // const auto le_id = id.To128BitLE(); // id_data.insert(id_data.end(), le_id.begin(), le_id.end()); // } @@ -593,7 +588,7 @@ bool Server::SetAdvertisement(const std::vector& ids, return true; } -bool Server::SetScanResponse(const std::vector& ids, +bool Server::SetScanResponse(const std::vector& ids, const std::vector& service_data, const std::vector& manufacturer_data, bool transmit_name) { @@ -601,8 +596,8 @@ bool Server::SetScanResponse(const std::vector& ids, // const auto& mutable_manufacturer_data = manufacturer_data; // const auto& mutable_service_data = service_data; - // for (const UUID &id : ids) { - // const auto le_id = id.GetFullLittleEndian(); + // for (const Uuid &id : ids) { + // const auto le_id = id.To128BitLE(); // id_data.insert(id_data.end(), le_id.begin(), le_id.end()); // } @@ -621,7 +616,7 @@ bool Server::SetScanResponse(const std::vector& ids, return true; } -bool Server::AddCharacteristic(const UUID& id, int properties, +bool Server::AddCharacteristic(const Uuid& id, int properties, int permissions) { std::unique_lock lock(internal_->lock); bt_status_t btstat = @@ -637,7 +632,7 @@ bool Server::AddCharacteristic(const UUID& id, int properties, return true; } -bool Server::AddBlob(const UUID& id, const UUID& control_id, int properties, +bool Server::AddBlob(const Uuid& id, const Uuid& control_id, int properties, int permissions) { std::unique_lock lock(internal_->lock); @@ -702,7 +697,7 @@ bool Server::GetScanResults(ScanResults* results) { return true; } -bool Server::SetCharacteristicValue(const UUID& id, +bool Server::SetCharacteristicValue(const Uuid& id, const std::vector& value) { std::lock_guard lock(internal_->lock); const int attribute_id = internal_->uuid_to_attribute[id]; @@ -719,7 +714,7 @@ bool Server::SetCharacteristicValue(const UUID& id, return true; } -bool Server::GetCharacteristicValue(const UUID& id, +bool Server::GetCharacteristicValue(const Uuid& id, std::vector* value) { std::lock_guard lock(internal_->lock); const int attribute_id = internal_->uuid_to_attribute[id]; diff --git a/service/gatt_server_old.h b/service/gatt_server_old.h index a16bdfc7a..f45394afa 100644 --- a/service/gatt_server_old.h +++ b/service/gatt_server_old.h @@ -15,6 +15,7 @@ // #pragma once +#include #include #include #include @@ -22,7 +23,6 @@ #include "hardware/bluetooth.h" #include "hardware/bt_gatt.h" -#include "service/common/bluetooth/uuid.h" namespace bluetooth { namespace gatt { @@ -71,35 +71,35 @@ class Server { // Register GATT interface, initialize internal state, // and open a pipe for characteristic write notification. - bool Initialize(const UUID& service_id, int* gatt_pipe); + bool Initialize(const Uuid& service_id, int* gatt_pipe); // Control the content of service advertisement. - bool SetAdvertisement(const std::vector& ids, + bool SetAdvertisement(const std::vector& ids, const std::vector& service_data, const std::vector& manufacturer_data, bool transmit_name); // Control the content of service scan response. - bool SetScanResponse(const std::vector& ids, + bool SetScanResponse(const std::vector& ids, const std::vector& service_data, const std::vector& manufacturer_data, bool transmit_name); // Add an ordinary characteristic for reading and/or writing. - bool AddCharacteristic(const UUID& id, int properties, int permissions); + bool AddCharacteristic(const Uuid& id, int properties, int permissions); // Add a special 'blob' characteristic with a corresponding control // attribute to manipulate which part of the blob the attribute represents. - bool AddBlob(const UUID& id, const UUID& control_id, int properties, + bool AddBlob(const Uuid& id, const Uuid& control_id, int properties, int permissions); // Put a new value into a characeteristic. // It will be read from a client starting at the next 0-offset read. - bool SetCharacteristicValue(const UUID& id, + bool SetCharacteristicValue(const Uuid& id, const std::vector& value); // Get the current value of a characteristic. - bool GetCharacteristicValue(const UUID& id, std::vector* value); + bool GetCharacteristicValue(const Uuid& id, std::vector* value); // Start this service. Activate advertisements, allow connections. // Characteristics should all be created before this. diff --git a/service/hal/bluetooth_gatt_interface.cc b/service/hal/bluetooth_gatt_interface.cc index f33fdbcce..1b20c8f84 100644 --- a/service/hal/bluetooth_gatt_interface.cc +++ b/service/hal/bluetooth_gatt_interface.cc @@ -79,7 +79,7 @@ GetServerObservers(); } while (0) void RegisterClientCallback(int status, int client_if, - const bt_uuid_t& app_uuid) { + const bluetooth::Uuid& app_uuid) { shared_lock lock(g_instance_lock); VLOG(2) << __func__ << " - status: " << status << " client_if: " << client_if; VERIFY_INTERFACE_OR_RETURN(); @@ -222,7 +222,7 @@ void ServicesAddedCallback(int conn_id, const btgatt_db_element_t& added, } void RegisterServerCallback(int status, int server_if, - const bt_uuid_t& app_uuid) { + const bluetooth::Uuid& app_uuid) { shared_lock lock(g_instance_lock); VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if; VERIFY_INTERFACE_OR_RETURN(); @@ -567,7 +567,7 @@ void BluetoothGattInterface::ScannerObserver::ScanResultCallback( void BluetoothGattInterface::ClientObserver::RegisterClientCallback( BluetoothGattInterface* /* gatt_iface */, int /* status */, - int /* client_if */, const bt_uuid_t& /* app_uuid */) { + int /* client_if */, const bluetooth::Uuid& /* app_uuid */) { // Do nothing. } @@ -639,7 +639,7 @@ void BluetoothGattInterface::ClientObserver::ServicesAddedCallback( void BluetoothGattInterface::ServerObserver::RegisterServerCallback( BluetoothGattInterface* /* gatt_iface */, int /* status */, - int /* server_if */, const bt_uuid_t& /* app_uuid */) { + int /* server_if */, const bluetooth::Uuid& /* app_uuid */) { // Do nothing. } diff --git a/service/hal/bluetooth_gatt_interface.h b/service/hal/bluetooth_gatt_interface.h index 345ed88af..da857303c 100644 --- a/service/hal/bluetooth_gatt_interface.h +++ b/service/hal/bluetooth_gatt_interface.h @@ -67,7 +67,7 @@ class BluetoothGattInterface { virtual void RegisterClientCallback(BluetoothGattInterface* gatt_iface, int status, int client_if, - const bt_uuid_t& app_uuid); + const bluetooth::Uuid& app_uuid); virtual void ConnectCallback(BluetoothGattInterface* gatt_iface, int conn_id, int status, int client_if, @@ -120,7 +120,7 @@ class BluetoothGattInterface { virtual void RegisterServerCallback(BluetoothGattInterface* gatt_iface, int status, int server_if, - const bt_uuid_t& app_uuid); + const bluetooth::Uuid& app_uuid); virtual void ConnectionCallback(BluetoothGattInterface* gatt_iface, int conn_id, int server_if, int connected, diff --git a/service/hal/fake_bluetooth_gatt_interface.cc b/service/hal/fake_bluetooth_gatt_interface.cc index 42ccb51c0..1d9399999 100644 --- a/service/hal/fake_bluetooth_gatt_interface.cc +++ b/service/hal/fake_bluetooth_gatt_interface.cc @@ -28,7 +28,7 @@ std::shared_ptr g_scanner_handler; std::shared_ptr g_client_handler; std::shared_ptr g_server_handler; -bt_status_t FakeRegisterClient(const bt_uuid_t& app_uuid) { +bt_status_t FakeRegisterClient(const bluetooth::Uuid& app_uuid) { if (g_client_handler) return g_client_handler->RegisterClient(app_uuid); return BT_STATUS_FAIL; @@ -57,7 +57,7 @@ bt_status_t FakeDisconnect(int client_if, const RawAddress& bd_addr, return BT_STATUS_FAIL; } -bt_status_t FakeRegisterServer(const bt_uuid_t& app_uuid) { +bt_status_t FakeRegisterServer(const bluetooth::Uuid& app_uuid) { if (g_server_handler) return g_server_handler->RegisterServer(app_uuid); return BT_STATUS_FAIL; @@ -183,7 +183,7 @@ void FakeBluetoothGattInterface::NotifyScanResultCallback( } void FakeBluetoothGattInterface::NotifyRegisterClientCallback( - int status, int client_if, const bt_uuid_t& app_uuid) { + int status, int client_if, const bluetooth::Uuid& app_uuid) { FOR_EACH_OBSERVER(ClientObserver, client_observers_, RegisterClientCallback(this, status, client_if, app_uuid)); } @@ -202,7 +202,7 @@ void FakeBluetoothGattInterface::NotifyDisconnectCallback( } void FakeBluetoothGattInterface::NotifyRegisterServerCallback( - int status, int server_if, const bt_uuid_t& app_uuid) { + int status, int server_if, const Uuid& app_uuid) { FOR_EACH_OBSERVER(ServerObserver, server_observers_, RegisterServerCallback(this, status, server_if, app_uuid)); } diff --git a/service/hal/fake_bluetooth_gatt_interface.h b/service/hal/fake_bluetooth_gatt_interface.h index 7ad3c899b..371e60a77 100644 --- a/service/hal/fake_bluetooth_gatt_interface.h +++ b/service/hal/fake_bluetooth_gatt_interface.h @@ -35,7 +35,7 @@ class FakeBluetoothGattInterface : public BluetoothGattInterface { public: virtual ~TestClientHandler() = default; - virtual bt_status_t RegisterClient(const bt_uuid_t& app_uuid) = 0; + virtual bt_status_t RegisterClient(const bluetooth::Uuid& app_uuid) = 0; virtual bt_status_t UnregisterClient(int client_if) = 0; virtual bt_status_t Connect(int client_if, const RawAddress& bd_addr, @@ -51,7 +51,7 @@ class FakeBluetoothGattInterface : public BluetoothGattInterface { public: virtual ~TestServerHandler() = default; - virtual bt_status_t RegisterServer(const bt_uuid_t& app_uuid) = 0; + virtual bt_status_t RegisterServer(const bluetooth::Uuid& app_uuid) = 0; virtual bt_status_t UnregisterServer(int server_if) = 0; virtual bt_status_t AddService( int server_if, std::vector service) = 0; @@ -77,13 +77,13 @@ class FakeBluetoothGattInterface : public BluetoothGattInterface { // given parameters. void NotifyRegisterScannerCallback(int status, int client_if, - const bt_uuid_t& app_uuid); + const bluetooth::Uuid& app_uuid); void NotifyScanResultCallback(const RawAddress& bda, int rssi, std::vector adv_data); // Client callbacks: void NotifyRegisterClientCallback(int status, int client_if, - const bt_uuid_t& app_uuid); + const bluetooth::Uuid& app_uuid); void NotifyConnectCallback(int conn_id, int status, int client_if, const RawAddress& bda); void NotifyDisconnectCallback(int conn_id, int status, int client_if, @@ -91,17 +91,17 @@ class FakeBluetoothGattInterface : public BluetoothGattInterface { // Server callbacks: void NotifyRegisterServerCallback(int status, int server_if, - const bt_uuid_t& app_uuid); + const bluetooth::Uuid& app_uuid); void NotifyServerConnectionCallback(int conn_id, int server_if, int connected, const RawAddress& bda); void NotifyServiceAddedCallback(int status, int server_if, std::vector srvc); void NotifyCharacteristicAddedCallback(int status, int server_if, - const bt_uuid_t& uuid, int srvc_handle, - int char_handle); + const bluetooth::Uuid& uuid, + int srvc_handle, int char_handle); void NotifyDescriptorAddedCallback(int status, int server_if, - const bt_uuid_t& uuid, int srvc_handle, - int desc_handle); + const bluetooth::Uuid& uuid, + int srvc_handle, int desc_handle); void NotifyServiceStartedCallback(int status, int server_if, int srvc_handle); void NotifyRequestReadCharacteristicCallback(int conn_id, int trans_id, const RawAddress& bda, diff --git a/service/ipc/binder/bluetooth_binder_server.h b/service/ipc/binder/bluetooth_binder_server.h index 55a330046..3918899c1 100644 --- a/service/ipc/binder/bluetooth_binder_server.h +++ b/service/ipc/binder/bluetooth_binder_server.h @@ -30,9 +30,9 @@ #include #include #include +#include #include "service/adapter.h" -#include "service/common/bluetooth/uuid.h" #include "service/ipc/binder/remote_callback_list.h" using android::String16; diff --git a/service/ipc/binder/interface_with_instances_base.cc b/service/ipc/binder/interface_with_instances_base.cc index 18b1eae4a..c39c8b372 100644 --- a/service/ipc/binder/interface_with_instances_base.cc +++ b/service/ipc/binder/interface_with_instances_base.cc @@ -34,7 +34,7 @@ bool InterfaceWithInstancesBase::RegisterInstanceBase( // Store the callback in the pending list. It will get removed later when the // stack notifies us asynchronously. - bluetooth::UUID app_uuid = bluetooth::UUID::GetRandom(); + bluetooth::Uuid app_uuid = bluetooth::Uuid::GetRandom(); if (!pending_callbacks_.Register(app_uuid, callback)) { LOG(ERROR) << "Failed to store |callback| to map"; return false; @@ -45,21 +45,22 @@ bool InterfaceWithInstancesBase::RegisterInstanceBase( // (sp) we are using a wp here rather than std::weak_ptr. android::wp weak_ptr_to_this(this); - bluetooth::BluetoothInstanceFactory::RegisterCallback cb = [weak_ptr_to_this]( - bluetooth::BLEStatus status, const bluetooth::UUID& in_uuid, - std::unique_ptr instance) { - // If the weak pointer was invalidated then there is nothing we can do. - android::sp strong_ptr_to_this = - weak_ptr_to_this.promote(); - if (!strong_ptr_to_this.get()) { - VLOG(2) << "InterfaceWithInstancesBase was deleted while instance was" - << " being registered"; - return; - } - - strong_ptr_to_this->OnRegisterInstance(status, in_uuid, - std::move(instance)); - }; + bluetooth::BluetoothInstanceFactory::RegisterCallback cb = + [weak_ptr_to_this]( + bluetooth::BLEStatus status, const bluetooth::Uuid& in_uuid, + std::unique_ptr instance) { + // If the weak pointer was invalidated then there is nothing we can do. + android::sp strong_ptr_to_this = + weak_ptr_to_this.promote(); + if (!strong_ptr_to_this.get()) { + VLOG(2) << "InterfaceWithInstancesBase was deleted while instance was" + << " being registered"; + return; + } + + strong_ptr_to_this->OnRegisterInstance(status, in_uuid, + std::move(instance)); + }; if (factory->RegisterInstance(app_uuid, cb)) return true; @@ -99,7 +100,7 @@ InterfaceWithInstancesBase::GetInstance(int instance_id) { } void InterfaceWithInstancesBase::OnRegisterInstance( - bluetooth::BLEStatus status, const bluetooth::UUID& uuid, + bluetooth::BLEStatus status, const bluetooth::Uuid& uuid, std::unique_ptr instance) { VLOG(2) << __func__ << " - status: " << status; diff --git a/service/ipc/binder/interface_with_instances_base.h b/service/ipc/binder/interface_with_instances_base.h index f0aa762a2..deebb0e2e 100644 --- a/service/ipc/binder/interface_with_instances_base.h +++ b/service/ipc/binder/interface_with_instances_base.h @@ -20,9 +20,9 @@ #include #include +#include #include "service/bluetooth_instance.h" -#include "service/common/bluetooth/uuid.h" #include "service/ipc/binder/remote_callback_map.h" namespace ipc { @@ -44,7 +44,7 @@ class InterfaceWithInstancesBase protected: // The initial entry point for registering a instance. Invoke this from the - // registration API to add a instance/UUID pair to the pending list and set up + // registration API to add a instance/Uuid pair to the pending list and set up // the generic asynchronous callback handler and initiate the process with the // given |factory| instance. Returns false, if there were any errors that // could be synchronously reported. @@ -74,7 +74,7 @@ class InterfaceWithInstancesBase private: // Base implementation of the register callback. void OnRegisterInstance( - bluetooth::BLEStatus status, const bluetooth::UUID& uuid, + bluetooth::BLEStatus status, const bluetooth::Uuid& uuid, std::unique_ptr instance); // Called when the callback registration has completed. |instance| is owned by @@ -89,7 +89,7 @@ class InterfaceWithInstancesBase // Instances that are pending registration. Once their registration is // complete, the entry will be removed from this map. - RemoteCallbackMap pending_callbacks_; + RemoteCallbackMap pending_callbacks_; // We keep two maps here: one from instance_id IDs to callback Binders and one // from instance_id IDs to the BluetoothInstance structures themselves. diff --git a/service/ipc/linux_ipc_host.cc b/service/ipc/linux_ipc_host.cc index 7b328cf53..b0c08b855 100644 --- a/service/ipc/linux_ipc_host.cc +++ b/service/ipc/linux_ipc_host.cc @@ -38,7 +38,7 @@ #include "service/adapter.h" using bluetooth::Adapter; -using bluetooth::UUID; +using bluetooth::Uuid; using namespace bluetooth::gatt; @@ -108,8 +108,8 @@ bool LinuxIPCHost::OnCreateService(const std::string& service_uuid) { gatt_servers_[service_uuid] = std::unique_ptr(new Server); int gattfd; - bool status = - gatt_servers_[service_uuid]->Initialize(UUID(service_uuid), &gattfd); + bool status = gatt_servers_[service_uuid]->Initialize( + Uuid::FromString(service_uuid), &gattfd); if (!status) { LOG_ERROR(LOG_TAG, "Failed to initialize bluetooth"); return false; @@ -155,11 +155,12 @@ bool LinuxIPCHost::OnAddCharacteristic(const std::string& service_uuid, if (control_uuid.empty()) { gatt_servers_[service_uuid]->AddCharacteristic( - UUID(characteristic_uuid), properties_mask, permissions_mask); + Uuid::FromString(characteristic_uuid), properties_mask, + permissions_mask); } else { - gatt_servers_[service_uuid]->AddBlob(UUID(characteristic_uuid), - UUID(control_uuid), properties_mask, - permissions_mask); + gatt_servers_[service_uuid]->AddBlob(Uuid::FromString(characteristic_uuid), + Uuid::FromString(control_uuid), + properties_mask, permissions_mask); } return true; } @@ -170,8 +171,8 @@ bool LinuxIPCHost::OnSetCharacteristicValue( std::string decoded_data; base::Base64Decode(value, &decoded_data); std::vector blob_data(decoded_data.begin(), decoded_data.end()); - gatt_servers_[service_uuid]->SetCharacteristicValue(UUID(characteristic_uuid), - blob_data); + gatt_servers_[service_uuid]->SetCharacteristicValue( + Uuid::FromString(characteristic_uuid), blob_data); return true; } @@ -187,10 +188,10 @@ bool LinuxIPCHost::OnSetAdvertisement(const std::string& service_uuid, std::vector advertise_uuid_tokens = base::SplitString( advertise_uuids, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); - // string -> vector - std::vector ids; + // string -> vector + std::vector ids; for (const auto& uuid_token : advertise_uuid_tokens) - ids.emplace_back(uuid_token); + ids.emplace_back(Uuid::FromString(uuid_token)); std::string decoded_data; base::Base64Decode(advertise_data, &decoded_data); @@ -215,10 +216,10 @@ bool LinuxIPCHost::OnSetScanResponse(const std::string& service_uuid, std::vector scan_response_uuid_tokens = base::SplitString( scan_response_uuids, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); - // string -> vector - std::vector ids; + // string -> vector + std::vector ids; for (const auto& uuid_token : scan_response_uuid_tokens) - ids.emplace_back(uuid_token); + ids.emplace_back(Uuid::FromString(uuid_token)); std::string decoded_data; base::Base64Decode(scan_response_data, &decoded_data); @@ -304,7 +305,7 @@ bool LinuxIPCHost::OnMessage() { } bool LinuxIPCHost::OnGattWrite() { - UUID::UUID128Bit id; + Uuid::UUID128Bit id; ssize_t r; OSI_NO_INTR(r = read(pfds_[kFdGatt].fd, id.data(), id.size())); @@ -316,7 +317,7 @@ bool LinuxIPCHost::OnGattWrite() { std::vector value; // TODO(icoolidge): Generalize this for multiple clients. auto server = gatt_servers_.begin(); - server->second->GetCharacteristicValue(UUID(id), &value); + server->second->GetCharacteristicValue(Uuid::From128BitBE(id), &value); const std::string value_string(value.begin(), value.end()); std::string encoded_value; base::Base64Encode(value_string, &encoded_value); diff --git a/service/ipc/linux_ipc_host.h b/service/ipc/linux_ipc_host.h index a51a96602..1ff48820c 100644 --- a/service/ipc/linux_ipc_host.h +++ b/service/ipc/linux_ipc_host.h @@ -17,11 +17,11 @@ #include +#include #include #include #include -#include "service/common/bluetooth/uuid.h" #include "service/gatt_server_old.h" namespace bluetooth { diff --git a/service/low_energy_advertiser.cc b/service/low_energy_advertiser.cc index 3df55db39..14a1472ee 100644 --- a/service/low_energy_advertiser.cc +++ b/service/low_energy_advertiser.cc @@ -120,7 +120,7 @@ void DoNothing(uint8_t status) {} // LowEnergyAdvertiser implementation // ======================================================== -LowEnergyAdvertiser::LowEnergyAdvertiser(const UUID& uuid, int advertiser_id) +LowEnergyAdvertiser::LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id) : app_identifier_(uuid), advertiser_id_(advertiser_id), adv_started_(false), @@ -226,7 +226,7 @@ bool LowEnergyAdvertiser::IsStoppingAdvertising() const { return IsAdvertisingStarted() && adv_stop_callback_; } -const UUID& LowEnergyAdvertiser::GetAppIdentifier() const { +const Uuid& LowEnergyAdvertiser::GetAppIdentifier() const { return app_identifier_; } @@ -294,13 +294,13 @@ LowEnergyAdvertiserFactory::LowEnergyAdvertiserFactory() {} LowEnergyAdvertiserFactory::~LowEnergyAdvertiserFactory() {} bool LowEnergyAdvertiserFactory::RegisterInstance( - const UUID& app_uuid, const RegisterCallback& callback) { + const Uuid& app_uuid, const RegisterCallback& callback) { VLOG(1) << __func__; lock_guard lock(pending_calls_lock_); if (pending_calls_.find(app_uuid) != pending_calls_.end()) { - LOG(ERROR) << "Low-Energy advertiser with given UUID already registered - " - << "UUID: " << app_uuid.ToString(); + LOG(ERROR) << "Low-Energy advertiser with given Uuid already registered - " + << "Uuid: " << app_uuid.ToString(); return false; } @@ -319,7 +319,7 @@ bool LowEnergyAdvertiserFactory::RegisterInstance( } void LowEnergyAdvertiserFactory::RegisterAdvertiserCallback( - const RegisterCallback& callback, const UUID& app_uuid, + const RegisterCallback& callback, const Uuid& app_uuid, uint8_t advertiser_id, uint8_t status) { VLOG(1) << __func__; lock_guard lock(pending_calls_lock_); diff --git a/service/low_energy_advertiser.h b/service/low_energy_advertiser.h index a014ae132..6bada9c80 100644 --- a/service/low_energy_advertiser.h +++ b/service/low_energy_advertiser.h @@ -22,6 +22,7 @@ #include #include +#include #include "service/bluetooth_instance.h" #include "service/common/bluetooth/advertise_data.h" @@ -30,7 +31,6 @@ #include "service/common/bluetooth/scan_filter.h" #include "service/common/bluetooth/scan_result.h" #include "service/common/bluetooth/scan_settings.h" -#include "service/common/bluetooth/uuid.h" #include "service/hal/bluetooth_gatt_interface.h" namespace bluetooth { @@ -75,7 +75,7 @@ class LowEnergyAdvertiser : public BluetoothInstance { } // BluetoothClientInstace overrides: - const UUID& GetAppIdentifier() const override; + const Uuid& GetAppIdentifier() const override; int GetInstanceId() const override; private: @@ -83,7 +83,7 @@ class LowEnergyAdvertiser : public BluetoothInstance { // Constructor shouldn't be called directly as instances are meant to be // obtained from the factory. - LowEnergyAdvertiser(const UUID& uuid, int advertiser_id); + LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id); // BluetoothGattInterface::AdvertiserObserver overrides: void SetDataCallback(uint8_t advertiser_id, uint8_t status); @@ -95,7 +95,7 @@ class LowEnergyAdvertiser : public BluetoothInstance { void InvokeAndClearStopCallback(BLEStatus status); // See getters above for documentation. - UUID app_identifier_; + Uuid app_identifier_; int advertiser_id_; // Protects advertising-related members below. @@ -124,7 +124,7 @@ class LowEnergyAdvertiserFactory : public BluetoothInstanceFactory { ~LowEnergyAdvertiserFactory() override; // BluetoothInstanceFactory override: - bool RegisterInstance(const UUID& app_uuid, + bool RegisterInstance(const Uuid& app_uuid, const RegisterCallback& callback) override; private: @@ -132,12 +132,12 @@ class LowEnergyAdvertiserFactory : public BluetoothInstanceFactory { // BluetoothGattInterface::AdvertiserObserver overrides: void RegisterAdvertiserCallback(const RegisterCallback& callback, - const UUID& app_uuid, uint8_t advertiser_id, + const Uuid& app_uuid, uint8_t advertiser_id, uint8_t status); // Map of pending calls to register. std::mutex pending_calls_lock_; - std::unordered_set pending_calls_; + std::unordered_set pending_calls_; DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiserFactory); }; diff --git a/service/low_energy_client.cc b/service/low_energy_client.cc index 97f3ca68a..ed9c5f650 100644 --- a/service/low_energy_client.cc +++ b/service/low_energy_client.cc @@ -31,7 +31,7 @@ namespace bluetooth { // LowEnergyClient implementation // ======================================================== -LowEnergyClient::LowEnergyClient(Adapter& adapter, const UUID& uuid, +LowEnergyClient::LowEnergyClient(Adapter& adapter, const Uuid& uuid, int client_id) : adapter_(adapter), app_identifier_(uuid), @@ -126,7 +126,7 @@ void LowEnergyClient::SetDelegate(Delegate* delegate) { delegate_ = delegate; } -const UUID& LowEnergyClient::GetAppIdentifier() const { +const Uuid& LowEnergyClient::GetAppIdentifier() const { return app_identifier_; } @@ -205,21 +205,20 @@ LowEnergyClientFactory::~LowEnergyClientFactory() { } bool LowEnergyClientFactory::RegisterInstance( - const UUID& uuid, const RegisterCallback& callback) { - VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); + const Uuid& uuid, const RegisterCallback& callback) { + VLOG(1) << __func__ << " - Uuid: " << uuid.ToString(); lock_guard lock(pending_calls_lock_); if (pending_calls_.find(uuid) != pending_calls_.end()) { - LOG(ERROR) << "Low-Energy client with given UUID already registered - " - << "UUID: " << uuid.ToString(); + LOG(ERROR) << "Low-Energy client with given Uuid already registered - " + << "Uuid: " << uuid.ToString(); return false; } const btgatt_client_interface_t* hal_iface = hal::BluetoothGattInterface::Get()->GetClientHALInterface(); - bt_uuid_t app_uuid = uuid.GetBlueDroid(); - if (hal_iface->register_client(app_uuid) != BT_STATUS_SUCCESS) return false; + if (hal_iface->register_client(uuid) != BT_STATUS_SUCCESS) return false; pending_calls_[uuid] = callback; @@ -228,10 +227,10 @@ bool LowEnergyClientFactory::RegisterInstance( void LowEnergyClientFactory::RegisterClientCallback( hal::BluetoothGattInterface* gatt_iface, int status, int client_id, - const bt_uuid_t& app_uuid) { - UUID uuid(app_uuid); + const bluetooth::Uuid& app_uuid) { + Uuid uuid(app_uuid); - VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); + VLOG(1) << __func__ << " - Uuid: " << uuid.ToString(); lock_guard lock(pending_calls_lock_); auto iter = pending_calls_.find(uuid); diff --git a/service/low_energy_client.h b/service/low_energy_client.h index 16ef4a5a4..f75398272 100644 --- a/service/low_energy_client.h +++ b/service/low_energy_client.h @@ -22,13 +22,13 @@ #include #include +#include #include "service/bluetooth_instance.h" #include "service/common/bluetooth/low_energy_constants.h" #include "service/common/bluetooth/scan_filter.h" #include "service/common/bluetooth/scan_result.h" #include "service/common/bluetooth/scan_settings.h" -#include "service/common/bluetooth/uuid.h" #include "service/hal/bluetooth_gatt_interface.h" namespace bluetooth { @@ -91,7 +91,7 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, bool SetMtu(const std::string& address, int mtu); // BluetoothClientInstace overrides: - const UUID& GetAppIdentifier() const override; + const Uuid& GetAppIdentifier() const override; int GetInstanceId() const override; private: @@ -99,7 +99,7 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, // Constructor shouldn't be called directly as instances are meant to be // obtained from the factory. - LowEnergyClient(Adapter& adapter, const UUID& uuid, int client_id); + LowEnergyClient(Adapter& adapter, const Uuid& uuid, int client_id); // BluetoothGattInterface::ClientObserver overrides: void ConnectCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id, @@ -119,7 +119,7 @@ class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver, Adapter& adapter_; // See getters above for documentation. - UUID app_identifier_; + Uuid app_identifier_; int client_id_; // Raw handle to the Delegate, which must outlive this LowEnergyClient @@ -151,7 +151,7 @@ class LowEnergyClientFactory ~LowEnergyClientFactory() override; // BluetoothInstanceFactory override: - bool RegisterInstance(const UUID& uuid, + bool RegisterInstance(const Uuid& uuid, const RegisterCallback& callback) override; private: @@ -160,11 +160,11 @@ class LowEnergyClientFactory // BluetoothGattInterface::ClientObserver overrides: void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface, int status, int client_id, - const bt_uuid_t& app_uuid) override; + const bluetooth::Uuid& app_uuid) override; // Map of pending calls to register. std::mutex pending_calls_lock_; - std::map pending_calls_; + std::map pending_calls_; // Raw pointer to the Adapter that owns this factory. Adapter& adapter_; diff --git a/service/low_energy_scanner.cc b/service/low_energy_scanner.cc index 093a55463..3f31795e9 100644 --- a/service/low_energy_scanner.cc +++ b/service/low_energy_scanner.cc @@ -65,7 +65,7 @@ size_t GetScanRecordLength(std::vector bytes) { // LowEnergyScanner implementation // ======================================================== -LowEnergyScanner::LowEnergyScanner(Adapter& adapter, const UUID& uuid, +LowEnergyScanner::LowEnergyScanner(Adapter& adapter, const Uuid& uuid, int scanner_id) : adapter_(adapter), app_identifier_(uuid), @@ -131,7 +131,7 @@ bool LowEnergyScanner::StopScan() { return true; } -const UUID& LowEnergyScanner::GetAppIdentifier() const { +const Uuid& LowEnergyScanner::GetAppIdentifier() const { return app_identifier_; } @@ -170,13 +170,13 @@ LowEnergyScannerFactory::~LowEnergyScannerFactory() { } bool LowEnergyScannerFactory::RegisterInstance( - const UUID& uuid, const RegisterCallback& callback) { - VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); + const Uuid& uuid, const RegisterCallback& callback) { + VLOG(1) << __func__ << " - Uuid: " << uuid.ToString(); lock_guard lock(pending_calls_lock_); if (pending_calls_.find(uuid) != pending_calls_.end()) { - LOG(ERROR) << "Low-Energy scanner with given UUID already registered - " - << "UUID: " << uuid.ToString(); + LOG(ERROR) << "Low-Energy scanner with given Uuid already registered - " + << "Uuid: " << uuid.ToString(); return false; } @@ -193,11 +193,11 @@ bool LowEnergyScannerFactory::RegisterInstance( } void LowEnergyScannerFactory::RegisterScannerCallback( - const RegisterCallback& callback, const UUID& app_uuid, uint8_t scanner_id, + const RegisterCallback& callback, const Uuid& app_uuid, uint8_t scanner_id, uint8_t status) { - UUID uuid(app_uuid); + Uuid uuid(app_uuid); - VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); + VLOG(1) << __func__ << " - Uuid: " << uuid.ToString(); lock_guard lock(pending_calls_lock_); auto iter = pending_calls_.find(uuid); diff --git a/service/low_energy_scanner.h b/service/low_energy_scanner.h index 279ea7baf..5e93bc68a 100644 --- a/service/low_energy_scanner.h +++ b/service/low_energy_scanner.h @@ -22,6 +22,7 @@ #include #include +#include #include "service/bluetooth_instance.h" #include "service/common/bluetooth/advertise_data.h" @@ -30,7 +31,6 @@ #include "service/common/bluetooth/scan_filter.h" #include "service/common/bluetooth/scan_result.h" #include "service/common/bluetooth/scan_settings.h" -#include "service/common/bluetooth/uuid.h" #include "service/hal/bluetooth_gatt_interface.h" namespace bluetooth { @@ -81,7 +81,7 @@ class LowEnergyScanner : private hal::BluetoothGattInterface::ScannerObserver, const ScanSettings& scan_settings() const { return scan_settings_; } // BluetoothInstace overrides: - const UUID& GetAppIdentifier() const override; + const Uuid& GetAppIdentifier() const override; int GetInstanceId() const override; void ScanResultCallback(hal::BluetoothGattInterface* gatt_iface, @@ -93,7 +93,7 @@ class LowEnergyScanner : private hal::BluetoothGattInterface::ScannerObserver, // Constructor shouldn't be called directly as instances are meant to be // obtained from the factory. - LowEnergyScanner(Adapter& adapter, const UUID& uuid, int scanner_id); + LowEnergyScanner(Adapter& adapter, const Uuid& uuid, int scanner_id); // Calls and clears the pending callbacks. void InvokeAndClearStartCallback(BLEStatus status); @@ -103,7 +103,7 @@ class LowEnergyScanner : private hal::BluetoothGattInterface::ScannerObserver, Adapter& adapter_; // See getters above for documentation. - UUID app_identifier_; + Uuid app_identifier_; int scanner_id_; // Protects device scan related members below. @@ -137,7 +137,7 @@ class LowEnergyScannerFactory ~LowEnergyScannerFactory() override; // BluetoothInstanceFactory override: - bool RegisterInstance(const UUID& app_uuid, + bool RegisterInstance(const Uuid& app_uuid, const RegisterCallback& callback) override; private: @@ -145,12 +145,12 @@ class LowEnergyScannerFactory // BluetoothGattInterface::ScannerObserver overrides: void RegisterScannerCallback(const RegisterCallback& callback, - const UUID& app_uuid, uint8_t scanner_id, + const Uuid& app_uuid, uint8_t scanner_id, uint8_t status); // Map of pending calls to register. std::mutex pending_calls_lock_; - std::unordered_set pending_calls_; + std::unordered_set pending_calls_; // Raw pointer to the Adapter that owns this factory. Adapter& adapter_; diff --git a/service/test/gatt_client_unittest.cc b/service/test/gatt_client_unittest.cc index 2d5910699..e039d0d71 100644 --- a/service/test/gatt_client_unittest.cc +++ b/service/test/gatt_client_unittest.cc @@ -33,7 +33,7 @@ class MockGattHandler MockGattHandler() = default; ~MockGattHandler() override = default; - MOCK_METHOD1(RegisterClient, bt_status_t(const bt_uuid_t&)); + MOCK_METHOD1(RegisterClient, bt_status_t(const bluetooth::Uuid&)); MOCK_METHOD1(UnregisterClient, bt_status_t(int)); MOCK_METHOD1(Scan, bt_status_t(bool)); MOCK_METHOD4(Connect, bt_status_t(int, const RawAddress&, bool, int)); @@ -85,11 +85,11 @@ TEST_F(GattClientTest, RegisterInstance) { // These will be asynchronously populated with a result when the callback // executes. BLEStatus status = BLE_STATUS_SUCCESS; - UUID cb_uuid; + Uuid cb_uuid; std::unique_ptr client; int callback_count = 0; - auto callback = [&](BLEStatus in_status, const UUID& uuid, + auto callback = [&](BLEStatus in_status, const Uuid& uuid, std::unique_ptr in_client) { status = in_status; cb_uuid = uuid; @@ -98,7 +98,7 @@ TEST_F(GattClientTest, RegisterInstance) { callback_count++; }; - UUID uuid0 = UUID::GetRandom(); + Uuid uuid0 = Uuid::GetRandom(); // HAL returns failure. EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); @@ -108,30 +108,28 @@ TEST_F(GattClientTest, RegisterInstance) { EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback)); EXPECT_EQ(0, callback_count); - // Calling twice with the same UUID should fail with no additional call into + // Calling twice with the same Uuid should fail with no additional call into // the stack. EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); - // Call with a different UUID while one is pending. - UUID uuid1 = UUID::GetRandom(); + // Call with a different Uuid while one is pending. + Uuid uuid1 = Uuid::GetRandom(); EXPECT_CALL(*mock_handler_, RegisterClient(_)) .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback)); - // Trigger callback with an unknown UUID. This should get ignored. - UUID uuid2 = UUID::GetRandom(); - bt_uuid_t hal_uuid = uuid2.GetBlueDroid(); - fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, hal_uuid); + // Trigger callback with an unknown Uuid. This should get ignored. + Uuid uuid2 = Uuid::GetRandom(); + fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, uuid2); EXPECT_EQ(0, callback_count); // |uuid0| succeeds. int client_id0 = 2; // Pick something that's not 0. - hal_uuid = uuid0.GetBlueDroid(); fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_SUCCESS, - client_id0, hal_uuid); + client_id0, uuid0); EXPECT_EQ(1, callback_count); ASSERT_TRUE(client.get() != nullptr); // Assert to terminate in case of error @@ -149,9 +147,8 @@ TEST_F(GattClientTest, RegisterInstance) { // |uuid1| fails. int client_id1 = 3; - hal_uuid = uuid1.GetBlueDroid(); fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_FAIL, client_id1, - hal_uuid); + uuid1); EXPECT_EQ(2, callback_count); ASSERT_TRUE(client.get() == nullptr); // Assert to terminate in case of error diff --git a/service/test/gatt_server_unittest.cc b/service/test/gatt_server_unittest.cc index 0e70ac9d2..16a320c1a 100644 --- a/service/test/gatt_server_unittest.cc +++ b/service/test/gatt_server_unittest.cc @@ -32,11 +32,12 @@ class MockGattHandler MockGattHandler() = default; ~MockGattHandler() override = default; - MOCK_METHOD1(RegisterServer, bt_status_t(const bt_uuid_t&)); + MOCK_METHOD1(RegisterServer, bt_status_t(const bluetooth::Uuid&)); MOCK_METHOD1(UnregisterServer, bt_status_t(int)); MOCK_METHOD2(AddService, bt_status_t(int, std::vector)); - MOCK_METHOD5(AddCharacteristic, bt_status_t(int, int, bt_uuid_t*, int, int)); - MOCK_METHOD4(AddDescriptor, bt_status_t(int, int, bt_uuid_t*, int)); + MOCK_METHOD5(AddCharacteristic, + bt_status_t(int, int, bluetooth::Uuid*, int, int)); + MOCK_METHOD4(AddDescriptor, bt_status_t(int, int, bluetooth::Uuid*, int)); MOCK_METHOD3(StartService, bt_status_t(int, int, int)); MOCK_METHOD2(DeleteService, bt_status_t(int, int)); MOCK_METHOD5(SendIndication, @@ -211,8 +212,8 @@ class GattServerPostRegisterTest : public GattServerTest { void SetUp() override { GattServerTest::SetUp(); - UUID uuid = UUID::GetRandom(); - auto callback = [&](BLEStatus status, const UUID& in_uuid, + Uuid uuid = Uuid::GetRandom(); + auto callback = [&](BLEStatus status, const Uuid& in_uuid, std::unique_ptr in_client) { CHECK(in_uuid == uuid); CHECK(in_client.get()); @@ -228,9 +229,8 @@ class GattServerPostRegisterTest : public GattServerTest { factory_->RegisterInstance(uuid, callback); - bt_uuid_t hal_uuid = uuid.GetBlueDroid(); - fake_hal_gatt_iface_->NotifyRegisterServerCallback( - BT_STATUS_SUCCESS, kDefaultServerId, hal_uuid); + fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_SUCCESS, + kDefaultServerId, uuid); } void TearDown() override { @@ -246,9 +246,9 @@ class GattServerPostRegisterTest : public GattServerTest { .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); - UUID uuid0 = UUID::GetRandom(); - UUID uuid1 = UUID::GetRandom(); - UUID uuid2 = UUID::GetRandom(); + Uuid uuid0 = Uuid::GetRandom(); + Uuid uuid1 = Uuid::GetRandom(); + Uuid uuid2 = Uuid::GetRandom(); bool register_success = false; @@ -257,7 +257,7 @@ class GattServerPostRegisterTest : public GattServerTest { ASSERT_TRUE(gatt_server_->AddService( service, [&](BLEStatus status, const Service& added_service) { ASSERT_EQ(BLE_STATUS_SUCCESS, status); - ASSERT_TRUE(UUID(added_service.uuid()) == UUID(service.uuid())); + ASSERT_TRUE(Uuid(added_service.uuid()) == Uuid(service.uuid())); ASSERT_TRUE(added_service.handle() == 0x0001); register_success = true; })); @@ -268,13 +268,13 @@ class GattServerPostRegisterTest : public GattServerTest { std::vector service_with_handles = { {.type = BTGATT_DB_PRIMARY_SERVICE, - .uuid = uuid0.GetBlueDroid(), + .uuid = uuid0, .attribute_handle = srvc_handle_}, {.type = BTGATT_DB_CHARACTERISTIC, - .uuid = uuid1.GetBlueDroid(), + .uuid = uuid1, .attribute_handle = char_handle_}, {.type = BTGATT_DB_DESCRIPTOR, - .uuid = uuid2.GetBlueDroid(), + .uuid = uuid2, .attribute_handle = desc_handle_}, }; @@ -306,11 +306,11 @@ TEST_F(GattServerTest, RegisterServer) { // These will be asynchronously populate with a result when the callback // executes. BLEStatus status = BLE_STATUS_SUCCESS; - UUID cb_uuid; + Uuid cb_uuid; std::unique_ptr server; int callback_count = 0; - auto callback = [&](BLEStatus in_status, const UUID& uuid, + auto callback = [&](BLEStatus in_status, const Uuid& uuid, std::unique_ptr in_server) { status = in_status; cb_uuid = uuid; @@ -319,7 +319,7 @@ TEST_F(GattServerTest, RegisterServer) { callback_count++; }; - UUID uuid0 = UUID::GetRandom(); + Uuid uuid0 = Uuid::GetRandom(); // HAL returns failure. EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); @@ -329,30 +329,28 @@ TEST_F(GattServerTest, RegisterServer) { EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback)); EXPECT_EQ(0, callback_count); - // Calling twice with the same UUID should fail with no additional calls into + // Calling twice with the same Uuid should fail with no additional calls into // the stack. EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); - // Call with a different UUID while one is pending. - UUID uuid1 = UUID::GetRandom(); + // Call with a different Uuid while one is pending. + Uuid uuid1 = Uuid::GetRandom(); EXPECT_CALL(*mock_handler_, RegisterServer(_)) .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback)); - // Trigger callback with an unknown UUID. This should get ignored. - UUID uuid2 = UUID::GetRandom(); - bt_uuid_t hal_uuid = uuid2.GetBlueDroid(); + // Trigger callback with an unknown Uuid. This should get ignored. + bluetooth::Uuid hal_uuid = bluetooth::Uuid::GetRandom(); fake_hal_gatt_iface_->NotifyRegisterServerCallback(0, 0, hal_uuid); EXPECT_EQ(0, callback_count); // |uuid0| succeeds. int server_if0 = 2; // Pick something that's not 0. - hal_uuid = uuid0.GetBlueDroid(); fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_SUCCESS, - server_if0, hal_uuid); + server_if0, uuid0); EXPECT_EQ(1, callback_count); ASSERT_TRUE(server.get() != nullptr); // Assert to terminate in case of error @@ -371,9 +369,8 @@ TEST_F(GattServerTest, RegisterServer) { // |uuid1| fails. int server_if1 = 3; - hal_uuid = uuid1.GetBlueDroid(); fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_FAIL, server_if1, - hal_uuid); + uuid1); EXPECT_EQ(2, callback_count); ASSERT_TRUE(server.get() == nullptr); // Assert to terminate in case of error diff --git a/service/test/low_energy_advertiser_unittest.cc b/service/test/low_energy_advertiser_unittest.cc index 03188d73e..9ea396c82 100644 --- a/service/test/low_energy_advertiser_unittest.cc +++ b/service/test/low_energy_advertiser_unittest.cc @@ -126,8 +126,8 @@ class LowEnergyAdvertiserPostRegisterTest : public LowEnergyAdvertiserTest { void RegisterTestAdvertiser( const std::function advertiser)> callback) { - UUID uuid = UUID::GetRandom(); - auto api_callback = [&](BLEStatus status, const UUID& in_uuid, + Uuid uuid = Uuid::GetRandom(); + auto api_callback = [&](BLEStatus status, const Uuid& in_uuid, std::unique_ptr in_client) { CHECK(in_uuid == uuid); CHECK(in_client.get()); @@ -207,11 +207,11 @@ TEST_F(LowEnergyAdvertiserTest, RegisterInstance) { // These will be asynchronously populated with a result when the callback // executes. BLEStatus status = BLE_STATUS_SUCCESS; - UUID cb_uuid; + Uuid cb_uuid; std::unique_ptr advertiser; int callback_count = 0; - auto callback = [&](BLEStatus in_status, const UUID& uuid, + auto callback = [&](BLEStatus in_status, const Uuid& uuid, std::unique_ptr in_client) { status = in_status; cb_uuid = uuid; @@ -220,7 +220,7 @@ TEST_F(LowEnergyAdvertiserTest, RegisterInstance) { callback_count++; }; - UUID uuid0 = UUID::GetRandom(); + Uuid uuid0 = Uuid::GetRandom(); reg_cb reg_adv1_cb; EXPECT_CALL(*mock_handler_, RegisterAdvertiser(_)) @@ -231,14 +231,14 @@ TEST_F(LowEnergyAdvertiserTest, RegisterInstance) { EXPECT_TRUE(ble_advertiser_factory_->RegisterInstance(uuid0, callback)); EXPECT_EQ(0, callback_count); - // Calling twice with the same UUID should fail with no additional call into + // Calling twice with the same Uuid should fail with no additional call into // the stack. EXPECT_FALSE(ble_advertiser_factory_->RegisterInstance(uuid0, callback)); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); - // Call with a different UUID while one is pending. - UUID uuid1 = UUID::GetRandom(); + // Call with a different Uuid while one is pending. + Uuid uuid1 = Uuid::GetRandom(); reg_cb reg_adv2_cb; EXPECT_CALL(*mock_handler_, RegisterAdvertiser(_)) .Times(1) @@ -426,11 +426,11 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, InvalidAdvertiseData) { } TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { - const std::vector kUUID16BitData{ + const std::vector kUuid16BitData{ 0x03, HCI_EIR_COMPLETE_16BITS_UUID_TYPE, 0xDE, 0xAD, }; - const std::vector kUUID32BitData{ + const std::vector kUuid32BitData{ 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02}; const std::vector kUUID128BitData{ @@ -444,7 +444,7 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { 0x0B, 0x0C, 0x0D, 0x0E}; - const std::vector kMultiUUIDData{ + const std::vector kMultiUuidData{ 0x11, HCI_EIR_COMPLETE_128BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, @@ -492,7 +492,7 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { 0xDE, 0xAD, 0xBE, 0xEF}; - const std::vector kServiceUUIDMatch{ + const std::vector kServiceUuidMatch{ 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, @@ -501,7 +501,7 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { 0x01, 0x02, 0xBE, 0xEF}; - const std::vector kServiceUUIDMismatch{ + const std::vector kServiceUuidMismatch{ 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x01, @@ -510,18 +510,18 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { 0x01, 0x02, 0xBE, 0xEF}; - AdvertiseData uuid_16bit_adv(kUUID16BitData); - AdvertiseData uuid_32bit_adv(kUUID32BitData); + AdvertiseData uuid_16bit_adv(kUuid16BitData); + AdvertiseData uuid_32bit_adv(kUuid32BitData); AdvertiseData uuid_128bit_adv(kUUID128BitData); - AdvertiseData multi_uuid_adv(kMultiUUIDData); + AdvertiseData multi_uuid_adv(kMultiUuidData); AdvertiseData service_16bit_adv(kServiceData16Bit); AdvertiseData service_32bit_adv(kServiceData32Bit); AdvertiseData service_128bit_adv(kServiceData128Bit); AdvertiseData multi_service_adv(kMultiServiceData); - AdvertiseData service_uuid_match(kServiceUUIDMatch); - AdvertiseData service_uuid_mismatch(kServiceUUIDMismatch); + AdvertiseData service_uuid_match(kServiceUuidMatch); + AdvertiseData service_uuid_mismatch(kServiceUuidMismatch); AdvertiseSettings settings; @@ -533,7 +533,7 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { }; status_cb start_advertising_cb; - // Multiple UUID test + // Multiple Uuid test EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _)) .Times(1) .WillOnce(SaveArg<1>(&start_advertising_cb)); @@ -612,7 +612,7 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { EXPECT_EQ(8, callback_count); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); - // Service data and UUID where the UUID for both match, should succeed. + // Service data and Uuid where the Uuid for both match, should succeed. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _)) .Times(1) .WillOnce(SaveArg<1>(&start_advertising_cb)); @@ -620,7 +620,7 @@ TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) { EXPECT_EQ(9, callback_count); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); - // Service data and UUID where the UUID for dont match, should fail + // Service data and Uuid where the Uuid for dont match, should fail EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _)) .Times(1) .WillOnce(SaveArg<1>(&start_advertising_cb)); diff --git a/service/test/low_energy_client_unittest.cc b/service/test/low_energy_client_unittest.cc index 5870f2eac..2e04accbf 100644 --- a/service/test/low_energy_client_unittest.cc +++ b/service/test/low_energy_client_unittest.cc @@ -40,7 +40,7 @@ class MockGattHandler MockGattHandler(){}; ~MockGattHandler() override = default; - MOCK_METHOD1(RegisterClient, bt_status_t(const bt_uuid_t&)); + MOCK_METHOD1(RegisterClient, bt_status_t(const bluetooth::Uuid&)); MOCK_METHOD1(UnregisterClient, bt_status_t(int)); MOCK_METHOD4(Connect, bt_status_t(int, const RawAddress&, bool, int)); MOCK_METHOD3(Disconnect, bt_status_t(int, const RawAddress&, int)); @@ -134,8 +134,8 @@ class LowEnergyClientPostRegisterTest : public LowEnergyClientTest { void RegisterTestClient( const std::function client)> callback) { - UUID uuid = UUID::GetRandom(); - auto api_callback = [&](BLEStatus status, const UUID& in_uuid, + Uuid uuid = Uuid::GetRandom(); + auto api_callback = [&](BLEStatus status, const Uuid& in_uuid, std::unique_ptr in_client) { CHECK(in_uuid == uuid); CHECK(in_client.get()); @@ -151,9 +151,8 @@ class LowEnergyClientPostRegisterTest : public LowEnergyClientTest { ble_factory_->RegisterInstance(uuid, api_callback); - bt_uuid_t hal_uuid = uuid.GetBlueDroid(); fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, next_client_id_++, - hal_uuid); + uuid); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); } @@ -175,11 +174,11 @@ TEST_F(LowEnergyClientTest, RegisterInstance) { // These will be asynchronously populated with a result when the callback // executes. BLEStatus status = BLE_STATUS_SUCCESS; - UUID cb_uuid; + Uuid cb_uuid; std::unique_ptr client; int callback_count = 0; - auto callback = [&](BLEStatus in_status, const UUID& uuid, + auto callback = [&](BLEStatus in_status, const Uuid& uuid, std::unique_ptr in_client) { status = in_status; cb_uuid = uuid; @@ -188,7 +187,7 @@ TEST_F(LowEnergyClientTest, RegisterInstance) { callback_count++; }; - UUID uuid0 = UUID::GetRandom(); + Uuid uuid0 = Uuid::GetRandom(); // HAL returns failure. EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback)); @@ -198,30 +197,28 @@ TEST_F(LowEnergyClientTest, RegisterInstance) { EXPECT_TRUE(ble_factory_->RegisterInstance(uuid0, callback)); EXPECT_EQ(0, callback_count); - // Calling twice with the same UUID should fail with no additional call into + // Calling twice with the same Uuid should fail with no additional call into // the stack. EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback)); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); - // Call with a different UUID while one is pending. - UUID uuid1 = UUID::GetRandom(); + // Call with a different Uuid while one is pending. + Uuid uuid1 = Uuid::GetRandom(); EXPECT_CALL(*mock_handler_, RegisterClient(_)) .Times(1) .WillOnce(Return(BT_STATUS_SUCCESS)); EXPECT_TRUE(ble_factory_->RegisterInstance(uuid1, callback)); - // Trigger callback with an unknown UUID. This should get ignored. - UUID uuid2 = UUID::GetRandom(); - bt_uuid_t hal_uuid = uuid2.GetBlueDroid(); - fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, hal_uuid); + // Trigger callback with an unknown Uuid. This should get ignored. + Uuid uuid2 = Uuid::GetRandom(); + fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, uuid2); EXPECT_EQ(0, callback_count); // |uuid0| succeeds. int client_if0 = 2; // Pick something that's not 0. - hal_uuid = uuid0.GetBlueDroid(); fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_SUCCESS, - client_if0, hal_uuid); + client_if0, uuid0); EXPECT_EQ(1, callback_count); ASSERT_TRUE(client.get() != nullptr); // Assert to terminate in case of error @@ -239,9 +236,8 @@ TEST_F(LowEnergyClientTest, RegisterInstance) { // |uuid1| fails. int client_if1 = 3; - hal_uuid = uuid1.GetBlueDroid(); fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_FAIL, client_if1, - hal_uuid); + uuid1); EXPECT_EQ(2, callback_count); ASSERT_TRUE(client.get() == nullptr); // Assert to terminate in case of error diff --git a/service/test/low_energy_scanner_unittest.cc b/service/test/low_energy_scanner_unittest.cc index d723a509e..8eae46fd1 100644 --- a/service/test/low_energy_scanner_unittest.cc +++ b/service/test/low_energy_scanner_unittest.cc @@ -73,8 +73,8 @@ class MockScannerHandler : public BleScannerInterface { void ScanFilterAddRemove(int action, int filt_type, int filt_index, int company_id, int company_id_mask, - const bt_uuid_t* p_uuid, - const bt_uuid_t* p_uuid_mask, + const bluetooth::Uuid* p_uuid, + const bluetooth::Uuid* p_uuid_mask, const RawAddress* bd_addr, char addr_type, std::vector data, std::vector p_mask, @@ -164,8 +164,8 @@ class LowEnergyScannerPostRegisterTest : public LowEnergyScannerTest { void RegisterTestScanner( const std::function scanner)> callback) { - UUID uuid = UUID::GetRandom(); - auto api_callback = [&](BLEStatus status, const UUID& in_uuid, + Uuid uuid = Uuid::GetRandom(); + auto api_callback = [&](BLEStatus status, const Uuid& in_uuid, std::unique_ptr in_scanner) { CHECK(in_uuid == uuid); CHECK(in_scanner.get()); @@ -204,11 +204,11 @@ TEST_F(LowEnergyScannerTest, RegisterInstance) { // These will be asynchronously populated with a result when the callback // executes. BLEStatus status = BLE_STATUS_SUCCESS; - UUID cb_uuid; + Uuid cb_uuid; std::unique_ptr scanner; int callback_count = 0; - auto callback = [&](BLEStatus in_status, const UUID& uuid, + auto callback = [&](BLEStatus in_status, const Uuid& uuid, std::unique_ptr in_scanner) { status = in_status; cb_uuid = uuid; @@ -217,20 +217,20 @@ TEST_F(LowEnergyScannerTest, RegisterInstance) { callback_count++; }; - UUID uuid0 = UUID::GetRandom(); + Uuid uuid0 = Uuid::GetRandom(); // HAL returns success. EXPECT_TRUE(ble_factory_->RegisterInstance(uuid0, callback)); EXPECT_EQ(0, callback_count); - // Calling twice with the same UUID should fail with no additional call into + // Calling twice with the same Uuid should fail with no additional call into // the stack. EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback)); ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); - // Call with a different UUID while one is pending. - UUID uuid1 = UUID::GetRandom(); + // Call with a different Uuid while one is pending. + Uuid uuid1 = Uuid::GetRandom(); BleScannerInterface::RegisterCallback reg_scanner_cb2; EXPECT_CALL(*mock_handler_, RegisterScanner(_)) .Times(1) diff --git a/service/test/parcelable_unittest.cc b/service/test/parcelable_unittest.cc index dcfb8fbb0..33c39eedb 100644 --- a/service/test/parcelable_unittest.cc +++ b/service/test/parcelable_unittest.cc @@ -37,7 +37,7 @@ using bluetooth::ScanFilter; using bluetooth::ScanResult; using bluetooth::ScanSettings; using bluetooth::Service; -using bluetooth::UUID; +using bluetooth::Uuid; namespace bluetooth { namespace { @@ -82,11 +82,11 @@ TEST(ParcelableTest, NonEmptyAdvertiseSettings) { EXPECT_TRUE(result); } -TEST(ParcelableTest, UUID) { - // Try a whole bunch of UUIDs. +TEST(ParcelableTest, Uuid) { + // Try a whole bunch of Uuids. for (int i = 0; i < 10; i++) { - UUID uuid = UUID::GetRandom(); - TestData(uuid); + Uuid uuid = Uuid::GetRandom(); + TestData(uuid); } } @@ -115,13 +115,13 @@ TEST(ParcelableTest, ScanFilter) { bool result = TestData(filter); EXPECT_TRUE(result); - UUID uuid = UUID::GetRandom(); + Uuid uuid = Uuid::GetRandom(); filter.SetServiceUuid(uuid); result = TestData(filter); EXPECT_TRUE(result); - UUID mask = UUID::GetRandom(); + Uuid mask = Uuid::GetRandom(); filter.SetServiceUuidWithMask(uuid, mask); result = TestData(filter); EXPECT_TRUE(result); @@ -146,11 +146,11 @@ TEST(ParcelableTest, ScanResult) { } TEST(ParcelableTest, GattDescriptor) { - Descriptor s = Descriptor(0x0000, UUID::GetRandom(), + Descriptor s = Descriptor(0x0000, Uuid::GetRandom(), bluetooth::kAttributePermissionRead); - Descriptor s2 = Descriptor(0xFFFE, UUID::GetRandom(), + Descriptor s2 = Descriptor(0xFFFE, Uuid::GetRandom(), bluetooth::kAttributePermissionWrite); - Descriptor s3 = Descriptor(0x003D, UUID::GetRandom(), + Descriptor s3 = Descriptor(0x003D, Uuid::GetRandom(), bluetooth::kAttributePermissionReadEncryptedMITM | bluetooth::kAttributePermissionRead); @@ -168,10 +168,10 @@ TEST(ParcelableTest, GattDescriptor) { } TEST(ParcelableTest, GattCharacteristic) { - Characteristic c = Characteristic(0x0004, UUID::GetRandom(), 0, 0, - {Descriptor(0x0005, UUID::GetRandom(), 0), - Descriptor(0x0007, UUID::GetRandom(), 0), - Descriptor(0x00A1, UUID::GetRandom(), 0)}); + Characteristic c = Characteristic(0x0004, Uuid::GetRandom(), 0, 0, + {Descriptor(0x0005, Uuid::GetRandom(), 0), + Descriptor(0x0007, Uuid::GetRandom(), 0), + Descriptor(0x00A1, Uuid::GetRandom(), 0)}); bool result = TestData( @@ -181,19 +181,19 @@ TEST(ParcelableTest, GattCharacteristic) { TEST(ParcelableTest, GattService) { Service s = - Service(0x0001, true, UUID("CAFE"), - {Characteristic(0x0004, UUID::GetRandom(), + Service(0x0001, true, Uuid::FromString("CAFE", nullptr), + {Characteristic(0x0004, Uuid::GetRandom(), bluetooth::kCharacteristicPropertyNotify, bluetooth::kAttributePermissionRead, - {Descriptor(0x0005, UUID::GetRandom(), 0), - Descriptor(0x0007, UUID::GetRandom(), 0), - Descriptor(0x0009, UUID::GetRandom(), 0)}), - Characteristic(0x000D, UUID::GetRandom(), + {Descriptor(0x0005, Uuid::GetRandom(), 0), + Descriptor(0x0007, Uuid::GetRandom(), 0), + Descriptor(0x0009, Uuid::GetRandom(), 0)}), + Characteristic(0x000D, Uuid::GetRandom(), bluetooth::kCharacteristicPropertyWrite, bluetooth::kAttributePermissionWrite, - {Descriptor(0x0010, UUID::GetRandom(), 0), - Descriptor(0x0012, UUID::GetRandom(), 0)}), - Characteristic(0x0015, UUID::GetRandom(), 0, 0, {})}, + {Descriptor(0x0010, Uuid::GetRandom(), 0), + Descriptor(0x0012, Uuid::GetRandom(), 0)}), + Characteristic(0x0015, Uuid::GetRandom(), 0, 0, {})}, {}); Parcel parcel; diff --git a/service/test/uuid_unittest.cc b/service/test/uuid_unittest.cc deleted file mode 100644 index 3a8103a37..000000000 --- a/service/test/uuid_unittest.cc +++ /dev/null @@ -1,160 +0,0 @@ -// -// Copyright (C) 2015 Google, Inc. -// -// 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 -#include - -#include - -#include "service/common/bluetooth/uuid.h" - -using namespace bluetooth; - -namespace { - -const std::array kBtSigBaseUUID = {{ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5f, 0x9b, 0x34, 0xfb, -}}; - -} // namespace - -// Verify that an uninitialized UUID is equal -// To the BT SIG Base UUID. -TEST(UUIDTest, DefaultUUID) { - UUID uuid; - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullBigEndian() == kBtSigBaseUUID); -} - -// Verify that we initialize a 16-bit UUID in a -// way consistent with how we read it. -TEST(UUIDTest, Init16Bit) { - auto my_uuid_16 = kBtSigBaseUUID; - my_uuid_16[2] = 0xde; - my_uuid_16[3] = 0xad; - UUID uuid(UUID::UUID16Bit({{0xde, 0xad}})); - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_16); - ASSERT_TRUE(UUID::kNumBytes16 == uuid.GetShortestRepresentationSize()); -} - -// Verify that we initialize a 16-bit UUID in a -// way consistent with how we read it. -TEST(UUIDTest, Init16BitString) { - auto my_uuid_16 = kBtSigBaseUUID; - my_uuid_16[2] = 0xde; - my_uuid_16[3] = 0xad; - UUID uuid("dead"); - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_16); - ASSERT_TRUE(UUID::kNumBytes16 == uuid.GetShortestRepresentationSize()); - - uuid = UUID("0xdead"); - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_16); - ASSERT_TRUE(UUID::kNumBytes16 == uuid.GetShortestRepresentationSize()); -} - -// Verify that we initialize a 32-bit UUID in a -// way consistent with how we read it. -TEST(UUIDTest, Init32Bit) { - auto my_uuid_32 = kBtSigBaseUUID; - my_uuid_32[0] = 0xde; - my_uuid_32[1] = 0xad; - my_uuid_32[2] = 0xbe; - my_uuid_32[3] = 0xef; - UUID uuid(UUID::UUID32Bit({{0xde, 0xad, 0xbe, 0xef}})); - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_32); - ASSERT_TRUE(UUID::kNumBytes32 == uuid.GetShortestRepresentationSize()); -} - -// Verify correct reading of a 32-bit UUID initialized from string. -TEST(UUIDTest, Init32BitString) { - auto my_uuid_32 = kBtSigBaseUUID; - my_uuid_32[0] = 0xde; - my_uuid_32[1] = 0xad; - my_uuid_32[2] = 0xbe; - my_uuid_32[3] = 0xef; - UUID uuid("deadbeef"); - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_32); - ASSERT_TRUE(UUID::kNumBytes32 == uuid.GetShortestRepresentationSize()); -} - -// Verify that we initialize a 128-bit UUID in a -// way consistent with how we read it. -TEST(UUIDTest, Init128Bit) { - auto my_uuid_128 = kBtSigBaseUUID; - for (int i = 0; i < static_cast(my_uuid_128.size()); ++i) { - my_uuid_128[i] = i; - } - - UUID uuid(my_uuid_128); - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_128); - ASSERT_TRUE(UUID::kNumBytes128 == uuid.GetShortestRepresentationSize()); -} - -// Verify that we initialize a 128-bit UUID in a -// way consistent with how we read it as LE. -TEST(UUIDTest, Init128BitLittleEndian) { - auto my_uuid_128 = kBtSigBaseUUID; - for (int i = 0; i < static_cast(my_uuid_128.size()); ++i) { - my_uuid_128[i] = i; - } - - UUID uuid(my_uuid_128); - std::reverse(my_uuid_128.begin(), my_uuid_128.end()); - ASSERT_TRUE(uuid.is_valid()); - ASSERT_TRUE(uuid.GetFullLittleEndian() == my_uuid_128); -} - -// Verify that we initialize a 128-bit UUID in a -// way consistent with how we read it. -TEST(UUIDTest, Init128BitString) { - UUID::UUID128Bit my_uuid{ - {7, 1, 6, 8, 14, 255, 16, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; - std::string my_uuid_string("07010608-0eff-1002-0304-05060708090a"); - - UUID uuid0(my_uuid); - UUID uuid1(my_uuid_string); - - ASSERT_TRUE(uuid0.is_valid()); - ASSERT_TRUE(uuid1.is_valid()); - ASSERT_TRUE(uuid0 == uuid1); - ASSERT_TRUE(UUID::kNumBytes128 == uuid0.GetShortestRepresentationSize()); -} - -TEST(UUIDTest, InitInvalid) { - UUID uuid0("000102030405060708090A0B0C0D0E0F"); - ASSERT_FALSE(uuid0.is_valid()); - - UUID uuid1("1*90"); - ASSERT_FALSE(uuid1.is_valid()); - - UUID uuid2("109g"); - ASSERT_FALSE(uuid1.is_valid()); -} - -TEST(UUIDTest, ToString) { - const UUID::UUID16Bit data{{0x18, 0x0d}}; - UUID uuid(data); - std::string uuid_string = uuid.ToString(); - EXPECT_EQ("0000180d-0000-1000-8000-00805f9b34fb", uuid_string); -} diff --git a/stack/a2dp/a2dp_api.cc b/stack/a2dp/a2dp_api.cc index 4be2bddec..3c2f1bb81 100644 --- a/stack/a2dp/a2dp_api.cc +++ b/stack/a2dp/a2dp_api.cc @@ -35,6 +35,8 @@ #include "osi/include/log.h" #include "sdpdefs.h" +using bluetooth::Uuid; + /***************************************************************************** * Global data ****************************************************************************/ @@ -269,7 +271,6 @@ tA2DP_STATUS A2DP_AddRecord(uint16_t service_uuid, char* p_service_name, tA2DP_STATUS A2DP_FindService(uint16_t service_uuid, const RawAddress& bd_addr, tA2DP_SDP_DB_PARAMS* p_db, tA2DP_FIND_CBACK* p_cback) { - tSDP_UUID uuid_list; bool result = true; LOG_VERBOSE(LOG_TAG, "%s: uuid: 0x%x", __func__, service_uuid); @@ -282,10 +283,6 @@ tA2DP_STATUS A2DP_FindService(uint16_t service_uuid, const RawAddress& bd_addr, a2dp_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK) return A2DP_BUSY; - /* set up discovery database */ - uuid_list.len = LEN_UUID_16; - uuid_list.uu.uuid16 = service_uuid; - if (p_db->p_attrs == NULL || p_db->num_attr == 0) { p_db->p_attrs = a2dp_attr_list; p_db->num_attr = A2DP_NUM_ATTR; @@ -294,6 +291,7 @@ tA2DP_STATUS A2DP_FindService(uint16_t service_uuid, const RawAddress& bd_addr, if (a2dp_cb.find.p_db == NULL) a2dp_cb.find.p_db = (tSDP_DISCOVERY_DB*)osi_malloc(p_db->db_len); + Uuid uuid_list = Uuid::From16Bit(service_uuid); result = SDP_InitDiscoveryDb(a2dp_cb.find.p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr, p_db->p_attrs); diff --git a/stack/avrc/avrc_sdp.cc b/stack/avrc/avrc_sdp.cc index 3a7ed9618..0527fa383 100644 --- a/stack/avrc/avrc_sdp.cc +++ b/stack/avrc/avrc_sdp.cc @@ -27,6 +27,8 @@ #include "avrc_int.h" #include "bt_common.h" +using bluetooth::Uuid; + /***************************************************************************** * Global data ****************************************************************************/ @@ -106,7 +108,6 @@ static void avrc_sdp_cback(uint16_t status) { uint16_t AVRC_FindService(uint16_t service_uuid, const RawAddress& bd_addr, tAVRC_SDP_DB_PARAMS* p_db, tAVRC_FIND_CBACK* p_cback) { - tSDP_UUID uuid_list; bool result = true; AVRC_TRACE_API("%s uuid: %x", __func__, service_uuid); @@ -120,15 +121,13 @@ uint16_t AVRC_FindService(uint16_t service_uuid, const RawAddress& bd_addr, avrc_cb.service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) return AVRC_NO_RESOURCES; - /* set up discovery database */ - uuid_list.len = LEN_UUID_16; - uuid_list.uu.uuid16 = service_uuid; if (p_db->p_attrs == NULL || p_db->num_attr == 0) { p_db->p_attrs = a2dp_attr_list_sdp; p_db->num_attr = AVRC_NUM_ATTR; } + Uuid uuid_list = Uuid::From16Bit(service_uuid); result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr, p_db->p_attrs); diff --git a/stack/bnep/bnep_api.cc b/stack/bnep/bnep_api.cc index 923ce5093..89cf0beb6 100644 --- a/stack/bnep/bnep_api.cc +++ b/stack/bnep/bnep_api.cc @@ -26,6 +26,8 @@ #include #include "bnep_int.h" +using bluetooth::Uuid; + /******************************************************************************* * * Function BNEP_Init @@ -123,8 +125,8 @@ void BNEP_Deregister(void) { * BNEP_NO_RESOURCES if no resources * ******************************************************************************/ -tBNEP_RESULT BNEP_Connect(const RawAddress& p_rem_bda, tBT_UUID* src_uuid, - tBT_UUID* dst_uuid, uint16_t* p_handle) { +tBNEP_RESULT BNEP_Connect(const RawAddress& p_rem_bda, const Uuid& src_uuid, + const Uuid& dst_uuid, uint16_t* p_handle) { uint16_t cid; tBNEP_CONN* p_bcb = bnepu_find_bcb_by_bd_addr(p_rem_bda); @@ -132,9 +134,6 @@ tBNEP_RESULT BNEP_Connect(const RawAddress& p_rem_bda, tBT_UUID* src_uuid, if (!bnep_cb.profile_registered) return BNEP_WRONG_STATE; - /* Both source and destination UUID lengths should be same */ - if (src_uuid->len != dst_uuid->len) return BNEP_CONN_FAILED_UUID_SIZE; - if (!p_bcb) { p_bcb = bnepu_allocate_bcb(p_rem_bda); if (p_bcb == NULL) return (BNEP_NO_RESOURCES); @@ -142,29 +141,27 @@ tBNEP_RESULT BNEP_Connect(const RawAddress& p_rem_bda, tBT_UUID* src_uuid, return BNEP_WRONG_STATE; else { /* Backup current UUID values to restore if role change fails */ - memcpy((uint8_t*)&(p_bcb->prv_src_uuid), (uint8_t*)&(p_bcb->src_uuid), - sizeof(tBT_UUID)); - memcpy((uint8_t*)&(p_bcb->prv_dst_uuid), (uint8_t*)&(p_bcb->dst_uuid), - sizeof(tBT_UUID)); + p_bcb->prv_src_uuid = p_bcb->src_uuid; + p_bcb->prv_dst_uuid = p_bcb->dst_uuid; } /* We are the originator of this connection */ p_bcb->con_flags |= BNEP_FLAGS_IS_ORIG; - memcpy((uint8_t*)&(p_bcb->src_uuid), (uint8_t*)src_uuid, sizeof(tBT_UUID)); - memcpy((uint8_t*)&(p_bcb->dst_uuid), (uint8_t*)dst_uuid, sizeof(tBT_UUID)); + p_bcb->src_uuid = src_uuid; + p_bcb->dst_uuid = dst_uuid; if (p_bcb->con_state == BNEP_STATE_CONNECTED) { /* Transition to the next appropriate state, waiting for connection confirm. */ p_bcb->con_state = BNEP_STATE_SEC_CHECKING; - BNEP_TRACE_API("BNEP initiating security procedures for src uuid 0x%x", - p_bcb->src_uuid.uu.uuid16); + BNEP_TRACE_API("BNEP initiating security procedures for src uuid %s", + p_bcb->src_uuid.ToString().c_str()); #if (BNEP_DO_AUTH_FOR_ROLE_SWITCH == TRUE) btm_sec_mx_access_request(p_bcb->rem_bda, BT_PSM_BNEP, true, - BTM_SEC_PROTO_BNEP, bnep_get_uuid32(src_uuid), + BTM_SEC_PROTO_BNEP, src_uuid.As32Bit(), &bnep_sec_check_complete, p_bcb); #else bnep_sec_check_complete(p_bcb->rem_bda, p_bcb, BTM_SUCCESS); @@ -249,10 +246,8 @@ tBNEP_RESULT BNEP_ConnectResp(uint16_t handle, tBNEP_RESULT resp) { p_bcb->con_state = BNEP_STATE_CONNECTED; p_bcb->con_flags &= (~BNEP_FLAGS_SETUP_RCVD); - memcpy((uint8_t*)&(p_bcb->src_uuid), (uint8_t*)&(p_bcb->prv_src_uuid), - sizeof(tBT_UUID)); - memcpy((uint8_t*)&(p_bcb->dst_uuid), (uint8_t*)&(p_bcb->prv_dst_uuid), - sizeof(tBT_UUID)); + p_bcb->src_uuid = p_bcb->prv_src_uuid; + p_bcb->dst_uuid = p_bcb->prv_dst_uuid; } /* Process remaining part of the setup message (extension headers) */ @@ -680,8 +675,8 @@ tBNEP_RESULT BNEP_GetStatus(uint16_t handle, tBNEP_STATUS* p_status) { p_status->rcvd_mcast_filters = p_bcb->rcvd_mcast_filters; p_status->rem_bda = p_bcb->rem_bda; - memcpy(&(p_status->src_uuid), &(p_bcb->src_uuid), sizeof(tBT_UUID)); - memcpy(&(p_status->dst_uuid), &(p_bcb->dst_uuid), sizeof(tBT_UUID)); + p_status->src_uuid = p_bcb->src_uuid; + p_status->dst_uuid = p_bcb->dst_uuid; return BNEP_SUCCESS; #else diff --git a/stack/bnep/bnep_int.h b/stack/bnep/bnep_int.h index e25e7f822..2a7bac0ac 100644 --- a/stack/bnep/bnep_int.h +++ b/stack/bnep/bnep_int.h @@ -149,10 +149,10 @@ typedef struct { uint16_t bad_pkts_rcvd; uint8_t re_transmits; uint16_t handle; - tBT_UUID prv_src_uuid; - tBT_UUID prv_dst_uuid; - tBT_UUID src_uuid; - tBT_UUID dst_uuid; + bluetooth::Uuid prv_src_uuid; + bluetooth::Uuid prv_dst_uuid; + bluetooth::Uuid src_uuid; + bluetooth::Uuid dst_uuid; } tBNEP_CONN; @@ -231,6 +231,5 @@ extern tBNEP_RESULT bnep_is_packet_allowed(tBNEP_CONN* p_bcb, uint16_t protocol, bool fw_ext_present, uint8_t* p_data); -extern uint32_t bnep_get_uuid32(tBT_UUID* src_uuid); #endif diff --git a/stack/bnep/bnep_main.cc b/stack/bnep/bnep_main.cc index 636a51179..246d5c03a 100644 --- a/stack/bnep/bnep_main.cc +++ b/stack/bnep/bnep_main.cc @@ -254,9 +254,9 @@ static void bnep_config_ind(uint16_t l2cap_cid, tL2CAP_CFG_INFO* p_cfg) { bnep_conn_timer_timeout, p_bcb); if (p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) { - btm_sec_mx_access_request( - p_bcb->rem_bda, BT_PSM_BNEP, true, BTM_SEC_PROTO_BNEP, - bnep_get_uuid32(&(p_bcb->src_uuid)), &bnep_sec_check_complete, p_bcb); + btm_sec_mx_access_request(p_bcb->rem_bda, BT_PSM_BNEP, true, + BTM_SEC_PROTO_BNEP, p_bcb->src_uuid.As32Bit(), + &bnep_sec_check_complete, p_bcb); } } } @@ -298,8 +298,7 @@ static void bnep_config_cfm(uint16_t l2cap_cid, tL2CAP_CFG_INFO* p_cfg) { if (p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) { btm_sec_mx_access_request(p_bcb->rem_bda, BT_PSM_BNEP, true, - BTM_SEC_PROTO_BNEP, - bnep_get_uuid32(&(p_bcb->src_uuid)), + BTM_SEC_PROTO_BNEP, p_bcb->src_uuid.As32Bit(), &bnep_sec_check_complete, p_bcb); } } diff --git a/stack/bnep/bnep_utils.cc b/stack/bnep/bnep_utils.cc index 29d41fcea..070f275b4 100644 --- a/stack/bnep/bnep_utils.cc +++ b/stack/bnep/bnep_utils.cc @@ -33,6 +33,8 @@ #include "device/include/controller.h" #include "osi/include/osi.h" +using bluetooth::Uuid; + /******************************************************************************/ /* L O C A L F U N C T I O N P R O T O T Y P E S */ /******************************************************************************/ @@ -164,8 +166,8 @@ void bnep_send_conn_req(tBNEP_CONN* p_bcb) { BT_HDR* p_buf = (BT_HDR*)osi_malloc(BNEP_BUF_SIZE); uint8_t *p, *p_start; - BNEP_TRACE_DEBUG("%s: sending setup req with dst uuid %x", __func__, - p_bcb->dst_uuid.uu.uuid16); + BNEP_TRACE_DEBUG("%s: sending setup req with dst uuid %s", __func__, + p_bcb->dst_uuid.ToString().c_str()); p_buf->offset = L2CAP_MIN_OFFSET; p = p_start = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET; @@ -176,22 +178,26 @@ void bnep_send_conn_req(tBNEP_CONN* p_bcb) { /* Put in filter message type - set filters */ UINT8_TO_BE_STREAM(p, BNEP_SETUP_CONNECTION_REQUEST_MSG); - UINT8_TO_BE_STREAM(p, p_bcb->dst_uuid.len); - - if (p_bcb->dst_uuid.len == 2) { - UINT16_TO_BE_STREAM(p, p_bcb->dst_uuid.uu.uuid16); - UINT16_TO_BE_STREAM(p, p_bcb->src_uuid.uu.uuid16); - } else if (p_bcb->dst_uuid.len == 4) { - UINT32_TO_BE_STREAM(p, p_bcb->dst_uuid.uu.uuid32); - UINT32_TO_BE_STREAM(p, p_bcb->src_uuid.uu.uuid32); - } else if (p_bcb->dst_uuid.len == 16) { - memcpy(p, p_bcb->dst_uuid.uu.uuid128, p_bcb->dst_uuid.len); - p += p_bcb->dst_uuid.len; - memcpy(p, p_bcb->src_uuid.uu.uuid128, p_bcb->dst_uuid.len); - p += p_bcb->dst_uuid.len; + int len = std::max(p_bcb->dst_uuid.GetShortestRepresentationSize(), + p_bcb->src_uuid.GetShortestRepresentationSize()); + + UINT8_TO_BE_STREAM(p, len); + + if (len == Uuid::kNumBytes16) { + UINT16_TO_BE_STREAM(p, p_bcb->dst_uuid.As16Bit()); + UINT16_TO_BE_STREAM(p, p_bcb->src_uuid.As16Bit()); + } else if (len == Uuid::kNumBytes32) { + UINT32_TO_BE_STREAM(p, p_bcb->dst_uuid.As32Bit()); + UINT32_TO_BE_STREAM(p, p_bcb->src_uuid.As32Bit()); + } else if (len == Uuid::kNumBytes128) { + memcpy(p, p_bcb->dst_uuid.To128BitBE().data(), Uuid::kNumBytes128); + p += Uuid::kNumBytes128; + memcpy(p, p_bcb->src_uuid.To128BitBE().data(), Uuid::kNumBytes128); + p += Uuid::kNumBytes128; } else { - BNEP_TRACE_ERROR("%s: uuid: %x, invalid length: %x", __func__, - p_bcb->dst_uuid.uu.uuid16, p_bcb->dst_uuid.len); + BNEP_TRACE_ERROR("%s: uuid: %s, invalid length: %zu", __func__, + p_bcb->dst_uuid.ToString().c_str(), + p_bcb->dst_uuid.GetShortestRepresentationSize()); } p_buf->len = (uint16_t)(p - p_start); @@ -523,8 +529,7 @@ static uint8_t* bnepu_init_hdr(BT_HDR* p_buf, uint16_t hdr_len, ******************************************************************************/ void bnep_process_setup_conn_req(tBNEP_CONN* p_bcb, uint8_t* p_setup, uint8_t len) { - BNEP_TRACE_EVENT("BNEP - bnep_process_setup_conn_req for CID: 0x%x", - p_bcb->l2cap_cid); + BNEP_TRACE_EVENT("BNEP - %s for CID: 0x%x", __func__, p_bcb->l2cap_cid); if (p_bcb->con_state != BNEP_STATE_CONN_SETUP && p_bcb->con_state != BNEP_STATE_SEC_CHECKING && @@ -553,36 +558,43 @@ void bnep_process_setup_conn_req(tBNEP_CONN* p_bcb, uint8_t* p_setup, } if (p_bcb->con_state == BNEP_STATE_CONNECTED) { - memcpy((uint8_t*)&(p_bcb->prv_src_uuid), (uint8_t*)&(p_bcb->src_uuid), - sizeof(tBT_UUID)); - memcpy((uint8_t*)&(p_bcb->prv_dst_uuid), (uint8_t*)&(p_bcb->dst_uuid), - sizeof(tBT_UUID)); + p_bcb->prv_src_uuid = p_bcb->src_uuid; + p_bcb->prv_dst_uuid = p_bcb->dst_uuid; } - p_bcb->dst_uuid.len = p_bcb->src_uuid.len = len; - - if (p_bcb->dst_uuid.len == 2) { + if (len == Uuid::kNumBytes16) { /* because peer initiated connection keep src uuid as dst uuid */ - BE_STREAM_TO_UINT16(p_bcb->src_uuid.uu.uuid16, p_setup); - BE_STREAM_TO_UINT16(p_bcb->dst_uuid.uu.uuid16, p_setup); + uint16_t tmp; + + BE_STREAM_TO_UINT16(tmp, p_setup); + p_bcb->src_uuid = Uuid::From16Bit(tmp); + + BE_STREAM_TO_UINT16(tmp, p_setup); + p_bcb->dst_uuid = Uuid::From16Bit(tmp); /* If nothing has changed don't bother the profile */ if (p_bcb->con_state == BNEP_STATE_CONNECTED && - p_bcb->src_uuid.uu.uuid16 == p_bcb->prv_src_uuid.uu.uuid16 && - p_bcb->dst_uuid.uu.uuid16 == p_bcb->prv_dst_uuid.uu.uuid16) { + p_bcb->src_uuid == p_bcb->prv_src_uuid && + p_bcb->dst_uuid == p_bcb->prv_dst_uuid) { bnep_send_conn_responce(p_bcb, BNEP_SETUP_CONN_OK); return; } - } else if (p_bcb->dst_uuid.len == 4) { - BE_STREAM_TO_UINT32(p_bcb->src_uuid.uu.uuid32, p_setup); - BE_STREAM_TO_UINT32(p_bcb->dst_uuid.uu.uuid32, p_setup); - } else if (p_bcb->dst_uuid.len == 16) { - memcpy(p_bcb->src_uuid.uu.uuid128, p_setup, p_bcb->src_uuid.len); - p_setup += p_bcb->src_uuid.len; - memcpy(p_bcb->dst_uuid.uu.uuid128, p_setup, p_bcb->dst_uuid.len); - p_setup += p_bcb->dst_uuid.len; + } else if (len == Uuid::kNumBytes32) { + uint32_t tmp; + + BE_STREAM_TO_UINT32(tmp, p_setup); + p_bcb->src_uuid = Uuid::From32Bit(tmp); + + BE_STREAM_TO_UINT32(tmp, p_setup); + p_bcb->dst_uuid = Uuid::From32Bit(tmp); + } else if (len == Uuid::kNumBytes128) { + p_bcb->src_uuid = Uuid::From128BitBE(p_setup); + p_setup += len; + + p_bcb->dst_uuid = Uuid::From128BitBE(p_setup); + p_setup += len; } else { - BNEP_TRACE_ERROR("BNEP - Bad UID len %d in ConnReq", p_bcb->dst_uuid.len); + BNEP_TRACE_ERROR("BNEP - Bad UID len %d in ConnReq", len); bnep_send_conn_responce(p_bcb, BNEP_SETUP_INVALID_UUID_SIZE); return; } @@ -591,16 +603,16 @@ void bnep_process_setup_conn_req(tBNEP_CONN* p_bcb, uint8_t* p_setup, p_bcb->con_flags |= BNEP_FLAGS_SETUP_RCVD; BNEP_TRACE_EVENT( - "BNEP initiating security check for incoming call for uuid 0x%x", - p_bcb->src_uuid.uu.uuid16); + "BNEP initiating security check for incoming call for uuid %s", + p_bcb->src_uuid.ToString().c_str()); #if (BNEP_DO_AUTH_FOR_ROLE_SWITCH == FALSE) if (p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED) bnep_sec_check_complete(p_bcb->rem_bda, p_bcb, BTM_SUCCESS); else #endif - btm_sec_mx_access_request( - p_bcb->rem_bda, BT_PSM_BNEP, false, BTM_SEC_PROTO_BNEP, - bnep_get_uuid32(&(p_bcb->src_uuid)), &bnep_sec_check_complete, p_bcb); + btm_sec_mx_access_request(p_bcb->rem_bda, BT_PSM_BNEP, false, + BTM_SEC_PROTO_BNEP, p_bcb->src_uuid.As32Bit(), + &bnep_sec_check_complete, p_bcb); return; } @@ -664,10 +676,8 @@ void bnep_process_setup_conn_responce(tBNEP_CONN* p_bcb, uint8_t* p_setup) { /* Restore the earlier BNEP status */ p_bcb->con_state = BNEP_STATE_CONNECTED; p_bcb->con_flags &= (~BNEP_FLAGS_SETUP_RCVD); - memcpy((uint8_t*)&(p_bcb->src_uuid), (uint8_t*)&(p_bcb->prv_src_uuid), - sizeof(tBT_UUID)); - memcpy((uint8_t*)&(p_bcb->dst_uuid), (uint8_t*)&(p_bcb->prv_dst_uuid), - sizeof(tBT_UUID)); + p_bcb->src_uuid = p_bcb->prv_src_uuid; + p_bcb->dst_uuid = p_bcb->prv_dst_uuid; /* Ensure timer is stopped */ alarm_cancel(p_bcb->conn_timer); @@ -1116,10 +1126,8 @@ void bnep_sec_check_complete(UNUSED_ATTR const RawAddress* bd_addr, BNEP_SECURITY_FAIL, is_role_change); p_bcb->con_state = BNEP_STATE_CONNECTED; - memcpy((uint8_t*)&(p_bcb->src_uuid), (uint8_t*)&(p_bcb->prv_src_uuid), - sizeof(tBT_UUID)); - memcpy((uint8_t*)&(p_bcb->dst_uuid), (uint8_t*)&(p_bcb->prv_dst_uuid), - sizeof(tBT_UUID)); + p_bcb->src_uuid = p_bcb->prv_src_uuid; + p_bcb->dst_uuid = p_bcb->prv_dst_uuid; return; } @@ -1152,10 +1160,8 @@ void bnep_sec_check_complete(UNUSED_ATTR const RawAddress* bd_addr, * state */ p_bcb->con_state = BNEP_STATE_CONNECTED; p_bcb->con_flags &= (~BNEP_FLAGS_SETUP_RCVD); - memcpy((uint8_t*)&(p_bcb->src_uuid), (uint8_t*)&(p_bcb->prv_src_uuid), - sizeof(tBT_UUID)); - memcpy((uint8_t*)&(p_bcb->dst_uuid), (uint8_t*)&(p_bcb->prv_dst_uuid), - sizeof(tBT_UUID)); + p_bcb->src_uuid = p_bcb->prv_src_uuid; + p_bcb->dst_uuid = p_bcb->prv_dst_uuid; return; } @@ -1167,8 +1173,8 @@ void bnep_sec_check_complete(UNUSED_ATTR const RawAddress* bd_addr, if (bnep_cb.p_conn_ind_cb) { p_bcb->con_state = BNEP_STATE_CONN_SETUP; - (*bnep_cb.p_conn_ind_cb)(p_bcb->handle, p_bcb->rem_bda, &p_bcb->dst_uuid, - &p_bcb->src_uuid, is_role_change); + (*bnep_cb.p_conn_ind_cb)(p_bcb->handle, p_bcb->rem_bda, p_bcb->dst_uuid, + p_bcb->src_uuid, is_role_change); } else { /* Profile didn't register connection indication call back */ bnep_send_conn_responce(p_bcb, resp_code); @@ -1256,26 +1262,3 @@ tBNEP_RESULT bnep_is_packet_allowed(tBNEP_CONN* p_bcb, return BNEP_SUCCESS; } - -/******************************************************************************* - * - * Function bnep_get_uuid32 - * - * Description This function returns the 32-bit equivalent of the UUID - * - * Returns uint32_t - 32-bit equivalent of the UUID - * - ******************************************************************************/ -uint32_t bnep_get_uuid32(tBT_UUID* src_uuid) { - uint32_t result; - - if (src_uuid->len == 2) - return ((uint32_t)src_uuid->uu.uuid16); - else if (src_uuid->len == 4) - return (src_uuid->uu.uuid32 & 0x0000FFFF); - else { - result = src_uuid->uu.uuid128[2]; - result = (result << 8) | (src_uuid->uu.uuid128[3]); - return result; - } -} diff --git a/stack/btm/btm_ble_adv_filter.cc b/stack/btm/btm_ble_adv_filter.cc index eb3fc4df0..bcca3c870 100644 --- a/stack/btm/btm_ble_adv_filter.cc +++ b/stack/btm/btm_ble_adv_filter.cc @@ -34,6 +34,8 @@ #include "hcidefs.h" #include "hcimsgs.h" +using bluetooth::Uuid; + #define BTM_BLE_ADV_FILT_META_HDR_LENGTH 3 #define BTM_BLE_ADV_FILT_FEAT_SELN_LEN 13 #define BTM_BLE_ADV_FILT_TRACK_NUM 2 @@ -537,7 +539,8 @@ void BTM_LE_PF_addr_filter(tBTM_BLE_SCAN_COND_OP action, */ void BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action, tBTM_BLE_PF_FILT_INDEX filt_index, - tBTM_BLE_PF_COND_TYPE filter_type, tBT_UUID uuid, + tBTM_BLE_PF_COND_TYPE filter_type, + const bluetooth::Uuid& uuid, tBTM_BLE_PF_LOGIC_TYPE cond_logic, tBTM_BLE_PF_COND_MASK* p_uuid_mask, tBTM_BLE_PF_CFG_CBACK cb) { @@ -559,36 +562,38 @@ void BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action, UINT8_TO_STREAM(p, action); UINT8_TO_STREAM(p, filt_index); + uint8_t uuid_len = uuid.GetShortestRepresentationSize(); if (action != BTM_BLE_SCAN_COND_CLEAR) { - if (uuid.len == LEN_UUID_16) { - UINT16_TO_STREAM(p, uuid.uu.uuid16); - len += LEN_UUID_16; - } else if (uuid.len == LEN_UUID_32) { - UINT32_TO_STREAM(p, uuid.uu.uuid32); - len += LEN_UUID_32; - } else if (uuid.len == LEN_UUID_128) { - ARRAY_TO_STREAM(p, uuid.uu.uuid128, LEN_UUID_128); - len += LEN_UUID_128; + if (uuid_len == Uuid::kNumBytes16) { + UINT16_TO_STREAM(p, uuid.As16Bit()); + len += Uuid::kNumBytes16; + } else if (uuid_len == Uuid::kNumBytes32) { + UINT32_TO_STREAM(p, uuid.As32Bit()); + len += Uuid::kNumBytes32; + } else if (uuid_len == Uuid::kNumBytes128) { + const auto& tmp = uuid.To128BitLE(); + ARRAY_TO_STREAM(p, tmp.data(), (int)Uuid::kNumBytes128); + len += Uuid::kNumBytes128; } else { - BTM_TRACE_ERROR("illegal UUID length: %d", uuid.len); + BTM_TRACE_ERROR("illegal UUID length: %d", uuid_len); cb.Run(0, BTM_BLE_PF_CONFIG, 1 /*BTA_FAILURE*/); return; } if (p_uuid_mask) { - if (uuid.len == LEN_UUID_16) { + if (uuid_len == Uuid::kNumBytes16) { UINT16_TO_STREAM(p, p_uuid_mask->uuid16_mask); - len += LEN_UUID_16; - } else if (uuid.len == LEN_UUID_32) { + len += Uuid::kNumBytes16; + } else if (uuid_len == Uuid::kNumBytes32) { UINT32_TO_STREAM(p, p_uuid_mask->uuid32_mask); - len += LEN_UUID_32; - } else if (uuid.len == LEN_UUID_128) { - ARRAY_TO_STREAM(p, p_uuid_mask->uuid128_mask, LEN_UUID_128); - len += LEN_UUID_128; + len += Uuid::kNumBytes32; + } else if (uuid_len == Uuid::kNumBytes128) { + ARRAY_TO_STREAM(p, p_uuid_mask->uuid128_mask, (int)Uuid::kNumBytes128); + len += Uuid::kNumBytes128; } } else { - memset(p, 0xff, uuid.len); - len += uuid.len; + memset(p, 0xff, uuid_len); + len += uuid_len; } } diff --git a/stack/btm/btm_inq.cc b/stack/btm/btm_inq.cc index 213683c7a..df5eac272 100644 --- a/stack/btm/btm_inq.cc +++ b/stack/btm/btm_inq.cc @@ -43,6 +43,8 @@ #include "hcidefs.h" #include "hcimsgs.h" +using bluetooth::Uuid; + /* 3 second timeout waiting for responses */ #define BTM_INQ_REPLY_TIMEOUT_MS (3 * 1000) @@ -121,8 +123,6 @@ static const uint8_t* btm_eir_get_uuid_list(uint8_t* p_eir, size_t eir_len, uint8_t uuid_size, uint8_t* p_num_uuid, uint8_t* p_uuid_list_type); -static uint16_t btm_convert_uuid_to_uuid16(const uint8_t* p_uuid, - uint8_t uuid_size); /******************************************************************************* * @@ -2433,7 +2433,8 @@ uint8_t BTM_GetEirSupportedServices(uint32_t* p_eir_uuid, uint8_t** p, * * Parameters p_eir - EIR * eir_len - EIR len - * uuid_size - LEN_UUID_16, LEN_UUID_32, LEN_UUID_128 + * uuid_size - Uuid::kNumBytes16, Uuid::kNumBytes32, + * Uuid::kNumBytes128 * p_num_uuid - return number of UUID in found list * p_uuid_list - return UUID list * max_num_uuid - maximum number of UUID to be returned @@ -2455,7 +2456,7 @@ uint8_t BTM_GetEirUuidList(uint8_t* p_eir, size_t eir_len, uint8_t uuid_size, uint8_t yy, xx; uint16_t* p_uuid16 = (uint16_t*)p_uuid_list; uint32_t* p_uuid32 = (uint32_t*)p_uuid_list; - char buff[LEN_UUID_128 * 2 + 1]; + char buff[Uuid::kNumBytes128 * 2 + 1]; p_uuid_data = btm_eir_get_uuid_list(p_eir, eir_len, uuid_size, p_num_uuid, &type); @@ -2472,22 +2473,22 @@ uint8_t BTM_GetEirUuidList(uint8_t* p_eir, size_t eir_len, uint8_t uuid_size, BTM_TRACE_DEBUG("%s: type = %02X, number of uuid = %d", __func__, type, *p_num_uuid); - if (uuid_size == LEN_UUID_16) { + if (uuid_size == Uuid::kNumBytes16) { for (yy = 0; yy < *p_num_uuid; yy++) { STREAM_TO_UINT16(*(p_uuid16 + yy), p_uuid_data); BTM_TRACE_DEBUG(" 0x%04X", *(p_uuid16 + yy)); } - } else if (uuid_size == LEN_UUID_32) { + } else if (uuid_size == Uuid::kNumBytes32) { for (yy = 0; yy < *p_num_uuid; yy++) { STREAM_TO_UINT32(*(p_uuid32 + yy), p_uuid_data); BTM_TRACE_DEBUG(" 0x%08X", *(p_uuid32 + yy)); } - } else if (uuid_size == LEN_UUID_128) { + } else if (uuid_size == Uuid::kNumBytes128) { for (yy = 0; yy < *p_num_uuid; yy++) { - STREAM_TO_ARRAY16(p_uuid_list + yy * LEN_UUID_128, p_uuid_data); - for (xx = 0; xx < LEN_UUID_128; xx++) + STREAM_TO_ARRAY16(p_uuid_list + yy * Uuid::kNumBytes128, p_uuid_data); + for (xx = 0; xx < Uuid::kNumBytes128; xx++) snprintf(buff + xx * 2, sizeof(buff) - xx * 2, "%02X", - *(p_uuid_list + yy * LEN_UUID_128 + xx)); + *(p_uuid_list + yy * Uuid::kNumBytes128 + xx)); BTM_TRACE_DEBUG(" 0x%s", buff); } } @@ -2520,15 +2521,15 @@ static const uint8_t* btm_eir_get_uuid_list(uint8_t* p_eir, size_t eir_len, uint8_t uuid_len; switch (uuid_size) { - case LEN_UUID_16: + case Uuid::kNumBytes16: complete_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE; more_type = BTM_EIR_MORE_16BITS_UUID_TYPE; break; - case LEN_UUID_32: + case Uuid::kNumBytes32: complete_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE; more_type = BTM_EIR_MORE_32BITS_UUID_TYPE; break; - case LEN_UUID_128: + case Uuid::kNumBytes128: complete_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE; more_type = BTM_EIR_MORE_128BITS_UUID_TYPE; break; @@ -2567,7 +2568,7 @@ static const uint8_t* btm_eir_get_uuid_list(uint8_t* p_eir, size_t eir_len, ******************************************************************************/ static uint16_t btm_convert_uuid_to_uuid16(const uint8_t* p_uuid, uint8_t uuid_size) { - static const uint8_t base_uuid[LEN_UUID_128] = { + static const uint8_t base_uuid[Uuid::kNumBytes128] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; uint16_t uuid16 = 0; @@ -2576,26 +2577,26 @@ static uint16_t btm_convert_uuid_to_uuid16(const uint8_t* p_uuid, uint8_t xx; switch (uuid_size) { - case LEN_UUID_16: + case Uuid::kNumBytes16: STREAM_TO_UINT16(uuid16, p_uuid); break; - case LEN_UUID_32: + case Uuid::kNumBytes32: STREAM_TO_UINT32(uuid32, p_uuid); if (uuid32 < 0x10000) uuid16 = (uint16_t)uuid32; break; - case LEN_UUID_128: + case Uuid::kNumBytes128: /* See if we can compress his UUID down to 16 or 32bit UUIDs */ is_base_uuid = true; - for (xx = 0; xx < LEN_UUID_128 - 4; xx++) { + for (xx = 0; xx < Uuid::kNumBytes128 - 4; xx++) { if (p_uuid[xx] != base_uuid[xx]) { is_base_uuid = false; break; } } if (is_base_uuid) { - if ((p_uuid[LEN_UUID_128 - 1] == 0) && - (p_uuid[LEN_UUID_128 - 2] == 0)) { - p_uuid += (LEN_UUID_128 - 4); + if ((p_uuid[Uuid::kNumBytes128 - 1] == 0) && + (p_uuid[Uuid::kNumBytes128 - 2] == 0)) { + p_uuid += (Uuid::kNumBytes128 - 4); STREAM_TO_UINT16(uuid16, p_uuid); } } @@ -2629,7 +2630,7 @@ void btm_set_eir_uuid(uint8_t* p_eir, tBTM_INQ_RESULTS* p_results) { uint8_t type = BTM_EIR_MORE_16BITS_UUID_TYPE; p_uuid_data = btm_eir_get_uuid_list(p_eir, HCI_EXT_INQ_RESPONSE_LEN, - LEN_UUID_16, &num_uuid, &type); + Uuid::kNumBytes16, &num_uuid, &type); if (type == BTM_EIR_COMPLETE_16BITS_UUID_TYPE) { p_results->eir_complete_list = true; @@ -2648,21 +2649,21 @@ void btm_set_eir_uuid(uint8_t* p_eir, tBTM_INQ_RESULTS* p_results) { } p_uuid_data = btm_eir_get_uuid_list(p_eir, HCI_EXT_INQ_RESPONSE_LEN, - LEN_UUID_32, &num_uuid, &type); + Uuid::kNumBytes32, &num_uuid, &type); if (p_uuid_data) { for (yy = 0; yy < num_uuid; yy++) { - uuid16 = btm_convert_uuid_to_uuid16(p_uuid_data, LEN_UUID_32); - p_uuid_data += LEN_UUID_32; + uuid16 = btm_convert_uuid_to_uuid16(p_uuid_data, Uuid::kNumBytes32); + p_uuid_data += Uuid::kNumBytes32; if (uuid16) BTM_AddEirService(p_results->eir_uuid, uuid16); } } p_uuid_data = btm_eir_get_uuid_list(p_eir, HCI_EXT_INQ_RESPONSE_LEN, - LEN_UUID_128, &num_uuid, &type); + Uuid::kNumBytes128, &num_uuid, &type); if (p_uuid_data) { for (yy = 0; yy < num_uuid; yy++) { - uuid16 = btm_convert_uuid_to_uuid16(p_uuid_data, LEN_UUID_128); - p_uuid_data += LEN_UUID_128; + uuid16 = btm_convert_uuid_to_uuid16(p_uuid_data, Uuid::kNumBytes128); + p_uuid_data += Uuid::kNumBytes128; if (uuid16) BTM_AddEirService(p_results->eir_uuid, uuid16); } } diff --git a/stack/gap/gap_ble.cc b/stack/gap/gap_ble.cc index 2247a144c..62f12aa9b 100644 --- a/stack/gap/gap_ble.cc +++ b/stack/gap/gap_ble.cc @@ -26,6 +26,7 @@ #include "gatt_api.h" using base::StringPrintf; +using bluetooth::Uuid; namespace { @@ -246,8 +247,7 @@ bool send_cl_read_request(tGAP_CLCB& clcb) { tGATT_READ_PARAM param; memset(¶m, 0, sizeof(tGATT_READ_PARAM)); - param.service.uuid.len = LEN_UUID_16; - param.service.uuid.uu.uuid16 = uuid; + param.service.uuid = Uuid::From16Bit(uuid); param.service.s_handle = 1; param.service.e_handle = 0xFFFF; param.service.auth_req = 0; @@ -393,23 +393,22 @@ bool accept_client_operation(const RawAddress& peer_bda, uint16_t uuid, * ******************************************************************************/ void gap_attr_db_init(void) { - tBT_UUID app_uuid = {LEN_UUID_128, {0}}; uint16_t service_handle; /* Fill our internal UUID with a fixed pattern 0x82 */ - memset(&app_uuid.uu.uuid128, 0x82, LEN_UUID_128); + std::array tmp; + tmp.fill(0x82); + Uuid app_uuid = Uuid::From128BitBE(tmp); gatt_attr.fill({}); - gatt_if = GATT_Register(&app_uuid, &gap_cback); + gatt_if = GATT_Register(app_uuid, &gap_cback); GATT_StartIf(gatt_if); - bt_uuid_t svc_uuid, name_uuid, icon_uuid, pref_uuid, addr_res_uuid; - uuid_128_from_16(&svc_uuid, UUID_SERVCLASS_GAP_SERVER); - uuid_128_from_16(&name_uuid, GATT_UUID_GAP_DEVICE_NAME); - uuid_128_from_16(&icon_uuid, GATT_UUID_GAP_ICON); - uuid_128_from_16(&pref_uuid, GATT_UUID_GAP_PREF_CONN_PARAM); - uuid_128_from_16(&addr_res_uuid, GATT_UUID_GAP_CENTRAL_ADDR_RESOL); + Uuid svc_uuid = Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER); + Uuid name_uuid = Uuid::From16Bit(GATT_UUID_GAP_DEVICE_NAME); + Uuid icon_uuid = Uuid::From16Bit(GATT_UUID_GAP_ICON); + Uuid addr_res_uuid = Uuid::From16Bit(GATT_UUID_GAP_CENTRAL_ADDR_RESOL); btgatt_db_element_t service[] = { {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = svc_uuid}, @@ -428,7 +427,7 @@ void gap_attr_db_init(void) { #if (BTM_PERIPHERAL_ENABLED == TRUE) /* Only needed for peripheral testing */ , {.type = BTGATT_DB_CHARACTERISTIC, - .uuid = pref_uuid, + .uuid = Uuid::From16Bit(GATT_UUID_GAP_PREF_CONN_PARAM), .properties = GATT_CHAR_PROP_BIT_READ, .permissions = GATT_PERM_READ} #endif diff --git a/stack/gatt/att_protocol.cc b/stack/gatt/att_protocol.cc index ce809d04e..73763f1c5 100644 --- a/stack/gatt/att_protocol.cc +++ b/stack/gatt/att_protocol.cc @@ -32,6 +32,7 @@ #define GATT_START_END_HANDLE_SIZE 4 using base::StringPrintf; +using bluetooth::Uuid; /********************************************************************** * ATT protocl message building utility * **********************************************************************/ @@ -124,9 +125,9 @@ BT_HDR* attp_build_err_cmd(uint8_t cmd_code, uint16_t err_handle, * ******************************************************************************/ BT_HDR* attp_build_browse_cmd(uint8_t op_code, uint16_t s_hdl, uint16_t e_hdl, - tBT_UUID uuid) { + const bluetooth::Uuid& uuid) { const size_t payload_size = - (GATT_OP_CODE_SIZE) + (GATT_START_END_HANDLE_SIZE) + (LEN_UUID_128); + (GATT_OP_CODE_SIZE) + (GATT_START_END_HANDLE_SIZE) + (Uuid::kNumBytes128); BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET); diff --git a/stack/gatt/gatt_api.cc b/stack/gatt/gatt_api.cc index 1ee014d6a..fd301df5e 100644 --- a/stack/gatt/gatt_api.cc +++ b/stack/gatt/gatt_api.cc @@ -23,6 +23,7 @@ ******************************************************************************/ #include "bt_target.h" +#include #include #include #include @@ -34,6 +35,7 @@ #include "l2c_api.h" using base::StringPrintf; +using bluetooth::Uuid; /** * Add an service handle range to the list in decending order of the start @@ -95,53 +97,6 @@ bool GATTS_NVRegister(tGATT_APPL_INFO* p_cb_info) { return status; } -static uint8_t BASE_UUID[16] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -static int uuidType(unsigned char* p_uuid) { - if (memcmp(p_uuid, BASE_UUID, 12) != 0) return LEN_UUID_128; - if (memcmp(p_uuid + 14, BASE_UUID + 14, 2) != 0) return LEN_UUID_32; - - return LEN_UUID_16; -} - -/******************************************************************************* - * BTIF -> BTA conversion functions - ******************************************************************************/ - -static void btif_to_bta_uuid(tBT_UUID* p_dest, bt_uuid_t* p_src) { - char* p_byte = (char*)p_src; - int i = 0; - - p_dest->len = uuidType(p_src->uu); - - switch (p_dest->len) { - case LEN_UUID_16: - p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12]; - break; - - case LEN_UUID_32: - p_dest->uu.uuid32 = (p_src->uu[15] << 24) + (p_src->uu[14] << 16) + - (p_src->uu[13] << 8) + p_src->uu[12]; - break; - - case LEN_UUID_128: - for (i = 0; i != 16; ++i) p_dest->uu.uuid128[i] = p_byte[i]; - break; - - default: - LOG(ERROR) << __func__ << ": Unknown UUID length %d!" << +p_dest->len; - break; - } -} - -void uuid_128_from_16(bt_uuid_t* uuid, uint16_t uuid16) { - memcpy(uuid, &BASE_UUID, sizeof(bt_uuid_t)); - - uuid->uu[13] = (uint8_t)((0xFF00 & uuid16) >> 8); - uuid->uu[12] = (uint8_t)(0x00FF & uuid16); -} - static uint16_t compute_service_size(btgatt_db_element_t* service, int count) { int db_size = 0; btgatt_db_element_t* el = service; @@ -160,11 +115,11 @@ static uint16_t compute_service_size(btgatt_db_element_t* service, int count) { return db_size; } -static bool is_gatt_attr_type(const tBT_UUID& uuid) { - if (uuid.len == LEN_UUID_16 && (uuid.uu.uuid16 == GATT_UUID_PRI_SERVICE || - uuid.uu.uuid16 == GATT_UUID_SEC_SERVICE || - uuid.uu.uuid16 == GATT_UUID_INCLUDE_SERVICE || - uuid.uu.uuid16 == GATT_UUID_CHAR_DECLARE)) { +static bool is_gatt_attr_type(const Uuid& uuid) { + if (uuid == Uuid::From16Bit(GATT_UUID_PRI_SERVICE) || + uuid == Uuid::From16Bit(GATT_UUID_SEC_SERVICE) || + uuid == Uuid::From16Bit(GATT_UUID_INCLUDE_SERVICE) || + uuid == Uuid::From16Bit(GATT_UUID_CHAR_DECLARE)) { return true; } return false; @@ -198,28 +153,22 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service, uint16_t s_hdl = 0; bool save_hdl = false; tGATT_REG* p_reg = gatt_get_regcb(gatt_if); - tBT_UUID* p_app_uuid128; bool is_pri = (service->type == BTGATT_DB_PRIMARY_SERVICE) ? true : false; - tBT_UUID svc_uuid; - btif_to_bta_uuid(&svc_uuid, &service->uuid); + Uuid svc_uuid = service->uuid; LOG(INFO) << __func__; - if (p_reg == NULL) { + if (!p_reg) { LOG(ERROR) << "Inavlid gatt_if=" << +gatt_if; return GATT_INTERNAL_ERROR; } - p_app_uuid128 = &p_reg->app_uuid128; - uint16_t num_handles = compute_service_size(service, count); - if ((svc_uuid.len == LEN_UUID_16) && - (svc_uuid.uu.uuid16 == UUID_SERVCLASS_GATT_SERVER)) { + if (svc_uuid == Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER)) { s_hdl = gatt_cb.hdl_cfg.gatt_start_hdl; - } else if ((svc_uuid.len == LEN_UUID_16) && - (svc_uuid.uu.uuid16 == UUID_SERVCLASS_GAP_SERVER)) { + } else if (svc_uuid == Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER)) { s_hdl = gatt_cb.hdl_cfg.gap_start_hdl; } else { if (!gatt_cb.hdl_list_info->empty()) { @@ -241,7 +190,7 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service, } tGATT_HDL_LIST_ELEM& list = gatt_add_an_item_to_list(s_hdl); - list.asgn_range.app_uuid128 = *p_app_uuid128; + list.asgn_range.app_uuid128 = p_reg->app_uuid128; list.asgn_range.svc_uuid = svc_uuid; list.asgn_range.s_handle = s_hdl; list.asgn_range.e_handle = s_hdl + num_handles - 1; @@ -252,20 +201,18 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service, (*gatt_cb.cb_info.p_nv_save_callback)(true, &list.asgn_range); } - gatts_init_service_db(list.svc_db, &svc_uuid, is_pri, s_hdl, num_handles); + gatts_init_service_db(list.svc_db, svc_uuid, is_pri, s_hdl, num_handles); VLOG(1) << StringPrintf( - "%s: handles needed:%u s_hdl=%u e_hdl=%u %s[%x] is_primary=%d", __func__, + "%s: handles needed:%u s_hdl=%u e_hdl=%u %s is_primary=%d", __func__, num_handles, list.asgn_range.s_handle, list.asgn_range.e_handle, - ((list.asgn_range.svc_uuid.len == 2) ? "uuid16" : "uuid128"), - list.asgn_range.svc_uuid.uu.uuid16, list.asgn_range.is_primary); + list.asgn_range.svc_uuid.ToString().c_str(), list.asgn_range.is_primary); service->attribute_handle = s_hdl; btgatt_db_element_t* el = service + 1; for (int i = 0; i < count - 1; i++, el++) { - tBT_UUID uuid; - btif_to_bta_uuid(&uuid, &el->uuid); + const Uuid& uuid = el->uuid; if (el->type == BTGATT_DB_CHARACTERISTIC) { /* data validity checking */ @@ -282,8 +229,8 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service, if (is_gatt_attr_type(uuid)) { LOG(ERROR) << StringPrintf( "%s: attept to add characteristic with UUID equal to GATT " - "Attribute Type 0x%04x ", - __func__, uuid.uu.uuid16); + "Attribute Type %s ", + __func__, uuid.ToString().c_str()); return GATT_INTERNAL_ERROR; } @@ -293,8 +240,8 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service, if (is_gatt_attr_type(uuid)) { LOG(ERROR) << StringPrintf( "%s: attept to add descriptor with UUID equal to GATT " - "Attribute Type 0x%04x ", - __func__, uuid.uu.uuid16); + "Attribute Type %s", + __func__, uuid.ToString().c_str()); return GATT_INTERNAL_ERROR; } @@ -333,13 +280,13 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service, elem.p_db = &list.svc_db; elem.is_primary = list.asgn_range.is_primary; - memcpy(&elem.app_uuid, &list.asgn_range.app_uuid128, sizeof(tBT_UUID)); + elem.app_uuid = list.asgn_range.app_uuid128; elem.type = list.asgn_range.is_primary ? GATT_UUID_PRI_SERVICE : GATT_UUID_SEC_SERVICE; if (elem.type == GATT_UUID_PRI_SERVICE) { - tBT_UUID* p_uuid = gatts_get_service_uuid(elem.p_db); - elem.sdp_handle = gatt_add_sdp_record(p_uuid, elem.s_hdl, elem.e_hdl); + Uuid* p_uuid = gatts_get_service_uuid(elem.p_db); + elem.sdp_handle = gatt_add_sdp_record(*p_uuid, elem.s_hdl, elem.e_hdl); } else { elem.sdp_handle = 0; } @@ -355,17 +302,14 @@ uint16_t GATTS_AddService(tGATT_IF gatt_if, btgatt_db_element_t* service, return GATT_SERVICE_STARTED; } -bool is_active_service(tBT_UUID* p_app_uuid128, tBT_UUID* p_svc_uuid, +bool is_active_service(const Uuid& app_uuid128, Uuid* p_svc_uuid, uint16_t start_handle) { for (auto& info : *gatt_cb.srv_list_info) { - tBT_UUID* p_this_uuid = gatts_get_service_uuid(info.p_db); - - if (p_this_uuid && gatt_uuid_compare(*p_app_uuid128, info.app_uuid) && - gatt_uuid_compare(*p_svc_uuid, *p_this_uuid) && - (start_handle == info.s_hdl)) { - LOG(ERROR) << "Active Service Found"; - gatt_dbg_display_uuid(*p_svc_uuid); + Uuid* p_this_uuid = gatts_get_service_uuid(info.p_db); + if (p_this_uuid && app_uuid128 == info.app_uuid && + *p_svc_uuid == *p_this_uuid && (start_handle == info.s_hdl)) { + LOG(ERROR) << "Active Service Found: " << *p_svc_uuid; return true; } } @@ -386,7 +330,7 @@ bool is_active_service(tBT_UUID* p_app_uuid128, tBT_UUID* p_svc_uuid, * was not found. * ******************************************************************************/ -bool GATTS_DeleteService(tGATT_IF gatt_if, tBT_UUID* p_svc_uuid, +bool GATTS_DeleteService(tGATT_IF gatt_if, Uuid* p_svc_uuid, uint16_t svc_inst) { VLOG(1) << __func__; @@ -396,8 +340,8 @@ bool GATTS_DeleteService(tGATT_IF gatt_if, tBT_UUID* p_svc_uuid, return false; } - tBT_UUID* p_app_uuid128 = &p_reg->app_uuid128; - auto it = gatt_find_hdl_buffer_by_app_id(p_app_uuid128, p_svc_uuid, svc_inst); + auto it = + gatt_find_hdl_buffer_by_app_id(p_reg->app_uuid128, p_svc_uuid, svc_inst); if (it == gatt_cb.hdl_list_info->end()) { LOG(ERROR) << "No Service found"; return false; @@ -405,7 +349,7 @@ bool GATTS_DeleteService(tGATT_IF gatt_if, tBT_UUID* p_svc_uuid, gatt_proc_srv_chg(); - if (is_active_service(p_app_uuid128, p_svc_uuid, svc_inst)) { + if (is_active_service(p_reg->app_uuid128, p_svc_uuid, svc_inst)) { GATTS_StopService(it->asgn_range.s_handle); } @@ -660,7 +604,6 @@ tGATT_STATUS GATTC_ConfigureMTU(uint16_t conn_id, uint16_t mtu) { (tGATT_CL_MSG*)&mtu); } - /******************************************************************************* * * Function GATTC_Discover @@ -695,7 +638,7 @@ tGATT_STATUS GATTC_Discover(uint16_t conn_id, tGATT_DISC_TYPE disc_type, if (!GATT_HANDLE_IS_VALID(p_param->s_handle) || !GATT_HANDLE_IS_VALID(p_param->e_handle) || /* search by type does not have a valid UUID param */ - (disc_type == GATT_DISC_SRVC_BY_UUID && p_param->service.len == 0)) { + (disc_type == GATT_DISC_SRVC_BY_UUID && p_param->service.IsEmpty())) { return GATT_ILLEGAL_PARAMETER; } @@ -765,7 +708,7 @@ tGATT_STATUS GATTC_Read(uint16_t conn_id, tGATT_READ_TYPE type, case GATT_READ_CHAR_VALUE: p_clcb->s_handle = p_read->service.s_handle; p_clcb->e_handle = p_read->service.e_handle; - memcpy(&p_clcb->uuid, &p_read->service.uuid, sizeof(tBT_UUID)); + p_clcb->uuid = p_read->service.uuid; break; case GATT_READ_MULTIPLE: { p_clcb->s_handle = 0; @@ -778,7 +721,7 @@ tGATT_STATUS GATTC_Read(uint16_t conn_id, tGATT_READ_TYPE type, } case GATT_READ_BY_HANDLE: case GATT_READ_PARTIAL: - memset(&p_clcb->uuid, 0, sizeof(tBT_UUID)); + p_clcb->uuid = Uuid::kEmpty; p_clcb->s_handle = p_read->by_handle.handle; if (type == GATT_READ_PARTIAL) { @@ -990,19 +933,16 @@ void GATT_SetIdleTimeout(const RawAddress& bd_addr, uint16_t idle_tout, * with GATT * ******************************************************************************/ -tGATT_IF GATT_Register(tBT_UUID* p_app_uuid128, tGATT_CBACK* p_cb_info) { +tGATT_IF GATT_Register(const Uuid& app_uuid128, tGATT_CBACK* p_cb_info) { tGATT_REG* p_reg; uint8_t i_gatt_if = 0; tGATT_IF gatt_if = 0; - LOG(INFO) << __func__; - gatt_dbg_display_uuid(*p_app_uuid128); + LOG(INFO) << __func__ << app_uuid128; for (i_gatt_if = 0, p_reg = gatt_cb.cl_rcb; i_gatt_if < GATT_MAX_APPS; i_gatt_if++, p_reg++) { - if (p_reg->in_use && - !memcmp(p_app_uuid128->uu.uuid128, p_reg->app_uuid128.uu.uuid128, - LEN_UUID_128)) { + if (p_reg->in_use && p_reg->app_uuid128 == app_uuid128) { LOG(ERROR) << "application already registered."; return 0; } @@ -1013,7 +953,7 @@ tGATT_IF GATT_Register(tBT_UUID* p_app_uuid128, tGATT_CBACK* p_cb_info) { if (!p_reg->in_use) { memset(p_reg, 0, sizeof(tGATT_REG)); i_gatt_if++; /* one based number */ - p_reg->app_uuid128 = *p_app_uuid128; + p_reg->app_uuid128 = app_uuid128; gatt_if = p_reg->gatt_if = (tGATT_IF)i_gatt_if; p_reg->app_cb = *p_cb_info; p_reg->in_use = true; @@ -1063,7 +1003,7 @@ void GATT_Deregister(tGATT_IF gatt_if) { } /* free all services db buffers if owned by this application */ - gatt_free_srvc_db_buffer_app_id(&p_reg->app_uuid128); + gatt_free_srvc_db_buffer_app_id(p_reg->app_uuid128); /* When an application deregisters, check remove the link associated with the * app */ diff --git a/stack/gatt/gatt_attr.cc b/stack/gatt/gatt_attr.cc index f4e028aab..4f3578d88 100644 --- a/stack/gatt/gatt_attr.cc +++ b/stack/gatt/gatt_attr.cc @@ -26,12 +26,12 @@ #include "bt_target.h" #include "bt_utils.h" -#include "btcore/include/uuid.h" #include "gatt_api.h" #include "gatt_int.h" #include "osi/include/osi.h" using base::StringPrintf; +using bluetooth::Uuid; #define GATTP_MAX_NUM_INC_SVR 0 #define GATTP_MAX_CHAR_NUM 2 @@ -269,21 +269,19 @@ static void gatt_connect_cback(UNUSED_ATTR tGATT_IF gatt_if, * ******************************************************************************/ void gatt_profile_db_init(void) { - tBT_UUID app_uuid = {LEN_UUID_128, {0}}; uint16_t service_handle = 0; /* Fill our internal UUID with a fixed pattern 0x81 */ - memset(&app_uuid.uu.uuid128, 0x81, LEN_UUID_128); + std::array tmp; + tmp.fill(0x81); /* Create a GATT profile service */ - gatt_cb.gatt_if = GATT_Register(&app_uuid, &gatt_profile_cback); + gatt_cb.gatt_if = GATT_Register(Uuid::From128BitBE(tmp), &gatt_profile_cback); GATT_StartIf(gatt_cb.gatt_if); - bt_uuid_t service_uuid; - uuid_128_from_16(&service_uuid, UUID_SERVCLASS_GATT_SERVER); + Uuid service_uuid = Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER); - bt_uuid_t char_uuid; - uuid_128_from_16(&char_uuid, GATT_UUID_GATT_SRV_CHGD); + Uuid char_uuid = Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD); btgatt_db_element_t service[] = { {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = service_uuid}, @@ -329,7 +327,7 @@ static void gatt_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, break; case GATT_DISC_CHAR_DSCPT: /* stage 3 */ - if (p_data->type.uu.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG) { + if (p_data->type == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG)) { p_clcb->s_handle = p_data->handle; p_clcb->ccc_result++; } @@ -398,16 +396,14 @@ static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB* p_clcb) { case GATT_SVC_CHANGED_SERVICE: /* discover GATT service */ srvc_disc_param.s_handle = 1; srvc_disc_param.e_handle = 0xffff; - srvc_disc_param.service.len = 2; - srvc_disc_param.service.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER; + srvc_disc_param.service = Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER); GATTC_Discover(p_clcb->conn_id, GATT_DISC_SRVC_BY_UUID, &srvc_disc_param); break; case GATT_SVC_CHANGED_CHARACTERISTIC: /* discover service change char */ srvc_disc_param.s_handle = 1; srvc_disc_param.e_handle = p_clcb->e_handle; - srvc_disc_param.service.len = 2; - srvc_disc_param.service.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD; + srvc_disc_param.service = Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD); GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR, &srvc_disc_param); break; diff --git a/stack/gatt/gatt_cl.cc b/stack/gatt/gatt_cl.cc index a3fcf087f..cce268cbb 100644 --- a/stack/gatt/gatt_cl.cc +++ b/stack/gatt/gatt_cl.cc @@ -43,6 +43,8 @@ #define GATT_READ_BY_TYPE_RSP_MIN_LEN 1 using base::StringPrintf; +using bluetooth::Uuid; + /******************************************************************************* * G L O B A L G A T T D A T A * ******************************************************************************/ @@ -87,27 +89,27 @@ void gatt_act_discovery(tGATT_CLCB* p_clcb) { cl_req.browse.e_handle = p_clcb->e_handle; if (disc_type_to_uuid[p_clcb->op_subtype] != 0) { - cl_req.browse.uuid.len = 2; - cl_req.browse.uuid.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype]; + cl_req.browse.uuid = + bluetooth::Uuid::From16Bit(disc_type_to_uuid[p_clcb->op_subtype]); } if (p_clcb->op_subtype == GATT_DISC_SRVC_BY_UUID) /* fill in the FindByTypeValue request info*/ { - cl_req.find_type_value.uuid.len = 2; - cl_req.find_type_value.uuid.uu.uuid16 = - disc_type_to_uuid[p_clcb->op_subtype]; + cl_req.find_type_value.uuid = + bluetooth::Uuid::From16Bit(disc_type_to_uuid[p_clcb->op_subtype]); cl_req.find_type_value.s_handle = p_clcb->s_handle; cl_req.find_type_value.e_handle = p_clcb->e_handle; - cl_req.find_type_value.value_len = p_clcb->uuid.len; - /* if service type is 32 bits UUID, convert it now */ - if (p_clcb->uuid.len == LEN_UUID_32) { - cl_req.find_type_value.value_len = LEN_UUID_128; - gatt_convert_uuid32_to_uuid128(cl_req.find_type_value.value, - p_clcb->uuid.uu.uuid32); + + size_t size = p_clcb->uuid.GetShortestRepresentationSize(); + cl_req.find_type_value.value_len = size; + if (size == Uuid::kNumBytes32) { + /* if service type is 32 bits UUID, convert it now */ + memcpy(cl_req.find_type_value.value, p_clcb->uuid.To128BitLE().data(), + Uuid::kNumBytes128); } else - memcpy(cl_req.find_type_value.value, &p_clcb->uuid.uu, - p_clcb->uuid.len); + memcpy(cl_req.find_type_value.value, p_clcb->uuid.To128BitLE().data(), + size); } st = attp_send_cl_msg(*p_clcb->p_tcb, p_clcb, op_code, &cl_req); @@ -143,10 +145,9 @@ void gatt_act_read(tGATT_CLCB* p_clcb, uint16_t offset) { msg.browse.s_handle = p_clcb->s_handle; msg.browse.e_handle = p_clcb->e_handle; if (p_clcb->op_subtype == GATT_READ_BY_TYPE) - memcpy(&msg.browse.uuid, &p_clcb->uuid, sizeof(tBT_UUID)); + msg.browse.uuid = p_clcb->uuid; else { - msg.browse.uuid.len = LEN_UUID_16; - msg.browse.uuid.uu.uuid16 = GATT_UUID_CHAR_DECLARE; + msg.browse.uuid = bluetooth::Uuid::From16Bit(GATT_UUID_CHAR_DECLARE); } break; @@ -373,15 +374,13 @@ void gatt_process_find_type_value_rsp(UNUSED_ATTR tGATT_TCB& tcb, return; memset(&result, 0, sizeof(tGATT_DISC_RES)); - result.type.len = 2; - result.type.uu.uuid16 = GATT_UUID_PRI_SERVICE; + result.type = bluetooth::Uuid::From16Bit(GATT_UUID_PRI_SERVICE); /* returns a series of handle ranges */ while (len >= 4) { STREAM_TO_UINT16(result.handle, p); STREAM_TO_UINT16(result.value.group_value.e_handle, p); - memcpy(&result.value.group_value.service_type, &p_clcb->uuid, - sizeof(tBT_UUID)); + result.value.group_value.service_type = p_clcb->uuid; len -= 4; @@ -428,9 +427,9 @@ void gatt_process_read_info_rsp(UNUSED_ATTR tGATT_TCB& tcb, tGATT_CLCB* p_clcb, len -= 1; if (type == GATT_INFO_TYPE_PAIR_16) - uuid_len = LEN_UUID_16; + uuid_len = Uuid::kNumBytes16; else if (type == GATT_INFO_TYPE_PAIR_128) - uuid_len = LEN_UUID_128; + uuid_len = Uuid::kNumBytes128; while (len >= uuid_len + 2) { STREAM_TO_UINT16(result.handle, p); @@ -438,7 +437,7 @@ void gatt_process_read_info_rsp(UNUSED_ATTR tGATT_TCB& tcb, tGATT_CLCB* p_clcb, if (uuid_len > 0) { if (!gatt_parse_uuid_from_cmd(&result.type, uuid_len, &p)) break; } else - memcpy(&result.type, &p_clcb->uuid, sizeof(tBT_UUID)); + result.type = p_clcb->uuid; len -= (uuid_len + 2); @@ -590,9 +589,10 @@ void gatt_process_notification(tGATT_TCB& tcb, uint8_t op_code, uint16_t len, tGATT_REG* p_reg; uint16_t conn_id; tGATT_STATUS encrypt_status; - uint8_t *p = p_data, i, event = (op_code == GATT_HANDLE_VALUE_NOTIF) - ? GATTC_OPTYPE_NOTIFICATION - : GATTC_OPTYPE_INDICATION; + uint8_t *p = p_data, i, + event = (op_code == GATT_HANDLE_VALUE_NOTIF) + ? GATTC_OPTYPE_NOTIFICATION + : GATTC_OPTYPE_INDICATION; VLOG(1) << __func__; @@ -718,8 +718,8 @@ void gatt_process_read_by_type_rsp(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, memset(&record_value, 0, sizeof(tGATT_DISC_VALUE)); result.handle = handle; - result.type.len = 2; - result.type.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype]; + result.type = + bluetooth::Uuid::From16Bit(disc_type_to_uuid[p_clcb->op_subtype]); /* discover all services */ if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY && @@ -752,8 +752,10 @@ void gatt_process_read_by_type_rsp(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, } if (value_len == 6) { - STREAM_TO_UINT16(record_value.incl_service.service_type.uu.uuid16, p); - record_value.incl_service.service_type.len = LEN_UUID_16; + uint16_t tmp; + STREAM_TO_UINT16(tmp, p); + record_value.incl_service.service_type = + bluetooth::Uuid::From16Bit(tmp); } else if (value_len == 4) { p_clcb->s_handle = record_value.incl_service.s_handle; p_clcb->read_uuid128.wait_for_read_rsp = true; @@ -807,10 +809,14 @@ void gatt_process_read_by_type_rsp(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, } /* UUID not matching */ - if (!gatt_uuid_compare(record_value.dclr_value.char_uuid, p_clcb->uuid)) { + if (!p_clcb->uuid.IsEmpty() && + !record_value.dclr_value.char_uuid.IsEmpty() && + record_value.dclr_value.char_uuid != p_clcb->uuid) { len -= (value_len + 2); continue; /* skip the result, and look for next one */ - } else if (p_clcb->operation == GATTC_OPTYPE_READ) + } + + if (p_clcb->operation == GATTC_OPTYPE_READ) /* UUID match for read characteristic value */ { /* only read the first matching UUID characteristic value, and @@ -906,12 +912,9 @@ void gatt_process_read_rsp(tGATT_TCB& tcb, tGATT_CLCB* p_clcb, p_clcb->read_uuid128.wait_for_read_rsp) { p_clcb->s_handle = p_clcb->read_uuid128.next_disc_start_hdl; p_clcb->read_uuid128.wait_for_read_rsp = false; - if (len == LEN_UUID_128) { - memcpy(p_clcb->read_uuid128.result.value.incl_service.service_type.uu - .uuid128, - p, len); - p_clcb->read_uuid128.result.value.incl_service.service_type.len = - LEN_UUID_128; + if (len == Uuid::kNumBytes128) { + p_clcb->read_uuid128.result.value.incl_service.service_type = + bluetooth::Uuid::From128BitLE(p); if (p_clcb->p_reg->app_cb.p_disc_res_cb) (*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, diff --git a/stack/gatt/gatt_db.cc b/stack/gatt/gatt_db.cc index ac3d95943..0ba408e09 100644 --- a/stack/gatt/gatt_db.cc +++ b/stack/gatt/gatt_db.cc @@ -35,10 +35,12 @@ #include "osi/include/osi.h" using base::StringPrintf; +using bluetooth::Uuid; + /******************************************************************************* * L O C A L F U N C T I O N P R O T O T Y P E S * ******************************************************************************/ -static tGATT_ATTR& allocate_attr_in_db(tGATT_SVC_DB& db, const tBT_UUID& uuid, +static tGATT_ATTR& allocate_attr_in_db(tGATT_SVC_DB& db, const Uuid& uuid, tGATT_PERM perm); static tGATT_STATUS gatts_send_app_read_request( tGATT_TCB& tcb, uint8_t op_code, uint16_t handle, uint16_t offset, @@ -47,8 +49,8 @@ static tGATT_STATUS gatts_send_app_read_request( /** * Initialize a memory space to be a service database. */ -void gatts_init_service_db(tGATT_SVC_DB& db, tBT_UUID* p_service, bool is_pri, - uint16_t s_hdl, uint16_t num_handle) { +void gatts_init_service_db(tGATT_SVC_DB& db, const Uuid& service_uuid, + bool is_pri, uint16_t s_hdl, uint16_t num_handle) { db.attr_list.reserve(num_handle); VLOG(1) << StringPrintf("%s: s_hdl= %d num_handle= %d", __func__, s_hdl, @@ -59,14 +61,14 @@ void gatts_init_service_db(tGATT_SVC_DB& db, tBT_UUID* p_service, bool is_pri, db.end_handle = s_hdl + num_handle; /* add service declration record */ - tBT_UUID uuid = {LEN_UUID_16, {0}}; - uuid.uu.uuid16 = is_pri ? GATT_UUID_PRI_SERVICE : GATT_UUID_SEC_SERVICE; + Uuid uuid = + Uuid::From16Bit(is_pri ? GATT_UUID_PRI_SERVICE : GATT_UUID_SEC_SERVICE); tGATT_ATTR& attr = allocate_attr_in_db(db, uuid, GATT_PERM_READ); - attr.p_value.reset((tGATT_ATTR_VALUE*)(new tBT_UUID)); - memcpy(&attr.p_value->uuid, p_service, sizeof(tBT_UUID)); + attr.p_value.reset((tGATT_ATTR_VALUE*)(new Uuid)); + attr.p_value->uuid = service_uuid; } -tBT_UUID* gatts_get_service_uuid(tGATT_SVC_DB* p_db) { +Uuid* gatts_get_service_uuid(tGATT_SVC_DB* p_db) { if (!p_db || p_db->attr_list.empty()) { LOG(ERROR) << "service DB empty"; return NULL; @@ -119,8 +121,8 @@ static tGATT_STATUS gatts_check_attr_readability(const tGATT_ATTR& attr, return GATT_INSUF_KEY_SIZE; } - if (read_long && attr.uuid.len == LEN_UUID_16) { - switch (attr.uuid.uu.uuid16) { + if (read_long && attr.uuid.Is16Bit()) { + switch (attr.uuid.As16Bit()) { case GATT_UUID_PRI_SERVICE: case GATT_UUID_SEC_SERVICE: case GATT_UUID_CHAR_DECLARE: @@ -162,75 +164,70 @@ static tGATT_STATUS read_attr_value(tGATT_ATTR& attr16, uint16_t offset, uint8_t** p_data, bool read_long, uint16_t mtu, uint16_t* p_len, tGATT_SEC_FLAG sec_flag, uint8_t key_size) { - uint16_t len = 0, uuid16 = 0; uint8_t* p = *p_data; - VLOG(1) - << __func__ - << StringPrintf( - " uuid=0x%04x perm=0x%02x sec_flag=0x%x offset=%d read_long=%d", - attr16.uuid.uu.uuid16, attr16.permission, sec_flag, offset, - read_long); + VLOG(1) << __func__ << " uuid=" << attr16.uuid + << StringPrintf(" perm=0x%02x sec_flag=0x%x offset=%d read_long=%d", + attr16.permission, sec_flag, offset, read_long); tGATT_STATUS status = gatts_check_attr_readability(attr16, offset, read_long, sec_flag, key_size); - if (status != GATT_SUCCESS) return status; - if (attr16.uuid.len == LEN_UUID_16) uuid16 = attr16.uuid.uu.uuid16; + if (!attr16.uuid.Is16Bit()) { + /* characteristic description or characteristic value */ + return GATT_PENDING; + } - status = GATT_NO_RESOURCES; + uint16_t uuid16 = attr16.uuid.As16Bit(); if (uuid16 == GATT_UUID_PRI_SERVICE || uuid16 == GATT_UUID_SEC_SERVICE) { - len = attr16.p_value->uuid.len; - if (mtu >= attr16.p_value->uuid.len) { - gatt_build_uuid_to_stream(&p, attr16.p_value->uuid); - status = GATT_SUCCESS; - } - } else if (uuid16 == GATT_UUID_CHAR_DECLARE) { + *p_len = attr16.p_value->uuid.GetShortestRepresentationSize(); + if (mtu < *p_len) return GATT_NO_RESOURCES; + + gatt_build_uuid_to_stream(&p, attr16.p_value->uuid); + return GATT_SUCCESS; + } + + if (uuid16 == GATT_UUID_CHAR_DECLARE) { tGATT_ATTR* val_attr = &attr16 + 1; - len = (val_attr->uuid.len == LEN_UUID_16) ? 5 : 19; + uint8_t val_len = val_attr->uuid.GetShortestRepresentationSize(); + *p_len = (val_len == Uuid::kNumBytes16) ? 5 : 19; - if (mtu >= len) { - UINT8_TO_STREAM(p, attr16.p_value->char_decl.property); - UINT16_TO_STREAM(p, attr16.p_value->char_decl.char_val_handle); + if (mtu < *p_len) return GATT_NO_RESOURCES; - if (val_attr->uuid.len == LEN_UUID_16) { - UINT16_TO_STREAM(p, val_attr->uuid.uu.uuid16); - } - /* convert a 32bits UUID to 128 bits */ - else if (val_attr->uuid.len == LEN_UUID_32) { - gatt_convert_uuid32_to_uuid128(p, val_attr->uuid.uu.uuid32); - p += LEN_UUID_128; - } else { - ARRAY_TO_STREAM(p, val_attr->uuid.uu.uuid128, LEN_UUID_128); - } - status = GATT_SUCCESS; + UINT8_TO_STREAM(p, attr16.p_value->char_decl.property); + UINT16_TO_STREAM(p, attr16.p_value->char_decl.char_val_handle); + + if (val_len == Uuid::kNumBytes16) { + UINT16_TO_STREAM(p, val_attr->uuid.As16Bit()); + } else { + /* if 32 bit UUID, convert to 128 bit */ + ARRAY_TO_STREAM(p, val_attr->uuid.To128BitLE(), (int)Uuid::kNumBytes128); } + return GATT_SUCCESS; + } - } else if (uuid16 == GATT_UUID_INCLUDE_SERVICE) { - if (attr16.p_value->incl_handle.service_type.len == LEN_UUID_16) - len = 6; + if (uuid16 == GATT_UUID_INCLUDE_SERVICE) { + tGATT_INCL_SRVC& incl_handle = attr16.p_value->incl_handle; + if (incl_handle.service_type.Is16Bit()) + *p_len = 6; else - len = 4; + *p_len = 4; - if (mtu >= len) { - UINT16_TO_STREAM(p, attr16.p_value->incl_handle.s_handle); - UINT16_TO_STREAM(p, attr16.p_value->incl_handle.e_handle); + if (mtu < *p_len) return GATT_NO_RESOURCES; - if (attr16.p_value->incl_handle.service_type.len == LEN_UUID_16) { - UINT16_TO_STREAM(p, attr16.p_value->incl_handle.service_type.uu.uuid16); - } - status = GATT_SUCCESS; + UINT16_TO_STREAM(p, incl_handle.s_handle); + UINT16_TO_STREAM(p, incl_handle.e_handle); + + if (incl_handle.service_type.Is16Bit()) { + UINT16_TO_STREAM(p, incl_handle.service_type.As16Bit()); } - } else /* characteristic description or characteristic value */ - { - status = GATT_PENDING; + return GATT_SUCCESS; } - *p_len = len; - *p_data = p; - return status; + /* characteristic description or characteristic value (again) */ + return GATT_PENDING; } /******************************************************************************* @@ -253,7 +250,7 @@ static tGATT_STATUS read_attr_value(tGATT_ATTR& attr16, uint16_t offset, ******************************************************************************/ tGATT_STATUS gatts_db_read_attr_value_by_type( tGATT_TCB& tcb, tGATT_SVC_DB* p_db, uint8_t op_code, BT_HDR* p_rsp, - uint16_t s_handle, uint16_t e_handle, tBT_UUID type, uint16_t* p_len, + uint16_t s_handle, uint16_t e_handle, const Uuid& type, uint16_t* p_len, tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id, uint16_t* p_cur_handle) { tGATT_STATUS status = GATT_NOT_FOUND; @@ -262,9 +259,7 @@ tGATT_STATUS gatts_db_read_attr_value_by_type( if (p_db) { for (tGATT_ATTR& attr : p_db->attr_list) { - tBT_UUID attr_uuid = attr.uuid; - - if (attr.handle >= s_handle && gatt_uuid_compare(type, attr_uuid)) { + if (attr.handle >= s_handle && type == attr.uuid) { if (*p_len <= 2) { status = GATT_NO_RESOURCES; break; @@ -304,7 +299,7 @@ tGATT_STATUS gatts_db_read_attr_value_by_type( uint8_t flag = 0; if (BTM_GetSecurityFlags(tcb.peer_bda, &flag)) { if ((tcb.att_lcid == L2CAP_ATT_CID) && (status == GATT_PENDING) && - (type.uu.uuid16 == GATT_UUID_GAP_DEVICE_NAME)) { + (type.As16Bit() == GATT_UUID_GAP_DEVICE_NAME)) { if ((flag & (BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_FLAG_ENCRYPTED)) == BTM_SEC_LINK_KEY_KNOWN) { tACL_CONN* p = btm_bda_to_acl(tcb.peer_bda, BT_TRANSPORT_LE); @@ -328,13 +323,14 @@ tGATT_STATUS gatts_db_read_attr_value_by_type( * */ uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle, - uint16_t e_handle, tBT_UUID service) { - tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_INCLUDE_SERVICE}}; + uint16_t e_handle, const Uuid& service) { + Uuid uuid = Uuid::From16Bit(GATT_UUID_INCLUDE_SERVICE); - VLOG(1) << StringPrintf("%s: s_hdl = 0x%04x e_hdl = 0x%04x uuid = 0x%04x", - __func__, s_handle, e_handle, service.uu.uuid16); + VLOG(1) << __func__ + << StringPrintf(": s_hdl=0x%04x e_hdl=0x%04x ", s_handle, e_handle) + << "service uuid = " << service; - if (service.len == 0 || s_handle == 0 || e_handle == 0) { + if (service.IsEmpty() || s_handle == 0 || e_handle == 0) { LOG(ERROR) << __func__ << ": Illegal Params."; return 0; } @@ -344,7 +340,7 @@ uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle, attr.p_value.reset((tGATT_ATTR_VALUE*)(new tGATT_INCL_SRVC)); attr.p_value->incl_handle.s_handle = s_handle; attr.p_value->incl_handle.e_handle = e_handle; - memcpy(&attr.p_value->incl_handle.service_type, &service, sizeof(tBT_UUID)); + attr.p_value->incl_handle.service_type = service; return attr.handle; } @@ -366,8 +362,8 @@ uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle, ******************************************************************************/ uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm, tGATT_CHAR_PROP property, - tBT_UUID& char_uuid) { - tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_CHAR_DECLARE}}; + const Uuid& char_uuid) { + Uuid uuid = Uuid::From16Bit(GATT_UUID_CHAR_DECLARE); VLOG(1) << StringPrintf("%s: perm=0x%0x property=0x%0x", __func__, perm, property); @@ -384,46 +380,6 @@ uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm, /******************************************************************************* * - * Function gatt_convertchar_descr_type - * - * Description Convert a char descript UUID into descriptor type. - * - * Returns descriptor type. - * - ******************************************************************************/ -uint8_t gatt_convertchar_descr_type(tBT_UUID* p_descr_uuid) { - tBT_UUID std_descr = {LEN_UUID_16, {GATT_UUID_CHAR_EXT_PROP}}; - - if (gatt_uuid_compare(std_descr, *p_descr_uuid)) - return GATT_DESCR_EXT_DSCPTOR; - - std_descr.uu.uuid16++; - if (gatt_uuid_compare(std_descr, *p_descr_uuid)) - return GATT_DESCR_USER_DSCPTOR; - - std_descr.uu.uuid16++; - if (gatt_uuid_compare(std_descr, *p_descr_uuid)) return GATT_DESCR_CLT_CONFIG; - - std_descr.uu.uuid16++; - if (gatt_uuid_compare(std_descr, *p_descr_uuid)) return GATT_DESCR_SVR_CONFIG; - - std_descr.uu.uuid16++; - if (gatt_uuid_compare(std_descr, *p_descr_uuid)) - return GATT_DESCR_PRES_FORMAT; - - std_descr.uu.uuid16++; - if (gatt_uuid_compare(std_descr, *p_descr_uuid)) - return GATT_DESCR_AGGR_FORMAT; - - std_descr.uu.uuid16++; - if (gatt_uuid_compare(std_descr, *p_descr_uuid)) - return GATT_DESCR_VALID_RANGE; - - return GATT_DESCR_UNKNOWN; -} - -/******************************************************************************* - * * Function gatts_add_char_descr * * Description This function add a characteristics descriptor. @@ -437,9 +393,9 @@ uint8_t gatt_convertchar_descr_type(tBT_UUID* p_descr_uuid) { * ******************************************************************************/ uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm, - tBT_UUID& descr_uuid) { - VLOG(1) << StringPrintf("gatts_add_char_descr uuid=0x%04x", - descr_uuid.uu.uuid16); + const Uuid& descr_uuid) { + VLOG(1) << StringPrintf("gatts_add_char_descr uuid=%s", + descr_uuid.ToString().c_str()); /* Add characteristic descriptors */ tGATT_ATTR& char_dscptr = allocate_attr_in_db(db, descr_uuid, perm); @@ -626,13 +582,14 @@ tGATT_STATUS gatts_write_attr_perm_check(tGATT_SVC_DB* p_db, uint8_t op_code, status = GATT_INSUF_AUTHENTICATION; LOG(ERROR) << __func__ << ": GATT_INSUF_AUTHENTICATION: LE security mode 2 required"; - } else /* writable: must be char value declaration or char descritpors - */ + } else /* writable: must be char value declaration or char descritpors */ { uint16_t max_size = 0; - if (p_attr->uuid.len == LEN_UUID_16) { - switch (p_attr->uuid.uu.uuid16) { + if (p_attr->uuid.IsEmpty()) { + status = GATT_INVALID_PDU; + } else if (p_attr->uuid.Is16Bit()) { + switch (p_attr->uuid.As16Bit()) { case GATT_UUID_CHAR_PRESENT_FORMAT: /* should be readable only */ case GATT_UUID_CHAR_EXT_PROP: /* should be readable only */ case GATT_UUID_CHAR_AGG_FORMAT: /* should be readable only */ @@ -650,31 +607,28 @@ tGATT_STATUS gatts_write_attr_perm_check(tGATT_SVC_DB* p_db, uint8_t op_code, status = GATT_SUCCESS; break; } - } else if (p_attr->uuid.len == LEN_UUID_128 || - p_attr->uuid.len == LEN_UUID_32) { + } else { // 32 or 128 bit UUID status = GATT_SUCCESS; - } else { - status = GATT_INVALID_PDU; } if (p_data == NULL && len > 0) { - status = GATT_INVALID_PDU; + return GATT_INVALID_PDU; } + /* these attribute does not allow write blob */ - else if ((p_attr->uuid.len == LEN_UUID_16) && - (p_attr->uuid.uu.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG || - p_attr->uuid.uu.uuid16 == GATT_UUID_CHAR_SRVR_CONFIG)) { - if (op_code == GATT_REQ_PREPARE_WRITE && - offset != 0) /* does not allow write blob */ - { + if (p_attr->uuid.Is16Bit() && + (p_attr->uuid.As16Bit() == GATT_UUID_CHAR_CLIENT_CONFIG || + p_attr->uuid.As16Bit() == GATT_UUID_CHAR_SRVR_CONFIG)) { + if (op_code == GATT_REQ_PREPARE_WRITE && offset != 0) { + /* does not allow write blob */ status = GATT_NOT_LONG; LOG(ERROR) << __func__ << ": GATT_NOT_LONG"; - } else if (len != max_size) /* data does not match the required format */ - { + } else if (len != max_size) { + /* data does not match the required format */ status = GATT_INVALID_ATTR_LEN; LOG(ERROR) << __func__ << ": GATT_INVALID_PDU"; } else { - status = GATT_SUCCESS; + return GATT_SUCCESS; } } } @@ -682,26 +636,6 @@ tGATT_STATUS gatts_write_attr_perm_check(tGATT_SVC_DB* p_db, uint8_t op_code, return status; } -static void uuid_to_str(const tBT_UUID bt_uuid, char* str_buf, size_t buf_len) { - if (bt_uuid.len == LEN_UUID_16) { - snprintf(str_buf, buf_len, "0x%04x", bt_uuid.uu.uuid16); - } else if (bt_uuid.len == LEN_UUID_32) { - snprintf(str_buf, buf_len, "0x%08x", bt_uuid.uu.uuid32); - } else if (bt_uuid.len == LEN_UUID_128) { - int x = snprintf(str_buf, buf_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-", - bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14], - bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12], - bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10], - bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]); - snprintf(&str_buf[x], buf_len - x, "%02x%02x-%02x%02x%02x%02x%02x%02x", - bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6], - bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4], - bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2], - bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]); - } else - snprintf(str_buf, buf_len, "Unknown (len=%d)", bt_uuid.len); -} - /** * Description Allocate a memory space for a new attribute, and link this * attribute into the database attribute list. @@ -713,7 +647,7 @@ static void uuid_to_str(const tBT_UUID bt_uuid, char* str_buf, size_t buf_len) { * Returns pointer to the newly allocated attribute. * */ -static tGATT_ATTR& allocate_attr_in_db(tGATT_SVC_DB& db, const tBT_UUID& uuid, +static tGATT_ATTR& allocate_attr_in_db(tGATT_SVC_DB& db, const Uuid& uuid, tGATT_PERM perm) { if (db.next_handle >= db.end_handle) { LOG(FATAL) << __func__ @@ -726,10 +660,6 @@ static tGATT_ATTR& allocate_attr_in_db(tGATT_SVC_DB& db, const tBT_UUID& uuid, attr.handle = db.next_handle++; attr.uuid = uuid; attr.permission = perm; - - char uuid_str[37]; - uuid_to_str(attr.uuid, uuid_str, sizeof(uuid_str)); - return attr; } diff --git a/stack/gatt/gatt_int.h b/stack/gatt/gatt_int.h index 4274e710d..4e61d3559 100644 --- a/stack/gatt/gatt_int.h +++ b/stack/gatt/gatt_int.h @@ -101,7 +101,7 @@ typedef uint8_t tGATT_SEC_FLAG; /* GATT client FIND_TYPE_VALUE_Request data */ typedef struct { - tBT_UUID uuid; /* type of attribute to be found */ + bluetooth::Uuid uuid; /* type of attribute to be found */ uint16_t s_handle; /* starting handle */ uint16_t e_handle; /* ending handle */ uint16_t value_len; /* length of the attribute value */ @@ -152,7 +152,7 @@ typedef struct { /* attribute value maintained in the server database */ typedef union { - tBT_UUID uuid; /* service declaration */ + bluetooth::Uuid uuid; /* service declaration */ tGATT_CHAR_DECL char_decl; /* characteristic declaration */ tGATT_INCL_SRVC incl_handle; /* included service */ } tGATT_ATTR_VALUE; @@ -170,7 +170,7 @@ typedef struct { std::unique_ptr p_value; tGATT_PERM permission; uint16_t handle; - tBT_UUID uuid; + bluetooth::Uuid uuid; bt_gatt_db_attribute_type_t gatt_type; } tGATT_ATTR; @@ -188,7 +188,7 @@ typedef struct { /* attribute handle, service UUID and a set of GATT server callback. */ typedef struct { - tBT_UUID app_uuid128; + bluetooth::Uuid app_uuid128; tGATT_CBACK app_cb; tGATT_IF gatt_if; /* one based */ bool in_use; @@ -254,7 +254,7 @@ typedef struct hdl_list_elem { /* attribute handle, service UUID and a set of GATT server callback. */ typedef struct { tGATT_SVC_DB* p_db; /* pointer to the service database */ - tBT_UUID app_uuid; /* applicatino UUID */ + bluetooth::Uuid app_uuid; /* application UUID */ uint32_t sdp_handle; /* primamry service SDP handle */ uint16_t type; /* service type UUID, primary or secondary */ uint16_t s_hdl; /* service starting handle */ @@ -308,7 +308,7 @@ struct tGATT_CLCB { tGATT_REG* p_reg; /* owner of this CLCB */ uint8_t sccb_idx; uint8_t* p_attr_buf; /* attribute buffer for read multiple, prepare write */ - tBT_UUID uuid; + bluetooth::Uuid uuid; uint16_t conn_id; /* connection handle */ uint16_t s_handle; /* starting handle of the active request */ uint16_t e_handle; /* ending handle of the active request */ @@ -432,14 +432,12 @@ extern tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB& tcb, BT_HDR* p_toL2CAP); /* utility functions */ extern uint8_t* gatt_dbg_op_name(uint8_t op_code); -extern uint32_t gatt_add_sdp_record(tBT_UUID* p_uuid, uint16_t start_hdl, - uint16_t end_hdl); -extern bool gatt_parse_uuid_from_cmd(tBT_UUID* p_uuid, uint16_t len, +extern uint32_t gatt_add_sdp_record(const bluetooth::Uuid& uuid, + uint16_t start_hdl, uint16_t end_hdl); +extern bool gatt_parse_uuid_from_cmd(bluetooth::Uuid* p_uuid, uint16_t len, uint8_t** p_data); -extern uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst, tBT_UUID uuid); -extern bool gatt_uuid_compare(tBT_UUID src, tBT_UUID tar); -extern void gatt_convert_uuid32_to_uuid128(uint8_t uuid_128[LEN_UUID_128], - uint32_t uuid_32); +extern uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst, + const bluetooth::Uuid& uuid); extern void gatt_sr_get_sec_info(const RawAddress& rem_bda, tBT_TRANSPORT transport, uint8_t* p_sec_flag, uint8_t* p_key_size); @@ -452,7 +450,6 @@ extern void gatt_start_ind_ack_timer(tGATT_TCB& tcb); extern tGATT_STATUS gatt_send_error_rsp(tGATT_TCB& tcb, uint8_t err_code, uint8_t op_code, uint16_t handle, bool deq); -extern void gatt_dbg_display_uuid(tBT_UUID bt_uuid); extern bool gatt_is_srv_chg_ind_pending(tGATT_TCB* p_tcb); extern tGATTS_SRV_CHG* gatt_is_bda_in_the_srv_chg_clt_list( @@ -464,12 +461,13 @@ extern bool gatt_find_the_connected_bda(uint8_t start_idx, RawAddress& bda, extern void gatt_set_srv_chg(void); extern void gatt_delete_dev_from_srv_chg_clt_list(const RawAddress& bd_addr); extern tGATT_VALUE* gatt_add_pending_ind(tGATT_TCB* p_tcb, tGATT_VALUE* p_ind); -extern void gatt_free_srvc_db_buffer_app_id(tBT_UUID* p_app_id); +extern void gatt_free_srvc_db_buffer_app_id(const bluetooth::Uuid& app_id); extern bool gatt_cl_send_next_cmd_inq(tGATT_TCB& tcb); /* reserved handle list */ extern std::list::iterator gatt_find_hdl_buffer_by_app_id( - tBT_UUID* p_app_uuid128, tBT_UUID* p_svc_uuid, uint16_t svc_inst); + const bluetooth::Uuid& app_uuid128, bluetooth::Uuid* p_svc_uuid, + uint16_t svc_inst); extern tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle); extern tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg); @@ -486,9 +484,6 @@ extern void gatt_deregister_bgdev_list(tGATT_IF gatt_if); /* server function */ extern std::list::iterator gatt_sr_find_i_rcb_by_handle( uint16_t handle); -extern bool gatt_sr_find_i_rcb_by_app_id(tBT_UUID* p_app_uuid128, - tBT_UUID* p_svc_uuid, - uint16_t svc_inst); extern tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if, uint32_t trans_id, uint8_t op_code, tGATT_STATUS status, @@ -559,21 +554,22 @@ extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB* p_tcb); extern void gatt_set_sec_act(tGATT_TCB* p_tcb, tGATT_SEC_ACTION sec_act); /* gatt_db.cc */ -extern void gatts_init_service_db(tGATT_SVC_DB& db, tBT_UUID* p_service, - bool is_pri, uint16_t s_hdl, - uint16_t num_handle); +extern void gatts_init_service_db(tGATT_SVC_DB& db, + const bluetooth::Uuid& service, bool is_pri, + uint16_t s_hdl, uint16_t num_handle); extern uint16_t gatts_add_included_service(tGATT_SVC_DB& db, uint16_t s_handle, - uint16_t e_handle, tBT_UUID service); + uint16_t e_handle, + const bluetooth::Uuid& service); extern uint16_t gatts_add_characteristic(tGATT_SVC_DB& db, tGATT_PERM perm, tGATT_CHAR_PROP property, - tBT_UUID& char_uuid); + const bluetooth::Uuid& char_uuid); extern uint16_t gatts_add_char_descr(tGATT_SVC_DB& db, tGATT_PERM perm, - tBT_UUID& dscp_uuid); + const bluetooth::Uuid& dscp_uuid); extern tGATT_STATUS gatts_db_read_attr_value_by_type( tGATT_TCB& tcb, tGATT_SVC_DB* p_db, uint8_t op_code, BT_HDR* p_rsp, - uint16_t s_handle, uint16_t e_handle, tBT_UUID type, uint16_t* p_len, - tGATT_SEC_FLAG sec_flag, uint8_t key_size, uint32_t trans_id, - uint16_t* p_cur_handle); + uint16_t s_handle, uint16_t e_handle, const bluetooth::Uuid& type, + uint16_t* p_len, tGATT_SEC_FLAG sec_flag, uint8_t key_size, + uint32_t trans_id, uint16_t* p_cur_handle); extern tGATT_STATUS gatts_read_attr_value_by_handle( tGATT_TCB& tcb, tGATT_SVC_DB* p_db, uint8_t op_code, uint16_t handle, uint16_t offset, uint8_t* p_value, uint16_t* p_len, uint16_t mtu, @@ -585,6 +581,6 @@ extern tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB* p_db, bool is_long, uint16_t handle, tGATT_SEC_FLAG sec_flag, uint8_t key_size); -extern tBT_UUID* gatts_get_service_uuid(tGATT_SVC_DB* p_db); +extern bluetooth::Uuid* gatts_get_service_uuid(tGATT_SVC_DB* p_db); #endif diff --git a/stack/gatt/gatt_sr.cc b/stack/gatt/gatt_sr.cc index 211e57c9b..264bc82b7 100644 --- a/stack/gatt/gatt_sr.cc +++ b/stack/gatt/gatt_sr.cc @@ -33,6 +33,8 @@ #define GATT_MTU_REQ_MIN_LEN 2 using base::StringPrintf; +using bluetooth::Uuid; + /******************************************************************************* * * Function gatt_sr_enqueue_cmd @@ -439,57 +441,59 @@ void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len, ******************************************************************************/ static tGATT_STATUS gatt_build_primary_service_rsp( BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl, - uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, tBT_UUID value) { + uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, const Uuid& value) { tGATT_STATUS status = GATT_NOT_FOUND; - uint8_t handle_len = 4, *p; - tBT_UUID* p_uuid; + uint8_t handle_len = 4; - p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; + uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) { - if (el.s_hdl >= s_hdl && el.s_hdl <= e_hdl && - el.type == GATT_UUID_PRI_SERVICE) { - p_uuid = gatts_get_service_uuid(el.p_db); - if (p_uuid != NULL) { - if (op_code == GATT_REQ_READ_BY_GRP_TYPE) handle_len = 4 + p_uuid->len; - - /* get the length byte in the repsonse */ - if (p_msg->offset == 0) { - *p++ = op_code + 1; - p_msg->len++; - p_msg->offset = handle_len; - - if (op_code == GATT_REQ_READ_BY_GRP_TYPE) { - *p++ = (uint8_t)p_msg->offset; /* length byte */ - p_msg->len++; - } - } + if (el.s_hdl < s_hdl || el.s_hdl > e_hdl || + el.type != GATT_UUID_PRI_SERVICE) { + continue; + } - if (p_msg->len + p_msg->offset <= tcb.payload_size && - handle_len == p_msg->offset) { - if (op_code != GATT_REQ_FIND_TYPE_VALUE || - gatt_uuid_compare(value, *p_uuid)) { - UINT16_TO_STREAM(p, el.s_hdl); - - if (gatt_cb.last_primary_s_handle && - gatt_cb.last_primary_s_handle == el.s_hdl) { - VLOG(1) << "Use 0xFFFF for the last primary attribute"; - /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */ - UINT16_TO_STREAM(p, 0xFFFF); - } else { - UINT16_TO_STREAM(p, el.e_hdl); - } + Uuid* p_uuid = gatts_get_service_uuid(el.p_db); + if (!p_uuid) continue; - if (op_code == GATT_REQ_READ_BY_GRP_TYPE) - gatt_build_uuid_to_stream(&p, *p_uuid); + if (op_code == GATT_REQ_READ_BY_GRP_TYPE) + handle_len = 4 + p_uuid->GetShortestRepresentationSize(); - status = GATT_SUCCESS; - p_msg->len += p_msg->offset; - } - } else - break; + /* get the length byte in the repsonse */ + if (p_msg->offset == 0) { + *p++ = op_code + 1; + p_msg->len++; + p_msg->offset = handle_len; + + if (op_code == GATT_REQ_READ_BY_GRP_TYPE) { + *p++ = (uint8_t)p_msg->offset; /* length byte */ + p_msg->len++; } } + + if (p_msg->len + p_msg->offset > tcb.payload_size || + handle_len != p_msg->offset) { + break; + } + + if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue; + + UINT16_TO_STREAM(p, el.s_hdl); + + if (gatt_cb.last_primary_s_handle && + gatt_cb.last_primary_s_handle == el.s_hdl) { + VLOG(1) << "Use 0xFFFF for the last primary attribute"; + /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */ + UINT16_TO_STREAM(p, 0xFFFF); + } else { + UINT16_TO_STREAM(p, el.e_hdl); + } + + if (op_code == GATT_REQ_READ_BY_GRP_TYPE) + gatt_build_uuid_to_stream(&p, *p_uuid); + + status = GATT_SUCCESS; + p_msg->len += p_msg->offset; } p_msg->offset = L2CAP_MIN_OFFSET; @@ -518,25 +522,25 @@ static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el, if (attr.handle < s_hdl) continue; + uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize(); if (p_msg->offset == 0) - p_msg->offset = (attr.uuid.len == LEN_UUID_16) ? GATT_INFO_TYPE_PAIR_16 - : GATT_INFO_TYPE_PAIR_128; + p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16 + : GATT_INFO_TYPE_PAIR_128; if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES; if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && - attr.uuid.len == LEN_UUID_16) { + uuid_len == Uuid::kNumBytes16) { UINT16_TO_STREAM(p, attr.handle); - UINT16_TO_STREAM(p, attr.uuid.uu.uuid16); + UINT16_TO_STREAM(p, attr.uuid.As16Bit()); } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && - attr.uuid.len == LEN_UUID_128) { + uuid_len == Uuid::kNumBytes128) { UINT16_TO_STREAM(p, attr.handle); - ARRAY_TO_STREAM(p, attr.uuid.uu.uuid128, LEN_UUID_128); + ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128); } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && - attr.uuid.len == LEN_UUID_32) { + uuid_len == Uuid::kNumBytes32) { UINT16_TO_STREAM(p, attr.handle); - gatt_convert_uuid32_to_uuid128(p, attr.uuid.uu.uuid32); - p += LEN_UUID_128; + ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128); } else { LOG(ERROR) << "format mismatch"; return GATT_NO_RESOURCES; @@ -568,7 +572,7 @@ static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl, } static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len, - uint8_t*& p, tBT_UUID* p_uuid, + uint8_t*& p, Uuid* p_uuid, uint16_t& s_hdl, uint16_t& e_hdl) { tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl); @@ -602,7 +606,7 @@ static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len, void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len, uint8_t* p_data) { uint16_t s_hdl = 0, e_hdl = 0; - tBT_UUID uuid; + Uuid uuid = Uuid::kEmpty; uint8_t reason = gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl); @@ -611,25 +615,23 @@ void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code, return; } - tBT_UUID primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}}; - if (!gatt_uuid_compare(uuid, primary_service)) { + if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) { if (op_code == GATT_REQ_READ_BY_GRP_TYPE) { gatt_send_error_rsp(tcb, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl, false); - VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: 0x%04x", - uuid.uu.uuid16); + VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s", + uuid.ToString().c_str()); return; } // we do not support ReadByTypeValue with any non-primamry_service type gatt_send_error_rsp(tcb, GATT_NOT_FOUND, op_code, s_hdl, false); - VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: 0x%04x", - uuid.uu.uuid16); + VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s", + uuid.ToString().c_str()); return; } // TODO: we assume theh value is UUID, there is no such requirement in spec - tBT_UUID value; - memset(&value, 0, sizeof(tBT_UUID)); + Uuid value = Uuid::kEmpty; if (op_code == GATT_REQ_FIND_TYPE_VALUE) { if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == false) { gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, s_hdl, false); @@ -775,7 +777,7 @@ static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len, ******************************************************************************/ void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len, uint8_t* p_data) { - tBT_UUID uuid; + Uuid uuid = Uuid::kEmpty; uint16_t s_hdl, e_hdl, err_hdl = 0; tGATT_STATUS reason = gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl); diff --git a/stack/gatt/gatt_utils.cc b/stack/gatt/gatt_utils.cc index ad8ab7aa2..976bcae80 100644 --- a/stack/gatt/gatt_utils.cc +++ b/stack/gatt/gatt_utils.cc @@ -37,6 +37,7 @@ #include "sdp_api.h" using base::StringPrintf; +using bluetooth::Uuid; /* check if [x, y] and [a, b] have overlapping range */ #define GATT_VALIDATE_HANDLE_RANGE(x, y, a, b) ((y) >= (a) && (x) <= (b)) @@ -76,10 +77,6 @@ const char* const op_code_name[] = {"UNKNOWN", "ATT_HANDLE_VALUE_CONF", "ATT_OP_CODE_MAX"}; -static const uint8_t base_uuid[LEN_UUID_128] = { - 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - /******************************************************************************* * * Function gatt_free_pending_ind @@ -220,12 +217,12 @@ tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle) { * ******************************************************************************/ std::list::iterator gatt_find_hdl_buffer_by_app_id( - tBT_UUID* p_app_uuid128, tBT_UUID* p_svc_uuid, uint16_t start_handle) { + const Uuid& app_uuid128, Uuid* p_svc_uuid, uint16_t start_handle) { auto end_it = gatt_cb.hdl_list_info->end(); auto it = gatt_cb.hdl_list_info->begin(); for (; it != end_it; it++) { - if (gatt_uuid_compare(*p_app_uuid128, it->asgn_range.app_uuid128) && - gatt_uuid_compare(*p_svc_uuid, it->asgn_range.svc_uuid) && + if (app_uuid128 == it->asgn_range.app_uuid128 && + *p_svc_uuid == it->asgn_range.svc_uuid && (start_handle == it->asgn_range.s_handle)) { return it; } @@ -238,11 +235,11 @@ std::list::iterator gatt_find_hdl_buffer_by_app_id( * free the service attribute database buffers by the owner of the service app * ID. */ -void gatt_free_srvc_db_buffer_app_id(tBT_UUID* p_app_id) { +void gatt_free_srvc_db_buffer_app_id(const Uuid& app_id) { auto it = gatt_cb.hdl_list_info->begin(); auto end = gatt_cb.hdl_list_info->end(); while (it != end) { - if (memcmp(p_app_id, &it->asgn_range.app_uuid128, sizeof(tBT_UUID)) == 0) { + if (app_id == it->asgn_range.app_uuid128) { it = gatt_cb.hdl_list_info->erase(it); } else { it++; @@ -461,179 +458,51 @@ tGATT_TCB* gatt_allocate_tcb_by_bdaddr(const RawAddress& bda, return NULL; } -/******************************************************************************* - * - * Function gatt_convert_uuid16_to_uuid128 - * - * Description Convert a 16 bits UUID to be an standard 128 bits one. - * - * Returns true if two uuid match; false otherwise. - * - ******************************************************************************/ -void gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128], - uint16_t uuid_16) { - uint8_t* p = &uuid_128[LEN_UUID_128 - 4]; - - memcpy(uuid_128, base_uuid, LEN_UUID_128); - - UINT16_TO_STREAM(p, uuid_16); -} - -/******************************************************************************* - * - * Function gatt_convert_uuid32_to_uuid128 - * - * Description Convert a 32 bits UUID to be an standard 128 bits one. - * - * Returns true if two uuid match; false otherwise. - * - ******************************************************************************/ -void gatt_convert_uuid32_to_uuid128(uint8_t uuid_128[LEN_UUID_128], - uint32_t uuid_32) { - uint8_t* p = &uuid_128[LEN_UUID_128 - 4]; - - memcpy(uuid_128, base_uuid, LEN_UUID_128); - - UINT32_TO_STREAM(p, uuid_32); -} -/******************************************************************************* - * - * Function gatt_uuid_compare - * - * Description Compare two UUID to see if they are the same. - * - * Returns true if two uuid match; false otherwise. - * - ******************************************************************************/ -bool gatt_uuid_compare(tBT_UUID src, tBT_UUID tar) { - uint8_t su[LEN_UUID_128], tu[LEN_UUID_128]; - uint8_t *ps, *pt; - - /* any of the UUID is unspecified */ - if (src.len == 0 || tar.len == 0) { - return true; - } - - /* If both are 16-bit, we can do a simple compare */ - if (src.len == LEN_UUID_16 && tar.len == LEN_UUID_16) { - return src.uu.uuid16 == tar.uu.uuid16; - } +/** Add UUID into stream. Returns UUID length. */ +uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst, const Uuid& uuid) { + uint8_t* p = *p_dst; + size_t len = uuid.GetShortestRepresentationSize(); - /* If both are 32-bit, we can do a simple compare */ - if (src.len == LEN_UUID_32 && tar.len == LEN_UUID_32) { - return src.uu.uuid32 == tar.uu.uuid32; + if (uuid.IsEmpty()) { + return 0; } - /* One or both of the UUIDs is 128-bit */ - if (src.len == LEN_UUID_16) { - /* convert a 16 bits UUID to 128 bits value */ - gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16); - ps = su; - } else if (src.len == LEN_UUID_32) { - gatt_convert_uuid32_to_uuid128(su, src.uu.uuid32); - ps = su; - } else - ps = src.uu.uuid128; - - if (tar.len == LEN_UUID_16) { - /* convert a 16 bits UUID to 128 bits value */ - gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16); - pt = tu; - } else if (tar.len == LEN_UUID_32) { - /* convert a 32 bits UUID to 128 bits value */ - gatt_convert_uuid32_to_uuid128(tu, tar.uu.uuid32); - pt = tu; - } else - pt = tar.uu.uuid128; - - return (memcmp(ps, pt, LEN_UUID_128) == 0); -} - -/******************************************************************************* - * - * Function gatt_build_uuid_to_stream - * - * Description Add UUID into stream. - * - * Returns UUID length. - * - ******************************************************************************/ -uint8_t gatt_build_uuid_to_stream(uint8_t** p_dst, tBT_UUID uuid) { - uint8_t* p = *p_dst; - uint8_t len = 0; - - if (uuid.len == LEN_UUID_16) { - UINT16_TO_STREAM(p, uuid.uu.uuid16); - len = LEN_UUID_16; - } else if (uuid.len == - LEN_UUID_32) /* always convert 32 bits into 128 bits as alwats */ - { - gatt_convert_uuid32_to_uuid128(p, uuid.uu.uuid32); - p += LEN_UUID_128; - len = LEN_UUID_128; - } else if (uuid.len == LEN_UUID_128) { - ARRAY_TO_STREAM(p, uuid.uu.uuid128, LEN_UUID_128); - len = LEN_UUID_128; + if (len == Uuid::kNumBytes16) { + UINT16_TO_STREAM(p, uuid.As16Bit()); + } else if (len == Uuid::kNumBytes32) { + /* always convert 32 bits into 128 bits */ + ARRAY_TO_STREAM(p, uuid.To128BitLE(), (int)Uuid::kNumBytes128); + len = Uuid::kNumBytes128; + } else if (len == Uuid::kNumBytes128) { + ARRAY_TO_STREAM(p, uuid.To128BitLE(), (int)Uuid::kNumBytes128); } *p_dst = p; return len; } -/******************************************************************************* - * - * Function gatt_parse_uuid_from_cmd - * - * Description Convert a 128 bits UUID into a 16 bits UUID. - * - * Returns true if command sent, otherwise false. - * - ******************************************************************************/ -bool gatt_parse_uuid_from_cmd(tBT_UUID* p_uuid_rec, uint16_t uuid_size, +bool gatt_parse_uuid_from_cmd(Uuid* p_uuid_rec, uint16_t uuid_size, uint8_t** p_data) { - bool is_base_uuid, ret = true; - uint8_t xx; + bool ret = true; uint8_t* p_uuid = *p_data; - memset(p_uuid_rec, 0, sizeof(tBT_UUID)); - switch (uuid_size) { - case LEN_UUID_16: - p_uuid_rec->len = uuid_size; - STREAM_TO_UINT16(p_uuid_rec->uu.uuid16, p_uuid); - *p_data += LEN_UUID_16; - break; + case Uuid::kNumBytes16: { + uint16_t val; + STREAM_TO_UINT16(val, p_uuid); + *p_uuid_rec = Uuid::From16Bit(val); + *p_data += Uuid::kNumBytes16; + return true; + } - case LEN_UUID_128: - /* See if we can compress his UUID down to 16 or 32bit UUIDs */ - is_base_uuid = true; - for (xx = 0; xx < LEN_UUID_128 - 4; xx++) { - if (p_uuid[xx] != base_uuid[xx]) { - is_base_uuid = false; - break; - } - } - if (is_base_uuid) { - if ((p_uuid[LEN_UUID_128 - 1] == 0) && - (p_uuid[LEN_UUID_128 - 2] == 0)) { - p_uuid += (LEN_UUID_128 - 4); - p_uuid_rec->len = LEN_UUID_16; - STREAM_TO_UINT16(p_uuid_rec->uu.uuid16, p_uuid); - } else { - p_uuid += (LEN_UUID_128 - LEN_UUID_32); - p_uuid_rec->len = LEN_UUID_32; - STREAM_TO_UINT32(p_uuid_rec->uu.uuid32, p_uuid); - } - } - if (!is_base_uuid) { - p_uuid_rec->len = LEN_UUID_128; - memcpy(p_uuid_rec->uu.uuid128, p_uuid, LEN_UUID_128); - } - *p_data += LEN_UUID_128; - break; + case Uuid::kNumBytes128: { + *p_uuid_rec = Uuid::From128BitLE(p_uuid); + *p_data += Uuid::kNumBytes128; + return true; + } /* do not allow 32 bits UUID in ATT PDU now */ - case LEN_UUID_32: + case Uuid::kNumBytes32: LOG(ERROR) << "DO NOT ALLOW 32 BITS UUID IN ATT PDU"; return false; case 0: @@ -877,47 +746,43 @@ tGATT_STATUS gatt_send_error_rsp(tGATT_TCB& tcb, uint8_t err_code, * Returns 0 if error else sdp handle for the record. * ******************************************************************************/ -uint32_t gatt_add_sdp_record(tBT_UUID* p_uuid, uint16_t start_hdl, +uint32_t gatt_add_sdp_record(const Uuid& uuid, uint16_t start_hdl, uint16_t end_hdl) { - tSDP_PROTOCOL_ELEM proto_elem_list[2]; - uint32_t sdp_handle; - uint16_t list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; uint8_t buff[60]; uint8_t* p = buff; VLOG(1) << __func__ << StringPrintf(" s_hdl=0x%x s_hdl=0x%x", start_hdl, end_hdl); - sdp_handle = SDP_CreateRecord(); + uint32_t sdp_handle = SDP_CreateRecord(); if (sdp_handle == 0) return 0; - switch (p_uuid->len) { - case LEN_UUID_16: - SDP_AddServiceClassIdList(sdp_handle, 1, &p_uuid->uu.uuid16); + switch (uuid.GetShortestRepresentationSize()) { + case Uuid::kNumBytes16: { + uint16_t tmp = uuid.As16Bit(); + SDP_AddServiceClassIdList(sdp_handle, 1, &tmp); break; + } - case LEN_UUID_32: + case Uuid::kNumBytes32: { UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES); - UINT32_TO_BE_STREAM(p, p_uuid->uu.uuid32); + uint32_t tmp = uuid.As32Bit(); + UINT32_TO_BE_STREAM(p, tmp); SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - buff), buff); break; + } - case LEN_UUID_128: + case Uuid::kNumBytes128: UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES); - ARRAY_TO_BE_STREAM_REVERSE(p, p_uuid->uu.uuid128, LEN_UUID_128); + ARRAY_TO_BE_STREAM(p, uuid.To128BitBE().data(), (int)Uuid::kNumBytes128); SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - buff), buff); break; - - default: - LOG(ERROR) << "inavlid UUID len=" << +p_uuid->len; - SDP_DeleteRecord(sdp_handle); - return 0; - break; } /*** Fill out the protocol element sequence for SDP ***/ + tSDP_PROTOCOL_ELEM proto_elem_list[2]; proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP; proto_elem_list[0].num_params = 1; proto_elem_list[0].params[0] = BT_PSM_ATT; @@ -929,6 +794,7 @@ uint32_t gatt_add_sdp_record(tBT_UUID* p_uuid, uint16_t start_hdl, SDP_AddProtocolList(sdp_handle, 2, proto_elem_list); /* Make the service browseable */ + uint16_t list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &list); return (sdp_handle); @@ -1440,40 +1306,6 @@ uint8_t* gatt_dbg_op_name(uint8_t op_code) { return (uint8_t*)"Op Code Exceed Max"; } -/******************************************************************************* - * - * Function gatt_dbg_display_uuid - * - * Description Disaplay the UUID - * - * Returns None - * - ******************************************************************************/ -void gatt_dbg_display_uuid(tBT_UUID bt_uuid) { - char str_buf[50]; - - if (bt_uuid.len == LEN_UUID_16) { - snprintf(str_buf, sizeof(str_buf), "0x%04x", bt_uuid.uu.uuid16); - } else if (bt_uuid.len == LEN_UUID_32) { - snprintf(str_buf, sizeof(str_buf), "0x%08x", - (unsigned int)bt_uuid.uu.uuid32); - } else if (bt_uuid.len == LEN_UUID_128) { - int x = snprintf( - str_buf, sizeof(str_buf), "0x%02x%02x%02x%02x%02x%02x%02x%02x", - bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14], bt_uuid.uu.uuid128[13], - bt_uuid.uu.uuid128[12], bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10], - bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]); - snprintf( - &str_buf[x], sizeof(str_buf) - x, "%02x%02x%02x%02x%02x%02x%02x%02x", - bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6], bt_uuid.uu.uuid128[5], - bt_uuid.uu.uuid128[4], bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2], - bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]); - } else - strlcpy(str_buf, "Unknown UUID 0", sizeof(str_buf)); - - VLOG(1) << StringPrintf("UUID=[%s]", str_buf); -} - /** Returns true if this is one of the background devices for the application, * false otherwise */ bool gatt_is_bg_dev_for_app(tGATT_BG_CONN_DEV* p_dev, tGATT_IF gatt_if) { diff --git a/stack/hid/hidh_api.cc b/stack/hid/hidh_api.cc index 704426fe3..3fe2d28f0 100644 --- a/stack/hid/hidh_api.cc +++ b/stack/hid/hidh_api.cc @@ -35,6 +35,8 @@ #include "hidh_api.h" #include "hidh_int.h" +using bluetooth::Uuid; + tHID_HOST_CTB hh_cb; static void hidh_search_callback(uint16_t sdp_result); @@ -51,14 +53,11 @@ static void hidh_search_callback(uint16_t sdp_result); tHID_STATUS HID_HostGetSDPRecord(const RawAddress& addr, tSDP_DISCOVERY_DB* p_db, uint32_t db_len, tHID_HOST_SDP_CALLBACK* sdp_cback) { - tSDP_UUID uuid_list; if (hh_cb.sdp_busy) return HID_ERR_SDP_BUSY; - uuid_list.len = 2; - uuid_list.uu.uuid16 = UUID_SERVCLASS_HUMAN_INTERFACE; - hh_cb.p_sdp_db = p_db; + Uuid uuid_list = Uuid::From16Bit(UUID_SERVCLASS_HUMAN_INTERFACE); SDP_InitDiscoveryDb(p_db, db_len, 1, &uuid_list, 0, NULL); if (SDP_ServiceSearchRequest(addr, p_db, hidh_search_callback)) { @@ -92,12 +91,9 @@ static void hidh_search_callback(uint16_t sdp_result) { tSDP_DISCOVERY_DB* p_db = hh_cb.p_sdp_db; tSDP_DISC_REC* p_rec; tSDP_DISC_ATTR *p_attr, *p_subattr1, *p_subattr2, *p_repdesc; - tBT_UUID hid_uuid; tHID_DEV_SDP_INFO* p_nvi = &hh_cb.sdp_rec; uint16_t attr_mask = 0; - hid_uuid.len = LEN_UUID_16; - hid_uuid.uu.uuid16 = UUID_SERVCLASS_HUMAN_INTERFACE; hh_cb.sdp_busy = false; @@ -106,7 +102,8 @@ static void hidh_search_callback(uint16_t sdp_result) { return; } - p_rec = SDP_FindServiceUUIDInDb(p_db, &hid_uuid, NULL); + Uuid hid_uuid = Uuid::From16Bit(UUID_SERVCLASS_HUMAN_INTERFACE); + p_rec = SDP_FindServiceUUIDInDb(p_db, hid_uuid, NULL); if (p_rec == NULL) { hh_cb.sdp_cback(HID_SDP_NO_SERV_UUID, 0, NULL); return; diff --git a/stack/include/bnep_api.h b/stack/include/bnep_api.h index 4116ac929..579f59039 100644 --- a/stack/include/bnep_api.h +++ b/stack/include/bnep_api.h @@ -91,7 +91,8 @@ typedef void(tBNEP_CONN_STATE_CB)(uint16_t handle, const RawAddress& rem_bda, * use BNEP_ConnectResp call to accept or reject the request */ typedef void(tBNEP_CONNECT_IND_CB)(uint16_t handle, const RawAddress& bd_addr, - tBT_UUID* remote_uuid, tBT_UUID* local_uuid, + const bluetooth::Uuid& remote_uuid, + const bluetooth::Uuid& local_uuid, bool is_role_change); /* Data buffer received indication callback prototype. Parameters are @@ -190,8 +191,8 @@ typedef struct { uint16_t sent_mcast_filters; uint16_t rcvd_num_filters; uint16_t rcvd_mcast_filters; - tBT_UUID src_uuid; - tBT_UUID dst_uuid; + bluetooth::Uuid src_uuid; + bluetooth::Uuid dst_uuid; } tBNEP_STATUS; @@ -246,7 +247,8 @@ extern void BNEP_Deregister(void); * ******************************************************************************/ extern tBNEP_RESULT BNEP_Connect(const RawAddress& p_rem_bda, - tBT_UUID* src_uuid, tBT_UUID* dst_uuid, + const bluetooth::Uuid& src_uuid, + const bluetooth::Uuid& dst_uuid, uint16_t* p_handle); /******************************************************************************* diff --git a/stack/include/bt_types.h b/stack/include/bt_types.h index 22cb48d90..99f687aeb 100644 --- a/stack/include/bt_types.h +++ b/stack/include/bt_types.h @@ -639,23 +639,6 @@ typedef uint8_t ACCESS_CODE[ACCESS_CODE_BYTE_LEN]; #define BT_1SEC_TIMEOUT_MS (1 * 1000) /* 1 second */ -/* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */ -#define MAX_UUID_SIZE 16 -typedef struct { -#define LEN_UUID_16 2 -#define LEN_UUID_32 4 -#define LEN_UUID_128 16 - - uint16_t len; - - union { - uint16_t uuid16; - uint32_t uuid32; - uint8_t uuid128[MAX_UUID_SIZE]; - } uu; - -} tBT_UUID; - #define BT_EIR_FLAGS_TYPE 0x01 #define BT_EIR_MORE_16BITS_UUID_TYPE 0x02 #define BT_EIR_COMPLETE_16BITS_UUID_TYPE 0x03 diff --git a/stack/include/btm_api.h b/stack/include/btm_api.h index 10f4f6583..451471eed 100644 --- a/stack/include/btm_api.h +++ b/stack/include/btm_api.h @@ -1956,7 +1956,8 @@ extern uint8_t BTM_GetEirSupportedServices(uint32_t* p_eir_uuid, uint8_t** p, * * Parameters p_eir - EIR * eirl_len - EIR len - * uuid_size - LEN_UUID_16, LEN_UUID_32, LEN_UUID_128 + * uuid_size - Uuid::kNumBytes16, Uuid::kNumBytes32, + * Uuid::kNumBytes128 * p_num_uuid - return number of UUID in found list * p_uuid_list - return UUID 16-bit list * max_num_uuid - maximum number of UUID to be returned diff --git a/stack/include/btm_ble_api.h b/stack/include/btm_ble_api.h index 36c81289b..f43a1fb1f 100644 --- a/stack/include/btm_ble_api.h +++ b/stack/include/btm_ble_api.h @@ -775,7 +775,7 @@ extern void BTM_LE_PF_local_name(tBTM_BLE_SCAN_COND_OP action, extern void BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action, tBTM_BLE_PF_FILT_INDEX filt_index, tBTM_BLE_PF_COND_TYPE filter_type, - tBT_UUID uuid, + const bluetooth::Uuid& uuid, tBTM_BLE_PF_LOGIC_TYPE cond_logic, tBTM_BLE_PF_COND_MASK* p_uuid_mask, tBTM_BLE_PF_CFG_CBACK cb); diff --git a/stack/include/btm_ble_api_types.h b/stack/include/btm_ble_api_types.h index 57ef0d1d9..b9a7273d3 100644 --- a/stack/include/btm_ble_api_types.h +++ b/stack/include/btm_ble_api_types.h @@ -462,7 +462,7 @@ using tBTM_BLE_PF_PARAM_CB = base::Callback> 12) @@ -120,7 +117,7 @@ typedef struct { uint32_t mem_free; /* Memory still available */ tSDP_DISC_REC* p_first_rec; /* Addr of first record in DB */ uint16_t num_uuid_filters; /* Number of UUIds to filter */ - tSDP_UUID uuid_filters[SDP_MAX_UUID_FILTERS]; /* UUIDs to filter */ + bluetooth::Uuid uuid_filters[SDP_MAX_UUID_FILTERS]; /* UUIDs to filter */ uint16_t num_attr_filters; /* Number of attribute filters */ uint16_t attr_filters[SDP_MAX_ATTR_FILTERS]; /* Attributes to filter */ uint8_t* p_free_mem; /* Pointer to free memory */ @@ -176,7 +173,7 @@ typedef struct t_sdp_di_get_record { * ******************************************************************************/ bool SDP_InitDiscoveryDb(tSDP_DISCOVERY_DB* p_db, uint32_t len, - uint16_t num_uuid, tSDP_UUID* p_uuid_list, + uint16_t num_uuid, const bluetooth::Uuid* p_uuid_list, uint16_t num_attr, uint16_t* p_attr_list); /******************************************************************************* @@ -295,13 +292,13 @@ tSDP_DISC_REC* SDP_FindServiceInDb(tSDP_DISCOVERY_DB* p_db, * * NOTE the only difference between this function and the previous * function "SDP_FindServiceInDb()" is that this function takes - * a tBT_UUID input. + * a Uuid input. * * Returns Pointer to record containing service class, or NULL * ******************************************************************************/ tSDP_DISC_REC* SDP_FindServiceUUIDInDb(tSDP_DISCOVERY_DB* p_db, - tBT_UUID* p_uuid, + const bluetooth::Uuid& uuid, tSDP_DISC_REC* p_start_rec); /******************************************************************************* @@ -317,7 +314,8 @@ tSDP_DISC_REC* SDP_FindServiceUUIDInDb(tSDP_DISCOVERY_DB* p_db, * Returns true if found, otherwise false. * ******************************************************************************/ -bool SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid); +bool SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC* p_rec, + bluetooth::Uuid* p_uuid); /******************************************************************************* * @@ -636,11 +634,6 @@ uint8_t SDP_SetTraceLevel(uint8_t new_level); * Returns true if found, otherwise false. * ******************************************************************************/ -bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid); - -// Converts UUID-16 to UUID-128 by including the base UUID. -// |uuid16| is the 2-byte UUID to convert. -// The result with the expanded 128-bit UUID is stored in |p_uuid128|. -void sdpu_uuid16_to_uuid128(uint16_t uuid16, uint8_t* p_uuid128); +bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, bluetooth::Uuid* p_uuid); #endif /* SDP_API_H */ diff --git a/stack/pan/pan_api.cc b/stack/pan/pan_api.cc index a36793c1a..a7cace52c 100644 --- a/stack/pan/pan_api.cc +++ b/stack/pan/pan_api.cc @@ -37,6 +37,8 @@ #include "sdp_api.h" #include "sdpdefs.h" +using bluetooth::Uuid; + /******************************************************************************* * * Function PAN_Register @@ -266,9 +268,6 @@ tPAN_RESULT PAN_SetRole(uint8_t role, uint8_t* sec_mask, ******************************************************************************/ tPAN_RESULT PAN_Connect(const RawAddress& rem_bda, uint8_t src_role, uint8_t dst_role, uint16_t* handle) { - tPAN_CONN* pcb; - tBNEP_RESULT result; - tBT_UUID src_uuid, dst_uuid; uint32_t mx_chan_id; /* @@ -294,8 +293,9 @@ tPAN_RESULT PAN_Connect(const RawAddress& rem_bda, uint8_t src_role, } /* Check if connection exists for this remote device */ - pcb = pan_get_pcb_by_addr(rem_bda); + tPAN_CONN* pcb = pan_get_pcb_by_addr(rem_bda); + uint16_t src_uuid, dst_uuid; /* If we are PANU for this role validate destination role */ if (src_role == PAN_ROLE_CLIENT) { if ((pan_cb.num_conns > 1) || (pan_cb.num_conns && (!pcb))) { @@ -310,15 +310,15 @@ tPAN_RESULT PAN_Connect(const RawAddress& rem_bda, uint8_t src_role, return PAN_INVALID_SRC_ROLE; } - src_uuid.uu.uuid16 = UUID_SERVCLASS_PANU; + src_uuid = UUID_SERVCLASS_PANU; if (dst_role == PAN_ROLE_CLIENT) { - dst_uuid.uu.uuid16 = UUID_SERVCLASS_PANU; + dst_uuid = UUID_SERVCLASS_PANU; } else if (dst_role == PAN_ROLE_GN_SERVER) { - dst_uuid.uu.uuid16 = UUID_SERVCLASS_GN; + dst_uuid = UUID_SERVCLASS_GN; } else { - dst_uuid.uu.uuid16 = UUID_SERVCLASS_NAP; + dst_uuid = UUID_SERVCLASS_NAP; } - mx_chan_id = dst_uuid.uu.uuid16; + mx_chan_id = dst_uuid; } /* If destination is PANU role validate source role */ else if (dst_role == PAN_ROLE_CLIENT) { @@ -327,13 +327,13 @@ tPAN_RESULT PAN_Connect(const RawAddress& rem_bda, uint8_t src_role, return PAN_INVALID_SRC_ROLE; } - dst_uuid.uu.uuid16 = UUID_SERVCLASS_PANU; + dst_uuid = UUID_SERVCLASS_PANU; if (src_role == PAN_ROLE_GN_SERVER) { - src_uuid.uu.uuid16 = UUID_SERVCLASS_GN; + src_uuid = UUID_SERVCLASS_GN; } else { - src_uuid.uu.uuid16 = UUID_SERVCLASS_NAP; + src_uuid = UUID_SERVCLASS_NAP; } - mx_chan_id = src_uuid.uu.uuid16; + mx_chan_id = src_uuid; } /* The role combination is not valid */ else { @@ -364,16 +364,14 @@ tPAN_RESULT PAN_Connect(const RawAddress& rem_bda, uint8_t src_role, pcb->prv_src_uuid = pcb->src_uuid; pcb->prv_dst_uuid = pcb->dst_uuid; - pcb->src_uuid = src_uuid.uu.uuid16; - pcb->dst_uuid = dst_uuid.uu.uuid16; + pcb->src_uuid = src_uuid; + pcb->dst_uuid = dst_uuid; - src_uuid.len = 2; - dst_uuid.len = 2; - - result = BNEP_Connect(rem_bda, &src_uuid, &dst_uuid, &(pcb->handle)); - if (result != BNEP_SUCCESS) { + tBNEP_RESULT ret = BNEP_Connect(rem_bda, Uuid::From16Bit(src_uuid), + Uuid::From16Bit(dst_uuid), &(pcb->handle)); + if (ret != BNEP_SUCCESS) { pan_release_pcb(pcb); - return result; + return ret; } PAN_TRACE_DEBUG("PAN_Connect() current active role set to %d", src_role); diff --git a/stack/pan/pan_int.h b/stack/pan/pan_int.h index 27fe6668b..bd6d620b9 100644 --- a/stack/pan/pan_int.h +++ b/stack/pan/pan_int.h @@ -98,7 +98,8 @@ extern tPAN_CB pan_cb; /******************************************************************************/ extern void pan_register_with_bnep(void); extern void pan_conn_ind_cb(uint16_t handle, const RawAddress& p_bda, - tBT_UUID* remote_uuid, tBT_UUID* local_uuid, + const bluetooth::Uuid& remote_uuid, + const bluetooth::Uuid& local_uuid, bool is_role_change); extern void pan_connect_state_cb(uint16_t handle, const RawAddress& rem_bda, tBNEP_RESULT result, bool is_role_change); diff --git a/stack/pan/pan_main.cc b/stack/pan/pan_main.cc index 8817c4e61..61ea846d8 100644 --- a/stack/pan/pan_main.cc +++ b/stack/pan/pan_main.cc @@ -36,11 +36,9 @@ #include "sdp_api.h" #include "sdpdefs.h" -tPAN_CB pan_cb; +using bluetooth::Uuid; -#define UUID_CONSTANT_PART 12 -uint8_t constant_pan_uuid[UUID_CONSTANT_PART] = { - 0, 0, 0x10, 0, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; +tPAN_CB pan_cb; /******************************************************************************* * @@ -90,135 +88,71 @@ void pan_register_with_bnep(void) { * ******************************************************************************/ void pan_conn_ind_cb(uint16_t handle, const RawAddress& p_bda, - tBT_UUID* remote_uuid, tBT_UUID* local_uuid, + const Uuid& remote_uuid, const Uuid& local_uuid, bool is_role_change) { - tPAN_CONN* pcb; - uint8_t req_role; - bool wrong_uuid; - - /* - ** If we are in GN or NAP role and have one or more - ** active connections and the received connection is - ** for user role reject it. - ** If we are in user role with one connection active - ** reject the connection. - ** Allocate PCB and store the parameters - ** Make bridge request to the host system if connection - ** is for NAP - */ - wrong_uuid = false; - if (remote_uuid->len == 16) { - /* - ** If the UUID is 16 bytes forst two bytes should be zeros - ** and last 12 bytes should match the spec defined constant value - */ - if (memcmp(constant_pan_uuid, remote_uuid->uu.uuid128 + 4, - UUID_CONSTANT_PART)) - wrong_uuid = true; - - if (remote_uuid->uu.uuid128[0] || remote_uuid->uu.uuid128[1]) - wrong_uuid = true; - - /* Extract the 16 bit equivalent of the UUID */ - remote_uuid->uu.uuid16 = (uint16_t)((remote_uuid->uu.uuid128[2] << 8) | - remote_uuid->uu.uuid128[3]); - remote_uuid->len = 2; - } - if (remote_uuid->len == 4) { - /* First two bytes should be zeros */ - if (remote_uuid->uu.uuid32 & 0xFFFF0000) wrong_uuid = true; - - remote_uuid->uu.uuid16 = (uint16_t)remote_uuid->uu.uuid32; - remote_uuid->len = 2; - } + /* If we are in GN or NAP role and have one or more active connections and the + * received connection is for user role reject it. If we are in user role with + * one connection active reject the connection. Allocate PCB and store the + * parameters. Make bridge request to the host system if connection is for NAP + */ - if (wrong_uuid) { + if (!remote_uuid.Is16Bit()) { PAN_TRACE_ERROR("PAN Connection failed because of wrong remote UUID "); BNEP_ConnectResp(handle, BNEP_CONN_FAILED_SRC_UUID); return; } - wrong_uuid = false; - if (local_uuid->len == 16) { - /* - ** If the UUID is 16 bytes forst two bytes should be zeros - ** and last 12 bytes should match the spec defined constant value - */ - if (memcmp(constant_pan_uuid, local_uuid->uu.uuid128 + 4, - UUID_CONSTANT_PART)) - wrong_uuid = true; - - if (local_uuid->uu.uuid128[0] || local_uuid->uu.uuid128[1]) - wrong_uuid = true; - - /* Extract the 16 bit equivalent of the UUID */ - local_uuid->uu.uuid16 = (uint16_t)((local_uuid->uu.uuid128[2] << 8) | - local_uuid->uu.uuid128[3]); - local_uuid->len = 2; - } - if (local_uuid->len == 4) { - /* First two bytes should be zeros */ - if (local_uuid->uu.uuid32 & 0xFFFF0000) wrong_uuid = true; - - local_uuid->uu.uuid16 = (uint16_t)local_uuid->uu.uuid32; - local_uuid->len = 2; - } - - if (wrong_uuid) { + if (!local_uuid.Is16Bit()) { PAN_TRACE_ERROR("PAN Connection failed because of wrong local UUID "); BNEP_ConnectResp(handle, BNEP_CONN_FAILED_DST_UUID); return; } + uint16_t remote_uuid16 = remote_uuid.As16Bit(); + uint16_t local_uuid16 = local_uuid.As16Bit(); + PAN_TRACE_EVENT( - "pan_conn_ind_cb - for handle %d, current role %d, dst uuid 0x%x, src " - "uuid 0x%x, role change %s", - handle, pan_cb.role, local_uuid->uu.uuid16, remote_uuid->uu.uuid16, + "%s - handle %d, current role %d, dst uuid 0x%x, src uuid 0x%x, role " + "change %s", + __func__, handle, pan_cb.role, local_uuid16, remote_uuid16, is_role_change ? "YES" : "NO"); - /* The acceptable UUID size is only 2 */ - if (remote_uuid->len != 2) { - PAN_TRACE_ERROR("PAN Connection failed because of wrong UUID size %d", - remote_uuid->len); - BNEP_ConnectResp(handle, BNEP_CONN_FAILED_UUID_SIZE); - return; - } /* Check if the source UUID is a valid one */ - if (remote_uuid->uu.uuid16 != UUID_SERVCLASS_PANU && - remote_uuid->uu.uuid16 != UUID_SERVCLASS_NAP && - remote_uuid->uu.uuid16 != UUID_SERVCLASS_GN) { - PAN_TRACE_ERROR("Src UUID 0x%x is not valid", remote_uuid->uu.uuid16); + if (remote_uuid16 != UUID_SERVCLASS_PANU && + remote_uuid16 != UUID_SERVCLASS_NAP && + remote_uuid16 != UUID_SERVCLASS_GN) { + PAN_TRACE_ERROR("Src UUID 0x%x is not valid", remote_uuid16); BNEP_ConnectResp(handle, BNEP_CONN_FAILED_SRC_UUID); return; } /* Check if the destination UUID is a valid one */ - if (local_uuid->uu.uuid16 != UUID_SERVCLASS_PANU && - local_uuid->uu.uuid16 != UUID_SERVCLASS_NAP && - local_uuid->uu.uuid16 != UUID_SERVCLASS_GN) { - PAN_TRACE_ERROR("Dst UUID 0x%x is not valid", remote_uuid->uu.uuid16); + if (local_uuid16 != UUID_SERVCLASS_PANU && + local_uuid16 != UUID_SERVCLASS_NAP && local_uuid16 != UUID_SERVCLASS_GN) { + PAN_TRACE_ERROR("Dst UUID 0x%x is not valid", local_uuid16); BNEP_ConnectResp(handle, BNEP_CONN_FAILED_DST_UUID); return; } /* Check if currently we support the destination role requested */ if (((!(pan_cb.role & UUID_SERVCLASS_PANU)) && - local_uuid->uu.uuid16 == UUID_SERVCLASS_PANU) || + local_uuid16 == UUID_SERVCLASS_PANU) || ((!(pan_cb.role & UUID_SERVCLASS_GN)) && - local_uuid->uu.uuid16 == UUID_SERVCLASS_GN) || + local_uuid16 == UUID_SERVCLASS_GN) || ((!(pan_cb.role & UUID_SERVCLASS_NAP)) && - local_uuid->uu.uuid16 == UUID_SERVCLASS_NAP)) { + local_uuid16 == UUID_SERVCLASS_NAP)) { PAN_TRACE_ERROR( "PAN Connection failed because of unsupported destination UUID 0x%x", - local_uuid->uu.uuid16); + local_uuid16); BNEP_ConnectResp(handle, BNEP_CONN_FAILED_DST_UUID); return; } + uint8_t req_role; /* Requested destination role is */ - if (local_uuid->uu.uuid16 == UUID_SERVCLASS_PANU) + if (local_uuid16 == UUID_SERVCLASS_PANU) req_role = PAN_ROLE_CLIENT; - else if (local_uuid->uu.uuid16 == UUID_SERVCLASS_GN) + else if (local_uuid16 == UUID_SERVCLASS_GN) req_role = PAN_ROLE_GN_SERVER; else req_role = PAN_ROLE_NAP_SERVER; @@ -226,9 +160,9 @@ void pan_conn_ind_cb(uint16_t handle, const RawAddress& p_bda, /* If the connection indication is for the existing connection ** Check if the new destination role is acceptable */ - pcb = pan_get_pcb_by_handle(handle); + tPAN_CONN* pcb = pan_get_pcb_by_handle(handle); if (pcb) { - if (pan_cb.num_conns > 1 && local_uuid->uu.uuid16 == UUID_SERVCLASS_PANU) { + if (pan_cb.num_conns > 1 && local_uuid16 == UUID_SERVCLASS_PANU) { /* There are connections other than this one ** so we cann't accept PANU role. Reject */ @@ -241,14 +175,14 @@ void pan_conn_ind_cb(uint16_t handle, const RawAddress& p_bda, /* If it is already in connected state check for bridging status */ if (pcb->con_state == PAN_STATE_CONNECTED) { - PAN_TRACE_EVENT("PAN Role changing New Src 0x%x Dst 0x%x", - remote_uuid->uu.uuid16, local_uuid->uu.uuid16); + PAN_TRACE_EVENT("PAN Role changing New Src 0x%x Dst 0x%x", remote_uuid16, + local_uuid16); pcb->prv_src_uuid = pcb->src_uuid; pcb->prv_dst_uuid = pcb->dst_uuid; if (pcb->src_uuid == UUID_SERVCLASS_NAP && - local_uuid->uu.uuid16 != UUID_SERVCLASS_NAP) { + local_uuid16 != UUID_SERVCLASS_NAP) { /* Remove bridging */ if (pan_cb.pan_bridge_req_cb) (*pan_cb.pan_bridge_req_cb)(pcb->rem_bda, false); @@ -256,8 +190,8 @@ void pan_conn_ind_cb(uint16_t handle, const RawAddress& p_bda, } /* Set the latest active PAN role */ pan_cb.active_role = req_role; - pcb->src_uuid = local_uuid->uu.uuid16; - pcb->dst_uuid = remote_uuid->uu.uuid16; + pcb->src_uuid = local_uuid16; + pcb->dst_uuid = remote_uuid16; BNEP_ConnectResp(handle, BNEP_SUCCESS); return; } else { @@ -265,7 +199,7 @@ void pan_conn_ind_cb(uint16_t handle, const RawAddress& p_bda, ** we already have a connection then reject the request. ** If we have a connection in PANU role then reject it */ - if (pan_cb.num_conns && (local_uuid->uu.uuid16 == UUID_SERVCLASS_PANU || + if (pan_cb.num_conns && (local_uuid16 == UUID_SERVCLASS_PANU || pan_cb.active_role == PAN_ROLE_CLIENT)) { PAN_TRACE_ERROR("PAN already have a connection and can't be user"); BNEP_ConnectResp(handle, BNEP_CONN_FAILED_DST_UUID); @@ -282,12 +216,11 @@ void pan_conn_ind_cb(uint16_t handle, const RawAddress& p_bda, return; } - PAN_TRACE_EVENT("PAN connection destination UUID is 0x%x", - local_uuid->uu.uuid16); + PAN_TRACE_EVENT("PAN connection destination UUID is 0x%x", local_uuid16); /* Set the latest active PAN role */ pan_cb.active_role = req_role; - pcb->src_uuid = local_uuid->uu.uuid16; - pcb->dst_uuid = remote_uuid->uu.uuid16; + pcb->src_uuid = local_uuid16; + pcb->dst_uuid = remote_uuid16; pcb->con_state = PAN_STATE_CONN_START; pan_cb.num_conns++; diff --git a/stack/rfcomm/port_api.cc b/stack/rfcomm/port_api.cc index 808b567fb..b32e038d0 100644 --- a/stack/rfcomm/port_api.cc +++ b/stack/rfcomm/port_api.cc @@ -126,7 +126,7 @@ int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server, if ((scn == 0) || (scn >= PORT_MAX_RFC_PORTS)) { /* Server Channel Number(SCN) should be in range 1...30 */ - RFCOMM_TRACE_ERROR("RFCOMM_CreateConnection - invalid SCN"); + RFCOMM_TRACE_ERROR("%s - invalid SCN", __func__); return (PORT_INVALID_SCN); } @@ -136,10 +136,8 @@ int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server, dlci = (scn << 1) + 1; else dlci = (scn << 1); - RFCOMM_TRACE_API( - "RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, " - "p_mcb:%p", - scn, dlci, is_server, mtu, p_mcb); + RFCOMM_TRACE_API("%s: scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p", + __func__, scn, dlci, is_server, mtu, p_mcb); /* For the server side always allocate a new port. On the client side */ /* do not allow the same (dlci, bd_addr) to be opened twice by application */ @@ -149,9 +147,8 @@ int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server, /* if existing port is also a client port */ if (p_port->is_server == false) { RFCOMM_TRACE_ERROR( - "RFCOMM_CreateConnection - already opened state:%d, RFC state:%d, " - "MCB state:%d", - p_port->state, p_port->rfc.state, + "%s - already opened state:%d, RFC state:%d, MCB state:%d", + __func__, p_port->state, p_port->rfc.state, p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0); *p_handle = p_port->inx; return (PORT_ALREADY_OPENED); @@ -161,12 +158,11 @@ int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server, p_port = port_allocate_port(dlci, bd_addr); if (p_port == NULL) { - RFCOMM_TRACE_WARNING("RFCOMM_CreateConnection - no resources"); + RFCOMM_TRACE_WARNING("%s - no resources", __func__); return (PORT_NO_RESOURCES); } RFCOMM_TRACE_API( - "RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, " - "p_mcb:%p, p_port:%p", + "%s: scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p, p_port:%p", __func__, scn, dlci, is_server, mtu, p_mcb, p_port); p_port->default_signal_state = @@ -188,7 +184,7 @@ int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server, break; } - RFCOMM_TRACE_EVENT("RFCOMM_CreateConnection dlci:%d signal state:0x%x", dlci, + RFCOMM_TRACE_EVENT("%s dlci:%d signal state:0x%x", __func__, dlci, p_port->default_signal_state); *p_handle = p_port->inx; diff --git a/stack/sdp/sdp_api.cc b/stack/sdp/sdp_api.cc index c89eca51e..8800d07c1 100644 --- a/stack/sdp/sdp_api.cc +++ b/stack/sdp/sdp_api.cc @@ -39,6 +39,8 @@ #include "osi/include/osi.h" +using bluetooth::Uuid; + /********************************************************************** * C L I E N T F U N C T I O N P R O T O T Y P E S * **********************************************************************/ @@ -66,7 +68,7 @@ * ******************************************************************************/ bool SDP_InitDiscoveryDb(tSDP_DISCOVERY_DB* p_db, uint32_t len, - uint16_t num_uuid, tSDP_UUID* p_uuid_list, + uint16_t num_uuid, const Uuid* p_uuid_list, uint16_t num_attr, uint16_t* p_attr_list) { uint16_t xx; @@ -289,7 +291,7 @@ tSDP_DISC_ATTR* SDP_FindAttributeInRec(tSDP_DISC_REC* p_rec, uint16_t attr_id) { * Returns true if found, otherwise false. * ******************************************************************************/ -bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { +bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, Uuid* p_uuid) { tSDP_DISC_ATTR *p_attr, *p_sattr, *p_extra_sattr; p_attr = p_rec->p_first_attr; @@ -300,18 +302,14 @@ bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) { if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) { - if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == LEN_UUID_16) { - p_uuid->len = LEN_UUID_16; - p_uuid->uu.uuid16 = p_sattr->attr_value.v.u16; + if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == Uuid::kNumBytes16) { + *p_uuid = Uuid::From16Bit(p_sattr->attr_value.v.u16); + } else if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == + Uuid::kNumBytes128) { + *p_uuid = Uuid::From128BitBE(p_sattr->attr_value.v.array); } else if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == - LEN_UUID_128) { - p_uuid->len = LEN_UUID_128; - for (uint8_t i = 0; i != LEN_UUID_128; ++i) - p_uuid->uu.uuid128[i] = - p_sattr->attr_value.v.array[LEN_UUID_128 - i - 1]; - } else if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == LEN_UUID_32) { - p_uuid->len = LEN_UUID_32; - p_uuid->uu.uuid32 = p_sattr->attr_value.v.u32; + Uuid::kNumBytes32) { + *p_uuid = Uuid::From32Bit(p_sattr->attr_value.v.u32); } return (true); @@ -332,8 +330,7 @@ bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { UUID_DESC_TYPE) /* only support 16 bits UUID for now */ && (SDP_DISC_ATTR_LEN(p_extra_sattr->attr_len_type) == 2)) { - p_uuid->len = 2; - p_uuid->uu.uuid16 = p_extra_sattr->attr_value.v.u16; + *p_uuid = Uuid::From16Bit(p_extra_sattr->attr_value.v.u16); return (true); } } @@ -345,8 +342,7 @@ bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) /* only support 16 bits UUID for now */ && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2)) { - p_uuid->len = 2; - p_uuid->uu.uuid16 = p_attr->attr_value.v.u16; + *p_uuid = Uuid::From16Bit(p_attr->attr_value.v.u16); return (true); } } @@ -368,7 +364,7 @@ bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { * Returns true if found, otherwise false. * ******************************************************************************/ -bool SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { +bool SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC* p_rec, Uuid* p_uuid) { tSDP_DISC_ATTR* p_attr = p_rec->p_first_attr; while (p_attr) { if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) && @@ -378,10 +374,7 @@ bool SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) { /* only support 128 bits UUID for now */ if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 16) { - p_uuid->len = LEN_UUID_128; - for (uint8_t i = 0; i != LEN_UUID_128; ++i) - p_uuid->uu.uuid128[i] = - p_sattr->attr_value.v.array[LEN_UUID_128 - i - 1]; + *p_uuid = Uuid::From128BitBE(p_sattr->attr_value.v.array); } return (true); } @@ -393,10 +386,7 @@ bool SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC* p_rec, tBT_UUID* p_uuid) { if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) /* only support 128 bits UUID for now */ && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 16)) { - p_uuid->len = LEN_UUID_128; - for (uint8_t i = 0; i != LEN_UUID_128; ++i) - p_uuid->uu.uuid128[i] = - p_attr->attr_value.v.array[LEN_UUID_128 - i - 1]; + *p_uuid = Uuid::From128BitBE(p_attr->attr_value.v.array); return (true); } } @@ -573,13 +563,13 @@ tSDP_DISC_REC* SDP_FindServiceInDb_128bit(tSDP_DISCOVERY_DB* p_db, * * NOTE the only difference between this function and the previous * function "SDP_FindServiceInDb()" is that this function takes - * a tBT_UUID input + * a Uuid input * * Returns Pointer to record containing service class, or NULL * ******************************************************************************/ tSDP_DISC_REC* SDP_FindServiceUUIDInDb(tSDP_DISCOVERY_DB* p_db, - tBT_UUID* p_uuid, + const Uuid& uuid, tSDP_DISC_REC* p_start_rec) { tSDP_DISC_REC* p_rec; tSDP_DISC_ATTR *p_attr, *p_sattr; @@ -601,13 +591,13 @@ tSDP_DISC_REC* SDP_FindServiceUUIDInDb(tSDP_DISCOVERY_DB* p_db, for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) { if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) { - if (sdpu_compare_uuid_with_attr(p_uuid, p_sattr)) return (p_rec); + if (sdpu_compare_uuid_with_attr(uuid, p_sattr)) return (p_rec); } } break; } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) { if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) { - if (sdpu_compare_uuid_with_attr(p_uuid, p_attr)) return (p_rec); + if (sdpu_compare_uuid_with_attr(uuid, p_attr)) return (p_rec); } } @@ -829,9 +819,7 @@ uint16_t SDP_DiDiscover(const RawAddress& remote_device, uint16_t di_uuid = UUID_SERVCLASS_PNP_INFORMATION; /* build uuid for db init */ - tSDP_UUID init_uuid; - init_uuid.len = 2; - init_uuid.uu.uuid16 = di_uuid; + Uuid init_uuid = Uuid::From16Bit(di_uuid); if (SDP_InitDiscoveryDb(p_db, len, num_uuids, &init_uuid, 0, NULL)) if (SDP_ServiceSearchRequest(remote_device, p_db, p_cb)) diff --git a/stack/sdp/sdp_discovery.cc b/stack/sdp/sdp_discovery.cc index fd9339f08..9bc87a3ce 100644 --- a/stack/sdp/sdp_discovery.cc +++ b/stack/sdp/sdp_discovery.cc @@ -36,6 +36,8 @@ #include "sdp_api.h" #include "sdpint.h" +using bluetooth::Uuid; + #ifndef SDP_DEBUG_RAW #define SDP_DEBUG_RAW false #endif @@ -68,7 +70,7 @@ static uint8_t* add_attr(uint8_t* p, tSDP_DISCOVERY_DB* p_db, * ******************************************************************************/ static uint8_t* sdpu_build_uuid_seq(uint8_t* p_out, uint16_t num_uuids, - tSDP_UUID* p_uuid_list) { + Uuid* p_uuid_list) { uint16_t xx; uint8_t* p_len; @@ -81,18 +83,19 @@ static uint8_t* sdpu_build_uuid_seq(uint8_t* p_out, uint16_t num_uuids, /* Now, loop through and put in all the UUID(s) */ for (xx = 0; xx < num_uuids; xx++, p_uuid_list++) { - if (p_uuid_list->len == LEN_UUID_16) { + int len = p_uuid_list->GetShortestRepresentationSize(); + if (len == Uuid::kNumBytes16) { UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES); - UINT16_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid16); - } else if (p_uuid_list->len == LEN_UUID_32) { + UINT16_TO_BE_STREAM(p_out, p_uuid_list->As16Bit()); + } else if (len == Uuid::kNumBytes32) { UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES); - UINT32_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid32); - } else if (p_uuid_list->len == LEN_UUID_128) { + UINT32_TO_BE_STREAM(p_out, p_uuid_list->As32Bit()); + } else if (len == Uuid::kNumBytes128) { UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES); - ARRAY_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid128, p_uuid_list->len); + ARRAY_TO_BE_STREAM(p_out, p_uuid_list->To128BitBE(), + (int)Uuid::kNumBytes128); } else { - SDP_TRACE_ERROR("SDP: Passed UUID has invalid length %x", - p_uuid_list->len); + DCHECK(0) << "SDP: Passed UUID has invalid length " << len; } } @@ -874,12 +877,12 @@ static uint8_t* add_attr(uint8_t* p, tSDP_DISCOVERY_DB* p_db, (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 2; p += 2; BE_STREAM_TO_UINT16(p_attr->attr_value.v.u16, p); - p += MAX_UUID_SIZE - 4; + p += Uuid::kNumBytes128 - 4; } else { p_attr->attr_len_type = (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 4; BE_STREAM_TO_UINT32(p_attr->attr_value.v.u32, p); - p += MAX_UUID_SIZE - 4; + p += Uuid::kNumBytes128 - 4; } } else { BE_STREAM_TO_ARRAY(p, p_attr->attr_value.v.array, diff --git a/stack/sdp/sdp_utils.cc b/stack/sdp/sdp_utils.cc index 720c74672..76dc33a6d 100644 --- a/stack/sdp/sdp_utils.cc +++ b/stack/sdp/sdp_utils.cc @@ -39,6 +39,7 @@ #include "btu.h" +using bluetooth::Uuid; static const uint8_t sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; @@ -569,7 +570,7 @@ uint8_t* sdpu_get_len_from_type(uint8_t* p, uint8_t type, uint32_t* p_len) { bool sdpu_is_base_uuid(uint8_t* p_uuid) { uint16_t xx; - for (xx = 4; xx < MAX_UUID_SIZE; xx++) + for (xx = 4; xx < Uuid::kNumBytes128; xx++) if (p_uuid[xx] != sdp_base_uuid[xx]) return (false); /* If here, matched */ @@ -590,8 +591,8 @@ bool sdpu_is_base_uuid(uint8_t* p_uuid) { ******************************************************************************/ bool sdpu_compare_uuid_arrays(uint8_t* p_uuid1, uint32_t len1, uint8_t* p_uuid2, uint16_t len2) { - uint8_t nu1[MAX_UUID_SIZE]; - uint8_t nu2[MAX_UUID_SIZE]; + uint8_t nu1[Uuid::kNumBytes128]; + uint8_t nu2[Uuid::kNumBytes128]; if (((len1 != 2) && (len1 != 4) && (len1 != 16)) || ((len2 != 2) && (len2 != 4) && (len2 != 16))) { @@ -615,15 +616,15 @@ bool sdpu_compare_uuid_arrays(uint8_t* p_uuid1, uint32_t len1, uint8_t* p_uuid2, (p_uuid1[2] == p_uuid2[0]) && (p_uuid1[3] == p_uuid2[1])); } else { /* Normalize UUIDs to 16-byte form, then compare. Len1 must be 16 */ - memcpy(nu1, p_uuid1, MAX_UUID_SIZE); - memcpy(nu2, sdp_base_uuid, MAX_UUID_SIZE); + memcpy(nu1, p_uuid1, Uuid::kNumBytes128); + memcpy(nu2, sdp_base_uuid, Uuid::kNumBytes128); if (len2 == 4) memcpy(nu2, p_uuid2, len2); else if (len2 == 2) memcpy(nu2 + 2, p_uuid2, len2); - return (memcmp(nu1, nu2, MAX_UUID_SIZE) == 0); + return (memcmp(nu1, nu2, Uuid::kNumBytes128) == 0); } } else { /* len2 is greater than len1 */ @@ -633,47 +634,21 @@ bool sdpu_compare_uuid_arrays(uint8_t* p_uuid1, uint32_t len1, uint8_t* p_uuid2, (p_uuid2[2] == p_uuid1[0]) && (p_uuid2[3] == p_uuid1[1])); } else { /* Normalize UUIDs to 16-byte form, then compare. Len1 must be 16 */ - memcpy(nu2, p_uuid2, MAX_UUID_SIZE); - memcpy(nu1, sdp_base_uuid, MAX_UUID_SIZE); + memcpy(nu2, p_uuid2, Uuid::kNumBytes128); + memcpy(nu1, sdp_base_uuid, Uuid::kNumBytes128); if (len1 == 4) memcpy(nu1, p_uuid1, (size_t)len1); else if (len1 == 2) memcpy(nu1 + 2, p_uuid1, (size_t)len1); - return (memcmp(nu1, nu2, MAX_UUID_SIZE) == 0); + return (memcmp(nu1, nu2, Uuid::kNumBytes128) == 0); } } } /******************************************************************************* * - * Function sdpu_compare_bt_uuids - * - * Description This function compares 2 BT UUID structures. - * - * NOTE it is assumed that BT UUID structures are compressed to the - * smallest possible UUIDs (by removing the base SDP UUID) - * - * Returns true if matched, else false - * - ******************************************************************************/ -bool sdpu_compare_bt_uuids(tBT_UUID* p_uuid1, tBT_UUID* p_uuid2) { - /* Lengths must match for BT UUIDs to match */ - if (p_uuid1->len == p_uuid2->len) { - if (p_uuid1->len == 2) - return (p_uuid1->uu.uuid16 == p_uuid2->uu.uuid16); - else if (p_uuid1->len == 4) - return (p_uuid1->uu.uuid32 == p_uuid2->uu.uuid32); - else if (!memcmp(p_uuid1->uu.uuid128, p_uuid2->uu.uuid128, 16)) - return (true); - } - - return (false); -} - -/******************************************************************************* - * * Function sdpu_compare_uuid_with_attr * * Description This function compares a BT UUID structure with the UUID in @@ -688,18 +663,12 @@ bool sdpu_compare_bt_uuids(tBT_UUID* p_uuid1, tBT_UUID* p_uuid2) { * Returns true if matched, else false * ******************************************************************************/ -bool sdpu_compare_uuid_with_attr(tBT_UUID* p_btuuid, tSDP_DISC_ATTR* p_attr) { - uint16_t attr_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type); - - /* Since both UUIDs are compressed, lengths must match */ - if (p_btuuid->len != attr_len) return (false); - - if (p_btuuid->len == 2) - return (bool)(p_btuuid->uu.uuid16 == p_attr->attr_value.v.u16); - else if (p_btuuid->len == 4) - return (bool)(p_btuuid->uu.uuid32 == p_attr->attr_value.v.u32); - else if (!memcmp(p_btuuid->uu.uuid128, (void*)p_attr->attr_value.v.array, - MAX_UUID_SIZE)) +bool sdpu_compare_uuid_with_attr(const Uuid& uuid, tSDP_DISC_ATTR* p_attr) { + int len = uuid.GetShortestRepresentationSize(); + if (len == 2) return uuid.As16Bit() == p_attr->attr_value.v.u16; + if (len == 4) return uuid.As32Bit() == p_attr->attr_value.v.u32; + if (memcmp(uuid.To128BitBE().data(), (void*)p_attr->attr_value.v.array, + Uuid::kNumBytes128) == 0) return (true); return (false); @@ -903,25 +872,3 @@ uint8_t* sdpu_build_partial_attrib_entry(uint8_t* p_out, tSDP_ATTRIBUTE* p_attr, osi_free(p_attr_buff); return p_out; } - -/******************************************************************************* - * - * Function sdpu_uuid16_to_uuid128 - * - * Description This function converts UUID-16 to UUID-128 by including the - * base UUID - * - * uuid16: 2-byte UUID - * p_uuid128: Expanded 128-bit UUID - * - * Returns None - * - ******************************************************************************/ -void sdpu_uuid16_to_uuid128(uint16_t uuid16, uint8_t* p_uuid128) { - uint16_t uuid16_bo; - memset(p_uuid128, 0, 16); - - memcpy(p_uuid128, sdp_base_uuid, MAX_UUID_SIZE); - uuid16_bo = ntohs(uuid16); - memcpy(p_uuid128 + 2, &uuid16_bo, sizeof(uint16_t)); -} diff --git a/stack/sdp/sdpint.h b/stack/sdp/sdpint.h index 3a046b693..820c91492 100644 --- a/stack/sdp/sdpint.h +++ b/stack/sdp/sdpint.h @@ -25,6 +25,7 @@ #ifndef SDP_INT_H #define SDP_INT_H +#include "bluetooth/uuid.h" #include "bt_target.h" #include "l2c_api.h" #include "osi/include/alarm.h" @@ -65,7 +66,7 @@ #define SDP_DEVICE_NOTI_FLAG 0x03 /* Define the Protocol Data Unit (PDU) types. -*/ + */ #define SDP_PDU_ERROR_RESPONSE 0x01 #define SDP_PDU_SERVICE_SEARCH_REQ 0x02 #define SDP_PDU_SERVICE_SEARCH_RSP 0x03 @@ -88,7 +89,7 @@ /* Internal UUID sequence representation */ typedef struct { uint16_t len; - uint8_t value[MAX_UUID_SIZE]; + uint8_t value[bluetooth::Uuid::kNumBytes128]; } tUID_ENT; typedef struct { @@ -229,7 +230,7 @@ extern uint16_t sdp_set_max_attr_list_size(uint16_t max_size); #endif /* Functions provided by sdp_conn.cc -*/ + */ extern void sdp_conn_rcv_l2e_conn_ind(BT_HDR* p_msg); extern void sdp_conn_rcv_l2e_conn_cfm(BT_HDR* p_msg); extern void sdp_conn_rcv_l2e_disc(BT_HDR* p_msg); @@ -244,7 +245,7 @@ extern void sdp_conn_timer_timeout(void* data); extern tCONN_CB* sdp_conn_originate(const RawAddress& p_bd_addr); /* Functions provided by sdp_utils.cc -*/ + */ extern tCONN_CB* sdpu_find_ccb_by_cid(uint16_t cid); extern tCONN_CB* sdpu_find_ccb_by_db(tSDP_DISCOVERY_DB* p_db); extern tCONN_CB* sdpu_allocate_ccb(void); @@ -266,8 +267,7 @@ extern uint8_t* sdpu_get_len_from_type(uint8_t* p, uint8_t type, extern bool sdpu_is_base_uuid(uint8_t* p_uuid); extern bool sdpu_compare_uuid_arrays(uint8_t* p_uuid1, uint32_t len1, uint8_t* p_uuid2, uint16_t len2); -extern bool sdpu_compare_bt_uuids(tBT_UUID* p_uuid1, tBT_UUID* p_uuid2); -extern bool sdpu_compare_uuid_with_attr(tBT_UUID* p_btuuid, +extern bool sdpu_compare_uuid_with_attr(const bluetooth::Uuid& uuid, tSDP_DISC_ATTR* p_attr); extern void sdpu_sort_attr_list(uint16_t num_attr, tSDP_DISCOVERY_DB* p_db); @@ -281,7 +281,7 @@ extern uint8_t* sdpu_build_partial_attrib_entry(uint8_t* p_out, uint16_t len, uint16_t* offset); /* Functions provided by sdp_db.cc -*/ + */ extern tSDP_RECORD* sdp_db_service_search(tSDP_RECORD* p_rec, tSDP_UUID_SEQ* p_seq); extern tSDP_RECORD* sdp_db_find_record(uint32_t handle); @@ -290,7 +290,7 @@ extern tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(tSDP_RECORD* p_rec, uint16_t end_attr); /* Functions provided by sdp_server.cc -*/ + */ #if (SDP_SERVER_ENABLED == TRUE) extern void sdp_server_handle_client_req(tCONN_CB* p_ccb, BT_HDR* p_msg); #else @@ -298,7 +298,7 @@ extern void sdp_server_handle_client_req(tCONN_CB* p_ccb, BT_HDR* p_msg); #endif /* Functions provided by sdp_discovery.cc -*/ + */ extern void sdp_disc_connected(tCONN_CB* p_ccb); extern void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg); diff --git a/stack/srvc/srvc_battery.cc b/stack/srvc/srvc_battery.cc index 2d9fe18e5..9bae14b95 100644 --- a/stack/srvc/srvc_battery.cc +++ b/stack/srvc/srvc_battery.cc @@ -18,7 +18,6 @@ #include "bt_target.h" #include "bt_utils.h" -#include "btcore/include/uuid.h" #include "gatt_api.h" #include "gatt_int.h" #include "osi/include/osi.h" @@ -185,13 +184,13 @@ uint16_t Battery_Instantiate(uint8_t app_id, tBA_REG_INFO* p_reg_info) { btgatt_db_element_t service[BA_MAX_ATTR_NUM] = {}; - bt_uuid_t service_uuid; - uuid_128_from_16(&service_uuid, UUID_SERVCLASS_BATTERY); + bluetooth::Uuid service_uuid = + bluetooth::Uuid::From16Bit(UUID_SERVCLASS_BATTERY); service[0].type = /* p_reg_info->is_pri */ BTGATT_DB_PRIMARY_SERVICE; service[0].uuid = service_uuid; - bt_uuid_t char_uuid; - uuid_128_from_16(&char_uuid, GATT_UUID_BATTERY_LEVEL); + bluetooth::Uuid char_uuid = + bluetooth::Uuid::From16Bit(GATT_UUID_BATTERY_LEVEL); service[1].type = BTGATT_DB_CHARACTERISTIC; service[1].uuid = char_uuid; service[1].properties = GATT_CHAR_PROP_BIT_READ; @@ -200,8 +199,8 @@ uint16_t Battery_Instantiate(uint8_t app_id, tBA_REG_INFO* p_reg_info) { int i = 2; if (p_reg_info->ba_level_descr & BA_LEVEL_NOTIFY) { - bt_uuid_t desc_uuid; - uuid_128_from_16(&desc_uuid, GATT_UUID_CHAR_CLIENT_CONFIG); + bluetooth::Uuid desc_uuid = + bluetooth::Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG); service[i].type = BTGATT_DB_DESCRIPTOR; service[i].uuid = desc_uuid; @@ -211,8 +210,8 @@ uint16_t Battery_Instantiate(uint8_t app_id, tBA_REG_INFO* p_reg_info) { /* need presentation format descriptor? */ if (p_reg_info->ba_level_descr & BA_LEVEL_PRE_FMT) { - bt_uuid_t desc_uuid; - uuid_128_from_16(&desc_uuid, GATT_UUID_CHAR_PRESENT_FORMAT); + bluetooth::Uuid desc_uuid = + bluetooth::Uuid::From16Bit(GATT_UUID_CHAR_PRESENT_FORMAT); service[i].type = BTGATT_DB_DESCRIPTOR; service[i].uuid = desc_uuid; @@ -222,8 +221,8 @@ uint16_t Battery_Instantiate(uint8_t app_id, tBA_REG_INFO* p_reg_info) { /* need presentation format descriptor? */ if (p_reg_info->ba_level_descr & BA_LEVEL_RPT_REF) { - bt_uuid_t desc_uuid; - uuid_128_from_16(&desc_uuid, GATT_UUID_RPT_REF_DESCR); + bluetooth::Uuid desc_uuid = + bluetooth::Uuid::From16Bit(GATT_UUID_RPT_REF_DESCR); service[i].type = BTGATT_DB_DESCRIPTOR; service[i].uuid = desc_uuid; diff --git a/stack/srvc/srvc_dis.cc b/stack/srvc/srvc_dis.cc index 85de4b49d..399798402 100644 --- a/stack/srvc/srvc_dis.cc +++ b/stack/srvc/srvc_dis.cc @@ -20,7 +20,6 @@ #include "bt_target.h" #include "bt_utils.h" -#include "btcore/include/uuid.h" #include "gatt_api.h" #include "gatt_int.h" #include "osi/include/log.h" @@ -232,7 +231,6 @@ bool dis_gatt_c_read_dis_req(uint16_t conn_id) { memset(¶m, 0, sizeof(tGATT_READ_PARAM)); - param.service.uuid.len = LEN_UUID_16; param.service.s_handle = 1; param.service.e_handle = 0xFFFF; param.service.auth_req = 0; @@ -240,13 +238,14 @@ bool dis_gatt_c_read_dis_req(uint16_t conn_id) { while (dis_cb.dis_read_uuid_idx < DIS_MAX_CHAR_NUM) { if (dis_uuid_to_attr(dis_attr_uuid[dis_cb.dis_read_uuid_idx]) & dis_cb.request_mask) { - param.service.uuid.uu.uuid16 = dis_attr_uuid[dis_cb.dis_read_uuid_idx]; + param.service.uuid = + bluetooth::Uuid::From16Bit(dis_attr_uuid[dis_cb.dis_read_uuid_idx]); if (GATTC_Read(conn_id, GATT_READ_BY_TYPE, ¶m) == GATT_SUCCESS) return true; - LOG(ERROR) << "Read DISInfo: 0x" << std::hex - << param.service.uuid.uu.uuid16 << "GATT_Read Failed"; + LOG(ERROR) << "Read DISInfo: " << param.service.uuid + << " GATT_Read Failed"; } dis_cb.dis_read_uuid_idx++; @@ -350,16 +349,16 @@ tDIS_STATUS DIS_SrInit(tDIS_ATTR_MASK dis_attr_mask) { btgatt_db_element_t service[DIS_MAX_ATTR_NUM] = {}; - bt_uuid_t svc_uuid; - uuid_128_from_16(&svc_uuid, UUID_SERVCLASS_DEVICE_INFO); + bluetooth::Uuid svc_uuid = + bluetooth::Uuid::From16Bit(UUID_SERVCLASS_DEVICE_INFO); service[0].type = BTGATT_DB_PRIMARY_SERVICE; service[0].uuid = svc_uuid; for (int i = 0; dis_attr_mask != 0 && i < DIS_MAX_CHAR_NUM; i++) { dis_cb.dis_attr[i].uuid = dis_attr_uuid[i]; - bt_uuid_t char_uuid; - uuid_128_from_16(&char_uuid, dis_cb.dis_attr[i].uuid); + bluetooth::Uuid char_uuid = + bluetooth::Uuid::From16Bit(dis_cb.dis_attr[i].uuid); /* index 0 is service, so characteristics start from 1 */ service[i + 1].type = BTGATT_DB_CHARACTERISTIC; service[i + 1].uuid = char_uuid; diff --git a/stack/srvc/srvc_eng.cc b/stack/srvc/srvc_eng.cc index f054f6760..23fb8faf6 100644 --- a/stack/srvc/srvc_eng.cc +++ b/stack/srvc/srvc_eng.cc @@ -402,7 +402,6 @@ void srvc_eng_release_channel(uint16_t conn_id) { * ******************************************************************************/ tGATT_STATUS srvc_eng_init(void) { - tBT_UUID app_uuid = {LEN_UUID_16, {UUID_SERVCLASS_DEVICE_INFO}}; if (srvc_eng_cb.enabled) { LOG(ERROR) << "DIS already initalized"; @@ -410,7 +409,9 @@ tGATT_STATUS srvc_eng_init(void) { memset(&srvc_eng_cb, 0, sizeof(tSRVC_ENG_CB)); /* Create a GATT profile service */ - srvc_eng_cb.gatt_if = GATT_Register(&app_uuid, &srvc_gatt_cback); + bluetooth::Uuid app_uuid = + bluetooth::Uuid::From16Bit(UUID_SERVCLASS_DEVICE_INFO); + srvc_eng_cb.gatt_if = GATT_Register(app_uuid, &srvc_gatt_cback); GATT_StartIf(srvc_eng_cb.gatt_if); VLOG(1) << "Srvc_Init: gatt_if=" << +srvc_eng_cb.gatt_if; diff --git a/test/rootcanal/Android.bp b/test/rootcanal/Android.bp index faa0e7ce4..e5d3ac5bb 100644 --- a/test/rootcanal/Android.bp +++ b/test/rootcanal/Android.bp @@ -82,6 +82,7 @@ cc_library_shared { "-DHAS_NO_BDROID_BUILDCFG", ], static_libs: [ + "libbluetooth-types", "android.hardware.bluetooth-async", "android.hardware.bluetooth-hci", "libbt-rootcanal", diff --git a/test/suite/gatt/gatt_test.cc b/test/suite/gatt/gatt_test.cc index 8f3ff611d..757404fd3 100644 --- a/test/suite/gatt/gatt_test.cc +++ b/test/suite/gatt/gatt_test.cc @@ -91,7 +91,7 @@ const btgatt_server_interface_t* GattTest::gatt_server_interface() { void GattTest::RegisterClientCallback( bluetooth::hal::BluetoothGattInterface* /* unused */, int status, - int clientIf, const bt_uuid_t& app_uuid) { + int clientIf, const bluetooth::Uuid& app_uuid) { status_ = status; client_interface_id_ = clientIf; semaphore_post(register_client_callback_sem_); @@ -106,7 +106,7 @@ void GattTest::ScanResultCallback( // GATT server callbacks void GattTest::RegisterServerCallback( bluetooth::hal::BluetoothGattInterface* /* unused */, int status, - int server_if, const bt_uuid_t& uuid) { + int server_if, const bluetooth::Uuid& uuid) { status_ = status; server_interface_id_ = server_if; semaphore_post(register_server_callback_sem_); diff --git a/test/suite/gatt/gatt_test.h b/test/suite/gatt/gatt_test.h index 29e0c24d1..bf4098ba9 100644 --- a/test/suite/gatt/gatt_test.h +++ b/test/suite/gatt/gatt_test.h @@ -59,7 +59,7 @@ class GattTest : public BluetoothTest, // bluetooth::hal::BluetoothGattInterface::ClientObserver overrides void RegisterClientCallback( bluetooth::hal::BluetoothGattInterface* /* unused */, int status, - int clientIf, const bt_uuid_t& app_uuid) override; + int clientIf, const bluetooth::Uuid& app_uuid) override; void ScanResultCallback(bluetooth::hal::BluetoothGattInterface* /* unused */, const RawAddress& bda, int rssi, std::vector adv_data) override; @@ -67,7 +67,7 @@ class GattTest : public BluetoothTest, // bluetooth::hal::BluetoothGattInterface::ServerObserver overrides void RegisterServerCallback( bluetooth::hal::BluetoothGattInterface* /* unused */, int status, - int server_if, const bt_uuid_t& uuid) override; + int server_if, const bluetooth::Uuid& uuid) override; void ServiceAddedCallback( bluetooth::hal::BluetoothGattInterface* /* unused */, int status, int server_if, std::vector service) override; diff --git a/test/suite/gatt/gatt_unittest.cc b/test/suite/gatt/gatt_unittest.cc index bc4c29e89..9d44ae3a3 100644 --- a/test/suite/gatt/gatt_unittest.cc +++ b/test/suite/gatt/gatt_unittest.cc @@ -22,25 +22,11 @@ #include "gatt/gatt_test.h" -#define DEFAULT_RANDOM_SEED 42 - -namespace { - -static void create_random_uuid(bt_uuid_t* uuid, int seed) { - srand(seed < 0 ? time(NULL) : seed); - for (int i = 0; i < 16; ++i) { - uuid->uu[i] = (uint8_t)(rand() % 256); - } -} - -} // namespace - namespace bttest { TEST_F(GattTest, GattClientRegister) { // Registers gatt client. - bt_uuid_t gatt_client_uuid; - create_random_uuid(&gatt_client_uuid, DEFAULT_RANDOM_SEED); + bluetooth::Uuid gatt_client_uuid = bluetooth::Uuid::GetRandom(); gatt_client_interface()->register_client(gatt_client_uuid); semaphore_wait(register_client_callback_sem_); EXPECT_TRUE(status() == BT_STATUS_SUCCESS) @@ -52,8 +38,7 @@ TEST_F(GattTest, GattClientRegister) { TEST_F(GattTest, GattServerRegister) { // Registers gatt server. - bt_uuid_t gatt_server_uuid; - create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED); + bluetooth::Uuid gatt_server_uuid = bluetooth::Uuid::GetRandom(); gatt_server_interface()->register_server(gatt_server_uuid); semaphore_wait(register_server_callback_sem_); EXPECT_TRUE(status() == BT_STATUS_SUCCESS) @@ -65,24 +50,20 @@ TEST_F(GattTest, GattServerRegister) { TEST_F(GattTest, GattServerBuild) { // Registers gatt server. - bt_uuid_t gatt_server_uuid; - create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED); + bluetooth::Uuid gatt_server_uuid = bluetooth::Uuid::GetRandom(); gatt_server_interface()->register_server(gatt_server_uuid); semaphore_wait(register_server_callback_sem_); EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error registering GATT server app callback."; // Service UUID. - bt_uuid_t srvc_uuid; - create_random_uuid(&srvc_uuid, -1); + bluetooth::Uuid srvc_uuid = bluetooth::Uuid::GetRandom(); // Characteristics UUID. - bt_uuid_t char_uuid; - create_random_uuid(&char_uuid, -1); + bluetooth::Uuid char_uuid = bluetooth::Uuid::GetRandom(); // Descriptor UUID. - bt_uuid_t desc_uuid; - create_random_uuid(&desc_uuid, -1); + bluetooth::Uuid desc_uuid = bluetooth::Uuid::GetRandom(); // Adds service. int server_if = server_interface_id(); diff --git a/test/suite/rfcomm/rfcomm_test.cc b/test/suite/rfcomm/rfcomm_test.cc index 194320d2c..74404c1cb 100644 --- a/test/suite/rfcomm/rfcomm_test.cc +++ b/test/suite/rfcomm/rfcomm_test.cc @@ -19,13 +19,11 @@ #include "rfcomm/rfcomm_test.h" #include "adapter/bluetooth_test.h" -#include "btcore/include/uuid.h" +using bluetooth::Uuid; namespace bttest { -const bt_uuid_t RFCommTest::HFP_UUID = {{0x00, 0x00, 0x11, 0x1E, 0x00, 0x00, - 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, - 0x5F, 0x9B, 0x34, 0xFB}}; +const Uuid RFCommTest::HFP_UUID = Uuid::From16Bit(0x111E); void RFCommTest::SetUp() { BluetoothTest::SetUp(); @@ -40,7 +38,6 @@ void RFCommTest::SetUp() { // Find a bonded device that supports HFP bt_remote_bdaddr_ = RawAddress::kEmpty; - char value[1280]; bt_property_t* bonded_devices_prop = GetProperty(BT_PROPERTY_ADAPTER_BONDED_DEVICES); @@ -55,12 +52,11 @@ void RFCommTest::SetUp() { bt_property_t* uuid_prop = GetRemoteDeviceProperty(&devices[i], BT_PROPERTY_UUIDS); if (uuid_prop == nullptr) continue; - bt_uuid_t* uuids = (bt_uuid_t*)uuid_prop->val; - int num_uuids = uuid_prop->len / sizeof(bt_uuid_t); + Uuid* uuids = reinterpret_cast(uuid_prop->val); + int num_uuids = uuid_prop->len / sizeof(Uuid); for (int j = 0; j < num_uuids; j++) { - uuid_to_string(&uuids[j], (uuid_string_t*)value); - if (!memcmp(uuids + j, &HFP_UUID, sizeof(bt_uuid_t))) { + if (!memcmp(uuids + j, &HFP_UUID, sizeof(Uuid))) { bt_remote_bdaddr_ = *(devices + i); break; } diff --git a/test/suite/rfcomm/rfcomm_test.h b/test/suite/rfcomm/rfcomm_test.h index 03d273076..49b1f0157 100644 --- a/test/suite/rfcomm/rfcomm_test.h +++ b/test/suite/rfcomm/rfcomm_test.h @@ -38,7 +38,7 @@ class RFCommTest : public BluetoothTest { RawAddress bt_remote_bdaddr_; - static const bt_uuid_t HFP_UUID; + static const bluetooth::Uuid HFP_UUID; private: const btsock_interface_t* socket_interface_; diff --git a/test/suite/rfcomm/rfcomm_unittest.cc b/test/suite/rfcomm/rfcomm_unittest.cc index eabe73c92..82c617823 100644 --- a/test/suite/rfcomm/rfcomm_unittest.cc +++ b/test/suite/rfcomm/rfcomm_unittest.cc @@ -34,8 +34,7 @@ TEST_F(RFCommTest, RfcommConnectPairedDevice) { size_t len = 0; error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM, - (const uint8_t*)&HFP_UUID, 0, &fd, 0, - getuid()); + &HFP_UUID, 0, &fd, 0, getuid()); EXPECT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: " << error; EXPECT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd"; @@ -76,8 +75,7 @@ TEST_F(RFCommTest, RfcommRepeatedConnectPairedDevice) { size_t len = 0; error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM, - (const uint8_t*)&HFP_UUID, 0, &fd, 0, - getuid()); + &HFP_UUID, 0, &fd, 0, getuid()); ASSERT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: " << error; ASSERT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd"; diff --git a/tools/bdtool/adapter.c b/tools/bdtool/adapter.c index c7067fcf6..7a789a605 100644 --- a/tools/bdtool/adapter.c +++ b/tools/bdtool/adapter.c @@ -205,7 +205,7 @@ static void parse_properties(int num_properties, bt_property_t* property) { case BT_PROPERTY_UUIDS: { size_t num_uuid; - const bt_uuid_t* uuid = property_as_uuids(property, &num_uuid); + const Uuid* uuid = property_as_uuids(property, &num_uuid); if (uuid) { for (size_t i = 0; i < num_uuid; i++) { fprintf(stdout, " uuid:%zd: ", i); diff --git a/tools/bdtool/bdtool.c b/tools/bdtool/bdtool.c index d37d3d35b..d491d8090 100644 --- a/tools/bdtool/bdtool.c +++ b/tools/bdtool/bdtool.c @@ -26,12 +26,12 @@ #include "test/suite/support/callbacks.h" #include "test/suite/support/hal.h" -static const bt_uuid_t HFP_UUID = {{0x00, 0x00, 0x11, 0x1E, 0x00, 0x00, 0x10, - 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, - 0x34, 0xFB}}; -static const bt_uuid_t HFP_AG_UUID = {{0x00, 0x00, 0x11, 0x1F, 0x00, 0x00, 0x10, - 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, - 0x34, 0xFB}}; +static const Uuid HFP_UUID = + Uuid::From128BitBE({{0x00, 0x00, 0x11, 0x1E, 0x00, 0x00, 0x10, 0x00, 0x80, + 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}}); +static const Uuid HFP_AG_UUID = + Uuid::From128BitBE({{0x00, 0x00, 0x11, 0x1F, 0x00, 0x00, 0x10, 0x00, 0x80, + 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}}); const bt_interface_t* bt_interface; diff --git a/types/Android.bp b/types/Android.bp index 2f17fbb90..88d37eea1 100644 --- a/types/Android.bp +++ b/types/Android.bp @@ -17,6 +17,7 @@ cc_library { host_supported: true, srcs: [ "raw_address.cc", + "bluetooth/uuid.cc", ], header_libs: ["libbluetooth-types-header"], export_header_lib_headers: ["libbluetooth-types-header"], @@ -30,5 +31,6 @@ cc_test { host_supported: true, srcs: [ "test/raw_address_unittest.cc", + "test/bluetooth/uuid_unittest.cc", ], } diff --git a/types/bluetooth/uuid.cc b/types/bluetooth/uuid.cc new file mode 100644 index 000000000..749124c9f --- /dev/null +++ b/types/bluetooth/uuid.cc @@ -0,0 +1,170 @@ +/****************************************************************************** + * + * Copyright (C) 2017 The Android Open Source Project + * + * 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 "uuid.h" + +#include +#include +#include + +namespace bluetooth { + +static_assert(sizeof(Uuid) == 16, "Uuid must be 16 bytes long!"); + +using UUID128Bit = Uuid::UUID128Bit; + +Uuid Uuid::kEmpty = Uuid::From128BitBE(UUID128Bit{{0x00}}); +Uuid Uuid::kBase = Uuid::From128BitBE( + UUID128Bit{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, + 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}}); + +size_t Uuid::GetShortestRepresentationSize() const { + if (memcmp(uu.data() + kNumBytes32, kBase.uu.data() + kNumBytes32, + kNumBytes128 - kNumBytes32) != 0) { + return kNumBytes128; + } + + if (uu[0] == 0 && uu[1] == 0) return kNumBytes16; + + return kNumBytes32; +} + +bool Uuid::Is16Bit() const { + return GetShortestRepresentationSize() == kNumBytes16; +} + +uint16_t Uuid::As16Bit() const { return (((uint16_t)uu[2]) << 8) + uu[3]; } + +uint32_t Uuid::As32Bit() const { + return (((uint32_t)uu[0]) << 24) + (((uint32_t)uu[1]) << 16) + + (((uint32_t)uu[2]) << 8) + uu[3]; +} + +Uuid Uuid::FromString(const std::string& uuid, bool* is_valid) { + if (is_valid) *is_valid = false; + Uuid ret = kBase; + + if (uuid.empty()) return ret; + + uint8_t* p = ret.uu.data(); + if (uuid.size() == kString128BitLen) { + char c; + int rc = + sscanf(uuid.c_str(), + "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx" + "-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%c", + &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7], &p[8], + &p[9], &p[10], &p[11], &p[12], &p[13], &p[14], &p[15], &c); + if (rc != 16) return ret; + if (c != '\0') return ret; + + if (is_valid) *is_valid = true; + } else if (uuid.size() == 8) { + char c; + int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%02hhx%02hhx%c", &p[0], &p[1], + &p[2], &p[3], &c); + if (rc != 4) return ret; + if (c != '\0') return ret; + + if (is_valid) *is_valid = true; + } else if (uuid.size() == 4) { + char c; + int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%c", &p[2], &p[3], &c); + if (rc != 2) return ret; + if (c != '\0') return ret; + + if (is_valid) *is_valid = true; + } + + return ret; +} + +Uuid Uuid::From16Bit(uint16_t uuid16) { + Uuid u = kBase; + + u.uu[2] = (uint8_t)((0xFF00 & uuid16) >> 8); + u.uu[3] = (uint8_t)(0x00FF & uuid16); + return u; +} + +Uuid Uuid::From32Bit(uint32_t uuid32) { + Uuid u = kBase; + + u.uu[0] = (uint8_t)((0xFF000000 & uuid32) >> 24); + u.uu[1] = (uint8_t)((0x00FF0000 & uuid32) >> 16); + u.uu[2] = (uint8_t)((0x0000FF00 & uuid32) >> 8); + u.uu[3] = (uint8_t)(0x000000FF & uuid32); + return u; +} + +Uuid Uuid::From128BitBE(const UUID128Bit& uuid) { + Uuid u; + u.uu = uuid; + return u; +} + +Uuid Uuid::From128BitBE(const uint8_t* uuid) { + UUID128Bit tmp; + memcpy(tmp.data(), uuid, kNumBytes128); + return From128BitBE(tmp); +} + +Uuid Uuid::From128BitLE(const UUID128Bit& uuid) { + Uuid u; + std::reverse_copy(uuid.data(), uuid.data() + kNumBytes128, u.uu.begin()); + return u; +} + +Uuid Uuid::From128BitLE(const uint8_t* uuid) { + UUID128Bit tmp; + memcpy(tmp.data(), uuid, kNumBytes128); + return From128BitLE(tmp); +} + +const UUID128Bit Uuid::To128BitLE() const { + UUID128Bit le; + std::reverse_copy(uu.data(), uu.data() + kNumBytes128, le.begin()); + return le; +} + +const UUID128Bit& Uuid::To128BitBE() const { return uu; } + +Uuid Uuid::GetRandom() { + Uuid uuid; + base::RandBytes(uuid.uu.data(), uuid.uu.size()); + return uuid; +} + +bool Uuid::IsEmpty() const { return *this == kEmpty; } + +bool Uuid::operator<(const Uuid& rhs) const { + return std::lexicographical_compare(uu.begin(), uu.end(), rhs.uu.begin(), + rhs.uu.end()); +} + +bool Uuid::operator==(const Uuid& rhs) const { return uu == rhs.uu; } + +bool Uuid::operator!=(const Uuid& rhs) const { return uu != rhs.uu; } + +std::string Uuid::ToString() const { + return base::StringPrintf( + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], uu[8], uu[9], + uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]); +} +} // namespace bluetooth \ No newline at end of file diff --git a/types/bluetooth/uuid.h b/types/bluetooth/uuid.h new file mode 100644 index 000000000..70a6d60bd --- /dev/null +++ b/types/bluetooth/uuid.h @@ -0,0 +1,134 @@ +/****************************************************************************** + * + * Copyright (C) 2017 The Android Open Source Project + * + * 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. + * + ******************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace bluetooth { + +// This class is representing Bluetooth UUIDs across whole stack. +// Here are some general endianness rules: +// 1. UUID is internally kept as as Big Endian. +// 2. Bytes representing UUID coming from upper layers, Java or Binder, are Big +// Endian. +// 3. Bytes representing UUID coming from lower layer, HCI packets, are Little +// Endian. +// 4. UUID in storage is always string. +class Uuid final { + public: + static constexpr size_t kNumBytes128 = 16; + static constexpr size_t kNumBytes32 = 4; + static constexpr size_t kNumBytes16 = 2; + + static constexpr size_t kString128BitLen = 36; + + static Uuid kEmpty; // 00000000-0000-0000-0000-000000000000 + static Uuid kBase; // 00000000-0000-1000-8000-00805f9b34fb + + using UUID128Bit = std::array; + + // Creates and returns a random 128-bit UUID. + static Uuid GetRandom(); + + // Returns the shortest possible representation of this UUID in bytes. Either + // kNumBytes16, kNumBytes32, or kNumBytes128 + size_t GetShortestRepresentationSize() const; + + // Returns true if this UUID can be represented as 16 bit. + bool Is16Bit() const; + + // Returns 16 bit Little Endian representation of this UUID. Use + // GetShortestRepresentationSize() or Is16Bit() before using this method. + uint16_t As16Bit() const; + + // Returns 32 bit Little Endian representation of this UUID. Use + // GetShortestRepresentationSize() before using this method. + uint32_t As32Bit() const; + + // Converts string representing 128, 32, or 16 bit UUID in + // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx, or xxxx format to UUID. If + // set, optional is_valid parameter will be set to true if conversion is + // successfull, false otherwise. + static Uuid FromString(const std::string& uuid, bool* is_valid = nullptr); + + // Converts 16bit Little Endian representation of UUID to UUID + static Uuid From16Bit(uint16_t uuid16bit); + + // Converts 32bit Little Endian representation of UUID to UUID + static Uuid From32Bit(uint32_t uuid32bit); + + // Converts 128 bit Big Endian array representing UUID to UUID. + static Uuid From128BitBE(const UUID128Bit& uuid); + + // Converts 128 bit Big Endian array representing UUID to UUID. |uuid| points + // to beginning of array. + static Uuid From128BitBE(const uint8_t* uuid); + + // Converts 128 bit Little Endian array representing UUID to UUID. + static Uuid From128BitLE(const UUID128Bit& uuid); + + // Converts 128 bit Little Endian array representing UUID to UUID. |uuid| + // points to beginning of array. + static Uuid From128BitLE(const uint8_t* uuid); + + // Returns 128 bit Little Endian representation of this UUID + const UUID128Bit To128BitLE() const; + + // Returns 128 bit Big Endian representation of this UUID + const UUID128Bit& To128BitBE() const; + + // Returns string representing this UUID in + // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format, lowercase. + std::string ToString() const; + + // Returns true if this UUID is equal to kEmpty + bool IsEmpty() const; + + bool operator<(const Uuid& rhs) const; + bool operator==(const Uuid& rhs) const; + bool operator!=(const Uuid& rhs) const; + + private: + // Network-byte-ordered ID (Big Endian). + UUID128Bit uu; +}; +} // namespace bluetooth + +inline std::ostream& operator<<(std::ostream& os, const bluetooth::Uuid& a) { + os << a.ToString(); + return os; +} + +// Custom std::hash specialization so that bluetooth::UUID can be used as a key +// in std::unordered_map. +namespace std { + +template <> +struct hash { + std::size_t operator()(const bluetooth::Uuid& key) const { + const auto& uuid_bytes = key.To128BitBE(); + std::hash hash_fn; + return hash_fn(std::string(reinterpret_cast(uuid_bytes.data()), + uuid_bytes.size())); + } +}; + +} // namespace std diff --git a/types/test/bluetooth/uuid_unittest.cc b/types/test/bluetooth/uuid_unittest.cc new file mode 100644 index 000000000..a2de1c6a0 --- /dev/null +++ b/types/test/bluetooth/uuid_unittest.cc @@ -0,0 +1,166 @@ +/****************************************************************************** + * + * Copyright (C) 2017 Google, Inc. + * + * 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 + +using bluetooth::Uuid; + +static const Uuid ONES = Uuid::From128BitBE( + Uuid::UUID128Bit{{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}}); + +static const Uuid SEQUENTIAL = Uuid::From128BitBE( + Uuid::UUID128Bit{{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xab, + 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89}}); + +TEST(UuidTest, IsEmpty) { + EXPECT_TRUE(Uuid::kEmpty.IsEmpty()); + EXPECT_FALSE(Uuid::kBase.IsEmpty()); +} + +TEST(UuidTest, GetShortestRepresentationSize) { + EXPECT_TRUE(Uuid::kNumBytes16 == Uuid::kBase.GetShortestRepresentationSize()); + EXPECT_TRUE(Uuid::kNumBytes32 == + Uuid::From32Bit(0x01234567).GetShortestRepresentationSize()); + EXPECT_TRUE(Uuid::kNumBytes128 == + Uuid::kEmpty.GetShortestRepresentationSize()); +} + +TEST(UuidTest, As16Bit) { + // Even though this is is not 16bit UUID, we should be able to get proper bits + EXPECT_EQ((uint16_t)0x1111, ONES.As16Bit()); + EXPECT_EQ((uint16_t)0x4567, SEQUENTIAL.As16Bit()); + EXPECT_EQ((uint16_t)0x0000, Uuid::kBase.As16Bit()); +} + +TEST(UuidTest, As32Bit) { + // Even though this is is not 32bit UUID, we should be able to get proper bits + EXPECT_EQ((uint32_t)0x11111111, ONES.As32Bit()); + EXPECT_EQ((uint32_t)0x01234567, SEQUENTIAL.As32Bit()); + EXPECT_EQ((uint32_t)0x00000000, Uuid::kBase.As32Bit()); + EXPECT_EQ((uint32_t)0x12345678, Uuid::From32Bit(0x12345678).As32Bit()); +} + +TEST(UuidTest, Is16Bit) { + EXPECT_FALSE(ONES.Is16Bit()); + EXPECT_FALSE(SEQUENTIAL.Is16Bit()); + EXPECT_TRUE(Uuid::kBase.Is16Bit()); + EXPECT_TRUE(Uuid::FromString("1ae8").Is16Bit()); +} + +TEST(UuidTest, From16Bit) { + EXPECT_EQ(Uuid::From16Bit(0x0000), Uuid::kBase); + + const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + Uuid uuid = Uuid::From16Bit(0x0001); + EXPECT_TRUE(memcmp(&uuid, u2, sizeof(u2)) == 0); + + const uint8_t u3[] = {0x00, 0x00, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + uuid = Uuid::From16Bit(0x553e); + EXPECT_TRUE(memcmp(&uuid, u3, sizeof(u3)) == 0); + + const uint8_t u4[] = {0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + uuid = Uuid::From16Bit(0xffff); + EXPECT_TRUE(memcmp(&uuid, u4, sizeof(u4)) == 0); +} + +TEST(UuidTest, From32Bit) { + EXPECT_EQ(Uuid::From32Bit(0x00000000), Uuid::kBase); + + const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + Uuid uuid = Uuid::From32Bit(0x00000001); + EXPECT_TRUE(memcmp(&uuid, u2, sizeof(u2)) == 0); + + const uint8_t u3[] = {0x33, 0x44, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + uuid = Uuid::From32Bit(0x3344553e); + EXPECT_TRUE(memcmp(&uuid, u3, sizeof(u3)) == 0); + + const uint8_t u4[] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + uuid = Uuid::From32Bit(0xffffffff); + EXPECT_TRUE(memcmp(&uuid, u4, sizeof(u4)) == 0); +} + +TEST(UuidTest, ToString) { + const std::string UUID_BASE_STR = "00000000-0000-1000-8000-00805f9b34fb"; + const std::string UUID_EMP_STR = "00000000-0000-0000-0000-000000000000"; + const std::string UUID_ONES_STR = "11111111-1111-1111-1111-111111111111"; + const std::string UUID_SEQ_STR = "01234567-89ab-cdef-abcd-ef0123456789"; + + EXPECT_EQ(UUID_BASE_STR, Uuid::kBase.ToString()); + EXPECT_EQ(UUID_EMP_STR, Uuid::kEmpty.ToString()); + EXPECT_EQ(UUID_ONES_STR, ONES.ToString()); + EXPECT_EQ(UUID_SEQ_STR, SEQUENTIAL.ToString()); + + Uuid uuid = Uuid::From32Bit(0x12345678); + EXPECT_EQ("12345678-0000-1000-8000-00805f9b34fb", uuid.ToString()); +} + +TEST(BtifStorageTest, test_string_to_uuid) { + const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, + 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22}; + bool is_valid = false; + Uuid uuid = + Uuid::FromString("e39c6285-867f-4b1d-9db0-35fbd9aebf22", &is_valid); + EXPECT_TRUE(is_valid); + EXPECT_TRUE(memcmp(&uuid, u1, sizeof(u1)) == 0); + + const uint8_t u2[] = {0x00, 0x00, 0x1a, 0xe8, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + is_valid = false; + uuid = Uuid::FromString("1Ae8", &is_valid); + EXPECT_TRUE(is_valid); + EXPECT_TRUE(memcmp(&uuid, u2, sizeof(u2)) == 0); + + const uint8_t u3[] = {0x12, 0x34, 0x11, 0x28, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + is_valid = false; + uuid = Uuid::FromString("12341128", &is_valid); + EXPECT_TRUE(is_valid); + EXPECT_TRUE(memcmp(&uuid, u3, sizeof(u3)) == 0); +} + +TEST(BtifStorageTest, test_string_to_uuid_invalid) { + bool is_valid = false; + Uuid uuid = Uuid::FromString("This is not a UUID", &is_valid); + EXPECT_FALSE(is_valid); + + uuid = Uuid::FromString("11212", &is_valid); + EXPECT_FALSE(is_valid); + + uuid = Uuid::FromString("1121 ", &is_valid); + EXPECT_FALSE(is_valid); + + uuid = Uuid::FromString("AGFE", &is_valid); + EXPECT_FALSE(is_valid); + + uuid = Uuid::FromString("ABFG", &is_valid); + EXPECT_FALSE(is_valid); + + uuid = Uuid::FromString("e39c6285867f14b1d9db035fbd9aebf22", &is_valid); + EXPECT_FALSE(is_valid); + + uuid = Uuid::FromString("12234567-89ab-cdef-abcd-ef01234567ZZ", &is_valid); + EXPECT_FALSE(is_valid); +} -- 2.11.0