xref: /btstack/src/hci.c (revision 6c062428e1df1d0386fdea7301e424bf38468548)
11f504dbdSmatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 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.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
381f504dbdSmatthias.ringwald  *  hci.c
391f504dbdSmatthias.ringwald  *
401f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
411f504dbdSmatthias.ringwald  *
421f504dbdSmatthias.ringwald  */
431f504dbdSmatthias.ringwald 
44c8901d41Smatthias.ringwald #include "config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
477f2435e6Smatthias.ringwald 
4893b8dc03Smatthias.ringwald #include <stdarg.h>
4993b8dc03Smatthias.ringwald #include <string.h>
5056fe0872Smatthias.ringwald #include <stdio.h>
517f2435e6Smatthias.ringwald 
52549e6ebeSmatthias.ringwald #ifndef EMBEDDED
53549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5409ba8edeSmatthias.ringwald #include <btstack/version.h>
55549e6ebeSmatthias.ringwald #endif
56549e6ebeSmatthias.ringwald 
57a3b02b71Smatthias.ringwald #include "btstack_memory.h"
587f2435e6Smatthias.ringwald #include "debug.h"
59d8905019Smatthias.ringwald #include "hci_dump.h"
6093b8dc03Smatthias.ringwald 
61ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
621b0e3922Smatthias.ringwald 
63169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
64ee091cf1Smatthias.ringwald 
65a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
66da5275c5S[email protected] 
6728171530Smatthias.ringwald #ifdef USE_BLUETOOL
6828171530Smatthias.ringwald #include "bt_control_iphone.h"
6928171530Smatthias.ringwald #endif
7028171530Smatthias.ringwald 
71758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
72758b46ceSmatthias.ringwald 
7306b35ec0Smatthias.ringwald // the STACK is here
7416833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
7516833f0aSmatthias.ringwald 
7697addcc5Smatthias.ringwald /**
77ee091cf1Smatthias.ringwald  * get connection for a given handle
78ee091cf1Smatthias.ringwald  *
79ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
80ee091cf1Smatthias.ringwald  */
815061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
82ee091cf1Smatthias.ringwald     linked_item_t *it;
83ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
84ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
85ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
86ee091cf1Smatthias.ringwald         }
87ee091cf1Smatthias.ringwald     }
88ee091cf1Smatthias.ringwald     return NULL;
89ee091cf1Smatthias.ringwald }
90ee091cf1Smatthias.ringwald 
912b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
9228ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
93c785ef68Smatthias.ringwald #ifdef HAVE_TIME
94ee091cf1Smatthias.ringwald     struct timeval tv;
95ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
96c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
97ee091cf1Smatthias.ringwald         // connections might be timed out
98ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
99ee091cf1Smatthias.ringwald     }
1002b12a0b9Smatthias.ringwald #endif
101e5780900Smatthias.ringwald #ifdef HAVE_TICK
102c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
103c785ef68Smatthias.ringwald         // connections might be timed out
104c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
105c785ef68Smatthias.ringwald     }
106c785ef68Smatthias.ringwald #endif
107c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
108c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
109c785ef68Smatthias.ringwald }
110ee091cf1Smatthias.ringwald 
111ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
112c7492964Smatthias.ringwald #ifdef HAVE_TIME
113ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
114c7492964Smatthias.ringwald #endif
115e5780900Smatthias.ringwald #ifdef HAVE_TICK
116c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
117c785ef68Smatthias.ringwald #endif
118ee091cf1Smatthias.ringwald }
119ee091cf1Smatthias.ringwald 
120ee091cf1Smatthias.ringwald /**
121c8e4258aSmatthias.ringwald  * create connection for given address
122c8e4258aSmatthias.ringwald  *
12317f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
124c8e4258aSmatthias.ringwald  */
125c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
12628ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
127c8e4258aSmatthias.ringwald     if (!conn) return NULL;
128c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
129c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1307d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
131ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
132ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
133ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
134d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1357856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
13656cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
137c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
138c8e4258aSmatthias.ringwald     return conn;
139c8e4258aSmatthias.ringwald }
140c8e4258aSmatthias.ringwald 
141c8e4258aSmatthias.ringwald /**
14206b35ec0Smatthias.ringwald  * get connection for given address
14397addcc5Smatthias.ringwald  *
14497addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
14597addcc5Smatthias.ringwald  */
146fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
14706b35ec0Smatthias.ringwald     linked_item_t *it;
14806b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
14906b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
15006b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
15106b35ec0Smatthias.ringwald         }
15206b35ec0Smatthias.ringwald     }
15306b35ec0Smatthias.ringwald     return NULL;
15406b35ec0Smatthias.ringwald }
15506b35ec0Smatthias.ringwald 
15628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
15728ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
15828ca2b46S[email protected] }
15928ca2b46S[email protected] 
16028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
16128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
16228ca2b46S[email protected] }
16328ca2b46S[email protected] 
16428ca2b46S[email protected] 
16543bfb1bdSmatthias.ringwald /**
16680ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1677fde4af9Smatthias.ringwald  */
1687fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1697fde4af9Smatthias.ringwald     bd_addr_t addr;
1707fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1717fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1727fde4af9Smatthias.ringwald     if (conn) {
17328ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
17480ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1757fde4af9Smatthias.ringwald     }
1767fde4af9Smatthias.ringwald }
1777fde4af9Smatthias.ringwald 
17880ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
1795061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
18080ca58a0Smatthias.ringwald     if (!conn) return 0;
18180ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
18280ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
18380ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
18480ca58a0Smatthias.ringwald     return 1;
18580ca58a0Smatthias.ringwald }
18680ca58a0Smatthias.ringwald 
187c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
188c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
189c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
190c12e46e7Smatthias.ringwald     }
191c12e46e7Smatthias.ringwald }
192c12e46e7Smatthias.ringwald 
1937fde4af9Smatthias.ringwald 
1947fde4af9Smatthias.ringwald /**
19543bfb1bdSmatthias.ringwald  * count connections
19643bfb1bdSmatthias.ringwald  */
19740d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
19856c253c9Smatthias.ringwald     int count = 0;
19943bfb1bdSmatthias.ringwald     linked_item_t *it;
20043bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
20143bfb1bdSmatthias.ringwald     return count;
20243bfb1bdSmatthias.ringwald }
203c8e4258aSmatthias.ringwald 
20497addcc5Smatthias.ringwald /**
205ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
20616833f0aSmatthias.ringwald  */
2072718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
20816833f0aSmatthias.ringwald }
20916833f0aSmatthias.ringwald 
210998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2115061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
212998906cdSmatthias.ringwald     if (!connection) {
2137d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
214998906cdSmatthias.ringwald         return 0;
215998906cdSmatthias.ringwald     }
216998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
217998906cdSmatthias.ringwald }
218998906cdSmatthias.ringwald 
219998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
220998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
221998906cdSmatthias.ringwald     linked_item_t *it;
222998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
223998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
224998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2257d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
226998906cdSmatthias.ringwald             return 0;
227998906cdSmatthias.ringwald         }
228998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
229998906cdSmatthias.ringwald     }
230998906cdSmatthias.ringwald     return free_slots;
231998906cdSmatthias.ringwald }
232998906cdSmatthias.ringwald 
233c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2342b12a0b9Smatthias.ringwald 
2352b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2362b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2372b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2382b12a0b9Smatthias.ringwald             return 0;
2392b12a0b9Smatthias.ringwald         }
2402b12a0b9Smatthias.ringwald     }
2412b12a0b9Smatthias.ringwald 
2422b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
243c24735b1Smatthias.ringwald     switch (packet_type) {
244c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
245c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
246c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
247de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
248c24735b1Smatthias.ringwald         default:
249c24735b1Smatthias.ringwald             return 0;
250c24735b1Smatthias.ringwald     }
251c24735b1Smatthias.ringwald }
252c24735b1Smatthias.ringwald 
253ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2547856c818Smatthias.ringwald 
2556218e6f1Smatthias.ringwald     // check for free places on BT module
2565932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2576218e6f1Smatthias.ringwald 
2587856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2595061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
26056cf178bSmatthias.ringwald     if (!connection) return 0;
26156cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
26256cf178bSmatthias.ringwald 
26356cf178bSmatthias.ringwald     // count packet
26456cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2657b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2667856c818Smatthias.ringwald 
26700d8e42eSmatthias.ringwald     // send packet
26800d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2696218e6f1Smatthias.ringwald 
27000d8e42eSmatthias.ringwald     return err;
271ee091cf1Smatthias.ringwald }
272ee091cf1Smatthias.ringwald 
27316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2747856c818Smatthias.ringwald 
275e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
276e76a89eeS[email protected] 
2777856c818Smatthias.ringwald     // get info
2787856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2795061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
2807856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2817856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2827856c818Smatthias.ringwald 
2837856c818Smatthias.ringwald     // ignore non-registered handle
2847856c818Smatthias.ringwald     if (!conn){
2857d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2867856c818Smatthias.ringwald         return;
2877856c818Smatthias.ringwald     }
2887856c818Smatthias.ringwald 
289e76a89eeS[email protected]     // assert packet is complete
290e76a89eeS[email protected]     if (acl_length != size + 4){
291e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
292e76a89eeS[email protected]         return;
293e76a89eeS[email protected]     }
294e76a89eeS[email protected] 
2957856c818Smatthias.ringwald     // update idle timestamp
2967856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2977856c818Smatthias.ringwald 
2987856c818Smatthias.ringwald     // handle different packet types
2997856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3007856c818Smatthias.ringwald 
3017856c818Smatthias.ringwald         case 0x01: // continuation fragment
3027856c818Smatthias.ringwald 
3037856c818Smatthias.ringwald             // sanity check
3047856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3057d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3067856c818Smatthias.ringwald                 return;
3077856c818Smatthias.ringwald             }
3087856c818Smatthias.ringwald 
3097856c818Smatthias.ringwald             // append fragment payload (header already stored)
3107856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3117856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3127856c818Smatthias.ringwald 
3137d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
314decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3157856c818Smatthias.ringwald 
3167856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3177856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3187856c818Smatthias.ringwald 
3192718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3207856c818Smatthias.ringwald                 // reset recombination buffer
3217856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3227856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3237856c818Smatthias.ringwald             }
3247856c818Smatthias.ringwald             break;
3257856c818Smatthias.ringwald 
3267856c818Smatthias.ringwald         case 0x02: { // first fragment
3277856c818Smatthias.ringwald 
3287856c818Smatthias.ringwald             // sanity check
3297856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3307d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3317856c818Smatthias.ringwald                 return;
3327856c818Smatthias.ringwald             }
3337856c818Smatthias.ringwald 
3347856c818Smatthias.ringwald             // peek into L2CAP packet!
3357856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3367856c818Smatthias.ringwald 
337e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
338decc01a8Smatthias.ringwald 
3397856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3407856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3417856c818Smatthias.ringwald 
3427856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3432718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3447856c818Smatthias.ringwald 
3457856c818Smatthias.ringwald             } else {
3467856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3477856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3487856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3497856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
350decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3517856c818Smatthias.ringwald             }
3527856c818Smatthias.ringwald             break;
3537856c818Smatthias.ringwald 
3547856c818Smatthias.ringwald         }
3557856c818Smatthias.ringwald         default:
3567d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3577856c818Smatthias.ringwald             return;
3587856c818Smatthias.ringwald     }
35994ab26f8Smatthias.ringwald 
36094ab26f8Smatthias.ringwald     // execute main loop
36194ab26f8Smatthias.ringwald     hci_run();
36216833f0aSmatthias.ringwald }
36322909952Smatthias.ringwald 
36467a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
365339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3663c4d4b90Smatthias.ringwald 
3673c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3683c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3693c4d4b90Smatthias.ringwald 
370c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
371c785ef68Smatthias.ringwald 
372c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
373a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3743c4d4b90Smatthias.ringwald 
3753c4d4b90Smatthias.ringwald     // now it's gone
376c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
377c7e0c5f6Smatthias.ringwald }
378c7e0c5f6Smatthias.ringwald 
3790c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3808f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3818f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3828f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3838f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3848f8108aaSmatthias.ringwald };
38565389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
38665389bfcS[email protected]      0, // 3 slot packets
38765389bfcS[email protected]      1, // 5 slot packets
38865389bfcS[email protected]     25, // EDR 2 mpbs
38965389bfcS[email protected]     26, // EDR 3 mbps
39065389bfcS[email protected]     39, // 3 slot EDR packts
39165389bfcS[email protected]     40, // 5 slot EDR packet
39265389bfcS[email protected] };
39365389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
39465389bfcS[email protected]     0x0f00, // 3 slot packets
39565389bfcS[email protected]     0xf000, // 5 slot packets
39665389bfcS[email protected]     0x1102, // EDR 2 mpbs
39765389bfcS[email protected]     0x2204, // EDR 3 mbps
39865389bfcS[email protected]     0x0300, // 3 slot EDR packts
39965389bfcS[email protected]     0x3000, // 5 slot EDR packet
40065389bfcS[email protected] };
4018f8108aaSmatthias.ringwald 
40265389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
40365389bfcS[email protected]     // enable packet types based on size
4048f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4058f8108aaSmatthias.ringwald     int i;
4068f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4078f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4088f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4098f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4108f8108aaSmatthias.ringwald         }
4118f8108aaSmatthias.ringwald     }
41265389bfcS[email protected]     // disable packet types due to missing local supported features
41365389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
41465389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
41565389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
41665389bfcS[email protected]         if (feature_set) continue;
41765389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
41865389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
41965389bfcS[email protected]     }
4208f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4218f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4228f8108aaSmatthias.ringwald     return packet_types;
4238f8108aaSmatthias.ringwald }
4248f8108aaSmatthias.ringwald 
4258f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4268f8108aaSmatthias.ringwald     return hci_stack.packet_types;
4278f8108aaSmatthias.ringwald }
4288f8108aaSmatthias.ringwald 
4297dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4307dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4317dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
4327dc17943Smatthias.ringwald }
4337dc17943Smatthias.ringwald 
434f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4357dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
4367dc17943Smatthias.ringwald }
4377dc17943Smatthias.ringwald 
438f5d8d141S[email protected] int hci_ssp_supported(void){
439f5d8d141S[email protected]     // No 51, byte 6, bit 3
440f5d8d141S[email protected]     return (hci_stack.local_supported_features[6] & (1 << 3)) != 0;
441f5d8d141S[email protected] }
442f5d8d141S[email protected] 
443f5d8d141S[email protected] int hci_classic_supported(void){
444f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
445f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 5)) == 0;
446f5d8d141S[email protected] }
447f5d8d141S[email protected] 
448f5d8d141S[email protected] int hci_le_supported(void){
449f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
450f5d8d141S[email protected] #ifdef HAVE_BLE
451f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 6)) != 0;
452f5d8d141S[email protected] #else
453f5d8d141S[email protected]     return 0;
454f5d8d141S[email protected] #endif
455f5d8d141S[email protected] }
456f5d8d141S[email protected] 
45774ec757aSmatthias.ringwald // avoid huge local variables
458223aafc1Smatthias.ringwald #ifndef EMBEDDED
45974ec757aSmatthias.ringwald static device_name_t device_name;
460223aafc1Smatthias.ringwald #endif
46116833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
462e76a89eeS[email protected] 
463e76a89eeS[email protected]     uint16_t event_length = packet[1];
464e76a89eeS[email protected] 
465e76a89eeS[email protected]     // assert packet is complete
466e76a89eeS[email protected]     if (size != event_length + 2){
467e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
468e76a89eeS[email protected]         return;
469e76a89eeS[email protected]     }
470e76a89eeS[email protected] 
4711281a47eSmatthias.ringwald     bd_addr_t addr;
4722d00edd4Smatthias.ringwald     uint8_t link_type;
473fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
4741f7b95a1Smatthias.ringwald     hci_connection_t * conn;
47556cf178bSmatthias.ringwald     int i;
47622909952Smatthias.ringwald 
4775909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4785909f7f2Smatthias.ringwald 
4796772a24cSmatthias.ringwald     switch (packet[0]) {
48022909952Smatthias.ringwald 
4816772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4827ec5eeaaSmatthias.ringwald             // get num cmd packets
4837b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4847ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4857ec5eeaaSmatthias.ringwald 
486e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
487e2edc0c3Smatthias.ringwald                 // from offset 5
488e2edc0c3Smatthias.ringwald                 // status
4891d279b20Smatthias.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"
490e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
49156cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
492e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
49356cf178bSmatthias.ringwald                 // ignore: total num SCO packets
49456cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
495a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
4968fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
4978fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
4988f8108aaSmatthias.ringwald                     }
49965389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
50065389bfcS[email protected]                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
501e2edc0c3Smatthias.ringwald                 }
50256cf178bSmatthias.ringwald             }
50365a46ef3S[email protected] #ifdef HAVE_BLE
50465a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
50565a46ef3S[email protected]                 hci_stack.le_data_packet_length = READ_BT_16(packet, 6);
50665a46ef3S[email protected]                 hci_stack.total_num_le_packets  = packet[8];
50765a46ef3S[email protected]                 log_info("hci_le_read_buffer_size: size %u, count %u\n", hci_stack.le_data_packet_length, hci_stack.total_num_le_packets);
50865a46ef3S[email protected]             }
50965a46ef3S[email protected] #endif
510188981d2Smatthias.ringwald             // Dump local address
511188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
512e2386ba1S[email protected]                 bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
513188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
514e2386ba1S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr));
515188981d2Smatthias.ringwald             }
516381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
517381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
518381fbed8Smatthias.ringwald             }
51965389bfcS[email protected]             // Note: HCI init checks
520559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
52165389bfcS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
522559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
523559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
524559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
525559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
526559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
52765389bfcS[email protected] 
52865389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
52965389bfcS[email protected]                 hci_stack.packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(hci_stack.acl_data_packet_length, &hci_stack.local_supported_features[0]);
530f5d8d141S[email protected]                 log_info("packet types %04x", hci_stack.packet_types);
531f5d8d141S[email protected] 
532f5d8d141S[email protected]                 // Classic/LE
533f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
534559e517eS[email protected]             }
53556cf178bSmatthias.ringwald             break;
53656cf178bSmatthias.ringwald 
5377ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5387ec5eeaaSmatthias.ringwald             // get num cmd packets
5397b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
5407ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
5417ec5eeaaSmatthias.ringwald             break;
5427ec5eeaaSmatthias.ringwald 
54356cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
54456cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
54556cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
54656cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
5475061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
54856cf178bSmatthias.ringwald                 if (!conn){
5497d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
55056cf178bSmatthias.ringwald                     continue;
55156cf178bSmatthias.ringwald                 }
55256cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5537b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
55456cf178bSmatthias.ringwald             }
5556772a24cSmatthias.ringwald             break;
5566772a24cSmatthias.ringwald 
5571f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
55837eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
55937eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5602d00edd4Smatthias.ringwald             link_type = packet[11];
561339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
56237eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5631f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5641f7b95a1Smatthias.ringwald                 if (!conn) {
5651f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5661f7b95a1Smatthias.ringwald                 }
567ce4c8fabSmatthias.ringwald                 if (!conn) {
568ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
569ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
570ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
571ce4c8fabSmatthias.ringwald                     break;
572ce4c8fabSmatthias.ringwald                 }
57332ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
57432ab9390Smatthias.ringwald                 hci_run();
57537eaa4cfSmatthias.ringwald             } else {
576ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
577ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
578ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
57937eaa4cfSmatthias.ringwald             }
5801f7b95a1Smatthias.ringwald             break;
5811f7b95a1Smatthias.ringwald 
5826772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
583fe1ed1b8Smatthias.ringwald             // Connection management
584fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
585339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
5861f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
587fe1ed1b8Smatthias.ringwald             if (conn) {
588b448a0e7Smatthias.ringwald                 if (!packet[2]){
589c8e4258aSmatthias.ringwald                     conn->state = OPEN;
590fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
591ee091cf1Smatthias.ringwald 
592c785ef68Smatthias.ringwald                     // restart timer
593c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
594ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
595c785ef68Smatthias.ringwald 
596339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
59743bfb1bdSmatthias.ringwald 
59843bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
599b448a0e7Smatthias.ringwald                 } else {
600b448a0e7Smatthias.ringwald                     // connection failed, remove entry
601b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
602a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
603c12e46e7Smatthias.ringwald 
604c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
605c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
606c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
607c12e46e7Smatthias.ringwald                     }
608fe1ed1b8Smatthias.ringwald                 }
609fe1ed1b8Smatthias.ringwald             }
6106772a24cSmatthias.ringwald             break;
611fe1ed1b8Smatthias.ringwald 
6127fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
6137b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
6147fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
61574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
61632ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
61764472d52Smatthias.ringwald             hci_run();
618d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
61929d53098Smatthias.ringwald             return;
6207fde4af9Smatthias.ringwald 
6217fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
6227fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
62374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
62429d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
625287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
62629d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
6277fde4af9Smatthias.ringwald             break;
6287fde4af9Smatthias.ringwald 
6297fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
6307fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
631d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
632d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
633d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
634d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
6357fde4af9Smatthias.ringwald             break;
6367fde4af9Smatthias.ringwald 
6371d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
6381d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
6391d6b20aeS[email protected]             if (hci_stack.ssp_io_capability == SSP_IO_CAPABILITY_UNKNOWN) break;
640dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
641dbe1a790S[email protected]             break;
642dbe1a790S[email protected] 
643dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
644dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_CONFIRM_REQUEST);
645dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
646dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
647dbe1a790S[email protected]             break;
648dbe1a790S[email protected] 
649dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
650dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_PASSKEY_REQUEST);
651dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
652dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
6531d6b20aeS[email protected]             break;
6541d6b20aeS[email protected] 
655223aafc1Smatthias.ringwald #ifndef EMBEDDED
65674ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
65774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
65874ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
65974ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
6605a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
6615a06394aSmatthias.ringwald             for (i=0; i<248;i++){
6625a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
6635a06394aSmatthias.ringwald                     packet[9+i] = 0;
6645a06394aSmatthias.ringwald                     break;
665cdc9101dSmatthias.ringwald                 }
6665a06394aSmatthias.ringwald             }
667d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
66874ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
66974ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
67074ec757aSmatthias.ringwald             break;
67174ec757aSmatthias.ringwald 
67274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
67374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
67474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
67574ec757aSmatthias.ringwald             // first send inq result packet
67674ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
67774ec757aSmatthias.ringwald             // then send cached remote names
67874ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
67974ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
68074ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
68174ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
68274ec757aSmatthias.ringwald                 }
68374ec757aSmatthias.ringwald             }
68474ec757aSmatthias.ringwald             return;
685223aafc1Smatthias.ringwald #endif
68674ec757aSmatthias.ringwald 
6876772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
688fe1ed1b8Smatthias.ringwald             if (!packet[2]){
689fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
6905061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
691fe1ed1b8Smatthias.ringwald                 if (conn) {
692c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
693fe1ed1b8Smatthias.ringwald                 }
694fe1ed1b8Smatthias.ringwald             }
6956772a24cSmatthias.ringwald             break;
6966772a24cSmatthias.ringwald 
697c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
698*6c062428S[email protected]             if(hci_stack.control && hci_stack.control->hw_error){
699c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
700c68bdf90Smatthias.ringwald             }
701c68bdf90Smatthias.ringwald             break;
702c68bdf90Smatthias.ringwald 
7035909f7f2Smatthias.ringwald #ifdef HAVE_BLE
7045909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
7055909f7f2Smatthias.ringwald             switch (packet[2]) {
7065909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
7075909f7f2Smatthias.ringwald                     // Connection management
7085909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
7095909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
7105909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
7115909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
7125909f7f2Smatthias.ringwald                     if (packet[3]){
7135909f7f2Smatthias.ringwald                         if (conn){
7145909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
7155909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
7165909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
7175909f7f2Smatthias.ringwald 
7185909f7f2Smatthias.ringwald                         }
7195909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
7205909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
7215909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
7225909f7f2Smatthias.ringwald                         }
7235909f7f2Smatthias.ringwald                         break;
7245909f7f2Smatthias.ringwald                     }
7255909f7f2Smatthias.ringwald                     if (!conn){
7265909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
7275909f7f2Smatthias.ringwald                     }
7285909f7f2Smatthias.ringwald                     if (!conn){
7295909f7f2Smatthias.ringwald                         // no memory
7305909f7f2Smatthias.ringwald                         break;
7315909f7f2Smatthias.ringwald                     }
7325909f7f2Smatthias.ringwald 
7335909f7f2Smatthias.ringwald                     conn->state = OPEN;
7345909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
7355909f7f2Smatthias.ringwald 
7365909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
7375909f7f2Smatthias.ringwald 
7385909f7f2Smatthias.ringwald                     // restart timer
7395909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
7405909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
7415909f7f2Smatthias.ringwald 
7425909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
7435909f7f2Smatthias.ringwald 
7445909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
7455909f7f2Smatthias.ringwald                     break;
7465909f7f2Smatthias.ringwald 
74765a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
74865a46ef3S[email protected] 
7495909f7f2Smatthias.ringwald                 default:
7505909f7f2Smatthias.ringwald                     break;
7515909f7f2Smatthias.ringwald             }
7525909f7f2Smatthias.ringwald             break;
7535909f7f2Smatthias.ringwald #endif
7545909f7f2Smatthias.ringwald 
7556772a24cSmatthias.ringwald         default:
7566772a24cSmatthias.ringwald             break;
757fe1ed1b8Smatthias.ringwald     }
758fe1ed1b8Smatthias.ringwald 
7593429f56bSmatthias.ringwald     // handle BT initialization
7603429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
7613429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
7623429f56bSmatthias.ringwald             // odd: waiting for event
763f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
764c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
765c9af4d3fS[email protected]                 if (hci_stack.substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
7663429f56bSmatthias.ringwald                     hci_stack.substate++;
7673429f56bSmatthias.ringwald                 }
7683429f56bSmatthias.ringwald             }
76922909952Smatthias.ringwald         }
770c9af4d3fS[email protected]     }
77122909952Smatthias.ringwald 
77289db417bSmatthias.ringwald     // help with BT sleep
77389db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
77489db417bSmatthias.ringwald         && hci_stack.substate == 1
77589db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
77689db417bSmatthias.ringwald         hci_stack.substate++;
77789db417bSmatthias.ringwald     }
77889db417bSmatthias.ringwald 
7792718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
78094ab26f8Smatthias.ringwald 
78194ab26f8Smatthias.ringwald 	// execute main loop
78294ab26f8Smatthias.ringwald 	hci_run();
78316833f0aSmatthias.ringwald }
78416833f0aSmatthias.ringwald 
7850a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
78610e830c9Smatthias.ringwald     switch (packet_type) {
78710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
78810e830c9Smatthias.ringwald             event_handler(packet, size);
78910e830c9Smatthias.ringwald             break;
79010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
79110e830c9Smatthias.ringwald             acl_handler(packet, size);
79210e830c9Smatthias.ringwald             break;
79310e830c9Smatthias.ringwald         default:
79410e830c9Smatthias.ringwald             break;
79510e830c9Smatthias.ringwald     }
79610e830c9Smatthias.ringwald }
79710e830c9Smatthias.ringwald 
798fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
7992718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
8002718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
80116833f0aSmatthias.ringwald }
80216833f0aSmatthias.ringwald 
8034f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
804475c8125Smatthias.ringwald 
805475c8125Smatthias.ringwald     // reference to use transport layer implementation
80616833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
807475c8125Smatthias.ringwald 
80811e23e5fSmatthias.ringwald     // references to used control implementation
80911e23e5fSmatthias.ringwald     hci_stack.control = control;
81011e23e5fSmatthias.ringwald 
81111e23e5fSmatthias.ringwald     // reference to used config
81211e23e5fSmatthias.ringwald     hci_stack.config = config;
81311e23e5fSmatthias.ringwald 
814fe1ed1b8Smatthias.ringwald     // no connections yet
815fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
8160a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
817c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
818758b46ceSmatthias.ringwald 
819b031bebbSmatthias.ringwald     // no pending cmds
820b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
821b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
822b031bebbSmatthias.ringwald 
82316833f0aSmatthias.ringwald     // higher level handler
8242718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
82516833f0aSmatthias.ringwald 
826404843c1Smatthias.ringwald     // store and open remote device db
827404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
828404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
829404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
830404843c1Smatthias.ringwald     }
83129d53098Smatthias.ringwald 
8328fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
8338fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
8348fcba05dSmatthias.ringwald 
83516833f0aSmatthias.ringwald     // register packet handlers with transport
83610e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
837f5454fc6Smatthias.ringwald 
838f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
839e2386ba1S[email protected] 
840e2386ba1S[email protected]     // class of device
841e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
842a45d6b9fS[email protected] 
843ae5d8360S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, auto accept
844ae5d8360S[email protected]     hci_stack.ssp_enable = 1;
845ae5d8360S[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
846a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
847ae5d8360S[email protected]     hci_stack.ssp_auto_accept = 1;
848475c8125Smatthias.ringwald }
849475c8125Smatthias.ringwald 
850404843c1Smatthias.ringwald void hci_close(){
851404843c1Smatthias.ringwald     // close remote device db
852404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
853404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
854404843c1Smatthias.ringwald     }
855f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
856f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
857f5454fc6Smatthias.ringwald }
858f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
859404843c1Smatthias.ringwald }
860404843c1Smatthias.ringwald 
8618d213e1aSmatthias.ringwald // State-Module-Driver overview
8628d213e1aSmatthias.ringwald // state                    module  low-level
8638d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
8648d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
8658d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
8668d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
867d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
868d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
869c7e0c5f6Smatthias.ringwald 
87040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
8717301ad89Smatthias.ringwald 
872038bc64cSmatthias.ringwald     // power on
873f9a30166Smatthias.ringwald     int err = 0;
874f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
875f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
876f9a30166Smatthias.ringwald     }
877038bc64cSmatthias.ringwald     if (err){
8787d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
879038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
880038bc64cSmatthias.ringwald         return err;
881038bc64cSmatthias.ringwald     }
882038bc64cSmatthias.ringwald 
883038bc64cSmatthias.ringwald     // open low-level device
884038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
885038bc64cSmatthias.ringwald     if (err){
8867d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
887f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
888f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
889f9a30166Smatthias.ringwald         }
890038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
891038bc64cSmatthias.ringwald         return err;
892038bc64cSmatthias.ringwald     }
8938d213e1aSmatthias.ringwald     return 0;
8948d213e1aSmatthias.ringwald }
895038bc64cSmatthias.ringwald 
89640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
8978d213e1aSmatthias.ringwald 
8987b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
8999418f9c9Smatthias.ringwald 
9008d213e1aSmatthias.ringwald     // close low-level device
9018d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
9028d213e1aSmatthias.ringwald 
9037b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
9049418f9c9Smatthias.ringwald 
9058d213e1aSmatthias.ringwald     // power off
9068d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
9078d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
9088d213e1aSmatthias.ringwald     }
9099418f9c9Smatthias.ringwald 
9107b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
9119418f9c9Smatthias.ringwald 
91272ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
91372ea5239Smatthias.ringwald }
91472ea5239Smatthias.ringwald 
91540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
91672ea5239Smatthias.ringwald 
9177b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
9183144bce4Smatthias.ringwald 
919b429b9b7Smatthias.ringwald #if 0
920b429b9b7Smatthias.ringwald     // don't close serial port during sleep
921b429b9b7Smatthias.ringwald 
92272ea5239Smatthias.ringwald     // close low-level device
92372ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
924b429b9b7Smatthias.ringwald #endif
92572ea5239Smatthias.ringwald 
92672ea5239Smatthias.ringwald     // sleep mode
9273144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
92872ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
92972ea5239Smatthias.ringwald     }
930b429b9b7Smatthias.ringwald 
93172ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
9328d213e1aSmatthias.ringwald }
9338d213e1aSmatthias.ringwald 
93440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
935b429b9b7Smatthias.ringwald 
9367b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
937b429b9b7Smatthias.ringwald 
938b429b9b7Smatthias.ringwald     // wake on
939b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
940b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
941b429b9b7Smatthias.ringwald     }
942b429b9b7Smatthias.ringwald 
943b429b9b7Smatthias.ringwald #if 0
944b429b9b7Smatthias.ringwald     // open low-level device
945b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
946b429b9b7Smatthias.ringwald     if (err){
9477d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
948b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
949b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
950b429b9b7Smatthias.ringwald         }
951b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
952b429b9b7Smatthias.ringwald         return err;
953b429b9b7Smatthias.ringwald     }
954b429b9b7Smatthias.ringwald #endif
955b429b9b7Smatthias.ringwald 
956b429b9b7Smatthias.ringwald     return 0;
957b429b9b7Smatthias.ringwald }
958b429b9b7Smatthias.ringwald 
959b429b9b7Smatthias.ringwald 
9608d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
961d661ed19Smatthias.ringwald 
9627b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
963d661ed19Smatthias.ringwald 
9648d213e1aSmatthias.ringwald     int err = 0;
9658d213e1aSmatthias.ringwald     switch (hci_stack.state){
9668d213e1aSmatthias.ringwald 
9678d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
9688d213e1aSmatthias.ringwald             switch (power_mode){
9698d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9708d213e1aSmatthias.ringwald                     err = hci_power_control_on();
9718d213e1aSmatthias.ringwald                     if (err) return err;
9727301ad89Smatthias.ringwald                     // set up state machine
9737301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
9747301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9757301ad89Smatthias.ringwald                     hci_stack.substate = 0;
9768d213e1aSmatthias.ringwald                     break;
9778d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9788d213e1aSmatthias.ringwald                     // do nothing
9798d213e1aSmatthias.ringwald                     break;
9808d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
981b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
9828d213e1aSmatthias.ringwald                     break;
9838d213e1aSmatthias.ringwald             }
9848d213e1aSmatthias.ringwald             break;
9857301ad89Smatthias.ringwald 
9868d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9878d213e1aSmatthias.ringwald             switch (power_mode){
9888d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9898d213e1aSmatthias.ringwald                     // do nothing
9908d213e1aSmatthias.ringwald                     break;
9918d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9928d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
9938d213e1aSmatthias.ringwald                     hci_power_control_off();
9948d213e1aSmatthias.ringwald                     break;
9958d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
996b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
99772ea5239Smatthias.ringwald                     hci_power_control_sleep();
9988d213e1aSmatthias.ringwald                     break;
9998d213e1aSmatthias.ringwald             }
10008d213e1aSmatthias.ringwald             break;
10017301ad89Smatthias.ringwald 
10028d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
10038d213e1aSmatthias.ringwald             switch (power_mode){
10048d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10058d213e1aSmatthias.ringwald                     // do nothing
10068d213e1aSmatthias.ringwald                     break;
10078d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1008c7e0c5f6Smatthias.ringwald                     // see hci_run
1009c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10108d213e1aSmatthias.ringwald                     break;
10118d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1012b546ac54Smatthias.ringwald                     // see hci_run
1013b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
101489db417bSmatthias.ringwald                     hci_stack.substate = 0;
10158d213e1aSmatthias.ringwald                     break;
10168d213e1aSmatthias.ringwald             }
10178d213e1aSmatthias.ringwald             break;
10187301ad89Smatthias.ringwald 
10198d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
10208d213e1aSmatthias.ringwald             switch (power_mode){
10218d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10228d213e1aSmatthias.ringwald                     // set up state machine
10238d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10248d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
10258d213e1aSmatthias.ringwald                     break;
10268d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10278d213e1aSmatthias.ringwald                     // do nothing
10288d213e1aSmatthias.ringwald                     break;
10298d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1030b546ac54Smatthias.ringwald                     // see hci_run
1031b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
103289db417bSmatthias.ringwald                     hci_stack.substate = 0;
10338d213e1aSmatthias.ringwald                     break;
10348d213e1aSmatthias.ringwald             }
10358d213e1aSmatthias.ringwald             break;
10368d213e1aSmatthias.ringwald 
10378d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
10388d213e1aSmatthias.ringwald             switch (power_mode){
10398d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
104028171530Smatthias.ringwald 
104128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
104228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
104328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10448747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1045da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
104628171530Smatthias.ringwald                         break;
104728171530Smatthias.ringwald                     }
104828171530Smatthias.ringwald #endif
1049b546ac54Smatthias.ringwald                     // set up state machine
1050b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1051b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1052b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10538d213e1aSmatthias.ringwald                     break;
10548d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1055b546ac54Smatthias.ringwald                     // see hci_run
1056b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10578d213e1aSmatthias.ringwald                     break;
10588d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1059b546ac54Smatthias.ringwald                     // do nothing
10608d213e1aSmatthias.ringwald                     break;
10618d213e1aSmatthias.ringwald             }
10628d213e1aSmatthias.ringwald             break;
10638d213e1aSmatthias.ringwald 
10648d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
10658d213e1aSmatthias.ringwald             switch (power_mode){
10668d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
106728171530Smatthias.ringwald 
106828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
106928171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
107028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10718747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1072da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1073758b46ceSmatthias.ringwald                         hci_update_scan_enable();
107428171530Smatthias.ringwald                         break;
107528171530Smatthias.ringwald                     }
107628171530Smatthias.ringwald #endif
10773144bce4Smatthias.ringwald                     err = hci_power_control_wake();
10783144bce4Smatthias.ringwald                     if (err) return err;
1079b546ac54Smatthias.ringwald                     // set up state machine
1080b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1081b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1082b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10838d213e1aSmatthias.ringwald                     break;
10848d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10854ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10868d213e1aSmatthias.ringwald                     break;
10878d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1088b546ac54Smatthias.ringwald                     // do nothing
10898d213e1aSmatthias.ringwald                     break;
10908d213e1aSmatthias.ringwald             }
10918d213e1aSmatthias.ringwald             break;
109211e23e5fSmatthias.ringwald     }
109368d92d03Smatthias.ringwald 
1094038bc64cSmatthias.ringwald     // create internal event
1095ee8bf225Smatthias.ringwald 	hci_emit_state();
1096ee8bf225Smatthias.ringwald 
109768d92d03Smatthias.ringwald 	// trigger next/first action
109868d92d03Smatthias.ringwald 	hci_run();
109968d92d03Smatthias.ringwald 
1100475c8125Smatthias.ringwald     return 0;
1101475c8125Smatthias.ringwald }
1102475c8125Smatthias.ringwald 
1103758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1104758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1105758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1106758b46ceSmatthias.ringwald     hci_run();
1107758b46ceSmatthias.ringwald }
1108758b46ceSmatthias.ringwald 
1109381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1110381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1111381fbed8Smatthias.ringwald 
1112381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1113381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1114381fbed8Smatthias.ringwald         return;
1115381fbed8Smatthias.ringwald     }
1116381fbed8Smatthias.ringwald 
1117381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1118758b46ceSmatthias.ringwald     hci_update_scan_enable();
1119758b46ceSmatthias.ringwald }
1120b031bebbSmatthias.ringwald 
1121758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1122758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1123758b46ceSmatthias.ringwald 
1124758b46ceSmatthias.ringwald     // don't emit event
1125758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1126758b46ceSmatthias.ringwald 
1127758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1128758b46ceSmatthias.ringwald     hci_update_scan_enable();
1129381fbed8Smatthias.ringwald }
1130381fbed8Smatthias.ringwald 
11315061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
11325061f3afS[email protected]     return &hci_stack.local_bd_addr;
11335061f3afS[email protected] }
11345061f3afS[email protected] 
113506b35ec0Smatthias.ringwald void hci_run(){
11368a485f27Smatthias.ringwald 
113732ab9390Smatthias.ringwald     hci_connection_t * connection;
113832ab9390Smatthias.ringwald     linked_item_t * it;
113932ab9390Smatthias.ringwald 
1140ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1141ce4c8fabSmatthias.ringwald 
1142b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1143b031bebbSmatthias.ringwald 
1144b031bebbSmatthias.ringwald     // decline incoming connections
1145ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1146ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1147ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1148ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1149dbe1a790S[email protected]         return;
1150ce4c8fabSmatthias.ringwald     }
1151ce4c8fabSmatthias.ringwald 
1152b031bebbSmatthias.ringwald     // send scan enable
115392368cd3S[email protected]     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){
1154b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1155b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1156dbe1a790S[email protected]         return;
1157b031bebbSmatthias.ringwald     }
1158b031bebbSmatthias.ringwald 
115932ab9390Smatthias.ringwald     // send pending HCI commands
116032ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
116132ab9390Smatthias.ringwald 
1162b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
116332ab9390Smatthias.ringwald 
116432ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
11657b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
116632ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
116732ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1168dbe1a790S[email protected]             return;
1169c7e0c5f6Smatthias.ringwald         }
1170c7e0c5f6Smatthias.ringwald 
117132ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
117232ab9390Smatthias.ringwald             link_key_t link_key;
11737b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
117432ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
117532ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
117632ab9390Smatthias.ringwald             } else {
117732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
117832ab9390Smatthias.ringwald             }
117928ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
1180dbe1a790S[email protected]             return;
118132ab9390Smatthias.ringwald         }
11821d6b20aeS[email protected] 
1183dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
1184dbe1a790S[email protected]             hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement);
1185dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
1186dbe1a790S[email protected]             return;
118732ab9390Smatthias.ringwald         }
118832ab9390Smatthias.ringwald 
1189dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1190dbe1a790S[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1191dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
1192dbe1a790S[email protected]             return;
1193dbe1a790S[email protected]         }
1194dbe1a790S[email protected] 
1195dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1196dbe1a790S[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1197dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
1198dbe1a790S[email protected]             return;
1199dbe1a790S[email protected]         }
1200dbe1a790S[email protected]     }
1201c7e0c5f6Smatthias.ringwald 
12023429f56bSmatthias.ringwald     switch (hci_stack.state){
12033429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
12047b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
12053429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
12063429f56bSmatthias.ringwald                 // odd: waiting for command completion
120706b35ec0Smatthias.ringwald                 return;
12083429f56bSmatthias.ringwald             }
120990919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1210c7492964Smatthias.ringwald                 case 0: // RESET
121122909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
121224052c2aS[email protected] 
1213c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1214c7492964Smatthias.ringwald                         // skip baud change
1215c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1216c7492964Smatthias.ringwald                     }
12173429f56bSmatthias.ringwald                     break;
1218c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
12197dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
12207dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1221c7492964Smatthias.ringwald                     break;
1222c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1223c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1224c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1225c7492964Smatthias.ringwald                     // break missing here for fall through
1226c7492964Smatthias.ringwald 
1227c7492964Smatthias.ringwald                 case 3:
122824052c2aS[email protected]                     // Custom initialization
1229db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
12307dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1231d62aca9fSmatthias.ringwald                         if (valid_cmd){
12327dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
12337dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1234c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
123590919203Smatthias.ringwald                             break;
123690919203Smatthias.ringwald                         }
1237d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
123890919203Smatthias.ringwald                     }
12392f6c30e1Smatthias.ringwald                     // otherwise continue
1240f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1241f432a6ddSmatthias.ringwald 					break;
1242c7492964Smatthias.ringwald 				case 4:
1243e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1244e2edc0c3Smatthias.ringwald 					break;
1245c7492964Smatthias.ringwald                 case 5:
124665389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
12473429f56bSmatthias.ringwald                     break;
1248c7492964Smatthias.ringwald                 case 6:
1249d7e0e3a8S[email protected]                     if (hci_le_supported()){
1250d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1251d7e0e3a8S[email protected]                     } else {
1252d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1253d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1254d7e0e3a8S[email protected]                     }
1255f5d8d141S[email protected] 
1256f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
125724052c2aS[email protected]                     if (!hci_classic_supported()){
125824052c2aS[email protected]                         if (hci_le_supported()){
1259f5d8d141S[email protected]                             hci_stack.substate = 11 << 1;    // skip all classic command
126024052c2aS[email protected]                         } else {
126124052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
1262d7e0e3a8S[email protected]                             hci_stack.substate = 13 << 1;    // skip all
126324052c2aS[email protected]                         }
1264f5d8d141S[email protected]                     }
1265f5d8d141S[email protected]                     break;
1266f5d8d141S[email protected]                 case 7:
126724052c2aS[email protected]                     if (hci_ssp_supported()){
1268f5d8d141S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1269f5d8d141S[email protected]                         break;
127024052c2aS[email protected]                     }
127124052c2aS[email protected]                     hci_stack.substate += 2;
127224052c2aS[email protected]                     // break missing here for fall through
127324052c2aS[email protected] 
1274f5d8d141S[email protected]                 case 8:
127565389bfcS[email protected]                     // ca. 15 sec
127665389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1277559e517eS[email protected]                     break;
1278f5d8d141S[email protected]                 case 9:
1279e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1280e2386ba1S[email protected]                     break;
1281f5d8d141S[email protected]                 case 10:
1282e2386ba1S[email protected]                     if (hci_stack.local_name){
1283e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1284e2386ba1S[email protected]                     } else {
12858a485f27Smatthias.ringwald                         char hostname[30];
1286e2386ba1S[email protected] #ifdef EMBEDDED
1287e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1288e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1289e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1290e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1291e2386ba1S[email protected] #else
1292e2386ba1S[email protected]                         // hostname for POSIX systems
12938a485f27Smatthias.ringwald                         gethostname(hostname, 30);
12948a485f27Smatthias.ringwald                         hostname[29] = '\0';
1295e2386ba1S[email protected] #endif
12968a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
12978a485f27Smatthias.ringwald                     }
12983c4d4b90Smatthias.ringwald                     break;
1299e0dbca83S[email protected]                 case 11:
1300a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
130124052c2aS[email protected]                     if (!hci_le_supported()){
130224052c2aS[email protected]                         // SKIP LE init for Classic only configuration
130324052c2aS[email protected]                         hci_stack.substate = 13 << 1;
130424052c2aS[email protected]                     }
1305a45d6b9fS[email protected] 					break;
130624052c2aS[email protected] 
130743aa7007S[email protected] #ifdef HAVE_BLE
130824052c2aS[email protected]                 // LE INIT
1309a45d6b9fS[email protected]                 case 12:
131024052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
131124052c2aS[email protected]                     break;
131224052c2aS[email protected]                 case 13:
131324052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
131424052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
131524052c2aS[email protected]                     break;
131643aa7007S[email protected] #endif
131724052c2aS[email protected] 
131824052c2aS[email protected]                 // DONE
131924052c2aS[email protected]                 case 14:
13203429f56bSmatthias.ringwald                     // done.
13213429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1322b360b6adSmatthias.ringwald                     hci_emit_state();
13233429f56bSmatthias.ringwald                     break;
13243429f56bSmatthias.ringwald                 default:
13253429f56bSmatthias.ringwald                     break;
1326475c8125Smatthias.ringwald             }
13273429f56bSmatthias.ringwald             hci_stack.substate++;
13283429f56bSmatthias.ringwald             break;
1329c7e0c5f6Smatthias.ringwald 
1330c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1331c7e0c5f6Smatthias.ringwald 
13327b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1333c7e0c5f6Smatthias.ringwald             // close all open connections
1334c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1335c7e0c5f6Smatthias.ringwald             if (connection){
133632ab9390Smatthias.ringwald 
1337c7e0c5f6Smatthias.ringwald                 // send disconnect
133832ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
133932ab9390Smatthias.ringwald 
134079bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
13416ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1342c7e0c5f6Smatthias.ringwald 
1343c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1344c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1345c7e0c5f6Smatthias.ringwald                 return;
1346c7e0c5f6Smatthias.ringwald             }
13477b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1348c7e0c5f6Smatthias.ringwald 
134972ea5239Smatthias.ringwald             // switch mode
1350c7e0c5f6Smatthias.ringwald             hci_power_control_off();
13519418f9c9Smatthias.ringwald 
13527b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
135372ea5239Smatthias.ringwald             hci_emit_state();
13547b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
135572ea5239Smatthias.ringwald             break;
1356c7e0c5f6Smatthias.ringwald 
135772ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
135889db417bSmatthias.ringwald             switch(hci_stack.substate) {
135989db417bSmatthias.ringwald                 case 0:
13607b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
136172ea5239Smatthias.ringwald                     // close all open connections
136272ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
136366da7044Smatthias.ringwald 
136428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
136566da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
136666da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
136766da7044Smatthias.ringwald                         connection = NULL;
136866da7044Smatthias.ringwald                     }
136966da7044Smatthias.ringwald #endif
137072ea5239Smatthias.ringwald                     if (connection){
137132ab9390Smatthias.ringwald 
137272ea5239Smatthias.ringwald                         // send disconnect
137332ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
137432ab9390Smatthias.ringwald 
137579bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
13766ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
137772ea5239Smatthias.ringwald 
137872ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
137972ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
138072ea5239Smatthias.ringwald                         return;
138172ea5239Smatthias.ringwald                     }
138272ea5239Smatthias.ringwald 
138392368cd3S[email protected]                     if (hci_classic_supported()){
138489db417bSmatthias.ringwald                         // disable page and inquiry scan
138532ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
138632ab9390Smatthias.ringwald 
138792368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
1388758b46ceSmatthias.ringwald                         hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
138989db417bSmatthias.ringwald 
139089db417bSmatthias.ringwald                         // continue in next sub state
139189db417bSmatthias.ringwald                         hci_stack.substate++;
139289db417bSmatthias.ringwald                         break;
139392368cd3S[email protected]                     }
139492368cd3S[email protected]                     // fall through for ble-only chips
139592368cd3S[email protected] 
139689db417bSmatthias.ringwald                 case 2:
13977b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
139828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
139928171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
140028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
140128171530Smatthias.ringwald                         // SLEEP MODE reached
140228171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
140328171530Smatthias.ringwald                         hci_emit_state();
140428171530Smatthias.ringwald                         break;
140528171530Smatthias.ringwald                     }
140628171530Smatthias.ringwald #endif
140772ea5239Smatthias.ringwald                     // switch mode
140889db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1409c7e0c5f6Smatthias.ringwald                     hci_emit_state();
141028171530Smatthias.ringwald                     break;
141128171530Smatthias.ringwald 
141289db417bSmatthias.ringwald                 default:
141389db417bSmatthias.ringwald                     break;
141489db417bSmatthias.ringwald             }
1415c7e0c5f6Smatthias.ringwald             break;
1416c7e0c5f6Smatthias.ringwald 
14173429f56bSmatthias.ringwald         default:
14183429f56bSmatthias.ringwald             break;
14191f504dbdSmatthias.ringwald     }
14203429f56bSmatthias.ringwald }
142116833f0aSmatthias.ringwald 
142231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1423c8e4258aSmatthias.ringwald     bd_addr_t addr;
1424c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1425c8e4258aSmatthias.ringwald     // house-keeping
1426c8e4258aSmatthias.ringwald 
1427c8e4258aSmatthias.ringwald     // create_connection?
1428c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1429c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1430339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1431c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1432c8e4258aSmatthias.ringwald         if (conn) {
1433c8e4258aSmatthias.ringwald             // if connection exists
1434c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
143517f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
143617f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1437c8e4258aSmatthias.ringwald             }
143817f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1439c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1440c8e4258aSmatthias.ringwald 
144117f1ba2aSmatthias.ringwald         }
1442c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
144317f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
144417f1ba2aSmatthias.ringwald         if (!conn){
144517f1ba2aSmatthias.ringwald             // notify client that alloc failed
144617f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
144717f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
144817f1ba2aSmatthias.ringwald         }
1449c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1450c8e4258aSmatthias.ringwald     }
1451c8e4258aSmatthias.ringwald 
14527fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
14537fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
14547fde4af9Smatthias.ringwald     }
14557fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
14567fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
14577fde4af9Smatthias.ringwald     }
14587fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
14597fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
14607fde4af9Smatthias.ringwald     }
14617fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
14627fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
14637fde4af9Smatthias.ringwald     }
14647fde4af9Smatthias.ringwald 
14658ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
14668ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
14678ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
14688ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
14698ef73945Smatthias.ringwald         }
14708ef73945Smatthias.ringwald     }
1471c8e4258aSmatthias.ringwald 
147231452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1473622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
147431452debSmatthias.ringwald }
14758adf0ddaSmatthias.ringwald 
1476dbe1a790S[email protected] // Configure Secure Simple Pairing
1477dbe1a790S[email protected] 
1478dbe1a790S[email protected] // enable will enable SSP during init
1479dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
1480dbe1a790S[email protected]     hci_stack.ssp_enable = enable;
1481dbe1a790S[email protected] }
1482dbe1a790S[email protected] 
1483dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1484dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
1485dbe1a790S[email protected]     hci_stack.ssp_io_capability = io_capability;
1486dbe1a790S[email protected] }
1487dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
1488dbe1a790S[email protected]     hci_stack.ssp_authentication_requirement = authentication_requirement;
1489dbe1a790S[email protected] }
1490dbe1a790S[email protected] 
1491dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1492dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
1493dbe1a790S[email protected]     hci_stack.ssp_auto_accept = auto_accept;
1494dbe1a790S[email protected] }
1495dbe1a790S[email protected] 
14961cd208adSmatthias.ringwald /**
14971cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
14981cd208adSmatthias.ringwald  */
1499fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
15001cd208adSmatthias.ringwald     va_list argptr;
15011cd208adSmatthias.ringwald     va_start(argptr, cmd);
15027dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
15031cd208adSmatthias.ringwald     va_end(argptr);
15047dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
150593b8dc03Smatthias.ringwald }
1506c8e4258aSmatthias.ringwald 
1507ee091cf1Smatthias.ringwald // Create various non-HCI events.
1508ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1509ee091cf1Smatthias.ringwald 
1510c8e4258aSmatthias.ringwald void hci_emit_state(){
1511e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1512425d1371Smatthias.ringwald     uint8_t event[3];
151380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1514425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1515c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1516425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1517425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1518c8e4258aSmatthias.ringwald }
1519c8e4258aSmatthias.ringwald 
152017f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1521425d1371Smatthias.ringwald     uint8_t event[13];
1522c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1523425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
152417f1ba2aSmatthias.ringwald     event[2] = status;
1525c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1526c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1527c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1528c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1529425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1530425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1531c8e4258aSmatthias.ringwald }
1532c8e4258aSmatthias.ringwald 
15333c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1534425d1371Smatthias.ringwald     uint8_t event[6];
15353c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1536e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
15373c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
15383c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
15393c4d4b90Smatthias.ringwald     event[5] = reason;
1540425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1541425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15423c4d4b90Smatthias.ringwald }
15433c4d4b90Smatthias.ringwald 
1544ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1545e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1546425d1371Smatthias.ringwald     uint8_t event[4];
154780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1548425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1549ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1550425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1551425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1552ee091cf1Smatthias.ringwald }
155343bfb1bdSmatthias.ringwald 
155443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1555e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1556425d1371Smatthias.ringwald     uint8_t event[3];
155780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1558425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
155943bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1560425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1561425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
156243bfb1bdSmatthias.ringwald }
1563038bc64cSmatthias.ringwald 
1564038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1565e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1566425d1371Smatthias.ringwald     uint8_t event[2];
156780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1568425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1569425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1570425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1571038bc64cSmatthias.ringwald }
15721b0e3922Smatthias.ringwald 
157309ba8edeSmatthias.ringwald #ifndef EMBEDDED
15741b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1575e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1576425d1371Smatthias.ringwald     uint8_t event[6];
15771b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1578425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1579425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1580425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1581425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1582425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1583425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15841b0e3922Smatthias.ringwald }
158509ba8edeSmatthias.ringwald #endif
15861b0e3922Smatthias.ringwald 
15872ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1588e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1589425d1371Smatthias.ringwald     uint8_t event[3];
15902ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1591425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
15922ed6235cSmatthias.ringwald     event[2] = enabled;
1593425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1594e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15952ed6235cSmatthias.ringwald }
1596627c2f45Smatthias.ringwald 
1597627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1598e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1599627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1600e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1601f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
16022f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1603f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1604e0abb8e7S[email protected] 
1605e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1606e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1607e0abb8e7S[email protected] 
1608e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1609e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1610627c2f45Smatthias.ringwald }
1611381fbed8Smatthias.ringwald 
1612381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1613e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1614425d1371Smatthias.ringwald     uint8_t event[3];
1615381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1616425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1617381fbed8Smatthias.ringwald     event[2] = enabled;
1618425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1619425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1620381fbed8Smatthias.ringwald }
1621