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