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