xref: /btstack/src/hci.h (revision fe1ed1b8b680d23b4d9fe8dd47c59beebcb72020)
1 /*
2  *  hci.h
3  *
4  *  Created by Matthias Ringwald on 4/29/09.
5  *
6  */
7 
8 #pragma once
9 
10 #include "hci_cmds.h"
11 #include "utils.h"
12 #include "hci_transport.h"
13 #include "bt_control.h"
14 
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 
19 /**
20  *  Hardware state of Bluetooth controller
21  */
22 typedef enum {
23     HCI_POWER_OFF = 0,
24     HCI_POWER_ON
25 } HCI_POWER_MODE;
26 
27 /**
28  * State of BTstack
29  */
30 typedef enum {
31     HCI_STATE_OFF = 0,
32     HCI_STATE_INITIALIZING,
33     HCI_STATE_WORKING,
34     HCI_STATE_HALTING
35 } HCI_STATE;
36 
37 /**
38  * Connection State
39  */
40 typedef enum {
41     SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0,
42     SEND_PIN_CODE_RESPONSE = 1 << 1
43 } hci_connection_flags_t;
44 
45 typedef struct hci_connection {
46     // linked list - assert: first field
47     linked_item_t    item;
48 
49     // remote side
50     bd_addr_t address;
51 
52     // module handle
53     hci_con_handle_t con_handle;
54 
55     // errands
56     hci_connection_flags_t flags;
57 } hci_connection_t;
58 
59 /**
60  * main data structure
61  */
62 typedef struct {
63     // transport component with configuration
64     hci_transport_t  * hci_transport;
65     void             * config;
66 
67     // hardware power controller
68     bt_control_t     * control;
69 
70     // list of existing baseband connections
71     linked_list_t     connections;
72 
73     // single buffer for HCI Command assembly
74     uint8_t          * hci_cmd_buffer;
75 
76     /* host to controller flow control */
77     uint8_t  num_cmd_packets;
78     uint8_t  num_acl_packets;
79 
80     /* callback to L2CAP layer */
81     void (*event_packet_handler)(uint8_t *packet, uint16_t size);
82     void (*acl_packet_handler)  (uint8_t *packet, uint16_t size);
83 
84     /* hci state machine */
85     HCI_STATE state;
86     uint8_t   substate;
87     uint8_t   cmds_ready;
88 
89 } hci_stack_t;
90 
91 // set up HCI
92 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control);
93 
94 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size));
95 
96 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size));
97 
98 // power control
99 int hci_power_control(HCI_POWER_MODE mode);
100 
101 /**
102  * run the hci daemon loop once
103  */
104 void hci_run();
105 
106 // create and send hci command packets based on a template and a list of parameters
107 int hci_send_cmd(hci_cmd_t *cmd, ...);
108 
109 // send complete CMD packet
110 int hci_send_cmd_packet(uint8_t *packet, int size);
111 
112 // send ACL packet
113 int hci_send_acl_packet(uint8_t *packet, int size);
114 
115 //