xref: /btstack/src/hci_cmd.c (revision e9c5f44ee8add45f6cd4be8b6faa9e09a2804fcc)
1 /*
2  * Copyright (C) 2014 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 #define __BTSTACK_FILE__ "hci_cmd.c"
39 
40 /*
41  *  hci_cmd.c
42  *
43  *  Created by Matthias Ringwald on 7/23/09.
44  */
45 
46 #include "btstack_config.h"
47 
48 #include "classic/sdp_util.h"
49 #include "hci.h"
50 #include "hci_cmd.h"
51 #include "btstack_debug.h"
52 
53 #include <string.h>
54 
55 // calculate combined ogf/ocf value
56 #define OPCODE(ogf, ocf) (ocf | ogf << 10)
57 
58 /**
59  * construct HCI Command based on template
60  *
61  * Format:
62  *   1,2,3,4: one to four byte value
63  *   H: HCI connection handle
64  *   B: Bluetooth Baseband Address (BD_ADDR)
65  *   D: 8 byte data block
66  *   E: Extended Inquiry Result
67  *   N: Name up to 248 chars, \0 terminated
68  *   P: 16 byte data block. Pairing code, Simple Pairing Hash and Randomizer
69  *   A: 31 bytes advertising data
70  *   S: Service Record (Data Element Sequence)
71  *   Q: 32 byte data block, e.g. for X and Y coordinates of P-256 public key
72  */
73 uint16_t hci_cmd_create_from_template(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr){
74 
75     hci_cmd_buffer[0] = cmd->opcode & 0xff;
76     hci_cmd_buffer[1] = cmd->opcode >> 8;
77     int pos = 3;
78 
79     const char *format = cmd->format;
80     uint16_t word;
81     uint32_t longword;
82     uint8_t * ptr;
83     while (*format) {
84         switch(*format) {
85             case '1': //  8 bit value
86             case '2': // 16 bit value
87             case 'H': // hci_handle
88                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
89                 hci_cmd_buffer[pos++] = word & 0xff;
90                 if (*format == '2') {
91                     hci_cmd_buffer[pos++] = word >> 8;
92                 } else if (*format == 'H') {
93                     // TODO implement opaque client connection handles
94                     //      pass module handle for now
95                     hci_cmd_buffer[pos++] = word >> 8;
96                 }
97                 break;
98             case '3':
99             case '4':
100                 longword = va_arg(argptr, uint32_t);
101                 // longword = va_arg(argptr, int);
102                 hci_cmd_buffer[pos++] = longword;
103                 hci_cmd_buffer[pos++] = longword >> 8;
104                 hci_cmd_buffer[pos++] = longword >> 16;
105                 if (*format == '4'){
106                     hci_cmd_buffer[pos++] = longword >> 24;
107                 }
108                 break;
109             case 'B': // bt-addr
110                 ptr = va_arg(argptr, uint8_t *);
111                 hci_cmd_buffer[pos++] = ptr[5];
112                 hci_cmd_buffer[pos++] = ptr[4];
113                 hci_cmd_buffer[pos++] = ptr[3];
114                 hci_cmd_buffer[pos++] = ptr[2];
115                 hci_cmd_buffer[pos++] = ptr[1];
116                 hci_cmd_buffer[pos++] = ptr[0];
117                 break;
118             case 'D': // 8 byte data block
119                 ptr = va_arg(argptr, uint8_t *);
120                 memcpy(&hci_cmd_buffer[pos], ptr, 8);
121                 pos += 8;
122                 break;
123             case 'E': // Extended Inquiry Information 240 octets
124                 ptr = va_arg(argptr, uint8_t *);
125                 memcpy(&hci_cmd_buffer[pos], ptr, 240);
126                 pos += 240;
127                 break;
128             case 'N': { // UTF-8 string, null terminated
129                 ptr = va_arg(argptr, uint8_t *);
130                 uint16_t len = strlen((const char*) ptr);
131                 if (len > 248) {
132                     len = 248;
133                 }
134                 memcpy(&hci_cmd_buffer[pos], ptr, len);
135                 if (len < 248) {
136                     // fill remaining space with zeroes
137                     memset(&hci_cmd_buffer[pos+len], 0, 248-len);
138                 }
139                 pos += 248;
140                 break;
141             }
142             case 'P': // 16 byte PIN code or link key
143                 ptr = va_arg(argptr, uint8_t *);
144                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
145                 pos += 16;
146                 break;
147 #ifdef ENABLE_BLE
148             case 'A': // 31 bytes advertising data
149                 ptr = va_arg(argptr, uint8_t *);
150                 memcpy(&hci_cmd_buffer[pos], ptr, 31);
151                 pos += 31;
152                 break;
153 #endif
154 #ifdef ENABLE_SDP
155             case 'S': { // Service Record (Data Element Sequence)
156                 ptr = va_arg(argptr, uint8_t *);
157                 uint16_t len = de_get_len(ptr);
158                 memcpy(&hci_cmd_buffer[pos], ptr, len);
159                 pos += len;
160                 break;
161             }
162 #endif
163 #ifdef ENABLE_LE_SECURE_CONNECTIONS
164             case 'Q':
165                 ptr = va_arg(argptr, uint8_t *);
166                 reverse_bytes(ptr, &hci_cmd_buffer[pos], 32);
167                 pos += 32;
168                 break;
169 #endif
170             default:
171                 break;
172         }
173         format++;
174     };
175     hci_cmd_buffer[2] = pos - 3;
176     return pos;
177 }
178 
179 /**
180  *  Link Control Commands
181  */
182 
183 /**
184  * @param lap
185  * @param inquiry_length
186  * @param num_responses
187  */
188 const hci_cmd_t hci_inquiry = {
189 OPCODE(OGF_LINK_CONTROL, 0x01), "311"
190 };
191 
192 /**
193  */
194 const hci_cmd_t hci_inquiry_cancel = {
195 OPCODE(OGF_LINK_CONTROL, 0x02), ""
196 };
197 
198 /**
199  * @param bd_addr
200  * @param packet_type
201  * @param page_scan_repetition_mode
202  * @param reserved
203  * @param clock_offset
204  * @param allow_role_switch
205  */
206 const hci_cmd_t hci_create_connection = {
207 OPCODE(OGF_LINK_CONTROL, 0x05), "B21121"
208 };
209 
210 /**
211  * @param handle
212  * @param reason (0x05, 0x13-0x15, 0x1a, 0x29, see Errors Codes in BT Spec Part D)
213  */
214 const hci_cmd_t hci_disconnect = {
215 OPCODE(OGF_LINK_CONTROL, 0x06), "H1"
216 };
217 
218 /**
219  * @param bd_addr
220  */
221 const hci_cmd_t hci_create_connection_cancel = {
222 OPCODE(OGF_LINK_CONTROL, 0x08), "B"
223 };
224 
225 /**
226  * @param bd_addr
227  * @param role (become master, stay slave)
228  */
229 const hci_cmd_t hci_accept_connection_request = {
230 OPCODE(OGF_LINK_CONTROL, 0x09), "B1"
231 };
232 
233 /**
234  * @param bd_addr
235  * @param reason (e.g. CONNECTION REJECTED DUE TO LIMITED RESOURCES (0x0d))
236  */
237 const hci_cmd_t hci_reject_connection_request = {
238 OPCODE(OGF_LINK_CONTROL, 0x0a), "B1"
239 };
240 
241 /**
242  * @param bd_addr
243  * @param link_key
244  */
245 const hci_cmd_t hci_link_key_request_reply = {
246 OPCODE(OGF_LINK_CONTROL, 0x0b), "BP"
247 };
248 
249 /**
250  * @param bd_addr
251  */
252 const hci_cmd_t hci_link_key_request_negative_reply = {
253 OPCODE(OGF_LINK_CONTROL, 0x0c), "B"
254 };
255 
256 /**
257  * @param bd_addr
258  * @param pin_length
259  * @param pin (c-string)
260  */
261 const hci_cmd_t hci_pin_code_request_reply = {
262 OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P"
263 };
264 
265 /**
266  * @param bd_addr
267  */
268 const hci_cmd_t hci_pin_code_request_negative_reply = {
269 OPCODE(OGF_LINK_CONTROL, 0x0e), "B"
270 };
271 
272 /**
273  * @param handle
274  * @param packet_type
275  */
276 const hci_cmd_t hci_change_connection_packet_type = {
277 OPCODE(OGF_LINK_CONTROL, 0x0f), "H2"
278 };
279 
280 /**
281  * @param handle
282  */
283 const hci_cmd_t hci_authentication_requested = {
284 OPCODE(OGF_LINK_CONTROL, 0x11), "H"
285 };
286 
287 /**
288  * @param handle
289  * @param encryption_enable
290  */
291 const hci_cmd_t hci_set_connection_encryption = {
292 OPCODE(OGF_LINK_CONTROL, 0x13), "H1"
293 };
294 
295 /**
296  * @param handle
297  */
298 const hci_cmd_t hci_change_connection_link_key = {
299 OPCODE(OGF_LINK_CONTROL, 0x15), "H"
300 };
301 
302 /**
303  * @param bd_addr
304  * @param page_scan_repetition_mode
305  * @param reserved
306  * @param clock_offset
307  */
308 const hci_cmd_t hci_remote_name_request = {
309 OPCODE(OGF_LINK_CONTROL, 0x19), "B112"
310 };
311 
312 
313 /**
314  * @param bd_addr
315  */
316 const hci_cmd_t hci_remote_name_request_cancel = {
317 OPCODE(OGF_LINK_CONTROL, 0x1A), "B"
318 };
319 
320 /**
321  * @param handle
322  */
323 const hci_cmd_t hci_read_remote_supported_features_command = {
324 OPCODE(OGF_LINK_CONTROL, 0x1B), "H"
325 };
326 
327 /**
328  * @param handle
329  * @param transmit_bandwidth 8000(64kbps)
330  * @param receive_bandwidth  8000(64kbps)
331  * @param max_latency        >= 7ms for eSCO, 0xFFFF do not care
332  * @param voice_settings     e.g. CVSD, Input Coding: Linear, Input Data Format: 2’s complement, data 16bit: 00011000000 == 0x60
333  * @param retransmission_effort  e.g. 0xFF do not care
334  * @param packet_type        at least EV3 for eSCO
335  */
336 const hci_cmd_t hci_setup_synchronous_connection = {
337 OPCODE(OGF_LINK_CONTROL, 0x0028), "H442212"
338 };
339 
340 /**
341  * @param bd_addr
342  * @param transmit_bandwidth
343  * @param receive_bandwidth
344  * @param max_latency
345  * @param voice_settings
346  * @param retransmission_effort
347  * @param packet_type
348  */
349 const hci_cmd_t hci_accept_synchronous_connection = {
350 OPCODE(OGF_LINK_CONTROL, 0x0029), "B442212"
351 };
352 
353 /**
354  * @param bd_addr
355  * @param IO_capability
356  * @param OOB_data_present
357  * @param authentication_requirements
358  */
359 const hci_cmd_t hci_io_capability_request_reply = {
360 OPCODE(OGF_LINK_CONTROL, 0x2b), "B111"
361 };
362 
363 /**
364  * @param bd_addr
365  */
366 const hci_cmd_t hci_user_confirmation_request_reply = {
367 OPCODE(OGF_LINK_CONTROL, 0x2c), "B"
368 };
369 
370 /**
371  * @param bd_addr
372  */
373 const hci_cmd_t hci_user_confirmation_request_negative_reply = {
374 OPCODE(OGF_LINK_CONTROL, 0x2d), "B"
375 };
376 
377 /**
378  * @param bd_addr
379  * @param numeric_value
380  */
381 const hci_cmd_t hci_user_passkey_request_reply = {
382 OPCODE(OGF_LINK_CONTROL, 0x2e), "B4"
383 };
384 
385 /**
386  * @param bd_addr
387  */
388 const hci_cmd_t hci_user_passkey_request_negative_reply = {
389 OPCODE(OGF_LINK_CONTROL, 0x2f), "B"
390 };
391 
392 /**
393  * @param bd_addr
394  * @param c Simple Pairing Hash C
395  * @param r Simple Pairing Randomizer R
396  */
397 const hci_cmd_t hci_remote_oob_data_request_reply = {
398 OPCODE(OGF_LINK_CONTROL, 0x30), "BPP"
399 };
400 
401 /**
402  * @param bd_addr
403  */
404 const hci_cmd_t hci_remote_oob_data_request_negative_reply = {
405 OPCODE(OGF_LINK_CONTROL, 0x33), "B"
406 };
407 
408 /**
409  * @param bd_addr
410  * @param reason (Part D, Error codes)
411  */
412 const hci_cmd_t hci_io_capability_request_negative_reply = {
413 OPCODE(OGF_LINK_CONTROL, 0x34), "B1"
414 };
415 
416 /**
417  * @param handle
418  * @param transmit_bandwidth
419  * @param receive_bandwidth
420  * @param transmit_coding_format_type
421  * @param transmit_coding_format_company
422  * @param transmit_coding_format_codec
423  * @param receive_coding_format_type
424  * @param receive_coding_format_company
425  * @param receive_coding_format_codec
426  * @param transmit_coding_frame_size
427  * @param receive_coding_frame_size
428  * @param input_bandwidth
429  * @param output_bandwidth
430  * @param input_coding_format_type
431  * @param input_coding_format_company
432  * @param input_coding_format_codec
433  * @param output_coding_format_type
434  * @param output_coding_format_company
435  * @param output_coding_format_codec
436  * @param input_coded_data_size
437  * @param outupt_coded_data_size
438  * @param input_pcm_data_format
439  * @param output_pcm_data_format
440  * @param input_pcm_sample_payload_msb_position
441  * @param output_pcm_sample_payload_msb_position
442  * @param input_data_path
443  * @param output_data_path
444  * @param input_transport_unit_size
445  * @param output_transport_unit_size
446  * @param max_latency
447  * @param packet_type
448  * @param retransmission_effort
449  */
450 const hci_cmd_t hci_enhanced_setup_synchronous_connection = {
451 OPCODE(OGF_LINK_CONTROL, 0x3d), "H4412212222441221222211111111221"
452 };
453 
454 /**
455  * @param bd_addr
456  * @param transmit_bandwidth
457  * @param receive_bandwidth
458  * @param transmit_coding_format_type
459  * @param transmit_coding_format_company
460  * @param transmit_coding_format_codec
461  * @param receive_coding_format_type
462  * @param receive_coding_format_company
463  * @param receive_coding_format_codec
464  * @param transmit_coding_frame_size
465  * @param receive_coding_frame_size
466  * @param input_bandwidth
467  * @param output_bandwidth
468  * @param input_coding_format_type
469  * @param input_coding_format_company
470  * @param input_coding_format_codec
471  * @param output_coding_format_type
472  * @param output_coding_format_company
473  * @param output_coding_format_codec
474  * @param input_coded_data_size
475  * @param outupt_coded_data_size
476  * @param input_pcm_data_format
477  * @param output_pcm_data_format
478  * @param input_pcm_sample_payload_msb_position
479  * @param output_pcm_sample_payload_msb_position
480  * @param input_data_path
481  * @param output_data_path
482  * @param input_transport_unit_size
483  * @param output_transport_unit_size
484  * @param max_latency
485  * @param packet_type
486  * @param retransmission_effort
487  */
488 const hci_cmd_t hci_enhanced_accept_synchronous_connection = {
489 OPCODE(OGF_LINK_CONTROL, 0x3e), "B4412212222441221222211111111221"
490 };
491 
492 /**
493  *  Link Policy Commands
494  */
495 
496 /**
497  * @param handle
498  * @param sniff_max_interval
499  * @param sniff_min_interval
500  * @param sniff_attempt
501  * @param sniff_timeout
502  */
503 const hci_cmd_t hci_sniff_mode = {
504 OPCODE(OGF_LINK_POLICY, 0x03), "H2222"
505 };
506 
507 /**
508  * @param handle
509  */
510 const hci_cmd_t hci_exit_sniff_mode = {
511 OPCODE(OGF_LINK_POLICY, 0x04), "H"
512 };
513 
514 /**
515  * @param handle
516  * @param flags
517  * @param service_type
518  * @param token_rate (bytes/s)
519  * @param peak_bandwith (bytes/s)
520  * @param latency (us)
521  * @param delay_variation (us)
522  */
523 const hci_cmd_t hci_qos_setup = {
524 OPCODE(OGF_LINK_POLICY, 0x07), "H114444"
525 };
526 
527 /**
528  * @param handle
529  */
530 const hci_cmd_t hci_role_discovery = {
531 OPCODE(OGF_LINK_POLICY, 0x09), "H"
532 };
533 
534 /**
535  * @param bd_addr
536  * @param role (0=master,1=slave)
537  */
538 const hci_cmd_t hci_switch_role_command= {
539 OPCODE(OGF_LINK_POLICY, 0x0b), "B1"
540 };
541 
542 /**
543  * @param handle
544  */
545 const hci_cmd_t hci_read_link_policy_settings = {
546 OPCODE(OGF_LINK_POLICY, 0x0c), "H"
547 };
548 
549 /**
550  * @param handle
551  * @param settings
552  */
553 const hci_cmd_t hci_write_link_policy_settings = {
554 OPCODE(OGF_LINK_POLICY, 0x0d), "H2"
555 };
556 
557 /**
558  * @param policy
559  */
560 const hci_cmd_t hci_write_default_link_policy_setting = {
561     OPCODE(OGF_LINK_POLICY, 0x0F), "2"
562 };
563 
564 
565 /**
566  *  Controller & Baseband Commands
567  */
568 
569 
570 /**
571  * @param event_mask_lover_octets
572  * @param event_mask_higher_octets
573  */
574 const hci_cmd_t hci_set_event_mask = {
575 OPCODE(OGF_CONTROLLER_BASEBAND, 0x01), "44"
576 };
577 
578 /**
579  */
580 const hci_cmd_t hci_reset = {
581 OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
582 };
583 
584 /**
585  * @param handle
586  */
587 const hci_cmd_t hci_flush = {
588 OPCODE(OGF_CONTROLLER_BASEBAND, 0x09), "H"
589 };
590 
591 /**
592  * @param bd_addr
593  * @param delete_all_flags
594  */
595 const hci_cmd_t hci_delete_stored_link_key = {
596 OPCODE(OGF_CONTROLLER_BASEBAND, 0x12), "B1"
597 };
598 
599 #ifdef ENABLE_CLASSIC
600 /**
601  * @param local_name (UTF-8, Null Terminated, max 248 octets)
602  */
603 const hci_cmd_t hci_write_local_name = {
604 OPCODE(OGF_CONTROLLER_BASEBAND, 0x13), "N"
605 };
606 #endif
607 
608 /**
609  */
610 const hci_cmd_t hci_read_local_name = {
611 OPCODE(OGF_CONTROLLER_BASEBAND, 0x14), ""
612 };
613 
614 /**
615  * @param page_timeout (* 0.625 ms)
616  */
617 const hci_cmd_t hci_write_page_timeout = {
618 OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2"
619 };
620 
621 /**
622  * @param scan_enable (no, inq, page, inq+page)
623  */
624 const hci_cmd_t hci_write_scan_enable = {
625 OPCODE(OGF_CONTROLLER_BASEBAND, 0x1A), "1"
626 };
627 
628 /**
629  * @param authentication_enable
630  */
631 const hci_cmd_t hci_write_authentication_enable = {
632 OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1"
633 };
634 
635 /**
636  * @param class_of_device
637  */
638 const hci_cmd_t hci_write_class_of_device = {
639 OPCODE(OGF_CONTROLLER_BASEBAND, 0x24), "3"
640 };
641 
642 /**
643  */
644 const hci_cmd_t hci_read_num_broadcast_retransmissions = {
645 OPCODE(OGF_CONTROLLER_BASEBAND, 0x29), ""
646 };
647 
648 /**
649  * @param num_broadcast_retransmissions (e.g. 0 for a single broadcast)
650  */
651 const hci_cmd_t hci_write_num_broadcast_retransmissions = {
652 OPCODE(OGF_CONTROLLER_BASEBAND, 0x2a), "1"
653 };
654 
655 /**
656  * @param synchronous_flow_control_enable - if yes, num completed packet everts are sent for SCO packets
657  */
658 const hci_cmd_t hci_write_synchronous_flow_control_enable = {
659 OPCODE(OGF_CONTROLLER_BASEBAND, 0x2f), "1"
660 };
661 
662 #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL
663 
664 /**
665  * @param flow_control_enable - 0: off, 1: ACL only, 2: SCO only, 3: ACL + SCO
666  */
667 const hci_cmd_t hci_set_controller_to_host_flow_control = {
668 OPCODE(OGF_CONTROLLER_BASEBAND, 0x31), "1"
669 };
670 
671 /**
672  * @param host_acl_data_packet_length
673  * @param host_synchronous_data_packet_length
674  * @param host_total_num_acl_data_packets
675  * @param host_total_num_synchronous_data_packets
676  */
677 const hci_cmd_t hci_host_buffer_size = {
678 OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122"
679 };
680 
681 
682 #if 0
683 //
684 // command sent manually sent by hci_host_num_completed_packets
685 //
686 /**
687  * @note only single handle supported by BTstack command generator
688  * @param number_of_handles must be 1
689  * @param connection_handle
690  * @param host_num_of_completed_packets for the given connection handle
691  */
692 const hci_cmd_t hci_host_number_of_completed_packets = {
693 OPCODE(OGF_CONTROLLER_BASEBAND, 0x35), "1H2"
694 };
695 #endif
696 
697 #endif
698 
699 /**
700  * @param handle
701  */
702 const hci_cmd_t hci_read_link_supervision_timeout = {
703 OPCODE(OGF_CONTROLLER_BASEBAND, 0x36), "H"
704 };
705 
706 /**
707  * @param handle
708  * @param timeout (0x0001 - 0xFFFF Time -> Range: 0.625ms - 40.9 sec)
709  */
710 const hci_cmd_t hci_write_link_supervision_timeout = {
711 OPCODE(OGF_CONTROLLER_BASEBAND, 0x37), "H2"
712 };
713 
714 /**
715  * @param num_current_iac must be 2
716  * @param iac_lap1
717  * @param iac_lap2
718  */
719 const hci_cmd_t hci_write_current_iac_lap_two_iacs = {
720 OPCODE(OGF_CONTROLLER_BASEBAND, 0x3A), "133"
721 };
722 
723 /**
724  * @param inquiry_mode (0x00 = standard, 0x01 = with RSSI, 0x02 = extended)
725  */
726 const hci_cmd_t hci_write_inquiry_mode = {
727 OPCODE(OGF_CONTROLLER_BASEBAND, 0x45), "1"
728 };
729 
730 /**
731  * @param fec_required
732  * @param exstended_inquiry_response
733  */
734 const hci_cmd_t hci_write_extended_inquiry_response = {
735 OPCODE(OGF_CONTROLLER_BASEBAND, 0x52), "1E"
736 };
737 
738 /**
739  * @param mode (0 = off, 1 = on)
740  */
741 const hci_cmd_t hci_write_simple_pairing_mode = {
742 OPCODE(OGF_CONTROLLER_BASEBAND, 0x56), "1"
743 };
744 
745 /**
746  */
747 const hci_cmd_t hci_read_local_oob_data = {
748 OPCODE(OGF_CONTROLLER_BASEBAND, 0x57), ""
749 // return status, C, R
750 };
751 
752 /**
753  * @param mode (0 = off, 1 = on)
754  */
755 const hci_cmd_t hci_write_default_erroneous_data_reporting = {
756 OPCODE(OGF_CONTROLLER_BASEBAND, 0x5B), "1"
757 };
758 
759 /**
760  */
761 const hci_cmd_t hci_read_le_host_supported = {
762 OPCODE(OGF_CONTROLLER_BASEBAND, 0x6c), ""
763 // return: status, le supported host, simultaneous le host
764 };
765 
766 /**
767  * @param le_supported_host
768  * @param simultaneous_le_host
769  */
770 const hci_cmd_t hci_write_le_host_supported = {
771 OPCODE(OGF_CONTROLLER_BASEBAND, 0x6d), "11"
772 // return: status
773 };
774 
775 /**
776  */
777 const hci_cmd_t hci_read_local_extended_ob_data = {
778 OPCODE(OGF_CONTROLLER_BASEBAND, 0x7d), ""
779 // return status, C_192, R_192, R_256, C_256
780 };
781 
782 
783 /**
784  * Testing Commands
785  */
786 
787 
788 /**
789  */
790 const hci_cmd_t hci_read_loopback_mode = {
791 OPCODE(OGF_TESTING, 0x01), ""
792 // return: status, loopback mode (0 = off, 1 = local loopback, 2 = remote loopback)
793 };
794 
795 /**
796  * @param loopback_mode
797  */
798 const hci_cmd_t hci_write_loopback_mode = {
799 OPCODE(OGF_TESTING, 0x02), "1"
800 // return: status
801 };
802 
803 /**
804  */
805 const hci_cmd_t hci_enable_device_under_test_mode = {
806 OPCODE(OGF_TESTING, 0x03), ""
807 // return: status
808 };
809 
810 /**
811  * @param simple_pairing_debug_mode
812  */
813 const hci_cmd_t hci_write_simple_pairing_debug_mode = {
814 OPCODE(OGF_TESTING, 0x04), "1"
815 // return: status
816 };
817 
818 /**
819  * @param handle
820  * @param dm1_acl_u_mode
821  * @param esco_loopback_mode
822  */
823 const hci_cmd_t hci_write_secure_connections_test_mode = {
824 OPCODE(OGF_TESTING, 0x0a), "H11"
825 // return: status
826 };
827 
828 
829 /**
830  * Informational Parameters
831  */
832 
833 const hci_cmd_t hci_read_local_version_information = {
834 OPCODE(OGF_INFORMATIONAL_PARAMETERS, 0x01), ""
835 };
836 const hci_cmd_t hci_read_local_supported_commands = {
837 OPCODE(OGF_INFORMATIONAL_PARAMETERS, 0x02), ""
838 };
839 const hci_cmd_t hci_read_local_supported_features = {
840 OPCODE(OGF_INFORMATIONAL_PARAMETERS, 0x03), ""
841 };
842 const hci_cmd_t hci_read_buffer_size = {
843 OPCODE(OGF_INFORMATIONAL_PARAMETERS, 0x05), ""
844 };
845 const hci_cmd_t hci_read_bd_addr = {
846 OPCODE(OGF_INFORMATIONAL_PARAMETERS, 0x09), ""
847 };
848 
849 /**
850  * Status Paramters
851  */
852 
853 /**
854  * @param handle
855  */
856 const hci_cmd_t hci_read_rssi = {
857 OPCODE(OGF_STATUS_PARAMETERS, 0x05), "H"
858 // no params
859 };
860 
861 
862 
863 #ifdef ENABLE_BLE
864 /**
865  * Low Energy Commands
866  */
867 
868 /**
869  * @param event_mask_lower_octets
870  * @param event_mask_higher_octets
871  */
872 const hci_cmd_t hci_le_set_event_mask = {
873 OPCODE(OGF_LE_CONTROLLER, 0x01), "44"
874 // return: status
875 };
876 
877 const hci_cmd_t hci_le_read_buffer_size = {
878 OPCODE(OGF_LE_CONTROLLER, 0x02), ""
879 // return: status, le acl data packet len (16), total num le acl data packets(8)
880 };
881 const hci_cmd_t hci_le_read_supported_features = {
882 OPCODE(OGF_LE_CONTROLLER, 0x03), ""
883 // return: LE_Features See [Vol 6] Part B, Section 4.6
884 };
885 
886 /**
887  * @param random_bd_addr
888  */
889 const hci_cmd_t hci_le_set_random_address = {
890 OPCODE(OGF_LE_CONTROLLER, 0x05), "B"
891 // return: status
892 };
893 
894 /**
895  * @param advertising_interval_min ([0x0020,0x4000], default: 0x0800, unit: 0.625 msec)
896  * @param advertising_interval_max ([0x0020,0x4000], default: 0x0800, unit: 0.625 msec)
897  * @param advertising_type (enum from 0: ADV_IND, ADC_DIRECT_IND, ADV_SCAN_IND, ADV_NONCONN_IND)
898  * @param own_address_type (enum from 0: public device address, random device address)
899  * @param direct_address_type ()
900  * @param direct_address (public or random address of device to be connecteed)
901  * @param advertising_channel_map (flags: chan_37(1), chan_38(2), chan_39(4))
902  * @param advertising_filter_policy (enum from 0: scan any conn any, scan whitelist, con any, scan any conn whitelist, scan whitelist, con whitelist)
903  */
904 const hci_cmd_t hci_le_set_advertising_parameters = {
905 OPCODE(OGF_LE_CONTROLLER, 0x06), "22111B11"
906 // return: status
907 };
908 
909 const hci_cmd_t hci_le_read_advertising_channel_tx_power = {
910 OPCODE(OGF_LE_CONTROLLER, 0x07), ""
911 // return: status, level [-20,10] signed int (8), units dBm
912 };
913 
914 /**
915  * @param advertising_data_length
916  * @param advertising_data (31 bytes)
917  */
918 const hci_cmd_t hci_le_set_advertising_data= {
919 OPCODE(OGF_LE_CONTROLLER, 0x08), "1A"
920 // return: status
921 };
922 
923 /**
924  * @param scan_response_data_length
925  * @param scan_response_data (31 bytes)
926  */
927 const hci_cmd_t hci_le_set_scan_response_data= {
928 OPCODE(OGF_LE_CONTROLLER, 0x09), "1A"
929 // return: status
930 };
931 
932 /**
933  * @param advertise_enable (off: 0, on: 1)
934  */
935 const hci_cmd_t hci_le_set_advertise_enable = {
936 OPCODE(OGF_LE_CONTROLLER, 0x0a), "1"
937 // return: status
938 };
939 
940 /**
941  * @param le_scan_type (passive (0), active (1))
942  * @param le_scan_interval ([0x0004,0x4000], unit: 0.625 msec)
943  * @param le_scan_window   ([0x0004,0x4000], unit: 0.625 msec)
944  * @param own_address_type (public (0), random (1))
945  * @param scanning_filter_policy (any (0), only whitelist (1))
946  */
947 const hci_cmd_t hci_le_set_scan_parameters = {
948 OPCODE(OGF_LE_CONTROLLER, 0x0b), "12211"
949 // return: status
950 };
951 
952 /**
953  * @param le_scan_enable  (disabled (0), enabled (1))
954  * @param filter_duplices (disabled (0), enabled (1))
955  */
956 const hci_cmd_t hci_le_set_scan_enable = {
957 OPCODE(OGF_LE_CONTROLLER, 0x0c), "11"
958 // return: status
959 };
960 
961 /**
962  * @param le_scan_interval ([0x0004, 0x4000], unit: 0.625 msec)
963  * @param le_scan_window ([0x0004, 0x4000], unit: 0.625 msec)
964  * @param initiator_filter_policy (peer address type + peer address (0), whitelist (1))
965  * @param peer_address_type (public (0), random (1))
966  * @param peer_address
967  * @param own_address_type (public (0), random (1))
968  * @param conn_interval_min ([0x0006, 0x0c80], unit: 1.25 msec)
969  * @param conn_interval_max ([0x0006, 0x0c80], unit: 1.25 msec)
970  * @param conn_latency (number of connection events [0x0000, 0x01f4])
971  * @param supervision_timeout ([0x000a, 0x0c80], unit: 10 msec)
972  * @param minimum_CE_length ([0x0000, 0xffff], unit: 0.625 msec)
973  * @param maximum_CE_length ([0x0000, 0xffff], unit: 0.625 msec)
974  */
975 const hci_cmd_t hci_le_create_connection= {
976 OPCODE(OGF_LE_CONTROLLER, 0x0d), "2211B1222222"
977 // return: none -> le create connection complete event
978 };
979 
980 const hci_cmd_t hci_le_create_connection_cancel = {
981 OPCODE(OGF_LE_CONTROLLER, 0x0e), ""
982 // return: status
983 };
984 
985 const hci_cmd_t hci_le_read_white_list_size = {
986 OPCODE(OGF_LE_CONTROLLER, 0x0f), ""
987 // return: status, number of entries in controller whitelist
988 };
989 
990 const hci_cmd_t hci_le_clear_white_list = {
991 OPCODE(OGF_LE_CONTROLLER, 0x10), ""
992 // return: status
993 };
994 
995 /**
996  * @param address_type (public (0), random (1))
997  * @param bd_addr
998  */
999 const hci_cmd_t hci_le_add_device_to_white_list = {
1000 OPCODE(OGF_LE_CONTROLLER, 0x11), "1B"
1001 // return: status
1002 };
1003 
1004 /**
1005  * @param address_type (public (0), random (1))
1006  * @param bd_addr
1007  */
1008 const hci_cmd_t hci_le_remove_device_from_white_list = {
1009 OPCODE(OGF_LE_CONTROLLER, 0x12), "1B"
1010 // return: status
1011 };
1012 
1013 /**
1014  * @param conn_handle
1015  * @param conn_interval_min ([0x0006,0x0c80], unit: 1.25 msec)
1016  * @param conn_interval_max ([0x0006,0x0c80], unit: 1.25 msec)
1017  * @param conn_latency ([0x0000,0x03e8], number of connection events)
1018  * @param supervision_timeout ([0x000a,0x0c80], unit: 10 msec)
1019  * @param minimum_CE_length ([0x0000,0xffff], unit: 0.625 msec)
1020  * @param maximum_CE_length ([0x0000,0xffff], unit: 0.625 msec)
1021  */
1022 const hci_cmd_t hci_le_connection_update = {
1023 OPCODE(OGF_LE_CONTROLLER, 0x13), "H222222"
1024 // return: none -> le connection update complete event
1025 };
1026 
1027 /**
1028  * @param channel_map_lower_32bits
1029  * @param channel_map_higher_5bits
1030  */
1031 const hci_cmd_t hci_le_set_host_channel_classification = {
1032 OPCODE(OGF_LE_CONTROLLER, 0x14), "41"
1033 // return: status
1034 };
1035 
1036 /**
1037  * @param conn_handle
1038  */
1039 const hci_cmd_t hci_le_read_channel_map = {
1040 OPCODE(OGF_LE_CONTROLLER, 0x15), "H"
1041 // return: status, connection handle, channel map (5 bytes, 37 used)
1042 };
1043 
1044 /**
1045  * @param conn_handle
1046  */
1047 const hci_cmd_t hci_le_read_remote_used_features = {
1048 OPCODE(OGF_LE_CONTROLLER, 0x16), "H"
1049 // return: none -> le read remote used features complete event
1050 };
1051 
1052 /**
1053  * @param key ((128) for AES-128)
1054  * @param plain_text (128)
1055  */
1056 const hci_cmd_t hci_le_encrypt = {
1057 OPCODE(OGF_LE_CONTROLLER, 0x17), "PP"
1058 // return: status, encrypted data (128)
1059 };
1060 
1061 const hci_cmd_t hci_le_rand = {
1062 OPCODE(OGF_LE_CONTROLLER, 0x18), ""
1063 // return: status, random number (64)
1064 };
1065 
1066 /**
1067  * @param conn_handle
1068  * @param random_number_lower_32bits
1069  * @param random_number_higher_32bits
1070  * @param encryption_diversifier (16)
1071  * @param long_term_key (128)
1072  */
1073 const hci_cmd_t hci_le_start_encryption = {
1074 OPCODE(OGF_LE_CONTROLLER, 0x19), "H442P"
1075 // return: none -> encryption changed or encryption key refresh complete event
1076 };
1077 
1078 /**
1079  * @param connection_handle
1080  * @param long_term_key (128)
1081  */
1082 const hci_cmd_t hci_le_long_term_key_request_reply = {
1083 OPCODE(OGF_LE_CONTROLLER, 0x1a), "HP"
1084 // return: status, connection handle
1085 };
1086 
1087 /**
1088  * @param conn_handle
1089  */
1090 const hci_cmd_t hci_le_long_term_key_negative_reply = {
1091 OPCODE(OGF_LE_CONTROLLER, 0x1b), "H"
1092 // return: status, connection handle
1093 };
1094 
1095 /**
1096  * @param conn_handle
1097  */
1098 const hci_cmd_t hci_le_read_supported_states = {
1099 OPCODE(OGF_LE_CONTROLLER, 0x1c), "H"
1100 // return: status, LE states (64)
1101 };
1102 
1103 /**
1104  * @param rx_frequency ([0x00 0x27], frequency (MHz): 2420 + N*2)
1105  */
1106 const hci_cmd_t hci_le_receiver_test = {
1107 OPCODE(OGF_LE_CONTROLLER, 0x1d), "1"
1108 // return: status
1109 };
1110 
1111 /**
1112  * @param tx_frequency ([0x00 0x27], frequency (MHz): 2420 + N*2)
1113  * @param test_payload_lengh ([0x00,0x25])
1114  * @param packet_payload ([0,7] different patterns)
1115  */
1116 const hci_cmd_t hci_le_transmitter_test = {
1117 OPCODE(OGF_LE_CONTROLLER, 0x1e), "111"
1118 // return: status
1119 };
1120 
1121 /**
1122  * @param end_test_cmd
1123  */
1124 const hci_cmd_t hci_le_test_end = {
1125 OPCODE(OGF_LE_CONTROLLER, 0x1f), "1"
1126 // return: status, number of packets (8)
1127 };
1128 
1129 /**
1130  * @param conn_handle
1131  * @param conn_interval_min ([0x0006,0x0c80], unit: 1.25 msec)
1132  * @param conn_interval_max ([0x0006,0x0c80], unit: 1.25 msec)
1133  * @param conn_latency ([0x0000,0x03e8], number of connection events)
1134  * @param supervision_timeout ([0x000a,0x0c80], unit: 10 msec)
1135  * @param minimum_CE_length ([0x0000,0xffff], unit: 0.625 msec)
1136  * @param maximum_CE_length ([0x0000,0xffff], unit: 0.625 msec)
1137  */
1138 const hci_cmd_t hci_le_remote_connection_parameter_request_reply = {
1139 OPCODE(OGF_LE_CONTROLLER, 0x20), "H222222"
1140 // return: status, connection handle
1141 };
1142 
1143 /**
1144  * @param con_handle
1145  * @param reason
1146  */
1147 const hci_cmd_t hci_le_remote_connection_parameter_request_negative_reply = {
1148 OPCODE(OGF_LE_CONTROLLER, 0x21), "H1"
1149 // return: status, connection handle
1150 };
1151 
1152 /**
1153  * @param con_handle
1154  * @param tx_octets
1155  * @param tx_time
1156  */
1157 const hci_cmd_t hci_le_set_data_length = {
1158 OPCODE(OGF_LE_CONTROLLER, 0x22), "H22"
1159 // return: status, connection handle
1160 };
1161 
1162 /**
1163  */
1164 const hci_cmd_t hci_le_read_suggested_default_data_length = {
1165 OPCODE(OGF_LE_CONTROLLER, 0x23), ""
1166 // return: status, suggested max tx octets, suggested max tx time
1167 };
1168 
1169 /**
1170  * @param suggested_max_tx_octets
1171  * @param suggested_max_tx_time
1172  */
1173 const hci_cmd_t hci_le_write_suggested_default_data_length = {
1174 OPCODE(OGF_LE_CONTROLLER, 0x24), "22"
1175 // return: status
1176 };
1177 
1178 /**
1179  */
1180 const hci_cmd_t hci_le_read_local_p256_public_key = {
1181 OPCODE(OGF_LE_CONTROLLER, 0x25), ""
1182 //  LE Read Local P-256 Public Key Complete is generated on completion
1183 };
1184 
1185 /**
1186  * @param public key
1187  * @param private key
1188  */
1189 const hci_cmd_t hci_le_generate_dhkey = {
1190 OPCODE(OGF_LE_CONTROLLER, 0x26), "QQ"
1191 // LE Generate DHKey Complete is generated on completion
1192 };
1193 
1194 /**
1195  */
1196 const hci_cmd_t hci_le_read_maximum_data_length = {
1197 OPCODE(OGF_LE_CONTROLLER, 0x2F), ""
1198 // return: status, supported max tx octets, supported max tx time, supported max rx octets, supported max rx time
1199 };
1200 
1201 /**
1202  * @param con_handle
1203  */
1204 const hci_cmd_t hci_le_read_phy = {
1205 OPCODE(OGF_LE_CONTROLLER, 0x30), "H"
1206 // return: status, connection handler, tx phy, rx phy
1207 };
1208 
1209 /**
1210  * @param all_phys
1211  * @param tx_phys
1212  * @param rx_phys
1213  */
1214 const hci_cmd_t hci_le_set_default_phy = {
1215 OPCODE(OGF_LE_CONTROLLER, 0x31), "111"
1216 // return: status
1217 };
1218 
1219 /**
1220  * @param con_handle
1221  * @param all_phys
1222  * @param tx_phys
1223  * @param rx_phys
1224  * @param phy_options
1225  */
1226 const hci_cmd_t hci_le_set_phy = {
1227 OPCODE(OGF_LE_CONTROLLER, 0x32), "H1111"
1228 // LE PHY Update Complete is generated on completion
1229 };
1230 
1231 
1232 #endif
1233 
1234 // Broadcom / Cypress specific HCI commands
1235 
1236 /**
1237  * @brief Configure SCO Routing (BCM)
1238  * @param sco_routing is 0 for PCM, 1 for Transport, 2 for Codec and 3 for I2S
1239  * @param pcm_interface_rate is 0 for 128KBps, 1 for 256 KBps, 2 for 512KBps, 3 for 1024KBps, and 4 for 2048Kbps
1240  * @param frame_type is 0 for short and 1 for long
1241  * @param sync_mode is 0 for slave and 1 for master
1242  * @param clock_mode is 0 for slabe and 1 for master
1243  */
1244 const hci_cmd_t hci_bcm_write_sco_pcm_int = {
1245 OPCODE(0x3f, 0x1c), "11111"
1246 // return: status
1247 };
1248