Home | History | Annotate | Download | only in sdp
      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 "bt_common.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 *******************************************************************************/
     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 *******************************************************************************/
    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 *******************************************************************************/
    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 *******************************************************************************/
    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 *******************************************************************************/
    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 *******************************************************************************/
    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 *******************************************************************************/
    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 *******************************************************************************/
    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 *******************************************************************************/
    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;
    533     UINT8 *p_head;
    534     BOOLEAN result;
    535     UINT8 *p_buff = (UINT8 *)osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2);
    536 
    537     p = p_buff;
    538 
    539     /* First, build the sequence */
    540     for (xx = 0; xx < num_elem; xx++)
    541     {
    542         p_head = p;
    543         switch (len[xx])
    544         {
    545         case 1:
    546             UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_ONE_BYTE);
    547             break;
    548         case 2:
    549             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_TWO_BYTES);
    550             break;
    551         case 4:
    552             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_FOUR_BYTES);
    553             break;
    554         case 8:
    555             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_EIGHT_BYTES);
    556             break;
    557         case 16:
    558             UINT8_TO_BE_STREAM  (p, (type[xx] << 3) | SIZE_SIXTEEN_BYTES);
    559             break;
    560         default:
    561             UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_IN_NEXT_BYTE);
    562             UINT8_TO_BE_STREAM (p, len[xx]);
    563             break;
    564         }
    565 
    566         ARRAY_TO_BE_STREAM (p, p_val[xx], len[xx]);
    567 
    568         if (p - p_buff > SDP_MAX_ATTR_LEN)
    569         {
    570             /* go back to before we add this element */
    571             p = p_head;
    572             if(p_head == p_buff)
    573             {
    574                 /* the first element exceed the max length */
    575                 SDP_TRACE_ERROR ("SDP_AddSequence - too long(attribute is not added)!!");
    576                 osi_free(p_buff);
    577                 return FALSE;
    578             }
    579             else
    580                 SDP_TRACE_ERROR ("SDP_AddSequence - too long, add %d elements of %d", xx, num_elem);
    581             break;
    582         }
    583     }
    584     result = SDP_AddAttribute (handle, attr_id, DATA_ELE_SEQ_DESC_TYPE,(UINT32) (p - p_buff), p_buff);
    585     osi_free(p_buff);
    586     return result;
    587 #else   /* SDP_SERVER_ENABLED == FALSE */
    588     return (FALSE);
    589 #endif
    590 }
    591 
    592 
    593 /*******************************************************************************
    594 **
    595 ** Function         SDP_AddUuidSequence
    596 **
    597 ** Description      This function is called to add a UUID sequence to a record.
    598 **                  This would be through the SDP database maintenance API.
    599 **                  If the sequence already exists in the record, it is replaced
    600 **                  with the new sequence.
    601 **
    602 ** Returns          TRUE if added OK, else FALSE
    603 **
    604 *******************************************************************************/
    605 BOOLEAN SDP_AddUuidSequence (UINT32 handle,  UINT16 attr_id, UINT16 num_uuids,
    606                              UINT16 *p_uuids)
    607 {
    608 #if SDP_SERVER_ENABLED == TRUE
    609     UINT16 xx;
    610     UINT8 *p;
    611     INT32 max_len = SDP_MAX_ATTR_LEN -3;
    612     BOOLEAN result;
    613     UINT8 *p_buff = (UINT8 *)osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2);
    614 
    615     p = p_buff;
    616 
    617     /* First, build the sequence */
    618     for (xx = 0; xx < num_uuids ; xx++, p_uuids++)
    619     {
    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         {
    625             SDP_TRACE_WARNING ("SDP_AddUuidSequence - too long, add %d uuids of %d", xx, num_uuids);
    626             break;
    627         }
    628     }
    629 
    630     result = SDP_AddAttribute (handle, attr_id, DATA_ELE_SEQ_DESC_TYPE,(UINT32) (p - p_buff), p_buff);
    631     osi_free(p_buff);
    632     return result;
    633 #else   /* SDP_SERVER_ENABLED == FALSE */
    634     return (FALSE);
    635 #endif
    636 }
    637 
    638 /*******************************************************************************
    639 **
    640 ** Function         SDP_AddProtocolList
    641 **
    642 ** Description      This function is called to add a protocol descriptor list to
    643 **                  a record. This would be through the SDP database maintenance API.
    644 **                  If the protocol list already exists in the record, it is replaced
    645 **                  with the new list.
    646 **
    647 ** Returns          TRUE if added OK, else FALSE
    648 **
    649 *******************************************************************************/
    650 BOOLEAN SDP_AddProtocolList (UINT32 handle, UINT16 num_elem,
    651                              tSDP_PROTOCOL_ELEM *p_elem_list)
    652 {
    653 #if SDP_SERVER_ENABLED == TRUE
    654     int offset;
    655     BOOLEAN result;
    656     UINT8 *p_buff = (UINT8 *)osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2);
    657 
    658     offset = sdp_compose_proto_list(p_buff, num_elem, p_elem_list);
    659     result = SDP_AddAttribute (handle, ATTR_ID_PROTOCOL_DESC_LIST,DATA_ELE_SEQ_DESC_TYPE, (UINT32) offset, p_buff);
    660     osi_free(p_buff);
    661     return result;
    662 #else   /* SDP_SERVER_ENABLED == FALSE */
    663     return (FALSE);
    664 #endif
    665 }
    666 
    667 
    668 /*******************************************************************************
    669 **
    670 ** Function         SDP_AddAdditionProtoLists
    671 **
    672 ** Description      This function is called to add a protocol descriptor list to
    673 **                  a record. This would be through the SDP database maintenance API.
    674 **                  If the protocol list already exists in the record, it is replaced
    675 **                  with the new list.
    676 **
    677 ** Returns          TRUE if added OK, else FALSE
    678 **
    679 *******************************************************************************/
    680 BOOLEAN SDP_AddAdditionProtoLists (UINT32 handle, UINT16 num_elem,
    681                                    tSDP_PROTO_LIST_ELEM *p_proto_list)
    682 {
    683 #if SDP_SERVER_ENABLED == TRUE
    684     UINT16 xx;
    685     UINT8 *p;
    686     UINT8 *p_len;
    687     int offset;
    688     BOOLEAN result;
    689     UINT8 *p_buff = (UINT8 *)osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2);
    690 
    691     p = p_buff;
    692 
    693     /* for each ProtocolDescriptorList */
    694     for (xx = 0; xx < num_elem; xx++, p_proto_list++)
    695     {
    696         UINT8_TO_BE_STREAM  (p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
    697         p_len = p++;
    698 
    699         offset = sdp_compose_proto_list(p, p_proto_list->num_elems,
    700                                         p_proto_list->list_elem);
    701         p += offset;
    702 
    703         *p_len  = (UINT8)(p - p_len - 1);
    704     }
    705     result = SDP_AddAttribute (handle, ATTR_ID_ADDITION_PROTO_DESC_LISTS,DATA_ELE_SEQ_DESC_TYPE,
    706 	                           (UINT32) (p - p_buff), p_buff);
    707     osi_free(p_buff);
    708     return result;
    709 
    710 #else   /* SDP_SERVER_ENABLED == FALSE */
    711     return (FALSE);
    712 #endif
    713 }
    714 
    715 /*******************************************************************************
    716 **
    717 ** Function         SDP_AddProfileDescriptorList
    718 **
    719 ** Description      This function is called to add a profile descriptor list to
    720 **                  a record. This would be through the SDP database maintenance API.
    721 **                  If the version already exists in the record, it is replaced
    722 **                  with the new one.
    723 **
    724 ** Returns          TRUE if added OK, else FALSE
    725 **
    726 *******************************************************************************/
    727 BOOLEAN SDP_AddProfileDescriptorList (UINT32 handle, UINT16 profile_uuid,
    728                                       UINT16 version)
    729 {
    730 #if SDP_SERVER_ENABLED == TRUE
    731     UINT8 *p;
    732     BOOLEAN result;
    733     UINT8 *p_buff = (UINT8 *)osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN);
    734 
    735     p = p_buff + 2;
    736 
    737     /* First, build the profile descriptor list. This consists of a data element sequence. */
    738     /* The sequence consists of profile's UUID and version number  */
    739     UINT8_TO_BE_STREAM  (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
    740     UINT16_TO_BE_STREAM (p, profile_uuid);
    741 
    742     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
    743     UINT16_TO_BE_STREAM (p, version);
    744 
    745     /* Add in type and length fields */
    746     *p_buff = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
    747     *(p_buff+1) = (UINT8) (p - (p_buff+2));
    748 
    749     result = SDP_AddAttribute (handle, ATTR_ID_BT_PROFILE_DESC_LIST,DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p - p_buff), p_buff);
    750     osi_free(p_buff);
    751     return result;
    752 
    753 #else   /* SDP_SERVER_ENABLED == FALSE */
    754     return (FALSE);
    755 #endif
    756 }
    757 
    758 
    759 /*******************************************************************************
    760 **
    761 ** Function         SDP_AddLanguageBaseAttrIDList
    762 **
    763 ** Description      This function is called to add a language base attr list to
    764 **                  a record. This would be through the SDP database maintenance API.
    765 **                  If the version already exists in the record, it is replaced
    766 **                  with the new one.
    767 **
    768 ** Returns          TRUE if added OK, else FALSE
    769 **
    770 *******************************************************************************/
    771 BOOLEAN SDP_AddLanguageBaseAttrIDList (UINT32 handle, UINT16 lang,
    772                                        UINT16 char_enc, UINT16 base_id)
    773 {
    774 #if SDP_SERVER_ENABLED == TRUE
    775     UINT8 *p;
    776     BOOLEAN result;
    777     UINT8 *p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN);
    778 
    779     p = p_buff;
    780 
    781     /* First, build the language base descriptor list. This consists of a data */
    782     /* element sequence. The sequence consists of 9 bytes (3 UINt16 fields)    */
    783     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
    784     UINT16_TO_BE_STREAM (p, lang);
    785 
    786     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
    787     UINT16_TO_BE_STREAM (p, char_enc);
    788 
    789     UINT8_TO_BE_STREAM  (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
    790     UINT16_TO_BE_STREAM (p, base_id);
    791 
    792     result = SDP_AddAttribute (handle, ATTR_ID_LANGUAGE_BASE_ATTR_ID_LIST,DATA_ELE_SEQ_DESC_TYPE,
    793 	                           (UINT32) (p - p_buff), p_buff);
    794     osi_free(p_buff);
    795     return result;
    796 #else   /* SDP_SERVER_ENABLED == FALSE */
    797     return (FALSE);
    798 #endif
    799 }
    800 
    801 
    802 /*******************************************************************************
    803 **
    804 ** Function         SDP_AddServiceClassIdList
    805 **
    806 ** Description      This function is called to add a service list to a record.
    807 **                  This would be through the SDP database maintenance API.
    808 **                  If the service list already exists in the record, it is replaced
    809 **                  with the new list.
    810 **
    811 ** Returns          TRUE if added OK, else FALSE
    812 **
    813 *******************************************************************************/
    814 BOOLEAN SDP_AddServiceClassIdList (UINT32 handle, UINT16 num_services,
    815                                    UINT16 *p_service_uuids)
    816 {
    817 #if SDP_SERVER_ENABLED == TRUE
    818     UINT16 xx;
    819     UINT8 *p;
    820     BOOLEAN result;
    821     UINT8 *p_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2);
    822 
    823     p = p_buff;
    824 
    825     for (xx = 0; xx < num_services; xx++, p_service_uuids++)
    826     {
    827         UINT8_TO_BE_STREAM  (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
    828         UINT16_TO_BE_STREAM (p, *p_service_uuids);
    829     }
    830 
    831     result = SDP_AddAttribute (handle, ATTR_ID_SERVICE_CLASS_ID_LIST,DATA_ELE_SEQ_DESC_TYPE,
    832 	                           (UINT32) (p - p_buff), p_buff);
    833     osi_free(p_buff);
    834     return result;
    835 #else   /* SDP_SERVER_ENABLED == FALSE */
    836     return (FALSE);
    837 #endif
    838 }
    839 
    840 
    841 /*******************************************************************************
    842 **
    843 ** Function         SDP_DeleteAttribute
    844 **
    845 ** Description      This function is called to delete an attribute from a record.
    846 **                  This would be through the SDP database maintenance API.
    847 **
    848 ** Returns          TRUE if deleted OK, else FALSE if not found
    849 **
    850 *******************************************************************************/
    851 BOOLEAN SDP_DeleteAttribute (UINT32 handle, UINT16 attr_id)
    852 {
    853 #if SDP_SERVER_ENABLED == TRUE
    854     UINT16          xx, yy;
    855     tSDP_RECORD     *p_rec = &sdp_cb.server_db.record[0];
    856     UINT8           *pad_ptr;
    857     UINT32  len;                        /* Number of bytes in the entry */
    858 
    859     /* Find the record in the database */
    860     for (xx = 0; xx < sdp_cb.server_db.num_records; xx++, p_rec++)
    861     {
    862         if (p_rec->record_handle == handle)
    863         {
    864             tSDP_ATTRIBUTE  *p_attr = &p_rec->attribute[0];
    865 
    866             SDP_TRACE_API("Deleting attr_id 0x%04x for handle 0x%x", attr_id, handle);
    867             /* Found it. Now, find the attribute */
    868             for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++)
    869             {
    870                 if (p_attr->id == attr_id)
    871                 {
    872                     pad_ptr = p_attr->value_ptr;
    873                     len = p_attr->len;
    874 
    875                     if (len)
    876                     {
    877                         for (yy = 0; yy < p_rec->num_attributes; yy++)
    878                         {
    879                             if( p_rec->attribute[yy].value_ptr > pad_ptr )
    880                                 p_rec->attribute[yy].value_ptr -= len;
    881                         }
    882                     }
    883 
    884                     /* Found it. Shift everything up one */
    885                     p_rec->num_attributes--;
    886 
    887                     for (yy = xx; yy < p_rec->num_attributes; yy++, p_attr++)
    888                     {
    889                         *p_attr = *(p_attr + 1);
    890                     }
    891 
    892                     /* adjust attribute values if needed */
    893                     if (len)
    894                     {
    895                         xx = (p_rec->free_pad_ptr - ((pad_ptr+len) -
    896                                                   &p_rec->attr_pad[0]));
    897                         for( yy=0; yy<xx; yy++, pad_ptr++)
    898                             *pad_ptr = *(pad_ptr+len);
    899                         p_rec->free_pad_ptr -= len;
    900                     }
    901                     return (TRUE);
    902                 }
    903             }
    904         }
    905     }
    906 #endif
    907     /* If here, not found */
    908     return (FALSE);
    909 }
    910 
    911 /*******************************************************************************
    912 **
    913 ** Function         SDP_ReadRecord
    914 **
    915 ** Description      This function is called to get the raw data of the record
    916 **                  with the given handle from the database.
    917 **
    918 ** Returns          -1, if the record is not found.
    919 **                  Otherwise, the offset (0 or 1) to start of data in p_data.
    920 **
    921 **                  The size of data copied into p_data is in *p_data_len.
    922 **
    923 *******************************************************************************/
    924 #if (SDP_RAW_DATA_INCLUDED == TRUE)
    925 INT32 SDP_ReadRecord(UINT32 handle, UINT8 *p_data, INT32 *p_data_len)
    926 {
    927     INT32           len = 0;                        /* Number of bytes in the entry */
    928     INT32           offset = -1; /* default to not found */
    929 #if SDP_SERVER_ENABLED == TRUE
    930     tSDP_RECORD     *p_rec;
    931     UINT16          start = 0;
    932     UINT16          end = 0xffff;
    933     tSDP_ATTRIBUTE  *p_attr;
    934     UINT16          rem_len;
    935     UINT8           *p_rsp;
    936 
    937     /* Find the record in the database */
    938     p_rec = sdp_db_find_record(handle);
    939     if(p_rec && p_data && p_data_len)
    940     {
    941         p_rsp = &p_data[3];
    942         while ( (p_attr = sdp_db_find_attr_in_rec (p_rec, start, end)) != NULL)
    943         {
    944             /* Check if attribute fits. Assume 3-byte value type/length */
    945             rem_len = *p_data_len - (UINT16) (p_rsp - p_data);
    946 
    947             if (p_attr->len > (UINT32)(rem_len - 6))
    948                 break;
    949 
    950             p_rsp = sdpu_build_attrib_entry (p_rsp, p_attr);
    951 
    952             /* next attr id */
    953             start = p_attr->id + 1;
    954         }
    955         len = (INT32) (p_rsp - p_data);
    956 
    957         /* Put in the sequence header (2 or 3 bytes) */
    958         if (len > 255)
    959         {
    960             offset = 0;
    961             p_data[0] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
    962             p_data[1] = (UINT8) ((len - 3) >> 8);
    963             p_data[2] = (UINT8) (len - 3);
    964         }
    965         else
    966         {
    967             offset = 1;
    968 
    969             p_data[1] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
    970             p_data[2] = (UINT8) (len - 3);
    971 
    972             len--;
    973         }
    974         *p_data_len = len;
    975     }
    976 #endif
    977     /* If here, not found */
    978     return (offset);
    979 }
    980 #endif
    981