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