xref: /btstack/src/bluetooth.h (revision 8799c043586722debab309c79288c197c9840f38)
1 /*
2  * Copyright (C) 2015 BlueKitchen GmbH
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  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  * bluetooth.h
40  *
41  * Numbers defined or derived from the official Bluetooth specification
42  */
43 
44 #ifndef __BLUETOOTH_H
45 #define __BLUETOOTH_H
46 
47 #include <stdint.h>
48 
49 /**
50  * HCI Transport
51  */
52 
53 /**
54  * packet types - used in BTstack and over the H4 UART interface
55  */
56 #define HCI_COMMAND_DATA_PACKET 0x01
57 #define HCI_ACL_DATA_PACKET     0x02
58 #define HCI_SCO_DATA_PACKET     0x03
59 #define HCI_EVENT_PACKET        0x04
60 
61 // packet header sizes
62 #define HCI_CMD_HEADER_SIZE          3
63 #define HCI_ACL_HEADER_SIZE   	     4
64 #define HCI_SCO_HEADER_SIZE  	     3
65 #define HCI_EVENT_HEADER_SIZE        2
66 
67 /**
68  * HCI Layer
69  */
70 
71 //
72 // Error Codes
73 //
74 
75 // from Bluetooth Core Specification
76 #define ERROR_CODE_UNKNOWN_HCI_COMMAND                     0x01
77 #define ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER           0x02
78 #define ERROR_CODE_HARDWARE_FAILURE                        0x03
79 #define ERROR_CODE_PAGE_TIMEOUT                            0x04
80 #define ERROR_CODE_AUTHENTICATION_FAILURE                          0x05
81 #define ERROR_CODE_PIN_OR_KEY_MISSING                      0x06
82 #define ERROR_CODE_MEMORY_CAPACITY_EXCEEDED                    0x07
83 #define ERROR_CODE_CONNECTION_TIMEOUT                      0x08
84 #define ERROR_CODE_CONNECTION_LIMIT_EXCEEDED               0x09
85 #define ERROR_CODE_SYNCHRONOUS_CONNECTION_LIMIT_TO_A_DEVICE_EXCEEDED  0x0A
86 #define ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS           0x0B
87 #define ERROR_CODE_COMMAND_DISALLOWED                      0x0C
88 #define ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES 0x0D
89 #define ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS  0x0E
90 #define ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR 0x0F
91 #define ERROR_CODE_CONNECTION_ACCEPT_TIMEOUT_EXCEEDED      0x10
92 #define ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE  0x11
93 #define ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS          0x12
94 #define ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION       0x13
95 #define ERROR_CODE_REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES 0x14
96 #define ERROR_CODE_REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF     0x15
97 #define ERROR_CODE_CONNECTION_TERMINATED_BY_LOCAL_HOST     0x16
98 #define ERROR_CODE_REPEATED_ATTEMPTS                       0x17
99 #define ERROR_CODE_PAIRING_NOT_ALLOWED                     0x18
100 #define ERROR_CODE_UNKNOWN_LMP_PDU                         0x19
101 #define ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE 0x1A
102 #define ERROR_CODE_SCO_OFFSET_REJECTED                     0x1B
103 #define ERROR_CODE_SCO_INTERVAL_REJECTED                   0x1C
104 #define ERROR_CODE_SCO_AIR_MODE_REJECTED                   0x1D
105 #define ERROR_CODE_INVALID_LMP_PARAMETERS_INVALID_LL_PARAMETERS 0x1E
106 #define ERROR_CODE_UNSPECIFIED_ERROR                       0x1F
107 #define ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE 0x20
108 #define ERROR_CODE_ROLE_CHANGE_NOT_ALLOWED                 0x21
109 #define ERROR_CODE_LMP_RESPONSE_TIMEOUT_LL_RESPONSE_TIMEOUT 0x22
110 #define ERROR_CODE_LMP_ERROR_TRANSACTION_COLLISION         0x23
111 #define ERROR_CODE_LMP_PDU_NOT_ALLOWED                     0x24
112 #define ERROR_CODE_ENCRYPTION_MODE_NOT_ACCEPTABLE          0x25
113 #define ERROR_CODE_LINK_KEY_CANNOT_BE_CHANGED              0x26
114 #define ERROR_CODE_REQUESTED_QOS_NOT_SUPPORTED             0x27
115 #define ERROR_CODE_INSTANT_PASSED                          0x28
116 #define ERROR_CODE_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED     0x29
117 #define ERROR_CODE_DIFFERENT_TRANSACTION_COLLISION         0x2A
118 #define ERROR_CODE_RESERVED                                0x2B
119 #define ERROR_CODE_QOS_UNACCEPTABLE_PARAMETER              0x2C
120 #define ERROR_CODE_QOS_REJECTED                            0x2D
121 #define ERROR_CODE_CHANNEL_CLASSIFICATION_NOT_SUPPORTED    0x2E
122 #define ERROR_CODE_INSUFFICIENT_SECURITY                   0x2F
123 #define ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE        0x30
124 // #define ERROR_CODE_RESERVED
125 #define ERROR_CODE_ROLE_SWITCH_PENDING                     0x32
126 // #define ERROR_CODE_RESERVED
127 #define ERROR_CODE_RESERVED_SLOT_VIOLATION                 0x34
128 #define ERROR_CODE_ROLE_SWITCH_FAILED                      0x35
129 #define ERROR_CODE_EXTENDED_INQUIRY_RESPONSE_TOO_LARGE     0x36
130 #define ERROR_CODE_SECURE_SIMPLE_PAIRING_NOT_SUPPORTED_BY_HOST 0x37
131 #define ERROR_CODE_HOST_BUSY_PAIRING                       0x38
132 #define ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND 0x39
133 #define ERROR_CODE_CONTROLLER_BUSY                         0x3A
134 #define ERROR_CODE_UNACCEPTABLE_CONNECTION_PARAMETERS      0x3B
135 #define ERROR_CODE_DIRECTED_ADVERTISING_TIMEOUT            0x3C
136 #define ERROR_CODE_CONNECTION_TERMINATED_DUE_TO_MIC_FAILURE 0x3D
137 #define ERROR_CODE_CONNECTION_FAILED_TO_BE_ESTABLISHED     0x3E
138 #define ERROR_CODE_MAC_CONNECTION_FAILED                   0x3F
139 #define ERROR_CODE_COARSE_CLOCK_ADJUSTMENT_REJECTED_BUT_WILL_TRY_TO_ADJUST_USING_CLOCK_DRAGGING 0x40
140 
141 // HCI roles
142 #define HCI_ROLE_MASTER 0
143 #define HCI_ROLE_SLAVE  1
144 
145 // packet sizes (max payload)
146 #define HCI_ACL_DM1_SIZE            17
147 #define HCI_ACL_DH1_SIZE            27
148 #define HCI_ACL_2DH1_SIZE           54
149 #define HCI_ACL_3DH1_SIZE           83
150 #define HCI_ACL_DM3_SIZE           121
151 #define HCI_ACL_DH3_SIZE           183
152 #define HCI_ACL_DM5_SIZE           224
153 #define HCI_ACL_DH5_SIZE           339
154 #define HCI_ACL_2DH3_SIZE          367
155 #define HCI_ACL_3DH3_SIZE          552
156 #define HCI_ACL_2DH5_SIZE          679
157 #define HCI_ACL_3DH5_SIZE         1021
158 
159 #define HCI_EVENT_PAYLOAD_SIZE     255
160 #define HCI_CMD_PAYLOAD_SIZE       255
161 
162 #define LE_ADVERTISING_DATA_SIZE    31
163 
164 /**
165  * Default INQ Mode
166  */
167 #define HCI_INQUIRY_LAP 0x9E8B33L  // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
168 
169 /**
170  * SSP IO Capabilities
171  */
172 #define SSP_IO_CAPABILITY_DISPLAY_ONLY   0
173 #define SSP_IO_CAPABILITY_DISPLAY_YES_NO 1
174 #define SSP_IO_CAPABILITY_KEYBOARD_ONLY  2
175 #define SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT 3
176 #define SSP_IO_CAPABILITY_UNKNOWN 0xff
177 
178 
179 /**
180  * SSP Authentication Requirements, see IO Capability Request Reply Commmand
181  */
182 
183 // Numeric comparison with automatic accept allowed.
184 #define SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_NO_BONDING 0x00
185 
186 // Use IO Capabilities to deter- mine authentication procedure
187 #define SSP_IO_AUTHREQ_MITM_PROTECTION_REQUIRED_NO_BONDING 0x01
188 
189 // Numeric compar- ison with automatic accept allowed.
190 #define SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING 0x02
191 
192 // Use IO Capabilities to determine authentication procedure
193 #define SSP_IO_AUTHREQ_MITM_PROTECTION_REQUIRED_DEDICATED_BONDING 0x03
194 
195 // Numeric Compari- son with automatic accept allowed.
196 #define SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING 0x04
197 
198 // Use IO capabilities to determine authentication procedure.
199 #define SSP_IO_AUTHREQ_MITM_PROTECTION_REQUIRED_GENERAL_BONDING 0x05
200 
201 
202 // OGFs
203 #define OGF_LINK_CONTROL          0x01
204 #define OGF_LINK_POLICY           0x02
205 #define OGF_CONTROLLER_BASEBAND   0x03
206 #define OGF_INFORMATIONAL_PARAMETERS 0x04
207 #define OGF_STATUS_PARAMETERS     0x05
208 #define OGF_TESTING               0x06
209 #define OGF_LE_CONTROLLER 0x08
210 #define OGF_VENDOR  0x3f
211 
212 
213 // Events from host controller to host
214 
215 /**
216  * @format 1
217  * @param status
218  */
219 #define HCI_EVENT_INQUIRY_COMPLETE                         0x01
220 // no format yet, can contain multiple results
221 
222 /**
223  * @format 1B11132
224  * @param num_responses
225  * @param bd_addr
226  * @param page_scan_repetition_mode
227  * @param reserved1
228  * @param reserved2
229  * @param class_of_device
230  * @param clock_offset
231  */
232 #define HCI_EVENT_INQUIRY_RESULT                           0x02
233 
234 /**
235  * @format 12B11
236  * @param status
237  * @param connection_handle
238  * @param bd_addr
239  * @param link_type
240  * @param encryption_enabled
241  */
242 #define HCI_EVENT_CONNECTION_COMPLETE                      0x03
243 /**
244  * @format B31
245  * @param bd_addr
246  * @param class_of_device
247  * @param link_type
248  */
249 #define HCI_EVENT_CONNECTION_REQUEST                       0x04
250 /**
251  * @format 121
252  * @param status
253  * @param connection_handle
254  * @param reason
255  */
256 #define HCI_EVENT_DISCONNECTION_COMPLETE                   0x05
257 /**
258  * @format 12
259  * @param status
260  * @param connection_handle
261  */
262 #define HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT            0x06
263 /**
264  * @format 1BN
265  * @param status
266  * @param bd_addr
267  * @param remote_name
268  */
269 #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE             0x07
270 /**
271  * @format 121
272  * @param status
273  * @param connection_handle
274  * @param encryption_enabled
275  */
276 #define HCI_EVENT_ENCRYPTION_CHANGE                        0x08
277 /**
278  * @format 12
279  * @param status
280  * @param connection_handle
281  */
282 #define HCI_EVENT_CHANGE_CONNECTION_LINK_KEY_COMPLETE      0x09
283 /**
284  * @format 121
285  * @param status
286  * @param connection_handle
287  * @param key_flag
288  */
289 #define HCI_EVENT_MASTER_LINK_KEY_COMPLETE                 0x0A
290 #define HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE  0x0B
291 #define HCI_EVENT_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
292 #define HCI_EVENT_QOS_SETUP_COMPLETE                       0x0D
293 
294 /**
295  * @format 12R
296  * @param num_hci_command_packets
297  * @param command_opcode
298  * @param return_parameters
299  */
300 #define HCI_EVENT_COMMAND_COMPLETE                         0x0E
301 /**
302  * @format 112
303  * @param status
304  * @param num_hci_command_packets
305  * @param command_opcode
306  */
307 #define HCI_EVENT_COMMAND_STATUS                           0x0F
308 
309 /**
310  * @format 121
311  * @param hardware_code
312  */
313 #define HCI_EVENT_HARDWARE_ERROR                           0x10
314 
315 #define HCI_EVENT_FLUSH_OCCURED                            0x11
316 
317 /**
318  * @format 1B1
319  * @param status
320  * @param bd_addr
321  * @param role
322  */
323 #define HCI_EVENT_ROLE_CHANGE                              0x12
324 
325 #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS              0x13
326 #define HCI_EVENT_MODE_CHANGE_EVENT                        0x14
327 #define HCI_EVENT_RETURN_LINK_KEYS                         0x15
328 #define HCI_EVENT_PIN_CODE_REQUEST                         0x16
329 #define HCI_EVENT_LINK_KEY_REQUEST                         0x17
330 #define HCI_EVENT_LINK_KEY_NOTIFICATION                    0x18
331 #define HCI_EVENT_DATA_BUFFER_OVERFLOW                     0x1A
332 #define HCI_EVENT_MAX_SLOTS_CHANGED                        0x1B
333 #define HCI_EVENT_READ_CLOCK_OFFSET_COMPLETE               0x1C
334 #define HCI_EVENT_PACKET_TYPE_CHANGED                      0x1D
335 
336 /**
337  * @format 1B11321
338  * @param num_responses
339  * @param bd_addr
340  * @param page_scan_repetition_mode
341  * @param reserved
342  * @param class_of_device
343  * @param clock_offset
344  * @param rssi
345  */
346 #define HCI_EVENT_INQUIRY_RESULT_WITH_RSSI                 0x22
347 
348 /**
349  * @format 1HB111221
350  * @param status
351  * @param handle
352  * @param bd_addr
353  * @param link_type
354  * @param transmission_interval
355  * @param retransmission_interval
356  * @param rx_packet_length
357  * @param tx_packet_length
358  * @param air_mode
359  */
360 #define HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE          0x2C
361 
362 // TODO: serialize extended_inquiry_response and provide parser
363 /**
364  * @format 1B11321
365  * @param num_responses
366  * @param bd_addr
367  * @param page_scan_repetition_mode
368  * @param reserved
369  * @param class_of_device
370  * @param clock_offset
371  * @param rssi
372  */
373 #define HCI_EVENT_EXTENDED_INQUIRY_RESPONSE                0x2F
374 
375  /**
376   * @format 1H
377   * @param status
378   * @param handle
379   */
380 #define HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE          0x30
381 
382 #define HCI_EVENT_IO_CAPABILITY_REQUEST                    0x31
383 #define HCI_EVENT_IO_CAPABILITY_RESPONSE                   0x32
384 #define HCI_EVENT_USER_CONFIRMATION_REQUEST                0x33
385 #define HCI_EVENT_USER_PASSKEY_REQUEST                     0x34
386 #define HCI_EVENT_REMOTE_OOB_DATA_REQUEST                  0x35
387 #define HCI_EVENT_SIMPLE_PAIRING_COMPLETE                  0x36
388 
389 #define HCI_EVENT_LE_META                                  0x3E
390 
391 /**
392  * @format 11211B2221
393  * @param subevent_code
394  * @param status
395  * @param connection_handle
396  * @param role
397  * @param peer_address_type
398  * @param peer_address
399  * @param conn_interval
400  * @param conn_latency
401  * @param supervision_timeout
402  * @param master_clock_accuracy
403  */
404 #define HCI_SUBEVENT_LE_CONNECTION_COMPLETE                0x01
405 #define HCI_SUBEVENT_LE_ADVERTISING_REPORT                 0x02
406 #define HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE         0x03
407 #define HCI_SUBEVENT_LE_READ_REMOTE_USED_FEATURES_COMPLETE 0x04
408 #define HCI_SUBEVENT_LE_LONG_TERM_KEY_REQUEST              0x05
409 
410 // last used HCI_EVENT in 2.1 is 0x3d
411 // last used HCI_EVENT in 4.1 is 0x57
412 
413 /**
414  * L2CAP Layer
415  */
416 
417 // Fixed PSM numbers
418 #define PSM_SDP           0x01
419 #define PSM_RFCOMM 		  0x03
420 #define PSM_BNEP   	      0x0F
421 #define PSM_HID_CONTROL   0x11
422 #define PSM_HID_INTERRUPT 0x13
423 
424 
425 
426 
427 #endif