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