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 <base/strings/stringprintf.h>
20 #include <bluetooth/log.h>
21
22 #include <queue>
23 #include <string>
24
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 using tBTA_DM_MSG = std::variant<tBTA_DM_API_DISCOVER, tBTA_DM_SVC_RES>;
69
70 typedef enum { BTA_DM_DISCOVER_IDLE, BTA_DM_DISCOVER_ACTIVE } tBTA_DM_SERVICE_DISCOVERY_STATE;
71
bta_dm_state_text(const tBTA_DM_SERVICE_DISCOVERY_STATE & state)72 inline std::string bta_dm_state_text(const tBTA_DM_SERVICE_DISCOVERY_STATE& state) {
73 switch (state) {
74 CASE_RETURN_TEXT(BTA_DM_DISCOVER_IDLE);
75 CASE_RETURN_TEXT(BTA_DM_DISCOVER_ACTIVE);
76 }
77 }
78
79 #define MAX_DISC_RAW_DATA_BUF (4096)
80
81 typedef struct {
82 RawAddress bd_addr;
83 tBTA_SERVICE_MASK services_to_search;
84 tBTA_SERVICE_MASK services_found;
85
86 uint8_t service_index;
87 uint8_t peer_scn;
88
89 std::array<uint8_t, MAX_DISC_RAW_DATA_BUF> g_disc_raw_data_buf;
90
91 /* sdp_db must be together with sdp_db_buffer*/
92 alignas(tSDP_DISCOVERY_DB) uint8_t sdp_db_buffer[BTA_DM_SDP_DB_SIZE];
93 } tBTA_DM_SDP_STATE;
94
95 typedef struct {
96 service_discovery_callbacks service_search_cbacks;
97 tGATT_IF client_if;
98 std::queue<tBTA_DM_API_DISCOVER> pending_discovery_queue;
99
100 RawAddress peer_bdaddr;
101 uint8_t transports;
102 /* This covers service discovery state - callers of BTA_DmDiscover. That is
103 * initial service discovery after bonding and
104 * BluetoothDevice.fetchUuidsWithSdp(). Responsible for LE GATT Service
105 * Discovery and SDP */
106 tBTA_DM_SERVICE_DISCOVERY_STATE service_discovery_state;
107 std::unique_ptr<tBTA_DM_SDP_STATE> sdp_state;
108
109 tCONN_ID conn_id;
110 alarm_t* gatt_close_timer; /* GATT channel close delay timer */
111 RawAddress pending_close_bda; /* pending GATT channel remote device address */
112 } tBTA_DM_SERVICE_DISCOVERY_CB;
113
114 extern const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[];
115 extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
116
117 void bta_dm_disc_override_sdp_performer_for_testing(
118 base::RepeatingCallback<void(tBTA_DM_SDP_STATE*)> sdp_performer);
119 void bta_dm_disc_override_gatt_performer_for_testing(
120 base::RepeatingCallback<void(const RawAddress&)> test_gatt_performer);
121 void bta_dm_sdp_find_services(tBTA_DM_SDP_STATE* sdp_state);
122 void bta_dm_sdp_result(tSDP_STATUS sdp_result, tBTA_DM_SDP_STATE* sdp_state);
123 void bta_dm_sdp_finished(RawAddress bda, tBTA_STATUS result,
124 std::vector<bluetooth::Uuid> uuids = {},
125 std::vector<bluetooth::Uuid> gatt_uuids = {});
126 void bta_dm_gatt_finished(RawAddress bda, tBTA_STATUS result,
127 std::vector<bluetooth::Uuid> gatt_uuids = {});
128 void bta_dm_sdp_callback(const RawAddress& bd_addr, tSDP_STATUS sdp_status);
129
130 #ifdef TARGET_FLOSS
131 void bta_dm_sdp_received_di(const RawAddress& bd_addr, tSDP_DI_GET_RECORD& di_record);
132 #endif
133
134 namespace std {
135 template <>
136 struct formatter<tBTA_DM_DISC_EVT> : enum_formatter<tBTA_DM_DISC_EVT> {};
137 template <>
138 struct formatter<tBTA_DM_SERVICE_DISCOVERY_STATE>
139 : enum_formatter<tBTA_DM_SERVICE_DISCOVERY_STATE> {};
140 } // namespace std
141