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, teh 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 	} else {
325             SDP_TRACE_ERROR("SDP_CreateRecord fail, memory allocation failed\n");
326 	}
327     } else {
328         SDP_TRACE_ERROR("SDP_CreateRecord fail, exceed maximum records:%d\n", SDP_MAX_RECORDS);
329     }
330 #endif
331     return (0);
332 }
333 
334 
335 /*******************************************************************************
336 **
337 ** Function         SDP_DeleteRecord
338 **
339 ** Description      This function is called to add a record (or all records)
340 **                  from the database. This would be through the SDP database
341 **                  maintenance API.
342 **
343 **                  If a record handle of 0 is passed, all records are deleted.
344 **
345 ** Returns          TRUE if succeeded, else FALSE
346 **
347 *******************************************************************************/
SDP_DeleteRecord(UINT32 handle)348 BOOLEAN SDP_DeleteRecord (UINT32 handle)
349 {
350 #if SDP_SERVER_ENABLED == TRUE
351     tSDP_RECORD     *p_rec  = NULL;
352     list_node_t     *p_node = NULL;
353 
354     if (handle == 0 || sdp_cb.server_db.num_records == 0) {
355         /* Delete all records in the database */
356         sdp_cb.server_db.num_records = 0;
357         for(p_node = list_begin(sdp_cb.server_db.p_record_list); p_node; p_node = list_next(p_node)) {
358 	    list_remove(sdp_cb.server_db.p_record_list, p_node);
359 	}
360         /* require new DI record to be created in SDP_SetLocalDiRecord */
361         sdp_cb.server_db.di_primary_handle = 0;
362 
363         return (TRUE);
364     } else {
365         /* Find the record in the database */
366         for(p_node = list_begin(sdp_cb.server_db.p_record_list); p_node; p_node = list_next(p_node)) {
367 	    p_rec = list_node(p_node);
368             if (p_rec->record_handle == handle) {
369                 /* Found it. Shift everything up one */
370                 list_remove(sdp_cb.server_db.p_record_list, p_rec);
371 
372                 sdp_cb.server_db.num_records--;
373 
374                 SDP_TRACE_DEBUG("SDP_DeleteRecord ok, num_records:%d\n", sdp_cb.server_db.num_records);
375                 /* if we're deleting the primary DI record, clear the */
376                 /* value in the control block */
377                 if ( sdp_cb.server_db.di_primary_handle == handle ) {
378                     sdp_cb.server_db.di_primary_handle = 0;
379                 }
380 
381                 return (TRUE);
382             }
383         }
384     }
385 #endif
386     return (FALSE);
387 }
388 
389 
390 /*******************************************************************************
391 **
392 ** Function         SDP_AddAttribute
393 **
394 ** Description      This function is called to add an attribute to a record.
395 **                  This would be through the SDP database maintenance API.
396 **                  If the attribute already exists in the record, it is replaced
397 **                  with the new value.
398 **
399 ** NOTE             Attribute values must be passed as a Big Endian stream.
400 **
401 ** Returns          TRUE if added OK, else FALSE
402 **
403 *******************************************************************************/
SDP_AddAttribute(UINT32 handle,UINT16 attr_id,UINT8 attr_type,UINT32 attr_len,UINT8 * p_val)404 BOOLEAN SDP_AddAttribute (UINT32 handle, UINT16 attr_id, UINT8 attr_type,
405                           UINT32 attr_len, UINT8 *p_val)
406 {
407 #if SDP_SERVER_ENABLED == TRUE
408     UINT16          xx, yy;
409     tSDP_RECORD     *p_rec = NULL;
410     list_node_t     *p_node= NULL;
411 
412 #if (BT_TRACE_VERBOSE == TRUE)
413     if (sdp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) {
414         if ((attr_type == UINT_DESC_TYPE) ||
415                 (attr_type == TWO_COMP_INT_DESC_TYPE) ||
416                 (attr_type == UUID_DESC_TYPE) ||
417                 (attr_type == DATA_ELE_SEQ_DESC_TYPE) ||
418                 (attr_type == DATA_ELE_ALT_DESC_TYPE)) {
419             UINT8 num_array[400];
420             UINT32 i;
421             UINT32 len = (attr_len > 200) ? 200 : attr_len;
422 
423             num_array[0] = '\0';
424             for (i = 0; i < len; i++) {
425                 sprintf((char *)&num_array[i * 2], "%02X", (UINT8)(p_val[i]));
426             }
427             SDP_TRACE_DEBUG("SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, *p_val:%s\n",
428                             handle, attr_id, attr_type, attr_len, p_val, num_array);
429         } else if (attr_type == BOOLEAN_DESC_TYPE) {
430             SDP_TRACE_DEBUG("SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, *p_val:%d\n",
431                             handle, attr_id, attr_type, attr_len, p_val, *p_val);
432         } else {
433             SDP_TRACE_DEBUG("SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, *p_val:%s\n",
434                             handle, attr_id, attr_type, attr_len, p_val, p_val);
435         }
436     }
437 #endif
438 
439     /* Find the record in the database */
440     for(p_node = list_begin(sdp_cb.server_db.p_record_list); p_node; p_node = list_next(p_node)) {
441 	p_rec= list_node(p_node);
442         if (p_rec->record_handle == handle) {
443             tSDP_ATTRIBUTE  *p_attr = &p_rec->attribute[0];
444 
445             /* Found the record. Now, see if the attribute already exists */
446             for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
447                 /* The attribute exists. replace it */
448                 if (p_attr->id == attr_id) {
449                     SDP_DeleteAttribute (handle, attr_id);
450                     break;
451                 }
452                 if (p_attr->id > attr_id) {
453                     break;
454                 }
455             }
456 
457             if (p_rec->num_attributes == SDP_MAX_REC_ATTR) {
458                 return (FALSE);
459             }
460 
461             /* If not found, see if we can allocate a new entry */
462             if (xx == p_rec->num_attributes) {
463                 p_attr = &p_rec->attribute[p_rec->num_attributes];
464             } else {
465                 /* Since the attributes are kept in sorted order, insert ours here */
466                 for (yy = p_rec->num_attributes; yy > xx; yy--) {
467                     p_rec->attribute[yy] = p_rec->attribute[yy - 1];
468                 }
469             }
470 
471             p_attr->id   = attr_id;
472             p_attr->type = attr_type;
473             p_attr->len  = attr_len;
474 
475             if (p_rec->free_pad_ptr + attr_len >= SDP_MAX_PAD_LEN) {
476                 /* do truncate only for text string type descriptor */
477                 if (attr_type == TEXT_STR_DESC_TYPE) {
478                     SDP_TRACE_WARNING("SDP_AddAttribute: attr_len:%d too long. truncate to (%d)\n",
479                                       attr_len, SDP_MAX_PAD_LEN - p_rec->free_pad_ptr );
480 
481                     attr_len = SDP_MAX_PAD_LEN - p_rec->free_pad_ptr;
482                     p_val[SDP_MAX_PAD_LEN - p_rec->free_pad_ptr] = '\0';
483                     p_val[SDP_MAX_PAD_LEN - p_rec->free_pad_ptr + 1] = '\0';
484                 } else {
485                     attr_len = 0;
486                 }
487             }
488 
489             if ((attr_len > 0) && (p_val != 0)) {
490                 p_attr->len  = attr_len;
491                 memcpy (&p_rec->attr_pad[p_rec->free_pad_ptr], p_val, (size_t)attr_len);
492                 p_attr->value_ptr = &p_rec->attr_pad[p_rec->free_pad_ptr];
493                 p_rec->free_pad_ptr += attr_len;
494             } else if ((attr_len == 0 && p_attr->len != 0) || /* if truncate to 0 length, simply don't add */
495                        p_val == 0) {
496                 SDP_TRACE_ERROR("SDP_AddAttribute fail, length exceed maximum: ID %d: attr_len:%d \n",
497                                 attr_id, attr_len );
498                 p_attr->id   = p_attr->type = p_attr->len  = 0;
499                 return (FALSE);
500             }
501             p_rec->num_attributes++;
502             return (TRUE);
503         }
504     }
505 #endif
506     return (FALSE);
507 }
508 
509 
510 /*******************************************************************************
511 **
512 ** Function         SDP_AddSequence
513 **
514 ** Description      This function is called to add a sequence to a record.
515 **                  This would be through the SDP database maintenance API.
516 **                  If the sequence already exists in the record, it is replaced
517 **                  with the new sequence.
518 **
519 ** NOTE             Element values must be passed as a Big Endian stream.
520 **
521 ** Returns          TRUE if added OK, else FALSE
522 **
523 *******************************************************************************/
SDP_AddSequence(UINT32 handle,UINT16 attr_id,UINT16 num_elem,UINT8 type[],UINT8 len[],UINT8 * p_val[])524 BOOLEAN SDP_AddSequence (UINT32 handle,  UINT16 attr_id, UINT16 num_elem,
525                          UINT8 type[], UINT8 len[], UINT8 *p_val[])
526 {
527 #if SDP_SERVER_ENABLED == TRUE
528     UINT16          xx;
529     UINT8           *p_buff;
530     UINT8           *p;
531     UINT8           *p_head;
532     BOOLEAN         result;
533 
534     if ((p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
535         SDP_TRACE_ERROR("SDP_AddSequence cannot get a buffer!\n");
536         return (FALSE);
537     }
538     p = p_buff;
539 
540     /* First, build the sequence */
541     for (xx = 0; xx < num_elem; xx++) {
542         p_head = p;
543         switch (len[xx]) {
544         case 1:
545             UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_ONE_BYTE);
546             break;
547         case 2:
548             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_TWO_BYTES);
549             break;
550         case 4:
551             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_FOUR_BYTES);
552             break;
553         case 8:
554             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_EIGHT_BYTES);
555             break;
556         case 16:
557             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_SIXTEEN_BYTES);
558             break;
559         default:
560             UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_IN_NEXT_BYTE);
561             UINT8_TO_BE_STREAM (p, len[xx]);
562             break;
563         }
564 
565         ARRAY_TO_BE_STREAM (p, p_val[xx], len[xx]);
566 
567         if (p - p_buff > SDP_MAX_ATTR_LEN) {
568             /* go back to before we add this element */
569             p = p_head;
570             if (p_head == p_buff) {
571                 /* the first element exceed the max length */
572                 SDP_TRACE_ERROR ("SDP_AddSequence - too long(attribute is not added)!!\n");
573                 osi_free(p_buff);
574                 return FALSE;
575             } else {
576                 SDP_TRACE_ERROR ("SDP_AddSequence - too long, add %d elements of %d\n", xx, num_elem);
577             }
578             break;
579         }
580     }
581     result = SDP_AddAttribute (handle, attr_id, DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p - p_buff), p_buff);
582     osi_free(p_buff);
583     return result;
584 #else   /* SDP_SERVER_ENABLED == FALSE */
585     return (FALSE);
586 #endif
587 }
588 
589 
590 /*******************************************************************************
591 **
592 ** Function         SDP_AddUuidSequence
593 **
594 ** Description      This function is called to add a UUID sequence to a record.
595 **                  This would be through the SDP database maintenance API.
596 **                  If the sequence already exists in the record, it is replaced
597 **                  with the new sequence.
598 **
599 ** Returns          TRUE if added OK, else FALSE
600 **
601 *******************************************************************************/
SDP_AddUuidSequence(UINT32 handle,UINT16 attr_id,UINT16 num_uuids,UINT16 * p_uuids)602 BOOLEAN SDP_AddUuidSequence (UINT32 handle,  UINT16 attr_id, UINT16 num_uuids,
603                              UINT16 *p_uuids)
604 {
605 #if SDP_SERVER_ENABLED == TRUE
606     UINT16          xx;
607     UINT8           *p_buff;
608     UINT8           *p;
609     INT32           max_len = SDP_MAX_ATTR_LEN - 3;
610     BOOLEAN         result;
611 
612     if ((p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
613         SDP_TRACE_ERROR("SDP_AddUuidSequence cannot get a buffer!\n");
614         return (FALSE);
615     }
616     p = p_buff;
617 
618     /* First, build the sequence */
619     for (xx = 0; xx < num_uuids ; xx++, p_uuids++) {
620         UINT8_TO_BE_STREAM  (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
621         UINT16_TO_BE_STREAM (p, *p_uuids);
622 
623         if ((p - p_buff) > max_len) {
624             SDP_TRACE_WARNING ("SDP_AddUuidSequence - too long, add %d uuids of %d\n", xx, num_uuids);
625             break;
626         }
627     }
628 
629     result = SDP_AddAttribute (handle, attr_id, DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p - p_buff), p_buff);
630     osi_free(p_buff);
631     return result;
632 #else   /* SDP_SERVER_ENABLED == FALSE */
633     return (FALSE);
634 #endif
635 }
636 
637 /*******************************************************************************
638 **
639 ** Function         SDP_AddProtocolList
640 **
641 ** Description      This function is called to add a protocol descriptor list to
642 **                  a record. This would be through the SDP database maintenance API.
643 **                  If the protocol list already exists in the record, it is replaced
644 **                  with the new list.
645 **
646 ** Returns          TRUE if added OK, else FALSE
647 **
648 *******************************************************************************/
SDP_AddProtocolList(UINT32 handle,UINT16 num_elem,tSDP_PROTOCOL_ELEM * p_elem_list)649 BOOLEAN SDP_AddProtocolList (UINT32 handle, UINT16 num_elem,
650                              tSDP_PROTOCOL_ELEM *p_elem_list)
651 {
652 #if SDP_SERVER_ENABLED == TRUE
653     UINT8           *p_buff;
654     int             offset;
655     BOOLEAN         result;
656 
657     if ((p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
658         SDP_TRACE_ERROR("SDP_AddProtocolList cannot get a buffer!\n");
659         return (FALSE);
660     }
661 
662     offset = sdp_compose_proto_list(p_buff, num_elem, p_elem_list);
663     result = SDP_AddAttribute (handle, ATTR_ID_PROTOCOL_DESC_LIST, DATA_ELE_SEQ_DESC_TYPE, (UINT32) offset, p_buff);
664     osi_free(p_buff);
665     return result;
666 #else   /* SDP_SERVER_ENABLED == FALSE */
667     return (FALSE);
668 #endif
669 }
670 
671 
672 /*******************************************************************************
673 **
674 ** Function         SDP_AddAdditionProtoLists
675 **
676 ** Description      This function is called to add a protocol descriptor list to
677 **                  a record. This would be through the SDP database maintenance API.
678 **                  If the protocol list already exists in the record, it is replaced
679 **                  with the new list.
680 **
681 ** Returns          TRUE if added OK, else FALSE
682 **
683 *******************************************************************************/
SDP_AddAdditionProtoLists(UINT32 handle,UINT16 num_elem,tSDP_PROTO_LIST_ELEM * p_proto_list)684 BOOLEAN SDP_AddAdditionProtoLists (UINT32 handle, UINT16 num_elem,
685                                    tSDP_PROTO_LIST_ELEM *p_proto_list)
686 {
687 #if SDP_SERVER_ENABLED == TRUE
688     UINT16          xx;
689     UINT8           *p_buff;
690     UINT8           *p;
691     UINT8           *p_len;
692     int             offset;
693     BOOLEAN         result;
694 
695     if ((p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
696         SDP_TRACE_ERROR("SDP_AddAdditionProtoLists cannot get a buffer!\n");
697         return (FALSE);
698     }
699     p = p_buff;
700 
701     /* for each ProtocolDescriptorList */
702     for (xx = 0; xx < num_elem; xx++, p_proto_list++) {
703         UINT8_TO_BE_STREAM  (p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
704         p_len = p++;
705 
706         offset = sdp_compose_proto_list(p, p_proto_list->num_elems,
707                                         p_proto_list->list_elem);
708         p += offset;
709 
710         *p_len  = (UINT8)(p - p_len - 1);
711     }
712     result = SDP_AddAttribute (handle, ATTR_ID_ADDITION_PROTO_DESC_LISTS, DATA_ELE_SEQ_DESC_TYPE,
713                                (UINT32) (p - p_buff), p_buff);
714     osi_free(p_buff);
715     return result;
716 
717 #else   /* SDP_SERVER_ENABLED == FALSE */
718     return (FALSE);
719 #endif
720 }
721 
722 /*******************************************************************************
723 **
724 ** Function         SDP_AddProfileDescriptorList
725 **
726 ** Description      This function is called to add a profile descriptor list to
727 **                  a record. This would be through the SDP database maintenance API.
728 **                  If the version already exists in the record, it is replaced
729 **                  with the new one.
730 **
731 ** Returns          TRUE if added OK, else FALSE
732 **
733 *******************************************************************************/
SDP_AddProfileDescriptorList(UINT32 handle,UINT16 profile_uuid,UINT16 version)734 BOOLEAN SDP_AddProfileDescriptorList (UINT32 handle, UINT16 profile_uuid,
735                                       UINT16 version)
736 {
737 #if SDP_SERVER_ENABLED == TRUE
738     UINT8           *p_buff;
739     UINT8           *p;
740     BOOLEAN        result;
741 
742     if ((p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN)) == NULL) {
743         SDP_TRACE_ERROR("SDP_AddProfileDescriptorList cannot get a buffer!\n");
744         return (FALSE);
745     }
746     p = p_buff + 2;
747 
748     /* First, build the profile descriptor list. This consists of a data element sequence. */
749     /* The sequence consists of profile's UUID and version number  */
750     UINT8_TO_BE_STREAM  (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
751     UINT16_TO_BE_STREAM (p, profile_uuid);
752 
753     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
754     UINT16_TO_BE_STREAM (p, version);
755 
756     /* Add in type and length fields */
757     *p_buff = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
758     *(p_buff + 1) = (UINT8) (p - (p_buff + 2));
759 
760     result = SDP_AddAttribute (handle, ATTR_ID_BT_PROFILE_DESC_LIST, DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p - p_buff), p_buff);
761     osi_free(p_buff);
762     return result;
763 
764 #else   /* SDP_SERVER_ENABLED == FALSE */
765     return (FALSE);
766 #endif
767 }
768 
769 
770 /*******************************************************************************
771 **
772 ** Function         SDP_AddLanguageBaseAttrIDList
773 **
774 ** Description      This function is called to add a language base attr list to
775 **                  a record. This would be through the SDP database maintenance API.
776 **                  If the version already exists in the record, it is replaced
777 **                  with the new one.
778 **
779 ** Returns          TRUE if added OK, else FALSE
780 **
781 *******************************************************************************/
SDP_AddLanguageBaseAttrIDList(UINT32 handle,UINT16 lang,UINT16 char_enc,UINT16 base_id)782 BOOLEAN SDP_AddLanguageBaseAttrIDList (UINT32 handle, UINT16 lang,
783                                        UINT16 char_enc, UINT16 base_id)
784 {
785 #if SDP_SERVER_ENABLED == TRUE
786     UINT8           *p_buff;
787     UINT8           *p;
788     BOOLEAN         result;
789 
790     if ((p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN)) == NULL) {
791         SDP_TRACE_ERROR("SDP_AddLanguageBaseAttrIDList cannot get a buffer!\n");
792         return (FALSE);
793     }
794     p = p_buff;
795 
796     /* First, build the language base descriptor list. This consists of a data */
797     /* element sequence. The sequence consists of 9 bytes (3 UINt16 fields)    */
798     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
799     UINT16_TO_BE_STREAM (p, lang);
800 
801     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
802     UINT16_TO_BE_STREAM (p, char_enc);
803 
804     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
805     UINT16_TO_BE_STREAM (p, base_id);
806 
807     result = SDP_AddAttribute (handle, ATTR_ID_LANGUAGE_BASE_ATTR_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
808                                (UINT32) (p - p_buff), p_buff);
809     osi_free(p_buff);
810     return result;
811 #else   /* SDP_SERVER_ENABLED == FALSE */
812     return (FALSE);
813 #endif
814 }
815 
816 
817 /*******************************************************************************
818 **
819 ** Function         SDP_AddServiceClassIdList
820 **
821 ** Description      This function is called to add a service list to a record.
822 **                  This would be through the SDP database maintenance API.
823 **                  If the service list already exists in the record, it is replaced
824 **                  with the new list.
825 **
826 ** Returns          TRUE if added OK, else FALSE
827 **
828 *******************************************************************************/
SDP_AddServiceClassIdList(UINT32 handle,UINT16 num_services,UINT16 * p_service_uuids)829 BOOLEAN SDP_AddServiceClassIdList (UINT32 handle, UINT16 num_services,
830                                    UINT16 *p_service_uuids)
831 {
832 #if SDP_SERVER_ENABLED == TRUE
833     UINT16          xx;
834     UINT8           *p_buff;
835     UINT8           *p;
836     BOOLEAN         result;
837 
838     if ((p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
839         SDP_TRACE_ERROR("SDP_AddServiceClassIdList cannot get a buffer!\n");
840         return (FALSE);
841     }
842     p = p_buff;
843 
844     for (xx = 0; xx < num_services; xx++, p_service_uuids++) {
845         UINT8_TO_BE_STREAM  (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
846         UINT16_TO_BE_STREAM (p, *p_service_uuids);
847     }
848 
849     result = SDP_AddAttribute (handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
850                                (UINT32) (p - p_buff), p_buff);
851     osi_free(p_buff);
852     return result;
853 #else   /* SDP_SERVER_ENABLED == FALSE */
854     return (FALSE);
855 #endif
856 }
857 
858 
859 /*******************************************************************************
860 **
861 ** Function         SDP_DeleteAttribute
862 **
863 ** Description      This function is called to delete an attribute from a record.
864 **                  This would be through the SDP database maintenance API.
865 **
866 ** Returns          TRUE if deleted OK, else FALSE if not found
867 **
868 *******************************************************************************/
SDP_DeleteAttribute(UINT32 handle,UINT16 attr_id)869 BOOLEAN SDP_DeleteAttribute (UINT32 handle, UINT16 attr_id)
870 {
871 #if SDP_SERVER_ENABLED == TRUE
872     UINT16          xx, yy;
873     tSDP_RECORD     *p_rec = NULL;
874     list_node_t     *p_node= NULL;
875     UINT8           *pad_ptr;
876     UINT32  len;                        /* Number of bytes in the entry */
877 
878     /* Find the record in the database */
879     for(p_node = list_begin(sdp_cb.server_db.p_record_list); p_node; p_node = list_next(p_node)) {
880 	p_rec= list_node(p_node);
881         if (p_rec->record_handle == handle) {
882             tSDP_ATTRIBUTE  *p_attr = &p_rec->attribute[0];
883 
884             SDP_TRACE_API("Deleting attr_id 0x%04x for handle 0x%x\n", attr_id, handle);
885             /* Found it. Now, find the attribute */
886             for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
887                 if (p_attr->id == attr_id) {
888                     pad_ptr = p_attr->value_ptr;
889                     len = p_attr->len;
890 
891                     if (len) {
892                         for (yy = 0; yy < p_rec->num_attributes; yy++) {
893                             if ( p_rec->attribute[yy].value_ptr > pad_ptr ) {
894                                 p_rec->attribute[yy].value_ptr -= len;
895                             }
896                         }
897                     }
898 
899                     /* Found it. Shift everything up one */
900                     p_rec->num_attributes--;
901 
902                     for (yy = xx; yy < p_rec->num_attributes; yy++, p_attr++) {
903                         *p_attr = *(p_attr + 1);
904                     }
905 
906                     /* adjust attribute values if needed */
907                     if (len) {
908                         xx = (p_rec->free_pad_ptr - ((pad_ptr + len) -
909                                                      &p_rec->attr_pad[0]));
910                         for ( yy = 0; yy < xx; yy++, pad_ptr++) {
911                             *pad_ptr = *(pad_ptr + len);
912                         }
913                         p_rec->free_pad_ptr -= len;
914                     }
915                     return (TRUE);
916                 }
917             }
918         }
919     }
920 #endif
921     /* If here, not found */
922     return (FALSE);
923 }
924 
925 /*******************************************************************************
926 **
927 ** Function         SDP_ReadRecord
928 **
929 ** Description      This function is called to get the raw data of the record
930 **                  with the given handle from the database.
931 **
932 ** Returns          -1, if the record is not found.
933 **                  Otherwise, the offset (0 or 1) to start of data in p_data.
934 **
935 **                  The size of data copied into p_data is in *p_data_len.
936 **
937 *******************************************************************************/
938 #if (SDP_RAW_DATA_INCLUDED == TRUE)
SDP_ReadRecord(UINT32 handle,UINT8 * p_data,INT32 * p_data_len)939 INT32 SDP_ReadRecord(UINT32 handle, UINT8 *p_data, INT32 *p_data_len)
940 {
941     INT32           offset = -1; /* default to not found */
942 #if SDP_SERVER_ENABLED == TRUE
943     INT32           len = 0;                        /* Number of bytes in the entry */
944     tSDP_RECORD     *p_rec;
945     UINT16          start = 0;
946     UINT16          end = 0xffff;
947     tSDP_ATTRIBUTE  *p_attr;
948     UINT16          rem_len;
949     UINT8           *p_rsp;
950 
951     /* Find the record in the database */
952     p_rec = sdp_db_find_record(handle);
953     if (p_rec && p_data && p_data_len) {
954         p_rsp = &p_data[3];
955         while ( (p_attr = sdp_db_find_attr_in_rec (p_rec, start, end)) != NULL) {
956             /* Check if attribute fits. Assume 3-byte value type/length */
957             rem_len = *p_data_len - (UINT16) (p_rsp - p_data);
958 
959             if (p_attr->len > (UINT32)(rem_len - 6)) {
960                 break;
961             }
962 
963             p_rsp = sdpu_build_attrib_entry (p_rsp, p_attr);
964 
965             /* next attr id */
966             start = p_attr->id + 1;
967         }
968         len = (INT32) (p_rsp - p_data);
969 
970         /* Put in the sequence header (2 or 3 bytes) */
971         if (len > 255) {
972             offset = 0;
973             p_data[0] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
974             p_data[1] = (UINT8) ((len - 3) >> 8);
975             p_data[2] = (UINT8) (len - 3);
976         } else {
977             offset = 1;
978 
979             p_data[1] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
980             p_data[2] = (UINT8) (len - 3);
981 
982             len--;
983         }
984         *p_data_len = len;
985     }
986 #endif
987     /* If here, not found */
988     return (offset);
989 }
990 #endif
991 
992 #endif  ///SDP_INCLUDED == TRUE
993