xref: /btstack/src/hci.c (revision ac2e07f8dd09e7f1b309f34acfaf76b249bb0a2e)
11f504dbdSmatthias.ringwald /*
21f504dbdSmatthias.ringwald  *  hci.c
31f504dbdSmatthias.ringwald  *
41f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
51f504dbdSmatthias.ringwald  *
61f504dbdSmatthias.ringwald  */
71f504dbdSmatthias.ringwald 
8475c8125Smatthias.ringwald #include <unistd.h>
993b8dc03Smatthias.ringwald #include <stdarg.h>
1093b8dc03Smatthias.ringwald #include <string.h>
1156fe0872Smatthias.ringwald #include <stdio.h>
121f504dbdSmatthias.ringwald #include "hci.h"
13d8905019Smatthias.ringwald #include "hci_dump.h"
1493b8dc03Smatthias.ringwald 
151e6aba47Smatthias.ringwald // temp
161e6aba47Smatthias.ringwald #include "l2cap.h"
171e6aba47Smatthias.ringwald 
18ee091cf1Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT 10
19ee091cf1Smatthias.ringwald 
2006b35ec0Smatthias.ringwald // the STACK is here
2116833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
2216833f0aSmatthias.ringwald 
2397addcc5Smatthias.ringwald /**
24ee091cf1Smatthias.ringwald  * get connection for a given handle
25ee091cf1Smatthias.ringwald  *
26ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
27ee091cf1Smatthias.ringwald  */
28ee091cf1Smatthias.ringwald static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
29ee091cf1Smatthias.ringwald     linked_item_t *it;
30ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
31ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
32ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
33ee091cf1Smatthias.ringwald         }
34ee091cf1Smatthias.ringwald     }
35ee091cf1Smatthias.ringwald     return NULL;
36ee091cf1Smatthias.ringwald }
37ee091cf1Smatthias.ringwald 
38ee091cf1Smatthias.ringwald static void hci_connection_timeout_handler(timer_t *timer){
39ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
40ee091cf1Smatthias.ringwald     struct timeval tv;
41ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
42ee091cf1Smatthias.ringwald     if (tv.tv_sec > connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT) {
43ee091cf1Smatthias.ringwald         // connections might be timed out
44ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
45ee091cf1Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT);
46ee091cf1Smatthias.ringwald     } else {
47ee091cf1Smatthias.ringwald         // next timeout check at
48ee091cf1Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT;
49ee091cf1Smatthias.ringwald     }
50ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
51ee091cf1Smatthias.ringwald }
52ee091cf1Smatthias.ringwald 
53ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
54ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
55ee091cf1Smatthias.ringwald }
56ee091cf1Smatthias.ringwald 
57ee091cf1Smatthias.ringwald static void hci_connection_update_timestamp_for_acl(uint8_t *packet) {
58ee091cf1Smatthias.ringwald     // update timestamp
59ee091cf1Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
60ee091cf1Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
61ee091cf1Smatthias.ringwald     if (connection) hci_connection_timestamp(connection);
62ee091cf1Smatthias.ringwald }
63ee091cf1Smatthias.ringwald 
64ee091cf1Smatthias.ringwald /**
65c8e4258aSmatthias.ringwald  * create connection for given address
66c8e4258aSmatthias.ringwald  *
67c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
68c8e4258aSmatthias.ringwald  */
69c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
70c8e4258aSmatthias.ringwald     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
71c8e4258aSmatthias.ringwald     if (!conn) return NULL;
72c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
73c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
74c8e4258aSmatthias.ringwald     conn->flags = 0;
75ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
76ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
77ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
78c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
79c8e4258aSmatthias.ringwald     return conn;
80c8e4258aSmatthias.ringwald }
81c8e4258aSmatthias.ringwald 
82c8e4258aSmatthias.ringwald /**
8306b35ec0Smatthias.ringwald  * get connection for given address
8497addcc5Smatthias.ringwald  *
8597addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
8697addcc5Smatthias.ringwald  */
87fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
8806b35ec0Smatthias.ringwald     linked_item_t *it;
8906b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
9006b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
9106b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
9206b35ec0Smatthias.ringwald         }
9306b35ec0Smatthias.ringwald     }
9406b35ec0Smatthias.ringwald     return NULL;
9506b35ec0Smatthias.ringwald }
9606b35ec0Smatthias.ringwald 
9743bfb1bdSmatthias.ringwald /**
9843bfb1bdSmatthias.ringwald  * count connections
9943bfb1bdSmatthias.ringwald  */
10043bfb1bdSmatthias.ringwald static int nr_hci_connections(){
10143bfb1bdSmatthias.ringwald     int count;
10243bfb1bdSmatthias.ringwald     linked_item_t *it;
10343bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
10443bfb1bdSmatthias.ringwald     return count;
10543bfb1bdSmatthias.ringwald }
106c8e4258aSmatthias.ringwald 
10797addcc5Smatthias.ringwald /**
108ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
10916833f0aSmatthias.ringwald  */
1101cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
11116833f0aSmatthias.ringwald }
11216833f0aSmatthias.ringwald 
113ba681a6cSmatthias.ringwald /**
114ba681a6cSmatthias.ringwald  * Dummy control handler
115ba681a6cSmatthias.ringwald  */
116ba681a6cSmatthias.ringwald static int null_control_function(void *config){
117ba681a6cSmatthias.ringwald     return 0;
118ba681a6cSmatthias.ringwald }
119ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){
120ba681a6cSmatthias.ringwald     return "Hardware unknown";
121ba681a6cSmatthias.ringwald }
122ba681a6cSmatthias.ringwald static bt_control_t null_control = {
123ba681a6cSmatthias.ringwald     null_control_function,
124ba681a6cSmatthias.ringwald     null_control_function,
125ba681a6cSmatthias.ringwald     null_control_function,
126ba681a6cSmatthias.ringwald     null_control_name
127ba681a6cSmatthias.ringwald };
128ba681a6cSmatthias.ringwald 
129c8e4258aSmatthias.ringwald 
130ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
131ee091cf1Smatthias.ringwald     hci_connection_update_timestamp_for_acl(packet);
132ee091cf1Smatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
133ee091cf1Smatthias.ringwald }
134ee091cf1Smatthias.ringwald 
13516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
136ee091cf1Smatthias.ringwald     hci_connection_update_timestamp_for_acl(packet);
13716833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
13894ab26f8Smatthias.ringwald 
13994ab26f8Smatthias.ringwald     // execute main loop
14094ab26f8Smatthias.ringwald     hci_run();
14116833f0aSmatthias.ringwald }
14222909952Smatthias.ringwald 
14316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1441281a47eSmatthias.ringwald     bd_addr_t addr;
145fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
14622909952Smatthias.ringwald 
1473429f56bSmatthias.ringwald     // Get Num_HCI_Command_Packets
1483429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
1493429f56bSmatthias.ringwald         packet[0] == HCI_EVENT_COMMAND_STATUS){
1503429f56bSmatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
15122909952Smatthias.ringwald     }
15222909952Smatthias.ringwald 
153fe1ed1b8Smatthias.ringwald     // Connection management
154fe1ed1b8Smatthias.ringwald     if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) {
155fe1ed1b8Smatthias.ringwald         if (!packet[2]){
156fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
157c8e4258aSmatthias.ringwald             printf("Connection_complete "); print_bd_addr(addr); printf("\n");
158fe1ed1b8Smatthias.ringwald             hci_connection_t * conn = connection_for_address(addr);
159fe1ed1b8Smatthias.ringwald             if (conn) {
160c8e4258aSmatthias.ringwald                 conn->state = OPEN;
161fe1ed1b8Smatthias.ringwald                 conn->con_handle = READ_BT_16(packet, 3);
162fe1ed1b8Smatthias.ringwald                 conn->flags = 0;
163ee091cf1Smatthias.ringwald 
164ee091cf1Smatthias.ringwald                 gettimeofday(&conn->timestamp, NULL);
165ee091cf1Smatthias.ringwald                 run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT);
166ee091cf1Smatthias.ringwald                 run_loop_add_timer(&conn->timeout);
167ee091cf1Smatthias.ringwald 
168fe1ed1b8Smatthias.ringwald                 printf("New connection: handle %u, ", conn->con_handle);
169fe1ed1b8Smatthias.ringwald                 print_bd_addr( conn->address );
170fe1ed1b8Smatthias.ringwald                 printf("\n");
17143bfb1bdSmatthias.ringwald 
17243bfb1bdSmatthias.ringwald                 hci_emit_nr_connections_changed();
173fe1ed1b8Smatthias.ringwald             }
174fe1ed1b8Smatthias.ringwald         }
175fe1ed1b8Smatthias.ringwald     }
176fe1ed1b8Smatthias.ringwald 
177fe1ed1b8Smatthias.ringwald     if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) {
178fe1ed1b8Smatthias.ringwald         if (!packet[2]){
179fe1ed1b8Smatthias.ringwald             handle = READ_BT_16(packet, 3);
180fe1ed1b8Smatthias.ringwald             hci_connection_t * conn = connection_for_handle(handle);
181fe1ed1b8Smatthias.ringwald             if (conn) {
182fe1ed1b8Smatthias.ringwald                 printf("Connection closed: handle %u, ", conn->con_handle);
183fe1ed1b8Smatthias.ringwald                 print_bd_addr( conn->address );
184fe1ed1b8Smatthias.ringwald                 printf("\n");
185ee091cf1Smatthias.ringwald                 run_loop_remove_timer(&conn->timeout);
186fe1ed1b8Smatthias.ringwald                 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
187fe1ed1b8Smatthias.ringwald                 free( conn );
18843bfb1bdSmatthias.ringwald                 hci_emit_nr_connections_changed();
189fe1ed1b8Smatthias.ringwald             }
190fe1ed1b8Smatthias.ringwald         }
191fe1ed1b8Smatthias.ringwald     }
192fe1ed1b8Smatthias.ringwald 
1933429f56bSmatthias.ringwald     // handle BT initialization
1943429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
1957301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
1967301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
1977301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
1987301ad89Smatthias.ringwald         // }
1997301ad89Smatthias.ringwald         // handle normal init sequence
2003429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
2013429f56bSmatthias.ringwald             // odd: waiting for event
2023429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
2033429f56bSmatthias.ringwald                 hci_stack.substate++;
2043429f56bSmatthias.ringwald             }
2053429f56bSmatthias.ringwald         }
20622909952Smatthias.ringwald     }
20722909952Smatthias.ringwald 
2081281a47eSmatthias.ringwald     // link key request
2093429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
2101281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
2111281a47eSmatthias.ringwald         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
2121281a47eSmatthias.ringwald         return;
2131281a47eSmatthias.ringwald     }
2141281a47eSmatthias.ringwald 
2151281a47eSmatthias.ringwald     // pin code request
2163429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
2171281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
2181281a47eSmatthias.ringwald         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
2191281a47eSmatthias.ringwald     }
2201281a47eSmatthias.ringwald 
22116833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
22294ab26f8Smatthias.ringwald 
22394ab26f8Smatthias.ringwald 	// execute main loop
22494ab26f8Smatthias.ringwald 	hci_run();
22516833f0aSmatthias.ringwald }
22616833f0aSmatthias.ringwald 
227fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
2281cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
22916833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
23016833f0aSmatthias.ringwald }
2311cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
23216833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
23316833f0aSmatthias.ringwald }
23416833f0aSmatthias.ringwald 
23511e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
236475c8125Smatthias.ringwald 
237475c8125Smatthias.ringwald     // reference to use transport layer implementation
23816833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
239475c8125Smatthias.ringwald 
24011e23e5fSmatthias.ringwald     // references to used control implementation
2417301ad89Smatthias.ringwald     if (control) {
24211e23e5fSmatthias.ringwald         hci_stack.control = control;
2437301ad89Smatthias.ringwald     } else {
2447301ad89Smatthias.ringwald         hci_stack.control = &null_control;
2457301ad89Smatthias.ringwald     }
24611e23e5fSmatthias.ringwald 
24711e23e5fSmatthias.ringwald     // reference to used config
24811e23e5fSmatthias.ringwald     hci_stack.config = config;
24911e23e5fSmatthias.ringwald 
250fe1ed1b8Smatthias.ringwald     // no connections yet
251fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
252fe1ed1b8Smatthias.ringwald 
25302ea9861Smatthias.ringwald     // empty cmd buffer
25416833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
25516833f0aSmatthias.ringwald 
25616833f0aSmatthias.ringwald     // higher level handler
25716833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
25816833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
25916833f0aSmatthias.ringwald 
26016833f0aSmatthias.ringwald     // register packet handlers with transport
26116833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
26216833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
263475c8125Smatthias.ringwald }
264475c8125Smatthias.ringwald 
265475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
266f12adbd6Smatthias.ringwald     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
2677301ad89Smatthias.ringwald 
268038bc64cSmatthias.ringwald         // power on
269038bc64cSmatthias.ringwald         int err = hci_stack.control->on(hci_stack.config);
270038bc64cSmatthias.ringwald         if (err){
271*ac2e07f8Smatthias.ringwald             fprintf(stderr, "POWER_ON failed\n");
272038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
273038bc64cSmatthias.ringwald             return err;
274038bc64cSmatthias.ringwald         }
275038bc64cSmatthias.ringwald 
276038bc64cSmatthias.ringwald         // open low-level device
277038bc64cSmatthias.ringwald         err = hci_stack.hci_transport->open(hci_stack.config);
278038bc64cSmatthias.ringwald         if (err){
279*ac2e07f8Smatthias.ringwald             fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n");
280038bc64cSmatthias.ringwald             hci_stack.control->off(hci_stack.config);
281038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
282038bc64cSmatthias.ringwald             return err;
283038bc64cSmatthias.ringwald         }
284038bc64cSmatthias.ringwald 
2857301ad89Smatthias.ringwald         // set up state machine
2867301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
2877301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
2887301ad89Smatthias.ringwald         hci_stack.substate = 0;
2897301ad89Smatthias.ringwald 
290f12adbd6Smatthias.ringwald     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
2917301ad89Smatthias.ringwald 
2927301ad89Smatthias.ringwald         // close low-level device
2937301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
2947301ad89Smatthias.ringwald 
2957301ad89Smatthias.ringwald         // power off
29611e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
29743bfb1bdSmatthias.ringwald 
29843bfb1bdSmatthias.ringwald         // we're off now
29943bfb1bdSmatthias.ringwald         hci_stack.state = HCI_STATE_OFF;
30011e23e5fSmatthias.ringwald     }
30168d92d03Smatthias.ringwald 
302038bc64cSmatthias.ringwald     // create internal event
303ee8bf225Smatthias.ringwald 	hci_emit_state();
304ee8bf225Smatthias.ringwald 
30568d92d03Smatthias.ringwald 	// trigger next/first action
30668d92d03Smatthias.ringwald 	hci_run();
30768d92d03Smatthias.ringwald 
308475c8125Smatthias.ringwald     return 0;
309475c8125Smatthias.ringwald }
310475c8125Smatthias.ringwald 
31106b35ec0Smatthias.ringwald void hci_run(){
3123429f56bSmatthias.ringwald     uint8_t micro_packet;
3133429f56bSmatthias.ringwald     switch (hci_stack.state){
3143429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
3153429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
3163429f56bSmatthias.ringwald                 // odd: waiting for command completion
31706b35ec0Smatthias.ringwald                 return;
3183429f56bSmatthias.ringwald             }
3193429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
3203429f56bSmatthias.ringwald                 // cannot send command yet
32106b35ec0Smatthias.ringwald                 return;
3223429f56bSmatthias.ringwald             }
3233429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
3243429f56bSmatthias.ringwald                 case 0:
32522909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
3263429f56bSmatthias.ringwald                     break;
3273429f56bSmatthias.ringwald 				case 1:
328f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
329f432a6ddSmatthias.ringwald 					break;
330f432a6ddSmatthias.ringwald                 case 2:
3313429f56bSmatthias.ringwald                     // ca. 15 sec
3323429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
3333429f56bSmatthias.ringwald                     break;
334f432a6ddSmatthias.ringwald 				case 3:
335bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
336f432a6ddSmatthias.ringwald 					break;
337f432a6ddSmatthias.ringwald                 case 4:
3383429f56bSmatthias.ringwald                     // done.
3393429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
3408adf0ddaSmatthias.ringwald                     micro_packet = HCI_EVENT_BTSTACK_WORKING;
3413429f56bSmatthias.ringwald                     hci_stack.event_packet_handler(&micro_packet, 1);
3423429f56bSmatthias.ringwald                     break;
3433429f56bSmatthias.ringwald                 default:
3443429f56bSmatthias.ringwald                     break;
345475c8125Smatthias.ringwald             }
3463429f56bSmatthias.ringwald             hci_stack.substate++;
3473429f56bSmatthias.ringwald             break;
3483429f56bSmatthias.ringwald         default:
3493429f56bSmatthias.ringwald             break;
3501f504dbdSmatthias.ringwald     }
3513429f56bSmatthias.ringwald }
35216833f0aSmatthias.ringwald 
35331452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
354c8e4258aSmatthias.ringwald     bd_addr_t addr;
355c8e4258aSmatthias.ringwald     hci_connection_t * conn;
356c8e4258aSmatthias.ringwald     // house-keeping
357c8e4258aSmatthias.ringwald 
358c8e4258aSmatthias.ringwald     // create_connection?
359c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
360c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
361c8e4258aSmatthias.ringwald         printf("Create_connection to "); print_bd_addr(addr); printf("\n");
362c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
363c8e4258aSmatthias.ringwald         if (conn) {
364c8e4258aSmatthias.ringwald             // if connection exists
365c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
366c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
367c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
368c8e4258aSmatthias.ringwald             }
369c8e4258aSmatthias.ringwald             //    otherwise, just ignore
370c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
371c8e4258aSmatthias.ringwald 
372c8e4258aSmatthias.ringwald         } else{
373c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
374c8e4258aSmatthias.ringwald             if (conn){
375c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
376c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
377c8e4258aSmatthias.ringwald             }
378c8e4258aSmatthias.ringwald         }
379c8e4258aSmatthias.ringwald     }
380c8e4258aSmatthias.ringwald 
381c8e4258aSmatthias.ringwald     // accept connection
382c8e4258aSmatthias.ringwald 
383c8e4258aSmatthias.ringwald     // reject connection
384c8e4258aSmatthias.ringwald 
385c8e4258aSmatthias.ringwald     // close_connection?
386c8e4258aSmatthias.ringwald       // set state = SENT_DISCONNECT
387c8e4258aSmatthias.ringwald 
38831452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
38931452debSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
39031452debSmatthias.ringwald }
3918adf0ddaSmatthias.ringwald 
3921cd208adSmatthias.ringwald /**
3931cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
3941cd208adSmatthias.ringwald  */
3951cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
3961cd208adSmatthias.ringwald     va_list argptr;
3971cd208adSmatthias.ringwald     va_start(argptr, cmd);
3981cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
3991cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
4001cd208adSmatthias.ringwald     va_end(argptr);
4011cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
40293b8dc03Smatthias.ringwald }
403c8e4258aSmatthias.ringwald 
404ee091cf1Smatthias.ringwald // Create various non-HCI events.
405ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
406ee091cf1Smatthias.ringwald 
407c8e4258aSmatthias.ringwald void hci_emit_state(){
408c8e4258aSmatthias.ringwald     uint8_t len = 3;
409c8e4258aSmatthias.ringwald     uint8_t event[len];
410c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_BTSTACK_STATE;
411c8e4258aSmatthias.ringwald     event[1] = 1;
412c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
413c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
414c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
415c8e4258aSmatthias.ringwald }
416c8e4258aSmatthias.ringwald 
417c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
418c8e4258aSmatthias.ringwald     uint8_t len = 13;
419c8e4258aSmatthias.ringwald     uint8_t event[len];
420c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
421c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
422c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
423c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
424c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
425c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
426c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
427c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
428c8e4258aSmatthias.ringwald }
429c8e4258aSmatthias.ringwald 
430ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
431ee091cf1Smatthias.ringwald     uint8_t len = 4;
432ee091cf1Smatthias.ringwald     uint8_t event[len];
433ee091cf1Smatthias.ringwald     event[0] = HCI_EVENT_L2CAP_TIMEOUT_CHECK;
434ee091cf1Smatthias.ringwald     event[1] = 2;
435ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
436ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
437ee091cf1Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
438ee091cf1Smatthias.ringwald }
43943bfb1bdSmatthias.ringwald 
44043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
44143bfb1bdSmatthias.ringwald     uint8_t len = 3;
44243bfb1bdSmatthias.ringwald     uint8_t event[len];
44343bfb1bdSmatthias.ringwald     event[0] = HCI_EVENT_NR_CONNECTIONS_CHANGED;
44443bfb1bdSmatthias.ringwald     event[1] = 1;
44543bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
44643bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
44743bfb1bdSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
44843bfb1bdSmatthias.ringwald }
449038bc64cSmatthias.ringwald 
450038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
451038bc64cSmatthias.ringwald     uint8_t len = 1;
452038bc64cSmatthias.ringwald     uint8_t event[len];
453038bc64cSmatthias.ringwald     event[0] = HCI_EVENT_POWERON_FAILED;
454038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
455038bc64cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
456038bc64cSmatthias.ringwald }
457