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 #ifndef SDP_INT_H
26 #define SDP_INT_H
27 
28 #include <base/functional/callback.h>
29 #include <base/strings/stringprintf.h>
30 
31 #include <cstdint>
32 
33 #include "internal_include/bt_target.h"
34 #include "macros.h"
35 #include "osi/include/alarm.h"
36 #include "stack/include/bt_hdr.h"
37 #include "stack/include/l2c_api.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 typedef struct {
73   uint16_t len;
74   uint8_t value[bluetooth::Uuid::kNumBytes128];
75 } tUID_ENT;
76 
77 typedef struct {
78   uint16_t num_uids;
79   tUID_ENT uuid_entry[MAX_UUIDS_PER_SEQ];
80 } tSDP_UUID_SEQ;
81 
82 /* Internal attribute sequence definitions */
83 typedef struct {
84   uint16_t start;
85   uint16_t end;
86 } tATT_ENT;
87 
88 typedef struct {
89   uint16_t num_attr;
90   tATT_ENT attr_entry[MAX_ATTR_PER_SEQ];
91 } tSDP_ATTR_SEQ;
92 
93 /* Define the attribute element of the SDP database record */
94 typedef struct {
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 } tSDP_ATTRIBUTE;
100 
101 /* An SDP record consists of a handle, and 1 or more attributes */
102 typedef struct {
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 } tSDP_RECORD;
109 
110 /* Define the SDP database */
111 typedef struct {
112   uint32_t
113       di_primary_handle; /* Device ID Primary record or NULL if nonexistent */
114   uint16_t num_records;
115   tSDP_RECORD record[SDP_MAX_RECORDS];
116 } tSDP_DB;
117 
118 /* Continuation information for the SDP server response */
119 typedef struct {
120   uint16_t next_attr_index;    /* attr index for next continuation response */
121   uint16_t next_attr_start_id; /* attr id to start with for the attr index in
122                                   next cont. response */
123   const tSDP_RECORD* prev_sdp_rec; /* last sdp record that was completely sent
124                                 in the response */
125   bool last_attr_seq_desc_sent; /* whether attr seq length has been sent
126                                    previously */
127   uint16_t attr_offset; /* offset within the attr to keep trak of partial
128                            attributes in the responses */
129 } tSDP_CONT_INFO;
130 
131 enum : uint8_t {
132   SDP_STATE_IDLE = 0,
133   SDP_STATE_CONN_SETUP = 1,
134   SDP_STATE_CFG_SETUP = 2,
135   SDP_STATE_CONNECTED = 3,
136   SDP_STATE_CONN_PEND = 4,
137 };
138 typedef uint8_t tSDP_STATE;
139 
sdp_state_text(const tSDP_STATE & state)140 inline std::string sdp_state_text(const tSDP_STATE& state) {
141   switch (state) {
142     CASE_RETURN_TEXT(SDP_STATE_IDLE);
143     CASE_RETURN_TEXT(SDP_STATE_CONN_SETUP);
144     CASE_RETURN_TEXT(SDP_STATE_CFG_SETUP);
145     CASE_RETURN_TEXT(SDP_STATE_CONNECTED);
146     CASE_RETURN_TEXT(SDP_STATE_CONN_PEND);
147     default:
148       return std::string("UNKNOWN[") + std::to_string(state) + std::string("]");
149   }
150 }
151 
152 enum : uint8_t {
153   SDP_FLAGS_IS_ORIG = 0x01,
154   SDP_FLAGS_HIS_CFG_DONE = 0x02,
155   SDP_FLAGS_MY_CFG_DONE = 0x04,
156 };
157 typedef uint8_t tSDP_FLAGS;
158 
sdp_flags_text(const tSDP_FLAGS & flags)159 inline std::string sdp_flags_text(const tSDP_FLAGS& flags) {
160   switch (flags) {
161     CASE_RETURN_TEXT(SDP_FLAGS_IS_ORIG);
162     CASE_RETURN_TEXT(SDP_FLAGS_HIS_CFG_DONE);
163     CASE_RETURN_TEXT(SDP_FLAGS_MY_CFG_DONE);
164     default:
165       return std::string("UNKNOWN[") + std::to_string(flags) + std::string("]");
166   }
167 }
168 
169 enum : uint8_t {
170   SDP_DISC_WAIT_CONN = 0,
171   SDP_DISC_WAIT_HANDLES = 1,
172   SDP_DISC_WAIT_ATTR = 2,
173   SDP_DISC_WAIT_SEARCH_ATTR = 3,
174   SDP_DISC_WAIT_UNUSED4 = 4,
175   SDP_DISC_WAIT_CANCEL = 5,
176 };
177 typedef uint8_t tSDP_DISC_WAIT;
178 
179 /* Define the SDP Connection Control Block */
180 struct tCONN_CB {
181   uint8_t con_state;
182   uint8_t con_flags;
183 
184   RawAddress device_address;
185   alarm_t* sdp_conn_timer;
186   uint16_t rem_mtu_size;
187   uint16_t connection_id;
188   uint16_t list_len; /* length of the response in the GKI buffer */
189   uint16_t pse_dynamic_attributes_len; /* length of the attributes need to be
190                              added in final sdp response len */
191   uint8_t* rsp_list; /* pointer to GKI buffer holding response */
192 
193   tSDP_DISCOVERY_DB* p_db; /* Database to save info into   */
194   tSDP_DISC_CMPL_CB* p_cb; /* Callback for discovery done  */
195   /* OnceCallback would be more appropriate, but it doesn't have copy
196    * constructor, so won't compile with current memory management for control
197    * blocks */
198   base::RepeatingCallback<tSDP_DISC_CMPL_CB>
199       complete_callback; /* Callback for discovery */
200   uint32_t
201       handles[SDP_MAX_DISC_SERVER_RECS]; /* Discovered server record handles */
202   uint16_t num_handles;                  /* Number of server handles     */
203   uint16_t cur_handle;                   /* Current handle being processed */
204   uint16_t transaction_id;
205   uint16_t disconnect_reason; /* Disconnect reason            */
206 
207   uint8_t disc_state;
208   bool is_attr_search;
209 
210   uint16_t cont_offset;     /* Continuation state data in the server response */
211   tSDP_CONT_INFO cont_info; /* structure to hold continuation information for
212                                the server response */
213   tCONN_CB() = default;
214 
215  private:
216   tCONN_CB(const tCONN_CB&) = delete;
217 };
218 
sdp_disc_wait_text(const tSDP_DISC_WAIT & state)219 inline std::string sdp_disc_wait_text(const tSDP_DISC_WAIT& state) {
220   switch (state) {
221     CASE_RETURN_TEXT(SDP_DISC_WAIT_CONN);
222     CASE_RETURN_TEXT(SDP_DISC_WAIT_HANDLES);
223     CASE_RETURN_TEXT(SDP_DISC_WAIT_ATTR);
224     CASE_RETURN_TEXT(SDP_DISC_WAIT_SEARCH_ATTR);
225     CASE_RETURN_TEXT(SDP_DISC_WAIT_CANCEL);
226     default:
227       return base::StringPrintf("UNKNOWN[%d]", state);
228   }
229 }
230 
231 /*  The main SDP control block */
232 typedef struct {
233   tL2CAP_CFG_INFO l2cap_my_cfg; /* My L2CAP config     */
234   tCONN_CB ccb[SDP_MAX_CONNECTIONS];
235   tSDP_DB server_db;
236   tL2CAP_APPL_INFO reg_info;    /* L2CAP Registration info */
237   uint16_t max_attr_list_size;  /* Max attribute list size to use   */
238   uint16_t max_recs_per_search; /* Max records we want per seaarch  */
239 } tSDP_CB;
240 
241 /* Global SDP data */
242 extern tSDP_CB sdp_cb;
243 
244 /* Functions provided by sdp_main.cc */
245 void sdp_init(void);
246 void sdp_free(void);
247 void sdp_disconnect(tCONN_CB* p_ccb, tSDP_REASON reason);
248 
249 void sdp_conn_timer_timeout(void* data);
250 
251 tCONN_CB* sdp_conn_originate(const RawAddress& bd_addr);
252 
253 /* Functions provided by sdp_utils.cc
254  */
255 void sdpu_log_attribute_metrics(const RawAddress& bda, tSDP_DISCOVERY_DB* p_db);
256 tCONN_CB* sdpu_find_ccb_by_cid(uint16_t cid);
257 tCONN_CB* sdpu_find_ccb_by_db(const tSDP_DISCOVERY_DB* p_db);
258 tCONN_CB* sdpu_allocate_ccb(void);
259 void sdpu_release_ccb(tCONN_CB& p_ccb);
260 
261 uint8_t* sdpu_build_attrib_seq(uint8_t* p_out, uint16_t* p_attr,
262                                uint16_t num_attrs);
263 uint8_t* sdpu_build_attrib_entry(uint8_t* p_out, const tSDP_ATTRIBUTE* p_attr);
264 void sdpu_build_n_send_error(tCONN_CB* p_ccb, uint16_t trans_num,
265                              uint16_t error_code, char* p_error_text);
266 
267 uint8_t* sdpu_extract_attr_seq(uint8_t* p, uint16_t param_len,
268                                tSDP_ATTR_SEQ* p_seq);
269 uint8_t* sdpu_extract_uid_seq(uint8_t* p, uint16_t param_len,
270                               tSDP_UUID_SEQ* p_seq);
271 
272 uint8_t* sdpu_get_len_from_type(uint8_t* p, uint8_t* p_end, uint8_t type,
273                                 uint32_t* p_len);
274 bool sdpu_is_base_uuid(uint8_t* p_uuid);
275 bool sdpu_compare_uuid_arrays(const uint8_t* p_uuid1, uint32_t len1,
276                               const uint8_t* p_uuid2, uint16_t len2);
277 bool sdpu_compare_uuid_with_attr(const bluetooth::Uuid& uuid,
278                                  tSDP_DISC_ATTR* p_attr);
279 
280 void sdpu_sort_attr_list(uint16_t num_attr, tSDP_DISCOVERY_DB* p_db);
281 uint16_t sdpu_get_list_len(tSDP_UUID_SEQ* uid_seq, tSDP_ATTR_SEQ* attr_seq);
282 uint16_t sdpu_get_attrib_seq_len(const tSDP_RECORD* p_rec,
283                                  const tSDP_ATTR_SEQ* attr_seq);
284 uint16_t sdpu_get_attrib_entry_len(const tSDP_ATTRIBUTE* p_attr);
285 uint8_t* sdpu_build_partial_attrib_entry(uint8_t* p_out,
286                                          const tSDP_ATTRIBUTE* p_attr,
287                                          uint16_t len, uint16_t* offset);
288 bool SDP_AddAttributeToRecord(tSDP_RECORD* p_rec, uint16_t attr_id,
289                               uint8_t attr_type, uint32_t attr_len,
290                               uint8_t* p_val);
291 bool SDP_AddProfileDescriptorListToRecord(tSDP_RECORD* p_rec,
292                                           uint16_t profile_uuid,
293                                           uint16_t version);
294 bool SDP_DeleteAttributeFromRecord(tSDP_RECORD* p_rec, uint16_t attr_id);
295 uint16_t sdpu_is_avrcp_profile_description_list(const tSDP_ATTRIBUTE* p_attr);
296 bool sdpu_is_service_id_avrc_target(const tSDP_ATTRIBUTE* p_attr);
297 bool spdu_is_avrcp_version_valid(const uint16_t version);
298 void sdpu_set_avrc_target_version(const tSDP_ATTRIBUTE* p_attr,
299                                   const RawAddress* bdaddr);
300 void sdpu_set_avrc_target_features(const tSDP_ATTRIBUTE* p_attr,
301                                    const RawAddress* bdaddr,
302                                    uint16_t profile_version);
303 uint16_t sdpu_get_active_ccb_cid(const RawAddress& bd_addr);
304 bool sdpu_process_pend_ccb_same_cid(tCONN_CB& ccb);
305 bool sdpu_process_pend_ccb_new_cid(tCONN_CB& ccb);
306 void sdpu_clear_pend_ccb(tCONN_CB& ccb);
307 void sdpu_callback(tCONN_CB& ccb, tSDP_REASON reason);
308 
309 /* Functions provided by sdp_db.cc
310  */
311 const tSDP_RECORD* sdp_db_service_search(const tSDP_RECORD* p_rec,
312                                          const tSDP_UUID_SEQ* p_seq);
313 tSDP_RECORD* sdp_db_find_record(uint32_t handle);
314 const tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(const tSDP_RECORD* p_rec,
315                                               uint16_t start_attr,
316                                               uint16_t end_attr);
317 
318 /* Functions provided by sdp_server.cc
319  */
320 void sdp_server_handle_client_req(tCONN_CB* p_ccb, BT_HDR* p_msg);
321 
322 /* Functions provided by sdp_discovery.cc
323  */
324 void sdp_disc_connected(tCONN_CB* p_ccb);
325 void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg);
326 
327 void update_pce_entry_to_interop_database(RawAddress remote_addr);
328 bool is_sdp_pbap_pce_disabled(RawAddress remote_addr);
329 void sdp_save_local_pse_record_attributes(int32_t rfcomm_channel_number,
330                                           int32_t l2cap_psm,
331                                           int32_t profile_version,
332                                           uint32_t supported_features,
333                                           uint32_t supported_repositories);
334 
335 size_t sdp_get_num_records(const tSDP_DISCOVERY_DB& db);
336 size_t sdp_get_num_attributes(const tSDP_DISC_REC& sdp_disc_rec);
337 
338 #endif
339