xref: /btstack/src/classic/sdp_util.c (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
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 // functions to create record
172 static void de_store_descriptor(uint8_t * header, de_type_t type, de_size_t size){
173     header[0] = (type << 3) | size;
174 }
175 
176 void de_store_descriptor_with_len(uint8_t * header, de_type_t type, de_size_t size, uint32_t len){
177     header[0] = (type << 3) | size;
178     switch (size){
179         case DE_SIZE_VAR_8:
180             header[1] = len;
181             break;
182         case DE_SIZE_VAR_16:
183             big_endian_store_16(header, 1, len);
184             break;
185         case DE_SIZE_VAR_32:
186             big_endian_store_32(header, 1, len);
187             break;
188         default:
189             break;
190     }
191 }
192 
193 // MARK: DataElement creation
194 
195 /* starts a new sequence in empty buffer - first call */
196 void de_create_sequence(uint8_t *header){
197     de_store_descriptor_with_len( header, DE_DES, DE_SIZE_VAR_16, 0); // DES, 2 Byte Length
198 };
199 
200 /* starts a sub-sequence, @returns handle for sub-sequence */
201 uint8_t * de_push_sequence(uint8_t *header){
202     int element_len = de_get_len(header);
203     de_store_descriptor_with_len(header+element_len, DE_DES, DE_SIZE_VAR_16, 0); // DES, 2 Byte Length
204     return header + element_len;
205 }
206 
207 /* closes the current sequence and updates the parent sequence */
208 void de_pop_sequence(uint8_t * parent, uint8_t * child){
209     int child_len = de_get_len(child);
210     int data_size_parent = big_endian_read_16(parent,1);
211     big_endian_store_16(parent, 1, data_size_parent + child_len);
212 }
213 
214 /* adds a single number value and 16+32 bit UUID to the sequence */
215 void de_add_number(uint8_t *seq, de_type_t type, de_size_t size, uint32_t value){
216     int data_size   = big_endian_read_16(seq,1);
217     int element_size = 1;   // e.g. for DE_TYPE_NIL
218     de_store_descriptor(seq+3+data_size, type, size);
219     switch (size){
220         case DE_SIZE_8:
221             if (type != DE_NIL){
222                 seq[4+data_size] = value;
223                 element_size = 2;
224             }
225             break;
226         case DE_SIZE_16:
227             big_endian_store_16(seq, 4+data_size, value);
228             element_size = 3;
229             break;
230         case DE_SIZE_32:
231             big_endian_store_32(seq, 4+data_size, value);
232             element_size = 5;
233             break;
234         default:
235             break;
236     }
237     big_endian_store_16(seq, 1, data_size+element_size);
238 }
239 
240 /* add a single block of data, e.g. as DE_STRING, DE_URL */
241 void de_add_data( uint8_t *seq, de_type_t type, uint16_t size, uint8_t *data){
242     int data_size   = big_endian_read_16(seq,1);
243     if (size > 0xff) {
244         // use 16-bit lengh information (3 byte header)
245         de_store_descriptor_with_len(seq+3+data_size, type, DE_SIZE_VAR_16, size);
246         data_size += 3;
247     } else {
248         // use 8-bit lengh information (2 byte header)
249         de_store_descriptor_with_len(seq+3+data_size, type, DE_SIZE_VAR_8, size);
250         data_size += 2;
251     }
252     memcpy( seq + 3 + data_size, data, size);
253     data_size += size;
254     big_endian_store_16(seq, 1, data_size);
255 }
256 
257 void de_add_uuid128(uint8_t * seq, uint8_t * uuid){
258     int data_size   = big_endian_read_16(seq,1);
259     de_store_descriptor(seq+3+data_size, DE_UUID, DE_SIZE_128);
260     memcpy( seq + 4 + data_size, uuid, 16);
261     big_endian_store_16(seq, 1, data_size+1+16);
262 }
263 
264 // MARK: DES iterator
265 int des_iterator_init(des_iterator_t * it, uint8_t * element){
266     de_type_t type = de_get_element_type(element);
267     if (type != DE_DES) return 0;
268 
269     it->element = element;
270     it->pos = de_get_header_size(element);
271     it->length = de_get_len(element);
272     // printf("des_iterator_init current pos %d, total len %d\n", it->pos, it->length);
273     return 1;
274 }
275 
276 de_type_t des_iterator_get_type (des_iterator_t * it){
277     return de_get_element_type(&it->element[it->pos]);
278 }
279 
280 uint16_t des_iterator_get_size (des_iterator_t * it){
281     int length = de_get_len(&it->element[it->pos]);
282     int header_size = de_get_header_size(&it->element[it->pos]);
283     return length - header_size;
284 }
285 
286 int des_iterator_has_more(des_iterator_t * it){
287     return it->pos < it->length;
288 }
289 
290 uint8_t * des_iterator_get_element(des_iterator_t * it){
291     if (!des_iterator_has_more(it)) return NULL;
292     return &it->element[it->pos];
293 }
294 
295 void des_iterator_next(des_iterator_t * it){
296     int element_len = de_get_len(&it->element[it->pos]);
297     // printf("des_iterator_next element size %d, current pos %d, total len %d\n", element_len, it->pos, it->length);
298     it->pos += element_len;
299 }
300 
301 // MARK: DataElementSequence traversal
302 typedef int (*de_traversal_callback_t)(uint8_t * element, de_type_t type, de_size_t size, void *context);
303 static void de_traverse_sequence(uint8_t * element, de_traversal_callback_t handler, void *context){
304     de_type_t type = de_get_element_type(element);
305     if (type != DE_DES) return;
306     int pos = de_get_header_size(element);
307     int end_pos = de_get_len(element);
308     while (pos < end_pos){
309         de_type_t elemType = de_get_element_type(element + pos);
310         de_size_t elemSize = de_get_size_type(element + pos);
311         uint8_t done = (*handler)(element + pos, elemType, elemSize, context);
312         if (done) break;
313         pos += de_get_len(element + pos);
314     }
315 }
316 
317 // MARK: AttributeList traversal
318 typedef int (*sdp_attribute_list_traversal_callback_t)(uint16_t attributeID, uint8_t * attributeValue, de_type_t type, de_size_t size, void *context);
319 static void sdp_attribute_list_traverse_sequence(uint8_t * element, sdp_attribute_list_traversal_callback_t handler, void *context){
320     de_type_t type = de_get_element_type(element);
321     if (type != DE_DES) return;
322     int pos = de_get_header_size(element);
323     int end_pos = de_get_len(element);
324     while (pos < end_pos){
325         de_type_t idType = de_get_element_type(element + pos);
326         de_size_t idSize = de_get_size_type(element + pos);
327         if (idType != DE_UINT || idSize != DE_SIZE_16) break; // wrong type
328         uint16_t attribute_id = big_endian_read_16(element, pos + 1);
329         pos += 3;
330         if (pos >= end_pos) break; // array out of bounds
331         de_type_t valueType = de_get_element_type(element + pos);
332         de_size_t valueSize = de_get_size_type(element + pos);
333         uint8_t done = (*handler)(attribute_id, element + pos, valueType, valueSize, context);
334         if (done) break;
335         pos += de_get_len(element + pos);
336     }
337 }
338 
339 // MARK: AttributeID in AttributeIDList
340 // attribute ID in AttributeIDList
341 // context { result, attributeID }
342 struct sdp_context_attributeID_search {
343     int result;
344     uint16_t attributeID;
345 };
346 static int sdp_traversal_attributeID_search(uint8_t * element, de_type_t type, de_size_t size, void *my_context){
347     struct sdp_context_attributeID_search * context = (struct sdp_context_attributeID_search *) my_context;
348     if (type != DE_UINT) return 0;
349     switch (size) {
350         case DE_SIZE_16:
351             if (big_endian_read_16(element, 1) == context->attributeID) {
352                 context->result = 1;
353                 return 1;
354             }
355             break;
356         case DE_SIZE_32:
357             if (big_endian_read_16(element, 1) <= context->attributeID
358             &&  context->attributeID <= big_endian_read_16(element, 3)) {
359                 context->result = 1;
360                 return 1;
361             }
362             break;
363         default:
364             break;
365     }
366     return 0;
367 }
368 
369 int sdp_attribute_list_constains_id(uint8_t *attributeIDList, uint16_t attributeID){
370     struct sdp_context_attributeID_search attributeID_search;
371     attributeID_search.result = 0;
372     attributeID_search.attributeID = attributeID;
373     de_traverse_sequence(attributeIDList, sdp_traversal_attributeID_search, &attributeID_search);
374     return attributeID_search.result;
375 }
376 
377 // MARK: Append Attributes for AttributeIDList
378 // pre: buffer contains DES with 2 byte length field
379 struct sdp_context_append_attributes {
380     uint8_t * buffer;
381     uint16_t startOffset;     // offset of when to start copying
382     uint16_t maxBytes;
383     uint16_t usedBytes;
384     uint8_t *attributeIDList;
385 };
386 
387 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){
388     UNUSED(de_type);
389     UNUSED(de_size);
390     struct sdp_context_append_attributes * context = (struct sdp_context_append_attributes *) my_context;
391     if (sdp_attribute_list_constains_id(context->attributeIDList, attributeID)) {
392         // DES_HEADER(3) + DES_DATA + (UINT16(3) + attribute)
393         uint16_t data_size = big_endian_read_16(context->buffer, 1);
394         int attribute_len = de_get_len(attributeValue);
395         if (3 + data_size + (3 + attribute_len) <= context->maxBytes) {
396             // copy Attribute
397             de_add_number(context->buffer, DE_UINT, DE_SIZE_16, attributeID);
398             data_size += 3; // 3 bytes
399             memcpy(context->buffer + 3 + data_size, attributeValue, attribute_len);
400             big_endian_store_16(context->buffer,1,data_size+attribute_len);
401         } else {
402             // not enought space left -> continue with previous element
403             return 1;
404         }
405     }
406     return 0;
407 }
408 
409 // maxBytes: maximal size of data element sequence
410 uint16_t sdp_append_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint8_t *buffer){
411     struct sdp_context_append_attributes context;
412     context.buffer = buffer;
413     context.maxBytes = maxBytes;
414     context.usedBytes = 0;
415     context.startOffset = startOffset;
416     context.attributeIDList = attributeIDList;
417     sdp_attribute_list_traverse_sequence(record, sdp_traversal_append_attributes, &context);
418     return context.usedBytes;
419 }
420 
421 // MARK: Filter attributes that match attribute list from startOffset and a max nr bytes
422 struct sdp_context_filter_attributes {
423     uint8_t * buffer;
424     uint16_t startOffset;     // offset of when to start copying
425     uint16_t maxBytes;
426     uint16_t usedBytes;
427     uint8_t *attributeIDList;
428     int      complete;
429 };
430 
431 // copy data with given start offset and max bytes, returns OK if all data has been copied
432 static int spd_append_range(struct sdp_context_filter_attributes* context, uint16_t len, uint8_t *data){
433     int ok = 1;
434     uint16_t remainder_len = len - context->startOffset;
435     if (context->maxBytes < remainder_len){
436         remainder_len = context->maxBytes;
437         ok = 0;
438     }
439     memcpy(context->buffer, &data[context->startOffset], remainder_len);
440     context->usedBytes += remainder_len;
441     context->buffer    += remainder_len;
442     context->maxBytes  -= remainder_len;
443     context->startOffset = 0;
444     return ok;
445 }
446 
447 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){
448     UNUSED(de_type);
449     UNUSED(de_size);
450 
451     struct sdp_context_filter_attributes * context = (struct sdp_context_filter_attributes *) my_context;
452 
453     if (!sdp_attribute_list_constains_id(context->attributeIDList, attributeID)) return 0;
454 
455     // { Attribute ID (Descriptor, big endian 16-bit ID), AttributeValue (data)}
456 
457     // handle Attribute ID
458     if (context->startOffset >= 3){
459         context->startOffset -= 3;
460     } else {
461         uint8_t idBuffer[3];
462         de_store_descriptor(idBuffer, DE_UINT,  DE_SIZE_16);
463         big_endian_store_16(idBuffer,1,attributeID);
464 
465         int ok = spd_append_range(context, 3, idBuffer);
466         if (!ok) {
467             context->complete = 0;
468             return 1;
469         }
470     }
471 
472     // handle Attribute Value
473     int attribute_len = de_get_len(attributeValue);
474     if (context->startOffset >= attribute_len) {
475         context->startOffset -= attribute_len;
476         return 0;
477     }
478 
479     int ok = spd_append_range(context, attribute_len, attributeValue);
480     if (!ok) {
481         context->complete = 0;
482         return 1;
483     }
484     return 0;
485 }
486 
487 int sdp_filter_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint16_t *usedBytes, uint8_t *buffer){
488 
489     struct sdp_context_filter_attributes context;
490     context.buffer = buffer;
491     context.maxBytes = maxBytes;
492     context.usedBytes = 0;
493     context.startOffset = startOffset;
494     context.attributeIDList = attributeIDList;
495     context.complete = 1;
496 
497     sdp_attribute_list_traverse_sequence(record, sdp_traversal_filter_attributes, &context);
498 
499     *usedBytes = context.usedBytes;
500     return context.complete;
501 }
502 
503 // MARK: Get sum of attributes matching attribute list
504 struct sdp_context_get_filtered_size {
505     uint8_t *attributeIDList;
506     uint16_t size;
507 };
508 
509 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){
510     UNUSED(de_type);
511     UNUSED(de_size);
512 
513     struct sdp_context_get_filtered_size * context = (struct sdp_context_get_filtered_size *) my_context;
514     if (sdp_attribute_list_constains_id(context->attributeIDList, attributeID)) {
515         context->size += 3 + de_get_len(attributeValue);
516     }
517     return 0;
518 }
519 
520 int spd_get_filtered_size(uint8_t *record, uint8_t *attributeIDList){
521     struct sdp_context_get_filtered_size context;
522     context.size = 0;
523     context.attributeIDList = attributeIDList;
524     sdp_attribute_list_traverse_sequence(record, sdp_traversal_get_filtered_size, &context);
525     return context.size;
526 }
527 
528 // MARK: Get AttributeValue for AttributeID
529 // find attribute (ELEMENT) by ID
530 struct sdp_context_attribute_by_id {
531     uint16_t  attributeID;
532     uint8_t * attributeValue;
533 };
534 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){
535     UNUSED(de_type);
536     UNUSED(de_size);
537 
538     struct sdp_context_attribute_by_id * context = (struct sdp_context_attribute_by_id *) my_context;
539     if (attributeID == context->attributeID) {
540         context->attributeValue = attributeValue;
541         return 1;
542     }
543     return 0;
544 }
545 
546 uint8_t * sdp_get_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID){
547     struct sdp_context_attribute_by_id context;
548     context.attributeValue = NULL;
549     context.attributeID = attributeID;
550     sdp_attribute_list_traverse_sequence(record, sdp_traversal_attribute_by_id, &context);
551     return context.attributeValue;
552 }
553 
554 // MARK: Set AttributeValue for AttributeID
555 struct sdp_context_set_attribute_for_id {
556     uint16_t  attributeID;
557     uint32_t  attributeValue;
558     uint8_t   attributeFound;
559 };
560 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){
561     struct sdp_context_set_attribute_for_id * context = (struct sdp_context_set_attribute_for_id *) my_context;
562     if (attributeID == context->attributeID) {
563         context->attributeFound = 1;
564         switch (size){
565             case DE_SIZE_8:
566                 if (attributeType != DE_NIL){
567                     attributeValue[1] = context->attributeValue;
568                 }
569                 break;
570             case DE_SIZE_16:
571                 big_endian_store_16(attributeValue, 1, context->attributeValue);
572                 break;
573             case DE_SIZE_32:
574                 big_endian_store_32(attributeValue, 1, context->attributeValue);
575                 break;
576                 // Might want to support STRINGS to, copy upto original length
577             default:
578                 break;
579         }
580         return 1;
581     }
582     return 0;
583 }
584 uint8_t sdp_set_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID, uint32_t value){
585     struct sdp_context_set_attribute_for_id context;
586     context.attributeID = attributeID;
587     context.attributeValue = value;
588     context.attributeFound = 0;
589     sdp_attribute_list_traverse_sequence(record, sdp_traversal_set_attribute_for_id, &context);
590     return context.attributeFound;
591 }
592 
593 // MARK: ServiceRecord contains UUID
594 // service record contains UUID
595 // context { normalizedUUID }
596 struct sdp_context_contains_uuid128 {
597     uint8_t * uuid128;
598     int result;
599 };
600 int sdp_record_contains_UUID128(uint8_t *record, uint8_t *uuid128);
601 static int sdp_traversal_contains_UUID128(uint8_t * element, de_type_t type, de_size_t de_size, void *my_context){
602     UNUSED(de_size);
603 
604     struct sdp_context_contains_uuid128 * context = (struct sdp_context_contains_uuid128 *) my_context;
605     uint8_t normalizedUUID[16];
606     if (type == DE_UUID){
607         uint8_t uuidOK = de_get_normalized_uuid(normalizedUUID, element);
608         context->result = uuidOK && memcmp(context->uuid128, normalizedUUID, 16) == 0;
609     }
610     if (type == DE_DES){
611         context->result = sdp_record_contains_UUID128(element, context->uuid128);
612     }
613     return context->result;
614 }
615 int sdp_record_contains_UUID128(uint8_t *record, uint8_t *uuid128){
616     struct sdp_context_contains_uuid128 context;
617     context.uuid128 = uuid128;
618     context.result = 0;
619     de_traverse_sequence(record, sdp_traversal_contains_UUID128, &context);
620     return context.result;
621 }
622 
623 // MARK: ServiceRecord matches SearchServicePattern
624 // if UUID in searchServicePattern is not found in record => false
625 // context { result, record }
626 struct sdp_context_match_pattern {
627     uint8_t * record;
628     int result;
629 };
630 
631 int sdp_traversal_match_pattern(uint8_t * element, de_type_t de_type, de_size_t de_size, void *my_context){
632     UNUSED(de_type);
633     UNUSED(de_size);
634 
635     struct sdp_context_match_pattern * context = (struct sdp_context_match_pattern *) my_context;
636     uint8_t normalizedUUID[16];
637     uint8_t uuidOK = de_get_normalized_uuid(normalizedUUID, element);
638     if (!uuidOK || !sdp_record_contains_UUID128(context->record, normalizedUUID)){
639         context->result = 0;
640         return 1;
641     }
642     return 0;
643 }
644 int sdp_record_matches_service_search_pattern(uint8_t *record, uint8_t *serviceSearchPattern){
645     struct sdp_context_match_pattern context;
646     context.record = record;
647     context.result = 1;
648     de_traverse_sequence(serviceSearchPattern, sdp_traversal_match_pattern, &context);
649     return context.result;
650 }
651 
652 // MARK: Dump DataElement
653 // context { indent }
654 #ifdef ENABLE_SDP_DES_DUMP
655 static int de_traversal_dump_data(uint8_t * element, de_type_t de_type, de_size_t de_size, void *my_context){
656     int indent = *(int*) my_context;
657     int i;
658     for (i=0; i<indent;i++) printf("    ");
659     unsigned int pos     = de_get_header_size(element);
660     unsigned int end_pos = de_get_len(element);
661     printf("type %5s (%u), element len %2u ", type_names[de_type], de_type, end_pos);
662     if (de_type == DE_DES) {
663 		printf("\n");
664         indent++;
665         de_traverse_sequence(element, de_traversal_dump_data, (void *)&indent);
666     } else if (de_type == DE_UUID && de_size == DE_SIZE_128) {
667         printf(", value: %s\n", uuid128_to_str(element+1));
668     } else if (de_type == DE_STRING) {
669         unsigned int len = 0;
670         switch (de_size){
671             case DE_SIZE_VAR_8:
672                 len = element[1];
673                 break;
674             case DE_SIZE_VAR_16:
675                 len = big_endian_read_16(element, 1);
676                 break;
677             default:
678                 break;
679         }
680         printf("len %u (0x%02x)\n", len, len);
681         printf_hexdump(&element[pos], len);
682     } else {
683         uint32_t value = 0;
684         switch (de_size) {
685             case DE_SIZE_8:
686                 if (de_type != DE_NIL){
687                     value = element[pos];
688                 }
689                 break;
690             case DE_SIZE_16:
691 				value = big_endian_read_16(element,pos);
692                 break;
693             case DE_SIZE_32:
694 				value = big_endian_read_32(element,pos);
695                 break;
696             default:
697                 break;
698         }
699         printf(", value: 0x%08" PRIx32 "\n", value);
700     }
701     return 0;
702 }
703 #endif
704 
705 void de_dump_data_element(const uint8_t * record){
706 #ifdef ENABLE_SDP_DES_DUMP
707     int indent = 0;
708     // hack to get root DES, too.
709     de_type_t type = de_get_element_type(record);
710     de_size_t size = de_get_size_type(record);
711     de_traversal_dump_data((uint8_t *) record, type, size, (void*) &indent);
712 #endif
713 }
714 
715 uint8_t* sdp_service_search_pattern_for_uuid16(uint16_t uuid16){
716     big_endian_store_16(des_serviceSearchPatternUUID16, 3, uuid16);
717     return (uint8_t*)des_serviceSearchPatternUUID16;
718 }
719 
720 uint8_t* sdp_service_search_pattern_for_uuid128(const uint8_t * uuid128){
721     memcpy(&des_serviceSearchPatternUUID128[3], uuid128, 16);
722     return (uint8_t*)des_serviceSearchPatternUUID128;
723 }
724 
725