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