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