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 <btstack/hci_cmds.h> 42 #include <btstack/utils.h> 43 #include "hci_transport.h" 44 #include "bt_control.h" 45 46 #include <stdint.h> 47 #include <stdlib.h> 48 #include <stdarg.h> 49 50 // packet header lenghts 51 #define HCI_CMD_DATA_PKT_HDR 0x03 52 #define HCI_ACL_DATA_PKT_HDR 0x04 53 #define HCI_SCO_DATA_PKT_HDR 0x03 54 #define HCI_EVENT_PKT_HDR 0x02 55 56 // packet sizes 57 #define HCI_ACL_3DH5_SIZE 1021 58 #define HCI_ACL_DH5_SIZE 339 59 60 // OGFs 61 #define OGF_LINK_CONTROL 0x01 62 #define OGF_LINK_POLICY 0x02 63 #define OGF_CONTROLLER_BASEBAND 0x03 64 #define OGF_INFORMATIONAL_PARAMETERS 0x04 65 #define OGF_BTSTACK 0x3d 66 #define OGF_VENDOR 0x3f 67 68 // cmds for BTstack 69 // get state: @returns HCI_STATE 70 #define BTSTACK_GET_STATE 0x01 71 72 // set power mode: @param HCI_POWER_MODE 73 #define BTSTACK_SET_POWER_MODE 0x02 74 75 // set capture mode: @param on 76 #define BTSTACK_SET_ACL_CAPTURE_MODE 0x03 77 78 // get BTstack version 79 #define BTSTACK_GET_VERSION 0x04 80 81 // get system Bluetooth state 82 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED 0x05 83 84 // set system Bluetooth state 85 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED 0x06 86 87 // create l2cap channel: @param bd_addr(48), psm (16) 88 #define L2CAP_CREATE_CHANNEL 0x20 89 90 // disconnect l2cap disconnect, @param channel(16), reason(8) 91 #define L2CAP_DISCONNECT 0x21 92 93 // register l2cap service: @param psm(16), mtu (16) 94 #define L2CAP_REGISTER_SERVICE 0x22 95 96 // unregister l2cap disconnect, @param psm(16) 97 #define L2CAP_UNREGISTER_SERVICE 0x23 98 99 // accept connection @param bd_addr(48), dest cid (16) 100 #define L2CAP_ACCEPT_CONNECTION 0x24 101 102 // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8) 103 #define L2CAP_DECLINE_CONNECTION 0x25 104 105 // create l2cap channel: @param bd_addr(48), psm (16), mtu (16) 106 #define L2CAP_CREATE_CHANNEL_MTU 0x26 107 108 // register SDP Service Record: service record (size) 109 #define SDP_REGISTER_SERVICE_RECORD 0x30 110 111 // unregister SDP Service Record 112 #define SDP_UNREGISTER_SERVICE_RECORD 0x31 113 114 115 116 // 117 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode) 118 119 // data: event(8) 120 #define DAEMON_EVENT_CONNECTION_CLOSED 0x70 121 122 // data: event(8), nr_connections(8) 123 #define DAEMON_NR_CONNECTIONS_CHANGED 0x71 124 125 126 /** 127 * Connection State 128 */ 129 typedef enum { 130 RECV_LINK_KEY_REQUEST = 0x01, 131 SENT_LINK_KEY_REPLY = 0x02, 132 SENT_LINK_KEY_NEGATIVE_REQUEST = 0x04, 133 RECV_LINK_KEY_NOTIFICATION = 0x08, 134 RECV_PIN_CODE_REQUEST = 0x10, 135 SENT_PIN_CODE_REPLY = 0x20, 136 SENT_PIN_CODE_NEGATIVE_REPLY = 0x40 137 } hci_authentication_flags_t; 138 139 typedef enum { 140 SENT_CREATE_CONNECTION = 1, 141 RECEIVED_CONNECTION_REQUEST, 142 ACCEPTED_CONNECTION_REQUEST, 143 REJECTED_CONNECTION_REQUEST, 144 OPEN, 145 SENT_DISCONNECT 146 } CONNECTION_STATE; 147 148 typedef enum { 149 BLUETOOTH_OFF = 1, 150 BLUETOOTH_ON, 151 BLUETOOTH_ACTIVE 152 } BLUETOOTH_STATE; 153 154 typedef struct { 155 // linked list - assert: first field 156 linked_item_t item; 157 158 // remote side 159 bd_addr_t address; 160 161 // module handle 162 hci_con_handle_t con_handle; 163 164 // state 165 CONNECTION_STATE state; 166 167 // errands 168 hci_authentication_flags_t authentication_flags; 169 170 // timer 171 timer_source_t timeout; 172 struct timeval timestamp; 173 174 // ACL packet recombination 175 uint8_t acl_recombination_buffer[HCI_ACL_3DH5_SIZE]; // max packet: DH5 = header(4) + payload (339) 176 uint16_t acl_recombination_pos; 177 uint16_t acl_recombination_length; 178 179 // number ACL packets sent to controller 180 uint8_t num_acl_packets_sent; 181 182 } hci_connection_t; 183 184 /** 185 * main data structure 186 */ 187 typedef struct { 188 // transport component with configuration 189 hci_transport_t * hci_transport; 190 void * config; 191 192 // hardware power controller 193 bt_control_t * control; 194 195 // list of existing baseband connections 196 linked_list_t connections; 197 198 // single buffer for HCI Command assembly 199 uint8_t * hci_cmd_buffer; 200 201 /* host to controller flow control */ 202 uint8_t num_cmd_packets; 203 // uint8_t total_num_cmd_packets; 204 uint8_t total_num_acl_packets; 205 uint16_t acl_data_packet_length; 206 207 /* callback to L2CAP layer */ 208 void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 209 210 /* hci state machine */ 211 HCI_STATE state; 212 uint8_t substate; 213 uint8_t cmds_ready; 214 215 } hci_stack_t; 216 217 // create and send hci command packets based on a template and a list of parameters 218 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); 219 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr); 220 221 // set up HCI 222 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control); 223 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 224 225 // power control 226 int hci_power_control(HCI_POWER_MODE mode); 227 228 /** 229 * run the hci control loop once 230 */ 231 void hci_run(); 232 233 // create and send hci command packets based on a template and a list of parameters 234 int hci_send_cmd(const hci_cmd_t *cmd, ...); 235 236 // send complete CMD packet 237 int hci_send_cmd_packet(uint8_t *packet, int size); 238 239 // send ACL packet 240 int hci_send_acl_packet(uint8_t *packet, int size); 241 242 hci_connection_t * connection_for_handle(hci_con_handle_t con_handle); 243 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle); 244 uint8_t hci_number_free_acl_slots(); 245 int hci_ready_to_send(hci_con_handle_t handle); 246 int hci_authentication_active_for_handle(hci_con_handle_t handle); 247 248 // 249 void hci_emit_state(); 250 void hci_emit_connection_complete(hci_connection_t *conn); 251 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 252 void hci_emit_nr_connections_changed(); 253 void hci_emit_hci_open_failed(); 254 void hci_emit_btstack_version(); 255 void hci_emit_system_bluetooth_enabled(uint8_t enabled); 256