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 38ab2c6ae4SMatthias 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 66b12ad867SMatthias Ringwald int ad_iterator_has_more(const ad_context_t * context){ 67*88949f84SMatthias Ringwald // assert chunk_len and chunk_type are withing buffer 68*88949f84SMatthias Ringwald if ((context->offset+1) >= context->length) return 0; 69*88949f84SMatthias Ringwald 70*88949f84SMatthias Ringwald // assert chunk_len > 0 71*88949f84SMatthias Ringwald int chunk_len = context->data[context->offset]; 72*88949f84SMatthias Ringwald if (chunk_len == 0) return 0; 73*88949f84SMatthias Ringwald 74*88949f84SMatthias Ringwald // assert complete chunk fits into buffer 75*88949f84SMatthias Ringwald if (context->offset + 1 + chunk_len > context->length) return 0; 76*88949f84SMatthias Ringwald 77*88949f84SMatthias Ringwald return 1; 78b12ad867SMatthias Ringwald } 79b12ad867SMatthias Ringwald 80*88949f84SMatthias 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]; 83*88949f84SMatthias 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 98b12ad867SMatthias Ringwald int ad_data_contains_uuid16(uint8_t ad_len, const uint8_t * ad_data, uint16_t uuid16){ 99b12ad867SMatthias Ringwald ad_context_t context; 100b12ad867SMatthias Ringwald for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 101b12ad867SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 102b12ad867SMatthias Ringwald uint8_t data_len = ad_iterator_get_data_len(&context); 103b12ad867SMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 104b12ad867SMatthias Ringwald 105b12ad867SMatthias Ringwald int i; 106b12ad867SMatthias Ringwald uint8_t ad_uuid128[16], uuid128_bt[16]; 107b12ad867SMatthias Ringwald 108b12ad867SMatthias Ringwald switch (data_type){ 1091d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 1101d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 111b12ad867SMatthias Ringwald for (i=0; i<data_len; i+=2){ 112b12ad867SMatthias Ringwald uint16_t uuid = little_endian_read_16(data, i); 113b12ad867SMatthias Ringwald if ( uuid == uuid16 ) return 1; 114b12ad867SMatthias Ringwald } 115b12ad867SMatthias Ringwald break; 1161d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS: 1171d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS: 118b12ad867SMatthias Ringwald uuid_add_bluetooth_prefix(ad_uuid128, uuid16); 119b12ad867SMatthias Ringwald reverse_128(ad_uuid128, uuid128_bt); 120b12ad867SMatthias Ringwald 121b12ad867SMatthias Ringwald for (i=0; i<data_len; i+=16){ 122b12ad867SMatthias Ringwald if (memcmp(uuid128_bt, &data[i], 16) == 0) return 1; 123b12ad867SMatthias Ringwald } 124b12ad867SMatthias Ringwald break; 125b12ad867SMatthias Ringwald default: 126b12ad867SMatthias Ringwald break; 127b12ad867SMatthias Ringwald } 128b12ad867SMatthias Ringwald } 129b12ad867SMatthias Ringwald return 0; 130b12ad867SMatthias Ringwald } 131b12ad867SMatthias Ringwald 132b12ad867SMatthias Ringwald int ad_data_contains_uuid128(uint8_t ad_len, const uint8_t * ad_data, const uint8_t * uuid128){ 133b12ad867SMatthias Ringwald ad_context_t context; 134b12ad867SMatthias Ringwald // input in big endian/network order, bluetooth data in little endian 135b12ad867SMatthias Ringwald uint8_t uuid128_le[16]; 136b12ad867SMatthias Ringwald reverse_128(uuid128, uuid128_le); 137b12ad867SMatthias Ringwald for (ad_iterator_init(&context, ad_len, ad_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 138b12ad867SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 139b12ad867SMatthias Ringwald uint8_t data_len = ad_iterator_get_data_len(&context); 140b12ad867SMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 141b12ad867SMatthias Ringwald 142b12ad867SMatthias Ringwald int i; 143b12ad867SMatthias Ringwald uint8_t ad_uuid128[16]; 144b12ad867SMatthias Ringwald 145b12ad867SMatthias Ringwald 146b12ad867SMatthias Ringwald switch (data_type){ 1471d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 1481d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 149b12ad867SMatthias Ringwald for (i=0; i<data_len; i+=2){ 150b12ad867SMatthias Ringwald uint16_t uuid16 = little_endian_read_16(data, i); 151b12ad867SMatthias Ringwald uuid_add_bluetooth_prefix(ad_uuid128, uuid16); 152b12ad867SMatthias Ringwald 153b12ad867SMatthias Ringwald if (memcmp(ad_uuid128, uuid128_le, 16) == 0) return 1; 154b12ad867SMatthias Ringwald } 155b12ad867SMatthias Ringwald 156b12ad867SMatthias Ringwald break; 1571d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS: 1581d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS: 159b12ad867SMatthias Ringwald for (i=0; i<data_len; i+=16){ 160b12ad867SMatthias Ringwald if (memcmp(uuid128_le, &data[i], 16) == 0) return 1; 161b12ad867SMatthias Ringwald } 162b12ad867SMatthias Ringwald break; 163b12ad867SMatthias Ringwald default: 164b12ad867SMatthias Ringwald break; 165b12ad867SMatthias Ringwald } 166b12ad867SMatthias Ringwald } 167b12ad867SMatthias Ringwald return 0; 168b12ad867SMatthias Ringwald } 169b12ad867SMatthias Ringwald 170