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