xref: /btstack/src/hci.h (revision 6b64433e6a17b046d431031fa3995f31c29aa188)
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 #if HCI_ACL_BUFFER_SIZE > HCI_CMD_BUFFER_SIZE
94 #define HCI_PACKET_BUFFER_SIZE HCI_ACL_BUFFER_SIZE
95 #else
96 #define HCI_PACKET_BUFFER_SIZE HCI_CMD_BUFFER_SIZE
97 #endif
98 
99 // OGFs
100 #define OGF_LINK_CONTROL          0x01
101 #define OGF_LINK_POLICY           0x02
102 #define OGF_CONTROLLER_BASEBAND   0x03
103 #define OGF_INFORMATIONAL_PARAMETERS 0x04
104 #define OGF_LE_CONTROLLER 0x08
105 #define OGF_BTSTACK 0x3d
106 #define OGF_VENDOR  0x3f
107 
108 // cmds for BTstack
109 // get state: @returns HCI_STATE
110 #define BTSTACK_GET_STATE                                  0x01
111 
112 // set power mode: @param HCI_POWER_MODE
113 #define BTSTACK_SET_POWER_MODE                             0x02
114 
115 // set capture mode: @param on
116 #define BTSTACK_SET_ACL_CAPTURE_MODE                       0x03
117 
118 // get BTstack version
119 #define BTSTACK_GET_VERSION                                0x04
120 
121 // get system Bluetooth state
122 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED               0x05
123 
124 // set system Bluetooth state
125 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED               0x06
126 
127 // enable inquiry scan for this client
128 #define BTSTACK_SET_DISCOVERABLE                           0x07
129 
130 // set global Bluetooth state
131 #define BTSTACK_SET_BLUETOOTH_ENABLED                      0x08
132 
133 // create l2cap channel: @param bd_addr(48), psm (16)
134 #define L2CAP_CREATE_CHANNEL                               0x20
135 
136 // disconnect l2cap disconnect, @param channel(16), reason(8)
137 #define L2CAP_DISCONNECT                                   0x21
138 
139 // register l2cap service: @param psm(16), mtu (16)
140 #define L2CAP_REGISTER_SERVICE                             0x22
141 
142 // unregister l2cap disconnect, @param psm(16)
143 #define L2CAP_UNREGISTER_SERVICE                           0x23
144 
145 // accept connection @param bd_addr(48), dest cid (16)
146 #define L2CAP_ACCEPT_CONNECTION                            0x24
147 
148 // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8)
149 #define L2CAP_DECLINE_CONNECTION                           0x25
150 
151 // create l2cap channel: @param bd_addr(48), psm (16), mtu (16)
152 #define L2CAP_CREATE_CHANNEL_MTU                           0x26
153 
154 // register SDP Service Record: service record (size)
155 #define SDP_REGISTER_SERVICE_RECORD                        0x30
156 
157 // unregister SDP Service Record
158 #define SDP_UNREGISTER_SERVICE_RECORD                      0x31
159 
160 // RFCOMM "HCI" Commands
161 #define RFCOMM_CREATE_CHANNEL       0x40
162 #define RFCOMM_DISCONNECT			0x41
163 #define RFCOMM_REGISTER_SERVICE     0x42
164 #define RFCOMM_UNREGISTER_SERVICE   0x43
165 #define RFCOMM_ACCEPT_CONNECTION    0x44
166 #define RFCOMM_DECLINE_CONNECTION   0x45
167 #define RFCOMM_PERSISTENT_CHANNEL   0x46
168 #define RFCOMM_CREATE_CHANNEL_WITH_CREDITS   0x47
169 #define RFCOMM_REGISTER_SERVICE_WITH_CREDITS 0x48
170 #define RFCOMM_GRANT_CREDITS                 0x49
171 
172 //
173 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode)
174 
175 // data: event(8)
176 #define DAEMON_EVENT_CONNECTION_OPENED                     0x50
177 
178 // data: event(8)
179 #define DAEMON_EVENT_CONNECTION_CLOSED                     0x51
180 
181 // data: event(8), nr_connections(8)
182 #define DAEMON_NR_CONNECTIONS_CHANGED                      0x52
183 
184 // data: event(8)
185 #define DAEMON_EVENT_NEW_RFCOMM_CREDITS                    0x53
186 
187 // data: event()
188 #define DAEMON_EVENT_HCI_PACKET_SENT                       0x54
189 
190 /**
191  * Connection State
192  */
193 typedef enum {
194     AUTH_FLAGS_NONE                = 0x00,
195     RECV_LINK_KEY_REQUEST          = 0x01,
196     HANDLE_LINK_KEY_REQUEST        = 0x02,
197     SENT_LINK_KEY_REPLY            = 0x04,
198     SENT_LINK_KEY_NEGATIVE_REQUEST = 0x08,
199     RECV_LINK_KEY_NOTIFICATION     = 0x10,
200     RECV_PIN_CODE_REQUEST          = 0x20,
201     SENT_PIN_CODE_REPLY            = 0x40,
202     SENT_PIN_CODE_NEGATIVE_REPLY   = 0x80
203 } hci_authentication_flags_t;
204 
205 typedef enum {
206     SENT_CREATE_CONNECTION = 1,
207     RECEIVED_CONNECTION_REQUEST,
208     ACCEPTED_CONNECTION_REQUEST,
209     REJECTED_CONNECTION_REQUEST,
210     OPEN,
211     SENT_DISCONNECT
212 } CONNECTION_STATE;
213 
214 typedef enum {
215     BLUETOOTH_OFF = 1,
216     BLUETOOTH_ON,
217     BLUETOOTH_ACTIVE
218 } BLUETOOTH_STATE;
219 
220 typedef struct {
221     // linked list - assert: first field
222     linked_item_t    item;
223 
224     // remote side
225     bd_addr_t address;
226 
227     // module handle
228     hci_con_handle_t con_handle;
229 
230     // state
231     CONNECTION_STATE state;
232 
233     // errands
234     hci_authentication_flags_t authentication_flags;
235 
236     timer_source_t timeout;
237 
238 #ifdef HAVE_TIME
239     // timer
240     struct timeval timestamp;
241 #endif
242 #ifdef HAVE_TICK
243     uint32_t timestamp; // timeout in system ticks
244 #endif
245 
246     // ACL packet recombination - ACL Header + ACL payload
247     uint8_t  acl_recombination_buffer[4 + HCI_ACL_BUFFER_SIZE];
248     uint16_t acl_recombination_pos;
249     uint16_t acl_recombination_length;
250 
251     // number ACL packets sent to controller
252     uint8_t num_acl_packets_sent;
253 
254 } hci_connection_t;
255 
256 /**
257  * main data structure
258  */
259 typedef struct {
260     // transport component with configuration
261     hci_transport_t  * hci_transport;
262     void             * config;
263 
264     // hardware power controller
265     bt_control_t     * control;
266 
267     // list of existing baseband connections
268     linked_list_t     connections;
269 
270     // single buffer for HCI Command assembly
271     uint8_t          hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8)
272 
273     /* host to controller flow control */
274     uint8_t  num_cmd_packets;
275     // uint8_t  total_num_cmd_packets;
276     uint8_t  total_num_acl_packets;
277     uint16_t acl_data_packet_length;
278 
279     // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE
280     uint16_t packet_types;
281 
282     /* callback to L2CAP layer */
283     void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
284 
285     /* remote device db */
286     remote_device_db_t const*remote_device_db;
287 
288     /* hci state machine */
289     HCI_STATE state;
290     uint8_t   substate;
291     uint8_t   cmds_ready;
292 
293     uint8_t   discoverable;
294     uint8_t   connectable;
295 
296     /* buffer for scan enable cmd - 0xff no change */
297     uint8_t   new_scan_enable_value;
298 
299     // buffer for single connection decline
300     uint8_t   decline_reason;
301     bd_addr_t decline_addr;
302 
303 } hci_stack_t;
304 
305 // create and send hci command packets based on a template and a list of parameters
306 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...);
307 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr);
308 
309 // set up HCI
310 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db);
311 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size));
312 void hci_close(void);
313 
314 // power and inquriy scan control
315 int  hci_power_control(HCI_POWER_MODE mode);
316 void hci_discoverable_control(uint8_t enable);
317 void hci_connectable_control(uint8_t enable);
318 
319 /**
320  * run the hci control loop once
321  */
322 void hci_run(void);
323 
324 // create and send hci command packets based on a template and a list of parameters
325 int hci_send_cmd(const hci_cmd_t *cmd, ...);
326 
327 // send complete CMD packet
328 int hci_send_cmd_packet(uint8_t *packet, int size);
329 
330 // send ACL packet
331 int hci_send_acl_packet(uint8_t *packet, int size);
332 
333 // non-blocking UART driver needs
334 int hci_can_send_packet_now(uint8_t packet_type);
335 
336 hci_connection_t * connection_for_handle(hci_con_handle_t con_handle);
337 uint8_t  hci_number_outgoing_packets(hci_con_handle_t handle);
338 uint8_t  hci_number_free_acl_slots(void);
339 int      hci_authentication_active_for_handle(hci_con_handle_t handle);
340 void     hci_drop_link_key_for_bd_addr(bd_addr_t *addr);
341 uint16_t hci_max_acl_data_packet_length(void);
342 uint16_t hci_usable_acl_packet_types(void);
343 uint8_t* hci_get_outgoing_acl_packet_buffer(void);
344 
345 //
346 void hci_emit_state(void);
347 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status);
348 void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
349 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason);
350 void hci_emit_nr_connections_changed(void);
351 void hci_emit_hci_open_failed(void);
352 void hci_emit_btstack_version(void);
353 void hci_emit_system_bluetooth_enabled(uint8_t enabled);
354 void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name);
355 void hci_emit_discoverable_enabled(uint8_t enabled);
356 
357 #if defined __cplusplus
358 }
359 #endif
360