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