1 /* 2 * hci.h 3 * 4 * Created by Matthias Ringwald on 4/29/09. 5 * 6 */ 7 8 #pragma once 9 10 #include "hci_cmds.h" 11 #include "utils.h" 12 #include "hci_transport.h" 13 #include "bt_control.h" 14 15 #include <stdint.h> 16 #include <stdlib.h> 17 #include <stdarg.h> 18 19 /** 20 * Connection State 21 */ 22 typedef enum { 23 SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0, 24 SEND_PIN_CODE_RESPONSE = 1 << 1 25 } hci_connection_flags_t; 26 27 typedef enum { 28 SENT_CREATE_CONNECTION = 1, 29 RECEIVED_CONNECTION_REQUEST, 30 ACCEPTED_CONNECTION_REQUEST, 31 REJECTED_CONNECTION_REQUEST, 32 OPEN, 33 SENT_DISCONNECT 34 } CONNECTION_STATE; 35 36 typedef enum { 37 BLUETOOTH_OFF = 1, 38 BLUETOOTH_ON, 39 BLUETOOTH_ACTIVE 40 } BLUETOOTH_STATE; 41 42 typedef struct { 43 // linked list - assert: first field 44 linked_item_t item; 45 46 // remote side 47 bd_addr_t address; 48 49 // module handle 50 hci_con_handle_t con_handle; 51 52 // state 53 CONNECTION_STATE state; 54 55 // errands 56 hci_connection_flags_t flags; 57 58 // timer 59 timer_t timeout; 60 struct timeval timestamp; 61 62 } hci_connection_t; 63 64 /** 65 * main data structure 66 */ 67 typedef struct { 68 // transport component with configuration 69 hci_transport_t * hci_transport; 70 void * config; 71 72 // hardware power controller 73 bt_control_t * control; 74 75 // list of existing baseband connections 76 linked_list_t connections; 77 78 // single buffer for HCI Command assembly 79 uint8_t * hci_cmd_buffer; 80 81 /* host to controller flow control */ 82 uint8_t num_cmd_packets; 83 uint8_t num_acl_packets; 84 85 /* callback to L2CAP layer */ 86 void (*event_packet_handler)(uint8_t *packet, uint16_t size); 87 void (*acl_packet_handler) (uint8_t *packet, uint16_t size); 88 89 /* hci state machine */ 90 HCI_STATE state; 91 uint8_t substate; 92 uint8_t cmds_ready; 93 94 } hci_stack_t; 95 96 // set up HCI 97 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control); 98 99 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)); 100 101 void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)); 102 103 // power control 104 int hci_power_control(HCI_POWER_MODE mode); 105 106 /** 107 * run the hci control loop once 108 */ 109 void hci_run(); 110 111 // create and send hci command packets based on a template and a list of parameters 112 int hci_send_cmd(hci_cmd_t *cmd, ...); 113 114 // send complete CMD packet 115 int hci_send_cmd_packet(uint8_t *packet, int size); 116 117 // send ACL packet 118 int hci_send_acl_packet(uint8_t *packet, int size); 119 120 // 121 void hci_emit_state(); 122 void hci_emit_connection_complete(hci_connection_t *conn); 123 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 124 void hci_emit_nr_connections_changed(); 125 void hci_emit_hci_open_failed(); 126