xref: /btstack/src/ad_parser.c (revision b12ad86731289e1fe57e6b39dca956855fa22842)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 
39 // *****************************************************************************
40 //
41 // Advertising Data Parser
42 //
43 // *****************************************************************************
44 
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "btstack_util.h"
51 #include "classic/sdp_util.h"
52 #include "hci_cmd.h"
53 
54 #include "hci.h"
55 #include "ble/ad_parser.h"
56 
57 typedef enum {
58     IncompleteList16 = 0x02,
59     CompleteList16 = 0x03,
60     IncompleteList128 = 0x06,
61     CompleteList128 = 0x07
62 } UUID_TYPE;
63 
64 void ad_iterator_init(ad_context_t *context, uint8_t ad_len, const uint8_t * ad_data){
65     context->data = ad_data;
66     context->length = ad_len;
67     context->offset = 0;
68 }
69 
70 int  ad_iterator_has_more(const ad_context_t * context){
71     return context->offset < context->length;
72 }
73 
74 void ad_iterator_next(ad_context_t * context){
75     int chunk_len = context->data[context->offset];
76     int new_offset = context->offset + 1 + chunk_len;
77     // avoid uint8_t overrun
78     if (new_offset > 0xff){
79         new_offset = 0xff;
80     }
81     context->offset = new_offset;
82 }
83 
84 uint8_t   ad_iterator_get_data_len(const ad_context_t * context){
85     return context->data[context->offset] - 1;
86 }
87 
88 uint8_t   ad_iterator_get_data_type(const ad_context_t * context){
89     return context->data[context->offset + 1];
90 }
91 
92 const uint8_t * ad_iterator_get_data(const ad_context_t * context){
93     return &context->data[context->offset + 2];
94 }
95 
96 int ad_data_contains_uuid16(uint8_t ad_len, const uint8_t * ad_data, uint16_t uuid16){
97     ad_context_t context;
98     for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
99         uint8_t data_type    = ad_iterator_get_data_type(&context);
100         uint8_t data_len     = ad_iterator_get_data_len(&context);
101         const uint8_t * data = ad_iterator_get_data(&context);
102 
103         int i;
104         uint8_t ad_uuid128[16], uuid128_bt[16];
105 
106         switch (data_type){
107             case IncompleteList16:
108             case CompleteList16:
109                 for (i=0; i<data_len; i+=2){
110                     uint16_t uuid = little_endian_read_16(data, i);
111                     if ( uuid == uuid16 ) return 1;
112                 }
113                 break;
114             case IncompleteList128:
115             case CompleteList128:
116                 uuid_add_bluetooth_prefix(ad_uuid128, uuid16);
117                 reverse_128(ad_uuid128, uuid128_bt);
118 
119                 for (i=0; i<data_len; i+=16){
120                     if (memcmp(uuid128_bt, &data[i], 16) == 0) return 1;
121                 }
122                 break;
123             default:
124                 break;
125         }
126     }
127     return 0;
128 }
129 
130 int ad_data_contains_uuid128(uint8_t ad_len, const uint8_t * ad_data, const uint8_t * uuid128){
131     ad_context_t context;
132     // input in big endian/network order, bluetooth data in little endian
133     uint8_t uuid128_le[16];
134     reverse_128(uuid128, uuid128_le);
135     for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
136         uint8_t data_type = ad_iterator_get_data_type(&context);
137         uint8_t data_len  = ad_iterator_get_data_len(&context);
138         const uint8_t * data = ad_iterator_get_data(&context);
139 
140         int i;
141         uint8_t ad_uuid128[16];
142 
143 
144         switch (data_type){
145             case IncompleteList16:
146             case CompleteList16:
147                 for (i=0; i<data_len; i+=2){
148                     uint16_t uuid16 = little_endian_read_16(data, i);
149                     uuid_add_bluetooth_prefix(ad_uuid128, uuid16);
150 
151                     if (memcmp(ad_uuid128, uuid128_le, 16) == 0) return 1;
152                 }
153 
154                 break;
155             case IncompleteList128:
156             case CompleteList128:
157                 for (i=0; i<data_len; i+=16){
158                     if (memcmp(uuid128_le, &data[i], 16) == 0) return 1;
159                 }
160                 break;
161             default:
162                 break;
163         }
164     }
165     return 0;
166 }
167 
168