1 /****************************************************************************** 2 * 3 * Copyright 1999-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #ifndef BT_TYPES_H 20 #define BT_TYPES_H 21 22 #include <stdbool.h> 23 #include <stdint.h> 24 #ifdef __cplusplus 25 #include <string> 26 #endif // __cplusplus 27 28 #include "stack/include/bt_dev_class.h" 29 #include "stack/include/bt_device_type.h" 30 #include "stack/include/bt_hdr.h" 31 #include "stack/include/bt_name.h" 32 #include "stack/include/bt_octets.h" 33 #ifdef __cplusplus 34 #include "include/hardware/bluetooth.h" 35 #include "types/bluetooth/uuid.h" 36 #include "types/raw_address.h" 37 #endif // __cplusplus 38 39 /* READ WELL !! 40 * 41 * This section defines global events. These are events that cross layers. 42 * Any event that passes between layers MUST be one of these events. Tasks 43 * can use their own events internally, but a FUNDAMENTAL design issue is 44 * that global events MUST be one of these events defined below. 45 * 46 * The convention used is the the event name contains the layer that the 47 * event is going to. 48 */ 49 #define BT_EVT_MASK 0xFF00 50 #define BT_SUB_EVT_MASK 0x00FF 51 /* To Bluetooth Upper Layers */ 52 /************************************/ 53 /* HCI Event */ 54 #define BT_EVT_TO_BTU_HCI_EVT 0x1000 55 /* ACL Data from HCI */ 56 #define BT_EVT_TO_BTU_HCI_ACL 0x1100 57 /* SCO Data from HCI */ 58 #define BT_EVT_TO_BTU_HCI_SCO 0x1200 59 /* HCI Transport Error */ 60 #define BT_EVT_TO_BTU_HCIT_ERR 0x1300 61 62 /* Serial Port Data */ 63 #define BT_EVT_TO_BTU_SP_DATA 0x1500 64 65 /* HCI command from upper layer */ 66 #define BT_EVT_TO_BTU_HCI_CMD 0x1600 67 68 /* ISO Data from HCI */ 69 #define BT_EVT_TO_BTU_HCI_ISO 0x1700 70 71 /* L2CAP segment(s) transmitted */ 72 #define BT_EVT_TO_BTU_L2C_SEG_XMIT 0x1900 73 74 /* To LM */ 75 /************************************/ 76 /* HCI Command */ 77 #define BT_EVT_TO_LM_HCI_CMD 0x2000 78 /* HCI ACL Data */ 79 #define BT_EVT_TO_LM_HCI_ACL 0x2100 80 /* HCI SCO Data */ 81 #define BT_EVT_TO_LM_HCI_SCO 0x2200 82 /* HCI ISO Data */ 83 #define BT_EVT_TO_LM_HCI_ISO 0x2d00 84 85 #define BT_EVT_HCISU 0x5000 86 87 /* BTIF Events */ 88 #define BT_EVT_BTIF 0xA000 89 #define BT_EVT_CONTEXT_SWITCH_EVT (0x0001 | BT_EVT_BTIF) 90 91 /* ISO Layer specific */ 92 #define BT_ISO_HDR_CONTAINS_TS (0x0001) 93 #define BT_ISO_HDR_OFFSET_POINTS_DATA (0x0002) 94 95 enum { 96 BT_PSM_SDP = 0x0001, 97 BT_PSM_RFCOMM = 0x0003, 98 BT_PSM_TCS = 0x0005, 99 BT_PSM_CTP = 0x0007, 100 BT_PSM_BNEP = 0x000F, 101 BT_PSM_HIDC = 0x0011, 102 HID_PSM_CONTROL = 0x0011, 103 BT_PSM_HIDI = 0x0013, 104 HID_PSM_INTERRUPT = 0x0013, 105 BT_PSM_UPNP = 0x0015, 106 BT_PSM_AVCTP = 0x0017, 107 BT_PSM_AVDTP = 0x0019, 108 BT_PSM_AVCTP_13 = 0x001B, /* Advanced Control - Browsing */ 109 BT_PSM_UDI_CP = 110 0x001D, /* Unrestricted Digital Information Profile C-Plane */ 111 BT_PSM_ATT = 0x001F, /* Attribute Protocol */ 112 BT_PSM_EATT = 0x0027, 113 /* We will not allocate a PSM in the reserved range to 3rd party apps 114 */ 115 BRCM_RESERVED_PSM_START = 0x5AE1, 116 BRCM_RESERVED_PSM_END = 0x5AFF, 117 }; 118 119 /******************************************************************************* 120 * Macros to get and put bytes to and from a stream (Little Endian format). 121 */ 122 #define UINT64_TO_BE_STREAM(p, u64) \ 123 { \ 124 *(p)++ = (uint8_t)((u64) >> 56); \ 125 *(p)++ = (uint8_t)((u64) >> 48); \ 126 *(p)++ = (uint8_t)((u64) >> 40); \ 127 *(p)++ = (uint8_t)((u64) >> 32); \ 128 *(p)++ = (uint8_t)((u64) >> 24); \ 129 *(p)++ = (uint8_t)((u64) >> 16); \ 130 *(p)++ = (uint8_t)((u64) >> 8); \ 131 *(p)++ = (uint8_t)(u64); \ 132 } 133 #define UINT32_TO_STREAM(p, u32) \ 134 { \ 135 *(p)++ = (uint8_t)(u32); \ 136 *(p)++ = (uint8_t)((u32) >> 8); \ 137 *(p)++ = (uint8_t)((u32) >> 16); \ 138 *(p)++ = (uint8_t)((u32) >> 24); \ 139 } 140 #define UINT24_TO_STREAM(p, u24) \ 141 { \ 142 *(p)++ = (uint8_t)(u24); \ 143 *(p)++ = (uint8_t)((u24) >> 8); \ 144 *(p)++ = (uint8_t)((u24) >> 16); \ 145 } 146 #define UINT16_TO_STREAM(p, u16) \ 147 { \ 148 *(p)++ = (uint8_t)(u16); \ 149 *(p)++ = (uint8_t)((u16) >> 8); \ 150 } 151 #define UINT8_TO_STREAM(p, u8) \ 152 { *(p)++ = (uint8_t)(u8); } 153 #define INT8_TO_STREAM(p, u8) \ 154 { *(p)++ = (int8_t)(u8); } 155 #define ARRAY16_TO_STREAM(p, a) \ 156 { \ 157 int ijk; \ 158 for (ijk = 0; ijk < 16; ijk++) *(p)++ = (uint8_t)(a)[15 - ijk]; \ 159 } 160 #define ARRAY8_TO_STREAM(p, a) \ 161 { \ 162 int ijk; \ 163 for (ijk = 0; ijk < 8; ijk++) *(p)++ = (uint8_t)(a)[7 - ijk]; \ 164 } 165 #define LAP_TO_STREAM(p, a) \ 166 { \ 167 int ijk; \ 168 for (ijk = 0; ijk < LAP_LEN; ijk++) \ 169 *(p)++ = (uint8_t)(a)[LAP_LEN - 1 - ijk]; \ 170 } 171 #define ARRAY_TO_STREAM(p, a, len) \ 172 { \ 173 int ijk; \ 174 for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \ 175 } 176 #define STREAM_TO_INT8(u8, p) \ 177 { \ 178 (u8) = (*((int8_t*)(p))); \ 179 (p) += 1; \ 180 } 181 #define STREAM_TO_UINT8(u8, p) \ 182 { \ 183 (u8) = (uint8_t)(*(p)); \ 184 (p) += 1; \ 185 } 186 #define STREAM_TO_UINT16(u16, p) \ 187 { \ 188 (u16) = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); \ 189 (p) += 2; \ 190 } 191 #define STREAM_TO_UINT24(u32, p) \ 192 { \ 193 (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \ 194 ((((uint32_t)(*((p) + 2)))) << 16)); \ 195 (p) += 3; \ 196 } 197 #define STREAM_TO_UINT32(u32, p) \ 198 { \ 199 (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \ 200 ((((uint32_t)(*((p) + 2)))) << 16) + \ 201 ((((uint32_t)(*((p) + 3)))) << 24)); \ 202 (p) += 4; \ 203 } 204 #define STREAM_TO_UINT64(u64, p) \ 205 { \ 206 (u64) = (((uint64_t)(*(p))) + ((((uint64_t)(*((p) + 1)))) << 8) + \ 207 ((((uint64_t)(*((p) + 2)))) << 16) + \ 208 ((((uint64_t)(*((p) + 3)))) << 24) + \ 209 ((((uint64_t)(*((p) + 4)))) << 32) + \ 210 ((((uint64_t)(*((p) + 5)))) << 40) + \ 211 ((((uint64_t)(*((p) + 6)))) << 48) + \ 212 ((((uint64_t)(*((p) + 7)))) << 56)); \ 213 (p) += 8; \ 214 } 215 #define STREAM_TO_ARRAY16(a, p) \ 216 { \ 217 int ijk; \ 218 uint8_t* _pa = (uint8_t*)(a) + 15; \ 219 for (ijk = 0; ijk < 16; ijk++) *_pa-- = *(p)++; \ 220 } 221 #define STREAM_TO_ARRAY8(a, p) \ 222 { \ 223 int ijk; \ 224 uint8_t* _pa = (uint8_t*)(a) + 7; \ 225 for (ijk = 0; ijk < 8; ijk++) *_pa-- = *(p)++; \ 226 } 227 #define STREAM_TO_LAP(a, p) \ 228 { \ 229 int ijk; \ 230 uint8_t* plap = (uint8_t*)(a) + LAP_LEN - 1; \ 231 for (ijk = 0; ijk < LAP_LEN; ijk++) *plap-- = *(p)++; \ 232 } 233 #define STREAM_TO_ARRAY(a, p, len) \ 234 { \ 235 int ijk; \ 236 for (ijk = 0; ijk < (len); ijk++) ((uint8_t*)(a))[ijk] = *(p)++; \ 237 } 238 #define STREAM_SKIP_UINT8(p) \ 239 do { \ 240 (p) += 1; \ 241 } while (0) 242 #define STREAM_SKIP_UINT16(p) \ 243 do { \ 244 (p) += 2; \ 245 } while (0) 246 #define STREAM_SKIP_UINT32(p) \ 247 do { \ 248 (p) += 4; \ 249 } while (0) 250 251 /******************************************************************************* 252 * Macros to get and put bytes to and from a stream (Big Endian format) 253 */ 254 #define UINT32_TO_BE_STREAM(p, u32) \ 255 { \ 256 *(p)++ = (uint8_t)((u32) >> 24); \ 257 *(p)++ = (uint8_t)((u32) >> 16); \ 258 *(p)++ = (uint8_t)((u32) >> 8); \ 259 *(p)++ = (uint8_t)(u32); \ 260 } 261 #define UINT24_TO_BE_STREAM(p, u24) \ 262 { \ 263 *(p)++ = (uint8_t)((u24) >> 16); \ 264 *(p)++ = (uint8_t)((u24) >> 8); \ 265 *(p)++ = (uint8_t)(u24); \ 266 } 267 #define UINT16_TO_BE_STREAM(p, u16) \ 268 { \ 269 *(p)++ = (uint8_t)((u16) >> 8); \ 270 *(p)++ = (uint8_t)(u16); \ 271 } 272 #define UINT8_TO_BE_STREAM(p, u8) \ 273 { *(p)++ = (uint8_t)(u8); } 274 #define ARRAY_TO_BE_STREAM(p, a, len) \ 275 { \ 276 int ijk; \ 277 for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \ 278 } 279 #define BE_STREAM_TO_UINT8(u8, p) \ 280 { \ 281 (u8) = (uint8_t)(*(p)); \ 282 (p) += 1; \ 283 } 284 #define BE_STREAM_TO_UINT16(u16, p) \ 285 { \ 286 (u16) = (uint16_t)(((uint16_t)(*(p)) << 8) + (uint16_t)(*((p) + 1))); \ 287 (p) += 2; \ 288 } 289 #define BE_STREAM_TO_UINT24(u32, p) \ 290 { \ 291 (u32) = (((uint32_t)(*((p) + 2))) + ((uint32_t)(*((p) + 1)) << 8) + \ 292 ((uint32_t)(*(p)) << 16)); \ 293 (p) += 3; \ 294 } 295 #define BE_STREAM_TO_UINT32(u32, p) \ 296 { \ 297 (u32) = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) + \ 298 ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \ 299 (p) += 4; \ 300 } 301 #define BE_STREAM_TO_UINT64(u64, p) \ 302 { \ 303 (u64) = ((uint64_t)(*((p) + 7)) + ((uint64_t)(*((p) + 6)) << 8) + \ 304 ((uint64_t)(*((p) + 5)) << 16) + ((uint64_t)(*((p) + 4)) << 24) + \ 305 ((uint64_t)(*((p) + 3)) << 32) + ((uint64_t)(*((p) + 2)) << 40) + \ 306 ((uint64_t)(*((p) + 1)) << 48) + ((uint64_t)(*(p)) << 56)); \ 307 (p) += 8; \ 308 } 309 #define BE_STREAM_TO_ARRAY(p, a, len) \ 310 { \ 311 int ijk; \ 312 for (ijk = 0; ijk < (len); ijk++) ((uint8_t*)(a))[ijk] = *(p)++; \ 313 } 314 315 /******************************************************************************* 316 * Macros to get and put bytes to and from a field (Big Endian format). 317 * These are the same as to stream, except the pointer is not incremented. 318 */ 319 #define UINT32_TO_BE_FIELD(p, u32) \ 320 { \ 321 *(uint8_t*)(p) = (uint8_t)((u32) >> 24); \ 322 *((uint8_t*)(p) + 1) = (uint8_t)((u32) >> 16); \ 323 *((uint8_t*)(p) + 2) = (uint8_t)((u32) >> 8); \ 324 *((uint8_t*)(p) + 3) = (uint8_t)(u32); \ 325 } 326 #define UINT16_TO_BE_FIELD(p, u16) \ 327 { \ 328 *(uint8_t*)(p) = (uint8_t)((u16) >> 8); \ 329 *((uint8_t*)(p) + 1) = (uint8_t)(u16); \ 330 } 331 332 /* Common Bluetooth field definitions */ 333 334 #define LAP_LEN 3 335 typedef uint8_t LAP[LAP_LEN]; /* IAC as passed to Inquiry (LAP) */ 336 337 #define BT_1SEC_TIMEOUT_MS (1 * 1000) /* 1 second */ 338 339 #endif 340