1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains internally used SDP definitions
22  *
23  ******************************************************************************/
24 
25 #pragma once
26 
27 #include <base/functional/callback.h>
28 #include <base/strings/stringprintf.h>
29 
30 #include <cstdint>
31 #include <string>
32 
33 #include "include/macros.h"
34 #include "internal_include/bt_target.h"
35 #include "osi/include/alarm.h"
36 #include "stack/include/bt_hdr.h"
37 #include "stack/include/l2cap_interface.h"
38 #include "stack/include/sdp_callback.h"
39 #include "stack/sdp/sdp_discovery_db.h"
40 #include "types/bluetooth/uuid.h"
41 #include "types/raw_address.h"
42 
43 /* Continuation length - we use a 2-byte offset */
44 #define SDP_CONTINUATION_LEN 2
45 #define SDP_MAX_CONTINUATION_LEN 16 /* As per the spec */
46 
47 /* Timeout definitions. */
48 #define SDP_INACT_TIMEOUT_MS (30 * 1000) /* Inactivity timeout (in ms) */
49 
50 /* Define the Protocol Data Unit (PDU) types.
51  */
52 #define SDP_PDU_ERROR_RESPONSE 0x01
53 #define SDP_PDU_SERVICE_SEARCH_REQ 0x02
54 #define SDP_PDU_SERVICE_SEARCH_RSP 0x03
55 #define SDP_PDU_SERVICE_ATTR_REQ 0x04
56 #define SDP_PDU_SERVICE_ATTR_RSP 0x05
57 #define SDP_PDU_SERVICE_SEARCH_ATTR_REQ 0x06
58 #define SDP_PDU_SERVICE_SEARCH_ATTR_RSP 0x07
59 
60 /* Max UUIDs and attributes we support per sequence */
61 #define MAX_UUIDS_PER_SEQ 16
62 #define MAX_ATTR_PER_SEQ 16
63 
64 /* Max length we support for any attribute */
65 #ifdef SDP_MAX_ATTR_LEN
66 #define MAX_ATTR_LEN SDP_MAX_ATTR_LEN
67 #else
68 #define MAX_ATTR_LEN 256
69 #endif
70 
71 /* Internal UUID sequence representation */
72 struct tUID_ENT {
73   uint16_t len;
74   uint8_t value[bluetooth::Uuid::kNumBytes128];
75 };
76 
77 struct tSDP_UUID_SEQ {
78   uint16_t num_uids;
79   tUID_ENT uuid_entry[MAX_UUIDS_PER_SEQ];
80 };
81 
82 /* Internal attribute sequence definitions */
83 struct tATT_ENT {
84   uint16_t start;
85   uint16_t end;
86 };
87 
88 struct tSDP_ATTR_SEQ {
89   uint16_t num_attr;
90   tATT_ENT attr_entry[MAX_ATTR_PER_SEQ];
91 };
92 
93 /* Define the attribute element of the SDP database record */
94 struct tSDP_ATTRIBUTE {
95   uint32_t len;       /* Number of bytes in the entry */
96   uint8_t* value_ptr; /* Points to attr_pad */
97   uint16_t id;
98   uint8_t type;
99 };
100 
101 /* An SDP record consists of a handle, and 1 or more attributes */
102 struct tSDP_RECORD {
103   uint32_t record_handle;
104   uint32_t free_pad_ptr;
105   uint16_t num_attributes;
106   tSDP_ATTRIBUTE attribute[SDP_MAX_REC_ATTR];
107   uint8_t attr_pad[SDP_MAX_PAD_LEN];
108 };
109 
110 /* Define the SDP database */
111 struct tSDP_DB {
112   uint32_t di_primary_handle; /* Device ID Primary record or NULL if nonexistent */
113   uint16_t num_records;
114   tSDP_RECORD record[SDP_MAX_RECORDS];
115 };
116 
117 /* Continuation information for the SDP server response */
118 struct tSDP_CONT_INFO {
119   uint16_t next_attr_index;         // attr index for next continuation response
120   uint16_t next_attr_start_id;      // attr id to start with for the attr index in
121                                     //   next cont. response
122   const tSDP_RECORD* prev_sdp_rec;  // last sdp record that was completely sent
123                                     // in the response
124   bool last_attr_seq_desc_sent;     // whether attr seq length has been sent
125                                     //    previously
126   uint16_t attr_offset;             // offset within the attr to keep trak of partial
127                                     //   attributes in the responses
128 };
129 
130 enum class tSDP_STATE : uint8_t {
131   IDLE = 0,
132   CONN_SETUP = 1,
133   CFG_SETUP = 2,
134   CONNECTED = 3,
135   CONN_PEND = 4,
136 };
137 
sdp_state_text(const tSDP_STATE & state)138 inline std::string sdp_state_text(const tSDP_STATE& state) {
139   switch (state) {
140     CASE_RETURN_STRING(tSDP_STATE::IDLE);
141     CASE_RETURN_STRING(tSDP_STATE::CONN_SETUP);
142     CASE_RETURN_STRING(tSDP_STATE::CFG_SETUP);
143     CASE_RETURN_STRING(tSDP_STATE::CONNECTED);
144     CASE_RETURN_STRING(tSDP_STATE::CONN_PEND);
145   }
146   RETURN_UNKNOWN_TYPE_STRING(tSDP_STATE, state);
147 }
148 
149 enum : uint8_t {
150   SDP_FLAGS_NONE = 0x00,
151   SDP_FLAGS_IS_ORIG = 0x01,
152   SDP_FLAGS_HIS_CFG_DONE = 0x02,
153   SDP_FLAGS_MY_CFG_DONE = 0x04,
154 };
155 typedef uint8_t tSDP_FLAGS;
156 
sdp_flags_text(const tSDP_FLAGS & flags)157 inline std::string sdp_flags_text(const tSDP_FLAGS& flags) {
158   switch (flags) {
159     CASE_RETURN_TEXT(SDP_FLAGS_IS_ORIG);
160     CASE_RETURN_TEXT(SDP_FLAGS_HIS_CFG_DONE);
161     CASE_RETURN_TEXT(SDP_FLAGS_MY_CFG_DONE);
162     default:
163       return std::string("UNKNOWN[") + std::to_string(flags) + std::string("]");
164   }
165 }
166 
167 enum : uint8_t {
168   SDP_DISC_WAIT_CONN = 0,
169   SDP_DISC_WAIT_HANDLES = 1,
170   SDP_DISC_WAIT_ATTR = 2,
171   SDP_DISC_WAIT_SEARCH_ATTR = 3,
172   SDP_DISC_WAIT_UNUSED4 = 4,
173   SDP_DISC_WAIT_CANCEL = 5,
174 };
175 typedef uint8_t tSDP_DISC_WAIT;
176 
177 /* Define the SDP Connection Control Block */
178 struct tCONN_CB {
179   tSDP_STATE con_state{tSDP_STATE::IDLE};
180   tSDP_FLAGS con_flags{SDP_FLAGS_NONE};
181 
182   RawAddress device_address;
183   alarm_t* sdp_conn_timer;
184   uint16_t rem_mtu_size;
185   uint16_t connection_id;
186   uint16_t list_len;                   /* length of the response in the GKI buffer */
187   uint16_t pse_dynamic_attributes_len;  // length of the attributes need to be
188                                         // added in final sdp response len
189   uint8_t* rsp_list;                   /* pointer to GKI buffer holding response */
190 
191   tSDP_DISCOVERY_DB* p_db; /* Database to save info into   */
192   tSDP_DISC_CMPL_CB* p_cb; /* Callback for discovery done  */
193   /* OnceCallback would be more appropriate, but it doesn't have copy
194    * constructor, so won't compile with current memory management for control
195    * blocks */
196   base::RepeatingCallback<tSDP_DISC_CMPL_CB> complete_callback; /* Callback for discovery */
197   uint32_t handles[SDP_MAX_DISC_SERVER_RECS]; /* Discovered server record handles */
198   uint16_t num_handles;                       /* Number of server handles     */
199   uint16_t cur_handle;                        /* Current handle being processed */
200   uint16_t transaction_id;
201   tSDP_REASON disconnect_reason; /* Disconnect reason            */
202 
203   tSDP_DISC_WAIT disc_state{SDP_DISC_WAIT_CONN};
204   bool is_attr_search{false};
205 
206   uint16_t cont_offset;     /* Continuation state data in the server response */
207   tSDP_CONT_INFO cont_info;  // structure to hold continuation information for
208                              //   the server response
209   tCONN_CB() = default;
210 
211 private:
212   tCONN_CB(const tCONN_CB&) = delete;
213 };
214 
sdp_disc_wait_text(const tSDP_DISC_WAIT & state)215 inline std::string sdp_disc_wait_text(const tSDP_DISC_WAIT& state) {
216   switch (state) {
217     CASE_RETURN_TEXT(SDP_DISC_WAIT_CONN);
218     CASE_RETURN_TEXT(SDP_DISC_WAIT_HANDLES);
219     CASE_RETURN_TEXT(SDP_DISC_WAIT_ATTR);
220     CASE_RETURN_TEXT(SDP_DISC_WAIT_SEARCH_ATTR);
221     CASE_RETURN_TEXT(SDP_DISC_WAIT_CANCEL);
222     default:
223       return base::StringPrintf("UNKNOWN[%d]", state);
224   }
225 }
226 
227 /*  The main SDP control block */
228 struct tSDP_CB {
229   tL2CAP_CFG_INFO l2cap_my_cfg; /* My L2CAP config     */
230   tCONN_CB ccb[SDP_MAX_CONNECTIONS];
231   tSDP_DB server_db;
232   tL2CAP_APPL_INFO reg_info;    /* L2CAP Registration info */
233   uint16_t max_attr_list_size;  /* Max attribute list size to use   */
234   uint16_t max_recs_per_search; /* Max records we want per seaarch  */
235 };
236 
237 /* Global SDP data */
238 extern tSDP_CB sdp_cb;
239 
240 /* Functions provided by sdp_main.cc */
241 void sdp_init(void);
242 void sdp_free(void);
243 void sdp_disconnect(tCONN_CB* p_ccb, tSDP_REASON reason);
244 
245 void sdp_conn_timer_timeout(void* data);
246 
247 [[nodiscard]] tCONN_CB* sdp_conn_originate(const RawAddress& bd_addr);
248 
249 /* Functions provided by sdp_utils.cc
250  */
251 void sdpu_log_attribute_metrics(const RawAddress& bda, tSDP_DISCOVERY_DB* p_db);
252 tCONN_CB* sdpu_find_ccb_by_cid(uint16_t cid);
253 tCONN_CB* sdpu_find_ccb_by_db(const tSDP_DISCOVERY_DB* p_db);
254 tCONN_CB* sdpu_allocate_ccb(void);
255 void sdpu_release_ccb(tCONN_CB& p_ccb);
256 void sdpu_dump_all_ccb();
257 
258 uint8_t* sdpu_build_attrib_seq(uint8_t* p_out, uint16_t* p_attr, uint16_t num_attrs);
259 uint8_t* sdpu_build_attrib_entry(uint8_t* p_out, const tSDP_ATTRIBUTE* p_attr);
260 void sdpu_build_n_send_error(tCONN_CB* p_ccb, uint16_t trans_num, tSDP_STATUS error_code,
261                              char* p_error_text);
262 
263 uint8_t* sdpu_extract_attr_seq(uint8_t* p, uint16_t param_len, tSDP_ATTR_SEQ* p_seq);
264 uint8_t* sdpu_extract_uid_seq(uint8_t* p, uint16_t param_len, tSDP_UUID_SEQ* p_seq);
265 
266 uint8_t* sdpu_get_len_from_type(uint8_t* p, uint8_t* p_end, uint8_t type, uint32_t* p_len);
267 bool sdpu_is_base_uuid(uint8_t* p_uuid);
268 bool sdpu_compare_uuid_arrays(const uint8_t* p_uuid1, uint32_t len1, const uint8_t* p_uuid2,
269                               uint16_t len2);
270 bool sdpu_compare_uuid_with_attr(const bluetooth::Uuid& uuid, tSDP_DISC_ATTR* p_attr);
271 
272 void sdpu_sort_attr_list(uint16_t num_attr, tSDP_DISCOVERY_DB* p_db);
273 uint16_t sdpu_get_list_len(tSDP_UUID_SEQ* uid_seq, tSDP_ATTR_SEQ* attr_seq);
274 uint16_t sdpu_get_attrib_seq_len(const tSDP_RECORD* p_rec, const tSDP_ATTR_SEQ* attr_seq);
275 uint16_t sdpu_get_attrib_entry_len(const tSDP_ATTRIBUTE* p_attr);
276 uint8_t* sdpu_build_partial_attrib_entry(uint8_t* p_out, const tSDP_ATTRIBUTE* p_attr, uint16_t len,
277                                          uint16_t* offset);
278 bool SDP_AddAttributeToRecord(tSDP_RECORD* p_rec, uint16_t attr_id, uint8_t attr_type,
279                               uint32_t attr_len, uint8_t* p_val);
280 bool SDP_AddProfileDescriptorListToRecord(tSDP_RECORD* p_rec, uint16_t profile_uuid,
281                                           uint16_t version);
282 bool SDP_DeleteAttributeFromRecord(tSDP_RECORD* p_rec, uint16_t attr_id);
283 uint16_t sdpu_is_avrcp_profile_description_list(const tSDP_ATTRIBUTE* p_attr);
284 bool sdpu_is_service_id_avrc_target(const tSDP_ATTRIBUTE* p_attr);
285 bool spdu_is_avrcp_version_valid(const uint16_t version);
286 void sdpu_set_avrc_target_version(const tSDP_ATTRIBUTE* p_attr, const RawAddress* bdaddr);
287 void sdpu_set_avrc_target_features(const tSDP_ATTRIBUTE* p_attr, const RawAddress* bdaddr,
288                                    uint16_t profile_version);
289 uint16_t sdpu_get_active_ccb_cid(const RawAddress& bd_addr);
290 bool sdpu_process_pend_ccb_same_cid(const tCONN_CB& ccb);
291 bool sdpu_process_pend_ccb_new_cid(const tCONN_CB& ccb);
292 void sdpu_clear_pend_ccb(const tCONN_CB& ccb);
293 void sdpu_callback(const tCONN_CB& ccb, tSDP_REASON reason);
294 
295 /* Functions provided by sdp_db.cc */
296 const tSDP_RECORD* sdp_db_service_search(const tSDP_RECORD* p_rec, const tSDP_UUID_SEQ* p_seq);
297 tSDP_RECORD* sdp_db_find_record(uint32_t handle);
298 const tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(const tSDP_RECORD* p_rec, uint16_t start_attr,
299                                               uint16_t end_attr);
300 
301 /* Functions provided by sdp_server.cc */
302 void sdp_server_handle_client_req(tCONN_CB* p_ccb, BT_HDR* p_msg);
303 bool sdp_dynamic_change_hfp_version(const tSDP_ATTRIBUTE* p_attr, const RawAddress& remote_address);
304 
305 /* Functions provided by sdp_discovery.cc */
306 void sdp_disc_connected(tCONN_CB* p_ccb);
307 void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg);
308 
309 void update_pce_entry_to_interop_database(RawAddress remote_addr);
310 bool is_sdp_pbap_pce_disabled(RawAddress remote_addr);
311 void sdp_save_local_pse_record_attributes(int32_t rfcomm_channel_number, int32_t l2cap_psm,
312                                           int32_t profile_version, uint32_t supported_features,
313                                           uint32_t supported_repositories);
314 
315 size_t sdp_get_num_records(const tSDP_DISCOVERY_DB& db);
316 size_t sdp_get_num_attributes(const tSDP_DISC_REC& sdp_disc_rec);
317