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