1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <bluetooth/log.h>
20
21 #include <queue>
22 #include <string>
23
24 #include "bta/dm/bta_dm_device_search_int.h"
25 #include "bta/include/bta_api.h"
26 #include "bta/sys/bta_sys.h"
27 #include "macros.h"
28 #include "stack/include/sdp_status.h"
29 #include "stack/sdp/sdp_discovery_db.h"
30 #include "types/bluetooth/uuid.h"
31 #include "types/raw_address.h"
32
33 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
34
35 typedef enum : uint16_t {
36 /* service discovery events */
37 BTA_DM_API_DISCOVER_EVT,
38 BTA_DM_SDP_RESULT_EVT,
39 BTA_DM_DISCOVERY_RESULT_EVT,
40 BTA_DM_DISC_CLOSE_TOUT_EVT,
41 } tBTA_DM_DISC_EVT;
42
bta_dm_event_text(const tBTA_DM_DISC_EVT & event)43 inline std::string bta_dm_event_text(const tBTA_DM_DISC_EVT& event) {
44 switch (event) {
45 CASE_RETURN_TEXT(BTA_DM_API_DISCOVER_EVT);
46 CASE_RETURN_TEXT(BTA_DM_SDP_RESULT_EVT);
47 CASE_RETURN_TEXT(BTA_DM_DISCOVERY_RESULT_EVT);
48 CASE_RETURN_TEXT(BTA_DM_DISC_CLOSE_TOUT_EVT);
49 }
50 }
51
52 /* data type for BTA_DM_API_DISCOVER_EVT */
53 typedef struct {
54 RawAddress bd_addr;
55 service_discovery_callbacks cbacks;
56 tBT_TRANSPORT transport;
57 } tBTA_DM_API_DISCOVER;
58
59 typedef struct {
60 RawAddress bd_addr; /* BD address peer device. */
61 bool is_gatt_over_ble;
62 std::vector<bluetooth::Uuid> uuids;
63 std::vector<bluetooth::Uuid> gatt_uuids;
64 tBTA_STATUS result;
65 tHCI_STATUS hci_status;
66 } tBTA_DM_SVC_RES;
67
68 /* data type for BTA_DM_API_DISCOVER_EVT */
69 typedef struct {
70 uint16_t conn_id;
71 } tBTA_DM_TOUT;
72
73 using tBTA_DM_MSG = std::variant<tBTA_DM_API_DISCOVER, tBTA_DM_SVC_RES, tBTA_DM_TOUT>;
74
75 typedef enum { BTA_DM_DISCOVER_IDLE, BTA_DM_DISCOVER_ACTIVE } tBTA_DM_SERVICE_DISCOVERY_STATE;
76
bta_dm_state_text(const tBTA_DM_SERVICE_DISCOVERY_STATE & state)77 inline std::string bta_dm_state_text(const tBTA_DM_SERVICE_DISCOVERY_STATE& state) {
78 switch (state) {
79 CASE_RETURN_TEXT(BTA_DM_DISCOVER_IDLE);
80 CASE_RETURN_TEXT(BTA_DM_DISCOVER_ACTIVE);
81 }
82 }
83
84 #define MAX_DISC_RAW_DATA_BUF (4096)
85
86 typedef struct {
87 RawAddress bd_addr;
88 tBTA_SERVICE_MASK services_to_search;
89 tBTA_SERVICE_MASK services_found;
90
91 uint8_t service_index;
92 uint8_t peer_scn;
93
94 std::array<uint8_t, MAX_DISC_RAW_DATA_BUF> g_disc_raw_data_buf;
95
96 /* sdp_db must be together with sdp_db_buffer*/
97 alignas(tSDP_DISCOVERY_DB) uint8_t sdp_db_buffer[BTA_DM_SDP_DB_SIZE];
98 } tBTA_DM_SDP_STATE;
99
100 typedef struct {
101 service_discovery_callbacks service_search_cbacks;
102 tGATT_IF client_if;
103 std::queue<tBTA_DM_API_DISCOVER> pending_discovery_queue;
104
105 RawAddress peer_bdaddr;
106 uint8_t transports;
107 /* This covers service discovery state - callers of BTA_DmDiscover. That is
108 * initial service discovery after bonding and
109 * BluetoothDevice.fetchUuidsWithSdp(). Responsible for LE GATT Service
110 * Discovery and SDP */
111 tBTA_DM_SERVICE_DISCOVERY_STATE service_discovery_state;
112 std::unique_ptr<tBTA_DM_SDP_STATE> sdp_state;
113
114 tCONN_ID conn_id;
115 alarm_t* gatt_close_timer; /* GATT channel close delay timer */
116 RawAddress pending_close_bda; /* pending GATT channel remote device address */
117 } tBTA_DM_SERVICE_DISCOVERY_CB;
118
119 extern const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[];
120 extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
121
122 void bta_dm_disc_override_sdp_performer_for_testing(
123 base::RepeatingCallback<void(tBTA_DM_SDP_STATE*)> sdp_performer);
124 void bta_dm_disc_override_gatt_performer_for_testing(
125 base::RepeatingCallback<void(const RawAddress&)> test_gatt_performer);
126 void bta_dm_sdp_find_services(tBTA_DM_SDP_STATE* sdp_state);
127 void bta_dm_sdp_result(tSDP_STATUS sdp_result, tBTA_DM_SDP_STATE* sdp_state);
128 void bta_dm_sdp_finished(RawAddress bda, tBTA_STATUS result,
129 std::vector<bluetooth::Uuid> uuids = {},
130 std::vector<bluetooth::Uuid> gatt_uuids = {});
131 void bta_dm_gatt_finished(RawAddress bda, tBTA_STATUS result,
132 std::vector<bluetooth::Uuid> gatt_uuids = {});
133 void bta_dm_sdp_callback(const RawAddress& bd_addr, tSDP_STATUS sdp_status);
134
135 #ifdef TARGET_FLOSS
136 void bta_dm_sdp_received_di(const RawAddress& bd_addr, tSDP_DI_GET_RECORD& di_record);
137 #endif
138
139 namespace std {
140 template <>
141 struct formatter<tBTA_DM_DISC_EVT> : enum_formatter<tBTA_DM_DISC_EVT> {};
142 template <>
143 struct formatter<tBTA_DM_SERVICE_DISCOVERY_STATE>
144 : enum_formatter<tBTA_DM_SERVICE_DISCOVERY_STATE> {};
145 } // namespace std
146
147 namespace bluetooth::legacy::testing {
148
149 tBT_TRANSPORT bta_dm_determine_discovery_transport(const RawAddress& bd_addr);
150 void bta_dm_remote_name_cmpl(const tBTA_DM_REMOTE_NAME& remote_name_msg);
151
152 } // namespace bluetooth::legacy::testing
153