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 23ea6387dfSmatthias.ringwald // packet headers 24ea6387dfSmatthias.ringwald #define HCI_CMD_DATA_PKT_HDR 0x03 25ea6387dfSmatthias.ringwald #define HCI_ACL_DATA_PKT_HDR 0x04 26ea6387dfSmatthias.ringwald #define HCI_SCO_DATA_PKT_HDR 0x03 27ea6387dfSmatthias.ringwald #define HCI_EVENT_PKT_HDR 0x02 28ea6387dfSmatthias.ringwald 29ea6387dfSmatthias.ringwald /** 30ea6387dfSmatthias.ringwald * @brief Length of a bluetooth device address. 31ea6387dfSmatthias.ringwald */ 32ea6387dfSmatthias.ringwald #define BD_ADDR_LEN 6 33ea6387dfSmatthias.ringwald typedef uint8_t bd_addr_t[BD_ADDR_LEN]; 34ea6387dfSmatthias.ringwald 35ea6387dfSmatthias.ringwald /** 36ea6387dfSmatthias.ringwald * @brief The link key type 37ea6387dfSmatthias.ringwald */ 38ea6387dfSmatthias.ringwald #define LINK_KEY_LEN 16 39ea6387dfSmatthias.ringwald typedef uint8_t link_key_t[LINK_KEY_LEN]; 40ea6387dfSmatthias.ringwald 4143625864Smatthias.ringwald /** 4243625864Smatthias.ringwald * @brief hci connection handle type 4343625864Smatthias.ringwald */ 4443625864Smatthias.ringwald typedef uint16_t hci_con_handle_t; 45ea6387dfSmatthias.ringwald 46475c8125Smatthias.ringwald typedef enum { 47475c8125Smatthias.ringwald HCI_POWER_OFF = 0, 48475c8125Smatthias.ringwald HCI_POWER_ON 49475c8125Smatthias.ringwald } HCI_POWER_MODE; 50475c8125Smatthias.ringwald 5193b8dc03Smatthias.ringwald typedef struct { 520a974e0cSmatthias.ringwald uint16_t opcode; 5393b8dc03Smatthias.ringwald const char *format; 5493b8dc03Smatthias.ringwald } hci_cmd_t; 5593b8dc03Smatthias.ringwald 56*16833f0aSmatthias.ringwald typedef struct hci_connection { 57*16833f0aSmatthias.ringwald struct hci_connection * next; 58*16833f0aSmatthias.ringwald bd_addr_t address; 59*16833f0aSmatthias.ringwald hci_con_handle_t con_handle; 60*16833f0aSmatthias.ringwald } hci_connection_t; 61*16833f0aSmatthias.ringwald 62*16833f0aSmatthias.ringwald typedef struct { 63*16833f0aSmatthias.ringwald 64*16833f0aSmatthias.ringwald hci_transport_t * hci_transport; 65*16833f0aSmatthias.ringwald uint8_t * hci_cmd_buffer; 66*16833f0aSmatthias.ringwald hci_connection_t *connections; 67*16833f0aSmatthias.ringwald 68*16833f0aSmatthias.ringwald /* host to controller flow control */ 69*16833f0aSmatthias.ringwald uint8_t num_cmd_packets; 70*16833f0aSmatthias.ringwald uint8_t num_acl_packets; 71*16833f0aSmatthias.ringwald 72*16833f0aSmatthias.ringwald /* callback to L2CAP layer */ 73*16833f0aSmatthias.ringwald void (*event_packet_handler)(uint8_t *packet, int size); 74*16833f0aSmatthias.ringwald void (*acl_packet_handler) (uint8_t *packet, int size); 75*16833f0aSmatthias.ringwald 76*16833f0aSmatthias.ringwald } hci_stack_t; 77*16833f0aSmatthias.ringwald 7802ea9861Smatthias.ringwald 79475c8125Smatthias.ringwald // set up HCI 80475c8125Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config); 81475c8125Smatthias.ringwald 82*16833f0aSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)); 83*16833f0aSmatthias.ringwald 84*16833f0aSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size)); 85*16833f0aSmatthias.ringwald 86475c8125Smatthias.ringwald // power control 87475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE mode); 88475c8125Smatthias.ringwald 891f504dbdSmatthias.ringwald // run the hci daemon loop 90475c8125Smatthias.ringwald void hci_run(); 911f504dbdSmatthias.ringwald 92554588a5Smatthias.ringwald // 93554588a5Smatthias.ringwald void hexdump(uint8_t *data, int size); 94554588a5Smatthias.ringwald 9502ea9861Smatthias.ringwald // create and send hci command packet based on a template and a list of parameters 9602ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...); 97554588a5Smatthias.ringwald 9843625864Smatthias.ringwald // send ACL packet 9943625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size); 10043625864Smatthias.ringwald 101*16833f0aSmatthias.ringwald 102*16833f0aSmatthias.ringwald 10343625864Smatthias.ringwald // helper 10443625864Smatthias.ringwald extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 10543625864Smatthias.ringwald 10693b8dc03Smatthias.ringwald extern hci_cmd_t hci_inquiry; 1073091b266Smatthias.ringwald extern hci_cmd_t hci_link_key_request_negative_reply; 1083091b266Smatthias.ringwald extern hci_cmd_t hci_pin_code_request_reply; 10993b8dc03Smatthias.ringwald extern hci_cmd_t hci_reset; 11002ea9861Smatthias.ringwald extern hci_cmd_t hci_create_connection; 11102ea9861Smatthias.ringwald extern hci_cmd_t hci_host_buffer_size; 1123091b266Smatthias.ringwald extern hci_cmd_t hci_write_authentication_enable; 11302ea9861Smatthias.ringwald extern hci_cmd_t hci_write_page_timeout; 11493b8dc03Smatthias.ringwald 11593b8dc03Smatthias.ringwald #define HCI_INQUIRY_LAP 0x9E8B33L // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC) 116