xref: /btstack/src/hci.h (revision c785ef68cf2f75708d1b63e77b925cf02aef517e)
11f504dbdSmatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
331f504dbdSmatthias.ringwald  *  hci.h
341f504dbdSmatthias.ringwald  *
351f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
361f504dbdSmatthias.ringwald  *
371f504dbdSmatthias.ringwald  */
381f504dbdSmatthias.ringwald 
391f504dbdSmatthias.ringwald #pragma once
401f504dbdSmatthias.ringwald 
4123053b9eSmatthias.ringwald #include <btstack/hci_cmds.h>
4223053b9eSmatthias.ringwald #include <btstack/utils.h>
43ba681a6cSmatthias.ringwald #include "hci_transport.h"
44ba681a6cSmatthias.ringwald #include "bt_control.h"
4529d53098Smatthias.ringwald #include "remote_device_db.h"
46ba681a6cSmatthias.ringwald 
4793b8dc03Smatthias.ringwald #include <stdint.h>
4802ea9861Smatthias.ringwald #include <stdlib.h>
491cd208adSmatthias.ringwald #include <stdarg.h>
5093b8dc03Smatthias.ringwald 
51e3b6fe7eSmatthias.ringwald #if defined __cplusplus
52e3b6fe7eSmatthias.ringwald extern "C" {
53e3b6fe7eSmatthias.ringwald #endif
54e3b6fe7eSmatthias.ringwald 
5580d52d6bSmatthias.ringwald // packet header lenghts
5680d52d6bSmatthias.ringwald #define HCI_CMD_DATA_PKT_HDR	  0x03
5780d52d6bSmatthias.ringwald #define HCI_ACL_DATA_PKT_HDR	  0x04
5880d52d6bSmatthias.ringwald #define HCI_SCO_DATA_PKT_HDR	  0x03
5980d52d6bSmatthias.ringwald #define HCI_EVENT_PKT_HDR         0x02
6080d52d6bSmatthias.ringwald 
6111c41d51Smatthias.ringwald // packet sizes
6211c41d51Smatthias.ringwald #define HCI_ACL_3DH5_SIZE         1021
6311c41d51Smatthias.ringwald #define HCI_ACL_DH5_SIZE           339
6411c41d51Smatthias.ringwald 
65945627e4Smatthias.ringwald // OGFs
66945627e4Smatthias.ringwald #define OGF_LINK_CONTROL          0x01
67d59f499aSmatthias.ringwald #define OGF_LINK_POLICY           0x02
68945627e4Smatthias.ringwald #define OGF_CONTROLLER_BASEBAND   0x03
69945627e4Smatthias.ringwald #define OGF_INFORMATIONAL_PARAMETERS 0x04
70945627e4Smatthias.ringwald #define OGF_BTSTACK 0x3d
71945627e4Smatthias.ringwald #define OGF_VENDOR  0x3f
7280d52d6bSmatthias.ringwald 
7380d52d6bSmatthias.ringwald // cmds for BTstack
7480d52d6bSmatthias.ringwald // get state: @returns HCI_STATE
7580d52d6bSmatthias.ringwald #define BTSTACK_GET_STATE                                  0x01
7680d52d6bSmatthias.ringwald 
7780d52d6bSmatthias.ringwald // set power mode: @param HCI_POWER_MODE
7880d52d6bSmatthias.ringwald #define BTSTACK_SET_POWER_MODE                             0x02
7980d52d6bSmatthias.ringwald 
8080d52d6bSmatthias.ringwald // set capture mode: @param on
8180d52d6bSmatthias.ringwald #define BTSTACK_SET_ACL_CAPTURE_MODE                       0x03
8280d52d6bSmatthias.ringwald 
831b0e3922Smatthias.ringwald // get BTstack version
841b0e3922Smatthias.ringwald #define BTSTACK_GET_VERSION                                0x04
851b0e3922Smatthias.ringwald 
862ed6235cSmatthias.ringwald // get system Bluetooth state
872ed6235cSmatthias.ringwald #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED               0x05
882ed6235cSmatthias.ringwald 
892ed6235cSmatthias.ringwald // set system Bluetooth state
902ed6235cSmatthias.ringwald #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED               0x06
912ed6235cSmatthias.ringwald 
92381fbed8Smatthias.ringwald // enable inquiry scan for this client
93381fbed8Smatthias.ringwald #define BTSTACK_SET_DISCOVERABLE                           0x07
94381fbed8Smatthias.ringwald 
953c4d4b90Smatthias.ringwald // set global Bluetooth state
963c4d4b90Smatthias.ringwald #define BTSTACK_SET_BLUETOOTH_ENABLED                      0x08
973c4d4b90Smatthias.ringwald 
9880d52d6bSmatthias.ringwald // create l2cap channel: @param bd_addr(48), psm (16)
9980d52d6bSmatthias.ringwald #define L2CAP_CREATE_CHANNEL                               0x20
10080d52d6bSmatthias.ringwald 
10180d52d6bSmatthias.ringwald // disconnect l2cap disconnect, @param channel(16), reason(8)
10280d52d6bSmatthias.ringwald #define L2CAP_DISCONNECT                                   0x21
10380d52d6bSmatthias.ringwald 
104116ee617Smatthias.ringwald // register l2cap service: @param psm(16), mtu (16)
105116ee617Smatthias.ringwald #define L2CAP_REGISTER_SERVICE                             0x22
106116ee617Smatthias.ringwald 
107116ee617Smatthias.ringwald // unregister l2cap disconnect, @param psm(16)
108116ee617Smatthias.ringwald #define L2CAP_UNREGISTER_SERVICE                           0x23
109116ee617Smatthias.ringwald 
110116ee617Smatthias.ringwald // accept connection @param bd_addr(48), dest cid (16)
111116ee617Smatthias.ringwald #define L2CAP_ACCEPT_CONNECTION                            0x24
112116ee617Smatthias.ringwald 
113116ee617Smatthias.ringwald // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8)
114116ee617Smatthias.ringwald #define L2CAP_DECLINE_CONNECTION                           0x25
115116ee617Smatthias.ringwald 
11615470d27Smatthias.ringwald // create l2cap channel: @param bd_addr(48), psm (16), mtu (16)
11715470d27Smatthias.ringwald #define L2CAP_CREATE_CHANNEL_MTU                           0x26
11815470d27Smatthias.ringwald 
11906d7e47eSmatthias.ringwald // register SDP Service Record: service record (size)
12006d7e47eSmatthias.ringwald #define SDP_REGISTER_SERVICE_RECORD                        0x30
12106d7e47eSmatthias.ringwald 
12206d7e47eSmatthias.ringwald // unregister SDP Service Record
12306d7e47eSmatthias.ringwald #define SDP_UNREGISTER_SERVICE_RECORD                      0x31
12406d7e47eSmatthias.ringwald 
125e115bf24Smatthias.ringwald // RFCOMM "HCI" Commands
126e115bf24Smatthias.ringwald #define RFCOMM_CREATE_CHANNEL       0x40
127e115bf24Smatthias.ringwald #define RFCOMM_DISCONNECT			0x41
128e115bf24Smatthias.ringwald #define RFCOMM_REGISTER_SERVICE     0x42
129e115bf24Smatthias.ringwald #define RFCOMM_UNREGISTER_SERVICE   0x43
130e115bf24Smatthias.ringwald #define RFCOMM_ACCEPT_CONNECTION    0x44
131e115bf24Smatthias.ringwald #define RFCOMM_DECLINE_CONNECTION   0x45
132933d8a80Smatthias.ringwald #define RFCOMM_PERSISTENT_CHANNEL   0x46
13306d7e47eSmatthias.ringwald 
13480d52d6bSmatthias.ringwald //
13580d52d6bSmatthias.ringwald #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode)
13680d52d6bSmatthias.ringwald 
137945627e4Smatthias.ringwald // data: event(8)
138d8293395Smatthias.ringwald #define DAEMON_EVENT_CONNECTION_OPENED                     0x50
13927d86fbbSmatthias.ringwald 
14027d86fbbSmatthias.ringwald // data: event(8)
141d8293395Smatthias.ringwald #define DAEMON_EVENT_CONNECTION_CLOSED                     0x51
142945627e4Smatthias.ringwald 
143945627e4Smatthias.ringwald // data: event(8), nr_connections(8)
144d8293395Smatthias.ringwald #define DAEMON_NR_CONNECTIONS_CHANGED                      0x52
14527d86fbbSmatthias.ringwald 
146bf8dada5Smatthias.ringwald // data: event(8)
147d8293395Smatthias.ringwald #define DAEMON_EVENT_NEW_RFCOMM_CREDITS                    0x53
148d8293395Smatthias.ringwald 
149d8293395Smatthias.ringwald // data: event()
150d8293395Smatthias.ringwald #define DAEMON_EVENT_HCI_PACKET_SENT                       0x54
151945627e4Smatthias.ringwald 
15206b35ec0Smatthias.ringwald /**
15306b35ec0Smatthias.ringwald  * Connection State
15406b35ec0Smatthias.ringwald  */
155c01e9cbdSmatthias.ringwald typedef enum {
1567d3b3569Smatthias.ringwald     AUTH_FLAGS_NONE                = 0x00,
1577fde4af9Smatthias.ringwald     RECV_LINK_KEY_REQUEST          = 0x01,
15832ab9390Smatthias.ringwald     HANDLE_LINK_KEY_REQUEST        = 0x02,
15932ab9390Smatthias.ringwald     SENT_LINK_KEY_REPLY            = 0x04,
16032ab9390Smatthias.ringwald     SENT_LINK_KEY_NEGATIVE_REQUEST = 0x08,
16132ab9390Smatthias.ringwald     RECV_LINK_KEY_NOTIFICATION     = 0x10,
16232ab9390Smatthias.ringwald     RECV_PIN_CODE_REQUEST          = 0x20,
16332ab9390Smatthias.ringwald     SENT_PIN_CODE_REPLY            = 0x40,
16432ab9390Smatthias.ringwald     SENT_PIN_CODE_NEGATIVE_REPLY   = 0x80
1657fde4af9Smatthias.ringwald } hci_authentication_flags_t;
166c01e9cbdSmatthias.ringwald 
167c8e4258aSmatthias.ringwald typedef enum {
168c8e4258aSmatthias.ringwald     SENT_CREATE_CONNECTION = 1,
169c8e4258aSmatthias.ringwald     RECEIVED_CONNECTION_REQUEST,
170c8e4258aSmatthias.ringwald     ACCEPTED_CONNECTION_REQUEST,
171c8e4258aSmatthias.ringwald     REJECTED_CONNECTION_REQUEST,
172c8e4258aSmatthias.ringwald     OPEN,
173c8e4258aSmatthias.ringwald     SENT_DISCONNECT
174c8e4258aSmatthias.ringwald } CONNECTION_STATE;
175c8e4258aSmatthias.ringwald 
17643bfb1bdSmatthias.ringwald typedef enum {
17743bfb1bdSmatthias.ringwald     BLUETOOTH_OFF = 1,
17843bfb1bdSmatthias.ringwald     BLUETOOTH_ON,
17943bfb1bdSmatthias.ringwald     BLUETOOTH_ACTIVE
18043bfb1bdSmatthias.ringwald } BLUETOOTH_STATE;
18143bfb1bdSmatthias.ringwald 
182c8e4258aSmatthias.ringwald typedef struct {
18306b35ec0Smatthias.ringwald     // linked list - assert: first field
18406b35ec0Smatthias.ringwald     linked_item_t    item;
185c01e9cbdSmatthias.ringwald 
186c01e9cbdSmatthias.ringwald     // remote side
18716833f0aSmatthias.ringwald     bd_addr_t address;
18806b35ec0Smatthias.ringwald 
18906b35ec0Smatthias.ringwald     // module handle
19016833f0aSmatthias.ringwald     hci_con_handle_t con_handle;
191c01e9cbdSmatthias.ringwald 
192c8e4258aSmatthias.ringwald     // state
193c8e4258aSmatthias.ringwald     CONNECTION_STATE state;
194c8e4258aSmatthias.ringwald 
19506b35ec0Smatthias.ringwald     // errands
1967fde4af9Smatthias.ringwald     hci_authentication_flags_t authentication_flags;
197c8e4258aSmatthias.ringwald 
198*c785ef68Smatthias.ringwald     timer_source_t timeout;
199*c785ef68Smatthias.ringwald 
2000bece81eSmatthias.ringwald #ifdef HAVE_TIME
201ee091cf1Smatthias.ringwald     // timer
202ee091cf1Smatthias.ringwald     struct timeval timestamp;
2030bece81eSmatthias.ringwald #endif
204*c785ef68Smatthias.ringwald #ifdef EMBEDDED
205*c785ef68Smatthias.ringwald     uint32_t timeout; // timeout in system ticks
206*c785ef68Smatthias.ringwald #endif
207ee091cf1Smatthias.ringwald 
208d55db49eSmatthias.ringwald     // ACL packet recombination
20943bb3ff1Smatthias.ringwald     uint8_t  acl_recombination_buffer[4 + HCI_ACL_3DH5_SIZE];
210c1012e46Smatthias.ringwald     uint16_t acl_recombination_pos;
211d55db49eSmatthias.ringwald     uint16_t acl_recombination_length;
212d55db49eSmatthias.ringwald 
21356cf178bSmatthias.ringwald     // number ACL packets sent to controller
21456cf178bSmatthias.ringwald     uint8_t num_acl_packets_sent;
21556cf178bSmatthias.ringwald 
21616833f0aSmatthias.ringwald } hci_connection_t;
21716833f0aSmatthias.ringwald 
21806b35ec0Smatthias.ringwald /**
21906b35ec0Smatthias.ringwald  * main data structure
22006b35ec0Smatthias.ringwald  */
22116833f0aSmatthias.ringwald typedef struct {
22206b35ec0Smatthias.ringwald     // transport component with configuration
22316833f0aSmatthias.ringwald     hci_transport_t  * hci_transport;
22411e23e5fSmatthias.ringwald     void             * config;
22511e23e5fSmatthias.ringwald 
22606b35ec0Smatthias.ringwald     // hardware power controller
22706b35ec0Smatthias.ringwald     bt_control_t     * control;
22806b35ec0Smatthias.ringwald 
22906b35ec0Smatthias.ringwald     // list of existing baseband connections
230fe1ed1b8Smatthias.ringwald     linked_list_t     connections;
23116833f0aSmatthias.ringwald 
23206b35ec0Smatthias.ringwald     // single buffer for HCI Command assembly
233124062bcSmatthias.ringwald     uint8_t          hci_cmd_buffer[3+255]; // opcode (16), len(8)
23406b35ec0Smatthias.ringwald 
23516833f0aSmatthias.ringwald     /* host to controller flow control */
23616833f0aSmatthias.ringwald     uint8_t  num_cmd_packets;
237e2edc0c3Smatthias.ringwald     // uint8_t  total_num_cmd_packets;
238e2edc0c3Smatthias.ringwald     uint8_t  total_num_acl_packets;
239e2edc0c3Smatthias.ringwald     uint16_t acl_data_packet_length;
24016833f0aSmatthias.ringwald 
24116833f0aSmatthias.ringwald     /* callback to L2CAP layer */
2422718e2e7Smatthias.ringwald     void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
24316833f0aSmatthias.ringwald 
24429d53098Smatthias.ringwald     /* remote device db */
24529d53098Smatthias.ringwald     remote_device_db_t *remote_device_db;
24629d53098Smatthias.ringwald 
2473429f56bSmatthias.ringwald     /* hci state machine */
2483429f56bSmatthias.ringwald     HCI_STATE state;
2493429f56bSmatthias.ringwald     uint8_t   substate;
2503429f56bSmatthias.ringwald     uint8_t   cmds_ready;
2513429f56bSmatthias.ringwald 
252381fbed8Smatthias.ringwald     /* */
253381fbed8Smatthias.ringwald     uint8_t   discoverable;
2543dbb95f9Smatthias.ringwald 
2553dbb95f9Smatthias.ringwald     // buffer for single connection decline
2563dbb95f9Smatthias.ringwald     uint8_t   decline_reason;
2573dbb95f9Smatthias.ringwald     bd_addr_t decline_addr;
258381fbed8Smatthias.ringwald 
25916833f0aSmatthias.ringwald } hci_stack_t;
26016833f0aSmatthias.ringwald 
261945627e4Smatthias.ringwald // create and send hci command packets based on a template and a list of parameters
262945627e4Smatthias.ringwald uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...);
263df33c7a6Smatthias.ringwald uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr);
264945627e4Smatthias.ringwald 
265475c8125Smatthias.ringwald // set up HCI
266404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db);
2672718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size));
26869415922Smatthias.ringwald void hci_close(void);
26916833f0aSmatthias.ringwald 
270574e07e1Smatthias.ringwald // power and inquriy scan control
271475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE mode);
272574e07e1Smatthias.ringwald void hci_discoverable_control(uint8_t enable);
273475c8125Smatthias.ringwald 
2743429f56bSmatthias.ringwald /**
275c8e4258aSmatthias.ringwald  * run the hci control loop once
2763429f56bSmatthias.ringwald  */
27769415922Smatthias.ringwald void hci_run(void);
2781f504dbdSmatthias.ringwald 
2791cd208adSmatthias.ringwald // create and send hci command packets based on a template and a list of parameters
280df33c7a6Smatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...);
281d8905019Smatthias.ringwald 
282d8905019Smatthias.ringwald // send complete CMD packet
28349857181Smatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size);
284554588a5Smatthias.ringwald 
28543625864Smatthias.ringwald // send ACL packet
28643625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size);
28706b35ec0Smatthias.ringwald 
288c24735b1Smatthias.ringwald // non-blocking UART driver needs
289c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type);
290c24735b1Smatthias.ringwald 
291645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle);
292998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle);
29369415922Smatthias.ringwald uint8_t hci_number_free_acl_slots(void);
29480ca58a0Smatthias.ringwald int     hci_authentication_active_for_handle(hci_con_handle_t handle);
295c12e46e7Smatthias.ringwald void    hci_drop_link_key_for_bd_addr(bd_addr_t *addr);
29669415922Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(void);
297645658c9Smatthias.ringwald 
29806b35ec0Smatthias.ringwald //
29969415922Smatthias.ringwald void hci_emit_state(void);
30017f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status);
301ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
3023c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason);
30369415922Smatthias.ringwald void hci_emit_nr_connections_changed(void);
30469415922Smatthias.ringwald void hci_emit_hci_open_failed(void);
30569415922Smatthias.ringwald void hci_emit_btstack_version(void);
3062ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled);
307627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name);
308381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled);
309e3b6fe7eSmatthias.ringwald 
310e3b6fe7eSmatthias.ringwald #if defined __cplusplus
311e3b6fe7eSmatthias.ringwald }
312e3b6fe7eSmatthias.ringwald #endif
313