1 /* 2 * hci.h 3 * 4 * Created by Matthias Ringwald on 4/29/09. 5 * 6 */ 7 8 #pragma once 9 10 #include <btstack/hci_cmds.h> 11 #include <btstack/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 // packet header lenghts 20 #define HCI_CMD_DATA_PKT_HDR 0x03 21 #define HCI_ACL_DATA_PKT_HDR 0x04 22 #define HCI_SCO_DATA_PKT_HDR 0x03 23 #define HCI_EVENT_PKT_HDR 0x02 24 25 26 // cmds for BTstack 27 // get state: @returns HCI_STATE 28 #define BTSTACK_GET_STATE 0x01 29 30 // set power mode: @param HCI_POWER_MODE 31 #define BTSTACK_SET_POWER_MODE 0x02 32 33 // set capture mode: @param on 34 #define BTSTACK_SET_ACL_CAPTURE_MODE 0x03 35 36 // create l2cap channel: @param bd_addr(48), psm (16) 37 #define L2CAP_CREATE_CHANNEL 0x20 38 39 // disconnect l2cap disconnect, @param channel(16), reason(8) 40 #define L2CAP_DISCONNECT 0x21 41 42 // 43 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode) 44 45 /** 46 * Connection State 47 */ 48 typedef enum { 49 SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0, 50 SEND_PIN_CODE_RESPONSE = 1 << 1 51 } hci_connection_flags_t; 52 53 typedef enum { 54 SENT_CREATE_CONNECTION = 1, 55 RECEIVED_CONNECTION_REQUEST, 56 ACCEPTED_CONNECTION_REQUEST, 57 REJECTED_CONNECTION_REQUEST, 58 OPEN, 59 SENT_DISCONNECT 60 } CONNECTION_STATE; 61 62 typedef enum { 63 BLUETOOTH_OFF = 1, 64 BLUETOOTH_ON, 65 BLUETOOTH_ACTIVE 66 } BLUETOOTH_STATE; 67 68 typedef struct { 69 // linked list - assert: first field 70 linked_item_t item; 71 72 // remote side 73 bd_addr_t address; 74 75 // module handle 76 hci_con_handle_t con_handle; 77 78 // state 79 CONNECTION_STATE state; 80 81 // errands 82 hci_connection_flags_t flags; 83 84 // timer 85 timer_t timeout; 86 struct timeval timestamp; 87 88 } hci_connection_t; 89 90 /** 91 * main data structure 92 */ 93 typedef struct { 94 // transport component with configuration 95 hci_transport_t * hci_transport; 96 void * config; 97 98 // hardware power controller 99 bt_control_t * control; 100 101 // list of existing baseband connections 102 linked_list_t connections; 103 104 // single buffer for HCI Command assembly 105 uint8_t * hci_cmd_buffer; 106 107 /* host to controller flow control */ 108 uint8_t num_cmd_packets; 109 uint8_t num_acl_packets; 110 111 /* callback to L2CAP layer */ 112 void (*event_packet_handler)(uint8_t *packet, uint16_t size); 113 void (*acl_packet_handler) (uint8_t *packet, uint16_t size); 114 115 /* hci state machine */ 116 HCI_STATE state; 117 uint8_t substate; 118 uint8_t cmds_ready; 119 120 } hci_stack_t; 121 122 // set up HCI 123 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control); 124 125 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)); 126 127 void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)); 128 129 // power control 130 int hci_power_control(HCI_POWER_MODE mode); 131 132 /** 133 * run the hci control loop once 134 */ 135 void hci_run(); 136 137 // create and send hci command packets based on a template and a list of parameters 138 int hci_send_cmd(hci_cmd_t *cmd, ...); 139 140 // send complete CMD packet 141 int hci_send_cmd_packet(uint8_t *packet, int size); 142 143 // send ACL packet 144 int hci_send_acl_packet(uint8_t *packet, int size); 145 146 // 147 void hci_emit_state(); 148 void hci_emit_connection_complete(hci_connection_t *conn); 149 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 150 void hci_emit_nr_connections_changed(); 151 void hci_emit_hci_open_failed(); 152