xref: /btstack/src/hci.h (revision 43625864d262f22c09d4ea5b6eb3c8becbe91100)
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 // packet headers
24 #define HCI_CMD_DATA_PKT_HDR	  0x03
25 #define HCI_ACL_DATA_PKT_HDR	  0x04
26 #define HCI_SCO_DATA_PKT_HDR	  0x03
27 #define HCI_EVENT_PKT_HDR         0x02
28 
29 /**
30  * @brief Length of a bluetooth device address.
31  */
32 #define BD_ADDR_LEN 6
33 typedef uint8_t bd_addr_t[BD_ADDR_LEN];
34 
35 /**
36  * @brief The link key type
37  */
38 #define LINK_KEY_LEN 16
39 typedef uint8_t link_key_t[LINK_KEY_LEN];
40 
41 /**
42  * @brief hci connection handle type
43  */
44 typedef uint16_t hci_con_handle_t;
45 
46 typedef enum {
47     HCI_POWER_OFF = 0,
48     HCI_POWER_ON
49 } HCI_POWER_MODE;
50 
51 typedef struct {
52     uint16_t    opcode;
53     const char *format;
54 } hci_cmd_t;
55 
56 
57 // set up HCI
58 void hci_init(hci_transport_t *transport, void *config);
59 
60 // power control
61 int hci_power_control(HCI_POWER_MODE mode);
62 
63 // run the hci daemon loop
64 void hci_run();
65 
66 //
67 void hexdump(uint8_t *data, int size);
68 
69 // create and send hci command packet based on a template and a list of parameters
70 int hci_send_cmd(hci_cmd_t *cmd, ...);
71 
72 // send ACL packet
73 int hci_send_acl_packet(uint8_t *packet, int size);
74 
75 // helper
76 extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
77 
78 extern hci_cmd_t hci_inquiry;
79 extern hci_cmd_t hci_link_key_request_negative_reply;
80 extern hci_cmd_t hci_pin_code_request_reply;
81 extern hci_cmd_t hci_reset;
82 extern hci_cmd_t hci_create_connection;
83 extern hci_cmd_t hci_host_buffer_size;
84 extern hci_cmd_t hci_write_authentication_enable;
85 extern hci_cmd_t hci_write_page_timeout;
86 
87 #define HCI_INQUIRY_LAP 0x9E8B33L  // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
88