1 /* 2 * Copyright (C) 2009-2012 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 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at [email protected] 34 * 35 */ 36 37 /* 38 * hci.h 39 * 40 * Created by Matthias Ringwald on 4/29/09. 41 * 42 */ 43 44 #ifndef __HCI_H 45 #define __HCI_H 46 47 #include "btstack-config.h" 48 49 #include <btstack/hci_cmds.h> 50 #include <btstack/utils.h> 51 #include "hci_transport.h" 52 #include "bt_control.h" 53 #include "remote_device_db.h" 54 55 #include <stdint.h> 56 #include <stdlib.h> 57 #include <stdarg.h> 58 59 #if defined __cplusplus 60 extern "C" { 61 #endif 62 63 // packet header sizes 64 #define HCI_CMD_HEADER_SIZE 3 65 #define HCI_ACL_HEADER_SIZE 4 66 #define HCI_SCO_HEADER_SIZE 3 67 #define HCI_EVENT_HEADER_SIZE 2 68 69 // packet sizes (max payload) 70 #define HCI_ACL_DM1_SIZE 17 71 #define HCI_ACL_DH1_SIZE 27 72 #define HCI_ACL_2DH1_SIZE 54 73 #define HCI_ACL_3DH1_SIZE 83 74 #define HCI_ACL_DM3_SIZE 121 75 #define HCI_ACL_DH3_SIZE 183 76 #define HCI_ACL_DM5_SIZE 224 77 #define HCI_ACL_DH5_SIZE 339 78 #define HCI_ACL_2DH3_SIZE 367 79 #define HCI_ACL_3DH3_SIZE 552 80 #define HCI_ACL_2DH5_SIZE 679 81 #define HCI_ACL_3DH5_SIZE 1021 82 83 #define HCI_EVENT_PAYLOAD_SIZE 255 84 #define HCI_CMD_PAYLOAD_SIZE 255 85 86 // packet buffer sizes 87 // HCI_ACL_PAYLOAD_SIZE is configurable and defined in config.h 88 #define HCI_EVENT_BUFFER_SIZE (HCI_EVENT_HEADER_SIZE + HCI_EVENT_PAYLOAD_SIZE) 89 #define HCI_CMD_BUFFER_SIZE (HCI_CMD_HEADER_SIZE + HCI_CMD_PAYLOAD_SIZE) 90 #define HCI_ACL_BUFFER_SIZE (HCI_ACL_HEADER_SIZE + HCI_ACL_PAYLOAD_SIZE) 91 92 // size of hci buffers, big enough for command, event, or acl packet without H4 packet type 93 // @note cmd buffer is bigger than event buffer 94 #ifdef HCI_PACKET_BUFFER_SIZE 95 #if HCI_PACKET_BUFFER_SIZE < HCI_ACL_BUFFER_SIZE 96 #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_ACL_BUFFER_SIZE 97 #endif 98 #if HCI_PACKET_BUFFER_SIZE < HCI_CMD_BUFFER_SIZE 99 #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_CMD_BUFFER_SIZE 100 #endif 101 #else 102 #if HCI_ACL_BUFFER_SIZE > HCI_CMD_BUFFER_SIZE 103 #define HCI_PACKET_BUFFER_SIZE HCI_ACL_BUFFER_SIZE 104 #else 105 #define HCI_PACKET_BUFFER_SIZE HCI_CMD_BUFFER_SIZE 106 #endif 107 #endif 108 109 // OGFs 110 #define OGF_LINK_CONTROL 0x01 111 #define OGF_LINK_POLICY 0x02 112 #define OGF_CONTROLLER_BASEBAND 0x03 113 #define OGF_INFORMATIONAL_PARAMETERS 0x04 114 #define OGF_STATUS_PARAMETERS 0x05 115 #define OGF_LE_CONTROLLER 0x08 116 #define OGF_BTSTACK 0x3d 117 #define OGF_VENDOR 0x3f 118 119 // cmds for BTstack 120 // get state: @returns HCI_STATE 121 #define BTSTACK_GET_STATE 0x01 122 123 // set power mode: @param HCI_POWER_MODE 124 #define BTSTACK_SET_POWER_MODE 0x02 125 126 // set capture mode: @param on 127 #define BTSTACK_SET_ACL_CAPTURE_MODE 0x03 128 129 // get BTstack version 130 #define BTSTACK_GET_VERSION 0x04 131 132 // get system Bluetooth state 133 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED 0x05 134 135 // set system Bluetooth state 136 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED 0x06 137 138 // enable inquiry scan for this client 139 #define BTSTACK_SET_DISCOVERABLE 0x07 140 141 // set global Bluetooth state 142 #define BTSTACK_SET_BLUETOOTH_ENABLED 0x08 143 144 // create l2cap channel: @param bd_addr(48), psm (16) 145 #define L2CAP_CREATE_CHANNEL 0x20 146 147 // disconnect l2cap disconnect, @param channel(16), reason(8) 148 #define L2CAP_DISCONNECT 0x21 149 150 // register l2cap service: @param psm(16), mtu (16) 151 #define L2CAP_REGISTER_SERVICE 0x22 152 153 // unregister l2cap disconnect, @param psm(16) 154 #define L2CAP_UNREGISTER_SERVICE 0x23 155 156 // accept connection @param bd_addr(48), dest cid (16) 157 #define L2CAP_ACCEPT_CONNECTION 0x24 158 159 // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8) 160 #define L2CAP_DECLINE_CONNECTION 0x25 161 162 // create l2cap channel: @param bd_addr(48), psm (16), mtu (16) 163 #define L2CAP_CREATE_CHANNEL_MTU 0x26 164 165 // register SDP Service Record: service record (size) 166 #define SDP_REGISTER_SERVICE_RECORD 0x30 167 168 // unregister SDP Service Record 169 #define SDP_UNREGISTER_SERVICE_RECORD 0x31 170 171 // Get remote RFCOMM services 172 #define SDP_CLIENT_QUERY_RFCOMM_SERVICES 0x32 173 174 // Get remote SDP services 175 #define SDP_CLIENT_QUERY_SERVICES 0x33 176 177 // RFCOMM "HCI" Commands 178 #define RFCOMM_CREATE_CHANNEL 0x40 179 #define RFCOMM_DISCONNECT 0x41 180 #define RFCOMM_REGISTER_SERVICE 0x42 181 #define RFCOMM_UNREGISTER_SERVICE 0x43 182 #define RFCOMM_ACCEPT_CONNECTION 0x44 183 #define RFCOMM_DECLINE_CONNECTION 0x45 184 #define RFCOMM_PERSISTENT_CHANNEL 0x46 185 #define RFCOMM_CREATE_CHANNEL_WITH_CREDITS 0x47 186 #define RFCOMM_REGISTER_SERVICE_WITH_CREDITS 0x48 187 #define RFCOMM_GRANT_CREDITS 0x49 188 189 // GAP Classic 0x50 190 #define GAP_DISCONNECT 0x50 191 192 // GAP LE 0x60 193 #define GAP_LE_SCAN_START 0x60 194 #define GAP_LE_SCAN_STOP 0x61 195 #define GAP_LE_CONNECT 0x62 196 #define GAP_LE_CONNECT_CANCEL 0x63 197 #define GAP_LE_SET_SCAN_PARAMETERS 0x64 198 199 // GATT (Client) 0x70 200 #define GATT_DISCOVER_ALL_PRIMARY_SERVICES 0x70 201 #define GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID16 0x71 202 #define GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID128 0x72 203 #define GATT_FIND_INCLUDED_SERVICES_FOR_SERVICE 0x73 204 #define GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE 0x74 205 #define GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID128 0x75 206 #define GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS 0x76 207 #define GATT_READ_VALUE_OF_CHARACTERISTIC 0x77 208 #define GATT_READ_LONG_VALUE_OF_CHARACTERISTIC 0x78 209 #define GATT_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE 0x79 210 #define GATT_WRITE_VALUE_OF_CHARACTERISTIC 0x7A 211 #define GATT_WRITE_LONG_VALUE_OF_CHARACTERISTIC 0x7B 212 #define GATT_RELIABLE_WRITE_LONG_VALUE_OF_CHARACTERISTIC 0x7C 213 #define GATT_READ_CHARACTERISTIC_DESCRIPTOR 0X7D 214 #define GATT_READ_LONG_CHARACTERISTIC_DESCRIPTOR 0X7E 215 #define GATT_WRITE_CHARACTERISTIC_DESCRIPTOR 0X7F 216 #define GATT_WRITE_LONG_CHARACTERISTIC_DESCRIPTOR 0X80 217 #define GATT_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION 0X81 218 219 // 220 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode) 221 222 // data: event(8) 223 #define DAEMON_EVENT_CONNECTION_OPENED 0x50 224 225 // data: event(8) 226 #define DAEMON_EVENT_CONNECTION_CLOSED 0x51 227 228 // data: event(8), nr_connections(8) 229 #define DAEMON_NR_CONNECTIONS_CHANGED 0x52 230 231 // data: event(8) 232 #define DAEMON_EVENT_NEW_RFCOMM_CREDITS 0x53 233 234 // data: event() 235 #define DAEMON_EVENT_HCI_PACKET_SENT 0x54 236 237 /** 238 * Connection State 239 */ 240 typedef enum { 241 AUTH_FLAGS_NONE = 0x0000, 242 RECV_LINK_KEY_REQUEST = 0x0001, 243 HANDLE_LINK_KEY_REQUEST = 0x0002, 244 SENT_LINK_KEY_REPLY = 0x0004, 245 SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008, 246 RECV_LINK_KEY_NOTIFICATION = 0x0010, 247 DENY_PIN_CODE_REQUEST = 0x0040, 248 RECV_IO_CAPABILITIES_REQUEST = 0x0080, 249 SEND_IO_CAPABILITIES_REPLY = 0x0100, 250 SEND_USER_CONFIRM_REPLY = 0x0200, 251 SEND_USER_PASSKEY_REPLY = 0x0400, 252 253 // pairing status 254 LEGACY_PAIRING_ACTIVE = 0x2000, 255 SSP_PAIRING_ACTIVE = 0x4000, 256 257 // connection status 258 CONNECTION_ENCRYPTED = 0x8000, 259 } hci_authentication_flags_t; 260 261 typedef enum { 262 SEND_CREATE_CONNECTION = 0, 263 SENT_CREATE_CONNECTION, 264 SEND_CANCEL_CONNECTION, 265 SENT_CANCEL_CONNECTION, 266 RECEIVED_CONNECTION_REQUEST, 267 ACCEPTED_CONNECTION_REQUEST, 268 REJECTED_CONNECTION_REQUEST, 269 OPEN, 270 SEND_DISCONNECT, 271 SENT_DISCONNECT 272 } CONNECTION_STATE; 273 274 typedef enum { 275 BONDING_REQUEST_REMOTE_FEATURES = 0x01, 276 BONDING_RECEIVED_REMOTE_FEATURES = 0x02, 277 BONDING_REMOTE_SUPPORTS_SSP = 0x04, 278 BONDING_DISCONNECT_SECURITY_BLOCK = 0x08, 279 BONDING_DISCONNECT_DEDICATED_DONE = 0x10, 280 BONDING_SEND_AUTHENTICATE_REQUEST = 0x20, 281 BONDING_SEND_ENCRYPTION_REQUEST = 0x40, 282 BONDING_DEDICATED = 0x80, 283 BONDING_EMIT_COMPLETE_ON_DISCONNECT = 0x100 284 } bonding_flags_t; 285 286 typedef enum { 287 BLUETOOTH_OFF = 1, 288 BLUETOOTH_ON, 289 BLUETOOTH_ACTIVE 290 } BLUETOOTH_STATE; 291 292 // le central scanning state 293 typedef enum { 294 LE_SCAN_IDLE, 295 LE_START_SCAN, 296 LE_SCANNING, 297 LE_STOP_SCAN, 298 } le_scanning_state_t; 299 300 301 typedef struct { 302 // linked list - assert: first field 303 linked_item_t item; 304 305 // remote side 306 bd_addr_t address; 307 308 // module handle 309 hci_con_handle_t con_handle; 310 311 // le public, le random, classic 312 bd_addr_type_t address_type; 313 314 // connection state 315 CONNECTION_STATE state; 316 317 // bonding 318 uint16_t bonding_flags; 319 uint8_t bonding_status; 320 // requested security level 321 gap_security_level_t requested_security_level; 322 323 // 324 link_key_type_t link_key_type; 325 326 // errands 327 uint32_t authentication_flags; 328 329 timer_source_t timeout; 330 331 #ifdef HAVE_TIME 332 // timer 333 struct timeval timestamp; 334 #endif 335 #ifdef HAVE_TICK 336 uint32_t timestamp; // timeout in system ticks 337 #endif 338 339 // ACL packet recombination - ACL Header + ACL payload 340 uint8_t acl_recombination_buffer[4 + HCI_ACL_BUFFER_SIZE]; 341 uint16_t acl_recombination_pos; 342 uint16_t acl_recombination_length; 343 344 // number ACL packets sent to controller 345 uint8_t num_acl_packets_sent; 346 } hci_connection_t; 347 348 349 /** 350 * main data structure 351 */ 352 typedef struct { 353 // transport component with configuration 354 hci_transport_t * hci_transport; 355 void * config; 356 357 // bsic configuration 358 const char * local_name; 359 uint32_t class_of_device; 360 bd_addr_t local_bd_addr; 361 uint8_t ssp_enable; 362 uint8_t ssp_io_capability; 363 uint8_t ssp_authentication_requirement; 364 uint8_t ssp_auto_accept; 365 366 // hardware power controller 367 bt_control_t * control; 368 369 // list of existing baseband connections 370 linked_list_t connections; 371 372 // single buffer for HCI Command assembly 373 uint8_t hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8) 374 uint8_t hci_packet_buffer_reserved; 375 376 /* host to controller flow control */ 377 uint8_t num_cmd_packets; 378 // uint8_t total_num_cmd_packets; 379 uint8_t acl_packets_total_num; 380 uint16_t acl_data_packet_length; 381 uint8_t le_acl_packets_total_num; 382 uint16_t le_data_packets_length; 383 384 /* local supported features */ 385 uint8_t local_supported_features[8]; 386 387 // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE 388 uint16_t packet_types; 389 390 /* callback to L2CAP layer */ 391 void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 392 393 /* remote device db */ 394 remote_device_db_t const*remote_device_db; 395 396 /* hci state machine */ 397 HCI_STATE state; 398 uint8_t substate; 399 uint8_t cmds_ready; 400 401 uint16_t last_cmd_opcode; 402 403 uint8_t discoverable; 404 uint8_t connectable; 405 uint8_t bondable; 406 407 /* buffer for scan enable cmd - 0xff no change */ 408 uint8_t new_scan_enable_value; 409 410 // buffer for single connection decline 411 uint8_t decline_reason; 412 bd_addr_t decline_addr; 413 414 uint8_t adv_addr_type; 415 bd_addr_t adv_address; 416 417 le_scanning_state_t le_scanning_state; 418 419 // buffer for le scan type command - 0xff not set 420 uint8_t le_scan_type; 421 uint16_t le_scan_interval; 422 uint16_t le_scan_window; 423 } hci_stack_t; 424 425 //*************** le client start 426 427 le_command_status_t le_central_start_scan(); 428 le_command_status_t le_central_stop_scan(); 429 le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type); 430 le_command_status_t le_central_connect_cancel(); 431 le_command_status_t gap_disconnect(hci_con_handle_t handle); 432 void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window); 433 434 //*************** le client end 435 436 // create and send hci command packets based on a template and a list of parameters 437 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); 438 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr); 439 440 void hci_connectable_control(uint8_t enable); 441 void hci_close(void); 442 443 /** 444 * run the hci control loop once 445 */ 446 void hci_run(void); 447 448 // send complete CMD packet 449 int hci_send_cmd_packet(uint8_t *packet, int size); 450 451 // send ACL packet prepared in hci packet buffer 452 int hci_send_acl_packet_buffer(int size); 453 454 // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 455 int hci_can_send_command_packet_now(void); 456 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle); 457 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle); 458 459 // non-blocking UART driver needs 460 // @deprecated use hci_can_send_X_now instead 461 int hci_can_send_packet_now(uint8_t packet_type); 462 463 // same as hci_can_send_packet_now, but also checks if packet buffer is free for use 464 // @deprecated use hci_can_send_X_now instead 465 int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type); 466 467 // reserves outgoing packet buffer. @returns 1 if successful 468 int hci_reserve_packet_buffer(void); 469 void hci_release_packet_buffer(void); 470 471 // used for internal checks in l2cap[-le].c 472 int hci_is_packet_buffer_reserved(void); 473 474 // get point to packet buffer 475 uint8_t* hci_get_outgoing_packet_buffer(void); 476 477 bd_addr_t * hci_local_bd_addr(void); 478 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle); 479 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t *addr, bd_addr_type_t addr_type); 480 int hci_is_le_connection(hci_connection_t * connection); 481 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle); 482 uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle); 483 int hci_authentication_active_for_handle(hci_con_handle_t handle); 484 uint16_t hci_max_acl_data_packet_length(void); 485 uint16_t hci_usable_acl_packet_types(void); 486 int hci_non_flushable_packet_boundary_flag_supported(void); 487 488 void hci_disconnect_all(); 489 490 void hci_emit_state(void); 491 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status); 492 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 493 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason); 494 void hci_emit_nr_connections_changed(void); 495 void hci_emit_hci_open_failed(void); 496 void hci_emit_btstack_version(void); 497 void hci_emit_system_bluetooth_enabled(uint8_t enabled); 498 void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name); 499 void hci_emit_discoverable_enabled(uint8_t enabled); 500 void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 501 void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 502 503 // query if remote side supports SSP 504 // query if the local side supports SSP 505 int hci_local_ssp_activated(); 506 507 // query if the remote side supports SSP 508 int hci_remote_ssp_supported(hci_con_handle_t con_handle); 509 510 // query if both sides support SSP 511 int hci_ssp_supported_on_both_sides(hci_con_handle_t handle); 512 513 // disable automatic l2cap disconnect for testing 514 void hci_disable_l2cap_timeout_check(); 515 516 // disconnect because of security block 517 void hci_disconnect_security_block(hci_con_handle_t con_handle); 518 519 /** Embedded API **/ 520 521 // Set up HCI. Needs to be called before any other function 522 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db); 523 524 // Set class of device that will be set during Bluetooth init 525 void hci_set_class_of_device(uint32_t class_of_device); 526 527 // Registers a packet handler. Used if L2CAP is not used (rarely). 528 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 529 530 // Requests the change of BTstack power mode. 531 int hci_power_control(HCI_POWER_MODE mode); 532 533 // Allows to control if device is discoverable. OFF by default. 534 void hci_discoverable_control(uint8_t enable); 535 536 // Creates and sends HCI command packets based on a template and 537 // a list of parameters. Will return error if outgoing data buffer 538 // is occupied. 539 int hci_send_cmd(const hci_cmd_t *cmd, ...); 540 541 // Deletes link key for remote device with baseband address. 542 void hci_drop_link_key_for_bd_addr(bd_addr_t *addr); 543 544 // Configure Secure Simple Pairing 545 546 // enable will enable SSP during init 547 void hci_ssp_set_enable(int enable); 548 549 // if set, BTstack will respond to io capability request using authentication requirement 550 void hci_ssp_set_io_capability(int ssp_io_capability); 551 void hci_ssp_set_authentication_requirement(int authentication_requirement); 552 553 // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 554 void hci_ssp_set_auto_accept(int auto_accept); 555 556 // get addr type and address used in advertisement packets 557 void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr); 558 559 560 #if defined __cplusplus 561 } 562 #endif 563 564 #endif // __HCI_H 565