xref: /btstack/src/hci.h (revision 1e6aba4769cc9f1f9e6a695a7254eb66be5d4943)
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 struct hci_connection {
28     // linked list - assert: first field
29     linked_item_t    item;
30 
31     // remote side
32     bd_addr_t address;
33 
34     // module handle
35     hci_con_handle_t con_handle;
36 
37     // errands
38     hci_connection_flags_t flags;
39 } hci_connection_t;
40 
41 /**
42  * main data structure
43  */
44 typedef struct {
45     // transport component with configuration
46     hci_transport_t  * hci_transport;
47     void             * config;
48 
49     // hardware power controller
50     bt_control_t     * control;
51 
52     // list of existing baseband connections
53     linked_list_t     connections;
54 
55     // single buffer for HCI Command assembly
56     uint8_t          * hci_cmd_buffer;
57 
58     /* host to controller flow control */
59     uint8_t  num_cmd_packets;
60     uint8_t  num_acl_packets;
61 
62     /* callback to L2CAP layer */
63     void (*event_packet_handler)(uint8_t *packet, uint16_t size);
64     void (*acl_packet_handler)  (uint8_t *packet, uint16_t size);
65 
66     /* hci state machine */
67     HCI_STATE state;
68     uint8_t   substate;
69     uint8_t   cmds_ready;
70 
71 } hci_stack_t;
72 
73 // set up HCI
74 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control);
75 
76 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size));
77 
78 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size));
79 
80 // power control
81 int hci_power_control(HCI_POWER_MODE mode);
82 
83 /**
84  * run the hci daemon loop once
85  */
86 void hci_run();
87 
88 // create and send hci command packets based on a template and a list of parameters
89 int hci_send_cmd(hci_cmd_t *cmd, ...);
90 
91 // send complete CMD packet
92 int hci_send_cmd_packet(uint8_t *packet, int size);
93 
94 // send ACL packet
95 int hci_send_acl_packet(uint8_t *packet, int size);
96 
97 //
98 void hci_emit_state();
99