xref: /btstack/src/hci.c (revision 1d279b206496c8979a17d1ddcfa0a3a5385c73d3)
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 
4193b8dc03Smatthias.ringwald #include <stdarg.h>
4293b8dc03Smatthias.ringwald #include <string.h>
4356fe0872Smatthias.ringwald #include <stdio.h>
447f2435e6Smatthias.ringwald 
45549e6ebeSmatthias.ringwald #ifndef EMBEDDED
46549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
47549e6ebeSmatthias.ringwald #endif
48549e6ebeSmatthias.ringwald 
497f2435e6Smatthias.ringwald #include "debug.h"
50d8905019Smatthias.ringwald #include "hci_dump.h"
5193b8dc03Smatthias.ringwald 
522ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h"
532ed6235cSmatthias.ringwald #include "../include/btstack/version.h"
541b0e3922Smatthias.ringwald 
551e6aba47Smatthias.ringwald // temp
561e6aba47Smatthias.ringwald #include "l2cap.h"
571e6aba47Smatthias.ringwald 
58169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
59ee091cf1Smatthias.ringwald 
6006b35ec0Smatthias.ringwald // the STACK is here
6116833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
6216833f0aSmatthias.ringwald 
6397addcc5Smatthias.ringwald /**
64ee091cf1Smatthias.ringwald  * get connection for a given handle
65ee091cf1Smatthias.ringwald  *
66ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
67ee091cf1Smatthias.ringwald  */
68645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
69ee091cf1Smatthias.ringwald     linked_item_t *it;
70ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
71ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
72ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
73ee091cf1Smatthias.ringwald         }
74ee091cf1Smatthias.ringwald     }
75ee091cf1Smatthias.ringwald     return NULL;
76ee091cf1Smatthias.ringwald }
77ee091cf1Smatthias.ringwald 
78981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
79c7492964Smatthias.ringwald #ifdef HAVE_TIME
80ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
81ee091cf1Smatthias.ringwald     struct timeval tv;
82ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
83c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
84ee091cf1Smatthias.ringwald         // connections might be timed out
85ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
86c21e6239Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
87ee091cf1Smatthias.ringwald     } else {
88ee091cf1Smatthias.ringwald         // next timeout check at
89c21e6239Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
90ee091cf1Smatthias.ringwald     }
91ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
92c7492964Smatthias.ringwald #endif
93ee091cf1Smatthias.ringwald }
94ee091cf1Smatthias.ringwald 
95ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
96c7492964Smatthias.ringwald #ifdef HAVE_TIME
97ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
98c7492964Smatthias.ringwald #endif
99ee091cf1Smatthias.ringwald }
100ee091cf1Smatthias.ringwald 
101ee091cf1Smatthias.ringwald /**
102c8e4258aSmatthias.ringwald  * create connection for given address
103c8e4258aSmatthias.ringwald  *
104c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
105c8e4258aSmatthias.ringwald  */
106c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
107c8e4258aSmatthias.ringwald     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
108c8e4258aSmatthias.ringwald     if (!conn) return NULL;
109c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
110c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1117fde4af9Smatthias.ringwald     conn->authentication_flags = 0;
112c7492964Smatthias.ringwald #ifdef HAVE_TIME
113ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
114ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
115ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
116c7492964Smatthias.ringwald #endif
117d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1187856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
11956cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
120c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
121c8e4258aSmatthias.ringwald     return conn;
122c8e4258aSmatthias.ringwald }
123c8e4258aSmatthias.ringwald 
124c8e4258aSmatthias.ringwald /**
12506b35ec0Smatthias.ringwald  * get connection for given address
12697addcc5Smatthias.ringwald  *
12797addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
12897addcc5Smatthias.ringwald  */
129fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
13006b35ec0Smatthias.ringwald     linked_item_t *it;
13106b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
13206b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
13306b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
13406b35ec0Smatthias.ringwald         }
13506b35ec0Smatthias.ringwald     }
13606b35ec0Smatthias.ringwald     return NULL;
13706b35ec0Smatthias.ringwald }
13806b35ec0Smatthias.ringwald 
13943bfb1bdSmatthias.ringwald /**
14080ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1417fde4af9Smatthias.ringwald  */
1427fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1437fde4af9Smatthias.ringwald     bd_addr_t addr;
1447fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1457fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1467fde4af9Smatthias.ringwald     if (conn) {
1477fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
14880ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1497fde4af9Smatthias.ringwald     }
1507fde4af9Smatthias.ringwald }
1517fde4af9Smatthias.ringwald 
15280ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
15380ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
15480ca58a0Smatthias.ringwald     if (!conn) return 0;
15580ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
15680ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
15780ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
15880ca58a0Smatthias.ringwald     return 1;
15980ca58a0Smatthias.ringwald }
16080ca58a0Smatthias.ringwald 
161c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
162c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
163c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
164c12e46e7Smatthias.ringwald     }
165c12e46e7Smatthias.ringwald }
166c12e46e7Smatthias.ringwald 
1677fde4af9Smatthias.ringwald 
1687fde4af9Smatthias.ringwald /**
16943bfb1bdSmatthias.ringwald  * count connections
17043bfb1bdSmatthias.ringwald  */
17140d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
17256c253c9Smatthias.ringwald     int count = 0;
17343bfb1bdSmatthias.ringwald     linked_item_t *it;
17443bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
17543bfb1bdSmatthias.ringwald     return count;
17643bfb1bdSmatthias.ringwald }
177c8e4258aSmatthias.ringwald 
17897addcc5Smatthias.ringwald /**
179ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
18016833f0aSmatthias.ringwald  */
1812718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
18216833f0aSmatthias.ringwald }
18316833f0aSmatthias.ringwald 
184998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
185998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
186998906cdSmatthias.ringwald     if (!connection) {
187998906cdSmatthias.ringwald         log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
188998906cdSmatthias.ringwald         return 0;
189998906cdSmatthias.ringwald     }
190998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
191998906cdSmatthias.ringwald }
192998906cdSmatthias.ringwald 
193998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
194998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
195998906cdSmatthias.ringwald     linked_item_t *it;
196998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
197998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
198998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
199998906cdSmatthias.ringwald             log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
200998906cdSmatthias.ringwald             return 0;
201998906cdSmatthias.ringwald         }
202998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
203998906cdSmatthias.ringwald     }
204998906cdSmatthias.ringwald     return free_slots;
205998906cdSmatthias.ringwald }
206998906cdSmatthias.ringwald 
2075250fb9eSmatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
2085250fb9eSmatthias.ringwald     return hci_stack.acl_data_packet_length;
2095250fb9eSmatthias.ringwald }
2105250fb9eSmatthias.ringwald 
211998906cdSmatthias.ringwald int hci_ready_to_send(hci_con_handle_t handle){
212998906cdSmatthias.ringwald     return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2;
213998906cdSmatthias.ringwald }
214c8e4258aSmatthias.ringwald 
215ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2167856c818Smatthias.ringwald 
2176218e6f1Smatthias.ringwald     // check for free places on BT module
2186218e6f1Smatthias.ringwald     if (!hci_number_free_acl_slots()) return -1;
2196218e6f1Smatthias.ringwald 
2207856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2217856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
22256cf178bSmatthias.ringwald     if (!connection) return 0;
22356cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
22456cf178bSmatthias.ringwald 
22556cf178bSmatthias.ringwald     // count packet
22656cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2278ea03fa5Smatthias.ringwald     // log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2287856c818Smatthias.ringwald 
2296218e6f1Smatthias.ringwald     // send packet - ignore errors
230622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2316218e6f1Smatthias.ringwald 
2326218e6f1Smatthias.ringwald     return 0;
233ee091cf1Smatthias.ringwald }
234ee091cf1Smatthias.ringwald 
23516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2367856c818Smatthias.ringwald 
2377856c818Smatthias.ringwald     // get info
2387856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2397856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2407856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2417856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2427856c818Smatthias.ringwald 
2437856c818Smatthias.ringwald     // ignore non-registered handle
2447856c818Smatthias.ringwald     if (!conn){
2457f2435e6Smatthias.ringwald         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2467856c818Smatthias.ringwald         return;
2477856c818Smatthias.ringwald     }
2487856c818Smatthias.ringwald 
2497856c818Smatthias.ringwald     // update idle timestamp
2507856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2517856c818Smatthias.ringwald 
2527856c818Smatthias.ringwald     // handle different packet types
2537856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2547856c818Smatthias.ringwald 
2557856c818Smatthias.ringwald         case 0x01: // continuation fragment
2567856c818Smatthias.ringwald 
2577856c818Smatthias.ringwald             // sanity check
2587856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2597f2435e6Smatthias.ringwald                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2607856c818Smatthias.ringwald                 return;
2617856c818Smatthias.ringwald             }
2627856c818Smatthias.ringwald 
2637856c818Smatthias.ringwald             // append fragment payload (header already stored)
2647856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2657856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2667856c818Smatthias.ringwald 
2677f2435e6Smatthias.ringwald             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
2687856c818Smatthias.ringwald             //        acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
2697856c818Smatthias.ringwald 
2707856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2717856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2727856c818Smatthias.ringwald 
2732718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2747856c818Smatthias.ringwald                 // reset recombination buffer
2757856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2767856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2777856c818Smatthias.ringwald             }
2787856c818Smatthias.ringwald             break;
2797856c818Smatthias.ringwald 
2807856c818Smatthias.ringwald         case 0x02: { // first fragment
2817856c818Smatthias.ringwald 
2827856c818Smatthias.ringwald             // sanity check
2837856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
2847f2435e6Smatthias.ringwald                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
2857856c818Smatthias.ringwald                 return;
2867856c818Smatthias.ringwald             }
2877856c818Smatthias.ringwald 
2887856c818Smatthias.ringwald             // peek into L2CAP packet!
2897856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
2907856c818Smatthias.ringwald 
2917856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
2927856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
2937856c818Smatthias.ringwald 
2947856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
2952718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
2967856c818Smatthias.ringwald 
2977856c818Smatthias.ringwald             } else {
2987856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
2997856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3007856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3017856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
3027856c818Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
3037f2435e6Smatthias.ringwald                 // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
3047856c818Smatthias.ringwald             }
3057856c818Smatthias.ringwald             break;
3067856c818Smatthias.ringwald 
3077856c818Smatthias.ringwald         }
3087856c818Smatthias.ringwald         default:
3097f2435e6Smatthias.ringwald             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3107856c818Smatthias.ringwald             return;
3117856c818Smatthias.ringwald     }
31294ab26f8Smatthias.ringwald 
31394ab26f8Smatthias.ringwald     // execute main loop
31494ab26f8Smatthias.ringwald     hci_run();
31516833f0aSmatthias.ringwald }
31622909952Smatthias.ringwald 
31767a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
318c7e0c5f6Smatthias.ringwald     log_dbg("Connection closed: handle %u, ", conn->con_handle);
319c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
320c7e0c5f6Smatthias.ringwald     log_dbg("\n");
3213c4d4b90Smatthias.ringwald 
3223c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3233c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3243c4d4b90Smatthias.ringwald 
325c7492964Smatthias.ringwald #ifdef HAVE_TIME
326c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
327c7492964Smatthias.ringwald #endif
328c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
329c7e0c5f6Smatthias.ringwald     free( conn );
3303c4d4b90Smatthias.ringwald 
3313c4d4b90Smatthias.ringwald     // now it's gone
332c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
333c7e0c5f6Smatthias.ringwald }
334c7e0c5f6Smatthias.ringwald 
33574ec757aSmatthias.ringwald // avoid huge local variables
33674ec757aSmatthias.ringwald static device_name_t device_name;
33716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3381281a47eSmatthias.ringwald     bd_addr_t addr;
339fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3401f7b95a1Smatthias.ringwald     hci_connection_t * conn;
34156cf178bSmatthias.ringwald     int i;
34229d53098Smatthias.ringwald     link_key_t link_key;
34322909952Smatthias.ringwald 
3446772a24cSmatthias.ringwald     switch (packet[0]) {
34522909952Smatthias.ringwald 
3466772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3477ec5eeaaSmatthias.ringwald             // get num cmd packets
3487ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3497ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3507ec5eeaaSmatthias.ringwald 
351e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
352e2edc0c3Smatthias.ringwald                 // from offset 5
353e2edc0c3Smatthias.ringwald                 // status
354*1d279b20Smatthias.ringwald                 // "The HC_ACL_Data_Packet_Length return parameter will be used to determine the size of the L2CAP segments contained in ACL Data Packets"
355e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
35656cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
357e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
35856cf178bSmatthias.ringwald                 // ignore: total num SCO packets
35956cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
360e2edc0c3Smatthias.ringwald                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
361e2edc0c3Smatthias.ringwald                 }
36256cf178bSmatthias.ringwald             }
363381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
364381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
365381fbed8Smatthias.ringwald             }
36656cf178bSmatthias.ringwald             break;
36756cf178bSmatthias.ringwald 
3687ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
3697ec5eeaaSmatthias.ringwald             // get num cmd packets
3707ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
3717ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
3727ec5eeaaSmatthias.ringwald             break;
3737ec5eeaaSmatthias.ringwald 
37456cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
37556cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
37656cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
37756cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
37856cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
37956cf178bSmatthias.ringwald                 if (!conn){
38056cf178bSmatthias.ringwald                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
38156cf178bSmatthias.ringwald                     continue;
38256cf178bSmatthias.ringwald                 }
38356cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
3848ea03fa5Smatthias.ringwald                 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
38556cf178bSmatthias.ringwald             }
3866772a24cSmatthias.ringwald             break;
3876772a24cSmatthias.ringwald 
3881f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
38937eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
39037eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
39137eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
392cf0b66f0Smatthias.ringwald             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
39337eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
3941f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
3951f7b95a1Smatthias.ringwald                 if (!conn) {
3961f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
3971f7b95a1Smatthias.ringwald                 }
3981f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
3991f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
40037eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
40137eaa4cfSmatthias.ringwald             } else {
40237eaa4cfSmatthias.ringwald                 // TODO: decline request
40337eaa4cfSmatthias.ringwald             }
4041f7b95a1Smatthias.ringwald             break;
4051f7b95a1Smatthias.ringwald 
4066772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
407fe1ed1b8Smatthias.ringwald             // Connection management
408fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
409cf0b66f0Smatthias.ringwald             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
4101f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
411fe1ed1b8Smatthias.ringwald             if (conn) {
412b448a0e7Smatthias.ringwald                 if (!packet[2]){
413c8e4258aSmatthias.ringwald                     conn->state = OPEN;
414fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
415ee091cf1Smatthias.ringwald 
416c7492964Smatthias.ringwald #ifdef HAVE_TIME
417ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
418c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
419ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
420c7492964Smatthias.ringwald #endif
421cf0b66f0Smatthias.ringwald                     log_dbg("New connection: handle %u, ", conn->con_handle);
422fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
423cf0b66f0Smatthias.ringwald                     log_dbg("\n");
42443bfb1bdSmatthias.ringwald 
42543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
426b448a0e7Smatthias.ringwald                 } else {
427b448a0e7Smatthias.ringwald                     // connection failed, remove entry
428b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
429b448a0e7Smatthias.ringwald                     free( conn );
430c12e46e7Smatthias.ringwald 
431c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
432c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
433c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
434c12e46e7Smatthias.ringwald                     }
435fe1ed1b8Smatthias.ringwald                 }
436fe1ed1b8Smatthias.ringwald             }
4376772a24cSmatthias.ringwald             break;
438fe1ed1b8Smatthias.ringwald 
4397fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
4407fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
44174ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
44229d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
44329d53098Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &addr, &link_key)){
44429d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_reply, &addr, &link_key);
44529d53098Smatthias.ringwald             } else {
44629d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
44729d53098Smatthias.ringwald             }
44829d53098Smatthias.ringwald             // request already answered
44929d53098Smatthias.ringwald             return;
4507fde4af9Smatthias.ringwald 
4517fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4527fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
45374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
45429d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
455287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
45629d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4577fde4af9Smatthias.ringwald             break;
4587fde4af9Smatthias.ringwald 
4597fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4607fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4617fde4af9Smatthias.ringwald             break;
4627fde4af9Smatthias.ringwald 
46374ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
46474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
46574ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
46674ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
46774ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
46874ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
46974ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
47074ec757aSmatthias.ringwald             break;
47174ec757aSmatthias.ringwald 
47274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
47374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
47474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
47574ec757aSmatthias.ringwald             // first send inq result packet
47674ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
47774ec757aSmatthias.ringwald             // then send cached remote names
47874ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
47974ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
48074ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
48174ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
48274ec757aSmatthias.ringwald                 }
48374ec757aSmatthias.ringwald             }
48474ec757aSmatthias.ringwald             return;
48574ec757aSmatthias.ringwald 
4866772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
487fe1ed1b8Smatthias.ringwald             if (!packet[2]){
488fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
489fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
490fe1ed1b8Smatthias.ringwald                 if (conn) {
491c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
492fe1ed1b8Smatthias.ringwald                 }
493fe1ed1b8Smatthias.ringwald             }
4946772a24cSmatthias.ringwald             break;
4956772a24cSmatthias.ringwald 
496c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
497c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
498c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
499c68bdf90Smatthias.ringwald             }
500c68bdf90Smatthias.ringwald             break;
501c68bdf90Smatthias.ringwald 
5026772a24cSmatthias.ringwald         default:
5036772a24cSmatthias.ringwald             break;
504fe1ed1b8Smatthias.ringwald     }
505fe1ed1b8Smatthias.ringwald 
5063429f56bSmatthias.ringwald     // handle BT initialization
5073429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5087301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5097301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5107301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5117301ad89Smatthias.ringwald         // }
5127301ad89Smatthias.ringwald         // handle normal init sequence
5133429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5143429f56bSmatthias.ringwald             // odd: waiting for event
5153429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5163429f56bSmatthias.ringwald                 hci_stack.substate++;
5173429f56bSmatthias.ringwald             }
5183429f56bSmatthias.ringwald         }
51922909952Smatthias.ringwald     }
52022909952Smatthias.ringwald 
52189db417bSmatthias.ringwald     // help with BT sleep
52289db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
52389db417bSmatthias.ringwald         && hci_stack.substate == 1
52489db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
52589db417bSmatthias.ringwald         hci_stack.substate++;
52689db417bSmatthias.ringwald     }
52789db417bSmatthias.ringwald 
5282718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
52994ab26f8Smatthias.ringwald 
53094ab26f8Smatthias.ringwald 	// execute main loop
53194ab26f8Smatthias.ringwald 	hci_run();
53216833f0aSmatthias.ringwald }
53316833f0aSmatthias.ringwald 
53410e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
53510e830c9Smatthias.ringwald     switch (packet_type) {
53610e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
53710e830c9Smatthias.ringwald             event_handler(packet, size);
53810e830c9Smatthias.ringwald             break;
53910e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
54010e830c9Smatthias.ringwald             acl_handler(packet, size);
54110e830c9Smatthias.ringwald             break;
54210e830c9Smatthias.ringwald         default:
54310e830c9Smatthias.ringwald             break;
54410e830c9Smatthias.ringwald     }
54510e830c9Smatthias.ringwald }
54610e830c9Smatthias.ringwald 
547fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5482718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5492718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
55016833f0aSmatthias.ringwald }
55116833f0aSmatthias.ringwald 
552404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
553475c8125Smatthias.ringwald 
554475c8125Smatthias.ringwald     // reference to use transport layer implementation
55516833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
556475c8125Smatthias.ringwald 
55711e23e5fSmatthias.ringwald     // references to used control implementation
55811e23e5fSmatthias.ringwald     hci_stack.control = control;
55911e23e5fSmatthias.ringwald 
56011e23e5fSmatthias.ringwald     // reference to used config
56111e23e5fSmatthias.ringwald     hci_stack.config = config;
56211e23e5fSmatthias.ringwald 
563fe1ed1b8Smatthias.ringwald     // no connections yet
564fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
5650a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
566fe1ed1b8Smatthias.ringwald 
56702ea9861Smatthias.ringwald     // empty cmd buffer
56816833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
56916833f0aSmatthias.ringwald 
57016833f0aSmatthias.ringwald     // higher level handler
5712718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
57216833f0aSmatthias.ringwald 
573404843c1Smatthias.ringwald     // store and open remote device db
574404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
575404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
576404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
577404843c1Smatthias.ringwald     }
57829d53098Smatthias.ringwald 
57916833f0aSmatthias.ringwald     // register packet handlers with transport
58010e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
581475c8125Smatthias.ringwald }
582475c8125Smatthias.ringwald 
583404843c1Smatthias.ringwald void hci_close(){
584404843c1Smatthias.ringwald     // close remote device db
585404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
586404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
587404843c1Smatthias.ringwald     }
588404843c1Smatthias.ringwald }
589404843c1Smatthias.ringwald 
5908d213e1aSmatthias.ringwald // State-Module-Driver overview
5918d213e1aSmatthias.ringwald // state                    module  low-level
5928d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
5938d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
5948d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
5958d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
596d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
597d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
598c7e0c5f6Smatthias.ringwald 
59940d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6007301ad89Smatthias.ringwald 
601038bc64cSmatthias.ringwald     // power on
602f9a30166Smatthias.ringwald     int err = 0;
603f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
604f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
605f9a30166Smatthias.ringwald     }
606038bc64cSmatthias.ringwald     if (err){
6077f2435e6Smatthias.ringwald         log_err( "POWER_ON failed\n");
608038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
609038bc64cSmatthias.ringwald         return err;
610038bc64cSmatthias.ringwald     }
611038bc64cSmatthias.ringwald 
612038bc64cSmatthias.ringwald     // open low-level device
613038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
614038bc64cSmatthias.ringwald     if (err){
6157f2435e6Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
616f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
617f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
618f9a30166Smatthias.ringwald         }
619038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
620038bc64cSmatthias.ringwald         return err;
621038bc64cSmatthias.ringwald     }
6228d213e1aSmatthias.ringwald     return 0;
6238d213e1aSmatthias.ringwald }
624038bc64cSmatthias.ringwald 
62540d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6268d213e1aSmatthias.ringwald 
6279418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off\n");
6289418f9c9Smatthias.ringwald 
6298d213e1aSmatthias.ringwald     // close low-level device
6308d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6318d213e1aSmatthias.ringwald 
6329418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off - hci_transport closed\n");
6339418f9c9Smatthias.ringwald 
6348d213e1aSmatthias.ringwald     // power off
6358d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6368d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6378d213e1aSmatthias.ringwald     }
6389418f9c9Smatthias.ringwald 
6399418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off - control closed\n");
6409418f9c9Smatthias.ringwald 
64172ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
64272ea5239Smatthias.ringwald }
64372ea5239Smatthias.ringwald 
64440d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
64572ea5239Smatthias.ringwald 
6469418f9c9Smatthias.ringwald     log_dbg("hci_power_control_sleep\n");
6473144bce4Smatthias.ringwald 
648b429b9b7Smatthias.ringwald #if 0
649b429b9b7Smatthias.ringwald     // don't close serial port during sleep
650b429b9b7Smatthias.ringwald 
65172ea5239Smatthias.ringwald     // close low-level device
65272ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
653b429b9b7Smatthias.ringwald #endif
65472ea5239Smatthias.ringwald 
65572ea5239Smatthias.ringwald     // sleep mode
6563144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
65772ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
65872ea5239Smatthias.ringwald     }
659b429b9b7Smatthias.ringwald 
66072ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6618d213e1aSmatthias.ringwald }
6628d213e1aSmatthias.ringwald 
66340d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
664b429b9b7Smatthias.ringwald 
6659418f9c9Smatthias.ringwald     log_dbg("hci_power_control_wake\n");
666b429b9b7Smatthias.ringwald 
667b429b9b7Smatthias.ringwald     // wake on
668b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
669b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
670b429b9b7Smatthias.ringwald     }
671b429b9b7Smatthias.ringwald 
672b429b9b7Smatthias.ringwald #if 0
673b429b9b7Smatthias.ringwald     // open low-level device
674b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
675b429b9b7Smatthias.ringwald     if (err){
676b429b9b7Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
677b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
678b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
679b429b9b7Smatthias.ringwald         }
680b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
681b429b9b7Smatthias.ringwald         return err;
682b429b9b7Smatthias.ringwald     }
683b429b9b7Smatthias.ringwald #endif
684b429b9b7Smatthias.ringwald 
685b429b9b7Smatthias.ringwald     return 0;
686b429b9b7Smatthias.ringwald }
687b429b9b7Smatthias.ringwald 
688b429b9b7Smatthias.ringwald 
6898d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
690d661ed19Smatthias.ringwald 
6917ec5eeaaSmatthias.ringwald     log_dbg("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
692d661ed19Smatthias.ringwald 
6938d213e1aSmatthias.ringwald     int err = 0;
6948d213e1aSmatthias.ringwald     switch (hci_stack.state){
6958d213e1aSmatthias.ringwald 
6968d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
6978d213e1aSmatthias.ringwald             switch (power_mode){
6988d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
6998d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7008d213e1aSmatthias.ringwald                     if (err) return err;
7017301ad89Smatthias.ringwald                     // set up state machine
7027301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7037301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7047301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7058d213e1aSmatthias.ringwald                     break;
7068d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7078d213e1aSmatthias.ringwald                     // do nothing
7088d213e1aSmatthias.ringwald                     break;
7098d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
710b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7118d213e1aSmatthias.ringwald                     break;
7128d213e1aSmatthias.ringwald             }
7138d213e1aSmatthias.ringwald             break;
7147301ad89Smatthias.ringwald 
7158d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7168d213e1aSmatthias.ringwald             switch (power_mode){
7178d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7188d213e1aSmatthias.ringwald                     // do nothing
7198d213e1aSmatthias.ringwald                     break;
7208d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7218d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7228d213e1aSmatthias.ringwald                     hci_power_control_off();
7238d213e1aSmatthias.ringwald                     break;
7248d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
725b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
72672ea5239Smatthias.ringwald                     hci_power_control_sleep();
7278d213e1aSmatthias.ringwald                     break;
7288d213e1aSmatthias.ringwald             }
7298d213e1aSmatthias.ringwald             break;
7307301ad89Smatthias.ringwald 
7318d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7328d213e1aSmatthias.ringwald             switch (power_mode){
7338d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7348d213e1aSmatthias.ringwald                     // do nothing
7358d213e1aSmatthias.ringwald                     break;
7368d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
737c7e0c5f6Smatthias.ringwald                     // see hci_run
738c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7398d213e1aSmatthias.ringwald                     break;
7408d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
741b546ac54Smatthias.ringwald                     // see hci_run
742b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
74389db417bSmatthias.ringwald                     hci_stack.substate = 0;
7448d213e1aSmatthias.ringwald                     break;
7458d213e1aSmatthias.ringwald             }
7468d213e1aSmatthias.ringwald             break;
7477301ad89Smatthias.ringwald 
7488d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7498d213e1aSmatthias.ringwald             switch (power_mode){
7508d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7518d213e1aSmatthias.ringwald                     // set up state machine
7528d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7538d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7548d213e1aSmatthias.ringwald                     break;
7558d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7568d213e1aSmatthias.ringwald                     // do nothing
7578d213e1aSmatthias.ringwald                     break;
7588d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
759b546ac54Smatthias.ringwald                     // see hci_run
760b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
76189db417bSmatthias.ringwald                     hci_stack.substate = 0;
7628d213e1aSmatthias.ringwald                     break;
7638d213e1aSmatthias.ringwald             }
7648d213e1aSmatthias.ringwald             break;
7658d213e1aSmatthias.ringwald 
7668d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7678d213e1aSmatthias.ringwald             switch (power_mode){
7688d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
769b546ac54Smatthias.ringwald                     // set up state machine
770b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
771b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
772b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7738d213e1aSmatthias.ringwald                     break;
7748d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
775b546ac54Smatthias.ringwald                     // see hci_run
776b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7778d213e1aSmatthias.ringwald                     break;
7788d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
779b546ac54Smatthias.ringwald                     // do nothing
7808d213e1aSmatthias.ringwald                     break;
7818d213e1aSmatthias.ringwald             }
7828d213e1aSmatthias.ringwald             break;
7838d213e1aSmatthias.ringwald 
7848d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
7858d213e1aSmatthias.ringwald             switch (power_mode){
7868d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7873144bce4Smatthias.ringwald                     err = hci_power_control_wake();
7883144bce4Smatthias.ringwald                     if (err) return err;
789b546ac54Smatthias.ringwald                     // set up state machine
790b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
791b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
792b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7938d213e1aSmatthias.ringwald                     break;
7948d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7954ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7968d213e1aSmatthias.ringwald                     break;
7978d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
798b546ac54Smatthias.ringwald                     // do nothing
7998d213e1aSmatthias.ringwald                     break;
8008d213e1aSmatthias.ringwald             }
8018d213e1aSmatthias.ringwald             break;
80211e23e5fSmatthias.ringwald     }
80368d92d03Smatthias.ringwald 
804038bc64cSmatthias.ringwald     // create internal event
805ee8bf225Smatthias.ringwald 	hci_emit_state();
806ee8bf225Smatthias.ringwald 
80768d92d03Smatthias.ringwald 	// trigger next/first action
80868d92d03Smatthias.ringwald 	hci_run();
80968d92d03Smatthias.ringwald 
810475c8125Smatthias.ringwald     return 0;
811475c8125Smatthias.ringwald }
812475c8125Smatthias.ringwald 
813381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
814381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
815381fbed8Smatthias.ringwald 
816381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
817381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
818381fbed8Smatthias.ringwald         return;
819381fbed8Smatthias.ringwald     }
820381fbed8Smatthias.ringwald 
821381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
822381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
823381fbed8Smatthias.ringwald }
824381fbed8Smatthias.ringwald 
82506b35ec0Smatthias.ringwald void hci_run(){
8268a485f27Smatthias.ringwald 
827c7e0c5f6Smatthias.ringwald     if (hci_stack.num_cmd_packets == 0) {
828c7e0c5f6Smatthias.ringwald         // cannot send command yet
829c7e0c5f6Smatthias.ringwald         return;
830c7e0c5f6Smatthias.ringwald     }
831c7e0c5f6Smatthias.ringwald 
832c7e0c5f6Smatthias.ringwald     hci_connection_t * connection;
833c7e0c5f6Smatthias.ringwald 
8343429f56bSmatthias.ringwald     switch (hci_stack.state){
8353429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8363429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
8373429f56bSmatthias.ringwald                 // odd: waiting for command completion
83806b35ec0Smatthias.ringwald                 return;
8393429f56bSmatthias.ringwald             }
84090919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
841c7492964Smatthias.ringwald                 case 0: // RESET
84222909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
843c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
844c7492964Smatthias.ringwald                         // skip baud change
845c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
846c7492964Smatthias.ringwald                     }
8473429f56bSmatthias.ringwald                     break;
848c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
849c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
850c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
851c7492964Smatthias.ringwald                     break;
852c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
853c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
854c7492964Smatthias.ringwald                     hci_stack.substate += 2;
855c7492964Smatthias.ringwald                     // break missing here for fall through
856c7492964Smatthias.ringwald 
857c7492964Smatthias.ringwald                 case 3:
85890919203Smatthias.ringwald                     // custom initialization
85990919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
86090919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
86190919203Smatthias.ringwald                         if (cmd) {
86290919203Smatthias.ringwald                             int size = 3 + cmd[2];
863622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
864c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
86590919203Smatthias.ringwald                             break;
86690919203Smatthias.ringwald                         }
867c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
86890919203Smatthias.ringwald                     }
8692f6c30e1Smatthias.ringwald                     // otherwise continue
870f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
871f432a6ddSmatthias.ringwald 					break;
872c7492964Smatthias.ringwald 				case 4:
873e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
874e2edc0c3Smatthias.ringwald 					break;
875c7492964Smatthias.ringwald                 case 5:
8763429f56bSmatthias.ringwald                     // ca. 15 sec
8773429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
8783429f56bSmatthias.ringwald                     break;
879c7492964Smatthias.ringwald 				case 6:
8800a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
881f432a6ddSmatthias.ringwald 					break;
882c7492964Smatthias.ringwald                 case 7:
8838a485f27Smatthias.ringwald #ifndef EMBEDDED
8848a485f27Smatthias.ringwald                 {
8858a485f27Smatthias.ringwald                     char hostname[30];
8868a485f27Smatthias.ringwald                     gethostname(hostname, 30);
8878a485f27Smatthias.ringwald                     hostname[29] = '\0';
8888a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
8898a485f27Smatthias.ringwald                     break;
8908a485f27Smatthias.ringwald                 }
891c7492964Smatthias.ringwald                 case 8:
8923c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
8933c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
8943c4d4b90Smatthias.ringwald                     break;
8953c4d4b90Smatthias.ringwald 
896c7492964Smatthias.ringwald                 case 9:
8973c4d4b90Smatthias.ringwald #endif
8988a485f27Smatthias.ringwald #endif
8993429f56bSmatthias.ringwald                     // done.
9003429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
901b360b6adSmatthias.ringwald                     hci_emit_state();
9023429f56bSmatthias.ringwald                     break;
9033429f56bSmatthias.ringwald                 default:
9043429f56bSmatthias.ringwald                     break;
905475c8125Smatthias.ringwald             }
9063429f56bSmatthias.ringwald             hci_stack.substate++;
9073429f56bSmatthias.ringwald             break;
908c7e0c5f6Smatthias.ringwald 
909c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
910c7e0c5f6Smatthias.ringwald 
9113144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING\n");
912c7e0c5f6Smatthias.ringwald             // close all open connections
913c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
914c7e0c5f6Smatthias.ringwald             if (connection){
915c7492964Smatthias.ringwald                 log_dbg("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle);
916c7e0c5f6Smatthias.ringwald                 // send disconnect
9176ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
918c7e0c5f6Smatthias.ringwald 
919c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
920c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
921c7e0c5f6Smatthias.ringwald                 return;
922c7e0c5f6Smatthias.ringwald             }
9233144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, calling off\n");
924c7e0c5f6Smatthias.ringwald 
92572ea5239Smatthias.ringwald             // switch mode
926c7e0c5f6Smatthias.ringwald             hci_power_control_off();
9279418f9c9Smatthias.ringwald 
9289418f9c9Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, emitting state\n");
92972ea5239Smatthias.ringwald             hci_emit_state();
9303144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, done\n");
93172ea5239Smatthias.ringwald             break;
932c7e0c5f6Smatthias.ringwald 
93372ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
93489db417bSmatthias.ringwald             switch(hci_stack.substate) {
93589db417bSmatthias.ringwald                 case 0:
93689db417bSmatthias.ringwald                     log_dbg("HCI_STATE_FALLING_ASLEEP\n");
93772ea5239Smatthias.ringwald                     // close all open connections
93872ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
93972ea5239Smatthias.ringwald                     if (connection){
940c7492964Smatthias.ringwald                         log_dbg("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle);
94172ea5239Smatthias.ringwald                         // send disconnect
9426ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
94372ea5239Smatthias.ringwald 
94472ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
94572ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
94672ea5239Smatthias.ringwald                         return;
94772ea5239Smatthias.ringwald                     }
94872ea5239Smatthias.ringwald 
94989db417bSmatthias.ringwald                     log_dbg("HCI_STATE_HALTING, disabling inq & page scans\n");
95089db417bSmatthias.ringwald 
95189db417bSmatthias.ringwald                     // disable page and inquiry scan
95289db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
95389db417bSmatthias.ringwald 
95489db417bSmatthias.ringwald                     // continue in next sub state
95589db417bSmatthias.ringwald                     hci_stack.substate++;
95689db417bSmatthias.ringwald                     break;
95789db417bSmatthias.ringwald                 case 1:
95889db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
95989db417bSmatthias.ringwald                     break;
96089db417bSmatthias.ringwald                 case 2:
96189db417bSmatthias.ringwald                     log_dbg("HCI_STATE_HALTING, calling sleep\n");
96272ea5239Smatthias.ringwald                     // switch mode
96389db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
964c7e0c5f6Smatthias.ringwald                     hci_emit_state();
96589db417bSmatthias.ringwald                 default:
96689db417bSmatthias.ringwald                     break;
96789db417bSmatthias.ringwald             }
968c7e0c5f6Smatthias.ringwald             break;
969c7e0c5f6Smatthias.ringwald 
9703429f56bSmatthias.ringwald         default:
9713429f56bSmatthias.ringwald             break;
9721f504dbdSmatthias.ringwald     }
9733429f56bSmatthias.ringwald }
97416833f0aSmatthias.ringwald 
97531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
976c8e4258aSmatthias.ringwald     bd_addr_t addr;
977c8e4258aSmatthias.ringwald     hci_connection_t * conn;
978c8e4258aSmatthias.ringwald     // house-keeping
979c8e4258aSmatthias.ringwald 
980c8e4258aSmatthias.ringwald     // create_connection?
981c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
982c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
983cf0b66f0Smatthias.ringwald         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
984c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
985c8e4258aSmatthias.ringwald         if (conn) {
986c8e4258aSmatthias.ringwald             // if connection exists
987c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
988c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
989c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
990c8e4258aSmatthias.ringwald             }
991c8e4258aSmatthias.ringwald             //    otherwise, just ignore
992c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
993c8e4258aSmatthias.ringwald 
994c8e4258aSmatthias.ringwald         } else{
995c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
996c8e4258aSmatthias.ringwald             if (conn){
997c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
998c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
999c8e4258aSmatthias.ringwald             }
1000c8e4258aSmatthias.ringwald         }
1001c8e4258aSmatthias.ringwald     }
1002c8e4258aSmatthias.ringwald 
10037fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
10047fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
10057fde4af9Smatthias.ringwald     }
10067fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
10077fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
10087fde4af9Smatthias.ringwald     }
10097fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
10107fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
10117fde4af9Smatthias.ringwald     }
10127fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
10137fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
10147fde4af9Smatthias.ringwald     }
10157fde4af9Smatthias.ringwald 
10168ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
10178ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
10188ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10198ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
10208ef73945Smatthias.ringwald         }
10218ef73945Smatthias.ringwald     }
1022c8e4258aSmatthias.ringwald 
102331452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1024622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
102531452debSmatthias.ringwald }
10268adf0ddaSmatthias.ringwald 
10271cd208adSmatthias.ringwald /**
10281cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
10291cd208adSmatthias.ringwald  */
1030fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
10311cd208adSmatthias.ringwald     va_list argptr;
10321cd208adSmatthias.ringwald     va_start(argptr, cmd);
10331cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
10341cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
10351cd208adSmatthias.ringwald     va_end(argptr);
10361cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
103793b8dc03Smatthias.ringwald }
1038c8e4258aSmatthias.ringwald 
1039ee091cf1Smatthias.ringwald // Create various non-HCI events.
1040ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1041ee091cf1Smatthias.ringwald 
1042c8e4258aSmatthias.ringwald void hci_emit_state(){
1043c8e4258aSmatthias.ringwald     uint8_t len = 3;
1044c8e4258aSmatthias.ringwald     uint8_t event[len];
104580d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
10461b0e3922Smatthias.ringwald     event[1] = len - 3;
1047c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1048c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10492718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1050c8e4258aSmatthias.ringwald }
1051c8e4258aSmatthias.ringwald 
1052c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
1053c8e4258aSmatthias.ringwald     uint8_t len = 13;
1054c8e4258aSmatthias.ringwald     uint8_t event[len];
1055c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
10561b0e3922Smatthias.ringwald     event[1] = len - 3;
1057c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
1058c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1059c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1060c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1061c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1062c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10632718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1064c8e4258aSmatthias.ringwald }
1065c8e4258aSmatthias.ringwald 
10663c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
10673c4d4b90Smatthias.ringwald     uint8_t len = 6;
10683c4d4b90Smatthias.ringwald     uint8_t event[len];
10693c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
10703c4d4b90Smatthias.ringwald     event[1] = len - 3;
10713c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
10723c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
10733c4d4b90Smatthias.ringwald     event[5] = reason;
10743c4d4b90Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10753c4d4b90Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
10763c4d4b90Smatthias.ringwald }
10773c4d4b90Smatthias.ringwald 
1078ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1079ee091cf1Smatthias.ringwald     uint8_t len = 4;
1080ee091cf1Smatthias.ringwald     uint8_t event[len];
108180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
10821b0e3922Smatthias.ringwald     event[1] = len - 2;
1083ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1084ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10852718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1086ee091cf1Smatthias.ringwald }
108743bfb1bdSmatthias.ringwald 
108843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
108943bfb1bdSmatthias.ringwald     uint8_t len = 3;
109043bfb1bdSmatthias.ringwald     uint8_t event[len];
109180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
10921b0e3922Smatthias.ringwald     event[1] = len - 2;
109343bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
109443bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10952718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
109643bfb1bdSmatthias.ringwald }
1097038bc64cSmatthias.ringwald 
1098038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
10991b0e3922Smatthias.ringwald     uint8_t len = 2;
1100038bc64cSmatthias.ringwald     uint8_t event[len];
110180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
11021b0e3922Smatthias.ringwald     event[1] = len - 2;
1103038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11042718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1105038bc64cSmatthias.ringwald }
11061b0e3922Smatthias.ringwald 
11071b0e3922Smatthias.ringwald 
11081b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
11091b0e3922Smatthias.ringwald     uint8_t len = 6;
11101b0e3922Smatthias.ringwald     uint8_t event[len];
11111b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
11121b0e3922Smatthias.ringwald     event[1] = len - 2;
11131b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
11141b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
11151b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
11161b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11172718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
11181b0e3922Smatthias.ringwald }
11191b0e3922Smatthias.ringwald 
11202ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
11212ed6235cSmatthias.ringwald     uint8_t len = 3;
11222ed6235cSmatthias.ringwald     uint8_t event[len];
11232ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1124ddce1d8dSmatthias.ringwald     event[1] = len - 2;
11252ed6235cSmatthias.ringwald     event[2] = enabled;
11262ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11272718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
11282ed6235cSmatthias.ringwald }
1129627c2f45Smatthias.ringwald 
1130627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1131f653b6bdSmatthias.ringwald     uint16_t len = 2+1+6+248;
1132627c2f45Smatthias.ringwald     uint8_t event[len];
1133627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1134627c2f45Smatthias.ringwald     event[1] = len - 2;
1135f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
11362f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1137f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1138627c2f45Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, len);
1139627c2f45Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1140627c2f45Smatthias.ringwald }
1141381fbed8Smatthias.ringwald 
1142381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1143381fbed8Smatthias.ringwald     uint8_t len = 3;
1144381fbed8Smatthias.ringwald     uint8_t event[len];
1145381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1146381fbed8Smatthias.ringwald     event[1] = len - 2;
1147381fbed8Smatthias.ringwald     event[2] = enabled;
1148381fbed8Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
1149381fbed8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1150381fbed8Smatthias.ringwald }
1151