xref: /btstack/src/ad_parser.c (revision 1a6822024624aab466a9aff5f67325f81ed6d5e2)
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 "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 int  ad_iterator_has_more(const ad_context_t * context){
65     return context->offset < context->length;
66 }
67 
68 void ad_iterator_next(ad_context_t * context){
69     int chunk_len = context->data[context->offset];
70     int new_offset = context->offset + 1 + chunk_len;
71     // avoid uint8_t overrun
72     if (new_offset > 0xff){
73         new_offset = 0xff;
74     }
75     context->offset = new_offset;
76 }
77 
78 uint8_t   ad_iterator_get_data_len(const ad_context_t * context){
79     return context->data[context->offset] - 1;
80 }
81 
82 uint8_t   ad_iterator_get_data_type(const ad_context_t * context){
83     return context->data[context->offset + 1];
84 }
85 
86 const uint8_t * ad_iterator_get_data(const ad_context_t * context){
87     return &context->data[context->offset + 2];
88 }
89 
90 int ad_data_contains_uuid16(uint8_t ad_len, const uint8_t * ad_data, uint16_t uuid16){
91     ad_context_t context;
92     for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
93         uint8_t data_type    = ad_iterator_get_data_type(&context);
94         uint8_t data_len     = ad_iterator_get_data_len(&context);
95         const uint8_t * data = ad_iterator_get_data(&context);
96 
97         int i;
98         uint8_t ad_uuid128[16], uuid128_bt[16];
99 
100         switch (data_type){
101             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
102             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
103                 for (i=0; i<data_len; i+=2){
104                     uint16_t uuid = little_endian_read_16(data, i);
105                     if ( uuid == uuid16 ) return 1;
106                 }
107                 break;
108             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
109             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
110                 uuid_add_bluetooth_prefix(ad_uuid128, uuid16);
111                 reverse_128(ad_uuid128, uuid128_bt);
112 
113                 for (i=0; i<data_len; i+=16){
114                     if (memcmp(uuid128_bt, &data[i], 16) == 0) return 1;
115                 }
116                 break;
117             default:
118                 break;
119         }
120     }
121     return 0;
122 }
123 
124 int ad_data_contains_uuid128(uint8_t ad_len, const uint8_t * ad_data, const uint8_t * uuid128){
125     ad_context_t context;
126     // input in big endian/network order, bluetooth data in little endian
127     uint8_t uuid128_le[16];
128     reverse_128(uuid128, uuid128_le);
129     for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){
130         uint8_t data_type = ad_iterator_get_data_type(&context);
131         uint8_t data_len  = ad_iterator_get_data_len(&context);
132         const uint8_t * data = ad_iterator_get_data(&context);
133 
134         int i;
135         uint8_t ad_uuid128[16];
136 
137 
138         switch (data_type){
139             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
140             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
141                 for (i=0; i<data_len; i+=2){
142                     uint16_t uuid16 = little_endian_read_16(data, i);
143                     uuid_add_bluetooth_prefix(ad_uuid128, uuid16);
144 
145                     if (memcmp(ad_uuid128, uuid128_le, 16) == 0) return 1;
146                 }
147 
148                 break;
149             case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
150             case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
151                 for (i=0; i<data_len; i+=16){
152                     if (memcmp(uuid128_le, &data[i], 16) == 0) return 1;
153                 }
154                 break;
155             default:
156                 break;
157         }
158     }
159     return 0;
160 }
161 
162