1 /* 2 * hci.h 3 * 4 * Created by Matthias Ringwald on 4/29/09. 5 * 6 */ 7 8 #pragma once 9 10 #include <stdint.h> 11 #include <stdlib.h> 12 13 #include "hci_transport.h" 14 15 // helper for BT little endian format 16 #define READ_BT_16( buffer, pos) (buffer[pos] | (buffer[pos+1] << 8)) 17 #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16)) 18 #define READ_BT_32( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16) | (((uint32_t) buffer[pos+3])) << 24) 19 20 // packet header lengh 21 #define HCI_CMD_DATA_PKT_HDR 0x03 22 #define HCI_ACL_DATA_PKT_HDR 0x04 23 #define HCI_SCO_DATA_PKT_HDR 0x03 24 #define HCI_EVENT_PKT_HDR 0x02 25 26 // Events from host controller to host 27 #define HCI_EVENT_INQUIRY_COMPLETE 0x01 28 #define HCI_EVENT_INQUIRY_RESULT 0x02 29 #define HCI_EVENT_CONNECTION_COMPLETE 0x03 30 #define HCI_EVENT_CONNECTION_REQUEST 0x04 31 #define HCI_EVENT_DISCONNECTION_COMPLETE 0x05 32 #define HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT 0x06 33 #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07 34 #define HCI_EVENT_ENCRIPTION_CHANGE 0x08 35 #define HCI_EVENT_CHANGE_CONNECTION_LINK_KEY_COMPLETE 0x09 36 #define HCI_EVENT_MASTER_LINK_KEY_COMPLETE 0x0A 37 #define HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE 0x0B 38 #define HCI_EVENT_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C 39 #define HCI_EVENT_QOS_SETUP_COMPLETE 0x0D 40 #define HCI_EVENT_COMMAND_COMPLETE 0x0E 41 #define HCI_EVENT_COMMAND_STATUS 0x0F 42 #define HCI_EVENT_HARDWARE_ERROR 0x10 43 #define HCI_EVENT_FLUSH_OCCURED 0x11 44 #define HCI_EVENT_ROLE_CHANGE 0x12 45 #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS 0x13 46 #define HCI_EVENT_MODE_CHANGE_EVENT 0x14 47 #define HCI_EVENT_RETURN_LINK_KEYS 0x15 48 #define HCI_EVENT_PIN_CODE_REQUEST 0x16 49 #define HCI_EVENT_LINK_KEY_REQUEST 0x17 50 #define HCI_EVENT_LINK_KEY_NOTIFICATION 0x18 51 #define HCI_EVENT_DATA_BUFFER_OVERFLOW 0x1A 52 #define HCI_EVENT_MAX_SLOTS_CHANGED 0x1B 53 #define HCI_EVENT_READ_CLOCK_OFFSET_COMPLETE 0x1C 54 #define NECTEVENT_ION_PACKET_TYPE_CHANGED 0x1D 55 #define HCI_EVENT_INQUIRY_RESULT_WITH_RSSI 0x22 56 #define HCI_EVENT_VENDOR_SPECIFIC 0xFF 57 58 // events from BTstack for application/client lib 59 #define BTSTACK_EVENT_HCI_WORKING 0x80 60 61 #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode) 62 63 64 /** 65 * Default INQ Mode 66 */ 67 #define HCI_INQUIRY_LAP 0x9E8B33L // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC) 68 69 /** 70 * @brief Length of a bluetooth device address. 71 */ 72 #define BD_ADDR_LEN 6 73 typedef uint8_t bd_addr_t[BD_ADDR_LEN]; 74 75 /** 76 * @brief The link key type 77 */ 78 #define LINK_KEY_LEN 16 79 typedef uint8_t link_key_t[LINK_KEY_LEN]; 80 81 /** 82 * @brief hci connection handle type 83 */ 84 typedef uint16_t hci_con_handle_t; 85 86 typedef enum { 87 HCI_POWER_OFF = 0, 88 HCI_POWER_ON 89 } HCI_POWER_MODE; 90 91 typedef enum { 92 HCI_STATE_OFF = 0, 93 HCI_STATE_INITIALIZING, 94 HCI_STATE_WORKING, 95 HCI_STATE_HALTING 96 } HCI_STATE; 97 98 typedef struct { 99 uint16_t opcode; 100 const char *format; 101 } hci_cmd_t; 102 103 typedef enum { 104 SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0, 105 SEND_PIN_CODE_RESPONSE = 1 << 1 106 } hci_connection_flags_t; 107 108 typedef struct hci_connection { 109 // linked list 110 struct hci_connection * next; 111 112 // remote side 113 bd_addr_t address; 114 hci_con_handle_t con_handle; 115 116 // hci state machine 117 hci_connection_flags_t flags; 118 119 } hci_connection_t; 120 121 122 typedef struct { 123 124 hci_transport_t * hci_transport; 125 uint8_t * hci_cmd_buffer; 126 hci_connection_t * connections; 127 128 /* host to controller flow control */ 129 uint8_t num_cmd_packets; 130 uint8_t num_acl_packets; 131 132 /* callback to L2CAP layer */ 133 void (*event_packet_handler)(uint8_t *packet, int size); 134 void (*acl_packet_handler) (uint8_t *packet, int size); 135 136 /* hci state machine */ 137 HCI_STATE state; 138 uint8_t substate; 139 uint8_t cmds_ready; 140 141 } hci_stack_t; 142 143 144 // set up HCI 145 void hci_init(hci_transport_t *transport, void *config); 146 147 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)); 148 149 void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size)); 150 151 // power control 152 int hci_power_control(HCI_POWER_MODE mode); 153 154 /** 155 * run the hci daemon loop once 156 * 157 * @return 0 or next timeout 158 */ 159 uint32_t hci_run(); 160 161 // 162 void hexdump(uint8_t *data, int size); 163 164 // create and send hci command packet based on a template and a list of parameters 165 int hci_send_cmd(hci_cmd_t *cmd, ...); 166 167 // send ACL packet 168 int hci_send_acl_packet(uint8_t *packet, int size); 169 170 // helper 171 extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 172 173 extern hci_cmd_t hci_inquiry; 174 extern hci_cmd_t hci_link_key_request_negative_reply; 175 extern hci_cmd_t hci_pin_code_request_reply; 176 extern hci_cmd_t hci_reset; 177 extern hci_cmd_t hci_create_connection; 178 extern hci_cmd_t hci_host_buffer_size; 179 extern hci_cmd_t hci_write_authentication_enable; 180 extern hci_cmd_t hci_write_page_timeout; 181 182