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