xref: /btstack/src/hci.c (revision 0a90cc40903af7afed920cc23feea765fa4bbe97)
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 
30867a3e8ecSmatthias.ringwald static void 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 
3276772a24cSmatthias.ringwald     switch (packet[0]) {
32822909952Smatthias.ringwald 
3296772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3307ec5eeaaSmatthias.ringwald             // get num cmd packets
3317ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3327ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3337ec5eeaaSmatthias.ringwald 
334e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
335e2edc0c3Smatthias.ringwald                 // from offset 5
336e2edc0c3Smatthias.ringwald                 // status
337e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
33856cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
339e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
34056cf178bSmatthias.ringwald                 // ignore: total num SCO packets
34156cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
342e2edc0c3Smatthias.ringwald                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
343e2edc0c3Smatthias.ringwald                 }
34456cf178bSmatthias.ringwald             }
345381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
346381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
347381fbed8Smatthias.ringwald             }
34856cf178bSmatthias.ringwald             break;
34956cf178bSmatthias.ringwald 
3507ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
3517ec5eeaaSmatthias.ringwald             // get num cmd packets
3527ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
3537ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
3547ec5eeaaSmatthias.ringwald             break;
3557ec5eeaaSmatthias.ringwald 
35656cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
35756cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
35856cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
35956cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
36056cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
36156cf178bSmatthias.ringwald                 if (!conn){
36256cf178bSmatthias.ringwald                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
36356cf178bSmatthias.ringwald                     continue;
36456cf178bSmatthias.ringwald                 }
36556cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
3668ea03fa5Smatthias.ringwald                 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
36756cf178bSmatthias.ringwald             }
3686772a24cSmatthias.ringwald             break;
3696772a24cSmatthias.ringwald 
3701f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
37137eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
37237eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
37337eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
374cf0b66f0Smatthias.ringwald             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
37537eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
3761f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
3771f7b95a1Smatthias.ringwald                 if (!conn) {
3781f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
3791f7b95a1Smatthias.ringwald                 }
3801f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
3811f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
38237eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
38337eaa4cfSmatthias.ringwald             } else {
38437eaa4cfSmatthias.ringwald                 // TODO: decline request
38537eaa4cfSmatthias.ringwald             }
3861f7b95a1Smatthias.ringwald             break;
3871f7b95a1Smatthias.ringwald 
3886772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
389fe1ed1b8Smatthias.ringwald             // Connection management
390fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
391cf0b66f0Smatthias.ringwald             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
3921f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
393fe1ed1b8Smatthias.ringwald             if (conn) {
394b448a0e7Smatthias.ringwald                 if (!packet[2]){
395c8e4258aSmatthias.ringwald                     conn->state = OPEN;
396fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
397ee091cf1Smatthias.ringwald 
398ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
399c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
400ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
401ee091cf1Smatthias.ringwald 
402cf0b66f0Smatthias.ringwald                     log_dbg("New connection: handle %u, ", conn->con_handle);
403fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
404cf0b66f0Smatthias.ringwald                     log_dbg("\n");
40543bfb1bdSmatthias.ringwald 
40643bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
407b448a0e7Smatthias.ringwald                 } else {
408b448a0e7Smatthias.ringwald                     // connection failed, remove entry
409b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
410b448a0e7Smatthias.ringwald                     free( conn );
411c12e46e7Smatthias.ringwald 
412c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
413c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
414c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
415c12e46e7Smatthias.ringwald                     }
416fe1ed1b8Smatthias.ringwald                 }
417fe1ed1b8Smatthias.ringwald             }
4186772a24cSmatthias.ringwald             break;
419fe1ed1b8Smatthias.ringwald 
4207fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
4217fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
42274ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
42329d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
42429d53098Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &addr, &link_key)){
42529d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_reply, &addr, &link_key);
42629d53098Smatthias.ringwald             } else {
42729d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
42829d53098Smatthias.ringwald             }
42929d53098Smatthias.ringwald             // request already answered
43029d53098Smatthias.ringwald             return;
4317fde4af9Smatthias.ringwald 
4327fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4337fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
43474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
43529d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
436287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
43729d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4387fde4af9Smatthias.ringwald             break;
4397fde4af9Smatthias.ringwald 
4407fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4417fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4427fde4af9Smatthias.ringwald             break;
4437fde4af9Smatthias.ringwald 
44474ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
44574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
44674ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
44774ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
44874ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
44974ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
45074ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
45174ec757aSmatthias.ringwald             break;
45274ec757aSmatthias.ringwald 
45374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
45474ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
45574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
45674ec757aSmatthias.ringwald             // first send inq result packet
45774ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
45874ec757aSmatthias.ringwald             // then send cached remote names
45974ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
46074ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
46174ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
46274ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
46374ec757aSmatthias.ringwald                 }
46474ec757aSmatthias.ringwald             }
46574ec757aSmatthias.ringwald             return;
46674ec757aSmatthias.ringwald 
4676772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
468fe1ed1b8Smatthias.ringwald             if (!packet[2]){
469fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
470fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
471fe1ed1b8Smatthias.ringwald                 if (conn) {
472c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
473fe1ed1b8Smatthias.ringwald                 }
474fe1ed1b8Smatthias.ringwald             }
4756772a24cSmatthias.ringwald             break;
4766772a24cSmatthias.ringwald 
4776772a24cSmatthias.ringwald         default:
4786772a24cSmatthias.ringwald             break;
479fe1ed1b8Smatthias.ringwald     }
480fe1ed1b8Smatthias.ringwald 
4813429f56bSmatthias.ringwald     // handle BT initialization
4823429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
4837301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
4847301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
4857301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
4867301ad89Smatthias.ringwald         // }
4877301ad89Smatthias.ringwald         // handle normal init sequence
4883429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
4893429f56bSmatthias.ringwald             // odd: waiting for event
4903429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
4913429f56bSmatthias.ringwald                 hci_stack.substate++;
4923429f56bSmatthias.ringwald             }
4933429f56bSmatthias.ringwald         }
49422909952Smatthias.ringwald     }
49522909952Smatthias.ringwald 
49689db417bSmatthias.ringwald     // help with BT sleep
49789db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
49889db417bSmatthias.ringwald         && hci_stack.substate == 1
49989db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
50089db417bSmatthias.ringwald         hci_stack.substate++;
50189db417bSmatthias.ringwald     }
50289db417bSmatthias.ringwald 
5032718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
50494ab26f8Smatthias.ringwald 
50594ab26f8Smatthias.ringwald 	// execute main loop
50694ab26f8Smatthias.ringwald 	hci_run();
50716833f0aSmatthias.ringwald }
50816833f0aSmatthias.ringwald 
50910e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
51010e830c9Smatthias.ringwald     switch (packet_type) {
51110e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
51210e830c9Smatthias.ringwald             event_handler(packet, size);
51310e830c9Smatthias.ringwald             break;
51410e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
51510e830c9Smatthias.ringwald             acl_handler(packet, size);
51610e830c9Smatthias.ringwald             break;
51710e830c9Smatthias.ringwald         default:
51810e830c9Smatthias.ringwald             break;
51910e830c9Smatthias.ringwald     }
52010e830c9Smatthias.ringwald }
52110e830c9Smatthias.ringwald 
522fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5232718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5242718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
52516833f0aSmatthias.ringwald }
52616833f0aSmatthias.ringwald 
527404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
528475c8125Smatthias.ringwald 
529475c8125Smatthias.ringwald     // reference to use transport layer implementation
53016833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
531475c8125Smatthias.ringwald 
53211e23e5fSmatthias.ringwald     // references to used control implementation
53311e23e5fSmatthias.ringwald     hci_stack.control = control;
53411e23e5fSmatthias.ringwald 
53511e23e5fSmatthias.ringwald     // reference to used config
53611e23e5fSmatthias.ringwald     hci_stack.config = config;
53711e23e5fSmatthias.ringwald 
538fe1ed1b8Smatthias.ringwald     // no connections yet
539fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
540*0a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
541fe1ed1b8Smatthias.ringwald 
54202ea9861Smatthias.ringwald     // empty cmd buffer
54316833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
54416833f0aSmatthias.ringwald 
54516833f0aSmatthias.ringwald     // higher level handler
5462718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
54716833f0aSmatthias.ringwald 
548404843c1Smatthias.ringwald     // store and open remote device db
549404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
550404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
551404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
552404843c1Smatthias.ringwald     }
55329d53098Smatthias.ringwald 
55416833f0aSmatthias.ringwald     // register packet handlers with transport
55510e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
556475c8125Smatthias.ringwald }
557475c8125Smatthias.ringwald 
558404843c1Smatthias.ringwald void hci_close(){
559404843c1Smatthias.ringwald     // close remote device db
560404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
561404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
562404843c1Smatthias.ringwald     }
563404843c1Smatthias.ringwald }
564404843c1Smatthias.ringwald 
5658d213e1aSmatthias.ringwald // State-Module-Driver overview
5668d213e1aSmatthias.ringwald // state                    module  low-level
5678d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
5688d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
5698d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
5708d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
571d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
572d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
573c7e0c5f6Smatthias.ringwald 
5748d213e1aSmatthias.ringwald static int hci_power_control_on(){
5757301ad89Smatthias.ringwald 
576038bc64cSmatthias.ringwald     // power on
577f9a30166Smatthias.ringwald     int err = 0;
578f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
579f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
580f9a30166Smatthias.ringwald     }
581038bc64cSmatthias.ringwald     if (err){
5827f2435e6Smatthias.ringwald         log_err( "POWER_ON failed\n");
583038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
584038bc64cSmatthias.ringwald         return err;
585038bc64cSmatthias.ringwald     }
586038bc64cSmatthias.ringwald 
587038bc64cSmatthias.ringwald     // open low-level device
588038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
589038bc64cSmatthias.ringwald     if (err){
5907f2435e6Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
591f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
592f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
593f9a30166Smatthias.ringwald         }
594038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
595038bc64cSmatthias.ringwald         return err;
596038bc64cSmatthias.ringwald     }
5978d213e1aSmatthias.ringwald     return 0;
5988d213e1aSmatthias.ringwald }
599038bc64cSmatthias.ringwald 
6008d213e1aSmatthias.ringwald static void hci_power_control_off(){
6018d213e1aSmatthias.ringwald 
6028d213e1aSmatthias.ringwald     // close low-level device
6038d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6048d213e1aSmatthias.ringwald 
6058d213e1aSmatthias.ringwald     // power off
6068d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6078d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6088d213e1aSmatthias.ringwald     }
60972ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
61072ea5239Smatthias.ringwald }
61172ea5239Smatthias.ringwald 
61272ea5239Smatthias.ringwald static void hci_power_control_sleep(){
61372ea5239Smatthias.ringwald 
6143144bce4Smatthias.ringwald     log_dbg("hci_power_control_sleep");
6153144bce4Smatthias.ringwald 
616b429b9b7Smatthias.ringwald #if 0
617b429b9b7Smatthias.ringwald     // don't close serial port during sleep
618b429b9b7Smatthias.ringwald 
61972ea5239Smatthias.ringwald     // close low-level device
62072ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
621b429b9b7Smatthias.ringwald #endif
62272ea5239Smatthias.ringwald 
62372ea5239Smatthias.ringwald     // sleep mode
6243144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
62572ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
62672ea5239Smatthias.ringwald     }
627b429b9b7Smatthias.ringwald 
62872ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6298d213e1aSmatthias.ringwald }
6308d213e1aSmatthias.ringwald 
631b429b9b7Smatthias.ringwald static int hci_power_control_wake(){
632b429b9b7Smatthias.ringwald 
633b429b9b7Smatthias.ringwald     log_dbg("hci_power_control_wake");
634b429b9b7Smatthias.ringwald 
635b429b9b7Smatthias.ringwald     // wake on
636b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
637b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
638b429b9b7Smatthias.ringwald     }
639b429b9b7Smatthias.ringwald 
640b429b9b7Smatthias.ringwald #if 0
641b429b9b7Smatthias.ringwald     // open low-level device
642b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
643b429b9b7Smatthias.ringwald     if (err){
644b429b9b7Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
645b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
646b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
647b429b9b7Smatthias.ringwald         }
648b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
649b429b9b7Smatthias.ringwald         return err;
650b429b9b7Smatthias.ringwald     }
651b429b9b7Smatthias.ringwald #endif
652b429b9b7Smatthias.ringwald 
653b429b9b7Smatthias.ringwald     return 0;
654b429b9b7Smatthias.ringwald }
655b429b9b7Smatthias.ringwald 
656b429b9b7Smatthias.ringwald 
6578d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
658d661ed19Smatthias.ringwald 
6597ec5eeaaSmatthias.ringwald     log_dbg("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
660d661ed19Smatthias.ringwald 
6618d213e1aSmatthias.ringwald     int err = 0;
6628d213e1aSmatthias.ringwald     switch (hci_stack.state){
6638d213e1aSmatthias.ringwald 
6648d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
6658d213e1aSmatthias.ringwald             switch (power_mode){
6668d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
6678d213e1aSmatthias.ringwald                     err = hci_power_control_on();
6688d213e1aSmatthias.ringwald                     if (err) return err;
6697301ad89Smatthias.ringwald                     // set up state machine
6707301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
6717301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
6727301ad89Smatthias.ringwald                     hci_stack.substate = 0;
6738d213e1aSmatthias.ringwald                     break;
6748d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
6758d213e1aSmatthias.ringwald                     // do nothing
6768d213e1aSmatthias.ringwald                     break;
6778d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
678b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
6798d213e1aSmatthias.ringwald                     break;
6808d213e1aSmatthias.ringwald             }
6818d213e1aSmatthias.ringwald             break;
6827301ad89Smatthias.ringwald 
6838d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
6848d213e1aSmatthias.ringwald             switch (power_mode){
6858d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
6868d213e1aSmatthias.ringwald                     // do nothing
6878d213e1aSmatthias.ringwald                     break;
6888d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
6898d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
6908d213e1aSmatthias.ringwald                     hci_power_control_off();
6918d213e1aSmatthias.ringwald                     break;
6928d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
693b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
69472ea5239Smatthias.ringwald                     hci_power_control_sleep();
6958d213e1aSmatthias.ringwald                     break;
6968d213e1aSmatthias.ringwald             }
6978d213e1aSmatthias.ringwald             break;
6987301ad89Smatthias.ringwald 
6998d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7008d213e1aSmatthias.ringwald             switch (power_mode){
7018d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7028d213e1aSmatthias.ringwald                     // do nothing
7038d213e1aSmatthias.ringwald                     break;
7048d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
705c7e0c5f6Smatthias.ringwald                     // see hci_run
706c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7078d213e1aSmatthias.ringwald                     break;
7088d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
709b546ac54Smatthias.ringwald                     // see hci_run
710b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
71189db417bSmatthias.ringwald                     hci_stack.substate = 0;
7128d213e1aSmatthias.ringwald                     break;
7138d213e1aSmatthias.ringwald             }
7148d213e1aSmatthias.ringwald             break;
7157301ad89Smatthias.ringwald 
7168d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7178d213e1aSmatthias.ringwald             switch (power_mode){
7188d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7198d213e1aSmatthias.ringwald                     // set up state machine
7208d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7218d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7228d213e1aSmatthias.ringwald                     break;
7238d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7248d213e1aSmatthias.ringwald                     // do nothing
7258d213e1aSmatthias.ringwald                     break;
7268d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
727b546ac54Smatthias.ringwald                     // see hci_run
728b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
72989db417bSmatthias.ringwald                     hci_stack.substate = 0;
7308d213e1aSmatthias.ringwald                     break;
7318d213e1aSmatthias.ringwald             }
7328d213e1aSmatthias.ringwald             break;
7338d213e1aSmatthias.ringwald 
7348d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7358d213e1aSmatthias.ringwald             switch (power_mode){
7368d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
737b546ac54Smatthias.ringwald                     // set up state machine
738b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
739b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
740b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7418d213e1aSmatthias.ringwald                     break;
7428d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
743b546ac54Smatthias.ringwald                     // see hci_run
744b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7458d213e1aSmatthias.ringwald                     break;
7468d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
747b546ac54Smatthias.ringwald                     // do nothing
7488d213e1aSmatthias.ringwald                     break;
7498d213e1aSmatthias.ringwald             }
7508d213e1aSmatthias.ringwald             break;
7518d213e1aSmatthias.ringwald 
7528d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
7538d213e1aSmatthias.ringwald             switch (power_mode){
7548d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7553144bce4Smatthias.ringwald                     err = hci_power_control_wake();
7563144bce4Smatthias.ringwald                     if (err) return err;
757b546ac54Smatthias.ringwald                     // set up state machine
758b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
759b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
760b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7618d213e1aSmatthias.ringwald                     break;
7628d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7634ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7648d213e1aSmatthias.ringwald                     break;
7658d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
766b546ac54Smatthias.ringwald                     // do nothing
7678d213e1aSmatthias.ringwald                     break;
7688d213e1aSmatthias.ringwald             }
7698d213e1aSmatthias.ringwald             break;
77011e23e5fSmatthias.ringwald     }
77168d92d03Smatthias.ringwald 
772038bc64cSmatthias.ringwald     // create internal event
773ee8bf225Smatthias.ringwald 	hci_emit_state();
774ee8bf225Smatthias.ringwald 
77568d92d03Smatthias.ringwald 	// trigger next/first action
77668d92d03Smatthias.ringwald 	hci_run();
77768d92d03Smatthias.ringwald 
778475c8125Smatthias.ringwald     return 0;
779475c8125Smatthias.ringwald }
780475c8125Smatthias.ringwald 
781381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
782381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
783381fbed8Smatthias.ringwald 
784381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
785381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
786381fbed8Smatthias.ringwald         return;
787381fbed8Smatthias.ringwald     }
788381fbed8Smatthias.ringwald 
789381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
790381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
791381fbed8Smatthias.ringwald }
792381fbed8Smatthias.ringwald 
79306b35ec0Smatthias.ringwald void hci_run(){
7948a485f27Smatthias.ringwald 
795c7e0c5f6Smatthias.ringwald     if (hci_stack.num_cmd_packets == 0) {
796c7e0c5f6Smatthias.ringwald         // cannot send command yet
797c7e0c5f6Smatthias.ringwald         return;
798c7e0c5f6Smatthias.ringwald     }
799c7e0c5f6Smatthias.ringwald 
800c7e0c5f6Smatthias.ringwald     hci_connection_t * connection;
801c7e0c5f6Smatthias.ringwald 
8023429f56bSmatthias.ringwald     switch (hci_stack.state){
8033429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8043429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
8053429f56bSmatthias.ringwald                 // odd: waiting for command completion
80606b35ec0Smatthias.ringwald                 return;
8073429f56bSmatthias.ringwald             }
80890919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
8093429f56bSmatthias.ringwald                 case 0:
81022909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
8113429f56bSmatthias.ringwald                     break;
8123429f56bSmatthias.ringwald                 case 1:
81390919203Smatthias.ringwald                     // custom initialization
81490919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
81590919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
81690919203Smatthias.ringwald                         if (cmd) {
81790919203Smatthias.ringwald                             int size = 3 + cmd[2];
818622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
8192f6c30e1Smatthias.ringwald                             hci_stack.substate = 0; // more init commands
82090919203Smatthias.ringwald                             break;
82190919203Smatthias.ringwald                         }
82290919203Smatthias.ringwald                     }
8232f6c30e1Smatthias.ringwald                     // otherwise continue
824f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
825f432a6ddSmatthias.ringwald 					break;
8262f6c30e1Smatthias.ringwald 				case 2:
827e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
828e2edc0c3Smatthias.ringwald 					break;
8292f6c30e1Smatthias.ringwald                 case 3:
8303429f56bSmatthias.ringwald                     // ca. 15 sec
8313429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
8323429f56bSmatthias.ringwald                     break;
8332f6c30e1Smatthias.ringwald 				case 4:
834*0a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
835f432a6ddSmatthias.ringwald 					break;
8362f6c30e1Smatthias.ringwald                 case 5:
8378a485f27Smatthias.ringwald #ifndef EMBEDDED
8388a485f27Smatthias.ringwald                 {
8398a485f27Smatthias.ringwald                     char hostname[30];
8408a485f27Smatthias.ringwald                     gethostname(hostname, 30);
8418a485f27Smatthias.ringwald                     hostname[29] = '\0';
8428a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
8438a485f27Smatthias.ringwald                     break;
8448a485f27Smatthias.ringwald                 }
8458a485f27Smatthias.ringwald                 case 6:
8468a485f27Smatthias.ringwald #endif
8473429f56bSmatthias.ringwald                     // done.
8483429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
849b360b6adSmatthias.ringwald                     hci_emit_state();
8503429f56bSmatthias.ringwald                     break;
8513429f56bSmatthias.ringwald                 default:
8523429f56bSmatthias.ringwald                     break;
853475c8125Smatthias.ringwald             }
8543429f56bSmatthias.ringwald             hci_stack.substate++;
8553429f56bSmatthias.ringwald             break;
856c7e0c5f6Smatthias.ringwald 
857c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
858c7e0c5f6Smatthias.ringwald 
8593144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING\n");
860c7e0c5f6Smatthias.ringwald             // close all open connections
861c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
862c7e0c5f6Smatthias.ringwald             if (connection){
8633144bce4Smatthias.ringwald                 log_dbg("HCI_STATE_HALTING, connection %u, handle %u\n", (int) connection, connection->con_handle);
864c7e0c5f6Smatthias.ringwald                 // send disconnect
8656ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
866c7e0c5f6Smatthias.ringwald 
867c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
868c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
869c7e0c5f6Smatthias.ringwald 
870c7e0c5f6Smatthias.ringwald                 // remove from table
871c7e0c5f6Smatthias.ringwald                 hci_stack.connections = connection->item.next;
872c7e0c5f6Smatthias.ringwald                 return;
873c7e0c5f6Smatthias.ringwald             }
8743144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, calling off\n");
875c7e0c5f6Smatthias.ringwald 
87672ea5239Smatthias.ringwald             // switch mode
877c7e0c5f6Smatthias.ringwald             hci_power_control_off();
87872ea5239Smatthias.ringwald             hci_emit_state();
8793144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, done\n");
88072ea5239Smatthias.ringwald             break;
881c7e0c5f6Smatthias.ringwald 
88272ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
88389db417bSmatthias.ringwald             switch(hci_stack.substate) {
88489db417bSmatthias.ringwald                 case 0:
88589db417bSmatthias.ringwald                     log_dbg("HCI_STATE_FALLING_ASLEEP\n");
88672ea5239Smatthias.ringwald                     // close all open connections
88772ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
88872ea5239Smatthias.ringwald                     if (connection){
88989db417bSmatthias.ringwald                         log_dbg("HCI_STATE_FALLING_ASLEEP, connection %u, handle %u\n", (int) connection, connection->con_handle);
89072ea5239Smatthias.ringwald                         // send disconnect
8916ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
89272ea5239Smatthias.ringwald 
89372ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
89472ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
89572ea5239Smatthias.ringwald 
89672ea5239Smatthias.ringwald                         // remove from table
89772ea5239Smatthias.ringwald                         hci_stack.connections = connection->item.next;
89872ea5239Smatthias.ringwald                         return;
89972ea5239Smatthias.ringwald                     }
90072ea5239Smatthias.ringwald 
90189db417bSmatthias.ringwald                     log_dbg("HCI_STATE_HALTING, disabling inq & page scans\n");
90289db417bSmatthias.ringwald 
90389db417bSmatthias.ringwald                     // disable page and inquiry scan
90489db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
90589db417bSmatthias.ringwald 
90689db417bSmatthias.ringwald                     // continue in next sub state
90789db417bSmatthias.ringwald                     hci_stack.substate++;
90889db417bSmatthias.ringwald                     break;
90989db417bSmatthias.ringwald                 case 1:
91089db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
91189db417bSmatthias.ringwald                     break;
91289db417bSmatthias.ringwald                 case 2:
91389db417bSmatthias.ringwald                     log_dbg("HCI_STATE_HALTING, calling sleep\n");
91472ea5239Smatthias.ringwald                     // switch mode
91589db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
916c7e0c5f6Smatthias.ringwald                     hci_emit_state();
91789db417bSmatthias.ringwald                 default:
91889db417bSmatthias.ringwald                     break;
91989db417bSmatthias.ringwald             }
920c7e0c5f6Smatthias.ringwald             break;
921c7e0c5f6Smatthias.ringwald 
9223429f56bSmatthias.ringwald         default:
9233429f56bSmatthias.ringwald             break;
9241f504dbdSmatthias.ringwald     }
9253429f56bSmatthias.ringwald }
92616833f0aSmatthias.ringwald 
92731452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
928c8e4258aSmatthias.ringwald     bd_addr_t addr;
929c8e4258aSmatthias.ringwald     hci_connection_t * conn;
930c8e4258aSmatthias.ringwald     // house-keeping
931c8e4258aSmatthias.ringwald 
932c8e4258aSmatthias.ringwald     // create_connection?
933c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
934c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
935cf0b66f0Smatthias.ringwald         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
936c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
937c8e4258aSmatthias.ringwald         if (conn) {
938c8e4258aSmatthias.ringwald             // if connection exists
939c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
940c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
941c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
942c8e4258aSmatthias.ringwald             }
943c8e4258aSmatthias.ringwald             //    otherwise, just ignore
944c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
945c8e4258aSmatthias.ringwald 
946c8e4258aSmatthias.ringwald         } else{
947c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
948c8e4258aSmatthias.ringwald             if (conn){
949c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
950c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
951c8e4258aSmatthias.ringwald             }
952c8e4258aSmatthias.ringwald         }
953c8e4258aSmatthias.ringwald     }
954c8e4258aSmatthias.ringwald 
9557fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
9567fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
9577fde4af9Smatthias.ringwald     }
9587fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
9597fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
9607fde4af9Smatthias.ringwald     }
9617fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
9627fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
9637fde4af9Smatthias.ringwald     }
9647fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
9657fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
9667fde4af9Smatthias.ringwald     }
9677fde4af9Smatthias.ringwald 
9688ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
9698ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
9708ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
9718ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
9728ef73945Smatthias.ringwald         }
9738ef73945Smatthias.ringwald     }
974c8e4258aSmatthias.ringwald 
97531452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
976622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
97731452debSmatthias.ringwald }
9788adf0ddaSmatthias.ringwald 
9791cd208adSmatthias.ringwald /**
9801cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
9811cd208adSmatthias.ringwald  */
982fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
9831cd208adSmatthias.ringwald     va_list argptr;
9841cd208adSmatthias.ringwald     va_start(argptr, cmd);
9851cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
9861cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
9871cd208adSmatthias.ringwald     va_end(argptr);
9881cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
98993b8dc03Smatthias.ringwald }
990c8e4258aSmatthias.ringwald 
991ee091cf1Smatthias.ringwald // Create various non-HCI events.
992ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
993ee091cf1Smatthias.ringwald 
994c8e4258aSmatthias.ringwald void hci_emit_state(){
995c8e4258aSmatthias.ringwald     uint8_t len = 3;
996c8e4258aSmatthias.ringwald     uint8_t event[len];
99780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
9981b0e3922Smatthias.ringwald     event[1] = len - 3;
999c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1000c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10012718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1002c8e4258aSmatthias.ringwald }
1003c8e4258aSmatthias.ringwald 
1004c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
1005c8e4258aSmatthias.ringwald     uint8_t len = 13;
1006c8e4258aSmatthias.ringwald     uint8_t event[len];
1007c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
10081b0e3922Smatthias.ringwald     event[1] = len - 3;
1009c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
1010c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1011c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1012c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1013c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1014c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10152718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1016c8e4258aSmatthias.ringwald }
1017c8e4258aSmatthias.ringwald 
1018ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1019ee091cf1Smatthias.ringwald     uint8_t len = 4;
1020ee091cf1Smatthias.ringwald     uint8_t event[len];
102180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
10221b0e3922Smatthias.ringwald     event[1] = len - 2;
1023ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1024ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10252718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1026ee091cf1Smatthias.ringwald }
102743bfb1bdSmatthias.ringwald 
102843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
102943bfb1bdSmatthias.ringwald     uint8_t len = 3;
103043bfb1bdSmatthias.ringwald     uint8_t event[len];
103180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
10321b0e3922Smatthias.ringwald     event[1] = len - 2;
103343bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
103443bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10352718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
103643bfb1bdSmatthias.ringwald }
1037038bc64cSmatthias.ringwald 
1038038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
10391b0e3922Smatthias.ringwald     uint8_t len = 2;
1040038bc64cSmatthias.ringwald     uint8_t event[len];
104180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
10421b0e3922Smatthias.ringwald     event[1] = len - 2;
1043038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10442718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1045038bc64cSmatthias.ringwald }
10461b0e3922Smatthias.ringwald 
10471b0e3922Smatthias.ringwald 
10481b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
10491b0e3922Smatthias.ringwald     uint8_t len = 6;
10501b0e3922Smatthias.ringwald     uint8_t event[len];
10511b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
10521b0e3922Smatthias.ringwald     event[1] = len - 2;
10531b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
10541b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
10551b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
10561b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10572718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
10581b0e3922Smatthias.ringwald }
10591b0e3922Smatthias.ringwald 
10602ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
10612ed6235cSmatthias.ringwald     uint8_t len = 3;
10622ed6235cSmatthias.ringwald     uint8_t event[len];
10632ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1064ddce1d8dSmatthias.ringwald     event[1] = len - 2;
10652ed6235cSmatthias.ringwald     event[2] = enabled;
10662ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10672718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
10682ed6235cSmatthias.ringwald }
1069627c2f45Smatthias.ringwald 
1070627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1071f653b6bdSmatthias.ringwald     uint16_t len = 2+1+6+248;
1072627c2f45Smatthias.ringwald     uint8_t event[len];
1073627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1074627c2f45Smatthias.ringwald     event[1] = len - 2;
1075f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
10762f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1077f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1078627c2f45Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, len);
1079627c2f45Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1080627c2f45Smatthias.ringwald }
1081381fbed8Smatthias.ringwald 
1082381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1083381fbed8Smatthias.ringwald     uint8_t len = 3;
1084381fbed8Smatthias.ringwald     uint8_t event[len];
1085381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1086381fbed8Smatthias.ringwald     event[1] = len - 2;
1087381fbed8Smatthias.ringwald     event[2] = enabled;
1088381fbed8Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
1089381fbed8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1090381fbed8Smatthias.ringwald }
1091