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