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