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