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> 121cd208adSmatthias.ringwald #include <stdarg.h> 1393b8dc03Smatthias.ringwald 141f504dbdSmatthias.ringwald #include "hci_transport.h" 1511e23e5fSmatthias.ringwald #include "bt_control.h" 161f504dbdSmatthias.ringwald 17ea6387dfSmatthias.ringwald // helper for BT little endian format 18*b9fa6cb5Smatthias.ringwald #define READ_BT_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[pos+1]) << 8)) 19ea6387dfSmatthias.ringwald #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16)) 20ea6387dfSmatthias.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) 21ea6387dfSmatthias.ringwald 22bd67ef2fSmatthias.ringwald // calculate combined ogf/ocf value 23bd67ef2fSmatthias.ringwald #define OPCODE(ogf, ocf) (ocf | ogf << 10) 24bd67ef2fSmatthias.ringwald 258adf0ddaSmatthias.ringwald // get HCI CMD OGF 268adf0ddaSmatthias.ringwald #define READ_CMD_OGF(buffer) (buffer[1] >> 2) 278adf0ddaSmatthias.ringwald #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0]) 288adf0ddaSmatthias.ringwald 2997addcc5Smatthias.ringwald // packet header lengh 30ea6387dfSmatthias.ringwald #define HCI_CMD_DATA_PKT_HDR 0x03 31ea6387dfSmatthias.ringwald #define HCI_ACL_DATA_PKT_HDR 0x04 32ea6387dfSmatthias.ringwald #define HCI_SCO_DATA_PKT_HDR 0x03 33ea6387dfSmatthias.ringwald #define HCI_EVENT_PKT_HDR 0x02 34ea6387dfSmatthias.ringwald 35bd67ef2fSmatthias.ringwald // OGFs 36bd67ef2fSmatthias.ringwald #define OGF_LINK_CONTROL 0x01 37bd67ef2fSmatthias.ringwald #define OGF_CONTROLLER_BASEBAND 0x03 38bd67ef2fSmatthias.ringwald #define OGF_INFORMATIONAL_PARAMETERS 0x04 398adf0ddaSmatthias.ringwald #define OGF_BTSTACK 0x3d 408adf0ddaSmatthias.ringwald #define OGF_VENDOR 0x3f 41bd67ef2fSmatthias.ringwald 423429f56bSmatthias.ringwald // Events from host controller to host 433429f56bSmatthias.ringwald #define HCI_EVENT_INQUIRY_COMPLETE 0x01 443429f56bSmatthias.ringwald #define HCI_EVENT_INQUIRY_RESULT 0x02 453429f56bSmatthias.ringwald #define HCI_EVENT_CONNECTION_COMPLETE 0x03 463429f56bSmatthias.ringwald #define HCI_EVENT_CONNECTION_REQUEST 0x04 473429f56bSmatthias.ringwald #define HCI_EVENT_DISCONNECTION_COMPLETE 0x05 483429f56bSmatthias.ringwald #define HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT 0x06 493429f56bSmatthias.ringwald #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07 503429f56bSmatthias.ringwald #define HCI_EVENT_ENCRIPTION_CHANGE 0x08 513429f56bSmatthias.ringwald #define HCI_EVENT_CHANGE_CONNECTION_LINK_KEY_COMPLETE 0x09 523429f56bSmatthias.ringwald #define HCI_EVENT_MASTER_LINK_KEY_COMPLETE 0x0A 533429f56bSmatthias.ringwald #define HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE 0x0B 543429f56bSmatthias.ringwald #define HCI_EVENT_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C 553429f56bSmatthias.ringwald #define HCI_EVENT_QOS_SETUP_COMPLETE 0x0D 563429f56bSmatthias.ringwald #define HCI_EVENT_COMMAND_COMPLETE 0x0E 573429f56bSmatthias.ringwald #define HCI_EVENT_COMMAND_STATUS 0x0F 583429f56bSmatthias.ringwald #define HCI_EVENT_HARDWARE_ERROR 0x10 593429f56bSmatthias.ringwald #define HCI_EVENT_FLUSH_OCCURED 0x11 603429f56bSmatthias.ringwald #define HCI_EVENT_ROLE_CHANGE 0x12 613429f56bSmatthias.ringwald #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS 0x13 623429f56bSmatthias.ringwald #define HCI_EVENT_MODE_CHANGE_EVENT 0x14 633429f56bSmatthias.ringwald #define HCI_EVENT_RETURN_LINK_KEYS 0x15 643429f56bSmatthias.ringwald #define HCI_EVENT_PIN_CODE_REQUEST 0x16 653429f56bSmatthias.ringwald #define HCI_EVENT_LINK_KEY_REQUEST 0x17 663429f56bSmatthias.ringwald #define HCI_EVENT_LINK_KEY_NOTIFICATION 0x18 673429f56bSmatthias.ringwald #define HCI_EVENT_DATA_BUFFER_OVERFLOW 0x1A 683429f56bSmatthias.ringwald #define HCI_EVENT_MAX_SLOTS_CHANGED 0x1B 693429f56bSmatthias.ringwald #define HCI_EVENT_READ_CLOCK_OFFSET_COMPLETE 0x1C 703429f56bSmatthias.ringwald #define NECTEVENT_ION_PACKET_TYPE_CHANGED 0x1D 713429f56bSmatthias.ringwald #define HCI_EVENT_INQUIRY_RESULT_WITH_RSSI 0x22 723429f56bSmatthias.ringwald #define HCI_EVENT_VENDOR_SPECIFIC 0xFF 733429f56bSmatthias.ringwald 743429f56bSmatthias.ringwald // events from BTstack for application/client lib 758adf0ddaSmatthias.ringwald #define HCI_EVENT_BTSTACK_WORKING 0x80 768adf0ddaSmatthias.ringwald #define HCI_EVENT_BTSTACK_STATE 0x81 778adf0ddaSmatthias.ringwald 788adf0ddaSmatthias.ringwald // cmds for BTstack 798adf0ddaSmatthias.ringwald #define HCI_BTSTACK_GET_STATE 0x01 803429f56bSmatthias.ringwald 813429f56bSmatthias.ringwald #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode) 823429f56bSmatthias.ringwald 8397addcc5Smatthias.ringwald 8497addcc5Smatthias.ringwald /** 8597addcc5Smatthias.ringwald * Default INQ Mode 8697addcc5Smatthias.ringwald */ 8797addcc5Smatthias.ringwald #define HCI_INQUIRY_LAP 0x9E8B33L // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC) 8897addcc5Smatthias.ringwald 89ea6387dfSmatthias.ringwald /** 90ea6387dfSmatthias.ringwald * @brief Length of a bluetooth device address. 91ea6387dfSmatthias.ringwald */ 92ea6387dfSmatthias.ringwald #define BD_ADDR_LEN 6 93ea6387dfSmatthias.ringwald typedef uint8_t bd_addr_t[BD_ADDR_LEN]; 94ea6387dfSmatthias.ringwald 95ea6387dfSmatthias.ringwald /** 96ea6387dfSmatthias.ringwald * @brief The link key type 97ea6387dfSmatthias.ringwald */ 98ea6387dfSmatthias.ringwald #define LINK_KEY_LEN 16 99ea6387dfSmatthias.ringwald typedef uint8_t link_key_t[LINK_KEY_LEN]; 100ea6387dfSmatthias.ringwald 10143625864Smatthias.ringwald /** 10243625864Smatthias.ringwald * @brief hci connection handle type 10343625864Smatthias.ringwald */ 10443625864Smatthias.ringwald typedef uint16_t hci_con_handle_t; 105ea6387dfSmatthias.ringwald 106475c8125Smatthias.ringwald typedef enum { 107475c8125Smatthias.ringwald HCI_POWER_OFF = 0, 108475c8125Smatthias.ringwald HCI_POWER_ON 109475c8125Smatthias.ringwald } HCI_POWER_MODE; 110475c8125Smatthias.ringwald 1113429f56bSmatthias.ringwald typedef enum { 1123429f56bSmatthias.ringwald HCI_STATE_OFF = 0, 1133429f56bSmatthias.ringwald HCI_STATE_INITIALIZING, 1143429f56bSmatthias.ringwald HCI_STATE_WORKING, 1153429f56bSmatthias.ringwald HCI_STATE_HALTING 1163429f56bSmatthias.ringwald } HCI_STATE; 1173429f56bSmatthias.ringwald 11893b8dc03Smatthias.ringwald typedef struct { 1190a974e0cSmatthias.ringwald uint16_t opcode; 12093b8dc03Smatthias.ringwald const char *format; 12193b8dc03Smatthias.ringwald } hci_cmd_t; 12293b8dc03Smatthias.ringwald 123c01e9cbdSmatthias.ringwald typedef enum { 124c01e9cbdSmatthias.ringwald SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0, 125c01e9cbdSmatthias.ringwald SEND_PIN_CODE_RESPONSE = 1 << 1 126c01e9cbdSmatthias.ringwald } hci_connection_flags_t; 127c01e9cbdSmatthias.ringwald 12816833f0aSmatthias.ringwald typedef struct hci_connection { 129c01e9cbdSmatthias.ringwald // linked list 13016833f0aSmatthias.ringwald struct hci_connection * next; 131c01e9cbdSmatthias.ringwald 132c01e9cbdSmatthias.ringwald // remote side 13316833f0aSmatthias.ringwald bd_addr_t address; 13416833f0aSmatthias.ringwald hci_con_handle_t con_handle; 135c01e9cbdSmatthias.ringwald 136c01e9cbdSmatthias.ringwald // hci state machine 137c01e9cbdSmatthias.ringwald hci_connection_flags_t flags; 138c01e9cbdSmatthias.ringwald 13916833f0aSmatthias.ringwald } hci_connection_t; 14016833f0aSmatthias.ringwald 141c01e9cbdSmatthias.ringwald 14216833f0aSmatthias.ringwald typedef struct { 14316833f0aSmatthias.ringwald 14416833f0aSmatthias.ringwald hci_transport_t * hci_transport; 14511e23e5fSmatthias.ringwald bt_control_t * control; 14611e23e5fSmatthias.ringwald void * config; 14711e23e5fSmatthias.ringwald 14816833f0aSmatthias.ringwald uint8_t * hci_cmd_buffer; 14916833f0aSmatthias.ringwald hci_connection_t * connections; 15016833f0aSmatthias.ringwald 15116833f0aSmatthias.ringwald /* host to controller flow control */ 15216833f0aSmatthias.ringwald uint8_t num_cmd_packets; 15316833f0aSmatthias.ringwald uint8_t num_acl_packets; 15416833f0aSmatthias.ringwald 15516833f0aSmatthias.ringwald /* callback to L2CAP layer */ 1561cd208adSmatthias.ringwald void (*event_packet_handler)(uint8_t *packet, uint16_t size); 1571cd208adSmatthias.ringwald void (*acl_packet_handler) (uint8_t *packet, uint16_t size); 15816833f0aSmatthias.ringwald 1593429f56bSmatthias.ringwald /* hci state machine */ 1603429f56bSmatthias.ringwald HCI_STATE state; 1613429f56bSmatthias.ringwald uint8_t substate; 1623429f56bSmatthias.ringwald uint8_t cmds_ready; 1633429f56bSmatthias.ringwald 16416833f0aSmatthias.ringwald } hci_stack_t; 16516833f0aSmatthias.ringwald 16602ea9861Smatthias.ringwald 167475c8125Smatthias.ringwald // set up HCI 16811e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control); 169475c8125Smatthias.ringwald 1701cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)); 17116833f0aSmatthias.ringwald 1721cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)); 17316833f0aSmatthias.ringwald 174475c8125Smatthias.ringwald // power control 175475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE mode); 176475c8125Smatthias.ringwald 1773429f56bSmatthias.ringwald /** 1783429f56bSmatthias.ringwald * run the hci daemon loop once 1793429f56bSmatthias.ringwald * 1803429f56bSmatthias.ringwald * @return 0 or next timeout 1813429f56bSmatthias.ringwald */ 1823429f56bSmatthias.ringwald uint32_t hci_run(); 1831f504dbdSmatthias.ringwald 184554588a5Smatthias.ringwald // 18502582713Smatthias.ringwald void hexdump(void *data, int size); 186554588a5Smatthias.ringwald 1871cd208adSmatthias.ringwald // create and send hci command packets based on a template and a list of parameters 1881cd208adSmatthias.ringwald uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, va_list argptr); 1891cd208adSmatthias.ringwald uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); 19002ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...); 19149857181Smatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size); 192554588a5Smatthias.ringwald 19343625864Smatthias.ringwald // send ACL packet 19443625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size); 19543625864Smatthias.ringwald 19643625864Smatthias.ringwald // helper 19743625864Smatthias.ringwald extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 1988b658ebcSmatthias.ringwald extern void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); 19968d92d03Smatthias.ringwald extern void bt_flip_addr(bd_addr_t dest, bd_addr_t src); 20043625864Smatthias.ringwald 2013b9efdc1Smatthias.ringwald // HCI Commands - see hci.c for info on parameters 20293b8dc03Smatthias.ringwald extern hci_cmd_t hci_inquiry; 2033b9efdc1Smatthias.ringwald extern hci_cmd_t hci_inquiry_cancel; 2043091b266Smatthias.ringwald extern hci_cmd_t hci_link_key_request_negative_reply; 2053091b266Smatthias.ringwald extern hci_cmd_t hci_pin_code_request_reply; 206bd67ef2fSmatthias.ringwald extern hci_cmd_t hci_set_event_mask; 20793b8dc03Smatthias.ringwald extern hci_cmd_t hci_reset; 20802ea9861Smatthias.ringwald extern hci_cmd_t hci_create_connection; 20902ea9861Smatthias.ringwald extern hci_cmd_t hci_host_buffer_size; 2103091b266Smatthias.ringwald extern hci_cmd_t hci_write_authentication_enable; 211bd67ef2fSmatthias.ringwald extern hci_cmd_t hci_write_local_name; 21202ea9861Smatthias.ringwald extern hci_cmd_t hci_write_page_timeout; 213bd67ef2fSmatthias.ringwald extern hci_cmd_t hci_write_class_of_device; 214aff8ac5cSmatthias.ringwald extern hci_cmd_t hci_remote_name_request; 215aff8ac5cSmatthias.ringwald extern hci_cmd_t hci_remote_name_request_cancel; 21668d92d03Smatthias.ringwald extern hci_cmd_t hci_read_bd_addr; 217e521ef40Smatthias.ringwald extern hci_cmd_t hci_delete_stored_link_key; 218f432a6ddSmatthias.ringwald extern hci_cmd_t hci_write_scan_enable; 219f432a6ddSmatthias.ringwald extern hci_cmd_t hci_accept_connection_request; 220bd67ef2fSmatthias.ringwald extern hci_cmd_t hci_write_inquiry_mode; 221bd67ef2fSmatthias.ringwald extern hci_cmd_t hci_write_extended_inquiry_response; 222bd67ef2fSmatthias.ringwald extern hci_cmd_t hci_write_simple_pairing_mode; 2238adf0ddaSmatthias.ringwald 2248adf0ddaSmatthias.ringwald // BTSTACK client/server commands - see hci.c for info on parameters 2258adf0ddaSmatthias.ringwald extern hci_cmd_t hci_get_btstack_state;