xref: /btstack/src/hci.c (revision 8d213e1a0d0f3fe8b6492fb288975e68b50c11bf)
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 
397f2435e6Smatthias.ringwald #include "hci.h"
407f2435e6Smatthias.ringwald 
41475c8125Smatthias.ringwald #include <unistd.h>
4293b8dc03Smatthias.ringwald #include <stdarg.h>
4393b8dc03Smatthias.ringwald #include <string.h>
4456fe0872Smatthias.ringwald #include <stdio.h>
457f2435e6Smatthias.ringwald 
467f2435e6Smatthias.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;
1047fde4af9Smatthias.ringwald     conn->authentication_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;
11056cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
111c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
112c8e4258aSmatthias.ringwald     return conn;
113c8e4258aSmatthias.ringwald }
114c8e4258aSmatthias.ringwald 
115c8e4258aSmatthias.ringwald /**
11606b35ec0Smatthias.ringwald  * get connection for given address
11797addcc5Smatthias.ringwald  *
11897addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
11997addcc5Smatthias.ringwald  */
120fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
12106b35ec0Smatthias.ringwald     linked_item_t *it;
12206b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
12306b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
12406b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
12506b35ec0Smatthias.ringwald         }
12606b35ec0Smatthias.ringwald     }
12706b35ec0Smatthias.ringwald     return NULL;
12806b35ec0Smatthias.ringwald }
12906b35ec0Smatthias.ringwald 
13043bfb1bdSmatthias.ringwald /**
13180ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1327fde4af9Smatthias.ringwald  */
1337fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1347fde4af9Smatthias.ringwald     bd_addr_t addr;
1357fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1367fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1377fde4af9Smatthias.ringwald     if (conn) {
1387fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
13980ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1407fde4af9Smatthias.ringwald     }
1417fde4af9Smatthias.ringwald }
1427fde4af9Smatthias.ringwald 
14380ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
14480ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
14580ca58a0Smatthias.ringwald     if (!conn) return 0;
14680ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
14780ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
14880ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
14980ca58a0Smatthias.ringwald     return 1;
15080ca58a0Smatthias.ringwald }
15180ca58a0Smatthias.ringwald 
152c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
153c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
154c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
155c12e46e7Smatthias.ringwald     }
156c12e46e7Smatthias.ringwald }
157c12e46e7Smatthias.ringwald 
1587fde4af9Smatthias.ringwald 
1597fde4af9Smatthias.ringwald /**
16043bfb1bdSmatthias.ringwald  * count connections
16143bfb1bdSmatthias.ringwald  */
16243bfb1bdSmatthias.ringwald static int nr_hci_connections(){
16356c253c9Smatthias.ringwald     int count = 0;
16443bfb1bdSmatthias.ringwald     linked_item_t *it;
16543bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
16643bfb1bdSmatthias.ringwald     return count;
16743bfb1bdSmatthias.ringwald }
168c8e4258aSmatthias.ringwald 
16997addcc5Smatthias.ringwald /**
170ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
17116833f0aSmatthias.ringwald  */
1722718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
17316833f0aSmatthias.ringwald }
17416833f0aSmatthias.ringwald 
175998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
176998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
177998906cdSmatthias.ringwald     if (!connection) {
178998906cdSmatthias.ringwald         log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
179998906cdSmatthias.ringwald         return 0;
180998906cdSmatthias.ringwald     }
181998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
182998906cdSmatthias.ringwald }
183998906cdSmatthias.ringwald 
184998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
185998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
186998906cdSmatthias.ringwald     linked_item_t *it;
187998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
188998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
189998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
190998906cdSmatthias.ringwald             log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
191998906cdSmatthias.ringwald             return 0;
192998906cdSmatthias.ringwald         }
193998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
194998906cdSmatthias.ringwald     }
195998906cdSmatthias.ringwald     return free_slots;
196998906cdSmatthias.ringwald }
197998906cdSmatthias.ringwald 
1985250fb9eSmatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
1995250fb9eSmatthias.ringwald     return hci_stack.acl_data_packet_length;
2005250fb9eSmatthias.ringwald }
2015250fb9eSmatthias.ringwald 
202998906cdSmatthias.ringwald int hci_ready_to_send(hci_con_handle_t handle){
203998906cdSmatthias.ringwald     return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2;
204998906cdSmatthias.ringwald }
205c8e4258aSmatthias.ringwald 
206ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2077856c818Smatthias.ringwald 
2086218e6f1Smatthias.ringwald     // check for free places on BT module
2096218e6f1Smatthias.ringwald     if (!hci_number_free_acl_slots()) return -1;
2106218e6f1Smatthias.ringwald 
2117856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2127856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
21356cf178bSmatthias.ringwald     if (!connection) return 0;
21456cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
21556cf178bSmatthias.ringwald 
21656cf178bSmatthias.ringwald     // count packet
21756cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2188ea03fa5Smatthias.ringwald     // log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2197856c818Smatthias.ringwald 
2206218e6f1Smatthias.ringwald     // send packet - ignore errors
221622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2226218e6f1Smatthias.ringwald 
2236218e6f1Smatthias.ringwald     return 0;
224ee091cf1Smatthias.ringwald }
225ee091cf1Smatthias.ringwald 
22616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2277856c818Smatthias.ringwald 
2287856c818Smatthias.ringwald     // get info
2297856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2307856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2317856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2327856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2337856c818Smatthias.ringwald 
2347856c818Smatthias.ringwald     // ignore non-registered handle
2357856c818Smatthias.ringwald     if (!conn){
2367f2435e6Smatthias.ringwald         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2377856c818Smatthias.ringwald         return;
2387856c818Smatthias.ringwald     }
2397856c818Smatthias.ringwald 
2407856c818Smatthias.ringwald     // update idle timestamp
2417856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2427856c818Smatthias.ringwald 
2437856c818Smatthias.ringwald     // handle different packet types
2447856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2457856c818Smatthias.ringwald 
2467856c818Smatthias.ringwald         case 0x01: // continuation fragment
2477856c818Smatthias.ringwald 
2487856c818Smatthias.ringwald             // sanity check
2497856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2507f2435e6Smatthias.ringwald                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2517856c818Smatthias.ringwald                 return;
2527856c818Smatthias.ringwald             }
2537856c818Smatthias.ringwald 
2547856c818Smatthias.ringwald             // append fragment payload (header already stored)
2557856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2567856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2577856c818Smatthias.ringwald 
2587f2435e6Smatthias.ringwald             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
2597856c818Smatthias.ringwald             //        acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
2607856c818Smatthias.ringwald 
2617856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2627856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2637856c818Smatthias.ringwald 
2642718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2657856c818Smatthias.ringwald                 // reset recombination buffer
2667856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2677856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2687856c818Smatthias.ringwald             }
2697856c818Smatthias.ringwald             break;
2707856c818Smatthias.ringwald 
2717856c818Smatthias.ringwald         case 0x02: { // first fragment
2727856c818Smatthias.ringwald 
2737856c818Smatthias.ringwald             // sanity check
2747856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
2757f2435e6Smatthias.ringwald                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
2767856c818Smatthias.ringwald                 return;
2777856c818Smatthias.ringwald             }
2787856c818Smatthias.ringwald 
2797856c818Smatthias.ringwald             // peek into L2CAP packet!
2807856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
2817856c818Smatthias.ringwald 
2827856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
2837856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
2847856c818Smatthias.ringwald 
2857856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
2862718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
2877856c818Smatthias.ringwald 
2887856c818Smatthias.ringwald             } else {
2897856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
2907856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
2917856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
2927856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
2937856c818Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
2947f2435e6Smatthias.ringwald                 // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
2957856c818Smatthias.ringwald             }
2967856c818Smatthias.ringwald             break;
2977856c818Smatthias.ringwald 
2987856c818Smatthias.ringwald         }
2997856c818Smatthias.ringwald         default:
3007f2435e6Smatthias.ringwald             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3017856c818Smatthias.ringwald             return;
3027856c818Smatthias.ringwald     }
30394ab26f8Smatthias.ringwald 
30494ab26f8Smatthias.ringwald     // execute main loop
30594ab26f8Smatthias.ringwald     hci_run();
30616833f0aSmatthias.ringwald }
30722909952Smatthias.ringwald 
308c7e0c5f6Smatthias.ringwald static hci_shutdown_connection(hci_connection_t *conn){
309c7e0c5f6Smatthias.ringwald     log_dbg("Connection closed: handle %u, ", conn->con_handle);
310c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
311c7e0c5f6Smatthias.ringwald     log_dbg("\n");
312c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
313c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
314c7e0c5f6Smatthias.ringwald     free( conn );
315c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
316c7e0c5f6Smatthias.ringwald }
317c7e0c5f6Smatthias.ringwald 
31874ec757aSmatthias.ringwald // avoid huge local variables
31974ec757aSmatthias.ringwald static device_name_t device_name;
32016833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3211281a47eSmatthias.ringwald     bd_addr_t addr;
322fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3231f7b95a1Smatthias.ringwald     hci_connection_t * conn;
32456cf178bSmatthias.ringwald     int i;
32529d53098Smatthias.ringwald     link_key_t link_key;
32622909952Smatthias.ringwald 
327e2edc0c3Smatthias.ringwald     // get num_cmd_packets
328e2edc0c3Smatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
329e2edc0c3Smatthias.ringwald         // Get Num_HCI_Command_Packets
330e2edc0c3Smatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
331e2edc0c3Smatthias.ringwald     }
332e2edc0c3Smatthias.ringwald 
3336772a24cSmatthias.ringwald     switch (packet[0]) {
33422909952Smatthias.ringwald 
3356772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
336e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
337e2edc0c3Smatthias.ringwald                 // from offset 5
338e2edc0c3Smatthias.ringwald                 // status
339e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
34056cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
341e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
34256cf178bSmatthias.ringwald                 // ignore: total num SCO packets
34356cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
344e2edc0c3Smatthias.ringwald                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
345e2edc0c3Smatthias.ringwald                 }
34656cf178bSmatthias.ringwald             }
34756cf178bSmatthias.ringwald             break;
34856cf178bSmatthias.ringwald 
34956cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
35056cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
35156cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
35256cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
35356cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
35456cf178bSmatthias.ringwald                 if (!conn){
35556cf178bSmatthias.ringwald                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
35656cf178bSmatthias.ringwald                     continue;
35756cf178bSmatthias.ringwald                 }
35856cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
3598ea03fa5Smatthias.ringwald                 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
36056cf178bSmatthias.ringwald             }
3616772a24cSmatthias.ringwald             break;
3626772a24cSmatthias.ringwald 
3631f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
36437eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
36537eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
36637eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
367cf0b66f0Smatthias.ringwald             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
36837eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
3691f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
3701f7b95a1Smatthias.ringwald                 if (!conn) {
3711f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
3721f7b95a1Smatthias.ringwald                 }
3731f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
3741f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
37537eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
37637eaa4cfSmatthias.ringwald             } else {
37737eaa4cfSmatthias.ringwald                 // TODO: decline request
37837eaa4cfSmatthias.ringwald             }
3791f7b95a1Smatthias.ringwald             break;
3801f7b95a1Smatthias.ringwald 
3816772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
382fe1ed1b8Smatthias.ringwald             // Connection management
383fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
384cf0b66f0Smatthias.ringwald             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
3851f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
386fe1ed1b8Smatthias.ringwald             if (conn) {
387b448a0e7Smatthias.ringwald                 if (!packet[2]){
388c8e4258aSmatthias.ringwald                     conn->state = OPEN;
389fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
390ee091cf1Smatthias.ringwald 
391ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
392c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
393ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
394ee091cf1Smatthias.ringwald 
395cf0b66f0Smatthias.ringwald                     log_dbg("New connection: handle %u, ", conn->con_handle);
396fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
397cf0b66f0Smatthias.ringwald                     log_dbg("\n");
39843bfb1bdSmatthias.ringwald 
39943bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
400b448a0e7Smatthias.ringwald                 } else {
401b448a0e7Smatthias.ringwald                     // connection failed, remove entry
402b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
403b448a0e7Smatthias.ringwald                     free( conn );
404c12e46e7Smatthias.ringwald 
405c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
406c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
407c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
408c12e46e7Smatthias.ringwald                     }
409fe1ed1b8Smatthias.ringwald                 }
410fe1ed1b8Smatthias.ringwald             }
4116772a24cSmatthias.ringwald             break;
412fe1ed1b8Smatthias.ringwald 
4137fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
4147fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
41574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
41629d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
41729d53098Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &addr, &link_key)){
41829d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_reply, &addr, &link_key);
41929d53098Smatthias.ringwald             } else {
42029d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
42129d53098Smatthias.ringwald             }
42229d53098Smatthias.ringwald             // request already answered
42329d53098Smatthias.ringwald             return;
4247fde4af9Smatthias.ringwald 
4257fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4267fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
42774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
42829d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
429287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
43029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4317fde4af9Smatthias.ringwald             break;
4327fde4af9Smatthias.ringwald 
4337fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4347fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4357fde4af9Smatthias.ringwald             break;
4367fde4af9Smatthias.ringwald 
43774ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
43874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
43974ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
44074ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
44174ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
44274ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
44374ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
44474ec757aSmatthias.ringwald             break;
44574ec757aSmatthias.ringwald 
44674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
44774ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
44874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
44974ec757aSmatthias.ringwald             // first send inq result packet
45074ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
45174ec757aSmatthias.ringwald             // then send cached remote names
45274ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
45374ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
45474ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
45574ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
45674ec757aSmatthias.ringwald                 }
45774ec757aSmatthias.ringwald             }
45874ec757aSmatthias.ringwald             return;
45974ec757aSmatthias.ringwald 
4606772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
461fe1ed1b8Smatthias.ringwald             if (!packet[2]){
462fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
463fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
464fe1ed1b8Smatthias.ringwald                 if (conn) {
465c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
466fe1ed1b8Smatthias.ringwald                 }
467fe1ed1b8Smatthias.ringwald             }
4686772a24cSmatthias.ringwald             break;
4696772a24cSmatthias.ringwald 
4706772a24cSmatthias.ringwald         default:
4716772a24cSmatthias.ringwald             break;
472fe1ed1b8Smatthias.ringwald     }
473fe1ed1b8Smatthias.ringwald 
4743429f56bSmatthias.ringwald     // handle BT initialization
4753429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
4767301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
4777301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
4787301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
4797301ad89Smatthias.ringwald         // }
4807301ad89Smatthias.ringwald         // handle normal init sequence
4813429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
4823429f56bSmatthias.ringwald             // odd: waiting for event
4833429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
4843429f56bSmatthias.ringwald                 hci_stack.substate++;
4853429f56bSmatthias.ringwald             }
4863429f56bSmatthias.ringwald         }
48722909952Smatthias.ringwald     }
48822909952Smatthias.ringwald 
4892718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
49094ab26f8Smatthias.ringwald 
49194ab26f8Smatthias.ringwald 	// execute main loop
49294ab26f8Smatthias.ringwald 	hci_run();
49316833f0aSmatthias.ringwald }
49416833f0aSmatthias.ringwald 
49510e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
49610e830c9Smatthias.ringwald     switch (packet_type) {
49710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
49810e830c9Smatthias.ringwald             event_handler(packet, size);
49910e830c9Smatthias.ringwald             break;
50010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
50110e830c9Smatthias.ringwald             acl_handler(packet, size);
50210e830c9Smatthias.ringwald             break;
50310e830c9Smatthias.ringwald         default:
50410e830c9Smatthias.ringwald             break;
50510e830c9Smatthias.ringwald     }
50610e830c9Smatthias.ringwald }
50710e830c9Smatthias.ringwald 
508fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5092718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5102718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
51116833f0aSmatthias.ringwald }
51216833f0aSmatthias.ringwald 
513404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
514475c8125Smatthias.ringwald 
515475c8125Smatthias.ringwald     // reference to use transport layer implementation
51616833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
517475c8125Smatthias.ringwald 
51811e23e5fSmatthias.ringwald     // references to used control implementation
51911e23e5fSmatthias.ringwald     hci_stack.control = control;
52011e23e5fSmatthias.ringwald 
52111e23e5fSmatthias.ringwald     // reference to used config
52211e23e5fSmatthias.ringwald     hci_stack.config = config;
52311e23e5fSmatthias.ringwald 
524fe1ed1b8Smatthias.ringwald     // no connections yet
525fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
526fe1ed1b8Smatthias.ringwald 
52702ea9861Smatthias.ringwald     // empty cmd buffer
52816833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
52916833f0aSmatthias.ringwald 
53016833f0aSmatthias.ringwald     // higher level handler
5312718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
53216833f0aSmatthias.ringwald 
533404843c1Smatthias.ringwald     // store and open remote device db
534404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
535404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
536404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
537404843c1Smatthias.ringwald     }
53829d53098Smatthias.ringwald 
53916833f0aSmatthias.ringwald     // register packet handlers with transport
54010e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
541475c8125Smatthias.ringwald }
542475c8125Smatthias.ringwald 
543404843c1Smatthias.ringwald void hci_close(){
544404843c1Smatthias.ringwald     // close remote device db
545404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
546404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
547404843c1Smatthias.ringwald     }
548404843c1Smatthias.ringwald }
549404843c1Smatthias.ringwald 
550*8d213e1aSmatthias.ringwald // State-Module-Driver overview
551*8d213e1aSmatthias.ringwald // state                    module  low-level
552*8d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
553*8d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
554*8d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
555*8d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
556*8d213e1aSmatthias.ringwald // HCI_STATE_SLEEPING,       ??        ??
557*8d213e1aSmatthias.ringwald // HCI_STATE_FALLING_ASLEEP  ??        ??
558c7e0c5f6Smatthias.ringwald 
559*8d213e1aSmatthias.ringwald static int hci_power_control_on(){
5607301ad89Smatthias.ringwald 
561038bc64cSmatthias.ringwald     // power on
562f9a30166Smatthias.ringwald     int err = 0;
563f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
564f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
565f9a30166Smatthias.ringwald     }
566038bc64cSmatthias.ringwald     if (err){
5677f2435e6Smatthias.ringwald         log_err( "POWER_ON failed\n");
568038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
569038bc64cSmatthias.ringwald         return err;
570038bc64cSmatthias.ringwald     }
571038bc64cSmatthias.ringwald 
572038bc64cSmatthias.ringwald     // open low-level device
573038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
574038bc64cSmatthias.ringwald     if (err){
5757f2435e6Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
576f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
577f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
578f9a30166Smatthias.ringwald         }
579038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
580038bc64cSmatthias.ringwald         return err;
581038bc64cSmatthias.ringwald     }
582*8d213e1aSmatthias.ringwald     return 0;
583*8d213e1aSmatthias.ringwald }
584038bc64cSmatthias.ringwald 
585*8d213e1aSmatthias.ringwald static void hci_power_control_off(){
586*8d213e1aSmatthias.ringwald 
587*8d213e1aSmatthias.ringwald     // close low-level device
588*8d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
589*8d213e1aSmatthias.ringwald 
590*8d213e1aSmatthias.ringwald     // power off
591*8d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
592*8d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
593*8d213e1aSmatthias.ringwald     }
594*8d213e1aSmatthias.ringwald }
595*8d213e1aSmatthias.ringwald 
596*8d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
597*8d213e1aSmatthias.ringwald     int err = 0;
598*8d213e1aSmatthias.ringwald     switch (hci_stack.state){
599*8d213e1aSmatthias.ringwald 
600*8d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
601*8d213e1aSmatthias.ringwald             switch (power_mode){
602*8d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
603*8d213e1aSmatthias.ringwald                     err = hci_power_control_on();
604*8d213e1aSmatthias.ringwald                     if (err) return err;
6057301ad89Smatthias.ringwald                     // set up state machine
6067301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
6077301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
6087301ad89Smatthias.ringwald                     hci_stack.substate = 0;
609*8d213e1aSmatthias.ringwald                     break;
610*8d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
611*8d213e1aSmatthias.ringwald                     // do nothing
612*8d213e1aSmatthias.ringwald                     break;
613*8d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
614*8d213e1aSmatthias.ringwald                     // TODO ...
615*8d213e1aSmatthias.ringwald                     break;
616*8d213e1aSmatthias.ringwald             }
617*8d213e1aSmatthias.ringwald             break;
6187301ad89Smatthias.ringwald 
619*8d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
620*8d213e1aSmatthias.ringwald             switch (power_mode){
621*8d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
622*8d213e1aSmatthias.ringwald                     // do nothing
623*8d213e1aSmatthias.ringwald                     break;
624*8d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
625*8d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
626*8d213e1aSmatthias.ringwald                     hci_power_control_off();
627*8d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_OFF;
628*8d213e1aSmatthias.ringwald                     break;
629*8d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
630*8d213e1aSmatthias.ringwald                     // TODO ...
631*8d213e1aSmatthias.ringwald                     break;
632*8d213e1aSmatthias.ringwald             }
633*8d213e1aSmatthias.ringwald             break;
6347301ad89Smatthias.ringwald 
635*8d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
636*8d213e1aSmatthias.ringwald             switch (power_mode){
637*8d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
638*8d213e1aSmatthias.ringwald                     // do nothing
639*8d213e1aSmatthias.ringwald                     break;
640*8d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
641c7e0c5f6Smatthias.ringwald                     // see hci_run
642c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
643*8d213e1aSmatthias.ringwald                     break;
644*8d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
645*8d213e1aSmatthias.ringwald                     // TODO ...
646*8d213e1aSmatthias.ringwald                     break;
647*8d213e1aSmatthias.ringwald             }
648*8d213e1aSmatthias.ringwald             break;
6497301ad89Smatthias.ringwald 
650*8d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
651*8d213e1aSmatthias.ringwald             switch (power_mode){
652*8d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
653*8d213e1aSmatthias.ringwald                     // set up state machine
654*8d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
655*8d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
656*8d213e1aSmatthias.ringwald                     break;
657*8d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
658*8d213e1aSmatthias.ringwald                     // do nothing
659*8d213e1aSmatthias.ringwald                     break;
660*8d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
661*8d213e1aSmatthias.ringwald                     // TODO ...
662*8d213e1aSmatthias.ringwald                     break;
663*8d213e1aSmatthias.ringwald             }
664*8d213e1aSmatthias.ringwald             break;
665*8d213e1aSmatthias.ringwald 
666*8d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
667*8d213e1aSmatthias.ringwald             switch (power_mode){
668*8d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
669*8d213e1aSmatthias.ringwald                     // TODO ...
670*8d213e1aSmatthias.ringwald                     break;
671*8d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
672*8d213e1aSmatthias.ringwald                     // TODO ...
673*8d213e1aSmatthias.ringwald                     break;
674*8d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
675*8d213e1aSmatthias.ringwald                     // TODO ...
676*8d213e1aSmatthias.ringwald                     break;
677*8d213e1aSmatthias.ringwald             }
678*8d213e1aSmatthias.ringwald             break;
679*8d213e1aSmatthias.ringwald 
680*8d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
681*8d213e1aSmatthias.ringwald             switch (power_mode){
682*8d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
683*8d213e1aSmatthias.ringwald                     // TODO ...
684*8d213e1aSmatthias.ringwald                     break;
685*8d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
686*8d213e1aSmatthias.ringwald                     // TODO ...
687*8d213e1aSmatthias.ringwald                     break;
688*8d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
689*8d213e1aSmatthias.ringwald                     // TODO ...
690*8d213e1aSmatthias.ringwald                     break;
691*8d213e1aSmatthias.ringwald             }
692*8d213e1aSmatthias.ringwald             break;
69311e23e5fSmatthias.ringwald     }
69468d92d03Smatthias.ringwald 
695038bc64cSmatthias.ringwald     // create internal event
696ee8bf225Smatthias.ringwald 	hci_emit_state();
697ee8bf225Smatthias.ringwald 
69868d92d03Smatthias.ringwald 	// trigger next/first action
69968d92d03Smatthias.ringwald 	hci_run();
70068d92d03Smatthias.ringwald 
701475c8125Smatthias.ringwald     return 0;
702475c8125Smatthias.ringwald }
703475c8125Smatthias.ringwald 
70406b35ec0Smatthias.ringwald void hci_run(){
7058a485f27Smatthias.ringwald 
706c7e0c5f6Smatthias.ringwald     if (hci_stack.num_cmd_packets == 0) {
707c7e0c5f6Smatthias.ringwald         // cannot send command yet
708c7e0c5f6Smatthias.ringwald         return;
709c7e0c5f6Smatthias.ringwald     }
710c7e0c5f6Smatthias.ringwald 
711c7e0c5f6Smatthias.ringwald     hci_connection_t * connection;
712c7e0c5f6Smatthias.ringwald 
7133429f56bSmatthias.ringwald     switch (hci_stack.state){
7143429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7153429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
7163429f56bSmatthias.ringwald                 // odd: waiting for command completion
71706b35ec0Smatthias.ringwald                 return;
7183429f56bSmatthias.ringwald             }
71990919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
7203429f56bSmatthias.ringwald                 case 0:
72122909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
7223429f56bSmatthias.ringwald                     break;
7233429f56bSmatthias.ringwald                 case 1:
72490919203Smatthias.ringwald                     // custom initialization
72590919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
72690919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
72790919203Smatthias.ringwald                         if (cmd) {
72890919203Smatthias.ringwald                             int size = 3 + cmd[2];
729622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
7302f6c30e1Smatthias.ringwald                             hci_stack.substate = 0; // more init commands
73190919203Smatthias.ringwald                             break;
73290919203Smatthias.ringwald                         }
73390919203Smatthias.ringwald                     }
7342f6c30e1Smatthias.ringwald                     // otherwise continue
735f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
736f432a6ddSmatthias.ringwald 					break;
7372f6c30e1Smatthias.ringwald 				case 2:
738e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
739e2edc0c3Smatthias.ringwald 					break;
7402f6c30e1Smatthias.ringwald                 case 3:
7413429f56bSmatthias.ringwald                     // ca. 15 sec
7423429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
7433429f56bSmatthias.ringwald                     break;
7442f6c30e1Smatthias.ringwald 				case 4:
745bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
746f432a6ddSmatthias.ringwald 					break;
7472f6c30e1Smatthias.ringwald                 case 5:
7488a485f27Smatthias.ringwald #ifndef EMBEDDED
7498a485f27Smatthias.ringwald                 {
7508a485f27Smatthias.ringwald                     char hostname[30];
7518a485f27Smatthias.ringwald                     gethostname(hostname, 30);
7528a485f27Smatthias.ringwald                     hostname[29] = '\0';
7538a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
7548a485f27Smatthias.ringwald                     break;
7558a485f27Smatthias.ringwald                 }
7568a485f27Smatthias.ringwald                 case 6:
7578a485f27Smatthias.ringwald #endif
7583429f56bSmatthias.ringwald                     // done.
7593429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
760b360b6adSmatthias.ringwald                     hci_emit_state();
7613429f56bSmatthias.ringwald                     break;
7623429f56bSmatthias.ringwald                 default:
7633429f56bSmatthias.ringwald                     break;
764475c8125Smatthias.ringwald             }
7653429f56bSmatthias.ringwald             hci_stack.substate++;
7663429f56bSmatthias.ringwald             break;
767c7e0c5f6Smatthias.ringwald 
768c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
769c7e0c5f6Smatthias.ringwald 
770c7e0c5f6Smatthias.ringwald             // close all open connections
771c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
772c7e0c5f6Smatthias.ringwald             if (connection){
773c7e0c5f6Smatthias.ringwald                 // send disconnect
774c7e0c5f6Smatthias.ringwald                 bt_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
775c7e0c5f6Smatthias.ringwald 
776c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
777c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
778c7e0c5f6Smatthias.ringwald 
779c7e0c5f6Smatthias.ringwald                 // remove from table
780c7e0c5f6Smatthias.ringwald                 hci_stack.connections = connection->item.next;
781c7e0c5f6Smatthias.ringwald                 return;
782c7e0c5f6Smatthias.ringwald             }
783c7e0c5f6Smatthias.ringwald 
784c7e0c5f6Smatthias.ringwald             hci_power_control_off();
785c7e0c5f6Smatthias.ringwald 
786c7e0c5f6Smatthias.ringwald             // we're off now
787c7e0c5f6Smatthias.ringwald             hci_stack.state = HCI_STATE_OFF;
788c7e0c5f6Smatthias.ringwald             hci_emit_state();
789c7e0c5f6Smatthias.ringwald             break;
790c7e0c5f6Smatthias.ringwald 
7913429f56bSmatthias.ringwald         default:
7923429f56bSmatthias.ringwald             break;
7931f504dbdSmatthias.ringwald     }
7943429f56bSmatthias.ringwald }
79516833f0aSmatthias.ringwald 
79631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
797c8e4258aSmatthias.ringwald     bd_addr_t addr;
798c8e4258aSmatthias.ringwald     hci_connection_t * conn;
799c8e4258aSmatthias.ringwald     // house-keeping
800c8e4258aSmatthias.ringwald 
801c8e4258aSmatthias.ringwald     // create_connection?
802c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
803c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
804cf0b66f0Smatthias.ringwald         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
805c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
806c8e4258aSmatthias.ringwald         if (conn) {
807c8e4258aSmatthias.ringwald             // if connection exists
808c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
809c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
810c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
811c8e4258aSmatthias.ringwald             }
812c8e4258aSmatthias.ringwald             //    otherwise, just ignore
813c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
814c8e4258aSmatthias.ringwald 
815c8e4258aSmatthias.ringwald         } else{
816c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
817c8e4258aSmatthias.ringwald             if (conn){
818c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
819c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
820c8e4258aSmatthias.ringwald             }
821c8e4258aSmatthias.ringwald         }
822c8e4258aSmatthias.ringwald     }
823c8e4258aSmatthias.ringwald 
8247fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
8257fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
8267fde4af9Smatthias.ringwald     }
8277fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
8287fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
8297fde4af9Smatthias.ringwald     }
8307fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
8317fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
8327fde4af9Smatthias.ringwald     }
8337fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
8347fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
8357fde4af9Smatthias.ringwald     }
8367fde4af9Smatthias.ringwald 
8378ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
8388ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
8398ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
8408ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
8418ef73945Smatthias.ringwald         }
8428ef73945Smatthias.ringwald     }
843c8e4258aSmatthias.ringwald 
84431452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
845622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
84631452debSmatthias.ringwald }
8478adf0ddaSmatthias.ringwald 
8481cd208adSmatthias.ringwald /**
8491cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
8501cd208adSmatthias.ringwald  */
851fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
8521cd208adSmatthias.ringwald     va_list argptr;
8531cd208adSmatthias.ringwald     va_start(argptr, cmd);
8541cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
8551cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
8561cd208adSmatthias.ringwald     va_end(argptr);
8571cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
85893b8dc03Smatthias.ringwald }
859c8e4258aSmatthias.ringwald 
860ee091cf1Smatthias.ringwald // Create various non-HCI events.
861ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
862ee091cf1Smatthias.ringwald 
863c8e4258aSmatthias.ringwald void hci_emit_state(){
864c8e4258aSmatthias.ringwald     uint8_t len = 3;
865c8e4258aSmatthias.ringwald     uint8_t event[len];
86680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
8671b0e3922Smatthias.ringwald     event[1] = len - 3;
868c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
869c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
8702718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
871c8e4258aSmatthias.ringwald }
872c8e4258aSmatthias.ringwald 
873c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
874c8e4258aSmatthias.ringwald     uint8_t len = 13;
875c8e4258aSmatthias.ringwald     uint8_t event[len];
876c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
8771b0e3922Smatthias.ringwald     event[1] = len - 3;
878c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
879c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
880c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
881c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
882c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
883c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
8842718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
885c8e4258aSmatthias.ringwald }
886c8e4258aSmatthias.ringwald 
887ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
888ee091cf1Smatthias.ringwald     uint8_t len = 4;
889ee091cf1Smatthias.ringwald     uint8_t event[len];
89080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
8911b0e3922Smatthias.ringwald     event[1] = len - 2;
892ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
893ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
8942718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
895ee091cf1Smatthias.ringwald }
89643bfb1bdSmatthias.ringwald 
89743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
89843bfb1bdSmatthias.ringwald     uint8_t len = 3;
89943bfb1bdSmatthias.ringwald     uint8_t event[len];
90080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
9011b0e3922Smatthias.ringwald     event[1] = len - 2;
90243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
90343bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
9042718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
90543bfb1bdSmatthias.ringwald }
906038bc64cSmatthias.ringwald 
907038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
9081b0e3922Smatthias.ringwald     uint8_t len = 2;
909038bc64cSmatthias.ringwald     uint8_t event[len];
91080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
9111b0e3922Smatthias.ringwald     event[1] = len - 2;
912038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
9132718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
914038bc64cSmatthias.ringwald }
9151b0e3922Smatthias.ringwald 
9161b0e3922Smatthias.ringwald 
9171b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
9181b0e3922Smatthias.ringwald     uint8_t len = 6;
9191b0e3922Smatthias.ringwald     uint8_t event[len];
9201b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
9211b0e3922Smatthias.ringwald     event[1] = len - 2;
9221b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
9231b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
9241b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
9251b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
9262718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
9271b0e3922Smatthias.ringwald }
9281b0e3922Smatthias.ringwald 
9292ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
9302ed6235cSmatthias.ringwald     uint8_t len = 3;
9312ed6235cSmatthias.ringwald     uint8_t event[len];
9322ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
933ddce1d8dSmatthias.ringwald     event[1] = len - 2;
9342ed6235cSmatthias.ringwald     event[2] = enabled;
9352ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
9362718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
9372ed6235cSmatthias.ringwald }
938627c2f45Smatthias.ringwald 
939627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
940f653b6bdSmatthias.ringwald     uint16_t len = 2+1+6+248;
941627c2f45Smatthias.ringwald     uint8_t event[len];
942627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
943627c2f45Smatthias.ringwald     event[1] = len - 2;
944f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
9452f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
946f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
947627c2f45Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, len);
948627c2f45Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
949627c2f45Smatthias.ringwald }
950