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