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