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_LE_CONTROLLER 0x08 115 #define OGF_BTSTACK 0x3d 116 #define OGF_VENDOR 0x3f 117 118 // cmds for BTstack 119 // get state: @returns HCI_STATE 120 #define BTSTACK_GET_STATE 0x01 121 122 // set power mode: @param HCI_POWER_MODE 123 #define BTSTACK_SET_POWER_MODE 0x02 124 125 // set capture mode: @param on 126 #define BTSTACK_SET_ACL_CAPTURE_MODE 0x03 127 128 // get BTstack version 129 #define BTSTACK_GET_VERSION 0x04 130 131 // get system Bluetooth state 132 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED 0x05 133 134 // set system Bluetooth state 135 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED 0x06 136 137 // enable inquiry scan for this client 138 #define BTSTACK_SET_DISCOVERABLE 0x07 139 140 // set global Bluetooth state 141 #define BTSTACK_SET_BLUETOOTH_ENABLED 0x08 142 143 // create l2cap channel: @param bd_addr(48), psm (16) 144 #define L2CAP_CREATE_CHANNEL 0x20 145 146 // disconnect l2cap disconnect, @param channel(16), reason(8) 147 #define L2CAP_DISCONNECT 0x21 148 149 // register l2cap service: @param psm(16), mtu (16) 150 #define L2CAP_REGISTER_SERVICE 0x22 151 152 // unregister l2cap disconnect, @param psm(16) 153 #define L2CAP_UNREGISTER_SERVICE 0x23 154 155 // accept connection @param bd_addr(48), dest cid (16) 156 #define L2CAP_ACCEPT_CONNECTION 0x24 157 158 // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8) 159 #define L2CAP_DECLINE_CONNECTION 0x25 160 161 // create l2cap channel: @param bd_addr(48), psm (16), mtu (16) 162 #define L2CAP_CREATE_CHANNEL_MTU 0x26 163 164 // register SDP Service Record: service record (size) 165 #define SDP_REGISTER_SERVICE_RECORD 0x30 166 167 // unregister SDP Service Record 168 #define SDP_UNREGISTER_SERVICE_RECORD 0x31 169 170 // Get remote RFCOMM services 171 #define SDP_CLIENT_QUERY_RFCOMM_SERVICES 0x32 172 173 // Get remote SDP services 174 #define SDP_CLIENT_QUERY_SERVICES 0x33 175 176 // RFCOMM "HCI" Commands 177 #define RFCOMM_CREATE_CHANNEL 0x40 178 #define RFCOMM_DISCONNECT 0x41 179 #define RFCOMM_REGISTER_SERVICE 0x42 180 #define RFCOMM_UNREGISTER_SERVICE 0x43 181 #define RFCOMM_ACCEPT_CONNECTION 0x44 182 #define RFCOMM_DECLINE_CONNECTION 0x45 183 #define RFCOMM_PERSISTENT_CHANNEL 0x46 184 #define RFCOMM_CREATE_CHANNEL_WITH_CREDITS 0x47 185 #define RFCOMM_REGISTER_SERVICE_WITH_CREDITS 0x48 186 #define RFCOMM_GRANT_CREDITS 0x49 187 188 // 189 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode) 190 191 // data: event(8) 192 #define DAEMON_EVENT_CONNECTION_OPENED 0x50 193 194 // data: event(8) 195 #define DAEMON_EVENT_CONNECTION_CLOSED 0x51 196 197 // data: event(8), nr_connections(8) 198 #define DAEMON_NR_CONNECTIONS_CHANGED 0x52 199 200 // data: event(8) 201 #define DAEMON_EVENT_NEW_RFCOMM_CREDITS 0x53 202 203 // data: event() 204 #define DAEMON_EVENT_HCI_PACKET_SENT 0x54 205 206 /** 207 * Connection State 208 */ 209 typedef enum { 210 AUTH_FLAGS_NONE = 0x0000, 211 RECV_LINK_KEY_REQUEST = 0x0001, 212 HANDLE_LINK_KEY_REQUEST = 0x0002, 213 SENT_LINK_KEY_REPLY = 0x0004, 214 SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008, 215 RECV_LINK_KEY_NOTIFICATION = 0x0010, 216 DENY_PIN_CODE_REQUEST = 0x0040, 217 RECV_IO_CAPABILITIES_REQUEST = 0x0080, 218 SEND_IO_CAPABILITIES_REPLY = 0x0100, 219 SEND_USER_CONFIRM_REPLY = 0x0200, 220 SEND_USER_PASSKEY_REPLY = 0x0400, 221 222 // pairing status 223 LEGACY_PAIRING_ACTIVE = 0x2000, 224 SSP_PAIRING_ACTIVE = 0x4000, 225 226 // connection status 227 CONNECTION_ENCRYPTED = 0x8000, 228 } hci_authentication_flags_t; 229 230 typedef enum { 231 SEND_CREATE_CONNECTION = 0, 232 SENT_CREATE_CONNECTION, 233 RECEIVED_CONNECTION_REQUEST, 234 ACCEPTED_CONNECTION_REQUEST, 235 REJECTED_CONNECTION_REQUEST, 236 OPEN, 237 SENT_DISCONNECT 238 } CONNECTION_STATE; 239 240 typedef enum { 241 BONDING_REQUEST_REMOTE_FEATURES = 0x01, 242 BONDING_RECEIVED_REMOTE_FEATURES = 0x02, 243 BONDING_REMOTE_SUPPORTS_SSP = 0x04, 244 BONDING_DISCONNECT_SECURITY_BLOCK = 0x08, 245 BONDING_DISCONNECT_DEDICATED_DONE = 0x10, 246 BONDING_SEND_AUTHENTICATE_REQUEST = 0x20, 247 BONDING_SEND_ENCRYPTION_REQUEST = 0x40, 248 BONDING_DEDICATED = 0x80, 249 } bonding_flags_t; 250 251 typedef enum { 252 BLUETOOTH_OFF = 1, 253 BLUETOOTH_ON, 254 BLUETOOTH_ACTIVE 255 } BLUETOOTH_STATE; 256 257 // le central scanning state 258 typedef enum { 259 LE_SCAN_IDLE, 260 LE_START_SCAN, 261 LE_SCANNING, 262 LE_STOP_SCAN, 263 } le_scanning_state_t; 264 265 typedef struct { 266 // linked list - assert: first field 267 linked_item_t item; 268 269 // remote side 270 bd_addr_t address; 271 272 // module handle 273 hci_con_handle_t con_handle; 274 275 // le public, le random, classic 276 bd_addr_type_t address_type; 277 278 // connection state 279 CONNECTION_STATE state; 280 281 // bonding 282 uint16_t bonding_flags; 283 284 // requested security level 285 gap_security_level_t requested_security_level; 286 287 // 288 link_key_type_t link_key_type; 289 290 // errands 291 uint32_t authentication_flags; 292 293 timer_source_t timeout; 294 295 #ifdef HAVE_TIME 296 // timer 297 struct timeval timestamp; 298 #endif 299 #ifdef HAVE_TICK 300 uint32_t timestamp; // timeout in system ticks 301 #endif 302 303 // ACL packet recombination - ACL Header + ACL payload 304 uint8_t acl_recombination_buffer[4 + HCI_ACL_BUFFER_SIZE]; 305 uint16_t acl_recombination_pos; 306 uint16_t acl_recombination_length; 307 308 // number ACL packets sent to controller 309 uint8_t num_acl_packets_sent; 310 311 } hci_connection_t; 312 313 /** 314 * main data structure 315 */ 316 typedef struct { 317 // transport component with configuration 318 hci_transport_t * hci_transport; 319 void * config; 320 321 // bsic configuration 322 const char * local_name; 323 uint32_t class_of_device; 324 bd_addr_t local_bd_addr; 325 uint8_t ssp_enable; 326 uint8_t ssp_io_capability; 327 uint8_t ssp_authentication_requirement; 328 uint8_t ssp_auto_accept; 329 330 // hardware power controller 331 bt_control_t * control; 332 333 // list of existing baseband connections 334 linked_list_t connections; 335 336 // single buffer for HCI Command assembly 337 uint8_t hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8) 338 uint8_t hci_packet_buffer_reserved; 339 340 /* host to controller flow control */ 341 uint8_t num_cmd_packets; 342 // uint8_t total_num_cmd_packets; 343 uint8_t total_num_acl_packets; 344 uint16_t acl_data_packet_length; 345 uint8_t total_num_le_packets; 346 uint16_t le_data_packet_length; 347 348 /* local supported features */ 349 uint8_t local_supported_features[8]; 350 351 // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE 352 uint16_t packet_types; 353 354 /* callback to L2CAP layer */ 355 void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 356 357 /* remote device db */ 358 remote_device_db_t const*remote_device_db; 359 360 /* hci state machine */ 361 HCI_STATE state; 362 uint8_t substate; 363 uint8_t cmds_ready; 364 365 uint8_t discoverable; 366 uint8_t connectable; 367 uint8_t bondable; 368 369 /* buffer for scan enable cmd - 0xff no change */ 370 uint8_t new_scan_enable_value; 371 372 // buffer for single connection decline 373 uint8_t decline_reason; 374 bd_addr_t decline_addr; 375 376 uint8_t adv_addr_type; 377 bd_addr_t adv_address; 378 le_scanning_state_t le_scanning_state; 379 } hci_stack_t; 380 381 //*************** le client start 382 383 typedef struct le_event { 384 uint8_t type; 385 } le_event_t; 386 387 typedef enum { 388 BLE_PERIPHERAL_OK = 0, 389 BLE_PERIPHERAL_IN_WRONG_STATE, 390 BLE_PERIPHERAL_DIFFERENT_CONTEXT_FOR_ADDRESS_ALREADY_EXISTS, 391 BLE_PERIPHERAL_NOT_CONNECTED, 392 BLE_VALUE_TOO_LONG, 393 BLE_PERIPHERAL_BUSY, 394 BLE_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED, 395 BLE_CHARACTERISTIC_INDICATION_NOT_SUPPORTED 396 } le_command_status_t; 397 398 399 typedef struct ad_event { 400 uint8_t type; 401 uint8_t event_type; 402 uint8_t address_type; 403 bd_addr_t address; 404 uint8_t rssi; 405 uint8_t length; 406 uint8_t * data; 407 } ad_event_t; 408 409 void le_central_register_handler(void (*le_callback)(le_event_t* event)); 410 le_command_status_t le_central_start_scan(); 411 le_command_status_t le_central_stop_scan(); 412 413 //*************** le client end 414 415 // create and send hci command packets based on a template and a list of parameters 416 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); 417 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr); 418 419 void hci_connectable_control(uint8_t enable); 420 void hci_close(void); 421 422 /** 423 * run the hci control loop once 424 */ 425 void hci_run(void); 426 427 // send complete CMD packet 428 int hci_send_cmd_packet(uint8_t *packet, int size); 429 430 // send ACL packet 431 int hci_send_acl_packet(uint8_t *packet, int size); 432 433 // non-blocking UART driver needs 434 int hci_can_send_packet_now(uint8_t packet_type); 435 436 // same as hci_can_send_packet_now, but also checks if packet buffer is free for use 437 int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type); 438 439 // reserves outgoing packet buffer. @returns 1 if successful 440 int hci_reserve_packet_buffer(void); 441 void hci_release_packet_buffer(void); 442 443 // used for internal checks in l2cap[-le].c 444 int hci_is_packet_buffer_reserved(void); 445 446 // get point to packet buffer 447 uint8_t* hci_get_outgoing_packet_buffer(void); 448 449 bd_addr_t * hci_local_bd_addr(void); 450 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle); 451 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t *addr, bd_addr_type_t addr_type); 452 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle); 453 uint8_t hci_number_free_acl_slots(void); 454 int hci_authentication_active_for_handle(hci_con_handle_t handle); 455 uint16_t hci_max_acl_data_packet_length(void); 456 uint16_t hci_usable_acl_packet_types(void); 457 458 // 459 void hci_emit_state(void); 460 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status); 461 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 462 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason); 463 void hci_emit_nr_connections_changed(void); 464 void hci_emit_hci_open_failed(void); 465 void hci_emit_btstack_version(void); 466 void hci_emit_system_bluetooth_enabled(uint8_t enabled); 467 void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name); 468 void hci_emit_discoverable_enabled(uint8_t enabled); 469 void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 470 void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status); 471 472 // query if remote side supports SSP 473 // query if the local side supports SSP 474 int hci_local_ssp_activated(); 475 476 // query if the remote side supports SSP 477 int hci_remote_ssp_supported(hci_con_handle_t con_handle); 478 479 // query if both sides support SSP 480 int hci_ssp_supported_on_both_sides(hci_con_handle_t handle); 481 482 // disable automatic l2cap disconnect for testing 483 void hci_disable_l2cap_timeout_check(); 484 485 // disconnect because of security block 486 void hci_disconnect_security_block(hci_con_handle_t con_handle); 487 488 /** Embedded API **/ 489 490 // Set up HCI. Needs to be called before any other function 491 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db); 492 493 // Set class of device that will be set during Bluetooth init 494 void hci_set_class_of_device(uint32_t class_of_device); 495 496 // Registers a packet handler. Used if L2CAP is not used (rarely). 497 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 498 499 // Requests the change of BTstack power mode. 500 int hci_power_control(HCI_POWER_MODE mode); 501 502 // Allows to control if device is discoverable. OFF by default. 503 void hci_discoverable_control(uint8_t enable); 504 505 // Creates and sends HCI command packets based on a template and 506 // a list of parameters. Will return error if outgoing data buffer 507 // is occupied. 508 int hci_send_cmd(const hci_cmd_t *cmd, ...); 509 510 // Deletes link key for remote device with baseband address. 511 void hci_drop_link_key_for_bd_addr(bd_addr_t *addr); 512 513 // Configure Secure Simple Pairing 514 515 // enable will enable SSP during init 516 void hci_ssp_set_enable(int enable); 517 518 // if set, BTstack will respond to io capability request using authentication requirement 519 void hci_ssp_set_io_capability(int ssp_io_capability); 520 void hci_ssp_set_authentication_requirement(int authentication_requirement); 521 522 // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 523 void hci_ssp_set_auto_accept(int auto_accept); 524 525 // get addr type and address used in advertisement packets 526 void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr); 527 528 529 #if defined __cplusplus 530 } 531 #endif 532 533 #endif // __HCI_H 534