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 SDP interface functions
22 *
23 ******************************************************************************/
24
25 #include "stack/include/sdp_api.h"
26
27 #include <string.h>
28
29 #include <cstdint>
30
31 #include "bt_target.h"
32 #include "osi/include/osi.h" // PTR_TO_UINT
33 #include "stack/include/bt_types.h"
34 #include "stack/sdp/sdpint.h"
35 #include "types/bluetooth/uuid.h"
36 #include "types/raw_address.h"
37
38 using bluetooth::Uuid;
39
40 /**********************************************************************
41 * C L I E N T F U N C T I O N P R O T O T Y P E S *
42 **********************************************************************/
43
44 /*******************************************************************************
45 *
46 * Function SDP_InitDiscoveryDb
47 *
48 * Description This function is called to initialize a discovery database.
49 *
50 * Parameters: p_db - (input) address of an area of memory where the
51 * discovery database is managed.
52 * len - (input) size (in bytes) of the memory
53 * NOTE: This must be larger than
54 * sizeof(tSDP_DISCOVERY_DB)
55 * num_uuid - (input) number of UUID filters applied
56 * p_uuid_list - (input) list of UUID filters
57 * num_attr - (input) number of attribute filters applied
58 * p_attr_list - (input) list of attribute filters
59 *
60 *
61 * Returns bool
62 * true if successful
63 * false if one or more parameters are bad
64 *
65 ******************************************************************************/
SDP_InitDiscoveryDb(tSDP_DISCOVERY_DB * p_db,uint32_t len,uint16_t num_uuid,const Uuid * p_uuid_list,uint16_t num_attr,const uint16_t * p_attr_list)66 bool SDP_InitDiscoveryDb(tSDP_DISCOVERY_DB* p_db, uint32_t len,
67 uint16_t num_uuid, const Uuid* p_uuid_list,
68 uint16_t num_attr, const uint16_t* p_attr_list) {
69 uint16_t xx;
70
71 /* verify the parameters */
72 if (p_db == NULL || (sizeof(tSDP_DISCOVERY_DB) > len) ||
73 num_attr > SDP_MAX_ATTR_FILTERS || num_uuid > SDP_MAX_UUID_FILTERS) {
74 SDP_TRACE_ERROR(
75 "SDP_InitDiscoveryDb Illegal param: p_db 0x%x, len %d, num_uuid %d, "
76 "num_attr %d",
77 PTR_TO_UINT(p_db), len, num_uuid, num_attr);
78
79 return (false);
80 }
81
82 memset(p_db, 0, (size_t)len);
83
84 p_db->mem_size = len - sizeof(tSDP_DISCOVERY_DB);
85 p_db->mem_free = p_db->mem_size;
86 p_db->p_first_rec = NULL;
87 p_db->p_free_mem = (uint8_t*)(p_db + 1);
88
89 for (xx = 0; xx < num_uuid; xx++) p_db->uuid_filters[xx] = *p_uuid_list++;
90
91 p_db->num_uuid_filters = num_uuid;
92
93 for (xx = 0; xx < num_attr; xx++) p_db->attr_filters[xx] = *p_attr_list++;
94
95 /* sort attributes */
96 sdpu_sort_attr_list(num_attr, p_db);
97
98 p_db->num_attr_filters = num_attr;
99 return (true);
100 }
101
102 /*******************************************************************************
103 *
104 * Function SDP_CancelServiceSearch
105 *
106 * Description This function cancels an active query to an SDP server.
107 *
108 * Returns true if discovery cancelled, false if a matching activity is
109 * not found.
110 *
111 ******************************************************************************/
SDP_CancelServiceSearch(const tSDP_DISCOVERY_DB * p_db)112 bool SDP_CancelServiceSearch(const tSDP_DISCOVERY_DB* p_db) {
113 tCONN_CB* p_ccb = sdpu_find_ccb_by_db(p_db);
114 if (!p_ccb) return (false);
115
116 sdp_disconnect(p_ccb, SDP_CANCEL);
117 p_ccb->disc_state = SDP_DISC_WAIT_CANCEL;
118 return (true);
119 }
120
121 /*******************************************************************************
122 *
123 * Function SDP_ServiceSearchRequest
124 *
125 * Description This function queries an SDP server for information.
126 *
127 * Returns true if discovery started, false if failed.
128 *
129 ******************************************************************************/
SDP_ServiceSearchRequest(const RawAddress & p_bd_addr,tSDP_DISCOVERY_DB * p_db,tSDP_DISC_CMPL_CB * p_cb)130 bool SDP_ServiceSearchRequest(const RawAddress& p_bd_addr,
131 tSDP_DISCOVERY_DB* p_db,
132 tSDP_DISC_CMPL_CB* p_cb) {
133 tCONN_CB* p_ccb;
134
135 /* Specific BD address */
136 p_ccb = sdp_conn_originate(p_bd_addr);
137
138 if (!p_ccb) return (false);
139
140 p_ccb->disc_state = SDP_DISC_WAIT_CONN;
141 p_ccb->p_db = p_db;
142 p_ccb->p_cb = p_cb;
143
144 return (true);
145 }
146
147 /*******************************************************************************
148 *
149 * Function SDP_ServiceSearchAttributeRequest
150 *
151 * Description This function queries an SDP server for information.
152 *
153 * The difference between this API function and the function
154 * SDP_ServiceSearchRequest is that this one does a
155 * combined ServiceSearchAttributeRequest SDP function.
156 * (This is for Unplug Testing)
157 *
158 * Returns true if discovery started, false if failed.
159 *
160 ******************************************************************************/
SDP_ServiceSearchAttributeRequest(const RawAddress & p_bd_addr,tSDP_DISCOVERY_DB * p_db,tSDP_DISC_CMPL_CB * p_cb)161 bool SDP_ServiceSearchAttributeRequest(const RawAddress& p_bd_addr,
162 tSDP_DISCOVERY_DB* p_db,
163 tSDP_DISC_CMPL_CB* p_cb) {
164 tCONN_CB* p_ccb;
165
166 /* Specific BD address */
167 p_ccb = sdp_conn_originate(p_bd_addr);
168
169 if (!p_ccb) return (false);
170
171 p_ccb->disc_state = SDP_DISC_WAIT_CONN;
172 p_ccb->p_db = p_db;
173 p_ccb->p_cb = p_cb;
174
175 p_ccb->is_attr_search = true;
176
177 return (true);
178 }
179 /*******************************************************************************
180 *
181 * Function SDP_ServiceSearchAttributeRequest2
182 *
183 * Description This function queries an SDP server for information.
184 *
185 * The difference between this API function and the function
186 * SDP_ServiceSearchRequest is that this one does a
187 * combined ServiceSearchAttributeRequest SDP function.
188 * (This is for Unplug Testing)
189 *
190 * Returns true if discovery started, false if failed.
191 *
192 ******************************************************************************/
SDP_ServiceSearchAttributeRequest2(const RawAddress & p_bd_addr,tSDP_DISCOVERY_DB * p_db,tSDP_DISC_CMPL_CB2 * p_cb2,const void * user_data)193 bool SDP_ServiceSearchAttributeRequest2(const RawAddress& p_bd_addr,
194 tSDP_DISCOVERY_DB* p_db,
195 tSDP_DISC_CMPL_CB2* p_cb2,
196 const void* user_data) {
197 tCONN_CB* p_ccb;
198
199 /* Specific BD address */
200 p_ccb = sdp_conn_originate(p_bd_addr);
201
202 if (!p_ccb) return (false);
203
204 p_ccb->disc_state = SDP_DISC_WAIT_CONN;
205 p_ccb->p_db = p_db;
206 p_ccb->p_cb2 = p_cb2;
207
208 p_ccb->is_attr_search = true;
209 p_ccb->user_data = user_data;
210
211 return (true);
212 }
213
214 /*******************************************************************************
215 *
216 * Function SDP_FindAttributeInRec
217 *
218 * Description This function searches an SDP discovery record for a
219 * specific attribute.
220 *
221 * Returns Pointer to matching attribute entry, or NULL
222 *
223 ******************************************************************************/
SDP_FindAttributeInRec(const tSDP_DISC_REC * p_rec,uint16_t attr_id)224 tSDP_DISC_ATTR* SDP_FindAttributeInRec(const tSDP_DISC_REC* p_rec,
225 uint16_t attr_id) {
226 tSDP_DISC_ATTR* p_attr;
227
228 p_attr = p_rec->p_first_attr;
229 while (p_attr) {
230 if (p_attr->attr_id == attr_id) return (p_attr);
231
232 p_attr = p_attr->p_next_attr;
233 }
234
235 /* If here, no matching attribute found */
236 return (NULL);
237 }
238
239 /*******************************************************************************
240 *
241 * Function SDP_FindServiceUUIDInRec
242 *
243 * Description This function is called to read the service UUID within a
244 * record if there is any.
245 *
246 * Parameters: p_rec - pointer to a SDP record.
247 * p_uuid - output parameter to save the UUID found.
248 *
249 * Returns true if found, otherwise false.
250 *
251 ******************************************************************************/
SDP_FindServiceUUIDInRec(const tSDP_DISC_REC * p_rec,Uuid * p_uuid)252 bool SDP_FindServiceUUIDInRec(const tSDP_DISC_REC* p_rec, Uuid* p_uuid) {
253 tSDP_DISC_ATTR *p_attr, *p_sattr, *p_extra_sattr;
254
255 p_attr = p_rec->p_first_attr;
256
257 while (p_attr) {
258 if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
259 (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
260 for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr;
261 p_sattr = p_sattr->p_next_attr) {
262 if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) {
263 if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == Uuid::kNumBytes16) {
264 *p_uuid = Uuid::From16Bit(p_sattr->attr_value.v.u16);
265 } else if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) ==
266 Uuid::kNumBytes128) {
267 *p_uuid = Uuid::From128BitBE(p_sattr->attr_value.v.array);
268 } else if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) ==
269 Uuid::kNumBytes32) {
270 *p_uuid = Uuid::From32Bit(p_sattr->attr_value.v.u32);
271 }
272
273 return (true);
274 }
275
276 /* Checking for Toyota G Block Car Kit:
277 ** This car kit puts an extra data element sequence
278 ** where the UUID is suppose to be!!!
279 */
280 else {
281 if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) ==
282 DATA_ELE_SEQ_DESC_TYPE) {
283 /* Look through data element sequence until no more UUIDs */
284 for (p_extra_sattr = p_sattr->attr_value.v.p_sub_attr;
285 p_extra_sattr; p_extra_sattr = p_extra_sattr->p_next_attr) {
286 /* Increment past this to see if the next attribut is UUID */
287 if ((SDP_DISC_ATTR_TYPE(p_extra_sattr->attr_len_type) ==
288 UUID_DESC_TYPE)
289 /* only support 16 bits UUID for now */
290 && (SDP_DISC_ATTR_LEN(p_extra_sattr->attr_len_type) == 2)) {
291 *p_uuid = Uuid::From16Bit(p_extra_sattr->attr_value.v.u16);
292 return (true);
293 }
294 }
295 }
296 }
297 }
298 break;
299 } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
300 if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
301 /* only support 16 bits UUID for now */
302 && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2)) {
303 *p_uuid = Uuid::From16Bit(p_attr->attr_value.v.u16);
304 return (true);
305 }
306 }
307 p_attr = p_attr->p_next_attr;
308 }
309 return false;
310 }
311
312 /*******************************************************************************
313 *
314 * Function SDP_FindServiceUUIDInRec_128bit
315 *
316 * Description This function is called to read the 128-bit service UUID
317 * within a record if there is any.
318 *
319 * Parameters: p_rec - pointer to a SDP record.
320 * p_uuid - output parameter to save the UUID found.
321 *
322 * Returns true if found, otherwise false.
323 *
324 ******************************************************************************/
SDP_FindServiceUUIDInRec_128bit(const tSDP_DISC_REC * p_rec,Uuid * p_uuid)325 bool SDP_FindServiceUUIDInRec_128bit(const tSDP_DISC_REC* p_rec, Uuid* p_uuid) {
326 tSDP_DISC_ATTR* p_attr = p_rec->p_first_attr;
327 while (p_attr) {
328 if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
329 (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
330 tSDP_DISC_ATTR* p_sattr = p_attr->attr_value.v.p_sub_attr;
331 while (p_sattr) {
332 if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) {
333 /* only support 128 bits UUID for now */
334 if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 16) {
335 *p_uuid = Uuid::From128BitBE(p_sattr->attr_value.v.array);
336 }
337 return (true);
338 }
339
340 p_sattr = p_sattr->p_next_attr;
341 }
342 break;
343 } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
344 if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
345 /* only support 128 bits UUID for now */
346 && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 16)) {
347 *p_uuid = Uuid::From128BitBE(p_attr->attr_value.v.array);
348 return (true);
349 }
350 }
351 p_attr = p_attr->p_next_attr;
352 }
353 return false;
354 }
355
356 /*******************************************************************************
357 *
358 * Function SDP_FindServiceInDb
359 *
360 * Description This function queries an SDP database for a specific
361 * service. If the p_start_rec pointer is NULL, it looks from
362 * the beginning of the database, else it continues from the
363 * next record after p_start_rec.
364 *
365 * Returns Pointer to record containing service class, or NULL
366 *
367 ******************************************************************************/
SDP_FindServiceInDb(const tSDP_DISCOVERY_DB * p_db,uint16_t service_uuid,tSDP_DISC_REC * p_start_rec)368 tSDP_DISC_REC* SDP_FindServiceInDb(const tSDP_DISCOVERY_DB* p_db,
369 uint16_t service_uuid,
370 tSDP_DISC_REC* p_start_rec) {
371 tSDP_DISC_REC* p_rec;
372 tSDP_DISC_ATTR *p_attr, *p_sattr, *p_extra_sattr;
373
374 /* Must have a valid database */
375 if (p_db == NULL) return (NULL);
376
377 if (!p_start_rec)
378 p_rec = p_db->p_first_rec;
379 else
380 p_rec = p_start_rec->p_next_rec;
381
382 while (p_rec) {
383 p_attr = p_rec->p_first_attr;
384 while (p_attr) {
385 if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
386 (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) ==
387 DATA_ELE_SEQ_DESC_TYPE)) {
388 for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr;
389 p_sattr = p_sattr->p_next_attr) {
390 if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
391 (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)) {
392 SDP_TRACE_DEBUG(
393 "SDP_FindServiceInDb - p_sattr value = 0x%x serviceuuid = 0x%x",
394 p_sattr->attr_value.v.u16, service_uuid);
395 if (service_uuid == UUID_SERVCLASS_HDP_PROFILE) {
396 if ((p_sattr->attr_value.v.u16 == UUID_SERVCLASS_HDP_SOURCE) ||
397 (p_sattr->attr_value.v.u16 == UUID_SERVCLASS_HDP_SINK)) {
398 SDP_TRACE_DEBUG(
399 "SDP_FindServiceInDb found HDP source or sink\n");
400 return (p_rec);
401 }
402 }
403 }
404
405 if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE &&
406 (service_uuid == 0 ||
407 (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2 &&
408 p_sattr->attr_value.v.u16 == service_uuid)))
409 /* for a specific uuid, or any one */
410 {
411 return (p_rec);
412 }
413
414 /* Checking for Toyota G Block Car Kit:
415 ** This car kit puts an extra data element sequence
416 ** where the UUID is suppose to be!!!
417 */
418 else {
419 if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) ==
420 DATA_ELE_SEQ_DESC_TYPE) {
421 /* Look through data element sequence until no more UUIDs */
422 for (p_extra_sattr = p_sattr->attr_value.v.p_sub_attr;
423 p_extra_sattr; p_extra_sattr = p_extra_sattr->p_next_attr) {
424 /* Increment past this to see if the next attribut is UUID */
425 if ((SDP_DISC_ATTR_TYPE(p_extra_sattr->attr_len_type) ==
426 UUID_DESC_TYPE) &&
427 (SDP_DISC_ATTR_LEN(p_extra_sattr->attr_len_type) == 2)
428 /* for a specific uuid, or any one */
429 && ((p_extra_sattr->attr_value.v.u16 == service_uuid) ||
430 (service_uuid == 0))) {
431 return (p_rec);
432 }
433 }
434 }
435 }
436 }
437 break;
438 } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
439 if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) &&
440 (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2)
441 /* find a specific UUID or anyone */
442 &&
443 ((p_attr->attr_value.v.u16 == service_uuid) || service_uuid == 0))
444 return (p_rec);
445 }
446
447 p_attr = p_attr->p_next_attr;
448 }
449
450 p_rec = p_rec->p_next_rec;
451 }
452 /* If here, no matching UUID found */
453 return (NULL);
454 }
455
456 /*******************************************************************************
457 *
458 * Function SDP_FindServiceInDb_128bit
459 *
460 * Description Query an SDP database for a specific service. If the
461 * p_start_rec pointer is NULL, it looks from the beginning of
462 * the database, else it continues from the next record after
463 * p_start_rec.
464 *
465 * This function is kept separate from SDP_FindServiceInDb
466 * since that API is expected to return only 16-bit UUIDs
467 *
468 * Returns Pointer to record containing service class, or NULL
469 *
470 ******************************************************************************/
SDP_FindServiceInDb_128bit(const tSDP_DISCOVERY_DB * p_db,tSDP_DISC_REC * p_start_rec)471 tSDP_DISC_REC* SDP_FindServiceInDb_128bit(const tSDP_DISCOVERY_DB* p_db,
472 tSDP_DISC_REC* p_start_rec) {
473 tSDP_DISC_REC* p_rec;
474 tSDP_DISC_ATTR *p_attr, *p_sattr;
475
476 /* Must have a valid database */
477 if (p_db == NULL) return (NULL);
478
479 if (!p_start_rec)
480 p_rec = p_db->p_first_rec;
481 else
482 p_rec = p_start_rec->p_next_rec;
483
484 while (p_rec) {
485 p_attr = p_rec->p_first_attr;
486 while (p_attr) {
487 if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
488 (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) ==
489 DATA_ELE_SEQ_DESC_TYPE)) {
490 for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr;
491 p_sattr = p_sattr->p_next_attr) {
492 if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
493 (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 16)) {
494 return (p_rec);
495 }
496 }
497 break;
498 } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
499 if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) &&
500 (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 16))
501 return (p_rec);
502 }
503
504 p_attr = p_attr->p_next_attr;
505 }
506
507 p_rec = p_rec->p_next_rec;
508 }
509 /* If here, no matching UUID found */
510 return (NULL);
511 }
512
513 /*******************************************************************************
514 *
515 * Function SDP_FindServiceUUIDInDb
516 *
517 * Description Query an SDP database for a specific service. If the
518 * p_start_rec pointer is NULL, it looks from the beginning of
519 * the database, else it continues from the next record after
520 * p_start_rec.
521 *
522 * NOTE the only difference between this function and the previous
523 * function "SDP_FindServiceInDb()" is that this function takes
524 * a Uuid input
525 *
526 * Returns Pointer to record containing service class, or NULL
527 *
528 ******************************************************************************/
SDP_FindServiceUUIDInDb(const tSDP_DISCOVERY_DB * p_db,const Uuid & uuid,tSDP_DISC_REC * p_start_rec)529 tSDP_DISC_REC* SDP_FindServiceUUIDInDb(const tSDP_DISCOVERY_DB* p_db,
530 const Uuid& uuid,
531 tSDP_DISC_REC* p_start_rec) {
532 tSDP_DISC_REC* p_rec;
533 tSDP_DISC_ATTR *p_attr, *p_sattr;
534
535 /* Must have a valid database */
536 if (p_db == NULL) return (NULL);
537
538 if (!p_start_rec)
539 p_rec = p_db->p_first_rec;
540 else
541 p_rec = p_start_rec->p_next_rec;
542
543 while (p_rec) {
544 p_attr = p_rec->p_first_attr;
545 while (p_attr) {
546 if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
547 (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) ==
548 DATA_ELE_SEQ_DESC_TYPE)) {
549 for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr;
550 p_sattr = p_sattr->p_next_attr) {
551 if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) {
552 if (sdpu_compare_uuid_with_attr(uuid, p_sattr)) return (p_rec);
553 }
554 }
555 break;
556 } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
557 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) {
558 if (sdpu_compare_uuid_with_attr(uuid, p_attr)) return (p_rec);
559 }
560 }
561
562 p_attr = p_attr->p_next_attr;
563 }
564
565 p_rec = p_rec->p_next_rec;
566 }
567 /* If here, no matching UUID found */
568 return (NULL);
569 }
570
571 /*******************************************************************************
572 *
573 * Function sdp_fill_proto_elem
574 *
575 * Description This function retrieves the protocol element.
576 *
577 * Returns true if found, false if not
578 * If found, the passed protocol list element is filled in.
579 *
580 ******************************************************************************/
sdp_fill_proto_elem(const tSDP_DISC_ATTR * p_attr,uint16_t layer_uuid,tSDP_PROTOCOL_ELEM * p_elem)581 static bool sdp_fill_proto_elem(const tSDP_DISC_ATTR* p_attr,
582 uint16_t layer_uuid,
583 tSDP_PROTOCOL_ELEM* p_elem) {
584 tSDP_DISC_ATTR* p_sattr;
585
586 /* Walk through the protocol descriptor list */
587 for (p_attr = p_attr->attr_value.v.p_sub_attr; p_attr;
588 p_attr = p_attr->p_next_attr) {
589 /* Safety check - each entry should itself be a sequence */
590 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
591 return (false);
592
593 /* Now, see if the entry contains the layer we are interested in */
594 for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr;
595 p_sattr = p_sattr->p_next_attr) {
596 /* SDP_TRACE_DEBUG ("SDP - p_sattr 0x%x, layer_uuid:0x%x, u16:0x%x####",
597 p_sattr, layer_uuid, p_sattr->attr_value.v.u16); */
598
599 if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
600 (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2) &&
601 (p_sattr->attr_value.v.u16 == layer_uuid)) {
602 /* Bingo. Now fill in the passed element */
603 p_elem->protocol_uuid = layer_uuid;
604 p_elem->num_params = 0;
605
606 /* Store the parameters, if any */
607 for (p_sattr = p_sattr->p_next_attr; p_sattr;
608 p_sattr = p_sattr->p_next_attr) {
609 if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) != UINT_DESC_TYPE)
610 break;
611
612 if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)
613 p_elem->params[p_elem->num_params++] = p_sattr->attr_value.v.u16;
614 else
615 p_elem->params[p_elem->num_params++] = p_sattr->attr_value.v.u8;
616
617 if (p_elem->num_params >= SDP_MAX_PROTOCOL_PARAMS) break;
618 }
619 return (true);
620 }
621 }
622 }
623
624 return (false);
625 }
626
627 /*******************************************************************************
628 *
629 * Function SDP_FindProtocolListElemInRec
630 *
631 * Description This function looks at a specific discovery record for a
632 * protocol list element.
633 *
634 * Returns true if found, false if not
635 * If found, the passed protocol list element is filled in.
636 *
637 ******************************************************************************/
SDP_FindProtocolListElemInRec(const tSDP_DISC_REC * p_rec,uint16_t layer_uuid,tSDP_PROTOCOL_ELEM * p_elem)638 bool SDP_FindProtocolListElemInRec(const tSDP_DISC_REC* p_rec,
639 uint16_t layer_uuid,
640 tSDP_PROTOCOL_ELEM* p_elem) {
641 tSDP_DISC_ATTR* p_attr;
642
643 p_attr = p_rec->p_first_attr;
644 while (p_attr) {
645 /* Find the protocol descriptor list */
646 if ((p_attr->attr_id == ATTR_ID_PROTOCOL_DESC_LIST) &&
647 (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
648 return sdp_fill_proto_elem(p_attr, layer_uuid, p_elem);
649 }
650 p_attr = p_attr->p_next_attr;
651 }
652 /* If here, no match found */
653 return (false);
654 }
655
656 /*******************************************************************************
657 *
658 * Function SDP_FindProfileVersionInRec
659 *
660 * Description This function looks at a specific discovery record for the
661 * Profile list descriptor, and pulls out the version number.
662 * The version number consists of an 8-bit major version and
663 * an 8-bit minor version.
664 *
665 * Returns true if found, false if not
666 * If found, the major and minor version numbers that were
667 * passed in are filled in.
668 *
669 ******************************************************************************/
SDP_FindProfileVersionInRec(const tSDP_DISC_REC * p_rec,uint16_t profile_uuid,uint16_t * p_version)670 bool SDP_FindProfileVersionInRec(const tSDP_DISC_REC* p_rec,
671 uint16_t profile_uuid, uint16_t* p_version) {
672 tSDP_DISC_ATTR *p_attr, *p_sattr;
673
674 p_attr = p_rec->p_first_attr;
675 while (p_attr) {
676 /* Find the profile descriptor list */
677 if ((p_attr->attr_id == ATTR_ID_BT_PROFILE_DESC_LIST) &&
678 (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
679 /* Walk through the protocol descriptor list */
680 for (p_attr = p_attr->attr_value.v.p_sub_attr; p_attr;
681 p_attr = p_attr->p_next_attr) {
682 /* Safety check - each entry should itself be a sequence */
683 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
684 return (false);
685
686 /* Now, see if the entry contains the profile UUID we are interested in
687 */
688 for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr;
689 p_sattr = p_sattr->p_next_attr) {
690 if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
691 (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) ==
692 2) /* <- This is bytes, not size code! */
693 && (p_sattr->attr_value.v.u16 == profile_uuid)) {
694 /* Now fill in the major and minor numbers */
695 /* if the attribute matches the description for version (type UINT,
696 * size 2 bytes) */
697 p_sattr = p_sattr->p_next_attr;
698
699 if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) ==
700 UINT_DESC_TYPE) &&
701 (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)) {
702 /* The high order 8 bits is the major number, low order is the
703 * minor number (big endian) */
704 *p_version = p_sattr->attr_value.v.u16;
705
706 return (true);
707 } else
708 return (false); /* The type and/or size was not valid for the
709 profile list version */
710 }
711 }
712 }
713
714 return (false);
715 }
716 p_attr = p_attr->p_next_attr;
717 }
718
719 /* If here, no match found */
720 return (false);
721 }
722
723 /*******************************************************************************
724 * Device Identification (DI) Client Functions
725 ******************************************************************************/
726
727 /*******************************************************************************
728 *
729 * Function SDP_DiDiscover
730 *
731 * Description This function queries a remote device for DI information.
732 *
733 * Returns SDP_SUCCESS if query started successfully, else error
734 *
735 ******************************************************************************/
SDP_DiDiscover(const RawAddress & remote_device,tSDP_DISCOVERY_DB * p_db,uint32_t len,tSDP_DISC_CMPL_CB * p_cb)736 tSDP_STATUS SDP_DiDiscover(const RawAddress& remote_device,
737 tSDP_DISCOVERY_DB* p_db, uint32_t len,
738 tSDP_DISC_CMPL_CB* p_cb) {
739 tSDP_STATUS result = SDP_DI_DISC_FAILED;
740 uint16_t num_uuids = 1;
741 uint16_t di_uuid = UUID_SERVCLASS_PNP_INFORMATION;
742
743 /* build uuid for db init */
744 Uuid init_uuid = Uuid::From16Bit(di_uuid);
745
746 if (SDP_InitDiscoveryDb(p_db, len, num_uuids, &init_uuid, 0, NULL))
747 if (SDP_ServiceSearchRequest(remote_device, p_db, p_cb))
748 result = SDP_SUCCESS;
749
750 return result;
751 }
752
753 /*******************************************************************************
754 *
755 * Function SDP_GetNumDiRecords
756 *
757 * Description Searches specified database for DI records
758 *
759 * Returns number of DI records found
760 *
761 ******************************************************************************/
SDP_GetNumDiRecords(const tSDP_DISCOVERY_DB * p_db)762 uint8_t SDP_GetNumDiRecords(const tSDP_DISCOVERY_DB* p_db) {
763 uint8_t num_records = 0;
764 tSDP_DISC_REC* p_curr_record = NULL;
765
766 do {
767 p_curr_record = SDP_FindServiceInDb(p_db, UUID_SERVCLASS_PNP_INFORMATION,
768 p_curr_record);
769 if (p_curr_record) num_records++;
770 } while (p_curr_record);
771
772 return num_records;
773 }
774
775 /*******************************************************************************
776 *
777 * Function SDP_AttrStringCopy
778 *
779 * Description This function copy given attribute to specified buffer as a
780 * string
781 *
782 * Returns none
783 *
784 ******************************************************************************/
SDP_AttrStringCopy(char * dst,const tSDP_DISC_ATTR * p_attr,uint16_t dst_size)785 static void SDP_AttrStringCopy(char* dst, const tSDP_DISC_ATTR* p_attr,
786 uint16_t dst_size) {
787 if (dst == NULL) return;
788 if (p_attr) {
789 uint16_t len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
790 if (len > dst_size - 1) {
791 len = dst_size - 1;
792 }
793 memcpy(dst, (const void*)p_attr->attr_value.v.array, len);
794 dst[len] = '\0';
795 } else {
796 dst[0] = '\0';
797 }
798 }
799
800 /*******************************************************************************
801 *
802 * Function SDP_GetDiRecord
803 *
804 * Description This function retrieves a remote device's DI record from
805 * the specified database.
806 *
807 * Returns SDP_SUCCESS if record retrieved, else error
808 *
809 ******************************************************************************/
SDP_GetDiRecord(uint8_t get_record_index,tSDP_DI_GET_RECORD * p_device_info,const tSDP_DISCOVERY_DB * p_db)810 uint16_t SDP_GetDiRecord(uint8_t get_record_index,
811 tSDP_DI_GET_RECORD* p_device_info,
812 const tSDP_DISCOVERY_DB* p_db) {
813 uint16_t result = SDP_NO_DI_RECORD_FOUND;
814 uint8_t curr_record_index = 1;
815
816 tSDP_DISC_REC* p_curr_record = NULL;
817
818 /* find the requested SDP record in the discovery database */
819 do {
820 p_curr_record = SDP_FindServiceInDb(p_db, UUID_SERVCLASS_PNP_INFORMATION,
821 p_curr_record);
822 if (p_curr_record) {
823 if (curr_record_index++ == get_record_index) {
824 result = SDP_SUCCESS;
825 break;
826 }
827 }
828 } while (p_curr_record);
829
830 if (result == SDP_SUCCESS) {
831 /* copy the information from the SDP record to the DI record */
832 tSDP_DISC_ATTR* p_curr_attr = NULL;
833
834 /* ClientExecutableURL is optional */
835 p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_CLIENT_EXE_URL);
836 SDP_AttrStringCopy(p_device_info->rec.client_executable_url, p_curr_attr,
837 SDP_MAX_ATTR_LEN);
838
839 /* Service Description is optional */
840 p_curr_attr =
841 SDP_FindAttributeInRec(p_curr_record, ATTR_ID_SERVICE_DESCRIPTION);
842 SDP_AttrStringCopy(p_device_info->rec.service_description, p_curr_attr,
843 SDP_MAX_ATTR_LEN);
844
845 /* DocumentationURL is optional */
846 p_curr_attr =
847 SDP_FindAttributeInRec(p_curr_record, ATTR_ID_DOCUMENTATION_URL);
848 SDP_AttrStringCopy(p_device_info->rec.documentation_url, p_curr_attr,
849 SDP_MAX_ATTR_LEN);
850
851 p_curr_attr =
852 SDP_FindAttributeInRec(p_curr_record, ATTR_ID_SPECIFICATION_ID);
853 if (p_curr_attr)
854 p_device_info->spec_id = p_curr_attr->attr_value.v.u16;
855 else
856 result = SDP_ERR_ATTR_NOT_PRESENT;
857
858 p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_VENDOR_ID);
859 if (p_curr_attr)
860 p_device_info->rec.vendor = p_curr_attr->attr_value.v.u16;
861 else
862 result = SDP_ERR_ATTR_NOT_PRESENT;
863
864 p_curr_attr =
865 SDP_FindAttributeInRec(p_curr_record, ATTR_ID_VENDOR_ID_SOURCE);
866 if (p_curr_attr)
867 p_device_info->rec.vendor_id_source = p_curr_attr->attr_value.v.u16;
868 else
869 result = SDP_ERR_ATTR_NOT_PRESENT;
870
871 p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_PRODUCT_ID);
872 if (p_curr_attr)
873 p_device_info->rec.product = p_curr_attr->attr_value.v.u16;
874 else
875 result = SDP_ERR_ATTR_NOT_PRESENT;
876
877 p_curr_attr =
878 SDP_FindAttributeInRec(p_curr_record, ATTR_ID_PRODUCT_VERSION);
879 if (p_curr_attr)
880 p_device_info->rec.version = p_curr_attr->attr_value.v.u16;
881 else
882 result = SDP_ERR_ATTR_NOT_PRESENT;
883
884 p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_PRIMARY_RECORD);
885 if (p_curr_attr)
886 p_device_info->rec.primary_record = (bool)p_curr_attr->attr_value.v.u8;
887 else
888 result = SDP_ERR_ATTR_NOT_PRESENT;
889 }
890
891 return result;
892 }
893
894 /*******************************************************************************
895 * Device Identification (DI) Server Functions
896 ******************************************************************************/
897
898 /*******************************************************************************
899 *
900 * Function SDP_SetLocalDiRecord
901 *
902 * Description This function adds a DI record to the local SDP database.
903 *
904 *
905 *
906 * Returns Returns SDP_SUCCESS if record added successfully, else error
907 *
908 ******************************************************************************/
SDP_SetLocalDiRecord(const tSDP_DI_RECORD * p_device_info,uint32_t * p_handle)909 uint16_t SDP_SetLocalDiRecord(const tSDP_DI_RECORD* p_device_info,
910 uint32_t* p_handle) {
911 uint16_t result = SDP_SUCCESS;
912 uint32_t handle;
913 uint16_t di_uuid = UUID_SERVCLASS_PNP_INFORMATION;
914 uint16_t di_specid = BLUETOOTH_DI_SPECIFICATION;
915 uint8_t temp_u16[2];
916 uint8_t* p_temp;
917 uint8_t u8;
918
919 *p_handle = 0;
920 if (p_device_info == NULL) return SDP_ILLEGAL_PARAMETER;
921
922 /* if record is to be primary record, get handle to replace old primary */
923 if (p_device_info->primary_record && sdp_cb.server_db.di_primary_handle)
924 handle = sdp_cb.server_db.di_primary_handle;
925 else {
926 handle = SDP_CreateRecord();
927 if (handle == 0) return SDP_NO_RESOURCES;
928 }
929
930 *p_handle = handle;
931
932 /* build the SDP entry */
933 /* Add the UUID to the Service Class ID List */
934 if (!(SDP_AddServiceClassIdList(handle, 1, &di_uuid)))
935 result = SDP_DI_REG_FAILED;
936
937 /* mandatory */
938 if (result == SDP_SUCCESS) {
939 p_temp = temp_u16;
940 UINT16_TO_BE_STREAM(p_temp, di_specid);
941 if (!(SDP_AddAttribute(handle, ATTR_ID_SPECIFICATION_ID, UINT_DESC_TYPE,
942 sizeof(di_specid), temp_u16)))
943 result = SDP_DI_REG_FAILED;
944 }
945
946 /* optional - if string is null, do not add attribute */
947 if (result == SDP_SUCCESS) {
948 if (p_device_info->client_executable_url[0] != '\0') {
949 if (!((strlen(p_device_info->client_executable_url) + 1 <=
950 SDP_MAX_ATTR_LEN) &&
951 SDP_AddAttribute(
952 handle, ATTR_ID_CLIENT_EXE_URL, URL_DESC_TYPE,
953 (uint32_t)(strlen(p_device_info->client_executable_url) + 1),
954 (uint8_t*)p_device_info->client_executable_url)))
955 result = SDP_DI_REG_FAILED;
956 }
957 }
958
959 /* optional - if string is null, do not add attribute */
960 if (result == SDP_SUCCESS) {
961 if (p_device_info->service_description[0] != '\0') {
962 if (!((strlen(p_device_info->service_description) + 1 <=
963 SDP_MAX_ATTR_LEN) &&
964 SDP_AddAttribute(
965 handle, ATTR_ID_SERVICE_DESCRIPTION, TEXT_STR_DESC_TYPE,
966 (uint32_t)(strlen(p_device_info->service_description) + 1),
967 (uint8_t*)p_device_info->service_description)))
968 result = SDP_DI_REG_FAILED;
969 }
970 }
971
972 /* optional - if string is null, do not add attribute */
973 if (result == SDP_SUCCESS) {
974 if (p_device_info->documentation_url[0] != '\0') {
975 if (!((strlen(p_device_info->documentation_url) + 1 <=
976 SDP_MAX_ATTR_LEN) &&
977 SDP_AddAttribute(
978 handle, ATTR_ID_DOCUMENTATION_URL, URL_DESC_TYPE,
979 (uint32_t)(strlen(p_device_info->documentation_url) + 1),
980 (uint8_t*)p_device_info->documentation_url)))
981 result = SDP_DI_REG_FAILED;
982 }
983 }
984
985 /* mandatory */
986 if (result == SDP_SUCCESS) {
987 p_temp = temp_u16;
988 UINT16_TO_BE_STREAM(p_temp, p_device_info->vendor);
989 if (!(SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID, UINT_DESC_TYPE,
990 sizeof(p_device_info->vendor), temp_u16)))
991 result = SDP_DI_REG_FAILED;
992 }
993
994 /* mandatory */
995 if (result == SDP_SUCCESS) {
996 p_temp = temp_u16;
997 UINT16_TO_BE_STREAM(p_temp, p_device_info->product);
998 if (!(SDP_AddAttribute(handle, ATTR_ID_PRODUCT_ID, UINT_DESC_TYPE,
999 sizeof(p_device_info->product), temp_u16)))
1000 result = SDP_DI_REG_FAILED;
1001 }
1002
1003 /* mandatory */
1004 if (result == SDP_SUCCESS) {
1005 p_temp = temp_u16;
1006 UINT16_TO_BE_STREAM(p_temp, p_device_info->version);
1007 if (!(SDP_AddAttribute(handle, ATTR_ID_PRODUCT_VERSION, UINT_DESC_TYPE,
1008 sizeof(p_device_info->version), temp_u16)))
1009 result = SDP_DI_REG_FAILED;
1010 }
1011
1012 /* mandatory */
1013 if (result == SDP_SUCCESS) {
1014 u8 = (uint8_t)p_device_info->primary_record;
1015 if (!(SDP_AddAttribute(handle, ATTR_ID_PRIMARY_RECORD, BOOLEAN_DESC_TYPE, 1,
1016 &u8)))
1017 result = SDP_DI_REG_FAILED;
1018 }
1019
1020 /* mandatory */
1021 if (result == SDP_SUCCESS) {
1022 p_temp = temp_u16;
1023 UINT16_TO_BE_STREAM(p_temp, p_device_info->vendor_id_source);
1024 if (!(SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID_SOURCE, UINT_DESC_TYPE,
1025 sizeof(p_device_info->vendor_id_source), temp_u16)))
1026 result = SDP_DI_REG_FAILED;
1027 }
1028
1029 if (result != SDP_SUCCESS)
1030 SDP_DeleteRecord(handle);
1031 else if (p_device_info->primary_record)
1032 sdp_cb.server_db.di_primary_handle = handle;
1033
1034 return result;
1035 }
1036
1037 /*******************************************************************************
1038 *
1039 * Function SDP_SetTraceLevel
1040 *
1041 * Description This function sets the trace level for SDP. If called with
1042 * a value of 0xFF, it simply reads the current trace level.
1043 *
1044 * Returns the new (current) trace level
1045 *
1046 ******************************************************************************/
SDP_SetTraceLevel(uint8_t new_level)1047 uint8_t SDP_SetTraceLevel(uint8_t new_level) {
1048 if (new_level != 0xFF) sdp_cb.trace_level = new_level;
1049
1050 return (sdp_cb.trace_level);
1051 }
1052