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