xref: /btstack/src/hci.h (revision b9fa6cb54b40ce580a9c2995e428445333cc2c6c)
1 /*
2  *  hci.h
3  *
4  *  Created by Matthias Ringwald on 4/29/09.
5  *
6  */
7 
8 #pragma once
9 
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 
14 #include "hci_transport.h"
15 #include "bt_control.h"
16 
17 // helper for BT little endian format
18 #define READ_BT_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[pos+1]) << 8))
19 #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16))
20 #define READ_BT_32( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16) | (((uint32_t) buffer[pos+3])) << 24)
21 
22 // calculate combined ogf/ocf value
23 #define OPCODE(ogf, ocf) (ocf | ogf << 10)
24 
25 // get HCI CMD OGF
26 #define READ_CMD_OGF(buffer) (buffer[1] >> 2)
27 #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0])
28 
29 // packet header lengh
30 #define HCI_CMD_DATA_PKT_HDR	  0x03
31 #define HCI_ACL_DATA_PKT_HDR	  0x04
32 #define HCI_SCO_DATA_PKT_HDR	  0x03
33 #define HCI_EVENT_PKT_HDR         0x02
34 
35 // OGFs
36 #define OGF_LINK_CONTROL 0x01
37 #define OGF_CONTROLLER_BASEBAND 0x03
38 #define OGF_INFORMATIONAL_PARAMETERS 0x04
39 #define OGF_BTSTACK 0x3d
40 #define OGF_VENDOR  0x3f
41 
42 // Events from host controller to host
43 #define HCI_EVENT_INQUIRY_COMPLETE				           0x01
44 #define HCI_EVENT_INQUIRY_RESULT				           0x02
45 #define HCI_EVENT_CONNECTION_COMPLETE			           0x03
46 #define HCI_EVENT_CONNECTION_REQUEST			           0x04
47 #define HCI_EVENT_DISCONNECTION_COMPLETE		      	   0x05
48 #define HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT            0x06
49 #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE	           0x07
50 #define HCI_EVENT_ENCRIPTION_CHANGE                        0x08
51 #define HCI_EVENT_CHANGE_CONNECTION_LINK_KEY_COMPLETE      0x09
52 #define HCI_EVENT_MASTER_LINK_KEY_COMPLETE                 0x0A
53 #define HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE  0x0B
54 #define HCI_EVENT_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
55 #define HCI_EVENT_QOS_SETUP_COMPLETE			           0x0D
56 #define HCI_EVENT_COMMAND_COMPLETE				           0x0E
57 #define HCI_EVENT_COMMAND_STATUS				           0x0F
58 #define HCI_EVENT_HARDWARE_ERROR                           0x10
59 #define HCI_EVENT_FLUSH_OCCURED                            0x11
60 #define HCI_EVENT_ROLE_CHANGE				               0x12
61 #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS	      	   0x13
62 #define HCI_EVENT_MODE_CHANGE_EVENT                        0x14
63 #define HCI_EVENT_RETURN_LINK_KEYS                         0x15
64 #define HCI_EVENT_PIN_CODE_REQUEST                         0x16
65 #define HCI_EVENT_LINK_KEY_REQUEST                         0x17
66 #define HCI_EVENT_LINK_KEY_NOTIFICATION                    0x18
67 #define HCI_EVENT_DATA_BUFFER_OVERFLOW                     0x1A
68 #define HCI_EVENT_MAX_SLOTS_CHANGED			               0x1B
69 #define HCI_EVENT_READ_CLOCK_OFFSET_COMPLETE               0x1C
70 #define NECTEVENT_ION_PACKET_TYPE_CHANGED                  0x1D
71 #define HCI_EVENT_INQUIRY_RESULT_WITH_RSSI		      	   0x22
72 #define HCI_EVENT_VENDOR_SPECIFIC				           0xFF
73 
74 // events from BTstack for application/client lib
75 #define HCI_EVENT_BTSTACK_WORKING                          0x80
76 #define HCI_EVENT_BTSTACK_STATE                            0x81
77 
78 // cmds for BTstack
79 #define HCI_BTSTACK_GET_STATE                              0x01
80 
81 #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode)
82 
83 
84 /**
85  * Default INQ Mode
86  */
87 #define HCI_INQUIRY_LAP 0x9E8B33L  // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
88 
89 /**
90  * @brief Length of a bluetooth device address.
91  */
92 #define BD_ADDR_LEN 6
93 typedef uint8_t bd_addr_t[BD_ADDR_LEN];
94 
95 /**
96  * @brief The link key type
97  */
98 #define LINK_KEY_LEN 16
99 typedef uint8_t link_key_t[LINK_KEY_LEN];
100 
101 /**
102  * @brief hci connection handle type
103  */
104 typedef uint16_t hci_con_handle_t;
105 
106 typedef enum {
107     HCI_POWER_OFF = 0,
108     HCI_POWER_ON
109 } HCI_POWER_MODE;
110 
111 typedef enum {
112     HCI_STATE_OFF = 0,
113     HCI_STATE_INITIALIZING,
114     HCI_STATE_WORKING,
115     HCI_STATE_HALTING
116 } HCI_STATE;
117 
118 typedef struct {
119     uint16_t    opcode;
120     const char *format;
121 } hci_cmd_t;
122 
123 typedef enum {
124     SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0,
125     SEND_PIN_CODE_RESPONSE = 1 << 1
126 } hci_connection_flags_t;
127 
128 typedef struct hci_connection {
129     // linked list
130     struct hci_connection * next;
131 
132     // remote side
133     bd_addr_t address;
134     hci_con_handle_t con_handle;
135 
136     // hci state machine
137     hci_connection_flags_t flags;
138 
139 } hci_connection_t;
140 
141 
142 typedef struct {
143 
144     hci_transport_t  * hci_transport;
145     bt_control_t     * control;
146     void             * config;
147 
148     uint8_t          * hci_cmd_buffer;
149     hci_connection_t * connections;
150 
151     /* host to controller flow control */
152     uint8_t  num_cmd_packets;
153     uint8_t  num_acl_packets;
154 
155     /* callback to L2CAP layer */
156     void (*event_packet_handler)(uint8_t *packet, uint16_t size);
157     void (*acl_packet_handler)  (uint8_t *packet, uint16_t size);
158 
159     /* hci state machine */
160     HCI_STATE state;
161     uint8_t   substate;
162     uint8_t   cmds_ready;
163 
164 } hci_stack_t;
165 
166 
167 // set up HCI
168 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control);
169 
170 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size));
171 
172 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size));
173 
174 // power control
175 int hci_power_control(HCI_POWER_MODE mode);
176 
177 /**
178  * run the hci daemon loop once
179  *
180  * @return 0 or next timeout
181  */
182 uint32_t hci_run();
183 
184 //
185 void hexdump(void *data, int size);
186 
187 // create and send hci command packets based on a template and a list of parameters
188 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, va_list argptr);
189 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...);
190 int hci_send_cmd(hci_cmd_t *cmd, ...);
191 int hci_send_cmd_packet(uint8_t *packet, int size);
192 
193 // send ACL packet
194 int hci_send_acl_packet(uint8_t *packet, int size);
195 
196 // helper
197 extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
198 extern void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
199 extern void bt_flip_addr(bd_addr_t dest, bd_addr_t src);
200 
201 // HCI Commands - see hci.c for info on parameters
202 extern hci_cmd_t hci_inquiry;
203 extern hci_cmd_t hci_inquiry_cancel;
204 extern hci_cmd_t hci_link_key_request_negative_reply;
205 extern hci_cmd_t hci_pin_code_request_reply;
206 extern hci_cmd_t hci_set_event_mask;
207 extern hci_cmd_t hci_reset;
208 extern hci_cmd_t hci_create_connection;
209 extern hci_cmd_t hci_host_buffer_size;
210 extern hci_cmd_t hci_write_authentication_enable;
211 extern hci_cmd_t hci_write_local_name;
212 extern hci_cmd_t hci_write_page_timeout;
213 extern hci_cmd_t hci_write_class_of_device;
214 extern hci_cmd_t hci_remote_name_request;
215 extern hci_cmd_t hci_remote_name_request_cancel;
216 extern hci_cmd_t hci_read_bd_addr;
217 extern hci_cmd_t hci_delete_stored_link_key;
218 extern hci_cmd_t hci_write_scan_enable;
219 extern hci_cmd_t hci_accept_connection_request;
220 extern hci_cmd_t hci_write_inquiry_mode;
221 extern hci_cmd_t hci_write_extended_inquiry_response;
222 extern hci_cmd_t hci_write_simple_pairing_mode;
223 
224 // BTSTACK client/server commands - see hci.c for info on parameters
225 extern hci_cmd_t hci_get_btstack_state;