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