xref: /btstack/src/hci.h (revision c8e4258af80ac01c2b8c90df5fb94a264ad985db)
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 } hci_connection_t;
53 
54 /**
55  * main data structure
56  */
57 typedef struct {
58     // transport component with configuration
59     hci_transport_t  * hci_transport;
60     void             * config;
61 
62     // hardware power controller
63     bt_control_t     * control;
64 
65     // list of existing baseband connections
66     linked_list_t     connections;
67 
68     // single buffer for HCI Command assembly
69     uint8_t          * hci_cmd_buffer;
70 
71     /* host to controller flow control */
72     uint8_t  num_cmd_packets;
73     uint8_t  num_acl_packets;
74 
75     /* callback to L2CAP layer */
76     void (*event_packet_handler)(uint8_t *packet, uint16_t size);
77     void (*acl_packet_handler)  (uint8_t *packet, uint16_t size);
78 
79     /* hci state machine */
80     HCI_STATE state;
81     uint8_t   substate;
82     uint8_t   cmds_ready;
83 
84 } hci_stack_t;
85 
86 // set up HCI
87 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control);
88 
89 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size));
90 
91 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size));
92 
93 // power control
94 int hci_power_control(HCI_POWER_MODE mode);
95 
96 /**
97  * run the hci control loop once
98  */
99 void hci_run();
100 
101 // create and send hci command packets based on a template and a list of parameters
102 int hci_send_cmd(hci_cmd_t *cmd, ...);
103 
104 // send complete CMD packet
105 int hci_send_cmd_packet(uint8_t *packet, int size);
106 
107 // send ACL packet
108 int hci_send_acl_packet(uint8_t *packet, int size);
109 
110 //
111 void hci_emit_state();
112 void hci_emit_connection_complete(hci_connection_t *conn);
113