xref: /btstack/src/hci.h (revision 2ed6235c6196977fad37720ae86bdb392f9820c1)
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 
46 #include <stdint.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 
50 // packet header lenghts
51 #define HCI_CMD_DATA_PKT_HDR	  0x03
52 #define HCI_ACL_DATA_PKT_HDR	  0x04
53 #define HCI_SCO_DATA_PKT_HDR	  0x03
54 #define HCI_EVENT_PKT_HDR         0x02
55 
56 // OGFs
57 #define OGF_LINK_CONTROL 0x01
58 #define OGF_CONTROLLER_BASEBAND 0x03
59 #define OGF_INFORMATIONAL_PARAMETERS 0x04
60 #define OGF_BTSTACK 0x3d
61 #define OGF_VENDOR  0x3f
62 
63 // cmds for BTstack
64 // get state: @returns HCI_STATE
65 #define BTSTACK_GET_STATE                                  0x01
66 
67 // set power mode: @param HCI_POWER_MODE
68 #define BTSTACK_SET_POWER_MODE                             0x02
69 
70 // set capture mode: @param on
71 #define BTSTACK_SET_ACL_CAPTURE_MODE                       0x03
72 
73 // get BTstack version
74 #define BTSTACK_GET_VERSION                                0x04
75 
76 // get system Bluetooth state
77 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED               0x05
78 
79 // set system Bluetooth state
80 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED               0x06
81 
82 // create l2cap channel: @param bd_addr(48), psm (16)
83 #define L2CAP_CREATE_CHANNEL                               0x20
84 
85 // disconnect l2cap disconnect, @param channel(16), reason(8)
86 #define L2CAP_DISCONNECT                                   0x21
87 
88 //
89 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode)
90 
91 // data: event(8)
92 #define DAEMON_EVENT_CONNECTION_CLOSED                     0x70
93 
94 // data: event(8), nr_connections(8)
95 #define DAEMON_NR_CONNECTIONS_CHANGED                      0x71
96 
97 
98 /**
99  * Connection State
100  */
101 typedef enum {
102     SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0,
103     SEND_PIN_CODE_RESPONSE = 1 << 1
104 } hci_connection_flags_t;
105 
106 typedef enum {
107     SENT_CREATE_CONNECTION = 1,
108     RECEIVED_CONNECTION_REQUEST,
109     ACCEPTED_CONNECTION_REQUEST,
110     REJECTED_CONNECTION_REQUEST,
111     OPEN,
112     SENT_DISCONNECT
113 } CONNECTION_STATE;
114 
115 typedef enum {
116     BLUETOOTH_OFF = 1,
117     BLUETOOTH_ON,
118     BLUETOOTH_ACTIVE
119 } BLUETOOTH_STATE;
120 
121 typedef struct {
122     // linked list - assert: first field
123     linked_item_t    item;
124 
125     // remote side
126     bd_addr_t address;
127 
128     // module handle
129     hci_con_handle_t con_handle;
130 
131     // state
132     CONNECTION_STATE state;
133 
134     // errands
135     hci_connection_flags_t flags;
136 
137     // timer
138     timer_source_t timeout;
139     struct timeval timestamp;
140 
141 } hci_connection_t;
142 
143 /**
144  * main data structure
145  */
146 typedef struct {
147     // transport component with configuration
148     hci_transport_t  * hci_transport;
149     void             * config;
150 
151     // hardware power controller
152     bt_control_t     * control;
153 
154     // list of existing baseband connections
155     linked_list_t     connections;
156 
157     // single buffer for HCI Command assembly
158     uint8_t          * hci_cmd_buffer;
159 
160     /* host to controller flow control */
161     uint8_t  num_cmd_packets;
162     uint8_t  num_acl_packets;
163 
164     /* callback to L2CAP layer */
165     void (*event_packet_handler)(uint8_t *packet, uint16_t size);
166     void (*acl_packet_handler)  (uint8_t *packet, uint16_t size);
167 
168     /* hci state machine */
169     HCI_STATE state;
170     uint8_t   substate;
171     uint8_t   cmds_ready;
172 
173 } hci_stack_t;
174 
175 // create and send hci command packets based on a template and a list of parameters
176 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...);
177 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, va_list argptr);
178 
179 // set up HCI
180 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control);
181 
182 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size));
183 
184 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size));
185 
186 // power control
187 int hci_power_control(HCI_POWER_MODE mode);
188 
189 /**
190  * run the hci control loop once
191  */
192 void hci_run();
193 
194 // create and send hci command packets based on a template and a list of parameters
195 int hci_send_cmd(hci_cmd_t *cmd, ...);
196 
197 // send complete CMD packet
198 int hci_send_cmd_packet(uint8_t *packet, int size);
199 
200 // send ACL packet
201 int hci_send_acl_packet(uint8_t *packet, int size);
202 
203 //
204 void hci_emit_state();
205 void hci_emit_connection_complete(hci_connection_t *conn);
206 void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
207 void hci_emit_nr_connections_changed();
208 void hci_emit_hci_open_failed();
209 void hci_emit_btstack_version();
210 void hci_emit_system_bluetooth_enabled(uint8_t enabled);
211