xref: /btstack/src/classic/sdp_util.c (revision d6de13b21f4bc2ba779236aacaf9c78424223082)
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 static int sdp_traversal_attribute_list_valie(uint8_t * element, de_type_t type, de_size_t size, void *my_context) {
395     bool ok = true;
396     if (type == DE_UINT) {
397         ok = false;
398     }
399     if ((size != DE_SIZE_16) && (size != DE_SIZE_32)) {
400         ok = false;
401     }
402     if (size == DE_SIZE_32){
403         uint16_t lower  = big_endian_read_16(element, 1);
404         uint16_t higher = big_endian_read_16(element, 3);
405         if (lower > higher){
406             ok = false;
407         }
408     }
409     if (ok){
410         return 0;
411     } else {
412         *(bool *)my_context = false;
413         return 0;
414     }
415 }
416 
417 bool sdp_attribute_list_valid(uint8_t *attributeIDList){
418     bool attribute_list_valid = true;
419     de_traverse_sequence(attributeIDList, sdp_traversal_attribute_list_valie, &attribute_list_valid);
420     return attribute_list_valid;
421 }
422 
423 static int sdp_traversal_valid_uuid(uint8_t * element, de_type_t type, de_size_t size, void *my_context) {
424     if (type == DE_UUID) {
425         return 0;
426     } else {
427         *(bool *)my_context = false;
428         return 0;
429     }
430 }
431 
432 bool sdp_valid_service_search_pattern(uint8_t *service_search_pattern){
433     bool search_pattenr_valid = true;
434     de_traverse_sequence(service_search_pattern, sdp_traversal_valid_uuid, &search_pattenr_valid);
435     return search_pattenr_valid;
436 }
437 
438 // MARK: Append Attributes for AttributeIDList
439 // pre: buffer contains DES with 2 byte length field
440 struct sdp_context_append_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 };
447 
448 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){
449     UNUSED(de_type);
450     UNUSED(de_size);
451     struct sdp_context_append_attributes * context = (struct sdp_context_append_attributes *) my_context;
452     if (sdp_attribute_list_contains_id(context->attributeIDList, attributeID)) {
453         // DES_HEADER(3) + DES_DATA + (UINT16(3) + attribute)
454         uint16_t data_size = big_endian_read_16(context->buffer, 1);
455         int attribute_len = de_get_len(attributeValue);
456         if ((3 + data_size + (3 + attribute_len)) <= context->maxBytes) {
457             // copy Attribute
458             de_add_number(context->buffer, DE_UINT, DE_SIZE_16, attributeID);
459             data_size += 3; // 3 bytes
460             (void)memcpy(context->buffer + 3 + data_size, attributeValue,
461                          attribute_len);
462             big_endian_store_16(context->buffer,1,data_size+attribute_len);
463         } else {
464             // not enought space left -> continue with previous element
465             return 1;
466         }
467     }
468     return 0;
469 }
470 
471 // maxBytes: maximal size of data element sequence
472 uint16_t sdp_append_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint8_t *buffer){
473     struct sdp_context_append_attributes context;
474     context.buffer = buffer;
475     context.maxBytes = maxBytes;
476     context.usedBytes = 0;
477     context.startOffset = startOffset;
478     context.attributeIDList = attributeIDList;
479     sdp_attribute_list_traverse_sequence(record, sdp_traversal_append_attributes, &context);
480     return context.usedBytes;
481 }
482 
483 // MARK: Filter attributes that match attribute list from startOffset and a max nr bytes
484 struct sdp_context_filter_attributes {
485     uint8_t * buffer;
486     uint16_t startOffset;     // offset of when to start copying
487     uint16_t maxBytes;
488     uint16_t usedBytes;
489     uint8_t *attributeIDList;
490     bool     complete;
491 };
492 
493 // copy data with given start offset and max bytes, returns OK if all data has been copied
494 static int spd_append_range(struct sdp_context_filter_attributes* context, uint16_t len, uint8_t *data){
495     int ok = 1;
496     uint16_t remainder_len = len - context->startOffset;
497     if (context->maxBytes < remainder_len){
498         remainder_len = context->maxBytes;
499         ok = 0;
500     }
501     (void)memcpy(context->buffer, &data[context->startOffset], remainder_len);
502     context->usedBytes += remainder_len;
503     context->buffer    += remainder_len;
504     context->maxBytes  -= remainder_len;
505     context->startOffset = 0;
506     return ok;
507 }
508 
509 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){
510     UNUSED(de_type);
511     UNUSED(de_size);
512 
513     struct sdp_context_filter_attributes * context = (struct sdp_context_filter_attributes *) my_context;
514 
515     if (!sdp_attribute_list_contains_id(context->attributeIDList, attributeID)) return 0;
516 
517     // { Attribute ID (Descriptor, big endian 16-bit ID), AttributeValue (data)}
518 
519     // handle Attribute ID
520     if (context->startOffset >= 3){
521         context->startOffset -= 3;
522     } else {
523         uint8_t idBuffer[3];
524         de_store_descriptor(idBuffer, DE_UINT,  DE_SIZE_16);
525         big_endian_store_16(idBuffer,1,attributeID);
526 
527         int ok = spd_append_range(context, 3, idBuffer);
528         if (!ok) {
529             context->complete = false;
530             return 1;
531         }
532     }
533 
534     // handle Attribute Value
535     int attribute_len = de_get_len(attributeValue);
536     if (context->startOffset >= attribute_len) {
537         context->startOffset -= attribute_len;
538         return 0;
539     }
540 
541     int ok = spd_append_range(context, attribute_len, attributeValue);
542     if (!ok) {
543         context->complete = false;
544         return 1;
545     }
546     return 0;
547 }
548 
549 bool sdp_filter_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint16_t *usedBytes, uint8_t *buffer){
550 
551     struct sdp_context_filter_attributes context;
552     context.buffer = buffer;
553     context.maxBytes = maxBytes;
554     context.usedBytes = 0;
555     context.startOffset = startOffset;
556     context.attributeIDList = attributeIDList;
557     context.complete = true;
558 
559     sdp_attribute_list_traverse_sequence(record, sdp_traversal_filter_attributes, &context);
560 
561     *usedBytes = context.usedBytes;
562     return context.complete;
563 }
564 
565 // MARK: Get sum of attributes matching attribute list
566 struct sdp_context_get_filtered_size {
567     uint8_t *attributeIDList;
568     uint16_t size;
569 };
570 
571 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){
572     UNUSED(de_type);
573     UNUSED(de_size);
574 
575     struct sdp_context_get_filtered_size * context = (struct sdp_context_get_filtered_size *) my_context;
576     if (sdp_attribute_list_contains_id(context->attributeIDList, attributeID)) {
577         context->size += 3 + de_get_len(attributeValue);
578     }
579     return 0;
580 }
581 
582 uint16_t sdp_get_filtered_size(uint8_t *record, uint8_t *attributeIDList){
583     struct sdp_context_get_filtered_size context;
584     context.size = 0;
585     context.attributeIDList = attributeIDList;
586     sdp_attribute_list_traverse_sequence(record, sdp_traversal_get_filtered_size, &context);
587     return context.size;
588 }
589 
590 // MARK: Get AttributeValue for AttributeID
591 // find attribute (ELEMENT) by ID
592 struct sdp_context_attribute_by_id {
593     uint16_t  attributeID;
594     uint8_t * attributeValue;
595 };
596 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){
597     UNUSED(de_type);
598     UNUSED(de_size);
599 
600     struct sdp_context_attribute_by_id * context = (struct sdp_context_attribute_by_id *) my_context;
601     if (attributeID == context->attributeID) {
602         context->attributeValue = attributeValue;
603         return 1;
604     }
605     return 0;
606 }
607 
608 uint8_t * sdp_get_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID){
609     struct sdp_context_attribute_by_id context;
610     context.attributeValue = NULL;
611     context.attributeID = attributeID;
612     sdp_attribute_list_traverse_sequence(record, sdp_traversal_attribute_by_id, &context);
613     return context.attributeValue;
614 }
615 
616 // MARK: Set AttributeValue for AttributeID
617 struct sdp_context_set_attribute_for_id {
618     uint16_t  attributeID;
619     uint32_t  attributeValue;
620     bool      attributeFound;
621 };
622 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){
623     struct sdp_context_set_attribute_for_id * context = (struct sdp_context_set_attribute_for_id *) my_context;
624     if (attributeID == context->attributeID) {
625         context->attributeFound = true;
626         switch (size){
627             case DE_SIZE_8:
628                 if (attributeType != DE_NIL){
629                     attributeValue[1] = context->attributeValue;
630                 }
631                 break;
632             case DE_SIZE_16:
633                 big_endian_store_16(attributeValue, 1, context->attributeValue);
634                 break;
635             case DE_SIZE_32:
636                 big_endian_store_32(attributeValue, 1, context->attributeValue);
637                 break;
638                 // Might want to support STRINGS to, copy upto original length
639             default:
640                 break;
641         }
642         return 1;
643     }
644     return 0;
645 }
646 bool sdp_set_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID, uint32_t value){
647     struct sdp_context_set_attribute_for_id context;
648     context.attributeID = attributeID;
649     context.attributeValue = value;
650     context.attributeFound = false;
651     sdp_attribute_list_traverse_sequence(record, sdp_traversal_set_attribute_for_id, &context);
652     return context.attributeFound;
653 }
654 
655 // MARK: ServiceRecord contains UUID
656 // service record contains UUID
657 // context { normalizedUUID }
658 struct sdp_context_contains_uuid128 {
659     uint8_t * uuid128;
660     int result;
661 };
662 int sdp_record_contains_UUID128(uint8_t *record, uint8_t *uuid128);
663 static int sdp_traversal_contains_UUID128(uint8_t * element, de_type_t type, de_size_t de_size, void *my_context){
664     UNUSED(de_size);
665 
666     struct sdp_context_contains_uuid128 * context = (struct sdp_context_contains_uuid128 *) my_context;
667     uint8_t normalizedUUID[16];
668     if (type == DE_UUID){
669         uint8_t uuidOK = de_get_normalized_uuid(normalizedUUID, element);
670         context->result = uuidOK && (memcmp(context->uuid128, normalizedUUID, 16) == 0);
671     }
672     if (type == DE_DES){
673         context->result = sdp_record_contains_UUID128(element, context->uuid128);
674     }
675     return context->result;
676 }
677 int sdp_record_contains_UUID128(uint8_t *record, uint8_t *uuid128){
678     struct sdp_context_contains_uuid128 context;
679     context.uuid128 = uuid128;
680     context.result = 0;
681     de_traverse_sequence(record, sdp_traversal_contains_UUID128, &context);
682     return context.result;
683 }
684 
685 // MARK: ServiceRecord matches SearchServicePattern
686 // if UUID in searchServicePattern is not found in record => false
687 // context { result, record }
688 struct sdp_context_match_pattern {
689     uint8_t * record;
690     bool result;
691 };
692 
693 static int sdp_traversal_match_pattern(uint8_t * element, de_type_t de_type, de_size_t de_size, void *my_context){
694     UNUSED(de_type);
695     UNUSED(de_size);
696 
697     struct sdp_context_match_pattern * context = (struct sdp_context_match_pattern *) my_context;
698     uint8_t normalizedUUID[16];
699     uint8_t uuidOK = de_get_normalized_uuid(normalizedUUID, element);
700     if (!uuidOK || !sdp_record_contains_UUID128(context->record, normalizedUUID)){
701         context->result = false;
702         return 1;
703     }
704     return 0;
705 }
706 bool sdp_record_matches_service_search_pattern(uint8_t *record, uint8_t *serviceSearchPattern){
707     struct sdp_context_match_pattern context;
708     context.record = record;
709     context.result = true;
710     de_traverse_sequence(serviceSearchPattern, sdp_traversal_match_pattern, &context);
711     return context.result;
712 }
713 
714 // MARK: Dump DataElement
715 // context { indent }
716 #ifdef ENABLE_SDP_DES_DUMP
717 static int de_traversal_dump_data(uint8_t * element, de_type_t de_type, de_size_t de_size, void *my_context){
718     unsigned int indent = *(int*) my_context;
719     unsigned int i;
720     for (i=0; i<indent;i++) printf("    ");
721     unsigned int pos     = de_get_header_size(element);
722     unsigned int end_pos = de_get_len(element);
723     printf("type %5s (%u), element len %2u ", type_names[de_type], de_type, end_pos);
724     if (de_type == DE_DES) {
725 		printf("\n");
726         indent++;
727         de_traverse_sequence(element, de_traversal_dump_data, (void *)&indent);
728     } else if (de_type == DE_UUID && de_size == DE_SIZE_128) {
729         printf(", value: %s\n", uuid128_to_str(element+1));
730     } else if (de_type == DE_STRING) {
731         unsigned int len = 0;
732         switch (de_size){
733             case DE_SIZE_VAR_8:
734                 len = element[1];
735                 break;
736             case DE_SIZE_VAR_16:
737                 len = big_endian_read_16(element, 1);
738                 break;
739             default:
740                 break;
741         }
742         printf(", len %2u, value: '", len);
743         for (i=0;i<len;i++){
744             uint8_t c = element[pos + i];
745             printf("%c", (c >= 0x20 && c <= 0x7f) ? c : '.');
746         }
747         printf("'\n");
748     } else {
749         uint32_t value = 0;
750         switch (de_size) {
751             case DE_SIZE_8:
752                 if (de_type != DE_NIL){
753                     value = element[pos];
754                 }
755                 break;
756             case DE_SIZE_16:
757 				value = big_endian_read_16(element,pos);
758                 break;
759             case DE_SIZE_32:
760 				value = big_endian_read_32(element,pos);
761                 break;
762             default:
763                 break;
764         }
765         printf(", value: 0x%08" PRIx32 "\n", value);
766     }
767     return 0;
768 }
769 #endif
770 
771 void de_dump_data_element(const uint8_t * record){
772 #ifdef ENABLE_SDP_DES_DUMP
773     unsigned int indent = 0;
774     // hack to get root DES, too.
775     de_type_t type = de_get_element_type(record);
776     de_size_t size = de_get_size_type(record);
777     de_traversal_dump_data((uint8_t *) record, type, size, (void*) &indent);
778 #else
779 UNUSED(record);
780 #endif
781 }
782 
783 uint8_t* sdp_service_search_pattern_for_uuid16(uint16_t uuid16){
784     big_endian_store_16(des_service_search_pattern_uuid16, 3, uuid16);
785     return (uint8_t*)des_service_search_pattern_uuid16;
786 }
787 
788 uint8_t* sdp_service_search_pattern_for_uuid128(const uint8_t * uuid128){
789     (void)memcpy(&des_service_search_pattern_uuid128[3], uuid128, 16);
790     return (uint8_t*)des_service_search_pattern_uuid128;
791 }
792 
793