xref: /btstack/src/hci.h (revision c01e9cbd6b0ba67c9c934852cf656b36693806ad)
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 
13 #include "hci_transport.h"
14 
15 
16 // helper for BT little endian format
17 #define READ_BT_16( buffer, pos) (buffer[pos] | (buffer[pos+1] << 8))
18 #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16))
19 #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)
20 
21 // #define STORE_BT_16( buffer, pos, value ) { buffer[pos] = (value) & 0xff; buffer[pos+1] = (value) >> 8; }
22 
23 
24 // packet headers
25 #define HCI_CMD_DATA_PKT_HDR	  0x03
26 #define HCI_ACL_DATA_PKT_HDR	  0x04
27 #define HCI_SCO_DATA_PKT_HDR	  0x03
28 #define HCI_EVENT_PKT_HDR         0x02
29 
30 
31 // Events from host controller to host
32 #define HCI_EVENT_INQUIRY_COMPLETE				           0x01
33 #define HCI_EVENT_INQUIRY_RESULT				           0x02
34 #define HCI_EVENT_CONNECTION_COMPLETE			           0x03
35 #define HCI_EVENT_CONNECTION_REQUEST			           0x04
36 #define HCI_EVENT_DISCONNECTION_COMPLETE		      	   0x05
37 #define HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT            0x06
38 #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE	           0x07
39 #define HCI_EVENT_ENCRIPTION_CHANGE                        0x08
40 #define HCI_EVENT_CHANGE_CONNECTION_LINK_KEY_COMPLETE      0x09
41 #define HCI_EVENT_MASTER_LINK_KEY_COMPLETE                 0x0A
42 #define HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE  0x0B
43 #define HCI_EVENT_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
44 #define HCI_EVENT_QOS_SETUP_COMPLETE			           0x0D
45 #define HCI_EVENT_COMMAND_COMPLETE				           0x0E
46 #define HCI_EVENT_COMMAND_STATUS				           0x0F
47 #define HCI_EVENT_HARDWARE_ERROR                           0x10
48 #define HCI_EVENT_FLUSH_OCCURED                            0x11
49 #define HCI_EVENT_ROLE_CHANGE				               0x12
50 #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS	      	   0x13
51 #define HCI_EVENT_MODE_CHANGE_EVENT                        0x14
52 #define HCI_EVENT_RETURN_LINK_KEYS                         0x15
53 #define HCI_EVENT_PIN_CODE_REQUEST                         0x16
54 #define HCI_EVENT_LINK_KEY_REQUEST                         0x17
55 #define HCI_EVENT_LINK_KEY_NOTIFICATION                    0x18
56 #define HCI_EVENT_DATA_BUFFER_OVERFLOW                     0x1A
57 #define HCI_EVENT_MAX_SLOTS_CHANGED			               0x1B
58 #define HCI_EVENT_READ_CLOCK_OFFSET_COMPLETE               0x1C
59 #define NECTEVENT_ION_PACKET_TYPE_CHANGED                  0x1D
60 #define HCI_EVENT_INQUIRY_RESULT_WITH_RSSI		      	   0x22
61 #define HCI_EVENT_VENDOR_SPECIFIC				           0xFF
62 
63 // events from BTstack for application/client lib
64 #define BTSTACK_EVENT_HCI_WORKING                          0x80
65 
66 #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode)
67 
68 /**
69  * @brief Length of a bluetooth device address.
70  */
71 #define BD_ADDR_LEN 6
72 typedef uint8_t bd_addr_t[BD_ADDR_LEN];
73 
74 /**
75  * @brief The link key type
76  */
77 #define LINK_KEY_LEN 16
78 typedef uint8_t link_key_t[LINK_KEY_LEN];
79 
80 /**
81  * @brief hci connection handle type
82  */
83 typedef uint16_t hci_con_handle_t;
84 
85 typedef enum {
86     HCI_POWER_OFF = 0,
87     HCI_POWER_ON
88 } HCI_POWER_MODE;
89 
90 typedef enum {
91     HCI_STATE_OFF = 0,
92     HCI_STATE_INITIALIZING,
93     HCI_STATE_WORKING,
94     HCI_STATE_HALTING
95 } HCI_STATE;
96 
97 typedef struct {
98     uint16_t    opcode;
99     const char *format;
100 } hci_cmd_t;
101 
102 typedef enum {
103     SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0,
104     SEND_PIN_CODE_RESPONSE = 1 << 1
105 } hci_connection_flags_t;
106 
107 typedef struct hci_connection {
108     // linked list
109     struct hci_connection * next;
110 
111     // remote side
112     bd_addr_t address;
113     hci_con_handle_t con_handle;
114 
115     // hci state machine
116     hci_connection_flags_t flags;
117 
118 } hci_connection_t;
119 
120 
121 typedef struct {
122 
123     hci_transport_t  * hci_transport;
124     uint8_t          * hci_cmd_buffer;
125     hci_connection_t * connections;
126 
127     /* host to controller flow control */
128     uint8_t  num_cmd_packets;
129     uint8_t  num_acl_packets;
130 
131     /* callback to L2CAP layer */
132     void (*event_packet_handler)(uint8_t *packet, int size);
133     void (*acl_packet_handler)  (uint8_t *packet, int size);
134 
135     /* hci state machine */
136     HCI_STATE state;
137     uint8_t   substate;
138     uint8_t   cmds_ready;
139 
140 } hci_stack_t;
141 
142 
143 // set up HCI
144 void hci_init(hci_transport_t *transport, void *config);
145 
146 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size));
147 
148 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size));
149 
150 // power control
151 int hci_power_control(HCI_POWER_MODE mode);
152 
153 /**
154  * run the hci daemon loop once
155  *
156  * @return 0 or next timeout
157  */
158 uint32_t hci_run();
159 
160 //
161 void hexdump(uint8_t *data, int size);
162 
163 // create and send hci command packet based on a template and a list of parameters
164 int hci_send_cmd(hci_cmd_t *cmd, ...);
165 
166 // send ACL packet
167 int hci_send_acl_packet(uint8_t *packet, int size);
168 
169 // helper
170 extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
171 
172 extern hci_cmd_t hci_inquiry;
173 extern hci_cmd_t hci_link_key_request_negative_reply;
174 extern hci_cmd_t hci_pin_code_request_reply;
175 extern hci_cmd_t hci_reset;
176 extern hci_cmd_t hci_create_connection;
177 extern hci_cmd_t hci_host_buffer_size;
178 extern hci_cmd_t hci_write_authentication_enable;
179 extern hci_cmd_t hci_write_page_timeout;
180 
181 #define HCI_INQUIRY_LAP 0x9E8B33L  // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
182