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 functions that handle the database
22 *
23 ******************************************************************************/
24
25 #include <string.h>
26
27 #include <cstdint>
28
29 #include "bt_target.h"
30 #include "osi/include/allocator.h"
31 #include "stack/include/bt_types.h"
32 #include "stack/include/sdp_api.h"
33 #include "stack/include/sdpdefs.h"
34 #include "stack/sdp/sdpint.h"
35 #include "types/bluetooth/uuid.h"
36
37 /******************************************************************************/
38 /* L O C A L F U N C T I O N P R O T O T Y P E S */
39 /******************************************************************************/
40 static bool find_uuid_in_seq(uint8_t* p, uint32_t seq_len,
41 const uint8_t* p_his_uuid, uint16_t his_len,
42 int nest_level);
43
44 /*******************************************************************************
45 *
46 * Function sdp_db_service_search
47 *
48 * Description This function searches for a record that contains the
49 * specified UIDs. It is passed either NULL to start at the
50 * beginning, or the previous record found.
51 *
52 * Returns Pointer to the record, or NULL if not found.
53 *
54 ******************************************************************************/
sdp_db_service_search(const tSDP_RECORD * p_rec,const tSDP_UUID_SEQ * p_seq)55 const tSDP_RECORD* sdp_db_service_search(const tSDP_RECORD* p_rec,
56 const tSDP_UUID_SEQ* p_seq) {
57 uint16_t xx, yy;
58 const tSDP_ATTRIBUTE* p_attr;
59 tSDP_RECORD* p_end = &sdp_cb.server_db.record[sdp_cb.server_db.num_records];
60
61 /* If NULL, start at the beginning, else start at the first specified record
62 */
63 if (!p_rec)
64 p_rec = &sdp_cb.server_db.record[0];
65 else
66 p_rec++;
67
68 /* Look through the records. The spec says that a match occurs if */
69 /* the record contains all the passed UUIDs in it. */
70 for (; p_rec < p_end; p_rec++) {
71 for (yy = 0; yy < p_seq->num_uids; yy++) {
72 p_attr = &p_rec->attribute[0];
73 for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
74 if (p_attr->type == UUID_DESC_TYPE) {
75 if (sdpu_compare_uuid_arrays(p_attr->value_ptr, p_attr->len,
76 &p_seq->uuid_entry[yy].value[0],
77 p_seq->uuid_entry[yy].len))
78 break;
79 } else if (p_attr->type == DATA_ELE_SEQ_DESC_TYPE) {
80 if (find_uuid_in_seq(p_attr->value_ptr, p_attr->len,
81 &p_seq->uuid_entry[yy].value[0],
82 p_seq->uuid_entry[yy].len, 0))
83 break;
84 }
85 }
86 /* If any UUID was not found, on to the next record */
87 if (xx == p_rec->num_attributes) break;
88 }
89
90 /* If every UUID was found in the record, return the record */
91 if (yy == p_seq->num_uids) return (p_rec);
92 }
93
94 /* If here, no more records found */
95 return (NULL);
96 }
97
98 /*******************************************************************************
99 *
100 * Function find_uuid_in_seq
101 *
102 * Description This function searches a data element sequenct for a UUID.
103 *
104 * Returns true if found, else false
105 *
106 ******************************************************************************/
find_uuid_in_seq(uint8_t * p,uint32_t seq_len,const uint8_t * p_uuid,uint16_t uuid_len,int nest_level)107 static bool find_uuid_in_seq(uint8_t* p, uint32_t seq_len,
108 const uint8_t* p_uuid, uint16_t uuid_len,
109 int nest_level) {
110 uint8_t* p_end = p + seq_len;
111 uint8_t type;
112 uint32_t len;
113
114 /* A little safety check to avoid excessive recursion */
115 if (nest_level > 3) return (false);
116
117 while (p < p_end) {
118 type = *p++;
119 p = sdpu_get_len_from_type(p, p_end, type, &len);
120 if (p == NULL || (p + len) > p_end) {
121 SDP_TRACE_WARNING("%s: bad length", __func__);
122 break;
123 }
124 type = type >> 3;
125 if (type == UUID_DESC_TYPE) {
126 if (sdpu_compare_uuid_arrays(p, len, p_uuid, uuid_len)) return (true);
127 } else if (type == DATA_ELE_SEQ_DESC_TYPE) {
128 if (find_uuid_in_seq(p, len, p_uuid, uuid_len, nest_level + 1))
129 return (true);
130 }
131 p = p + len;
132 }
133
134 /* If here, failed to match */
135 return (false);
136 }
137
138 /*******************************************************************************
139 *
140 * Function sdp_db_find_record
141 *
142 * Description This function searches for a record with a specific handle
143 * It is passed the handle of the record.
144 *
145 * Returns Pointer to the record, or NULL if not found.
146 *
147 ******************************************************************************/
sdp_db_find_record(uint32_t handle)148 tSDP_RECORD* sdp_db_find_record(uint32_t handle) {
149 tSDP_RECORD* p_rec;
150 tSDP_RECORD* p_end = &sdp_cb.server_db.record[sdp_cb.server_db.num_records];
151
152 /* Look through the records for the caller's handle */
153 for (p_rec = &sdp_cb.server_db.record[0]; p_rec < p_end; p_rec++) {
154 if (p_rec->record_handle == handle) return (p_rec);
155 }
156
157 /* Record with that handle not found. */
158 return (NULL);
159 }
160
161 /*******************************************************************************
162 *
163 * Function sdp_db_find_attr_in_rec
164 *
165 * Description This function searches a record for specific attributes.
166 * It is passed a pointer to the record. If the record contains
167 * the specified attribute, (the caller may specify be a range
168 * of attributes), the attribute is returned.
169 *
170 * Returns Pointer to the attribute, or NULL if not found.
171 *
172 ******************************************************************************/
sdp_db_find_attr_in_rec(const tSDP_RECORD * p_rec,uint16_t start_attr,uint16_t end_attr)173 const tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(const tSDP_RECORD* p_rec,
174 uint16_t start_attr,
175 uint16_t end_attr) {
176 const tSDP_ATTRIBUTE* p_at;
177 uint16_t xx;
178
179 /* Note that the attributes in a record are assumed to be in sorted order */
180 for (xx = 0, p_at = &p_rec->attribute[0]; xx < p_rec->num_attributes;
181 xx++, p_at++) {
182 if ((p_at->id >= start_attr) && (p_at->id <= end_attr)) return (p_at);
183 }
184
185 /* No matching attribute found */
186 return (NULL);
187 }
188
189 /*******************************************************************************
190 *
191 * Function sdp_compose_proto_list
192 *
193 * Description This function is called to compose a data sequence from
194 * protocol element list struct pointer
195 *
196 * Returns the length of the data sequence
197 *
198 ******************************************************************************/
sdp_compose_proto_list(uint8_t * p,uint16_t num_elem,tSDP_PROTOCOL_ELEM * p_elem_list)199 static int sdp_compose_proto_list(uint8_t* p, uint16_t num_elem,
200 tSDP_PROTOCOL_ELEM* p_elem_list) {
201 uint16_t xx, yy, len;
202 bool is_rfcomm_scn;
203 uint8_t* p_head = p;
204 uint8_t* p_len;
205
206 /* First, build the protocol list. This consists of a set of data element
207 ** sequences, one for each layer. Each layer sequence consists of layer's
208 ** UUID and optional parameters
209 */
210 for (xx = 0; xx < num_elem; xx++, p_elem_list++) {
211 len = 3 + (p_elem_list->num_params * 3);
212 UINT8_TO_BE_STREAM(p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
213
214 p_len = p;
215 *p++ = (uint8_t)len;
216
217 UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
218 UINT16_TO_BE_STREAM(p, p_elem_list->protocol_uuid);
219
220 if (p_elem_list->protocol_uuid == UUID_PROTOCOL_RFCOMM)
221 is_rfcomm_scn = true;
222 else
223 is_rfcomm_scn = false;
224
225 for (yy = 0; yy < p_elem_list->num_params; yy++) {
226 if (is_rfcomm_scn) {
227 UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_ONE_BYTE);
228 UINT8_TO_BE_STREAM(p, p_elem_list->params[yy]);
229
230 *p_len -= 1;
231 } else {
232 UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
233 UINT16_TO_BE_STREAM(p, p_elem_list->params[yy]);
234 }
235 }
236 }
237 return (p - p_head);
238 }
239
240 /*******************************************************************************
241 *
242 * Function SDP_CreateRecord
243 *
244 * Description This function is called to create a record in the database.
245 * This would be through the SDP database maintenance API. The
246 * record is created empty, teh application should then call
247 * "add_attribute" to add the record's attributes.
248 *
249 * Returns Record handle if OK, else 0.
250 *
251 ******************************************************************************/
SDP_CreateRecord(void)252 uint32_t SDP_CreateRecord(void) {
253 uint32_t handle;
254 uint8_t buf[4];
255 tSDP_DB* p_db = &sdp_cb.server_db;
256
257 /* First, check if there is a free record */
258 if (p_db->num_records < SDP_MAX_RECORDS) {
259 memset(&p_db->record[p_db->num_records], 0, sizeof(tSDP_RECORD));
260
261 /* We will use a handle of the first unreserved handle plus last record
262 ** number + 1 */
263 if (p_db->num_records)
264 handle = p_db->record[p_db->num_records - 1].record_handle + 1;
265 else
266 handle = 0x10000;
267
268 p_db->record[p_db->num_records].record_handle = handle;
269
270 p_db->num_records++;
271 SDP_TRACE_DEBUG("SDP_CreateRecord ok, num_records:%d", p_db->num_records);
272 /* Add the first attribute (the handle) automatically */
273 UINT32_TO_BE_FIELD(buf, handle);
274 SDP_AddAttribute(handle, ATTR_ID_SERVICE_RECORD_HDL, UINT_DESC_TYPE, 4,
275 buf);
276
277 return (p_db->record[p_db->num_records - 1].record_handle);
278 } else
279 SDP_TRACE_ERROR("SDP_CreateRecord fail, exceed maximum records:%d",
280 SDP_MAX_RECORDS);
281 return (0);
282 }
283
284 /*******************************************************************************
285 *
286 * Function SDP_DeleteRecord
287 *
288 * Description This function is called to add a record (or all records)
289 * from the database. This would be through the SDP database
290 * maintenance API.
291 *
292 * If a record handle of 0 is passed, all records are deleted.
293 *
294 * Returns true if succeeded, else false
295 *
296 ******************************************************************************/
SDP_DeleteRecord(uint32_t handle)297 bool SDP_DeleteRecord(uint32_t handle) {
298 uint16_t xx, yy, zz;
299 tSDP_RECORD* p_rec = &sdp_cb.server_db.record[0];
300
301 if (handle == 0 || sdp_cb.server_db.num_records == 0) {
302 /* Delete all records in the database */
303 sdp_cb.server_db.num_records = 0;
304
305 /* require new DI record to be created in SDP_SetLocalDiRecord */
306 sdp_cb.server_db.di_primary_handle = 0;
307
308 return (true);
309 } else {
310 /* Find the record in the database */
311 for (xx = 0; xx < sdp_cb.server_db.num_records; xx++, p_rec++) {
312 if (p_rec->record_handle == handle) {
313 /* Found it. Shift everything up one */
314 for (yy = xx; yy < sdp_cb.server_db.num_records - 1; yy++, p_rec++) {
315 *p_rec = *(p_rec + 1);
316
317 /* Adjust the attribute value pointer for each attribute */
318 for (zz = 0; zz < p_rec->num_attributes; zz++)
319 p_rec->attribute[zz].value_ptr -= sizeof(tSDP_RECORD);
320 }
321
322 sdp_cb.server_db.num_records--;
323
324 SDP_TRACE_DEBUG("SDP_DeleteRecord ok, num_records:%d",
325 sdp_cb.server_db.num_records);
326 /* if we're deleting the primary DI record, clear the */
327 /* value in the control block */
328 if (sdp_cb.server_db.di_primary_handle == handle) {
329 sdp_cb.server_db.di_primary_handle = 0;
330 }
331
332 return (true);
333 }
334 }
335 }
336 return (false);
337 }
338
339 /*******************************************************************************
340 *
341 * Function SDP_AddAttribute
342 *
343 * Description This function is called to add an attribute to a record.
344 * This would be through the SDP database maintenance API.
345 * If the attribute already exists in the record, it is
346 * replaced with the new value.
347 *
348 * NOTE Attribute values must be passed as a Big Endian stream.
349 *
350 * Returns true if added OK, else false
351 *
352 ******************************************************************************/
SDP_AddAttribute(uint32_t handle,uint16_t attr_id,uint8_t attr_type,uint32_t attr_len,uint8_t * p_val)353 bool SDP_AddAttribute(uint32_t handle, uint16_t attr_id, uint8_t attr_type,
354 uint32_t attr_len, uint8_t* p_val) {
355 uint16_t xx, yy, zz;
356 tSDP_RECORD* p_rec = &sdp_cb.server_db.record[0];
357
358 if (p_val == nullptr) {
359 SDP_TRACE_WARNING("Trying to add attribute with p_val == nullptr, skipped");
360 return (false);
361 }
362
363 if (sdp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) {
364 if ((attr_type == UINT_DESC_TYPE) ||
365 (attr_type == TWO_COMP_INT_DESC_TYPE) ||
366 (attr_type == UUID_DESC_TYPE) ||
367 (attr_type == DATA_ELE_SEQ_DESC_TYPE) ||
368 (attr_type == DATA_ELE_ALT_DESC_TYPE)) {
369 uint8_t num_array[400];
370 uint32_t len = (attr_len > 200) ? 200 : attr_len;
371
372 num_array[0] = '\0';
373 for (uint32_t i = 0; i < len; i++) {
374 snprintf((char*)&num_array[i * 2], sizeof(num_array) - i * 2, "%02X",
375 (uint8_t)(p_val[i]));
376 }
377 SDP_TRACE_DEBUG(
378 "SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, "
379 "*p_val:%s",
380 handle, attr_id, attr_type, attr_len, p_val, num_array);
381 } else if (attr_type == BOOLEAN_DESC_TYPE) {
382 SDP_TRACE_DEBUG(
383 "SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, "
384 "*p_val:%d",
385 handle, attr_id, attr_type, attr_len, p_val, *p_val);
386 } else if ((attr_type == TEXT_STR_DESC_TYPE) ||
387 (attr_type == URL_DESC_TYPE)) {
388 if (p_val[attr_len - 1] == '\0') {
389 SDP_TRACE_DEBUG(
390 "SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, "
391 "*p_val:%s",
392 handle, attr_id, attr_type, attr_len, p_val, (char*)p_val);
393 } else {
394 SDP_TRACE_DEBUG(
395 "SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p",
396 handle, attr_id, attr_type, attr_len, p_val);
397 }
398 } else {
399 SDP_TRACE_DEBUG(
400 "SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p",
401 handle, attr_id, attr_type, attr_len, p_val);
402 }
403 }
404
405 /* Find the record in the database */
406 for (zz = 0; zz < sdp_cb.server_db.num_records; zz++, p_rec++) {
407 if (p_rec->record_handle == handle) {
408 tSDP_ATTRIBUTE* p_attr = &p_rec->attribute[0];
409
410 // error out early, no need to look up
411 if (p_rec->free_pad_ptr >= SDP_MAX_PAD_LEN) {
412 SDP_TRACE_ERROR("the free pad for SDP record with handle %d is "
413 "full, skip adding the attribute", handle);
414 return (false);
415 }
416
417 /* Found the record. Now, see if the attribute already exists */
418 for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
419 /* The attribute exists. replace it */
420 if (p_attr->id == attr_id) {
421 SDP_DeleteAttribute(handle, attr_id);
422 break;
423 }
424 if (p_attr->id > attr_id) break;
425 }
426
427 if (p_rec->num_attributes == SDP_MAX_REC_ATTR) return (false);
428
429 /* If not found, see if we can allocate a new entry */
430 if (xx == p_rec->num_attributes)
431 p_attr = &p_rec->attribute[p_rec->num_attributes];
432 else {
433 /* Since the attributes are kept in sorted order, insert ours here */
434 for (yy = p_rec->num_attributes; yy > xx; yy--)
435 p_rec->attribute[yy] = p_rec->attribute[yy - 1];
436 }
437
438 p_attr->id = attr_id;
439 p_attr->type = attr_type;
440 p_attr->len = attr_len;
441
442 if (p_rec->free_pad_ptr + attr_len >= SDP_MAX_PAD_LEN) {
443 /* do truncate only for text string type descriptor */
444 if (attr_type == TEXT_STR_DESC_TYPE) {
445 SDP_TRACE_WARNING(
446 "SDP_AddAttribute: attr_len:%d too long. truncate to (%d)",
447 attr_len, SDP_MAX_PAD_LEN - p_rec->free_pad_ptr);
448
449 attr_len = SDP_MAX_PAD_LEN - p_rec->free_pad_ptr;
450 p_val[SDP_MAX_PAD_LEN - p_rec->free_pad_ptr - 1] = '\0';
451 } else
452 attr_len = 0;
453 }
454
455 if (attr_len > 0) {
456 p_attr->len = attr_len;
457 memcpy(&p_rec->attr_pad[p_rec->free_pad_ptr], p_val, (size_t)attr_len);
458 p_attr->value_ptr = &p_rec->attr_pad[p_rec->free_pad_ptr];
459 p_rec->free_pad_ptr += attr_len;
460 } else if (attr_len == 0 && p_attr->len != 0) {
461 /* if truncate to 0 length, simply don't add */
462 SDP_TRACE_ERROR(
463 "SDP_AddAttribute fail, length exceed maximum: ID %d: attr_len:%d ",
464 attr_id, attr_len);
465 p_attr->id = p_attr->type = p_attr->len = 0;
466 return (false);
467 }
468 p_rec->num_attributes++;
469 return (true);
470 }
471 }
472 return (false);
473 }
474
475 /*******************************************************************************
476 *
477 * Function SDP_AddSequence
478 *
479 * Description This function is called to add a sequence to a record.
480 * This would be through the SDP database maintenance API.
481 * If the sequence already exists in the record, it is replaced
482 * with the new sequence.
483 *
484 * NOTE Element values must be passed as a Big Endian stream.
485 *
486 * Returns true if added OK, else false
487 *
488 ******************************************************************************/
SDP_AddSequence(uint32_t handle,uint16_t attr_id,uint16_t num_elem,uint8_t type[],uint8_t len[],uint8_t * p_val[])489 bool SDP_AddSequence(uint32_t handle, uint16_t attr_id, uint16_t num_elem,
490 uint8_t type[], uint8_t len[], uint8_t* p_val[]) {
491 uint16_t xx;
492 uint8_t* p;
493 uint8_t* p_head;
494 bool result;
495 uint8_t* p_buff =
496 (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
497
498 p = p_buff;
499
500 /* First, build the sequence */
501 for (xx = 0; xx < num_elem; xx++) {
502 p_head = p;
503 switch (len[xx]) {
504 case 1:
505 UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_ONE_BYTE);
506 break;
507 case 2:
508 UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_TWO_BYTES);
509 break;
510 case 4:
511 UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_FOUR_BYTES);
512 break;
513 case 8:
514 UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_EIGHT_BYTES);
515 break;
516 case 16:
517 UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_SIXTEEN_BYTES);
518 break;
519 default:
520 UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_IN_NEXT_BYTE);
521 UINT8_TO_BE_STREAM(p, len[xx]);
522 break;
523 }
524
525 ARRAY_TO_BE_STREAM(p, p_val[xx], len[xx]);
526
527 if (p - p_buff > SDP_MAX_ATTR_LEN) {
528 /* go back to before we add this element */
529 p = p_head;
530 if (p_head == p_buff) {
531 /* the first element exceed the max length */
532 SDP_TRACE_ERROR("SDP_AddSequence - too long(attribute is not added)!!");
533 osi_free(p_buff);
534 return false;
535 } else
536 SDP_TRACE_ERROR("SDP_AddSequence - too long, add %d elements of %d", xx,
537 num_elem);
538 break;
539 }
540 }
541 result = SDP_AddAttribute(handle, attr_id, DATA_ELE_SEQ_DESC_TYPE,
542 (uint32_t)(p - p_buff), p_buff);
543 osi_free(p_buff);
544 return result;
545 }
546
547 /*******************************************************************************
548 *
549 * Function SDP_AddUuidSequence
550 *
551 * Description This function is called to add a UUID sequence to a record.
552 * This would be through the SDP database maintenance API.
553 * If the sequence already exists in the record, it is replaced
554 * with the new sequence.
555 *
556 * Returns true if added OK, else false
557 *
558 ******************************************************************************/
SDP_AddUuidSequence(uint32_t handle,uint16_t attr_id,uint16_t num_uuids,uint16_t * p_uuids)559 bool SDP_AddUuidSequence(uint32_t handle, uint16_t attr_id, uint16_t num_uuids,
560 uint16_t* p_uuids) {
561 uint16_t xx;
562 uint8_t* p;
563 int32_t max_len = SDP_MAX_ATTR_LEN - 3;
564 bool result;
565 uint8_t* p_buff =
566 (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
567
568 p = p_buff;
569
570 /* First, build the sequence */
571 for (xx = 0; xx < num_uuids; xx++, p_uuids++) {
572 UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
573 UINT16_TO_BE_STREAM(p, *p_uuids);
574
575 if ((p - p_buff) > max_len) {
576 SDP_TRACE_WARNING("SDP_AddUuidSequence - too long, add %d uuids of %d",
577 xx, num_uuids);
578 break;
579 }
580 }
581
582 result = SDP_AddAttribute(handle, attr_id, DATA_ELE_SEQ_DESC_TYPE,
583 (uint32_t)(p - p_buff), p_buff);
584 osi_free(p_buff);
585 return result;
586 }
587
588 /*******************************************************************************
589 *
590 * Function SDP_AddProtocolList
591 *
592 * Description This function is called to add a protocol descriptor list to
593 * a record. This would be through the SDP database
594 * maintenance API. If the protocol list already exists in the
595 * record, it is replaced with the new list.
596 *
597 * Returns true if added OK, else false
598 *
599 ******************************************************************************/
SDP_AddProtocolList(uint32_t handle,uint16_t num_elem,tSDP_PROTOCOL_ELEM * p_elem_list)600 bool SDP_AddProtocolList(uint32_t handle, uint16_t num_elem,
601 tSDP_PROTOCOL_ELEM* p_elem_list) {
602 int offset;
603 bool result;
604 uint8_t* p_buff =
605 (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
606
607 offset = sdp_compose_proto_list(p_buff, num_elem, p_elem_list);
608 result = SDP_AddAttribute(handle, ATTR_ID_PROTOCOL_DESC_LIST,
609 DATA_ELE_SEQ_DESC_TYPE, (uint32_t)offset, p_buff);
610 osi_free(p_buff);
611 return result;
612 }
613
614 /*******************************************************************************
615 *
616 * Function SDP_AddAdditionProtoLists
617 *
618 * Description This function is called to add a protocol descriptor list to
619 * a record. This would be through the SDP database maintenance
620 * API. If the protocol list already exists in the record, it
621 * is replaced with the new list.
622 *
623 * Returns true if added OK, else false
624 *
625 ******************************************************************************/
SDP_AddAdditionProtoLists(uint32_t handle,uint16_t num_elem,tSDP_PROTO_LIST_ELEM * p_proto_list)626 bool SDP_AddAdditionProtoLists(uint32_t handle, uint16_t num_elem,
627 tSDP_PROTO_LIST_ELEM* p_proto_list) {
628 uint16_t xx;
629 uint8_t* p;
630 uint8_t* p_len;
631 int offset;
632 bool result;
633 uint8_t* p_buff =
634 (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
635
636 p = p_buff;
637
638 /* for each ProtocolDescriptorList */
639 for (xx = 0; xx < num_elem; xx++, p_proto_list++) {
640 UINT8_TO_BE_STREAM(p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
641 p_len = p++;
642
643 offset = sdp_compose_proto_list(p, p_proto_list->num_elems,
644 p_proto_list->list_elem);
645 p += offset;
646
647 *p_len = (uint8_t)(p - p_len - 1);
648 }
649 result =
650 SDP_AddAttribute(handle, ATTR_ID_ADDITION_PROTO_DESC_LISTS,
651 DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - p_buff), p_buff);
652 osi_free(p_buff);
653 return result;
654 }
655
656 /*******************************************************************************
657 *
658 * Function SDP_AddProfileDescriptorList
659 *
660 * Description This function is called to add a profile descriptor list to
661 * a record. This would be through the SDP database maintenance
662 * API. If the version already exists in the record, it is
663 * replaced with the new one.
664 *
665 * Returns true if added OK, else false
666 *
667 ******************************************************************************/
SDP_AddProfileDescriptorList(uint32_t handle,uint16_t profile_uuid,uint16_t version)668 bool SDP_AddProfileDescriptorList(uint32_t handle, uint16_t profile_uuid,
669 uint16_t version) {
670 uint8_t* p;
671 bool result;
672 uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN);
673
674 p = p_buff + 2;
675
676 /* First, build the profile descriptor list. This consists of a data element
677 * sequence. */
678 /* The sequence consists of profile's UUID and version number */
679 UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
680 UINT16_TO_BE_STREAM(p, profile_uuid);
681
682 UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
683 UINT16_TO_BE_STREAM(p, version);
684
685 /* Add in type and length fields */
686 *p_buff = (uint8_t)((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
687 *(p_buff + 1) = (uint8_t)(p - (p_buff + 2));
688
689 result =
690 SDP_AddAttribute(handle, ATTR_ID_BT_PROFILE_DESC_LIST,
691 DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - p_buff), p_buff);
692 osi_free(p_buff);
693 return result;
694 }
695
696 /*******************************************************************************
697 *
698 * Function SDP_AddLanguageBaseAttrIDList
699 *
700 * Description This function is called to add a language base attr list to
701 * a record. This would be through the SDP database maintenance
702 * API. If the version already exists in the record, it is
703 * replaced with the new one.
704 *
705 * Returns true if added OK, else false
706 *
707 ******************************************************************************/
SDP_AddLanguageBaseAttrIDList(uint32_t handle,uint16_t lang,uint16_t char_enc,uint16_t base_id)708 bool SDP_AddLanguageBaseAttrIDList(uint32_t handle, uint16_t lang,
709 uint16_t char_enc, uint16_t base_id) {
710 uint8_t* p;
711 bool result;
712 uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN);
713
714 p = p_buff;
715
716 /* First, build the language base descriptor list. This consists of a data */
717 /* element sequence. The sequence consists of 9 bytes (3 UINt16 fields) */
718 UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
719 UINT16_TO_BE_STREAM(p, lang);
720
721 UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
722 UINT16_TO_BE_STREAM(p, char_enc);
723
724 UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
725 UINT16_TO_BE_STREAM(p, base_id);
726
727 result =
728 SDP_AddAttribute(handle, ATTR_ID_LANGUAGE_BASE_ATTR_ID_LIST,
729 DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - p_buff), p_buff);
730 osi_free(p_buff);
731 return result;
732 }
733
734 /*******************************************************************************
735 *
736 * Function SDP_AddServiceClassIdList
737 *
738 * Description This function is called to add a service list to a record.
739 * This would be through the SDP database maintenance API.
740 * If the service list already exists in the record, it is
741 * replaced with the new list.
742 *
743 * Returns true if added OK, else false
744 *
745 ******************************************************************************/
SDP_AddServiceClassIdList(uint32_t handle,uint16_t num_services,uint16_t * p_service_uuids)746 bool SDP_AddServiceClassIdList(uint32_t handle, uint16_t num_services,
747 uint16_t* p_service_uuids) {
748 uint16_t xx;
749 uint8_t* p;
750 bool result;
751 uint8_t* p_buff =
752 (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
753
754 p = p_buff;
755
756 for (xx = 0; xx < num_services; xx++, p_service_uuids++) {
757 UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
758 UINT16_TO_BE_STREAM(p, *p_service_uuids);
759 }
760
761 result =
762 SDP_AddAttribute(handle, ATTR_ID_SERVICE_CLASS_ID_LIST,
763 DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - p_buff), p_buff);
764 osi_free(p_buff);
765 return result;
766 }
767
768 /*******************************************************************************
769 *
770 * Function SDP_DeleteAttribute
771 *
772 * Description This function is called to delete an attribute from a
773 * record. This would be through the SDP database maintenance
774 * API.
775 *
776 * Returns true if deleted OK, else false if not found
777 *
778 ******************************************************************************/
SDP_DeleteAttribute(uint32_t handle,uint16_t attr_id)779 bool SDP_DeleteAttribute(uint32_t handle, uint16_t attr_id) {
780 tSDP_RECORD* p_rec = &sdp_cb.server_db.record[0];
781 uint8_t* pad_ptr;
782 uint32_t len; /* Number of bytes in the entry */
783
784 /* Find the record in the database */
785 for (uint16_t record_index = 0; record_index < sdp_cb.server_db.num_records; record_index++, p_rec++) {
786 if (p_rec->record_handle == handle) {
787 tSDP_ATTRIBUTE* p_attr = &p_rec->attribute[0];
788
789 SDP_TRACE_API("Deleting attr_id 0x%04x for handle 0x%x", attr_id, handle);
790 /* Found it. Now, find the attribute */
791 for (uint16_t attribute_index = 0; attribute_index < p_rec->num_attributes; attribute_index++, p_attr++) {
792 if (p_attr->id == attr_id) {
793 pad_ptr = p_attr->value_ptr;
794 len = p_attr->len;
795
796 if (len) {
797 for (uint16_t zz = 0; zz < p_rec->num_attributes; zz++) {
798 if (p_rec->attribute[zz].value_ptr > pad_ptr)
799 p_rec->attribute[zz].value_ptr -= len;
800 }
801 }
802
803 /* Found it. Shift everything up one */
804 p_rec->num_attributes--;
805
806 for (uint16_t zz = attribute_index; zz < p_rec->num_attributes; zz++, p_attr++) {
807 *p_attr = *(p_attr + 1);
808 }
809
810 /* adjust attribute values if needed */
811 if (len) {
812 uint16_t last_attribute_to_adjust =
813 (p_rec->free_pad_ptr - ((pad_ptr + len) - &p_rec->attr_pad[0]));
814 for (uint16_t zz = 0; zz < last_attribute_to_adjust; zz++, pad_ptr++) {
815 *pad_ptr = *(pad_ptr + len);
816 }
817 p_rec->free_pad_ptr -= len;
818 }
819 return (true);
820 }
821 }
822 }
823 }
824 /* If here, not found */
825 return (false);
826 }
827