1 /* 2 * Copyright (C) 2009 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 * 17 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 21 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * hci.h 34 * 35 * Created by Matthias Ringwald on 4/29/09. 36 * 37 */ 38 39 #pragma once 40 41 #include "config.h" 42 43 #include <btstack/hci_cmds.h> 44 #include <btstack/utils.h> 45 #include "hci_transport.h" 46 #include "bt_control.h" 47 #include "remote_device_db.h" 48 49 #include <stdint.h> 50 #include <stdlib.h> 51 #include <stdarg.h> 52 53 #if defined __cplusplus 54 extern "C" { 55 #endif 56 57 // packet header sizes 58 #define HCI_CMD_HEADER_SIZE 3 59 #define HCI_ACL_HEADER_SIZE 4 60 #define HCI_SCO_HEADER_SIZE 3 61 #define HCI_EVENT_HEADER_SIZE 2 62 63 // packet sizes (max payload) 64 #define HCI_ACL_DM1_SIZE 17 65 #define HCI_ACL_DH1_SIZE 27 66 #define HCI_ACL_2DH1_SIZE 54 67 #define HCI_ACL_3DH1_SIZE 83 68 #define HCI_ACL_DM3_SIZE 121 69 #define HCI_ACL_DH3_SIZE 183 70 #define HCI_ACL_DM5_SIZE 224 71 #define HCI_ACL_DH5_SIZE 339 72 #define HCI_ACL_2DH3_SIZE 367 73 #define HCI_ACL_3DH3_SIZE 552 74 #define HCI_ACL_2DH5_SIZE 679 75 #define HCI_ACL_3DH5_SIZE 1021 76 77 #define HCI_EVENT_PAYLOAD_SIZE 255 78 #define HCI_CMD_PAYLOAD_SIZE 255 79 80 // packet buffer sizes 81 // HCI_ACL_PAYLOAD_SIZE is configurable and defined in config.h 82 #define HCI_EVENT_BUFFER_SIZE (HCI_EVENT_HEADER_SIZE + HCI_EVENT_PAYLOAD_SIZE) 83 #define HCI_CMD_BUFFER_SIZE (HCI_CMD_HEADER_SIZE + HCI_CMD_PAYLOAD_SIZE) 84 #define HCI_ACL_BUFFER_SIZE (HCI_ACL_HEADER_SIZE + HCI_ACL_PAYLOAD_SIZE) 85 86 // size of hci buffers, big enough for command, event, or acl packet without H4 packet type 87 // @note cmd buffer is bigger than event buffer 88 #if HCI_ACL_BUFFER_SIZE > HCI_CMD_BUFFER_SIZE 89 #define HCI_PACKET_BUFFER_SIZE HCI_ACL_BUFFER_SIZE 90 #else 91 #define HCI_PACKET_BUFFER_SIZE HCI_CMD_BUFFER_SIZE 92 #endif 93 94 // OGFs 95 #define OGF_LINK_CONTROL 0x01 96 #define OGF_LINK_POLICY 0x02 97 #define OGF_CONTROLLER_BASEBAND 0x03 98 #define OGF_INFORMATIONAL_PARAMETERS 0x04 99 #define OGF_LE_CONTROLLER 0x08 100 #define OGF_BTSTACK 0x3d 101 #define OGF_VENDOR 0x3f 102 103 // cmds for BTstack 104 // get state: @returns HCI_STATE 105 #define BTSTACK_GET_STATE 0x01 106 107 // set power mode: @param HCI_POWER_MODE 108 #define BTSTACK_SET_POWER_MODE 0x02 109 110 // set capture mode: @param on 111 #define BTSTACK_SET_ACL_CAPTURE_MODE 0x03 112 113 // get BTstack version 114 #define BTSTACK_GET_VERSION 0x04 115 116 // get system Bluetooth state 117 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED 0x05 118 119 // set system Bluetooth state 120 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED 0x06 121 122 // enable inquiry scan for this client 123 #define BTSTACK_SET_DISCOVERABLE 0x07 124 125 // set global Bluetooth state 126 #define BTSTACK_SET_BLUETOOTH_ENABLED 0x08 127 128 // create l2cap channel: @param bd_addr(48), psm (16) 129 #define L2CAP_CREATE_CHANNEL 0x20 130 131 // disconnect l2cap disconnect, @param channel(16), reason(8) 132 #define L2CAP_DISCONNECT 0x21 133 134 // register l2cap service: @param psm(16), mtu (16) 135 #define L2CAP_REGISTER_SERVICE 0x22 136 137 // unregister l2cap disconnect, @param psm(16) 138 #define L2CAP_UNREGISTER_SERVICE 0x23 139 140 // accept connection @param bd_addr(48), dest cid (16) 141 #define L2CAP_ACCEPT_CONNECTION 0x24 142 143 // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8) 144 #define L2CAP_DECLINE_CONNECTION 0x25 145 146 // create l2cap channel: @param bd_addr(48), psm (16), mtu (16) 147 #define L2CAP_CREATE_CHANNEL_MTU 0x26 148 149 // register SDP Service Record: service record (size) 150 #define SDP_REGISTER_SERVICE_RECORD 0x30 151 152 // unregister SDP Service Record 153 #define SDP_UNREGISTER_SERVICE_RECORD 0x31 154 155 // RFCOMM "HCI" Commands 156 #define RFCOMM_CREATE_CHANNEL 0x40 157 #define RFCOMM_DISCONNECT 0x41 158 #define RFCOMM_REGISTER_SERVICE 0x42 159 #define RFCOMM_UNREGISTER_SERVICE 0x43 160 #define RFCOMM_ACCEPT_CONNECTION 0x44 161 #define RFCOMM_DECLINE_CONNECTION 0x45 162 #define RFCOMM_PERSISTENT_CHANNEL 0x46 163 #define RFCOMM_CREATE_CHANNEL_WITH_CREDITS 0x47 164 #define RFCOMM_REGISTER_SERVICE_WITH_CREDITS 0x48 165 #define RFCOMM_GRANT_CREDITS 0x49 166 167 // 168 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode) 169 170 // data: event(8) 171 #define DAEMON_EVENT_CONNECTION_OPENED 0x50 172 173 // data: event(8) 174 #define DAEMON_EVENT_CONNECTION_CLOSED 0x51 175 176 // data: event(8), nr_connections(8) 177 #define DAEMON_NR_CONNECTIONS_CHANGED 0x52 178 179 // data: event(8) 180 #define DAEMON_EVENT_NEW_RFCOMM_CREDITS 0x53 181 182 // data: event() 183 #define DAEMON_EVENT_HCI_PACKET_SENT 0x54 184 185 /** 186 * Connection State 187 */ 188 typedef enum { 189 AUTH_FLAGS_NONE = 0x00, 190 RECV_LINK_KEY_REQUEST = 0x01, 191 HANDLE_LINK_KEY_REQUEST = 0x02, 192 SENT_LINK_KEY_REPLY = 0x04, 193 SENT_LINK_KEY_NEGATIVE_REQUEST = 0x08, 194 RECV_LINK_KEY_NOTIFICATION = 0x10, 195 RECV_PIN_CODE_REQUEST = 0x20, 196 SENT_PIN_CODE_REPLY = 0x40, 197 SENT_PIN_CODE_NEGATIVE_REPLY = 0x80 198 } hci_authentication_flags_t; 199 200 typedef enum { 201 SENT_CREATE_CONNECTION = 1, 202 RECEIVED_CONNECTION_REQUEST, 203 ACCEPTED_CONNECTION_REQUEST, 204 REJECTED_CONNECTION_REQUEST, 205 OPEN, 206 SENT_DISCONNECT 207 } CONNECTION_STATE; 208 209 typedef enum { 210 BLUETOOTH_OFF = 1, 211 BLUETOOTH_ON, 212 BLUETOOTH_ACTIVE 213 } BLUETOOTH_STATE; 214 215 typedef struct { 216 // linked list - assert: first field 217 linked_item_t item; 218 219 // remote side 220 bd_addr_t address; 221 222 // module handle 223 hci_con_handle_t con_handle; 224 225 // state 226 CONNECTION_STATE state; 227 228 // errands 229 hci_authentication_flags_t authentication_flags; 230 231 timer_source_t timeout; 232 233 #ifdef HAVE_TIME 234 // timer 235 struct timeval timestamp; 236 #endif 237 #ifdef HAVE_TICK 238 uint32_t timestamp; // timeout in system ticks 239 #endif 240 241 // ACL packet recombination - ACL Header + ACL payload 242 uint8_t acl_recombination_buffer[4 + HCI_ACL_BUFFER_SIZE]; 243 uint16_t acl_recombination_pos; 244 uint16_t acl_recombination_length; 245 246 // number ACL packets sent to controller 247 uint8_t num_acl_packets_sent; 248 249 } hci_connection_t; 250 251 /** 252 * main data structure 253 */ 254 typedef struct { 255 // transport component with configuration 256 hci_transport_t * hci_transport; 257 void * config; 258 259 // hardware power controller 260 bt_control_t * control; 261 262 // list of existing baseband connections 263 linked_list_t connections; 264 265 // single buffer for HCI Command assembly 266 uint8_t hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8) 267 268 /* host to controller flow control */ 269 uint8_t num_cmd_packets; 270 // uint8_t total_num_cmd_packets; 271 uint8_t total_num_acl_packets; 272 uint16_t acl_data_packet_length; 273 274 // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE 275 uint16_t packet_types; 276 277 /* callback to L2CAP layer */ 278 void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 279 280 /* remote device db */ 281 remote_device_db_t const*remote_device_db; 282 283 /* hci state machine */ 284 HCI_STATE state; 285 uint8_t substate; 286 uint8_t cmds_ready; 287 288 uint8_t discoverable; 289 uint8_t connectable; 290 291 /* buffer for scan enable cmd - 0xff no change */ 292 uint8_t new_scan_enable_value; 293 294 // buffer for single connection decline 295 uint8_t decline_reason; 296 bd_addr_t decline_addr; 297 298 } hci_stack_t; 299 300 // create and send hci command packets based on a template and a list of parameters 301 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); 302 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr); 303 304 // set up HCI 305 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db); 306 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 307 void hci_close(void); 308 309 // power and inquriy scan control 310 int hci_power_control(HCI_POWER_MODE mode); 311 void hci_discoverable_control(uint8_t enable); 312 void hci_connectable_control(uint8_t enable); 313 314 /** 315 * run the hci control loop once 316 */ 317 void hci_run(void); 318 319 // create and send hci command packets based on a template and a list of parameters 320 int hci_send_cmd(const hci_cmd_t *cmd, ...); 321 322 // send complete CMD packet 323 int hci_send_cmd_packet(uint8_t *packet, int size); 324 325 // send ACL packet 326 int hci_send_acl_packet(uint8_t *packet, int size); 327 328 // non-blocking UART driver needs 329 int hci_can_send_packet_now(uint8_t packet_type); 330 331 hci_connection_t * connection_for_handle(hci_con_handle_t con_handle); 332 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle); 333 uint8_t hci_number_free_acl_slots(void); 334 int hci_authentication_active_for_handle(hci_con_handle_t handle); 335 void hci_drop_link_key_for_bd_addr(bd_addr_t *addr); 336 uint16_t hci_max_acl_data_packet_length(void); 337 uint16_t hci_usable_acl_packet_types(void); 338 uint8_t* hci_get_outgoing_acl_packet_buffer(void); 339 340 // 341 void hci_emit_state(void); 342 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status); 343 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 344 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason); 345 void hci_emit_nr_connections_changed(void); 346 void hci_emit_hci_open_failed(void); 347 void hci_emit_btstack_version(void); 348 void hci_emit_system_bluetooth_enabled(uint8_t enabled); 349 void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name); 350 void hci_emit_discoverable_enabled(uint8_t enabled); 351 352 #if defined __cplusplus 353 } 354 #endif 355