xref: /btstack/src/hci.c (revision ddce1d8d00f1a8831411de5700d1a214a83cf85d)
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 
39475c8125Smatthias.ringwald #include <unistd.h>
4093b8dc03Smatthias.ringwald #include <stdarg.h>
4193b8dc03Smatthias.ringwald #include <string.h>
4256fe0872Smatthias.ringwald #include <stdio.h>
431f504dbdSmatthias.ringwald #include "hci.h"
44d8905019Smatthias.ringwald #include "hci_dump.h"
4593b8dc03Smatthias.ringwald 
462ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h"
472ed6235cSmatthias.ringwald #include "../include/btstack/version.h"
481b0e3922Smatthias.ringwald 
491e6aba47Smatthias.ringwald // temp
501e6aba47Smatthias.ringwald #include "l2cap.h"
511e6aba47Smatthias.ringwald 
52169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
53ee091cf1Smatthias.ringwald 
5406b35ec0Smatthias.ringwald // the STACK is here
5516833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
5616833f0aSmatthias.ringwald 
5797addcc5Smatthias.ringwald /**
58ee091cf1Smatthias.ringwald  * get connection for a given handle
59ee091cf1Smatthias.ringwald  *
60ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
61ee091cf1Smatthias.ringwald  */
62645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
63ee091cf1Smatthias.ringwald     linked_item_t *it;
64ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
65ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
66ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
67ee091cf1Smatthias.ringwald         }
68ee091cf1Smatthias.ringwald     }
69ee091cf1Smatthias.ringwald     return NULL;
70ee091cf1Smatthias.ringwald }
71ee091cf1Smatthias.ringwald 
72981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
73ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
74ee091cf1Smatthias.ringwald     struct timeval tv;
75ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
76c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
77ee091cf1Smatthias.ringwald         // connections might be timed out
78ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
79c21e6239Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
80ee091cf1Smatthias.ringwald     } else {
81ee091cf1Smatthias.ringwald         // next timeout check at
82c21e6239Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
83ee091cf1Smatthias.ringwald     }
84ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
85ee091cf1Smatthias.ringwald }
86ee091cf1Smatthias.ringwald 
87ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
88ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
89ee091cf1Smatthias.ringwald }
90ee091cf1Smatthias.ringwald 
91ee091cf1Smatthias.ringwald /**
92c8e4258aSmatthias.ringwald  * create connection for given address
93c8e4258aSmatthias.ringwald  *
94c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
95c8e4258aSmatthias.ringwald  */
96c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
97c8e4258aSmatthias.ringwald     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
98c8e4258aSmatthias.ringwald     if (!conn) return NULL;
99c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
100c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
101c8e4258aSmatthias.ringwald     conn->flags = 0;
102ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
103ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
104ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
105d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1067856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
107c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
108c8e4258aSmatthias.ringwald     return conn;
109c8e4258aSmatthias.ringwald }
110c8e4258aSmatthias.ringwald 
111c8e4258aSmatthias.ringwald /**
11206b35ec0Smatthias.ringwald  * get connection for given address
11397addcc5Smatthias.ringwald  *
11497addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
11597addcc5Smatthias.ringwald  */
116fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
11706b35ec0Smatthias.ringwald     linked_item_t *it;
11806b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
11906b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
12006b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
12106b35ec0Smatthias.ringwald         }
12206b35ec0Smatthias.ringwald     }
12306b35ec0Smatthias.ringwald     return NULL;
12406b35ec0Smatthias.ringwald }
12506b35ec0Smatthias.ringwald 
12643bfb1bdSmatthias.ringwald /**
12743bfb1bdSmatthias.ringwald  * count connections
12843bfb1bdSmatthias.ringwald  */
12943bfb1bdSmatthias.ringwald static int nr_hci_connections(){
13056c253c9Smatthias.ringwald     int count = 0;
13143bfb1bdSmatthias.ringwald     linked_item_t *it;
13243bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
13343bfb1bdSmatthias.ringwald     return count;
13443bfb1bdSmatthias.ringwald }
135c8e4258aSmatthias.ringwald 
13697addcc5Smatthias.ringwald /**
137ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
13816833f0aSmatthias.ringwald  */
1391cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
14016833f0aSmatthias.ringwald }
14116833f0aSmatthias.ringwald 
142ba681a6cSmatthias.ringwald /**
143ba681a6cSmatthias.ringwald  * Dummy control handler
144ba681a6cSmatthias.ringwald  */
145ba681a6cSmatthias.ringwald static int null_control_function(void *config){
146ba681a6cSmatthias.ringwald     return 0;
147ba681a6cSmatthias.ringwald }
148ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){
149ba681a6cSmatthias.ringwald     return "Hardware unknown";
150ba681a6cSmatthias.ringwald }
151ba681a6cSmatthias.ringwald static bt_control_t null_control = {
152ba681a6cSmatthias.ringwald     null_control_function,
153ba681a6cSmatthias.ringwald     null_control_function,
154ba681a6cSmatthias.ringwald     null_control_function,
155ba681a6cSmatthias.ringwald     null_control_name
156ba681a6cSmatthias.ringwald };
157ba681a6cSmatthias.ringwald 
158c8e4258aSmatthias.ringwald 
159ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
1607856c818Smatthias.ringwald 
1617856c818Smatthias.ringwald     // update idle timestamp
1627856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
1637856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
1647856c818Smatthias.ringwald     if (connection) hci_connection_timestamp(connection);
1657856c818Smatthias.ringwald 
1667856c818Smatthias.ringwald     // send packet
167ee091cf1Smatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
168ee091cf1Smatthias.ringwald }
169ee091cf1Smatthias.ringwald 
17016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
1717856c818Smatthias.ringwald 
1727856c818Smatthias.ringwald     // get info
1737856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
1747856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
1757856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
1767856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
1777856c818Smatthias.ringwald 
1787856c818Smatthias.ringwald     // ignore non-registered handle
1797856c818Smatthias.ringwald     if (!conn){
1807856c818Smatthias.ringwald         fprintf(stderr, "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
1817856c818Smatthias.ringwald         return;
1827856c818Smatthias.ringwald     }
1837856c818Smatthias.ringwald 
1847856c818Smatthias.ringwald     // update idle timestamp
1857856c818Smatthias.ringwald     hci_connection_timestamp(conn);
1867856c818Smatthias.ringwald 
1877856c818Smatthias.ringwald     // handle different packet types
1887856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
1897856c818Smatthias.ringwald 
1907856c818Smatthias.ringwald         case 0x01: // continuation fragment
1917856c818Smatthias.ringwald 
1927856c818Smatthias.ringwald             // sanity check
1937856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
1947856c818Smatthias.ringwald                 fprintf(stderr, "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
1957856c818Smatthias.ringwald                 return;
1967856c818Smatthias.ringwald             }
1977856c818Smatthias.ringwald 
1987856c818Smatthias.ringwald             // append fragment payload (header already stored)
1997856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2007856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2017856c818Smatthias.ringwald 
2027856c818Smatthias.ringwald             // fprintf(stderr, "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
2037856c818Smatthias.ringwald             //        acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
2047856c818Smatthias.ringwald 
2057856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2067856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2077856c818Smatthias.ringwald 
2087856c818Smatthias.ringwald                 hci_stack.acl_packet_handler(conn->acl_recombination_buffer, conn->acl_recombination_pos);
2097856c818Smatthias.ringwald                 // reset recombination buffer
2107856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2117856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2127856c818Smatthias.ringwald             }
2137856c818Smatthias.ringwald             break;
2147856c818Smatthias.ringwald 
2157856c818Smatthias.ringwald         case 0x02: { // first fragment
2167856c818Smatthias.ringwald 
2177856c818Smatthias.ringwald             // sanity check
2187856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
2197856c818Smatthias.ringwald                 fprintf(stderr, "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
2207856c818Smatthias.ringwald                 return;
2217856c818Smatthias.ringwald             }
2227856c818Smatthias.ringwald 
2237856c818Smatthias.ringwald             // peek into L2CAP packet!
2247856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
2257856c818Smatthias.ringwald 
2267856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
2277856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
2287856c818Smatthias.ringwald 
2297856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
2307856c818Smatthias.ringwald                 hci_stack.acl_packet_handler(packet, acl_length + 4);
2317856c818Smatthias.ringwald 
2327856c818Smatthias.ringwald             } else {
2337856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
2347856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
2357856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
2367856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
2377856c818Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
2387856c818Smatthias.ringwald                 // fprintf(stderr, "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
2397856c818Smatthias.ringwald             }
2407856c818Smatthias.ringwald             break;
2417856c818Smatthias.ringwald 
2427856c818Smatthias.ringwald         }
2437856c818Smatthias.ringwald         default:
2447856c818Smatthias.ringwald             fprintf(stderr, "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
2457856c818Smatthias.ringwald             return;
2467856c818Smatthias.ringwald     }
24794ab26f8Smatthias.ringwald 
24894ab26f8Smatthias.ringwald     // execute main loop
24994ab26f8Smatthias.ringwald     hci_run();
25016833f0aSmatthias.ringwald }
25122909952Smatthias.ringwald 
25216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
2531281a47eSmatthias.ringwald     bd_addr_t addr;
254fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
2551f7b95a1Smatthias.ringwald     hci_connection_t * conn;
25622909952Smatthias.ringwald 
2576772a24cSmatthias.ringwald     switch (packet[0]) {
25822909952Smatthias.ringwald 
2596772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
2606772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
2616772a24cSmatthias.ringwald             // Get Num_HCI_Command_Packets
2626772a24cSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
2636772a24cSmatthias.ringwald             break;
2646772a24cSmatthias.ringwald 
2651f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
26637eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
26737eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
26837eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
26937eaa4cfSmatthias.ringwald             printf("Connection_incoming: "); print_bd_addr(addr); printf(", type %u\n", link_type);
27037eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
2711f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
2721f7b95a1Smatthias.ringwald                 if (!conn) {
2731f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
2741f7b95a1Smatthias.ringwald                 }
2751f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
2761f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
27737eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
27837eaa4cfSmatthias.ringwald             } else {
27937eaa4cfSmatthias.ringwald                 // TODO: decline request
28037eaa4cfSmatthias.ringwald             }
2811f7b95a1Smatthias.ringwald             break;
2821f7b95a1Smatthias.ringwald 
2836772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
284fe1ed1b8Smatthias.ringwald             // Connection management
285fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
286b448a0e7Smatthias.ringwald             printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n");
2871f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
288fe1ed1b8Smatthias.ringwald             if (conn) {
289b448a0e7Smatthias.ringwald                 if (!packet[2]){
290c8e4258aSmatthias.ringwald                     conn->state = OPEN;
291fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
292fe1ed1b8Smatthias.ringwald                     conn->flags = 0;
293ee091cf1Smatthias.ringwald 
294ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
295c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
296ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
297ee091cf1Smatthias.ringwald 
298fe1ed1b8Smatthias.ringwald                     printf("New connection: handle %u, ", conn->con_handle);
299fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
300fe1ed1b8Smatthias.ringwald                     printf("\n");
30143bfb1bdSmatthias.ringwald 
30243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
303b448a0e7Smatthias.ringwald                 } else {
304b448a0e7Smatthias.ringwald                     // connection failed, remove entry
305b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
306b448a0e7Smatthias.ringwald                     free( conn );
307fe1ed1b8Smatthias.ringwald                 }
308fe1ed1b8Smatthias.ringwald             }
3096772a24cSmatthias.ringwald             break;
310fe1ed1b8Smatthias.ringwald 
3116772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
312fe1ed1b8Smatthias.ringwald             if (!packet[2]){
313fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
314fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
315fe1ed1b8Smatthias.ringwald                 if (conn) {
316fe1ed1b8Smatthias.ringwald                     printf("Connection closed: handle %u, ", conn->con_handle);
317fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
318fe1ed1b8Smatthias.ringwald                     printf("\n");
319ee091cf1Smatthias.ringwald                     run_loop_remove_timer(&conn->timeout);
320fe1ed1b8Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
321fe1ed1b8Smatthias.ringwald                     free( conn );
32243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
323fe1ed1b8Smatthias.ringwald                 }
324fe1ed1b8Smatthias.ringwald             }
3256772a24cSmatthias.ringwald             break;
3266772a24cSmatthias.ringwald 
3276772a24cSmatthias.ringwald         default:
3286772a24cSmatthias.ringwald             break;
329fe1ed1b8Smatthias.ringwald     }
330fe1ed1b8Smatthias.ringwald 
3313429f56bSmatthias.ringwald     // handle BT initialization
3323429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
3337301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
3347301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
3357301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
3367301ad89Smatthias.ringwald         // }
3377301ad89Smatthias.ringwald         // handle normal init sequence
3383429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
3393429f56bSmatthias.ringwald             // odd: waiting for event
3403429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
3413429f56bSmatthias.ringwald                 hci_stack.substate++;
3423429f56bSmatthias.ringwald             }
3433429f56bSmatthias.ringwald         }
34422909952Smatthias.ringwald     }
34522909952Smatthias.ringwald 
34616833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
34794ab26f8Smatthias.ringwald 
34894ab26f8Smatthias.ringwald 	// execute main loop
34994ab26f8Smatthias.ringwald 	hci_run();
35016833f0aSmatthias.ringwald }
35116833f0aSmatthias.ringwald 
352fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
3531cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
35416833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
35516833f0aSmatthias.ringwald }
3561cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
35716833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
35816833f0aSmatthias.ringwald }
35916833f0aSmatthias.ringwald 
36011e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
361475c8125Smatthias.ringwald 
362475c8125Smatthias.ringwald     // reference to use transport layer implementation
36316833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
364475c8125Smatthias.ringwald 
36511e23e5fSmatthias.ringwald     // references to used control implementation
3667301ad89Smatthias.ringwald     if (control) {
36711e23e5fSmatthias.ringwald         hci_stack.control = control;
3687301ad89Smatthias.ringwald     } else {
3697301ad89Smatthias.ringwald         hci_stack.control = &null_control;
3707301ad89Smatthias.ringwald     }
37111e23e5fSmatthias.ringwald 
37211e23e5fSmatthias.ringwald     // reference to used config
37311e23e5fSmatthias.ringwald     hci_stack.config = config;
37411e23e5fSmatthias.ringwald 
375fe1ed1b8Smatthias.ringwald     // no connections yet
376fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
377fe1ed1b8Smatthias.ringwald 
37802ea9861Smatthias.ringwald     // empty cmd buffer
37916833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
38016833f0aSmatthias.ringwald 
38116833f0aSmatthias.ringwald     // higher level handler
38216833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
38316833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
38416833f0aSmatthias.ringwald 
38516833f0aSmatthias.ringwald     // register packet handlers with transport
38616833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
38716833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
388475c8125Smatthias.ringwald }
389475c8125Smatthias.ringwald 
390475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
391f12adbd6Smatthias.ringwald     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
3927301ad89Smatthias.ringwald 
393038bc64cSmatthias.ringwald         // power on
394038bc64cSmatthias.ringwald         int err = hci_stack.control->on(hci_stack.config);
395038bc64cSmatthias.ringwald         if (err){
396ac2e07f8Smatthias.ringwald             fprintf(stderr, "POWER_ON failed\n");
397038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
398038bc64cSmatthias.ringwald             return err;
399038bc64cSmatthias.ringwald         }
400038bc64cSmatthias.ringwald 
401038bc64cSmatthias.ringwald         // open low-level device
402038bc64cSmatthias.ringwald         err = hci_stack.hci_transport->open(hci_stack.config);
403038bc64cSmatthias.ringwald         if (err){
404ac2e07f8Smatthias.ringwald             fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n");
405038bc64cSmatthias.ringwald             hci_stack.control->off(hci_stack.config);
406038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
407038bc64cSmatthias.ringwald             return err;
408038bc64cSmatthias.ringwald         }
409038bc64cSmatthias.ringwald 
4107301ad89Smatthias.ringwald         // set up state machine
4117301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
4127301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
4137301ad89Smatthias.ringwald         hci_stack.substate = 0;
4147301ad89Smatthias.ringwald 
415f12adbd6Smatthias.ringwald     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
4167301ad89Smatthias.ringwald 
4177301ad89Smatthias.ringwald         // close low-level device
4187301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
4197301ad89Smatthias.ringwald 
4207301ad89Smatthias.ringwald         // power off
42111e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
42243bfb1bdSmatthias.ringwald 
42343bfb1bdSmatthias.ringwald         // we're off now
42443bfb1bdSmatthias.ringwald         hci_stack.state = HCI_STATE_OFF;
42511e23e5fSmatthias.ringwald     }
42668d92d03Smatthias.ringwald 
427038bc64cSmatthias.ringwald     // create internal event
428ee8bf225Smatthias.ringwald 	hci_emit_state();
429ee8bf225Smatthias.ringwald 
43068d92d03Smatthias.ringwald 	// trigger next/first action
43168d92d03Smatthias.ringwald 	hci_run();
43268d92d03Smatthias.ringwald 
433475c8125Smatthias.ringwald     return 0;
434475c8125Smatthias.ringwald }
435475c8125Smatthias.ringwald 
43606b35ec0Smatthias.ringwald void hci_run(){
4373429f56bSmatthias.ringwald     switch (hci_stack.state){
4383429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
4393429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
4403429f56bSmatthias.ringwald                 // odd: waiting for command completion
44106b35ec0Smatthias.ringwald                 return;
4423429f56bSmatthias.ringwald             }
4433429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
4443429f56bSmatthias.ringwald                 // cannot send command yet
44506b35ec0Smatthias.ringwald                 return;
4463429f56bSmatthias.ringwald             }
4473429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
4483429f56bSmatthias.ringwald                 case 0:
44922909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
4503429f56bSmatthias.ringwald                     break;
4513429f56bSmatthias.ringwald 				case 1:
452f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
453f432a6ddSmatthias.ringwald 					break;
454f432a6ddSmatthias.ringwald                 case 2:
4553429f56bSmatthias.ringwald                     // ca. 15 sec
4563429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
4573429f56bSmatthias.ringwald                     break;
458f432a6ddSmatthias.ringwald 				case 3:
459bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
460f432a6ddSmatthias.ringwald 					break;
461f432a6ddSmatthias.ringwald                 case 4:
4623429f56bSmatthias.ringwald                     // done.
4633429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
464b360b6adSmatthias.ringwald                     hci_emit_state();
4653429f56bSmatthias.ringwald                     break;
4663429f56bSmatthias.ringwald                 default:
4673429f56bSmatthias.ringwald                     break;
468475c8125Smatthias.ringwald             }
4693429f56bSmatthias.ringwald             hci_stack.substate++;
4703429f56bSmatthias.ringwald             break;
4713429f56bSmatthias.ringwald         default:
4723429f56bSmatthias.ringwald             break;
4731f504dbdSmatthias.ringwald     }
4743429f56bSmatthias.ringwald }
47516833f0aSmatthias.ringwald 
47631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
477c8e4258aSmatthias.ringwald     bd_addr_t addr;
478c8e4258aSmatthias.ringwald     hci_connection_t * conn;
479c8e4258aSmatthias.ringwald     // house-keeping
480c8e4258aSmatthias.ringwald 
481c8e4258aSmatthias.ringwald     // create_connection?
482c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
483c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
484c8e4258aSmatthias.ringwald         printf("Create_connection to "); print_bd_addr(addr); printf("\n");
485c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
486c8e4258aSmatthias.ringwald         if (conn) {
487c8e4258aSmatthias.ringwald             // if connection exists
488c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
489c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
490c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
491c8e4258aSmatthias.ringwald             }
492c8e4258aSmatthias.ringwald             //    otherwise, just ignore
493c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
494c8e4258aSmatthias.ringwald 
495c8e4258aSmatthias.ringwald         } else{
496c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
497c8e4258aSmatthias.ringwald             if (conn){
498c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
499c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
500c8e4258aSmatthias.ringwald             }
501c8e4258aSmatthias.ringwald         }
502c8e4258aSmatthias.ringwald     }
503c8e4258aSmatthias.ringwald 
504c8e4258aSmatthias.ringwald     // accept connection
505c8e4258aSmatthias.ringwald 
506c8e4258aSmatthias.ringwald     // reject connection
507c8e4258aSmatthias.ringwald 
508c8e4258aSmatthias.ringwald     // close_connection?
509c8e4258aSmatthias.ringwald       // set state = SENT_DISCONNECT
510c8e4258aSmatthias.ringwald 
51131452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
51231452debSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
51331452debSmatthias.ringwald }
5148adf0ddaSmatthias.ringwald 
5151cd208adSmatthias.ringwald /**
5161cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
5171cd208adSmatthias.ringwald  */
5181cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
5191cd208adSmatthias.ringwald     va_list argptr;
5201cd208adSmatthias.ringwald     va_start(argptr, cmd);
5211cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
5221cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
5231cd208adSmatthias.ringwald     va_end(argptr);
5241cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
52593b8dc03Smatthias.ringwald }
526c8e4258aSmatthias.ringwald 
527ee091cf1Smatthias.ringwald // Create various non-HCI events.
528ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
529ee091cf1Smatthias.ringwald 
530c8e4258aSmatthias.ringwald void hci_emit_state(){
531c8e4258aSmatthias.ringwald     uint8_t len = 3;
532c8e4258aSmatthias.ringwald     uint8_t event[len];
53380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
5341b0e3922Smatthias.ringwald     event[1] = len - 3;
535c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
536c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
537c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
538c8e4258aSmatthias.ringwald }
539c8e4258aSmatthias.ringwald 
540c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
541c8e4258aSmatthias.ringwald     uint8_t len = 13;
542c8e4258aSmatthias.ringwald     uint8_t event[len];
543c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
5441b0e3922Smatthias.ringwald     event[1] = len - 3;
545c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
546c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
547c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
548c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
549c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
550c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
551c8e4258aSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
552c8e4258aSmatthias.ringwald }
553c8e4258aSmatthias.ringwald 
554ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
555ee091cf1Smatthias.ringwald     uint8_t len = 4;
556ee091cf1Smatthias.ringwald     uint8_t event[len];
55780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
5581b0e3922Smatthias.ringwald     event[1] = len - 2;
559ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
560ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
561ee091cf1Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
562ee091cf1Smatthias.ringwald }
56343bfb1bdSmatthias.ringwald 
56443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
56543bfb1bdSmatthias.ringwald     uint8_t len = 3;
56643bfb1bdSmatthias.ringwald     uint8_t event[len];
56780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
5681b0e3922Smatthias.ringwald     event[1] = len - 2;
56943bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
57043bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
57143bfb1bdSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
57243bfb1bdSmatthias.ringwald }
573038bc64cSmatthias.ringwald 
574038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
5751b0e3922Smatthias.ringwald     uint8_t len = 2;
576038bc64cSmatthias.ringwald     uint8_t event[len];
57780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
5781b0e3922Smatthias.ringwald     event[1] = len - 2;
579038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
580038bc64cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
581038bc64cSmatthias.ringwald }
5821b0e3922Smatthias.ringwald 
5831b0e3922Smatthias.ringwald 
5841b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
5851b0e3922Smatthias.ringwald     uint8_t len = 6;
5861b0e3922Smatthias.ringwald     uint8_t event[len];
5871b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
5881b0e3922Smatthias.ringwald     event[1] = len - 2;
5891b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
5901b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
5911b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
5921b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
5931b0e3922Smatthias.ringwald     hci_stack.event_packet_handler(event, len);
5941b0e3922Smatthias.ringwald }
5951b0e3922Smatthias.ringwald 
5962ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
5972ed6235cSmatthias.ringwald     uint8_t len = 3;
5982ed6235cSmatthias.ringwald     uint8_t event[len];
5992ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
600*ddce1d8dSmatthias.ringwald     event[1] = len - 2;
6012ed6235cSmatthias.ringwald     event[2] = enabled;
6022ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
6032ed6235cSmatthias.ringwald     hci_stack.event_packet_handler(event, len);
6042ed6235cSmatthias.ringwald }
605