2009-10-29 20:25:42 +00:00
|
|
|
/*
|
2015-02-06 16:19:27 +00:00
|
|
|
* Copyright (C) 2014 BlueKitchen GmbH
|
2009-10-29 20:25:42 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2015-02-06 16:19:27 +00:00
|
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
|
|
* personal benefit and not for any commercial purpose or for
|
|
|
|
* monetary gain.
|
2009-10-29 20:25:42 +00:00
|
|
|
*
|
2015-02-06 16:19:27 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
2009-10-29 20:25:42 +00:00
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2015-02-06 16:19:27 +00:00
|
|
|
* Please inquire about commercial licensing options at
|
|
|
|
* contact@bluekitchen-gmbh.com
|
|
|
|
*
|
2009-10-29 20:25:42 +00:00
|
|
|
*/
|
|
|
|
|
2009-07-23 21:43:37 +00:00
|
|
|
/*
|
2016-01-20 16:10:07 +01:00
|
|
|
* btstack_util.h
|
2009-07-23 21:43:37 +00:00
|
|
|
*
|
|
|
|
* General utility functions
|
|
|
|
*
|
|
|
|
* Created by Matthias Ringwald on 7/23/09.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 17:26:18 +01:00
|
|
|
#ifndef __BTSTACK_UTIL_H
|
|
|
|
#define __BTSTACK_UTIL_H
|
2009-07-23 21:43:37 +00:00
|
|
|
|
2011-06-05 09:52:22 +00:00
|
|
|
|
2010-08-08 18:17:48 +00:00
|
|
|
#if defined __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2011-06-05 11:23:15 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2016-02-10 11:35:31 +01:00
|
|
|
#include "bluetooth.h"
|
|
|
|
#include "btstack_defines.h"
|
2016-02-03 11:59:48 +01:00
|
|
|
#include "btstack_linked_list.h"
|
2016-02-10 11:35:31 +01:00
|
|
|
|
|
|
|
// will be moved to daemon/btstack_device_name_db.h
|
2014-01-03 19:19:37 +00:00
|
|
|
|
2010-08-30 18:44:50 +00:00
|
|
|
/**
|
|
|
|
* @brief The device name type
|
|
|
|
*/
|
|
|
|
#define DEVICE_NAME_LEN 248
|
|
|
|
typedef uint8_t device_name_t[DEVICE_NAME_LEN+1];
|
|
|
|
|
2016-02-03 11:59:48 +01:00
|
|
|
|
2009-07-23 21:43:37 +00:00
|
|
|
// helper for BT little endian format
|
2016-01-31 00:07:32 +01:00
|
|
|
#define little_endian_read_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8))
|
|
|
|
#define little_endian_read_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16))
|
|
|
|
#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)
|
2009-07-23 21:43:37 +00:00
|
|
|
|
2010-06-09 15:30:49 +00:00
|
|
|
// helper for SDP big endian format
|
2016-01-31 00:07:32 +01:00
|
|
|
#define big_endian_read_16( buffer, pos) ( ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos ]) << 8))
|
|
|
|
#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)
|
2010-06-09 15:30:49 +00:00
|
|
|
|
2009-09-29 19:40:55 +00:00
|
|
|
// HCI CMD OGF/OCF
|
|
|
|
#define READ_CMD_OGF(buffer) (buffer[1] >> 2)
|
|
|
|
#define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0])
|
|
|
|
|
2009-09-29 20:13:43 +00:00
|
|
|
// check if command complete event for given command
|
2016-01-31 00:07:32 +01:00
|
|
|
#define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && little_endian_read_16(event,3) == cmd.opcode)
|
|
|
|
#define COMMAND_STATUS_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && little_endian_read_16(event,4) == cmd.opcode)
|
2011-11-26 19:43:08 +00:00
|
|
|
|
|
|
|
// Code+Len=2, Pkts+Opcode=3; total=5
|
|
|
|
#define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5
|
2009-09-29 20:13:43 +00:00
|
|
|
|
2009-07-29 21:06:04 +00:00
|
|
|
// ACL Packet
|
2016-01-31 00:07:32 +01:00
|
|
|
#define READ_ACL_CONNECTION_HANDLE( buffer ) ( little_endian_read_16(buffer,0) & 0x0fff)
|
2009-07-29 21:06:04 +00:00
|
|
|
#define READ_ACL_FLAGS( buffer ) ( buffer[1] >> 4 )
|
2016-01-31 00:07:32 +01:00
|
|
|
#define READ_ACL_LENGTH( buffer ) (little_endian_read_16(buffer, 2))
|
2009-07-29 21:06:04 +00:00
|
|
|
|
|
|
|
// L2CAP Packet
|
2016-01-31 00:07:32 +01:00
|
|
|
#define READ_L2CAP_LENGTH(buffer) ( little_endian_read_16(buffer, 4))
|
|
|
|
#define READ_L2CAP_CHANNEL_ID(buffer) ( little_endian_read_16(buffer, 6))
|
2009-07-29 21:06:04 +00:00
|
|
|
|
2016-01-31 00:07:32 +01:00
|
|
|
void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
|
|
|
|
void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
|
2009-07-23 21:43:37 +00:00
|
|
|
void bt_flip_addr(bd_addr_t dest, bd_addr_t src);
|
|
|
|
|
2016-01-31 00:07:32 +01:00
|
|
|
void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
|
|
|
|
void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
|
2010-06-09 15:30:49 +00:00
|
|
|
|
2015-04-03 22:20:11 +02:00
|
|
|
// hack: compilation with the android ndk causes an error as there's a swap64 macro
|
|
|
|
#ifdef swap64
|
|
|
|
#undef swap64
|
|
|
|
#endif
|
|
|
|
|
2016-01-29 17:26:18 +01:00
|
|
|
void swapX (const uint8_t *src, uint8_t * dst, int len);
|
|
|
|
void swap24 (const uint8_t *src, uint8_t * dst);
|
|
|
|
void swap48 (const uint8_t *src, uint8_t * dst);
|
|
|
|
void swap56 (const uint8_t *src, uint8_t * dst);
|
|
|
|
void swap64 (const uint8_t *src, uint8_t * dst);
|
|
|
|
void swap128(const uint8_t *src, uint8_t * dst);
|
2014-02-28 21:07:59 +00:00
|
|
|
|
2015-11-06 19:43:35 +01:00
|
|
|
char char_for_nibble(int nibble);
|
|
|
|
|
2014-08-15 22:04:48 +00:00
|
|
|
void printf_hexdump(const void *data, int size);
|
2014-08-15 21:26:50 +00:00
|
|
|
void hexdump(const void *data, int size);
|
2014-08-29 21:20:21 +00:00
|
|
|
void hexdumpf(const void *data, int size);
|
2015-03-31 16:15:55 +02:00
|
|
|
char * uuid128_to_str(uint8_t * uuid);
|
2014-02-28 21:45:33 +00:00
|
|
|
void printUUID128(uint8_t *uuid);
|
2014-08-15 21:26:50 +00:00
|
|
|
void log_key(const char * name, sm_key_t key);
|
2011-08-01 11:55:41 +00:00
|
|
|
|
|
|
|
// @deprecated please use more convenient bd_addr_to_str
|
|
|
|
void print_bd_addr( bd_addr_t addr);
|
2014-10-06 19:16:15 +00:00
|
|
|
|
2011-07-29 19:42:33 +00:00
|
|
|
char * bd_addr_to_str(bd_addr_t addr);
|
2014-10-06 19:16:15 +00:00
|
|
|
char * link_key_to_str(link_key_t link_key);
|
|
|
|
char *link_key_type_to_str(link_key_type_t link_key);
|
2011-08-01 11:55:41 +00:00
|
|
|
|
2015-11-13 15:04:41 +01:00
|
|
|
void sdp_normalize_uuid(uint8_t *uuid, uint32_t shortUUID);
|
|
|
|
int sdp_has_blueooth_base_uuid(uint8_t * uuid128);
|
|
|
|
|
2009-08-24 21:56:12 +00:00
|
|
|
int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr);
|
2014-10-16 13:02:20 +00:00
|
|
|
int sscan_link_key(char * addr_string, link_key_t link_key);
|
2014-10-06 19:16:15 +00:00
|
|
|
|
2009-09-24 19:36:52 +00:00
|
|
|
uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum);
|
|
|
|
uint8_t crc8_calc(uint8_t *data, uint16_t len);
|
|
|
|
|
2009-07-24 20:17:46 +00:00
|
|
|
#define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN)
|
2009-07-29 21:06:04 +00:00
|
|
|
#define BD_ADDR_COPY(dest,src) memcpy(dest,src,BD_ADDR_LEN)
|
2010-08-03 19:57:39 +00:00
|
|
|
|
2014-01-16 19:12:38 +00:00
|
|
|
int is_authenticated_link_key(link_key_type_t link_key_type);
|
|
|
|
|
2010-08-08 18:17:48 +00:00
|
|
|
#if defined __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-29 17:26:18 +01:00
|
|
|
#endif // __BTSTACK_UTIL_H
|