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