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 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 // #define STORE_BT_16( buffer, pos, value ) { buffer[pos] = (value) & 0xff; buffer[pos+1] = (value) >> 8; } 22 23 #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == 0x0e && READ_BT_16(event,3) == cmd.opcode) 24 25 // packet headers 26 #define HCI_CMD_DATA_PKT_HDR 0x03 27 #define HCI_ACL_DATA_PKT_HDR 0x04 28 #define HCI_SCO_DATA_PKT_HDR 0x03 29 #define HCI_EVENT_PKT_HDR 0x02 30 31 /** 32 * @brief Length of a bluetooth device address. 33 */ 34 #define BD_ADDR_LEN 6 35 typedef uint8_t bd_addr_t[BD_ADDR_LEN]; 36 37 /** 38 * @brief The link key type 39 */ 40 #define LINK_KEY_LEN 16 41 typedef uint8_t link_key_t[LINK_KEY_LEN]; 42 43 /** 44 * @brief hci connection handle type 45 */ 46 typedef uint16_t hci_con_handle_t; 47 48 typedef enum { 49 HCI_POWER_OFF = 0, 50 HCI_POWER_ON 51 } HCI_POWER_MODE; 52 53 typedef struct { 54 uint16_t opcode; 55 const char *format; 56 } hci_cmd_t; 57 58 typedef struct hci_connection { 59 struct hci_connection * next; 60 bd_addr_t address; 61 hci_con_handle_t con_handle; 62 } hci_connection_t; 63 64 typedef struct { 65 66 hci_transport_t * hci_transport; 67 uint8_t * hci_cmd_buffer; 68 hci_connection_t *connections; 69 70 /* host to controller flow control */ 71 uint8_t num_cmd_packets; 72 uint8_t num_acl_packets; 73 74 /* callback to L2CAP layer */ 75 void (*event_packet_handler)(uint8_t *packet, int size); 76 void (*acl_packet_handler) (uint8_t *packet, int size); 77 78 } hci_stack_t; 79 80 81 // set up HCI 82 void hci_init(hci_transport_t *transport, void *config); 83 84 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)); 85 86 void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size)); 87 88 // power control 89 int hci_power_control(HCI_POWER_MODE mode); 90 91 // run the hci daemon loop 92 void hci_run(); 93 94 // 95 void hexdump(uint8_t *data, int size); 96 97 // create and send hci command packet based on a template and a list of parameters 98 int hci_send_cmd(hci_cmd_t *cmd, ...); 99 100 // send ACL packet 101 int hci_send_acl_packet(uint8_t *packet, int size); 102 103 104 105 // helper 106 extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 107 108 extern hci_cmd_t hci_inquiry; 109 extern hci_cmd_t hci_link_key_request_negative_reply; 110 extern hci_cmd_t hci_pin_code_request_reply; 111 extern hci_cmd_t hci_reset; 112 extern hci_cmd_t hci_create_connection; 113 extern hci_cmd_t hci_host_buffer_size; 114 extern hci_cmd_t hci_write_authentication_enable; 115 extern hci_cmd_t hci_write_page_timeout; 116 117 #define HCI_INQUIRY_LAP 0x9E8B33L // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC) 118