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/btm/neighbor_inquiry.h"
29 #include "stack/include/sdp_status.h"
30 #include "stack/sdp/sdp_discovery_db.h"
31 #include "types/bluetooth/uuid.h"
32 #include "types/raw_address.h"
33 
34 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
35 
36 // TODO: Remove this file after flag separate_service_and_device_discovery rolls
37 // out
38 namespace bta_dm_disc_legacy {
39 
40 /* DM search events */
41 typedef enum : uint16_t {
42   /* DM search API events */
43   BTA_DM_API_SEARCH_EVT,
44   BTA_DM_API_SEARCH_CANCEL_EVT,
45   BTA_DM_API_DISCOVER_EVT,
46   BTA_DM_INQUIRY_CMPL_EVT,
47   BTA_DM_REMT_NAME_EVT,
48   BTA_DM_SDP_RESULT_EVT,
49   BTA_DM_SEARCH_CMPL_EVT,
50   BTA_DM_DISCOVERY_RESULT_EVT,
51   BTA_DM_DISC_CLOSE_TOUT_EVT,
52 } tBTA_DM_EVT;
53 
bta_dm_event_text(const tBTA_DM_EVT & event)54 inline std::string bta_dm_event_text(const tBTA_DM_EVT& event) {
55   switch (event) {
56     CASE_RETURN_TEXT(BTA_DM_API_SEARCH_EVT);
57     CASE_RETURN_TEXT(BTA_DM_API_SEARCH_CANCEL_EVT);
58     CASE_RETURN_TEXT(BTA_DM_API_DISCOVER_EVT);
59     CASE_RETURN_TEXT(BTA_DM_INQUIRY_CMPL_EVT);
60     CASE_RETURN_TEXT(BTA_DM_REMT_NAME_EVT);
61     CASE_RETURN_TEXT(BTA_DM_SDP_RESULT_EVT);
62     CASE_RETURN_TEXT(BTA_DM_SEARCH_CMPL_EVT);
63     CASE_RETURN_TEXT(BTA_DM_DISCOVERY_RESULT_EVT);
64     CASE_RETURN_TEXT(BTA_DM_DISC_CLOSE_TOUT_EVT);
65     default:
66       return base::StringPrintf("UNKNOWN[0x%04x]", event);
67   }
68 }
69 
70 /* data type for BTA_DM_API_SEARCH_EVT */
71 typedef struct {
72   tBTA_DM_SEARCH_CBACK* p_cback;
73 } tBTA_DM_API_SEARCH;
74 
75 /* data type for BTA_DM_API_DISCOVER_EVT */
76 typedef struct {
77   RawAddress bd_addr;
78   service_discovery_callbacks cbacks;
79   tBT_TRANSPORT transport;
80 } tBTA_DM_API_DISCOVER;
81 
82 typedef struct {
83 } tBTA_DM_API_DISCOVERY_CANCEL;
84 
85 typedef struct {
86   RawAddress bd_addr;
87   BD_NAME bd_name; /* Name of peer device. */
88   tHCI_STATUS hci_status;
89 } tBTA_DM_REMOTE_NAME;
90 
91 /* data type for tBTA_DM_DISC_RESULT */
92 typedef struct {
93   tBTA_DM_SEARCH result;
94 } tBTA_DM_DISC_RESULT;
95 
96 /* data type for BTA_DM_INQUIRY_CMPL_EVT */
97 typedef struct {
98   uint8_t num;
99 } tBTA_DM_INQUIRY_CMPL;
100 
101 /* data type for BTA_DM_SDP_RESULT_EVT */
102 typedef struct {
103   tSDP_RESULT sdp_result;
104 } tBTA_DM_SDP_RESULT;
105 
106 typedef struct {
107   bool enable;
108 } tBTA_DM_API_BLE_FEATURE;
109 
110 typedef struct {
111   RawAddress bd_addr;          /* BD address peer device. */
112   tBTA_SERVICE_MASK services;  /* Services found on peer device. */
113   tBT_DEVICE_TYPE device_type; /* device type in case it is BLE device */
114   std::vector<bluetooth::Uuid> uuids;
115   tBTA_STATUS result;
116   tHCI_STATUS hci_status;
117 } tBTA_DM_SVC_RES;
118 
119 using tBTA_DM_MSG =
120     std::variant<tBTA_DM_API_SEARCH, tBTA_DM_API_DISCOVER, tBTA_DM_REMOTE_NAME,
121                  tBTA_DM_DISC_RESULT, tBTA_DM_INQUIRY_CMPL, tBTA_DM_SDP_RESULT,
122                  tBTA_DM_SVC_RES>;
123 
124 /* DM search state */
125 typedef enum {
126 
127   BTA_DM_SEARCH_IDLE,
128   BTA_DM_SEARCH_ACTIVE,
129   BTA_DM_SEARCH_CANCELLING,
130   BTA_DM_DISCOVER_ACTIVE
131 
132 } tBTA_DM_STATE;
133 
bta_dm_state_text(const tBTA_DM_STATE & state)134 inline std::string bta_dm_state_text(const tBTA_DM_STATE& state) {
135   switch (state) {
136     CASE_RETURN_TEXT(BTA_DM_SEARCH_IDLE);
137     CASE_RETURN_TEXT(BTA_DM_SEARCH_ACTIVE);
138     CASE_RETURN_TEXT(BTA_DM_SEARCH_CANCELLING);
139     CASE_RETURN_TEXT(BTA_DM_DISCOVER_ACTIVE);
140     default:
141       return base::StringPrintf("UNKNOWN[%d]", state);
142   }
143 }
144 
145 /* DM search control block */
146 typedef struct {
147   tBTA_DM_SEARCH_CBACK* p_device_search_cback;
148   service_discovery_callbacks service_search_cbacks;
149   tBTM_INQ_INFO* p_btm_inq_info;
150   tBTA_SERVICE_MASK services_to_search;
151   tBTA_SERVICE_MASK services_found;
152   tSDP_DISCOVERY_DB* p_sdp_db;
153   tBTA_DM_STATE state;
154   RawAddress peer_bdaddr;
155   bool name_discover_done;
156   BD_NAME peer_name;
157   alarm_t* search_timer;
158   uint8_t service_index;
159   std::unique_ptr<tBTA_DM_MSG> p_pending_search;
160   std::queue<tBTA_DM_API_DISCOVER> pending_discovery_queue;
161   bool wait_disc;
162   bool sdp_results;
163   bluetooth::Uuid uuid;
164   uint8_t peer_scn;
165   tBT_TRANSPORT transport;
166   tBTA_DM_SEARCH_CBACK* p_csis_scan_cback;
167   tGATT_IF client_if;
168   uint8_t uuid_to_search;
169   bool gatt_disc_active;
170   uint16_t conn_id;
171   alarm_t* gatt_close_timer;    /* GATT channel close delay timer */
172   RawAddress pending_close_bda; /* pending GATT channel remote device address */
173 
174 } tBTA_DM_SEARCH_CB;
175 
176 extern const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[];
177 extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
178 
179 }  // namespace bta_dm_disc_legacy
180 
181 namespace fmt {
182 template <>
183 struct formatter<bta_dm_disc_legacy::tBTA_DM_EVT>
184     : enum_formatter<bta_dm_disc_legacy::tBTA_DM_EVT> {};
185 template <>
186 struct formatter<bta_dm_disc_legacy::tBTA_DM_STATE>
187     : enum_formatter<bta_dm_disc_legacy::tBTA_DM_STATE> {};
188 }  // namespace fmt
189