xref: /btstack/src/hci.h (revision 22909952ea76c45a8db20409374b80e7e0204e4c)
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 
2143625864Smatthias.ringwald // #define STORE_BT_16( buffer, pos, value ) { buffer[pos] = (value) & 0xff; buffer[pos+1] = (value) >> 8; }
22ea6387dfSmatthias.ringwald 
23*22909952Smatthias.ringwald #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == 0x0e && READ_BT_16(event,3) == cmd.opcode)
24*22909952Smatthias.ringwald 
25ea6387dfSmatthias.ringwald // packet headers
26ea6387dfSmatthias.ringwald #define HCI_CMD_DATA_PKT_HDR	  0x03
27ea6387dfSmatthias.ringwald #define HCI_ACL_DATA_PKT_HDR	  0x04
28ea6387dfSmatthias.ringwald #define HCI_SCO_DATA_PKT_HDR	  0x03
29ea6387dfSmatthias.ringwald #define HCI_EVENT_PKT_HDR         0x02
30ea6387dfSmatthias.ringwald 
31ea6387dfSmatthias.ringwald /**
32ea6387dfSmatthias.ringwald  * @brief Length of a bluetooth device address.
33ea6387dfSmatthias.ringwald  */
34ea6387dfSmatthias.ringwald #define BD_ADDR_LEN 6
35ea6387dfSmatthias.ringwald typedef uint8_t bd_addr_t[BD_ADDR_LEN];
36ea6387dfSmatthias.ringwald 
37ea6387dfSmatthias.ringwald /**
38ea6387dfSmatthias.ringwald  * @brief The link key type
39ea6387dfSmatthias.ringwald  */
40ea6387dfSmatthias.ringwald #define LINK_KEY_LEN 16
41ea6387dfSmatthias.ringwald typedef uint8_t link_key_t[LINK_KEY_LEN];
42ea6387dfSmatthias.ringwald 
4343625864Smatthias.ringwald /**
4443625864Smatthias.ringwald  * @brief hci connection handle type
4543625864Smatthias.ringwald  */
4643625864Smatthias.ringwald typedef uint16_t hci_con_handle_t;
47ea6387dfSmatthias.ringwald 
48475c8125Smatthias.ringwald typedef enum {
49475c8125Smatthias.ringwald     HCI_POWER_OFF = 0,
50475c8125Smatthias.ringwald     HCI_POWER_ON
51475c8125Smatthias.ringwald } HCI_POWER_MODE;
52475c8125Smatthias.ringwald 
5393b8dc03Smatthias.ringwald typedef struct {
540a974e0cSmatthias.ringwald     uint16_t    opcode;
5593b8dc03Smatthias.ringwald     const char *format;
5693b8dc03Smatthias.ringwald } hci_cmd_t;
5793b8dc03Smatthias.ringwald 
5816833f0aSmatthias.ringwald typedef struct hci_connection {
5916833f0aSmatthias.ringwald     struct hci_connection * next;
6016833f0aSmatthias.ringwald     bd_addr_t address;
6116833f0aSmatthias.ringwald     hci_con_handle_t con_handle;
6216833f0aSmatthias.ringwald } hci_connection_t;
6316833f0aSmatthias.ringwald 
6416833f0aSmatthias.ringwald typedef struct {
6516833f0aSmatthias.ringwald 
6616833f0aSmatthias.ringwald     hci_transport_t * hci_transport;
6716833f0aSmatthias.ringwald     uint8_t         * hci_cmd_buffer;
6816833f0aSmatthias.ringwald     hci_connection_t *connections;
6916833f0aSmatthias.ringwald 
7016833f0aSmatthias.ringwald     /* host to controller flow control */
7116833f0aSmatthias.ringwald     uint8_t  num_cmd_packets;
7216833f0aSmatthias.ringwald     uint8_t  num_acl_packets;
7316833f0aSmatthias.ringwald 
7416833f0aSmatthias.ringwald     /* callback to L2CAP layer */
7516833f0aSmatthias.ringwald     void (*event_packet_handler)(uint8_t *packet, int size);
7616833f0aSmatthias.ringwald     void (*acl_packet_handler)  (uint8_t *packet, int size);
7716833f0aSmatthias.ringwald 
7816833f0aSmatthias.ringwald } hci_stack_t;
7916833f0aSmatthias.ringwald 
8002ea9861Smatthias.ringwald 
81475c8125Smatthias.ringwald // set up HCI
82475c8125Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config);
83475c8125Smatthias.ringwald 
8416833f0aSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size));
8516833f0aSmatthias.ringwald 
8616833f0aSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size));
8716833f0aSmatthias.ringwald 
88475c8125Smatthias.ringwald // power control
89475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE mode);
90475c8125Smatthias.ringwald 
911f504dbdSmatthias.ringwald // run the hci daemon loop
92475c8125Smatthias.ringwald void hci_run();
931f504dbdSmatthias.ringwald 
94554588a5Smatthias.ringwald //
95554588a5Smatthias.ringwald void hexdump(uint8_t *data, int size);
96554588a5Smatthias.ringwald 
9702ea9861Smatthias.ringwald // create and send hci command packet based on a template and a list of parameters
9802ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...);
99554588a5Smatthias.ringwald 
10043625864Smatthias.ringwald // send ACL packet
10143625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size);
10243625864Smatthias.ringwald 
10316833f0aSmatthias.ringwald 
10416833f0aSmatthias.ringwald 
10543625864Smatthias.ringwald // helper
10643625864Smatthias.ringwald extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
10743625864Smatthias.ringwald 
10893b8dc03Smatthias.ringwald extern hci_cmd_t hci_inquiry;
1093091b266Smatthias.ringwald extern hci_cmd_t hci_link_key_request_negative_reply;
1103091b266Smatthias.ringwald extern hci_cmd_t hci_pin_code_request_reply;
11193b8dc03Smatthias.ringwald extern hci_cmd_t hci_reset;
11202ea9861Smatthias.ringwald extern hci_cmd_t hci_create_connection;
11302ea9861Smatthias.ringwald extern hci_cmd_t hci_host_buffer_size;
1143091b266Smatthias.ringwald extern hci_cmd_t hci_write_authentication_enable;
11502ea9861Smatthias.ringwald extern hci_cmd_t hci_write_page_timeout;
11693b8dc03Smatthias.ringwald 
11793b8dc03Smatthias.ringwald #define HCI_INQUIRY_LAP 0x9E8B33L  // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
118