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