xref: /btstack/src/hci.h (revision ee091cf1fdd21fd33ab1793564ec007cd1d10ad1)
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  * Connection State
21  */
22 typedef enum {
23     SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0,
24     SEND_PIN_CODE_RESPONSE = 1 << 1
25 } hci_connection_flags_t;
26 
27 typedef enum {
28     SENT_CREATE_CONNECTION = 1,
29     RECEIVED_CONNECTION_REQUEST,
30     ACCEPTED_CONNECTION_REQUEST,
31     REJECTED_CONNECTION_REQUEST,
32     OPEN,
33     SENT_DISCONNECT
34 } CONNECTION_STATE;
35 
36 typedef struct {
37     // linked list - assert: first field
38     linked_item_t    item;
39 
40     // remote side
41     bd_addr_t address;
42 
43     // module handle
44     hci_con_handle_t con_handle;
45 
46     // state
47     CONNECTION_STATE state;
48 
49     // errands
50     hci_connection_flags_t flags;
51 
52     // timer
53     timer_t timeout;
54     struct timeval timestamp;
55 
56 } hci_connection_t;
57 
58 /**
59  * main data structure
60  */
61 typedef struct {
62     // transport component with configuration
63     hci_transport_t  * hci_transport;
64     void             * config;
65 
66     // hardware power controller
67     bt_control_t     * control;
68 
69     // list of existing baseband connections
70     linked_list_t     connections;
71 
72     // single buffer for HCI Command assembly
73     uint8_t          * hci_cmd_buffer;
74 
75     /* host to controller flow control */
76     uint8_t  num_cmd_packets;
77     uint8_t  num_acl_packets;
78 
79     /* callback to L2CAP layer */
80     void (*event_packet_handler)(uint8_t *packet, uint16_t size);
81     void (*acl_packet_handler)  (uint8_t *packet, uint16_t size);
82 
83     /* hci state machine */
84     HCI_STATE state;
85     uint8_t   substate;
86     uint8_t   cmds_ready;
87 
88 } hci_stack_t;
89 
90 // set up HCI
91 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control);
92 
93 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size));
94 
95 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size));
96 
97 // power control
98 int hci_power_control(HCI_POWER_MODE mode);
99 
100 /**
101  * run the hci control loop once
102  */
103 void hci_run();
104 
105 // create and send hci command packets based on a template and a list of parameters
106 int hci_send_cmd(hci_cmd_t *cmd, ...);
107 
108 // send complete CMD packet
109 int hci_send_cmd_packet(uint8_t *packet, int size);
110 
111 // send ACL packet
112 int hci_send_acl_packet(uint8_t *packet, int size);
113 
114 //
115 void hci_emit_state();
116 void hci_emit_connection_complete(hci_connection_t *conn);
117 void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
118