1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "sdp_util.c" 39 40 /* 41 * sdp_util.c 42 */ 43 44 #include "bluetooth.h" 45 #include "btstack_config.h" 46 #include "btstack_util.h" 47 #include "classic/core.h" 48 #include "classic/sdp_util.h" 49 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <stdint.h> 54 #include <inttypes.h> // PRIx32 55 56 #ifdef ENABLE_SDP_DES_DUMP 57 // workaround for missing PRIx32 on mspgcc (16-bit MCU) 58 #ifndef PRIx32 59 #warning Using own: #define PRIx32 "lx" 60 #define PRIx32 "lx" 61 #endif 62 // date element type names 63 const char * const type_names[] = { "NIL", "UINT", "INT", "UUID", "STRING", "BOOL", "DES", "DEA", "URL"}; 64 #endif 65 66 static uint8_t des_serviceSearchPatternUUID16[] = {0x35, 0x03, 0x19, 0x00, 0x00}; 67 static uint8_t des_serviceSearchPatternUUID128[] = { 68 0x35, 0x11, 0x1c, 69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 71 72 // MARK: DataElement getter 73 de_size_t de_get_size_type(const uint8_t *header){ 74 return (de_size_t) (header[0] & 7); 75 } 76 77 de_type_t de_get_element_type(const uint8_t *header){ 78 return (de_type_t) (header[0] >> 3); 79 } 80 81 uint32_t de_get_header_size(const uint8_t * header){ 82 de_size_t de_size = de_get_size_type(header); 83 if (de_size <= DE_SIZE_128) { 84 return 1; 85 } 86 return 1 + (1 << (de_size-DE_SIZE_VAR_8)); 87 } 88 89 uint32_t de_get_data_size(const uint8_t * header){ 90 uint32_t result = 0; 91 de_type_t de_type = de_get_element_type(header); 92 de_size_t de_size = de_get_size_type(header); 93 switch (de_size){ 94 case DE_SIZE_VAR_8: 95 result = header[1]; 96 break; 97 case DE_SIZE_VAR_16: 98 result = big_endian_read_16(header,1); 99 break; 100 case DE_SIZE_VAR_32: 101 result = big_endian_read_32(header,1); 102 break; 103 default: 104 // case DE_SIZE_8: 105 // case DE_SIZE_16: 106 // case DE_SIZE_32: 107 // case DE_SIZE_64: 108 // case DE_SIZE_128: 109 if (de_type == DE_NIL) return 0; 110 return 1 << de_size; 111 } 112 return result; 113 } 114 115 int de_get_len(const uint8_t *header){ 116 return de_get_header_size(header) + de_get_data_size(header); 117 } 118 119 // returns data element length if data element fits in size 120 uint32_t de_get_len_safe(const uint8_t * header, uint32_t size){ 121 if (1 > size) return 0; 122 uint32_t header_size = de_get_header_size(header); 123 if (header_size > size) return 0; 124 uint32_t data_size = de_get_data_size(header); 125 if (data_size > size) return 0; 126 uint32_t de_len = header_size + data_size; 127 if (de_len > size) return 0; 128 return de_len; 129 } 130 131 // @returns OK, if UINT16 value was read 132 int de_element_get_uint16(const uint8_t * element, uint16_t * value){ 133 if (de_get_size_type(element) != DE_SIZE_16) return 0; 134 *value = big_endian_read_16(element, de_get_header_size(element)); 135 return 1; 136 } 137 138 // @returns: element is valid UUID 139 int de_get_normalized_uuid(uint8_t *uuid128, const uint8_t *element){ 140 de_type_t uuidType = de_get_element_type(element); 141 de_size_t uuidSize = de_get_size_type(element); 142 if (uuidType != DE_UUID) return 0; 143 uint32_t shortUUID; 144 switch (uuidSize){ 145 case DE_SIZE_16: 146 shortUUID = big_endian_read_16(element, 1); 147 break; 148 case DE_SIZE_32: 149 shortUUID = big_endian_read_32(element, 1); 150 break; 151 case DE_SIZE_128: 152 memcpy(uuid128, element+1, 16); 153 return 1; 154 default: 155 return 0; 156 } 157 uuid_add_bluetooth_prefix(uuid128, shortUUID); 158 return 1; 159 } 160 161 // @returns 0 if no UUID16 or UUID32 is present, and UUID32 otherwise 162 uint32_t de_get_uuid32(const uint8_t * element){ 163 uint8_t uuid128[16]; 164 int validUuid128 = de_get_normalized_uuid(uuid128, element); 165 if (!validUuid128) return 0; 166 int hasBlueoothBaseUuid = uuid_has_bluetooth_prefix(uuid128); 167 if (!hasBlueoothBaseUuid) return 0; 168 return big_endian_read_32(uuid128, 0); 169 } 170 171 const uint8_t * de_get_string(const uint8_t * element){ 172 if (de_get_element_type(element) != DE_STRING) return NULL; 173 return &element[de_get_header_size(element)]; 174 } 175 176 // functions to create record 177 static void de_store_descriptor(uint8_t * header, de_type_t type, de_size_t size){ 178 header[0] = (type << 3) | size; 179 } 180 181 void de_store_descriptor_with_len(uint8_t * header, de_type_t type, de_size_t size, uint32_t len){ 182 header[0] = (type << 3) | size; 183 switch (size){ 184 case DE_SIZE_VAR_8: 185 header[1] = len; 186 break; 187 case DE_SIZE_VAR_16: 188 big_endian_store_16(header, 1, len); 189 break; 190 case DE_SIZE_VAR_32: 191 big_endian_store_32(header, 1, len); 192 break; 193 default: 194 break; 195 } 196 } 197 198 // MARK: DataElement creation 199 200 /* starts a new sequence in empty buffer - first call */ 201 void de_create_sequence(uint8_t *header){ 202 de_store_descriptor_with_len( header, DE_DES, DE_SIZE_VAR_16, 0); // DES, 2 Byte Length 203 }; 204 205 /* starts a sub-sequence, @returns handle for sub-sequence */ 206 uint8_t * de_push_sequence(uint8_t *header){ 207 int element_len = de_get_len(header); 208 de_store_descriptor_with_len(header+element_len, DE_DES, DE_SIZE_VAR_16, 0); // DES, 2 Byte Length 209 return header + element_len; 210 } 211 212 /* closes the current sequence and updates the parent sequence */ 213 void de_pop_sequence(uint8_t * parent, uint8_t * child){ 214 int child_len = de_get_len(child); 215 int data_size_parent = big_endian_read_16(parent,1); 216 big_endian_store_16(parent, 1, data_size_parent + child_len); 217 } 218 219 /* adds a single number value and 16+32 bit UUID to the sequence */ 220 void de_add_number(uint8_t *seq, de_type_t type, de_size_t size, uint32_t value){ 221 int data_size = big_endian_read_16(seq,1); 222 int element_size = 1; // e.g. for DE_TYPE_NIL 223 de_store_descriptor(seq+3+data_size, type, size); 224 switch (size){ 225 case DE_SIZE_8: 226 if (type != DE_NIL){ 227 seq[4+data_size] = value; 228 element_size = 2; 229 } 230 break; 231 case DE_SIZE_16: 232 big_endian_store_16(seq, 4+data_size, value); 233 element_size = 3; 234 break; 235 case DE_SIZE_32: 236 big_endian_store_32(seq, 4+data_size, value); 237 element_size = 5; 238 break; 239 default: 240 break; 241 } 242 big_endian_store_16(seq, 1, data_size+element_size); 243 } 244 245 /* add a single block of data, e.g. as DE_STRING, DE_URL */ 246 void de_add_data( uint8_t *seq, de_type_t type, uint16_t size, uint8_t *data){ 247 int data_size = big_endian_read_16(seq,1); 248 if (size > 0xff) { 249 // use 16-bit lengh information (3 byte header) 250 de_store_descriptor_with_len(seq+3+data_size, type, DE_SIZE_VAR_16, size); 251 data_size += 3; 252 } else { 253 // use 8-bit lengh information (2 byte header) 254 de_store_descriptor_with_len(seq+3+data_size, type, DE_SIZE_VAR_8, size); 255 data_size += 2; 256 } 257 memcpy( seq + 3 + data_size, data, size); 258 data_size += size; 259 big_endian_store_16(seq, 1, data_size); 260 } 261 262 void de_add_uuid128(uint8_t * seq, uint8_t * uuid){ 263 int data_size = big_endian_read_16(seq,1); 264 de_store_descriptor(seq+3+data_size, DE_UUID, DE_SIZE_128); 265 memcpy( seq + 4 + data_size, uuid, 16); 266 big_endian_store_16(seq, 1, data_size+1+16); 267 } 268 269 // MARK: DES iterator 270 int des_iterator_init(des_iterator_t * it, uint8_t * element){ 271 de_type_t type = de_get_element_type(element); 272 if (type != DE_DES) return 0; 273 274 it->element = element; 275 it->pos = de_get_header_size(element); 276 it->length = de_get_len(element); 277 // printf("des_iterator_init current pos %d, total len %d\n", it->pos, it->length); 278 return 1; 279 } 280 281 de_type_t des_iterator_get_type (des_iterator_t * it){ 282 return de_get_element_type(&it->element[it->pos]); 283 } 284 285 uint16_t des_iterator_get_size (des_iterator_t * it){ 286 int length = de_get_len(&it->element[it->pos]); 287 int header_size = de_get_header_size(&it->element[it->pos]); 288 return length - header_size; 289 } 290 291 int des_iterator_has_more(des_iterator_t * it){ 292 return it->pos < it->length; 293 } 294 295 uint8_t * des_iterator_get_element(des_iterator_t * it){ 296 if (!des_iterator_has_more(it)) return NULL; 297 return &it->element[it->pos]; 298 } 299 300 void des_iterator_next(des_iterator_t * it){ 301 int element_len = de_get_len(&it->element[it->pos]); 302 // printf("des_iterator_next element size %d, current pos %d, total len %d\n", element_len, it->pos, it->length); 303 it->pos += element_len; 304 } 305 306 // MARK: DataElementSequence traversal 307 typedef int (*de_traversal_callback_t)(uint8_t * element, de_type_t type, de_size_t size, void *context); 308 static void de_traverse_sequence(uint8_t * element, de_traversal_callback_t handler, void *context){ 309 de_type_t type = de_get_element_type(element); 310 if (type != DE_DES) return; 311 int pos = de_get_header_size(element); 312 int end_pos = de_get_len(element); 313 while (pos < end_pos){ 314 de_type_t elemType = de_get_element_type(element + pos); 315 de_size_t elemSize = de_get_size_type(element + pos); 316 uint8_t done = (*handler)(element + pos, elemType, elemSize, context); 317 if (done) break; 318 pos += de_get_len(element + pos); 319 } 320 } 321 322 // MARK: AttributeList traversal 323 typedef int (*sdp_attribute_list_traversal_callback_t)(uint16_t attributeID, uint8_t * attributeValue, de_type_t type, de_size_t size, void *context); 324 static void sdp_attribute_list_traverse_sequence(uint8_t * element, sdp_attribute_list_traversal_callback_t handler, void *context){ 325 de_type_t type = de_get_element_type(element); 326 if (type != DE_DES) return; 327 int pos = de_get_header_size(element); 328 int end_pos = de_get_len(element); 329 while (pos < end_pos){ 330 de_type_t idType = de_get_element_type(element + pos); 331 de_size_t idSize = de_get_size_type(element + pos); 332 if ( (idType != DE_UINT) || (idSize != DE_SIZE_16) ) break; // wrong type 333 uint16_t attribute_id = big_endian_read_16(element, pos + 1); 334 pos += 3; 335 if (pos >= end_pos) break; // array out of bounds 336 de_type_t valueType = de_get_element_type(element + pos); 337 de_size_t valueSize = de_get_size_type(element + pos); 338 uint8_t done = (*handler)(attribute_id, element + pos, valueType, valueSize, context); 339 if (done) break; 340 pos += de_get_len(element + pos); 341 } 342 } 343 344 // MARK: AttributeID in AttributeIDList 345 // attribute ID in AttributeIDList 346 // context { result, attributeID } 347 struct sdp_context_attributeID_search { 348 int result; 349 uint16_t attributeID; 350 }; 351 static int sdp_traversal_attributeID_search(uint8_t * element, de_type_t type, de_size_t size, void *my_context){ 352 struct sdp_context_attributeID_search * context = (struct sdp_context_attributeID_search *) my_context; 353 if (type != DE_UINT) return 0; 354 switch (size) { 355 case DE_SIZE_16: 356 if (big_endian_read_16(element, 1) == context->attributeID) { 357 context->result = 1; 358 return 1; 359 } 360 break; 361 case DE_SIZE_32: 362 if ((big_endian_read_16(element, 1) <= context->attributeID) 363 && (context->attributeID <= big_endian_read_16(element, 3))) { 364 context->result = 1; 365 return 1; 366 } 367 break; 368 default: 369 break; 370 } 371 return 0; 372 } 373 374 int sdp_attribute_list_constains_id(uint8_t *attributeIDList, uint16_t attributeID){ 375 struct sdp_context_attributeID_search attributeID_search; 376 attributeID_search.result = 0; 377 attributeID_search.attributeID = attributeID; 378 de_traverse_sequence(attributeIDList, sdp_traversal_attributeID_search, &attributeID_search); 379 return attributeID_search.result; 380 } 381 382 // MARK: Append Attributes for AttributeIDList 383 // pre: buffer contains DES with 2 byte length field 384 struct sdp_context_append_attributes { 385 uint8_t * buffer; 386 uint16_t startOffset; // offset of when to start copying 387 uint16_t maxBytes; 388 uint16_t usedBytes; 389 uint8_t *attributeIDList; 390 }; 391 392 static int sdp_traversal_append_attributes(uint16_t attributeID, uint8_t * attributeValue, de_type_t de_type, de_size_t de_size, void *my_context){ 393 UNUSED(de_type); 394 UNUSED(de_size); 395 struct sdp_context_append_attributes * context = (struct sdp_context_append_attributes *) my_context; 396 if (sdp_attribute_list_constains_id(context->attributeIDList, attributeID)) { 397 // DES_HEADER(3) + DES_DATA + (UINT16(3) + attribute) 398 uint16_t data_size = big_endian_read_16(context->buffer, 1); 399 int attribute_len = de_get_len(attributeValue); 400 if ((3 + data_size + (3 + attribute_len)) <= context->maxBytes) { 401 // copy Attribute 402 de_add_number(context->buffer, DE_UINT, DE_SIZE_16, attributeID); 403 data_size += 3; // 3 bytes 404 memcpy(context->buffer + 3 + data_size, attributeValue, attribute_len); 405 big_endian_store_16(context->buffer,1,data_size+attribute_len); 406 } else { 407 // not enought space left -> continue with previous element 408 return 1; 409 } 410 } 411 return 0; 412 } 413 414 // maxBytes: maximal size of data element sequence 415 uint16_t sdp_append_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint8_t *buffer){ 416 struct sdp_context_append_attributes context; 417 context.buffer = buffer; 418 context.maxBytes = maxBytes; 419 context.usedBytes = 0; 420 context.startOffset = startOffset; 421 context.attributeIDList = attributeIDList; 422 sdp_attribute_list_traverse_sequence(record, sdp_traversal_append_attributes, &context); 423 return context.usedBytes; 424 } 425 426 // MARK: Filter attributes that match attribute list from startOffset and a max nr bytes 427 struct sdp_context_filter_attributes { 428 uint8_t * buffer; 429 uint16_t startOffset; // offset of when to start copying 430 uint16_t maxBytes; 431 uint16_t usedBytes; 432 uint8_t *attributeIDList; 433 int complete; 434 }; 435 436 // copy data with given start offset and max bytes, returns OK if all data has been copied 437 static int spd_append_range(struct sdp_context_filter_attributes* context, uint16_t len, uint8_t *data){ 438 int ok = 1; 439 uint16_t remainder_len = len - context->startOffset; 440 if (context->maxBytes < remainder_len){ 441 remainder_len = context->maxBytes; 442 ok = 0; 443 } 444 memcpy(context->buffer, &data[context->startOffset], remainder_len); 445 context->usedBytes += remainder_len; 446 context->buffer += remainder_len; 447 context->maxBytes -= remainder_len; 448 context->startOffset = 0; 449 return ok; 450 } 451 452 static int sdp_traversal_filter_attributes(uint16_t attributeID, uint8_t * attributeValue, de_type_t de_type, de_size_t de_size, void *my_context){ 453 UNUSED(de_type); 454 UNUSED(de_size); 455 456 struct sdp_context_filter_attributes * context = (struct sdp_context_filter_attributes *) my_context; 457 458 if (!sdp_attribute_list_constains_id(context->attributeIDList, attributeID)) return 0; 459 460 // { Attribute ID (Descriptor, big endian 16-bit ID), AttributeValue (data)} 461 462 // handle Attribute ID 463 if (context->startOffset >= 3){ 464 context->startOffset -= 3; 465 } else { 466 uint8_t idBuffer[3]; 467 de_store_descriptor(idBuffer, DE_UINT, DE_SIZE_16); 468 big_endian_store_16(idBuffer,1,attributeID); 469 470 int ok = spd_append_range(context, 3, idBuffer); 471 if (!ok) { 472 context->complete = 0; 473 return 1; 474 } 475 } 476 477 // handle Attribute Value 478 int attribute_len = de_get_len(attributeValue); 479 if (context->startOffset >= attribute_len) { 480 context->startOffset -= attribute_len; 481 return 0; 482 } 483 484 int ok = spd_append_range(context, attribute_len, attributeValue); 485 if (!ok) { 486 context->complete = 0; 487 return 1; 488 } 489 return 0; 490 } 491 492 int sdp_filter_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint16_t *usedBytes, uint8_t *buffer){ 493 494 struct sdp_context_filter_attributes context; 495 context.buffer = buffer; 496 context.maxBytes = maxBytes; 497 context.usedBytes = 0; 498 context.startOffset = startOffset; 499 context.attributeIDList = attributeIDList; 500 context.complete = 1; 501 502 sdp_attribute_list_traverse_sequence(record, sdp_traversal_filter_attributes, &context); 503 504 *usedBytes = context.usedBytes; 505 return context.complete; 506 } 507 508 // MARK: Get sum of attributes matching attribute list 509 struct sdp_context_get_filtered_size { 510 uint8_t *attributeIDList; 511 uint16_t size; 512 }; 513 514 static int sdp_traversal_get_filtered_size(uint16_t attributeID, uint8_t * attributeValue, de_type_t de_type, de_size_t de_size, void *my_context){ 515 UNUSED(de_type); 516 UNUSED(de_size); 517 518 struct sdp_context_get_filtered_size * context = (struct sdp_context_get_filtered_size *) my_context; 519 if (sdp_attribute_list_constains_id(context->attributeIDList, attributeID)) { 520 context->size += 3 + de_get_len(attributeValue); 521 } 522 return 0; 523 } 524 525 int spd_get_filtered_size(uint8_t *record, uint8_t *attributeIDList){ 526 struct sdp_context_get_filtered_size context; 527 context.size = 0; 528 context.attributeIDList = attributeIDList; 529 sdp_attribute_list_traverse_sequence(record, sdp_traversal_get_filtered_size, &context); 530 return context.size; 531 } 532 533 // MARK: Get AttributeValue for AttributeID 534 // find attribute (ELEMENT) by ID 535 struct sdp_context_attribute_by_id { 536 uint16_t attributeID; 537 uint8_t * attributeValue; 538 }; 539 static int sdp_traversal_attribute_by_id(uint16_t attributeID, uint8_t * attributeValue, de_type_t de_type, de_size_t de_size, void *my_context){ 540 UNUSED(de_type); 541 UNUSED(de_size); 542 543 struct sdp_context_attribute_by_id * context = (struct sdp_context_attribute_by_id *) my_context; 544 if (attributeID == context->attributeID) { 545 context->attributeValue = attributeValue; 546 return 1; 547 } 548 return 0; 549 } 550 551 uint8_t * sdp_get_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID){ 552 struct sdp_context_attribute_by_id context; 553 context.attributeValue = NULL; 554 context.attributeID = attributeID; 555 sdp_attribute_list_traverse_sequence(record, sdp_traversal_attribute_by_id, &context); 556 return context.attributeValue; 557 } 558 559 // MARK: Set AttributeValue for AttributeID 560 struct sdp_context_set_attribute_for_id { 561 uint16_t attributeID; 562 uint32_t attributeValue; 563 uint8_t attributeFound; 564 }; 565 static int sdp_traversal_set_attribute_for_id(uint16_t attributeID, uint8_t * attributeValue, de_type_t attributeType, de_size_t size, void *my_context){ 566 struct sdp_context_set_attribute_for_id * context = (struct sdp_context_set_attribute_for_id *) my_context; 567 if (attributeID == context->attributeID) { 568 context->attributeFound = 1; 569 switch (size){ 570 case DE_SIZE_8: 571 if (attributeType != DE_NIL){ 572 attributeValue[1] = context->attributeValue; 573 } 574 break; 575 case DE_SIZE_16: 576 big_endian_store_16(attributeValue, 1, context->attributeValue); 577 break; 578 case DE_SIZE_32: 579 big_endian_store_32(attributeValue, 1, context->attributeValue); 580 break; 581 // Might want to support STRINGS to, copy upto original length 582 default: 583 break; 584 } 585 return 1; 586 } 587 return 0; 588 } 589 uint8_t sdp_set_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID, uint32_t value){ 590 struct sdp_context_set_attribute_for_id context; 591 context.attributeID = attributeID; 592 context.attributeValue = value; 593 context.attributeFound = 0; 594 sdp_attribute_list_traverse_sequence(record, sdp_traversal_set_attribute_for_id, &context); 595 return context.attributeFound; 596 } 597 598 // MARK: ServiceRecord contains UUID 599 // service record contains UUID 600 // context { normalizedUUID } 601 struct sdp_context_contains_uuid128 { 602 uint8_t * uuid128; 603 int result; 604 }; 605 int sdp_record_contains_UUID128(uint8_t *record, uint8_t *uuid128); 606 static int sdp_traversal_contains_UUID128(uint8_t * element, de_type_t type, de_size_t de_size, void *my_context){ 607 UNUSED(de_size); 608 609 struct sdp_context_contains_uuid128 * context = (struct sdp_context_contains_uuid128 *) my_context; 610 uint8_t normalizedUUID[16]; 611 if (type == DE_UUID){ 612 uint8_t uuidOK = de_get_normalized_uuid(normalizedUUID, element); 613 context->result = uuidOK && (memcmp(context->uuid128, normalizedUUID, 16) == 0); 614 } 615 if (type == DE_DES){ 616 context->result = sdp_record_contains_UUID128(element, context->uuid128); 617 } 618 return context->result; 619 } 620 int sdp_record_contains_UUID128(uint8_t *record, uint8_t *uuid128){ 621 struct sdp_context_contains_uuid128 context; 622 context.uuid128 = uuid128; 623 context.result = 0; 624 de_traverse_sequence(record, sdp_traversal_contains_UUID128, &context); 625 return context.result; 626 } 627 628 // MARK: ServiceRecord matches SearchServicePattern 629 // if UUID in searchServicePattern is not found in record => false 630 // context { result, record } 631 struct sdp_context_match_pattern { 632 uint8_t * record; 633 int result; 634 }; 635 636 int sdp_traversal_match_pattern(uint8_t * element, de_type_t de_type, de_size_t de_size, void *my_context){ 637 UNUSED(de_type); 638 UNUSED(de_size); 639 640 struct sdp_context_match_pattern * context = (struct sdp_context_match_pattern *) my_context; 641 uint8_t normalizedUUID[16]; 642 uint8_t uuidOK = de_get_normalized_uuid(normalizedUUID, element); 643 if (!uuidOK || !sdp_record_contains_UUID128(context->record, normalizedUUID)){ 644 context->result = 0; 645 return 1; 646 } 647 return 0; 648 } 649 int sdp_record_matches_service_search_pattern(uint8_t *record, uint8_t *serviceSearchPattern){ 650 struct sdp_context_match_pattern context; 651 context.record = record; 652 context.result = 1; 653 de_traverse_sequence(serviceSearchPattern, sdp_traversal_match_pattern, &context); 654 return context.result; 655 } 656 657 // MARK: Dump DataElement 658 // context { indent } 659 #ifdef ENABLE_SDP_DES_DUMP 660 static int de_traversal_dump_data(uint8_t * element, de_type_t de_type, de_size_t de_size, void *my_context){ 661 int indent = *(int*) my_context; 662 int i; 663 for (i=0; i<indent;i++) printf(" "); 664 unsigned int pos = de_get_header_size(element); 665 unsigned int end_pos = de_get_len(element); 666 printf("type %5s (%u), element len %2u ", type_names[de_type], de_type, end_pos); 667 if (de_type == DE_DES) { 668 printf("\n"); 669 indent++; 670 de_traverse_sequence(element, de_traversal_dump_data, (void *)&indent); 671 } else if (de_type == DE_UUID && de_size == DE_SIZE_128) { 672 printf(", value: %s\n", uuid128_to_str(element+1)); 673 } else if (de_type == DE_STRING) { 674 unsigned int len = 0; 675 switch (de_size){ 676 case DE_SIZE_VAR_8: 677 len = element[1]; 678 break; 679 case DE_SIZE_VAR_16: 680 len = big_endian_read_16(element, 1); 681 break; 682 default: 683 break; 684 } 685 printf("len %u (0x%02x)\n", len, len); 686 printf_hexdump(&element[pos], len); 687 } else { 688 uint32_t value = 0; 689 switch (de_size) { 690 case DE_SIZE_8: 691 if (de_type != DE_NIL){ 692 value = element[pos]; 693 } 694 break; 695 case DE_SIZE_16: 696 value = big_endian_read_16(element,pos); 697 break; 698 case DE_SIZE_32: 699 value = big_endian_read_32(element,pos); 700 break; 701 default: 702 break; 703 } 704 printf(", value: 0x%08" PRIx32 "\n", value); 705 } 706 return 0; 707 } 708 #endif 709 710 void de_dump_data_element(const uint8_t * record){ 711 #ifdef ENABLE_SDP_DES_DUMP 712 int indent = 0; 713 // hack to get root DES, too. 714 de_type_t type = de_get_element_type(record); 715 de_size_t size = de_get_size_type(record); 716 de_traversal_dump_data((uint8_t *) record, type, size, (void*) &indent); 717 #endif 718 } 719 720 uint8_t* sdp_service_search_pattern_for_uuid16(uint16_t uuid16){ 721 big_endian_store_16(des_serviceSearchPatternUUID16, 3, uuid16); 722 return (uint8_t*)des_serviceSearchPatternUUID16; 723 } 724 725 uint8_t* sdp_service_search_pattern_for_uuid128(const uint8_t * uuid128){ 726 memcpy(&des_serviceSearchPatternUUID128[3], uuid128, 16); 727 return (uint8_t*)des_serviceSearchPatternUUID128; 728 } 729 730