1eb886013SMatthias Ringwald /* 2eb886013SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3eb886013SMatthias Ringwald * 4eb886013SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5eb886013SMatthias Ringwald * modification, are permitted provided that the following conditions 6eb886013SMatthias Ringwald * are met: 7eb886013SMatthias Ringwald * 8eb886013SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9eb886013SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10eb886013SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11eb886013SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12eb886013SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13eb886013SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14eb886013SMatthias Ringwald * contributors may be used to endorse or promote products derived 15eb886013SMatthias Ringwald * from this software without specific prior written permission. 16eb886013SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17eb886013SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18eb886013SMatthias Ringwald * monetary gain. 19eb886013SMatthias Ringwald * 20eb886013SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21eb886013SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22eb886013SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23eb886013SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24eb886013SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25eb886013SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26eb886013SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27eb886013SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28eb886013SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29eb886013SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30eb886013SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31eb886013SMatthias Ringwald * SUCH DAMAGE. 32eb886013SMatthias Ringwald * 33eb886013SMatthias Ringwald * Please inquire about commercial licensing options at 34eb886013SMatthias Ringwald * [email protected] 35eb886013SMatthias Ringwald * 36eb886013SMatthias Ringwald */ 37eb886013SMatthias Ringwald 38eb886013SMatthias Ringwald /* 39eb886013SMatthias Ringwald * btstack_util.h 40eb886013SMatthias Ringwald * 41eb886013SMatthias Ringwald * General utility functions 42eb886013SMatthias Ringwald * 43eb886013SMatthias Ringwald * Created by Matthias Ringwald on 7/23/09. 44eb886013SMatthias Ringwald */ 45eb886013SMatthias Ringwald 46bf1b35bfSMatthias Ringwald #ifndef __BTSTACK_UTIL_H 47bf1b35bfSMatthias Ringwald #define __BTSTACK_UTIL_H 48eb886013SMatthias Ringwald 49eb886013SMatthias Ringwald 50eb886013SMatthias Ringwald #if defined __cplusplus 51eb886013SMatthias Ringwald extern "C" { 52eb886013SMatthias Ringwald #endif 53eb886013SMatthias Ringwald 54eb886013SMatthias Ringwald #include <stdint.h> 55eb886013SMatthias Ringwald 56eb886013SMatthias Ringwald /** 57eb886013SMatthias Ringwald * @brief hci connection handle type 58eb886013SMatthias Ringwald */ 59eb886013SMatthias Ringwald typedef uint16_t hci_con_handle_t; 60eb886013SMatthias Ringwald 61eb886013SMatthias Ringwald /** 62eb886013SMatthias Ringwald * @brief Length of a bluetooth device address. 63eb886013SMatthias Ringwald */ 64eb886013SMatthias Ringwald #define BD_ADDR_LEN 6 65eb886013SMatthias Ringwald typedef uint8_t bd_addr_t[BD_ADDR_LEN]; 66eb886013SMatthias Ringwald 67eb886013SMatthias Ringwald /** 68eb886013SMatthias Ringwald * @brief link key and its type 69eb886013SMatthias Ringwald */ 70eb886013SMatthias Ringwald #define LINK_KEY_LEN 16 71eb886013SMatthias Ringwald #define LINK_KEY_STR_LEN (LINK_KEY_LEN*2) 72eb886013SMatthias Ringwald typedef uint8_t link_key_t[LINK_KEY_LEN]; 73eb886013SMatthias Ringwald 74eb886013SMatthias Ringwald typedef enum { 75eb886013SMatthias Ringwald COMBINATION_KEY = 0, // standard pairing 76eb886013SMatthias Ringwald LOCAL_UNIT_KEY, // ? 77eb886013SMatthias Ringwald REMOTE_UNIT_KEY, // ? 78eb886013SMatthias Ringwald DEBUG_COMBINATION_KEY, // SSP with debug 79eb886013SMatthias Ringwald UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192, // SSP Simple Pairing 80eb886013SMatthias Ringwald AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192, // SSP Passkey, Number confirm, OOB 81eb886013SMatthias Ringwald CHANGED_COMBINATION_KEY, // Link key changed using Change Connection Lnk Key 82eb886013SMatthias Ringwald UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256, // SSP Simpe Pairing 83eb886013SMatthias Ringwald AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256, // SSP Passkey, Number confirm, OOB 84eb886013SMatthias Ringwald } link_key_type_t; 85eb886013SMatthias Ringwald 86eb886013SMatthias Ringwald /** 87eb886013SMatthias Ringwald * @brief 128 bit key used with AES128 in Security Manager 88eb886013SMatthias Ringwald */ 89eb886013SMatthias Ringwald typedef uint8_t sm_key_t[16]; 90eb886013SMatthias Ringwald 91eb886013SMatthias Ringwald /** 92eb886013SMatthias Ringwald * @brief The device name type 93eb886013SMatthias Ringwald */ 94eb886013SMatthias Ringwald #define DEVICE_NAME_LEN 248 95eb886013SMatthias Ringwald typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; 96eb886013SMatthias Ringwald 97eb886013SMatthias Ringwald // packet handler 98eb886013SMatthias Ringwald typedef void (*btstack_packet_handler_t) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 99eb886013SMatthias Ringwald 100eb886013SMatthias Ringwald // helper for BT little endian format 101*f8fbdce0SMatthias Ringwald #define little_endian_read_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8)) 102*f8fbdce0SMatthias Ringwald #define little_endian_read_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16)) 103*f8fbdce0SMatthias Ringwald #define little_endian_read_32( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3])) << 24) 104eb886013SMatthias Ringwald 105eb886013SMatthias Ringwald // helper for SDP big endian format 106*f8fbdce0SMatthias Ringwald #define big_endian_read_16( buffer, pos) ( ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos ]) << 8)) 107*f8fbdce0SMatthias Ringwald #define bit_endian_read_32( buffer, pos) ( ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos])) << 24) 108eb886013SMatthias Ringwald 109eb886013SMatthias Ringwald // HCI CMD OGF/OCF 110eb886013SMatthias Ringwald #define READ_CMD_OGF(buffer) (buffer[1] >> 2) 111eb886013SMatthias Ringwald #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0]) 112eb886013SMatthias Ringwald 113eb886013SMatthias Ringwald // check if command complete event for given command 114*f8fbdce0SMatthias Ringwald #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && little_endian_read_16(event,3) == cmd.opcode) 115*f8fbdce0SMatthias Ringwald #define COMMAND_STATUS_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && little_endian_read_16(event,4) == cmd.opcode) 116eb886013SMatthias Ringwald 117eb886013SMatthias Ringwald // Code+Len=2, Pkts+Opcode=3; total=5 118eb886013SMatthias Ringwald #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5 119eb886013SMatthias Ringwald 120eb886013SMatthias Ringwald // ACL Packet 121*f8fbdce0SMatthias Ringwald #define READ_ACL_CONNECTION_HANDLE( buffer ) ( little_endian_read_16(buffer,0) & 0x0fff) 122eb886013SMatthias Ringwald #define READ_ACL_FLAGS( buffer ) ( buffer[1] >> 4 ) 123*f8fbdce0SMatthias Ringwald #define READ_ACL_LENGTH( buffer ) (little_endian_read_16(buffer, 2)) 124eb886013SMatthias Ringwald 125eb886013SMatthias Ringwald // L2CAP Packet 126*f8fbdce0SMatthias Ringwald #define READ_L2CAP_LENGTH(buffer) ( little_endian_read_16(buffer, 4)) 127*f8fbdce0SMatthias Ringwald #define READ_L2CAP_CHANNEL_ID(buffer) ( little_endian_read_16(buffer, 6)) 128eb886013SMatthias Ringwald 129*f8fbdce0SMatthias Ringwald void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 130*f8fbdce0SMatthias Ringwald void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); 131eb886013SMatthias Ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src); 132eb886013SMatthias Ringwald 133*f8fbdce0SMatthias Ringwald void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 134*f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); 135eb886013SMatthias Ringwald 136eb886013SMatthias Ringwald // hack: compilation with the android ndk causes an error as there's a swap64 macro 137eb886013SMatthias Ringwald #ifdef swap64 138eb886013SMatthias Ringwald #undef swap64 139eb886013SMatthias Ringwald #endif 140eb886013SMatthias Ringwald 141eb886013SMatthias Ringwald void swapX (const uint8_t *src, uint8_t * dst, int len); 142bf1b35bfSMatthias Ringwald void swap24 (const uint8_t *src, uint8_t * dst); 143bf1b35bfSMatthias Ringwald void swap48 (const uint8_t *src, uint8_t * dst); 144bf1b35bfSMatthias Ringwald void swap56 (const uint8_t *src, uint8_t * dst); 145bf1b35bfSMatthias Ringwald void swap64 (const uint8_t *src, uint8_t * dst); 146bf1b35bfSMatthias Ringwald void swap128(const uint8_t *src, uint8_t * dst); 147eb886013SMatthias Ringwald 148eb886013SMatthias Ringwald char char_for_nibble(int nibble); 149eb886013SMatthias Ringwald 150eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size); 151eb886013SMatthias Ringwald void hexdump(const void *data, int size); 152eb886013SMatthias Ringwald void hexdumpf(const void *data, int size); 153eb886013SMatthias Ringwald char * uuid128_to_str(uint8_t * uuid); 154eb886013SMatthias Ringwald void printUUID128(uint8_t *uuid); 155eb886013SMatthias Ringwald void log_key(const char * name, sm_key_t key); 156eb886013SMatthias Ringwald 157eb886013SMatthias Ringwald // @deprecated please use more convenient bd_addr_to_str 158eb886013SMatthias Ringwald void print_bd_addr( bd_addr_t addr); 159eb886013SMatthias Ringwald 160eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr); 161eb886013SMatthias Ringwald char * link_key_to_str(link_key_t link_key); 162eb886013SMatthias Ringwald char *link_key_type_to_str(link_key_type_t link_key); 163eb886013SMatthias Ringwald 164eb886013SMatthias Ringwald void sdp_normalize_uuid(uint8_t *uuid, uint32_t shortUUID); 165eb886013SMatthias Ringwald int sdp_has_blueooth_base_uuid(uint8_t * uuid128); 166eb886013SMatthias Ringwald 167eb886013SMatthias Ringwald int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr); 168eb886013SMatthias Ringwald int sscan_link_key(char * addr_string, link_key_t link_key); 169eb886013SMatthias Ringwald 170eb886013SMatthias Ringwald uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum); 171eb886013SMatthias Ringwald uint8_t crc8_calc(uint8_t *data, uint16_t len); 172eb886013SMatthias Ringwald 173eb886013SMatthias Ringwald #define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN) 174eb886013SMatthias Ringwald #define BD_ADDR_COPY(dest,src) memcpy(dest,src,BD_ADDR_LEN) 175eb886013SMatthias Ringwald 176eb886013SMatthias Ringwald int is_authenticated_link_key(link_key_type_t link_key_type); 177eb886013SMatthias Ringwald 178eb886013SMatthias Ringwald #if defined __cplusplus 179eb886013SMatthias Ringwald } 180eb886013SMatthias Ringwald #endif 181eb886013SMatthias Ringwald 182bf1b35bfSMatthias Ringwald #endif // __BTSTACK_UTIL_H 183