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 10ba681a6cSmatthias.ringwald #include "hci_cmds.h" 11ba681a6cSmatthias.ringwald #include "utils.h" 12ba681a6cSmatthias.ringwald #include "hci_transport.h" 13ba681a6cSmatthias.ringwald #include "bt_control.h" 14ba681a6cSmatthias.ringwald 1593b8dc03Smatthias.ringwald #include <stdint.h> 1602ea9861Smatthias.ringwald #include <stdlib.h> 171cd208adSmatthias.ringwald #include <stdarg.h> 1893b8dc03Smatthias.ringwald 1906b35ec0Smatthias.ringwald /** 2006b35ec0Smatthias.ringwald * Connection State 2106b35ec0Smatthias.ringwald */ 22c01e9cbdSmatthias.ringwald typedef enum { 23c01e9cbdSmatthias.ringwald SEND_NEGATIVE_LINK_KEY_REQUEST = 1 << 0, 24c01e9cbdSmatthias.ringwald SEND_PIN_CODE_RESPONSE = 1 << 1 25c01e9cbdSmatthias.ringwald } hci_connection_flags_t; 26c01e9cbdSmatthias.ringwald 27c8e4258aSmatthias.ringwald typedef enum { 28c8e4258aSmatthias.ringwald SENT_CREATE_CONNECTION = 1, 29c8e4258aSmatthias.ringwald RECEIVED_CONNECTION_REQUEST, 30c8e4258aSmatthias.ringwald ACCEPTED_CONNECTION_REQUEST, 31c8e4258aSmatthias.ringwald REJECTED_CONNECTION_REQUEST, 32c8e4258aSmatthias.ringwald OPEN, 33c8e4258aSmatthias.ringwald SENT_DISCONNECT 34c8e4258aSmatthias.ringwald } CONNECTION_STATE; 35c8e4258aSmatthias.ringwald 36c8e4258aSmatthias.ringwald typedef struct { 3706b35ec0Smatthias.ringwald // linked list - assert: first field 3806b35ec0Smatthias.ringwald linked_item_t item; 39c01e9cbdSmatthias.ringwald 40c01e9cbdSmatthias.ringwald // remote side 4116833f0aSmatthias.ringwald bd_addr_t address; 4206b35ec0Smatthias.ringwald 4306b35ec0Smatthias.ringwald // module handle 4416833f0aSmatthias.ringwald hci_con_handle_t con_handle; 45c01e9cbdSmatthias.ringwald 46c8e4258aSmatthias.ringwald // state 47c8e4258aSmatthias.ringwald CONNECTION_STATE state; 48c8e4258aSmatthias.ringwald 4906b35ec0Smatthias.ringwald // errands 50c01e9cbdSmatthias.ringwald hci_connection_flags_t flags; 51c8e4258aSmatthias.ringwald 52*ee091cf1Smatthias.ringwald // timer 53*ee091cf1Smatthias.ringwald timer_t timeout; 54*ee091cf1Smatthias.ringwald struct timeval timestamp; 55*ee091cf1Smatthias.ringwald 5616833f0aSmatthias.ringwald } hci_connection_t; 5716833f0aSmatthias.ringwald 5806b35ec0Smatthias.ringwald /** 5906b35ec0Smatthias.ringwald * main data structure 6006b35ec0Smatthias.ringwald */ 6116833f0aSmatthias.ringwald typedef struct { 6206b35ec0Smatthias.ringwald // transport component with configuration 6316833f0aSmatthias.ringwald hci_transport_t * hci_transport; 6411e23e5fSmatthias.ringwald void * config; 6511e23e5fSmatthias.ringwald 6606b35ec0Smatthias.ringwald // hardware power controller 6706b35ec0Smatthias.ringwald bt_control_t * control; 6806b35ec0Smatthias.ringwald 6906b35ec0Smatthias.ringwald // list of existing baseband connections 70fe1ed1b8Smatthias.ringwald linked_list_t connections; 7116833f0aSmatthias.ringwald 7206b35ec0Smatthias.ringwald // single buffer for HCI Command assembly 7306b35ec0Smatthias.ringwald uint8_t * hci_cmd_buffer; 7406b35ec0Smatthias.ringwald 7516833f0aSmatthias.ringwald /* host to controller flow control */ 7616833f0aSmatthias.ringwald uint8_t num_cmd_packets; 7716833f0aSmatthias.ringwald uint8_t num_acl_packets; 7816833f0aSmatthias.ringwald 7916833f0aSmatthias.ringwald /* callback to L2CAP layer */ 801cd208adSmatthias.ringwald void (*event_packet_handler)(uint8_t *packet, uint16_t size); 811cd208adSmatthias.ringwald void (*acl_packet_handler) (uint8_t *packet, uint16_t size); 8216833f0aSmatthias.ringwald 833429f56bSmatthias.ringwald /* hci state machine */ 843429f56bSmatthias.ringwald HCI_STATE state; 853429f56bSmatthias.ringwald uint8_t substate; 863429f56bSmatthias.ringwald uint8_t cmds_ready; 873429f56bSmatthias.ringwald 8816833f0aSmatthias.ringwald } hci_stack_t; 8916833f0aSmatthias.ringwald 90475c8125Smatthias.ringwald // set up HCI 9111e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control); 92475c8125Smatthias.ringwald 931cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)); 9416833f0aSmatthias.ringwald 951cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)); 9616833f0aSmatthias.ringwald 97475c8125Smatthias.ringwald // power control 98475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE mode); 99475c8125Smatthias.ringwald 1003429f56bSmatthias.ringwald /** 101c8e4258aSmatthias.ringwald * run the hci control loop once 1023429f56bSmatthias.ringwald */ 10306b35ec0Smatthias.ringwald void hci_run(); 1041f504dbdSmatthias.ringwald 1051cd208adSmatthias.ringwald // create and send hci command packets based on a template and a list of parameters 10602ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...); 107d8905019Smatthias.ringwald 108d8905019Smatthias.ringwald // send complete CMD packet 10949857181Smatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size); 110554588a5Smatthias.ringwald 11143625864Smatthias.ringwald // send ACL packet 11243625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size); 11306b35ec0Smatthias.ringwald 11406b35ec0Smatthias.ringwald // 1151e6aba47Smatthias.ringwald void hci_emit_state(); 116c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn); 117*ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 118