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