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