xref: /btstack/src/hci.c (revision 7f2435e617fe44b1b57a0b14ef11f991fc1b99f0)
11f504dbdSmatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
331f504dbdSmatthias.ringwald  *  hci.c
341f504dbdSmatthias.ringwald  *
351f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
361f504dbdSmatthias.ringwald  *
371f504dbdSmatthias.ringwald  */
381f504dbdSmatthias.ringwald 
39*7f2435e6Smatthias.ringwald #include "hci.h"
40*7f2435e6Smatthias.ringwald 
41475c8125Smatthias.ringwald #include <unistd.h>
4293b8dc03Smatthias.ringwald #include <stdarg.h>
4393b8dc03Smatthias.ringwald #include <string.h>
4456fe0872Smatthias.ringwald #include <stdio.h>
45*7f2435e6Smatthias.ringwald 
46*7f2435e6Smatthias.ringwald #include "debug.h"
47d8905019Smatthias.ringwald #include "hci_dump.h"
4893b8dc03Smatthias.ringwald 
492ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h"
502ed6235cSmatthias.ringwald #include "../include/btstack/version.h"
511b0e3922Smatthias.ringwald 
521e6aba47Smatthias.ringwald // temp
531e6aba47Smatthias.ringwald #include "l2cap.h"
541e6aba47Smatthias.ringwald 
55169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
56ee091cf1Smatthias.ringwald 
5706b35ec0Smatthias.ringwald // the STACK is here
5816833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
5916833f0aSmatthias.ringwald 
6097addcc5Smatthias.ringwald /**
61ee091cf1Smatthias.ringwald  * get connection for a given handle
62ee091cf1Smatthias.ringwald  *
63ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
64ee091cf1Smatthias.ringwald  */
65645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
66ee091cf1Smatthias.ringwald     linked_item_t *it;
67ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
68ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
69ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
70ee091cf1Smatthias.ringwald         }
71ee091cf1Smatthias.ringwald     }
72ee091cf1Smatthias.ringwald     return NULL;
73ee091cf1Smatthias.ringwald }
74ee091cf1Smatthias.ringwald 
75981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
76ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
77ee091cf1Smatthias.ringwald     struct timeval tv;
78ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
79c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
80ee091cf1Smatthias.ringwald         // connections might be timed out
81ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
82c21e6239Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
83ee091cf1Smatthias.ringwald     } else {
84ee091cf1Smatthias.ringwald         // next timeout check at
85c21e6239Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
86ee091cf1Smatthias.ringwald     }
87ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
88ee091cf1Smatthias.ringwald }
89ee091cf1Smatthias.ringwald 
90ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
91ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
92ee091cf1Smatthias.ringwald }
93ee091cf1Smatthias.ringwald 
94ee091cf1Smatthias.ringwald /**
95c8e4258aSmatthias.ringwald  * create connection for given address
96c8e4258aSmatthias.ringwald  *
97c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
98c8e4258aSmatthias.ringwald  */
99c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
100c8e4258aSmatthias.ringwald     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
101c8e4258aSmatthias.ringwald     if (!conn) return NULL;
102c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
103c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
104c8e4258aSmatthias.ringwald     conn->flags = 0;
105ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
106ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
107ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
108d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1097856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
110c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
111c8e4258aSmatthias.ringwald     return conn;
112c8e4258aSmatthias.ringwald }
113c8e4258aSmatthias.ringwald 
114c8e4258aSmatthias.ringwald /**
11506b35ec0Smatthias.ringwald  * get connection for given address
11697addcc5Smatthias.ringwald  *
11797addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
11897addcc5Smatthias.ringwald  */
119fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
12006b35ec0Smatthias.ringwald     linked_item_t *it;
12106b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
12206b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
12306b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
12406b35ec0Smatthias.ringwald         }
12506b35ec0Smatthias.ringwald     }
12606b35ec0Smatthias.ringwald     return NULL;
12706b35ec0Smatthias.ringwald }
12806b35ec0Smatthias.ringwald 
12943bfb1bdSmatthias.ringwald /**
13043bfb1bdSmatthias.ringwald  * count connections
13143bfb1bdSmatthias.ringwald  */
13243bfb1bdSmatthias.ringwald static int nr_hci_connections(){
13356c253c9Smatthias.ringwald     int count = 0;
13443bfb1bdSmatthias.ringwald     linked_item_t *it;
13543bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
13643bfb1bdSmatthias.ringwald     return count;
13743bfb1bdSmatthias.ringwald }
138c8e4258aSmatthias.ringwald 
13997addcc5Smatthias.ringwald /**
140ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
14116833f0aSmatthias.ringwald  */
1421cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
14316833f0aSmatthias.ringwald }
14416833f0aSmatthias.ringwald 
145ba681a6cSmatthias.ringwald /**
146ba681a6cSmatthias.ringwald  * Dummy control handler
147ba681a6cSmatthias.ringwald  */
148ba681a6cSmatthias.ringwald static int null_control_function(void *config){
149ba681a6cSmatthias.ringwald     return 0;
150ba681a6cSmatthias.ringwald }
151ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){
152ba681a6cSmatthias.ringwald     return "Hardware unknown";
153ba681a6cSmatthias.ringwald }
154ba681a6cSmatthias.ringwald static bt_control_t null_control = {
155ba681a6cSmatthias.ringwald     null_control_function,
156ba681a6cSmatthias.ringwald     null_control_function,
157ba681a6cSmatthias.ringwald     null_control_function,
158ba681a6cSmatthias.ringwald     null_control_name
159ba681a6cSmatthias.ringwald };
160ba681a6cSmatthias.ringwald 
161c8e4258aSmatthias.ringwald 
162ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
1637856c818Smatthias.ringwald 
1647856c818Smatthias.ringwald     // update idle timestamp
1657856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
1667856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
1677856c818Smatthias.ringwald     if (connection) hci_connection_timestamp(connection);
1687856c818Smatthias.ringwald 
1697856c818Smatthias.ringwald     // send packet
170ee091cf1Smatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
171ee091cf1Smatthias.ringwald }
172ee091cf1Smatthias.ringwald 
17316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
1747856c818Smatthias.ringwald 
1757856c818Smatthias.ringwald     // get info
1767856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
1777856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
1787856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
1797856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
1807856c818Smatthias.ringwald 
1817856c818Smatthias.ringwald     // ignore non-registered handle
1827856c818Smatthias.ringwald     if (!conn){
183*7f2435e6Smatthias.ringwald         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
1847856c818Smatthias.ringwald         return;
1857856c818Smatthias.ringwald     }
1867856c818Smatthias.ringwald 
1877856c818Smatthias.ringwald     // update idle timestamp
1887856c818Smatthias.ringwald     hci_connection_timestamp(conn);
1897856c818Smatthias.ringwald 
1907856c818Smatthias.ringwald     // handle different packet types
1917856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
1927856c818Smatthias.ringwald 
1937856c818Smatthias.ringwald         case 0x01: // continuation fragment
1947856c818Smatthias.ringwald 
1957856c818Smatthias.ringwald             // sanity check
1967856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
197*7f2435e6Smatthias.ringwald                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
1987856c818Smatthias.ringwald                 return;
1997856c818Smatthias.ringwald             }
2007856c818Smatthias.ringwald 
2017856c818Smatthias.ringwald             // append fragment payload (header already stored)
2027856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2037856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2047856c818Smatthias.ringwald 
205*7f2435e6Smatthias.ringwald             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
2067856c818Smatthias.ringwald             //        acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
2077856c818Smatthias.ringwald 
2087856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2097856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2107856c818Smatthias.ringwald 
2117856c818Smatthias.ringwald                 hci_stack.acl_packet_handler(conn->acl_recombination_buffer, conn->acl_recombination_pos);
2127856c818Smatthias.ringwald                 // reset recombination buffer
2137856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2147856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2157856c818Smatthias.ringwald             }
2167856c818Smatthias.ringwald             break;
2177856c818Smatthias.ringwald 
2187856c818Smatthias.ringwald         case 0x02: { // first fragment
2197856c818Smatthias.ringwald 
2207856c818Smatthias.ringwald             // sanity check
2217856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
222*7f2435e6Smatthias.ringwald                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
2237856c818Smatthias.ringwald                 return;
2247856c818Smatthias.ringwald             }
2257856c818Smatthias.ringwald 
2267856c818Smatthias.ringwald             // peek into L2CAP packet!
2277856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
2287856c818Smatthias.ringwald 
2297856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
2307856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
2317856c818Smatthias.ringwald 
2327856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
2337856c818Smatthias.ringwald                 hci_stack.acl_packet_handler(packet, acl_length + 4);
2347856c818Smatthias.ringwald 
2357856c818Smatthias.ringwald             } else {
2367856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
2377856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
2387856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
2397856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
2407856c818Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
241*7f2435e6Smatthias.ringwald                 // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
2427856c818Smatthias.ringwald             }
2437856c818Smatthias.ringwald             break;
2447856c818Smatthias.ringwald 
2457856c818Smatthias.ringwald         }
2467856c818Smatthias.ringwald         default:
247*7f2435e6Smatthias.ringwald             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
2487856c818Smatthias.ringwald             return;
2497856c818Smatthias.ringwald     }
25094ab26f8Smatthias.ringwald 
25194ab26f8Smatthias.ringwald     // execute main loop
25294ab26f8Smatthias.ringwald     hci_run();
25316833f0aSmatthias.ringwald }
25422909952Smatthias.ringwald 
25516833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
2561281a47eSmatthias.ringwald     bd_addr_t addr;
257fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
2581f7b95a1Smatthias.ringwald     hci_connection_t * conn;
25922909952Smatthias.ringwald 
2606772a24cSmatthias.ringwald     switch (packet[0]) {
26122909952Smatthias.ringwald 
2626772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
2636772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
2646772a24cSmatthias.ringwald             // Get Num_HCI_Command_Packets
2656772a24cSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
2666772a24cSmatthias.ringwald             break;
2676772a24cSmatthias.ringwald 
2681f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
26937eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
27037eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
27137eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
27237eaa4cfSmatthias.ringwald             printf("Connection_incoming: "); print_bd_addr(addr); printf(", type %u\n", link_type);
27337eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
2741f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
2751f7b95a1Smatthias.ringwald                 if (!conn) {
2761f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
2771f7b95a1Smatthias.ringwald                 }
2781f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
2791f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
28037eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
28137eaa4cfSmatthias.ringwald             } else {
28237eaa4cfSmatthias.ringwald                 // TODO: decline request
28337eaa4cfSmatthias.ringwald             }
2841f7b95a1Smatthias.ringwald             break;
2851f7b95a1Smatthias.ringwald 
2866772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
287fe1ed1b8Smatthias.ringwald             // Connection management
288fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
289b448a0e7Smatthias.ringwald             printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n");
2901f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
291fe1ed1b8Smatthias.ringwald             if (conn) {
292b448a0e7Smatthias.ringwald                 if (!packet[2]){
293c8e4258aSmatthias.ringwald                     conn->state = OPEN;
294fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
295fe1ed1b8Smatthias.ringwald                     conn->flags = 0;
296ee091cf1Smatthias.ringwald 
297ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
298c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
299ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
300ee091cf1Smatthias.ringwald 
301fe1ed1b8Smatthias.ringwald                     printf("New connection: handle %u, ", conn->con_handle);
302fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
303fe1ed1b8Smatthias.ringwald                     printf("\n");
30443bfb1bdSmatthias.ringwald 
30543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
306b448a0e7Smatthias.ringwald                 } else {
307b448a0e7Smatthias.ringwald                     // connection failed, remove entry
308b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
309b448a0e7Smatthias.ringwald                     free( conn );
310fe1ed1b8Smatthias.ringwald                 }
311fe1ed1b8Smatthias.ringwald             }
3126772a24cSmatthias.ringwald             break;
313fe1ed1b8Smatthias.ringwald 
3146772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
315fe1ed1b8Smatthias.ringwald             if (!packet[2]){
316fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
317fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
318fe1ed1b8Smatthias.ringwald                 if (conn) {
319fe1ed1b8Smatthias.ringwald                     printf("Connection closed: handle %u, ", conn->con_handle);
320fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
321fe1ed1b8Smatthias.ringwald                     printf("\n");
322ee091cf1Smatthias.ringwald                     run_loop_remove_timer(&conn->timeout);
323fe1ed1b8Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
324fe1ed1b8Smatthias.ringwald                     free( conn );
32543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
326fe1ed1b8Smatthias.ringwald                 }
327fe1ed1b8Smatthias.ringwald             }
3286772a24cSmatthias.ringwald             break;
3296772a24cSmatthias.ringwald 
3306772a24cSmatthias.ringwald         default:
3316772a24cSmatthias.ringwald             break;
332fe1ed1b8Smatthias.ringwald     }
333fe1ed1b8Smatthias.ringwald 
3343429f56bSmatthias.ringwald     // handle BT initialization
3353429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
3367301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
3377301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
3387301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
3397301ad89Smatthias.ringwald         // }
3407301ad89Smatthias.ringwald         // handle normal init sequence
3413429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
3423429f56bSmatthias.ringwald             // odd: waiting for event
3433429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
3443429f56bSmatthias.ringwald                 hci_stack.substate++;
3453429f56bSmatthias.ringwald             }
3463429f56bSmatthias.ringwald         }
34722909952Smatthias.ringwald     }
34822909952Smatthias.ringwald 
34916833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
35094ab26f8Smatthias.ringwald 
35194ab26f8Smatthias.ringwald 	// execute main loop
35294ab26f8Smatthias.ringwald 	hci_run();
35316833f0aSmatthias.ringwald }
35416833f0aSmatthias.ringwald 
355fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
3561cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
35716833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
35816833f0aSmatthias.ringwald }
3591cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
36016833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
36116833f0aSmatthias.ringwald }
36216833f0aSmatthias.ringwald 
36311e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
364475c8125Smatthias.ringwald 
365475c8125Smatthias.ringwald     // reference to use transport layer implementation
36616833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
367475c8125Smatthias.ringwald 
36811e23e5fSmatthias.ringwald     // references to used control implementation
3697301ad89Smatthias.ringwald     if (control) {
37011e23e5fSmatthias.ringwald         hci_stack.control = control;
3717301ad89Smatthias.ringwald     } else {
3727301ad89Smatthias.ringwald         hci_stack.control = &null_control;
3737301ad89Smatthias.ringwald     }
37411e23e5fSmatthias.ringwald 
37511e23e5fSmatthias.ringwald     // reference to used config
37611e23e5fSmatthias.ringwald     hci_stack.config = config;
37711e23e5fSmatthias.ringwald 
378fe1ed1b8Smatthias.ringwald     // no connections yet
379fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
380fe1ed1b8Smatthias.ringwald 
38102ea9861Smatthias.ringwald     // empty cmd buffer
38216833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
38316833f0aSmatthias.ringwald 
38416833f0aSmatthias.ringwald     // higher level handler
38516833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
38616833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
38716833f0aSmatthias.ringwald 
38816833f0aSmatthias.ringwald     // register packet handlers with transport
38916833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
39016833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
391475c8125Smatthias.ringwald }
392475c8125Smatthias.ringwald 
393475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
394f12adbd6Smatthias.ringwald     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
3957301ad89Smatthias.ringwald 
396038bc64cSmatthias.ringwald         // power on
397038bc64cSmatthias.ringwald         int err = hci_stack.control->on(hci_stack.config);
398038bc64cSmatthias.ringwald         if (err){
399*7f2435e6Smatthias.ringwald             log_err( "POWER_ON failed\n");
400038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
401038bc64cSmatthias.ringwald             return err;
402038bc64cSmatthias.ringwald         }
403038bc64cSmatthias.ringwald 
404038bc64cSmatthias.ringwald         // open low-level device
405038bc64cSmatthias.ringwald         err = hci_stack.hci_transport->open(hci_stack.config);
406038bc64cSmatthias.ringwald         if (err){
407*7f2435e6Smatthias.ringwald             log_err( "HCI_INIT failed, turning Bluetooth off again\n");
408038bc64cSmatthias.ringwald             hci_stack.control->off(hci_stack.config);
409038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
410038bc64cSmatthias.ringwald             return err;
411038bc64cSmatthias.ringwald         }
412038bc64cSmatthias.ringwald 
4137301ad89Smatthias.ringwald         // set up state machine
4147301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
4157301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
4167301ad89Smatthias.ringwald         hci_stack.substate = 0;
4177301ad89Smatthias.ringwald 
418f12adbd6Smatthias.ringwald     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
4197301ad89Smatthias.ringwald 
4207301ad89Smatthias.ringwald         // close low-level device
4217301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
4227301ad89Smatthias.ringwald 
4237301ad89Smatthias.ringwald         // power off
42411e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
42543bfb1bdSmatthias.ringwald 
42643bfb1bdSmatthias.ringwald         // we're off now
42743bfb1bdSmatthias.ringwald         hci_stack.state = HCI_STATE_OFF;
42811e23e5fSmatthias.ringwald     }
42968d92d03Smatthias.ringwald 
430038bc64cSmatthias.ringwald     // create internal event
431ee8bf225Smatthias.ringwald 	hci_emit_state();
432ee8bf225Smatthias.ringwald 
43368d92d03Smatthias.ringwald 	// trigger next/first action
43468d92d03Smatthias.ringwald 	hci_run();
43568d92d03Smatthias.ringwald 
436475c8125Smatthias.ringwald     return 0;
437475c8125Smatthias.ringwald }
438475c8125Smatthias.ringwald 
43906b35ec0Smatthias.ringwald void hci_run(){
4403429f56bSmatthias.ringwald     switch (hci_stack.state){
4413429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
4423429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
4433429f56bSmatthias.ringwald                 // odd: waiting for command completion
44406b35ec0Smatthias.ringwald                 return;
4453429f56bSmatthias.ringwald             }
4463429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
4473429f56bSmatthias.ringwald                 // cannot send command yet
44806b35ec0Smatthias.ringwald                 return;
4493429f56bSmatthias.ringwald             }
4503429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
4513429f56bSmatthias.ringwald                 case 0:
45222909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
4533429f56bSmatthias.ringwald                     break;
4543429f56bSmatthias.ringwald 				case 1:
455f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
456f432a6ddSmatthias.ringwald 					break;
457f432a6ddSmatthias.ringwald                 case 2:
4583429f56bSmatthias.ringwald                     // ca. 15 sec
4593429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
4603429f56bSmatthias.ringwald                     break;
461f432a6ddSmatthias.ringwald 				case 3:
462bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
463f432a6ddSmatthias.ringwald 					break;
464f432a6ddSmatthias.ringwald                 case 4:
4653429f56bSmatthias.ringwald                     // done.
4663429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
467b360b6adSmatthias.ringwald                     hci_emit_state();
4683429f56bSmatthias.ringwald                     break;
4693429f56bSmatthias.ringwald                 default:
4703429f56bSmatthias.ringwald                     break;
471475c8125Smatthias.ringwald             }
4723429f56bSmatthias.ringwald             hci_stack.substate++;
4733429f56bSmatthias.ringwald             break;
4743429f56bSmatthias.ringwald         default:
4753429f56bSmatthias.ringwald             break;
4761f504dbdSmatthias.ringwald     }
4773429f56bSmatthias.ringwald }
47816833f0aSmatthias.ringwald 
47931452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
480c8e4258aSmatthias.ringwald     bd_addr_t addr;
481c8e4258aSmatthias.ringwald     hci_connection_t * conn;
482c8e4258aSmatthias.ringwald     // house-keeping
483c8e4258aSmatthias.ringwald 
484c8e4258aSmatthias.ringwald     // create_connection?
485c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
486c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
487c8e4258aSmatthias.ringwald         printf("Create_connection to "); print_bd_addr(addr); printf("\n");
488c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
489c8e4258aSmatthias.ringwald         if (conn) {
490c8e4258aSmatthias.ringwald             // if connection exists
491c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
492c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
493c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
494c8e4258aSmatthias.ringwald             }
495c8e4258aSmatthias.ringwald             //    otherwise, just ignore
496c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
497c8e4258aSmatthias.ringwald 
498c8e4258aSmatthias.ringwald         } else{
499c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
500c8e4258aSmatthias.ringwald             if (conn){
501c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
502c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
503c8e4258aSmatthias.ringwald             }
504c8e4258aSmatthias.ringwald         }
505c8e4258aSmatthias.ringwald     }
506c8e4258aSmatthias.ringwald 
507c8e4258aSmatthias.ringwald     // accept connection
508c8e4258aSmatthias.ringwald 
509c8e4258aSmatthias.ringwald     // reject connection
510c8e4258aSmatthias.ringwald 
511c8e4258aSmatthias.ringwald     // close_connection?
512c8e4258aSmatthias.ringwald       // set state = SENT_DISCONNECT
513c8e4258aSmatthias.ringwald 
51431452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
51531452debSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
51631452debSmatthias.ringwald }
5178adf0ddaSmatthias.ringwald 
5181cd208adSmatthias.ringwald /**
5191cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
5201cd208adSmatthias.ringwald  */
5211cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
5221cd208adSmatthias.ringwald     va_list argptr;
5231cd208adSmatthias.ringwald     va_start(argptr, cmd);
5241cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
5251cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
5261cd208adSmatthias.ringwald     va_end(argptr);
5271cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
52893b8dc03Smatthias.ringwald }
529c8e4258aSmatthias.ringwald 
530ee091cf1Smatthias.ringwald // Create various non-HCI events.
531ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
532ee091cf1Smatthias.ringwald 
533c8e4258aSmatthias.ringwald void hci_emit_state(){
534c8e4258aSmatthias.ringwald     uint8_t len = 3;
535c8e4258aSmatthias.ringwald     uint8_t event[len];
53680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
5371b0e3922Smatthias.ringwald     event[1] = len - 3;
538c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
539c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
540c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
541c8e4258aSmatthias.ringwald }
542c8e4258aSmatthias.ringwald 
543c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
544c8e4258aSmatthias.ringwald     uint8_t len = 13;
545c8e4258aSmatthias.ringwald     uint8_t event[len];
546c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
5471b0e3922Smatthias.ringwald     event[1] = len - 3;
548c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
549c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
550c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
551c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
552c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
553c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
554c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
555c8e4258aSmatthias.ringwald }
556c8e4258aSmatthias.ringwald 
557ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
558ee091cf1Smatthias.ringwald     uint8_t len = 4;
559ee091cf1Smatthias.ringwald     uint8_t event[len];
56080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
5611b0e3922Smatthias.ringwald     event[1] = len - 2;
562ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
563ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
564ee091cf1Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
565ee091cf1Smatthias.ringwald }
56643bfb1bdSmatthias.ringwald 
56743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
56843bfb1bdSmatthias.ringwald     uint8_t len = 3;
56943bfb1bdSmatthias.ringwald     uint8_t event[len];
57080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
5711b0e3922Smatthias.ringwald     event[1] = len - 2;
57243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
57343bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
57443bfb1bdSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
57543bfb1bdSmatthias.ringwald }
576038bc64cSmatthias.ringwald 
577038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
5781b0e3922Smatthias.ringwald     uint8_t len = 2;
579038bc64cSmatthias.ringwald     uint8_t event[len];
58080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
5811b0e3922Smatthias.ringwald     event[1] = len - 2;
582038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
583038bc64cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
584038bc64cSmatthias.ringwald }
5851b0e3922Smatthias.ringwald 
5861b0e3922Smatthias.ringwald 
5871b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
5881b0e3922Smatthias.ringwald     uint8_t len = 6;
5891b0e3922Smatthias.ringwald     uint8_t event[len];
5901b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
5911b0e3922Smatthias.ringwald     event[1] = len - 2;
5921b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
5931b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
5941b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
5951b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5961b0e3922Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
5971b0e3922Smatthias.ringwald }
5981b0e3922Smatthias.ringwald 
5992ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
6002ed6235cSmatthias.ringwald     uint8_t len = 3;
6012ed6235cSmatthias.ringwald     uint8_t event[len];
6022ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
603ddce1d8dSmatthias.ringwald     event[1] = len - 2;
6042ed6235cSmatthias.ringwald     event[2] = enabled;
6052ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
6062ed6235cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
6072ed6235cSmatthias.ringwald }
608