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