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