11f504dbdSmatthias.ringwald /* 21f504dbdSmatthias.ringwald * hci.h 31f504dbdSmatthias.ringwald * 41f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 51f504dbdSmatthias.ringwald * 61f504dbdSmatthias.ringwald */ 71f504dbdSmatthias.ringwald 81f504dbdSmatthias.ringwald #pragma once 91f504dbdSmatthias.ringwald 1093b8dc03Smatthias.ringwald #include <stdint.h> 1102ea9861Smatthias.ringwald #include <stdlib.h> 1293b8dc03Smatthias.ringwald 131f504dbdSmatthias.ringwald #include "hci_transport.h" 141f504dbdSmatthias.ringwald 15ea6387dfSmatthias.ringwald 16ea6387dfSmatthias.ringwald // helper for BT little endian format 17ea6387dfSmatthias.ringwald #define READ_BT_16( buffer, pos) (buffer[pos] | (buffer[pos+1] << 8)) 18ea6387dfSmatthias.ringwald #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16)) 19ea6387dfSmatthias.ringwald #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) 20ea6387dfSmatthias.ringwald 2143625864Smatthias.ringwald // #define STORE_BT_16( buffer, pos, value ) { buffer[pos] = (value) & 0xff; buffer[pos+1] = (value) >> 8; } 22ea6387dfSmatthias.ringwald 2322909952Smatthias.ringwald 24ea6387dfSmatthias.ringwald // packet headers 25ea6387dfSmatthias.ringwald #define HCI_CMD_DATA_PKT_HDR 0x03 26ea6387dfSmatthias.ringwald #define HCI_ACL_DATA_PKT_HDR 0x04 27ea6387dfSmatthias.ringwald #define HCI_SCO_DATA_PKT_HDR 0x03 28ea6387dfSmatthias.ringwald #define HCI_EVENT_PKT_HDR 0x02 29ea6387dfSmatthias.ringwald 30*3429f56bSmatthias.ringwald 31*3429f56bSmatthias.ringwald // Events from host controller to host 32*3429f56bSmatthias.ringwald #define HCI_EVENT_INQUIRY_COMPLETE 0x01 33*3429f56bSmatthias.ringwald #define HCI_EVENT_INQUIRY_RESULT 0x02 34*3429f56bSmatthias.ringwald #define HCI_EVENT_CONNECTION_COMPLETE 0x03 35*3429f56bSmatthias.ringwald #define HCI_EVENT_CONNECTION_REQUEST 0x04 36*3429f56bSmatthias.ringwald #define HCI_EVENT_DISCONNECTION_COMPLETE 0x05 37*3429f56bSmatthias.ringwald #define HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT 0x06 38*3429f56bSmatthias.ringwald #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07 39*3429f56bSmatthias.ringwald #define HCI_EVENT_ENCRIPTION_CHANGE 0x08 40*3429f56bSmatthias.ringwald #define HCI_EVENT_CHANGE_CONNECTION_LINK_KEY_COMPLETE 0x09 41*3429f56bSmatthias.ringwald #define HCI_EVENT_MASTER_LINK_KEY_COMPLETE 0x0A 42*3429f56bSmatthias.ringwald #define HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE 0x0B 43*3429f56bSmatthias.ringwald #define HCI_EVENT_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C 44*3429f56bSmatthias.ringwald #define HCI_EVENT_QOS_SETUP_COMPLETE 0x0D 45*3429f56bSmatthias.ringwald #define HCI_EVENT_COMMAND_COMPLETE 0x0E 46*3429f56bSmatthias.ringwald #define HCI_EVENT_COMMAND_STATUS 0x0F 47*3429f56bSmatthias.ringwald #define HCI_EVENT_HARDWARE_ERROR 0x10 48*3429f56bSmatthias.ringwald #define HCI_EVENT_FLUSH_OCCURED 0x11 49*3429f56bSmatthias.ringwald #define HCI_EVENT_ROLE_CHANGE 0x12 50*3429f56bSmatthias.ringwald #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS 0x13 51*3429f56bSmatthias.ringwald #define HCI_EVENT_MODE_CHANGE_EVENT 0x14 52*3429f56bSmatthias.ringwald #define HCI_EVENT_RETURN_LINK_KEYS 0x15 53*3429f56bSmatthias.ringwald #define HCI_EVENT_PIN_CODE_REQUEST 0x16 54*3429f56bSmatthias.ringwald #define HCI_EVENT_LINK_KEY_REQUEST 0x17 55*3429f56bSmatthias.ringwald #define HCI_EVENT_LINK_KEY_NOTIFICATION 0x18 56*3429f56bSmatthias.ringwald #define HCI_EVENT_DATA_BUFFER_OVERFLOW 0x1A 57*3429f56bSmatthias.ringwald #define HCI_EVENT_MAX_SLOTS_CHANGED 0x1B 58*3429f56bSmatthias.ringwald #define HCI_EVENT_READ_CLOCK_OFFSET_COMPLETE 0x1C 59*3429f56bSmatthias.ringwald #define NECTEVENT_ION_PACKET_TYPE_CHANGED 0x1D 60*3429f56bSmatthias.ringwald #define HCI_EVENT_INQUIRY_RESULT_WITH_RSSI 0x22 61*3429f56bSmatthias.ringwald #define HCI_EVENT_VENDOR_SPECIFIC 0xFF 62*3429f56bSmatthias.ringwald 63*3429f56bSmatthias.ringwald // events from BTstack for application/client lib 64*3429f56bSmatthias.ringwald #define BTSTACK_EVENT_HCI_WORKING 0x80 65*3429f56bSmatthias.ringwald 66*3429f56bSmatthias.ringwald #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode) 67*3429f56bSmatthias.ringwald 68ea6387dfSmatthias.ringwald /** 69ea6387dfSmatthias.ringwald * @brief Length of a bluetooth device address. 70ea6387dfSmatthias.ringwald */ 71ea6387dfSmatthias.ringwald #define BD_ADDR_LEN 6 72ea6387dfSmatthias.ringwald typedef uint8_t bd_addr_t[BD_ADDR_LEN]; 73ea6387dfSmatthias.ringwald 74ea6387dfSmatthias.ringwald /** 75ea6387dfSmatthias.ringwald * @brief The link key type 76ea6387dfSmatthias.ringwald */ 77ea6387dfSmatthias.ringwald #define LINK_KEY_LEN 16 78ea6387dfSmatthias.ringwald typedef uint8_t link_key_t[LINK_KEY_LEN]; 79ea6387dfSmatthias.ringwald 8043625864Smatthias.ringwald /** 8143625864Smatthias.ringwald * @brief hci connection handle type 8243625864Smatthias.ringwald */ 8343625864Smatthias.ringwald typedef uint16_t hci_con_handle_t; 84ea6387dfSmatthias.ringwald 85475c8125Smatthias.ringwald typedef enum { 86475c8125Smatthias.ringwald HCI_POWER_OFF = 0, 87475c8125Smatthias.ringwald HCI_POWER_ON 88475c8125Smatthias.ringwald } HCI_POWER_MODE; 89475c8125Smatthias.ringwald 90*3429f56bSmatthias.ringwald typedef enum { 91*3429f56bSmatthias.ringwald HCI_STATE_OFF = 0, 92*3429f56bSmatthias.ringwald HCI_STATE_INITIALIZING, 93*3429f56bSmatthias.ringwald HCI_STATE_WORKING, 94*3429f56bSmatthias.ringwald HCI_STATE_HALTING 95*3429f56bSmatthias.ringwald } HCI_STATE; 96*3429f56bSmatthias.ringwald 9793b8dc03Smatthias.ringwald typedef struct { 980a974e0cSmatthias.ringwald uint16_t opcode; 9993b8dc03Smatthias.ringwald const char *format; 10093b8dc03Smatthias.ringwald } hci_cmd_t; 10193b8dc03Smatthias.ringwald 10216833f0aSmatthias.ringwald typedef struct hci_connection { 10316833f0aSmatthias.ringwald struct hci_connection * next; 10416833f0aSmatthias.ringwald bd_addr_t address; 10516833f0aSmatthias.ringwald hci_con_handle_t con_handle; 10616833f0aSmatthias.ringwald } hci_connection_t; 10716833f0aSmatthias.ringwald 10816833f0aSmatthias.ringwald typedef struct { 10916833f0aSmatthias.ringwald 11016833f0aSmatthias.ringwald hci_transport_t * hci_transport; 11116833f0aSmatthias.ringwald uint8_t * hci_cmd_buffer; 11216833f0aSmatthias.ringwald hci_connection_t * connections; 11316833f0aSmatthias.ringwald 11416833f0aSmatthias.ringwald /* host to controller flow control */ 11516833f0aSmatthias.ringwald uint8_t num_cmd_packets; 11616833f0aSmatthias.ringwald uint8_t num_acl_packets; 11716833f0aSmatthias.ringwald 11816833f0aSmatthias.ringwald /* callback to L2CAP layer */ 11916833f0aSmatthias.ringwald void (*event_packet_handler)(uint8_t *packet, int size); 12016833f0aSmatthias.ringwald void (*acl_packet_handler) (uint8_t *packet, int size); 12116833f0aSmatthias.ringwald 122*3429f56bSmatthias.ringwald /* hci state machine */ 123*3429f56bSmatthias.ringwald HCI_STATE state; 124*3429f56bSmatthias.ringwald uint8_t substate; 125*3429f56bSmatthias.ringwald uint8_t cmds_ready; 126*3429f56bSmatthias.ringwald 12716833f0aSmatthias.ringwald } hci_stack_t; 12816833f0aSmatthias.ringwald 12902ea9861Smatthias.ringwald 130475c8125Smatthias.ringwald // set up HCI 131475c8125Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config); 132475c8125Smatthias.ringwald 13316833f0aSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)); 13416833f0aSmatthias.ringwald 13516833f0aSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size)); 13616833f0aSmatthias.ringwald 137475c8125Smatthias.ringwald // power control 138475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE mode); 139475c8125Smatthias.ringwald 140*3429f56bSmatthias.ringwald /** 141*3429f56bSmatthias.ringwald * run the hci daemon loop once 142*3429f56bSmatthias.ringwald * 143*3429f56bSmatthias.ringwald * @return 0 or next timeout 144*3429f56bSmatthias.ringwald */ 145*3429f56bSmatthias.ringwald uint32_t hci_run(); 1461f504dbdSmatthias.ringwald 147554588a5Smatthias.ringwald // 148554588a5Smatthias.ringwald void hexdump(uint8_t *data, int size); 149554588a5Smatthias.ringwald 15002ea9861Smatthias.ringwald // create and send hci command packet based on a template and a list of parameters 15102ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...); 152554588a5Smatthias.ringwald 15343625864Smatthias.ringwald // send ACL packet 15443625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size); 15543625864Smatthias.ringwald 15643625864Smatthias.ringwald // helper 15743625864Smatthias.ringwald extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 15843625864Smatthias.ringwald 15993b8dc03Smatthias.ringwald extern hci_cmd_t hci_inquiry; 1603091b266Smatthias.ringwald extern hci_cmd_t hci_link_key_request_negative_reply; 1613091b266Smatthias.ringwald extern hci_cmd_t hci_pin_code_request_reply; 16293b8dc03Smatthias.ringwald extern hci_cmd_t hci_reset; 16302ea9861Smatthias.ringwald extern hci_cmd_t hci_create_connection; 16402ea9861Smatthias.ringwald extern hci_cmd_t hci_host_buffer_size; 1653091b266Smatthias.ringwald extern hci_cmd_t hci_write_authentication_enable; 16602ea9861Smatthias.ringwald extern hci_cmd_t hci_write_page_timeout; 16793b8dc03Smatthias.ringwald 16893b8dc03Smatthias.ringwald #define HCI_INQUIRY_LAP 0x9E8B33L // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC) 169