1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2000-2001 Qualcomm Incorporated 6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk (at) qualcomm.com> 7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel (at) holtmann.org> 8 * 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 * 24 */ 25 26 #ifndef __BLUETOOTH_H 27 #define __BLUETOOTH_H 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 #include <stdio.h> 34 #include <stdint.h> 35 #include <string.h> 36 #include <endian.h> 37 #include <byteswap.h> 38 #include <netinet/in.h> 39 40 #ifndef AF_BLUETOOTH 41 #define AF_BLUETOOTH 31 42 #define PF_BLUETOOTH AF_BLUETOOTH 43 #endif 44 45 #define BTPROTO_L2CAP 0 46 #define BTPROTO_HCI 1 47 #define BTPROTO_SCO 2 48 #define BTPROTO_RFCOMM 3 49 #define BTPROTO_BNEP 4 50 #define BTPROTO_CMTP 5 51 #define BTPROTO_HIDP 6 52 #define BTPROTO_AVDTP 7 53 54 #define SOL_HCI 0 55 #define SOL_L2CAP 6 56 #define SOL_SCO 17 57 #define SOL_RFCOMM 18 58 59 #ifndef SOL_BLUETOOTH 60 #define SOL_BLUETOOTH 274 61 #endif 62 63 #define BT_SECURITY 4 64 struct bt_security { 65 uint8_t level; 66 }; 67 #define BT_SECURITY_SDP 0 68 #define BT_SECURITY_LOW 1 69 #define BT_SECURITY_MEDIUM 2 70 #define BT_SECURITY_HIGH 3 71 72 #define BT_DEFER_SETUP 7 73 74 #define BT_FLUSHABLE 8 75 76 #define BT_FLUSHABLE_OFF 0 77 #define BT_FLUSHABLE_ON 1 78 79 #define BT_POWER 9 80 struct bt_power { 81 uint8_t force_active; 82 }; 83 84 /* Connection and socket states */ 85 enum { 86 BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ 87 BT_OPEN, 88 BT_BOUND, 89 BT_LISTEN, 90 BT_CONNECT, 91 BT_CONNECT2, 92 BT_CONFIG, 93 BT_DISCONN, 94 BT_CLOSED 95 }; 96 97 /* Byte order conversions */ 98 #if __BYTE_ORDER == __LITTLE_ENDIAN 99 #define htobs(d) (d) 100 #define htobl(d) (d) 101 #define btohs(d) (d) 102 #define btohl(d) (d) 103 #elif __BYTE_ORDER == __BIG_ENDIAN 104 #define htobs(d) bswap_16(d) 105 #define htobl(d) bswap_32(d) 106 #define btohs(d) bswap_16(d) 107 #define btohl(d) bswap_32(d) 108 #else 109 #error "Unknown byte order" 110 #endif 111 112 /* Bluetooth unaligned access */ 113 #define bt_get_unaligned(ptr) \ 114 ({ \ 115 struct __attribute__((packed)) { \ 116 typeof(*(ptr)) __v; \ 117 } *__p = (void *) (ptr); \ 118 __p->__v; \ 119 }) 120 121 #define bt_put_unaligned(val, ptr) \ 122 do { \ 123 struct __attribute__((packed)) { \ 124 typeof(*(ptr)) __v; \ 125 } *__p = (void *) (ptr); \ 126 __p->__v = (val); \ 127 } while(0) 128 129 /* BD Address */ 130 typedef struct { 131 uint8_t b[6]; 132 } __attribute__((packed)) bdaddr_t; 133 134 #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}}) 135 #define BDADDR_ALL (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}) 136 #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}}) 137 138 /* Copy, swap, convert BD Address */ 139 static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2) 140 { 141 return memcmp(ba1, ba2, sizeof(bdaddr_t)); 142 } 143 static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src) 144 { 145 memcpy(dst, src, sizeof(bdaddr_t)); 146 } 147 148 void baswap(bdaddr_t *dst, const bdaddr_t *src); 149 bdaddr_t *strtoba(const char *str); 150 char *batostr(const bdaddr_t *ba); 151 int ba2str(const bdaddr_t *ba, char *str); 152 int str2ba(const char *str, bdaddr_t *ba); 153 int ba2oui(const bdaddr_t *ba, char *oui); 154 int bachk(const char *str); 155 156 int baprintf(const char *format, ...); 157 int bafprintf(FILE *stream, const char *format, ...); 158 int basprintf(char *str, const char *format, ...); 159 int basnprintf(char *str, size_t size, const char *format, ...); 160 161 void *bt_malloc(size_t size); 162 void bt_free(void *ptr); 163 164 int bt_error(uint16_t code); 165 char *bt_compidtostr(int id); 166 167 typedef struct { 168 uint8_t data[16]; 169 } uint128_t; 170 171 #if __BYTE_ORDER == __BIG_ENDIAN 172 173 #define ntoh64(x) (x) 174 175 static inline void ntoh128(const uint128_t *src, uint128_t *dst) 176 { 177 memcpy(dst, src, sizeof(uint128_t)); 178 } 179 180 static inline void btoh128(const uint128_t *src, uint128_t *dst) 181 { 182 int i; 183 184 for (i = 0; i < 16; i++) 185 dst->data[15 - i] = src->data[i]; 186 } 187 188 #else 189 190 static inline uint64_t ntoh64(uint64_t n) 191 { 192 uint64_t h; 193 uint64_t tmp = ntohl(n & 0x00000000ffffffff); 194 195 h = ntohl(n >> 32); 196 h |= tmp << 32; 197 198 return h; 199 } 200 201 static inline void ntoh128(const uint128_t *src, uint128_t *dst) 202 { 203 int i; 204 205 for (i = 0; i < 16; i++) 206 dst->data[15 - i] = src->data[i]; 207 } 208 209 static inline void btoh128(const uint128_t *src, uint128_t *dst) 210 { 211 memcpy(dst, src, sizeof(uint128_t)); 212 } 213 214 #endif 215 216 #define hton64(x) ntoh64(x) 217 #define hton128(x, y) ntoh128(x, y) 218 #define htob128(x, y) btoh128(x, y) 219 220 #ifdef __cplusplus 221 } 222 #endif 223 224 #endif /* __BLUETOOTH_H */ 225