xref: /btstack/src/hci.h (revision 43625864d262f22c09d4ea5b6eb3c8becbe91100)
11f504dbdSmatthias.ringwald /*
21f504dbdSmatthias.ringwald  *  hci.h
31f504dbdSmatthias.ringwald  *
41f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
51f504dbdSmatthias.ringwald  *
61f504dbdSmatthias.ringwald  */
71f504dbdSmatthias.ringwald 
81f504dbdSmatthias.ringwald #pragma once
91f504dbdSmatthias.ringwald 
1093b8dc03Smatthias.ringwald #include <stdint.h>
1102ea9861Smatthias.ringwald #include <stdlib.h>
1293b8dc03Smatthias.ringwald 
131f504dbdSmatthias.ringwald #include "hci_transport.h"
141f504dbdSmatthias.ringwald 
15ea6387dfSmatthias.ringwald 
16ea6387dfSmatthias.ringwald // helper for BT little endian format
17ea6387dfSmatthias.ringwald #define READ_BT_16( buffer, pos) (buffer[pos] | (buffer[pos+1] << 8))
18ea6387dfSmatthias.ringwald #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16))
19ea6387dfSmatthias.ringwald #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)
20ea6387dfSmatthias.ringwald 
21*43625864Smatthias.ringwald // #define STORE_BT_16( buffer, pos, value ) { buffer[pos] = (value) & 0xff; buffer[pos+1] = (value) >> 8; }
22ea6387dfSmatthias.ringwald 
23ea6387dfSmatthias.ringwald // packet headers
24ea6387dfSmatthias.ringwald #define HCI_CMD_DATA_PKT_HDR	  0x03
25ea6387dfSmatthias.ringwald #define HCI_ACL_DATA_PKT_HDR	  0x04
26ea6387dfSmatthias.ringwald #define HCI_SCO_DATA_PKT_HDR	  0x03
27ea6387dfSmatthias.ringwald #define HCI_EVENT_PKT_HDR         0x02
28ea6387dfSmatthias.ringwald 
29ea6387dfSmatthias.ringwald /**
30ea6387dfSmatthias.ringwald  * @brief Length of a bluetooth device address.
31ea6387dfSmatthias.ringwald  */
32ea6387dfSmatthias.ringwald #define BD_ADDR_LEN 6
33ea6387dfSmatthias.ringwald typedef uint8_t bd_addr_t[BD_ADDR_LEN];
34ea6387dfSmatthias.ringwald 
35ea6387dfSmatthias.ringwald /**
36ea6387dfSmatthias.ringwald  * @brief The link key type
37ea6387dfSmatthias.ringwald  */
38ea6387dfSmatthias.ringwald #define LINK_KEY_LEN 16
39ea6387dfSmatthias.ringwald typedef uint8_t link_key_t[LINK_KEY_LEN];
40ea6387dfSmatthias.ringwald 
41*43625864Smatthias.ringwald /**
42*43625864Smatthias.ringwald  * @brief hci connection handle type
43*43625864Smatthias.ringwald  */
44*43625864Smatthias.ringwald typedef uint16_t hci_con_handle_t;
45ea6387dfSmatthias.ringwald 
46475c8125Smatthias.ringwald typedef enum {
47475c8125Smatthias.ringwald     HCI_POWER_OFF = 0,
48475c8125Smatthias.ringwald     HCI_POWER_ON
49475c8125Smatthias.ringwald } HCI_POWER_MODE;
50475c8125Smatthias.ringwald 
5193b8dc03Smatthias.ringwald typedef struct {
520a974e0cSmatthias.ringwald     uint16_t    opcode;
5393b8dc03Smatthias.ringwald     const char *format;
5493b8dc03Smatthias.ringwald } hci_cmd_t;
5593b8dc03Smatthias.ringwald 
5602ea9861Smatthias.ringwald 
57475c8125Smatthias.ringwald // set up HCI
58475c8125Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config);
59475c8125Smatthias.ringwald 
60475c8125Smatthias.ringwald // power control
61475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE mode);
62475c8125Smatthias.ringwald 
631f504dbdSmatthias.ringwald // run the hci daemon loop
64475c8125Smatthias.ringwald void hci_run();
651f504dbdSmatthias.ringwald 
66554588a5Smatthias.ringwald //
67554588a5Smatthias.ringwald void hexdump(uint8_t *data, int size);
68554588a5Smatthias.ringwald 
6902ea9861Smatthias.ringwald // create and send hci command packet based on a template and a list of parameters
7002ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...);
71554588a5Smatthias.ringwald 
72*43625864Smatthias.ringwald // send ACL packet
73*43625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size);
74*43625864Smatthias.ringwald 
75*43625864Smatthias.ringwald // helper
76*43625864Smatthias.ringwald extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
77*43625864Smatthias.ringwald 
7893b8dc03Smatthias.ringwald extern hci_cmd_t hci_inquiry;
793091b266Smatthias.ringwald extern hci_cmd_t hci_link_key_request_negative_reply;
803091b266Smatthias.ringwald extern hci_cmd_t hci_pin_code_request_reply;
8193b8dc03Smatthias.ringwald extern hci_cmd_t hci_reset;
8202ea9861Smatthias.ringwald extern hci_cmd_t hci_create_connection;
8302ea9861Smatthias.ringwald extern hci_cmd_t hci_host_buffer_size;
843091b266Smatthias.ringwald extern hci_cmd_t hci_write_authentication_enable;
8502ea9861Smatthias.ringwald extern hci_cmd_t hci_write_page_timeout;
8693b8dc03Smatthias.ringwald 
8793b8dc03Smatthias.ringwald #define HCI_INQUIRY_LAP 0x9E8B33L  // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
88