xref: /btstack/src/ad_parser.c (revision d58a1b5f11ada8ddf896c41fff5a35e7f140c37e)
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 <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "bluetooth_data_types.h"
53 #include "btstack_util.h"
54 #include "classic/sdp_util.h"
55 #include "hci.h"
56 #include "hci_cmd.h"
57 
58 #include "ad_parser.h"
59 
60 void ad_iterator_init(ad_context_t *context, uint8_t ad_len, const uint8_t * ad_data){
61     context->data = ad_data;
62     context->length = ad_len;
63     context->offset = 0;
64 }
65 
66 int  ad_iterator_has_more(const ad_context_t * context){
67     // assert chunk_len and chunk_type are withing buffer
68     if ((context->offset+1) >= context->length) return 0;
69 
70     // assert chunk_len > 0
71     int chunk_len = context->data[context->offset];
72     if (chunk_len == 0)                                    return 0;
73 
74     // assert complete chunk fits into buffer
75     if ((context->offset + 1 + chunk_len) > context->length) return 0;
76 
77     return 1;
78 }
79 
80 // pre: ad_iterator_has_more() == 1
81 void ad_iterator_next(ad_context_t * context){
82     int chunk_len = context->data[context->offset];
83     context->offset += 1 + chunk_len;
84 }
85 
86 uint8_t   ad_iterator_get_data_len(const ad_context_t * context){
87     return context->data[context->offset] - 1;
88 }
89 
90 uint8_t   ad_iterator_get_data_type(const ad_context_t * context){
91     return context->data[context->offset + 1];
92 }
93 
94 const uint8_t * ad_iterator_get_data(const ad_context_t * context){
95     return &context->data[context->offset + 2];
96 }
97 
98 int ad_data_contains_uuid16(uint8_t ad_len, const uint8_t * ad_data, uint16_t uuid16){
99     ad_context_t context;
100     for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
101         uint8_t data_type    = ad_iterator_get_data_type(&context);
102         uint8_t data_len     = ad_iterator_get_data_len(&context);
103         const uint8_t * data = ad_iterator_get_data(&context);
104 
105         int i;
106         uint8_t ad_uuid128[16], uuid128_bt[16];
107 
108         switch (data_type){
109             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
110             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
111                 for (i=0; i<data_len; i+=2){
112                     uint16_t uuid = little_endian_read_16(data, i);
113                     if ( uuid == uuid16 ) return 1;
114                 }
115                 break;
116             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
117             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
118                 uuid_add_bluetooth_prefix(ad_uuid128, uuid16);
119                 reverse_128(ad_uuid128, uuid128_bt);
120 
121                 for (i=0; i<data_len; i+=16){
122                     if (memcmp(uuid128_bt, &data[i], 16) == 0) return 1;
123                 }
124                 break;
125             default:
126                 break;
127         }
128     }
129     return 0;
130 }
131 
132 int ad_data_contains_uuid128(uint8_t ad_len, const uint8_t * ad_data, const uint8_t * uuid128){
133     ad_context_t context;
134     // input in big endian/network order, bluetooth data in little endian
135     uint8_t uuid128_le[16];
136     reverse_128(uuid128, uuid128_le);
137     for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
138         uint8_t data_type = ad_iterator_get_data_type(&context);
139         uint8_t data_len  = ad_iterator_get_data_len(&context);
140         const uint8_t * data = ad_iterator_get_data(&context);
141 
142         int i;
143         uint8_t ad_uuid128[16];
144 
145 
146         switch (data_type){
147             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
148             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
149                 for (i=0; i<data_len; i+=2){
150                     uint16_t uuid16 = little_endian_read_16(data, i);
151                     uuid_add_bluetooth_prefix(ad_uuid128, uuid16);
152 
153                     if (memcmp(ad_uuid128, uuid128_le, 16) == 0) return 1;
154                 }
155 
156                 break;
157             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
158             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
159                 for (i=0; i<data_len; i+=16){
160                     if (memcmp(uuid128_le, &data[i], 16) == 0) return 1;
161                 }
162                 break;
163             default:
164                 break;
165         }
166     }
167     return 0;
168 }
169 
170