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