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