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