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 #pragma once 45 46 #include "config.h" 47 48 #include <btstack/hci_cmds.h> 49 #include <btstack/utils.h> 50 #include "hci_transport.h" 51 #include "bt_control.h" 52 #include "remote_device_db.h" 53 54 #include <stdint.h> 55 #include <stdlib.h> 56 #include <stdarg.h> 57 58 #if defined __cplusplus 59 extern "C" { 60 #endif 61 62 // packet header sizes 63 #define HCI_CMD_HEADER_SIZE 3 64 #define HCI_ACL_HEADER_SIZE 4 65 #define HCI_SCO_HEADER_SIZE 3 66 #define HCI_EVENT_HEADER_SIZE 2 67 68 // packet sizes (max payload) 69 #define HCI_ACL_DM1_SIZE 17 70 #define HCI_ACL_DH1_SIZE 27 71 #define HCI_ACL_2DH1_SIZE 54 72 #define HCI_ACL_3DH1_SIZE 83 73 #define HCI_ACL_DM3_SIZE 121 74 #define HCI_ACL_DH3_SIZE 183 75 #define HCI_ACL_DM5_SIZE 224 76 #define HCI_ACL_DH5_SIZE 339 77 #define HCI_ACL_2DH3_SIZE 367 78 #define HCI_ACL_3DH3_SIZE 552 79 #define HCI_ACL_2DH5_SIZE 679 80 #define HCI_ACL_3DH5_SIZE 1021 81 82 #define HCI_EVENT_PAYLOAD_SIZE 255 83 #define HCI_CMD_PAYLOAD_SIZE 255 84 85 // packet buffer sizes 86 // HCI_ACL_PAYLOAD_SIZE is configurable and defined in config.h 87 #define HCI_EVENT_BUFFER_SIZE (HCI_EVENT_HEADER_SIZE + HCI_EVENT_PAYLOAD_SIZE) 88 #define HCI_CMD_BUFFER_SIZE (HCI_CMD_HEADER_SIZE + HCI_CMD_PAYLOAD_SIZE) 89 #define HCI_ACL_BUFFER_SIZE (HCI_ACL_HEADER_SIZE + HCI_ACL_PAYLOAD_SIZE) 90 91 // size of hci buffers, big enough for command, event, or acl packet without H4 packet type 92 // @note cmd buffer is bigger than event buffer 93 #ifndef HCI_PACKET_BUFFER_SIZE 94 #if HCI_ACL_BUFFER_SIZE > HCI_CMD_BUFFER_SIZE 95 #define HCI_PACKET_BUFFER_SIZE HCI_ACL_BUFFER_SIZE 96 #else 97 #define HCI_PACKET_BUFFER_SIZE HCI_CMD_BUFFER_SIZE 98 #endif 99 #endif 100 101 // OGFs 102 #define OGF_LINK_CONTROL 0x01 103 #define OGF_LINK_POLICY 0x02 104 #define OGF_CONTROLLER_BASEBAND 0x03 105 #define OGF_INFORMATIONAL_PARAMETERS 0x04 106 #define OGF_LE_CONTROLLER 0x08 107 #define OGF_BTSTACK 0x3d 108 #define OGF_VENDOR 0x3f 109 110 // cmds for BTstack 111 // get state: @returns HCI_STATE 112 #define BTSTACK_GET_STATE 0x01 113 114 // set power mode: @param HCI_POWER_MODE 115 #define BTSTACK_SET_POWER_MODE 0x02 116 117 // set capture mode: @param on 118 #define BTSTACK_SET_ACL_CAPTURE_MODE 0x03 119 120 // get BTstack version 121 #define BTSTACK_GET_VERSION 0x04 122 123 // get system Bluetooth state 124 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED 0x05 125 126 // set system Bluetooth state 127 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED 0x06 128 129 // enable inquiry scan for this client 130 #define BTSTACK_SET_DISCOVERABLE 0x07 131 132 // set global Bluetooth state 133 #define BTSTACK_SET_BLUETOOTH_ENABLED 0x08 134 135 // create l2cap channel: @param bd_addr(48), psm (16) 136 #define L2CAP_CREATE_CHANNEL 0x20 137 138 // disconnect l2cap disconnect, @param channel(16), reason(8) 139 #define L2CAP_DISCONNECT 0x21 140 141 // register l2cap service: @param psm(16), mtu (16) 142 #define L2CAP_REGISTER_SERVICE 0x22 143 144 // unregister l2cap disconnect, @param psm(16) 145 #define L2CAP_UNREGISTER_SERVICE 0x23 146 147 // accept connection @param bd_addr(48), dest cid (16) 148 #define L2CAP_ACCEPT_CONNECTION 0x24 149 150 // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8) 151 #define L2CAP_DECLINE_CONNECTION 0x25 152 153 // create l2cap channel: @param bd_addr(48), psm (16), mtu (16) 154 #define L2CAP_CREATE_CHANNEL_MTU 0x26 155 156 // register SDP Service Record: service record (size) 157 #define SDP_REGISTER_SERVICE_RECORD 0x30 158 159 // unregister SDP Service Record 160 #define SDP_UNREGISTER_SERVICE_RECORD 0x31 161 162 // Get remote RFCOMM services 163 #define SDP_CLIENT_QUERY_RFCOMM_SERVICES 0x32 164 165 // Get remote SDP services 166 #define SDP_CLIENT_QUERY_SERVICES 0x33 167 168 // RFCOMM "HCI" Commands 169 #define RFCOMM_CREATE_CHANNEL 0x40 170 #define RFCOMM_DISCONNECT 0x41 171 #define RFCOMM_REGISTER_SERVICE 0x42 172 #define RFCOMM_UNREGISTER_SERVICE 0x43 173 #define RFCOMM_ACCEPT_CONNECTION 0x44 174 #define RFCOMM_DECLINE_CONNECTION 0x45 175 #define RFCOMM_PERSISTENT_CHANNEL 0x46 176 #define RFCOMM_CREATE_CHANNEL_WITH_CREDITS 0x47 177 #define RFCOMM_REGISTER_SERVICE_WITH_CREDITS 0x48 178 #define RFCOMM_GRANT_CREDITS 0x49 179 180 // 181 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode) 182 183 // data: event(8) 184 #define DAEMON_EVENT_CONNECTION_OPENED 0x50 185 186 // data: event(8) 187 #define DAEMON_EVENT_CONNECTION_CLOSED 0x51 188 189 // data: event(8), nr_connections(8) 190 #define DAEMON_NR_CONNECTIONS_CHANGED 0x52 191 192 // data: event(8) 193 #define DAEMON_EVENT_NEW_RFCOMM_CREDITS 0x53 194 195 // data: event() 196 #define DAEMON_EVENT_HCI_PACKET_SENT 0x54 197 198 /** 199 * Connection State 200 */ 201 typedef enum { 202 AUTH_FLAGS_NONE = 0x0000, 203 RECV_LINK_KEY_REQUEST = 0x0001, 204 HANDLE_LINK_KEY_REQUEST = 0x0002, 205 SENT_LINK_KEY_REPLY = 0x0004, 206 SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008, 207 RECV_LINK_KEY_NOTIFICATION = 0x0010, 208 DENY_PIN_CODE_REQUEST = 0x0040, 209 RECV_IO_CAPABILITIES_REQUEST = 0x0080, 210 SEND_IO_CAPABILITIES_REPLY = 0x0100, 211 SEND_USER_CONFIRM_REPLY = 0x0200, 212 SEND_USER_PASSKEY_REPLY = 0x0400, 213 214 // pairing status 215 LEGACY_PAIRING_ACTIVE = 0x2000, 216 SSP_PAIRING_ACTIVE = 0x4000, 217 218 // connection status 219 CONNECTION_ENCRYPTED = 0x8000, 220 } hci_authentication_flags_t; 221 222 typedef enum { 223 SENT_CREATE_CONNECTION = 1, 224 RECEIVED_CONNECTION_REQUEST, 225 ACCEPTED_CONNECTION_REQUEST, 226 REJECTED_CONNECTION_REQUEST, 227 OPEN, 228 SENT_DISCONNECT 229 } CONNECTION_STATE; 230 231 typedef enum { 232 BONDING_REQUEST_REMOTE_FEATURES = 0x01, 233 BONDING_RECEIVED_REMOTE_FEATURES = 0x02, 234 BONDING_REMOTE_SUPPORTS_SSP = 0x04, 235 BONDING_DISCONNECT_SECURITY_BLOCK = 0x08, 236 BONDING_SEND_AUTHENTICATE_REQUEST = 0x10, 237 BONDING_SEND_ENCRYPTION_REQUEST = 0x20, 238 } bonding_flags_t; 239 240 typedef enum { 241 BLUETOOTH_OFF = 1, 242 BLUETOOTH_ON, 243 BLUETOOTH_ACTIVE 244 } BLUETOOTH_STATE; 245 246 typedef struct { 247 // linked list - assert: first field 248 linked_item_t item; 249 250 // remote side 251 bd_addr_t address; 252 253 // module handle 254 hci_con_handle_t con_handle; 255 256 // connection state 257 CONNECTION_STATE state; 258 259 // bonding 260 bonding_flags_t bonding_flags; 261 262 // requested security level 263 gap_security_level_t requested_security_level; 264 265 // 266 link_key_type_t link_key_type; 267 268 // errands 269 hci_authentication_flags_t authentication_flags; 270 271 timer_source_t timeout; 272 273 #ifdef HAVE_TIME 274 // timer 275 struct timeval timestamp; 276 #endif 277 #ifdef HAVE_TICK 278 uint32_t timestamp; // timeout in system ticks 279 #endif 280 281 // ACL packet recombination - ACL Header + ACL payload 282 uint8_t acl_recombination_buffer[4 + HCI_ACL_BUFFER_SIZE]; 283 uint16_t acl_recombination_pos; 284 uint16_t acl_recombination_length; 285 286 // number ACL packets sent to controller 287 uint8_t num_acl_packets_sent; 288 289 } hci_connection_t; 290 291 /** 292 * main data structure 293 */ 294 typedef struct { 295 // transport component with configuration 296 hci_transport_t * hci_transport; 297 void * config; 298 299 // bsic configuration 300 char * local_name; 301 uint32_t class_of_device; 302 bd_addr_t local_bd_addr; 303 uint8_t ssp_enable; 304 uint8_t ssp_io_capability; 305 uint8_t ssp_authentication_requirement; 306 uint8_t ssp_auto_accept; 307 308 // hardware power controller 309 bt_control_t * control; 310 311 // list of existing baseband connections 312 linked_list_t connections; 313 314 // single buffer for HCI Command assembly 315 uint8_t hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8) 316 317 /* host to controller flow control */ 318 uint8_t num_cmd_packets; 319 // uint8_t total_num_cmd_packets; 320 uint8_t total_num_acl_packets; 321 uint16_t acl_data_packet_length; 322 uint8_t total_num_le_packets; 323 uint16_t le_data_packet_length; 324 325 /* local supported features */ 326 uint8_t local_supported_features[8]; 327 328 // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE 329 uint16_t packet_types; 330 331 /* callback to L2CAP layer */ 332 void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 333 334 /* remote device db */ 335 remote_device_db_t const*remote_device_db; 336 337 /* hci state machine */ 338 HCI_STATE state; 339 uint8_t substate; 340 uint8_t cmds_ready; 341 342 uint8_t discoverable; 343 uint8_t connectable; 344 uint8_t bondable; 345 346 /* buffer for scan enable cmd - 0xff no change */ 347 uint8_t new_scan_enable_value; 348 349 // buffer for single connection decline 350 uint8_t decline_reason; 351 bd_addr_t decline_addr; 352 353 uint8_t adv_addr_type; 354 bd_addr_t adv_address; 355 356 } hci_stack_t; 357 358 // create and send hci command packets based on a template and a list of parameters 359 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); 360 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr); 361 362 void hci_connectable_control(uint8_t enable); 363 void hci_close(void); 364 365 /** 366 * run the hci control loop once 367 */ 368 void hci_run(void); 369 370 // send complete CMD packet 371 int hci_send_cmd_packet(uint8_t *packet, int size); 372 373 // send ACL packet 374 int hci_send_acl_packet(uint8_t *packet, int size); 375 376 // non-blocking UART driver needs 377 int hci_can_send_packet_now(uint8_t packet_type); 378 379 bd_addr_t * hci_local_bd_addr(void); 380 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle); 381 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle); 382 uint8_t hci_number_free_acl_slots(void); 383 int hci_authentication_active_for_handle(hci_con_handle_t handle); 384 uint16_t hci_max_acl_data_packet_length(void); 385 uint16_t hci_usable_acl_packet_types(void); 386 uint8_t* hci_get_outgoing_acl_packet_buffer(void); 387 388 // 389 void hci_emit_state(void); 390 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status); 391 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 392 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason); 393 void hci_emit_nr_connections_changed(void); 394 void hci_emit_hci_open_failed(void); 395 void hci_emit_btstack_version(void); 396 void hci_emit_system_bluetooth_enabled(uint8_t enabled); 397 void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name); 398 void hci_emit_discoverable_enabled(uint8_t enabled); 399 void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 400 401 // query if remote side supports SSP 402 // query if the local side supports SSP 403 int hci_local_ssp_activated(); 404 405 // query if the remote side supports SSP 406 int hci_remote_ssp_supported(hci_con_handle_t con_handle); 407 408 // query if both sides support SSP 409 int hci_ssp_supported_on_both_sides(hci_con_handle_t handle); 410 411 412 // disconnect because of security block 413 void hci_disconnect_security_block(hci_con_handle_t con_handle); 414 415 /** Embedded API **/ 416 417 // Set up HCI. 418 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db); 419 420 // Registers a packet handler. Used if L2CAP is not used (rarely). 421 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 422 423 // Requests the change of BTstack power mode. 424 int hci_power_control(HCI_POWER_MODE mode); 425 426 // Allows to control if device is discoverable. OFF by default. 427 void hci_discoverable_control(uint8_t enable); 428 429 // Creates and sends HCI command packets based on a template and 430 // a list of parameters. Will return error if outgoing data buffer 431 // is occupied. 432 int hci_send_cmd(const hci_cmd_t *cmd, ...); 433 434 // Deletes link key for remote device with baseband address. 435 void hci_drop_link_key_for_bd_addr(bd_addr_t *addr); 436 437 // Configure Secure Simple Pairing 438 439 // enable will enable SSP during init 440 void hci_ssp_set_enable(int enable); 441 442 // if set, BTstack will respond to io capability request using authentication requirement 443 void hci_ssp_set_io_capability(int ssp_io_capability); 444 void hci_ssp_set_authentication_requirement(int authentication_requirement); 445 446 // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 447 void hci_ssp_set_auto_accept(int auto_accept); 448 449 // get addr type and address used in advertisement packets 450 void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr); 451 452 453 #if defined __cplusplus 454 } 455 #endif 456