xref: /btstack/src/hci.c (revision 1eb2563e8a879e8d115d41616c44ad3f5871ea12)
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"
474c57c146S[email protected] #include "gap.h"
487f2435e6Smatthias.ringwald 
4993b8dc03Smatthias.ringwald #include <stdarg.h>
5093b8dc03Smatthias.ringwald #include <string.h>
5156fe0872Smatthias.ringwald #include <stdio.h>
527f2435e6Smatthias.ringwald 
53549e6ebeSmatthias.ringwald #ifndef EMBEDDED
54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5509ba8edeSmatthias.ringwald #include <btstack/version.h>
56549e6ebeSmatthias.ringwald #endif
57549e6ebeSmatthias.ringwald 
58a3b02b71Smatthias.ringwald #include "btstack_memory.h"
597f2435e6Smatthias.ringwald #include "debug.h"
60d8905019Smatthias.ringwald #include "hci_dump.h"
6193b8dc03Smatthias.ringwald 
62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
631b0e3922Smatthias.ringwald 
64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
65ee091cf1Smatthias.ringwald 
66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
67da5275c5S[email protected] 
6828171530Smatthias.ringwald #ifdef USE_BLUETOOL
6928171530Smatthias.ringwald #include "bt_control_iphone.h"
7028171530Smatthias.ringwald #endif
7128171530Smatthias.ringwald 
72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
74758b46ceSmatthias.ringwald 
7506b35ec0Smatthias.ringwald // the STACK is here
7616833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
7716833f0aSmatthias.ringwald 
7897addcc5Smatthias.ringwald /**
79ee091cf1Smatthias.ringwald  * get connection for a given handle
80ee091cf1Smatthias.ringwald  *
81ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
82ee091cf1Smatthias.ringwald  */
835061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
84ee091cf1Smatthias.ringwald     linked_item_t *it;
85ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
86ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
87ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
88ee091cf1Smatthias.ringwald         }
89ee091cf1Smatthias.ringwald     }
90ee091cf1Smatthias.ringwald     return NULL;
91ee091cf1Smatthias.ringwald }
92ee091cf1Smatthias.ringwald 
932b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
9428ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
95c785ef68Smatthias.ringwald #ifdef HAVE_TIME
96ee091cf1Smatthias.ringwald     struct timeval tv;
97ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
98c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
99ee091cf1Smatthias.ringwald         // connections might be timed out
100ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
101ee091cf1Smatthias.ringwald     }
1022b12a0b9Smatthias.ringwald #endif
103e5780900Smatthias.ringwald #ifdef HAVE_TICK
104c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
105c785ef68Smatthias.ringwald         // connections might be timed out
106c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
107c785ef68Smatthias.ringwald     }
108c785ef68Smatthias.ringwald #endif
109c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
110c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
111c785ef68Smatthias.ringwald }
112ee091cf1Smatthias.ringwald 
113ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
114c7492964Smatthias.ringwald #ifdef HAVE_TIME
115ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
116c7492964Smatthias.ringwald #endif
117e5780900Smatthias.ringwald #ifdef HAVE_TICK
118c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
119c785ef68Smatthias.ringwald #endif
120ee091cf1Smatthias.ringwald }
121ee091cf1Smatthias.ringwald 
122ee091cf1Smatthias.ringwald /**
123c8e4258aSmatthias.ringwald  * create connection for given address
124c8e4258aSmatthias.ringwald  *
12517f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
126c8e4258aSmatthias.ringwald  */
127c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
12828ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
129c8e4258aSmatthias.ringwald     if (!conn) return NULL;
130c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
131c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1327d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
133afd4e962S[email protected]     conn->bonding_flags = 0;
13434d2123cS[email protected]     conn->requested_security_level = LEVEL_0;
135ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
136ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
137ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
138d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1397856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
14056cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
141c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
142c8e4258aSmatthias.ringwald     return conn;
143c8e4258aSmatthias.ringwald }
144c8e4258aSmatthias.ringwald 
145c8e4258aSmatthias.ringwald /**
14606b35ec0Smatthias.ringwald  * get connection for given address
14797addcc5Smatthias.ringwald  *
14897addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
14997addcc5Smatthias.ringwald  */
150fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
15106b35ec0Smatthias.ringwald     linked_item_t *it;
15206b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
15306b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
15406b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
15506b35ec0Smatthias.ringwald         }
15606b35ec0Smatthias.ringwald     }
15706b35ec0Smatthias.ringwald     return NULL;
15806b35ec0Smatthias.ringwald }
15906b35ec0Smatthias.ringwald 
16028ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(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] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
16528ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
16628ca2b46S[email protected] }
16728ca2b46S[email protected] 
16828ca2b46S[email protected] 
16943bfb1bdSmatthias.ringwald /**
17080ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1717fde4af9Smatthias.ringwald  */
1727fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1737fde4af9Smatthias.ringwald     bd_addr_t addr;
1747fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1757fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1767fde4af9Smatthias.ringwald     if (conn) {
17728ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
17880ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1797fde4af9Smatthias.ringwald     }
1807fde4af9Smatthias.ringwald }
1817fde4af9Smatthias.ringwald 
18280ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
1835061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
18480ca58a0Smatthias.ringwald     if (!conn) return 0;
18580ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
18680ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
18780ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
18880ca58a0Smatthias.ringwald     return 1;
18980ca58a0Smatthias.ringwald }
19080ca58a0Smatthias.ringwald 
191c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
192c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
193c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
194c12e46e7Smatthias.ringwald     }
195c12e46e7Smatthias.ringwald }
196c12e46e7Smatthias.ringwald 
1977fde4af9Smatthias.ringwald 
1987fde4af9Smatthias.ringwald /**
19943bfb1bdSmatthias.ringwald  * count connections
20043bfb1bdSmatthias.ringwald  */
20140d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
20256c253c9Smatthias.ringwald     int count = 0;
20343bfb1bdSmatthias.ringwald     linked_item_t *it;
20443bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
20543bfb1bdSmatthias.ringwald     return count;
20643bfb1bdSmatthias.ringwald }
207c8e4258aSmatthias.ringwald 
20897addcc5Smatthias.ringwald /**
209ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
21016833f0aSmatthias.ringwald  */
2112718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
21216833f0aSmatthias.ringwald }
21316833f0aSmatthias.ringwald 
214998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2155061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
216998906cdSmatthias.ringwald     if (!connection) {
2177d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
218998906cdSmatthias.ringwald         return 0;
219998906cdSmatthias.ringwald     }
220998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
221998906cdSmatthias.ringwald }
222998906cdSmatthias.ringwald 
223998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
224998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
225998906cdSmatthias.ringwald     linked_item_t *it;
226998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
227998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
228998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2297d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
230998906cdSmatthias.ringwald             return 0;
231998906cdSmatthias.ringwald         }
232998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
233998906cdSmatthias.ringwald     }
234998906cdSmatthias.ringwald     return free_slots;
235998906cdSmatthias.ringwald }
236998906cdSmatthias.ringwald 
237c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2382b12a0b9Smatthias.ringwald 
2392b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2402b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2412b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2422b12a0b9Smatthias.ringwald             return 0;
2432b12a0b9Smatthias.ringwald         }
2442b12a0b9Smatthias.ringwald     }
2452b12a0b9Smatthias.ringwald 
2462b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
247c24735b1Smatthias.ringwald     switch (packet_type) {
248c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
249c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
250c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
251de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
252c24735b1Smatthias.ringwald         default:
253c24735b1Smatthias.ringwald             return 0;
254c24735b1Smatthias.ringwald     }
255c24735b1Smatthias.ringwald }
256c24735b1Smatthias.ringwald 
257ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2587856c818Smatthias.ringwald 
2596218e6f1Smatthias.ringwald     // check for free places on BT module
2605932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2616218e6f1Smatthias.ringwald 
2627856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2635061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
26456cf178bSmatthias.ringwald     if (!connection) return 0;
26556cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
26656cf178bSmatthias.ringwald 
26756cf178bSmatthias.ringwald     // count packet
26856cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2697b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2707856c818Smatthias.ringwald 
27100d8e42eSmatthias.ringwald     // send packet
27200d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2736218e6f1Smatthias.ringwald 
27400d8e42eSmatthias.ringwald     return err;
275ee091cf1Smatthias.ringwald }
276ee091cf1Smatthias.ringwald 
27716833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2787856c818Smatthias.ringwald 
279e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
280e76a89eeS[email protected] 
2817856c818Smatthias.ringwald     // get info
2827856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2835061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
2847856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2857856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2867856c818Smatthias.ringwald 
2877856c818Smatthias.ringwald     // ignore non-registered handle
2887856c818Smatthias.ringwald     if (!conn){
2897d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2907856c818Smatthias.ringwald         return;
2917856c818Smatthias.ringwald     }
2927856c818Smatthias.ringwald 
293e76a89eeS[email protected]     // assert packet is complete
2949ecc3e17S[email protected]     if (acl_length + 4 != size){
295e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
296e76a89eeS[email protected]         return;
297e76a89eeS[email protected]     }
298e76a89eeS[email protected] 
2997856c818Smatthias.ringwald     // update idle timestamp
3007856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3017856c818Smatthias.ringwald 
3027856c818Smatthias.ringwald     // handle different packet types
3037856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3047856c818Smatthias.ringwald 
3057856c818Smatthias.ringwald         case 0x01: // continuation fragment
3067856c818Smatthias.ringwald 
3077856c818Smatthias.ringwald             // sanity check
3087856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3097d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3107856c818Smatthias.ringwald                 return;
3117856c818Smatthias.ringwald             }
3127856c818Smatthias.ringwald 
3137856c818Smatthias.ringwald             // append fragment payload (header already stored)
3147856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3157856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3167856c818Smatthias.ringwald 
3177d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
318decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3197856c818Smatthias.ringwald 
3207856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3217856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3227856c818Smatthias.ringwald 
3232718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3247856c818Smatthias.ringwald                 // reset recombination buffer
3257856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3267856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3277856c818Smatthias.ringwald             }
3287856c818Smatthias.ringwald             break;
3297856c818Smatthias.ringwald 
3307856c818Smatthias.ringwald         case 0x02: { // first fragment
3317856c818Smatthias.ringwald 
3327856c818Smatthias.ringwald             // sanity check
3337856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3347d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3357856c818Smatthias.ringwald                 return;
3367856c818Smatthias.ringwald             }
3377856c818Smatthias.ringwald 
3387856c818Smatthias.ringwald             // peek into L2CAP packet!
3397856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3407856c818Smatthias.ringwald 
341e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
342decc01a8Smatthias.ringwald 
3437856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3447856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3457856c818Smatthias.ringwald 
3467856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3472718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3487856c818Smatthias.ringwald 
3497856c818Smatthias.ringwald             } else {
3507856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3517856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3527856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3537856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
354decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3557856c818Smatthias.ringwald             }
3567856c818Smatthias.ringwald             break;
3577856c818Smatthias.ringwald 
3587856c818Smatthias.ringwald         }
3597856c818Smatthias.ringwald         default:
3607d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3617856c818Smatthias.ringwald             return;
3627856c818Smatthias.ringwald     }
36394ab26f8Smatthias.ringwald 
36494ab26f8Smatthias.ringwald     // execute main loop
36594ab26f8Smatthias.ringwald     hci_run();
36616833f0aSmatthias.ringwald }
36722909952Smatthias.ringwald 
36867a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
369339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3703c4d4b90Smatthias.ringwald 
3713c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3723c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3733c4d4b90Smatthias.ringwald 
374c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
375c785ef68Smatthias.ringwald 
376c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
377a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3783c4d4b90Smatthias.ringwald 
3793c4d4b90Smatthias.ringwald     // now it's gone
380c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
381c7e0c5f6Smatthias.ringwald }
382c7e0c5f6Smatthias.ringwald 
3830c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3848f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3858f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3868f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3878f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3888f8108aaSmatthias.ringwald };
38965389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
39065389bfcS[email protected]      0, // 3 slot packets
39165389bfcS[email protected]      1, // 5 slot packets
39265389bfcS[email protected]     25, // EDR 2 mpbs
39365389bfcS[email protected]     26, // EDR 3 mbps
39465389bfcS[email protected]     39, // 3 slot EDR packts
39565389bfcS[email protected]     40, // 5 slot EDR packet
39665389bfcS[email protected] };
39765389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
39865389bfcS[email protected]     0x0f00, // 3 slot packets
39965389bfcS[email protected]     0xf000, // 5 slot packets
40065389bfcS[email protected]     0x1102, // EDR 2 mpbs
40165389bfcS[email protected]     0x2204, // EDR 3 mbps
40265389bfcS[email protected]     0x0300, // 3 slot EDR packts
40365389bfcS[email protected]     0x3000, // 5 slot EDR packet
40465389bfcS[email protected] };
4058f8108aaSmatthias.ringwald 
40665389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
40765389bfcS[email protected]     // enable packet types based on size
4088f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4098f8108aaSmatthias.ringwald     int i;
4108f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4118f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4128f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4138f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4148f8108aaSmatthias.ringwald         }
4158f8108aaSmatthias.ringwald     }
41665389bfcS[email protected]     // disable packet types due to missing local supported features
41765389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
41865389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
41965389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
42065389bfcS[email protected]         if (feature_set) continue;
42165389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
42265389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
42365389bfcS[email protected]     }
4248f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4258f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4268f8108aaSmatthias.ringwald     return packet_types;
4278f8108aaSmatthias.ringwald }
4288f8108aaSmatthias.ringwald 
4298f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4308f8108aaSmatthias.ringwald     return hci_stack.packet_types;
4318f8108aaSmatthias.ringwald }
4328f8108aaSmatthias.ringwald 
4337dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4347dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4357dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
4367dc17943Smatthias.ringwald }
4377dc17943Smatthias.ringwald 
438f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4397dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
4407dc17943Smatthias.ringwald }
4417dc17943Smatthias.ringwald 
442f5d8d141S[email protected] int hci_ssp_supported(void){
443f5d8d141S[email protected]     // No 51, byte 6, bit 3
444f5d8d141S[email protected]     return (hci_stack.local_supported_features[6] & (1 << 3)) != 0;
445f5d8d141S[email protected] }
446f5d8d141S[email protected] 
447f5d8d141S[email protected] int hci_classic_supported(void){
448f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
449f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 5)) == 0;
450f5d8d141S[email protected] }
451f5d8d141S[email protected] 
452f5d8d141S[email protected] int hci_le_supported(void){
453f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
454f5d8d141S[email protected] #ifdef HAVE_BLE
455f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 6)) != 0;
456f5d8d141S[email protected] #else
457f5d8d141S[email protected]     return 0;
458f5d8d141S[email protected] #endif
459f5d8d141S[email protected] }
460f5d8d141S[email protected] 
46169a97523S[email protected] // get addr type and address used in advertisement packets
46218904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
46369a97523S[email protected]     *addr_type = hci_stack.adv_addr_type;
46469a97523S[email protected]     if (hci_stack.adv_addr_type){
46569a97523S[email protected]         memcpy(addr, hci_stack.adv_address, 6);
46669a97523S[email protected]     } else {
46769a97523S[email protected]         memcpy(addr, hci_stack.local_bd_addr, 6);
46869a97523S[email protected]     }
46969a97523S[email protected] }
47069a97523S[email protected] 
47174ec757aSmatthias.ringwald // avoid huge local variables
472223aafc1Smatthias.ringwald #ifndef EMBEDDED
47374ec757aSmatthias.ringwald static device_name_t device_name;
474223aafc1Smatthias.ringwald #endif
47516833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
476e76a89eeS[email protected] 
477e76a89eeS[email protected]     uint16_t event_length = packet[1];
478e76a89eeS[email protected] 
479e76a89eeS[email protected]     // assert packet is complete
480e76a89eeS[email protected]     if (size != event_length + 2){
481e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
482e76a89eeS[email protected]         return;
483e76a89eeS[email protected]     }
484e76a89eeS[email protected] 
4851281a47eSmatthias.ringwald     bd_addr_t addr;
4862d00edd4Smatthias.ringwald     uint8_t link_type;
487fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
4881f7b95a1Smatthias.ringwald     hci_connection_t * conn;
48956cf178bSmatthias.ringwald     int i;
49022909952Smatthias.ringwald 
4915909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4925909f7f2Smatthias.ringwald 
4936772a24cSmatthias.ringwald     switch (packet[0]) {
49422909952Smatthias.ringwald 
4956772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4967ec5eeaaSmatthias.ringwald             // get num cmd packets
4977b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4987ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4997ec5eeaaSmatthias.ringwald 
500e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
501e2edc0c3Smatthias.ringwald                 // from offset 5
502e2edc0c3Smatthias.ringwald                 // status
5031d279b20Smatthias.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"
504e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
50556cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
506e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
50756cf178bSmatthias.ringwald                 // ignore: total num SCO packets
50856cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
509a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5108fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
5118fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
5128f8108aaSmatthias.ringwald                     }
51365389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
51465389bfcS[email protected]                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
515e2edc0c3Smatthias.ringwald                 }
51656cf178bSmatthias.ringwald             }
51765a46ef3S[email protected] #ifdef HAVE_BLE
51865a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
51965a46ef3S[email protected]                 hci_stack.le_data_packet_length = READ_BT_16(packet, 6);
52065a46ef3S[email protected]                 hci_stack.total_num_le_packets  = packet[8];
52165a46ef3S[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);
52265a46ef3S[email protected]             }
52365a46ef3S[email protected] #endif
524188981d2Smatthias.ringwald             // Dump local address
525188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
526e2386ba1S[email protected]                 bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
527188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
528e2386ba1S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr));
529188981d2Smatthias.ringwald             }
530381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
531381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
532381fbed8Smatthias.ringwald             }
53365389bfcS[email protected]             // Note: HCI init checks
534559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
53565389bfcS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
536559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
537559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
538559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
539559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
540559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
54165389bfcS[email protected] 
54265389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
54365389bfcS[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]);
544f5d8d141S[email protected]                 log_info("packet types %04x", hci_stack.packet_types);
545f5d8d141S[email protected] 
546f5d8d141S[email protected]                 // Classic/LE
547f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
548559e517eS[email protected]             }
54956cf178bSmatthias.ringwald             break;
55056cf178bSmatthias.ringwald 
5517ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5527ec5eeaaSmatthias.ringwald             // get num cmd packets
5537b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
5547ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
5557ec5eeaaSmatthias.ringwald             break;
5567ec5eeaaSmatthias.ringwald 
55756cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
55856cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
55956cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
56056cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
5615061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
56256cf178bSmatthias.ringwald                 if (!conn){
5637d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
56456cf178bSmatthias.ringwald                     continue;
56556cf178bSmatthias.ringwald                 }
56656cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5677b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
56856cf178bSmatthias.ringwald             }
5696772a24cSmatthias.ringwald             break;
5706772a24cSmatthias.ringwald 
5711f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
57237eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
57337eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5742d00edd4Smatthias.ringwald             link_type = packet[11];
575339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
57637eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5771f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5781f7b95a1Smatthias.ringwald                 if (!conn) {
5791f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5801f7b95a1Smatthias.ringwald                 }
581ce4c8fabSmatthias.ringwald                 if (!conn) {
582ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
583ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
584ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
585ce4c8fabSmatthias.ringwald                     break;
586ce4c8fabSmatthias.ringwald                 }
58732ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
58832ab9390Smatthias.ringwald                 hci_run();
58937eaa4cfSmatthias.ringwald             } else {
590ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
591ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
592ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
59337eaa4cfSmatthias.ringwald             }
5941f7b95a1Smatthias.ringwald             break;
5951f7b95a1Smatthias.ringwald 
5966772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
597fe1ed1b8Smatthias.ringwald             // Connection management
598fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
599339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
6001f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
601fe1ed1b8Smatthias.ringwald             if (conn) {
602b448a0e7Smatthias.ringwald                 if (!packet[2]){
603c8e4258aSmatthias.ringwald                     conn->state = OPEN;
604fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
605afd4e962S[email protected]                     conn->bonding_flags = BONDING_REQUEST_REMOTE_FEATURES;
606ee091cf1Smatthias.ringwald 
607c785ef68Smatthias.ringwald                     // restart timer
608c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
609ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
610c785ef68Smatthias.ringwald 
611339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
61243bfb1bdSmatthias.ringwald 
61343bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
614b448a0e7Smatthias.ringwald                 } else {
615b448a0e7Smatthias.ringwald                     // connection failed, remove entry
616b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
617a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
618c12e46e7Smatthias.ringwald 
619c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
620c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
621c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
622c12e46e7Smatthias.ringwald                     }
623fe1ed1b8Smatthias.ringwald                 }
624fe1ed1b8Smatthias.ringwald             }
6256772a24cSmatthias.ringwald             break;
626fe1ed1b8Smatthias.ringwald 
627afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
628afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
629afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
630afd4e962S[email protected]             if (!conn) break;
631afd4e962S[email protected]             if (!packet[2]){
632afd4e962S[email protected]                 uint8_t * features = &packet[5];
633afd4e962S[email protected]                 if (features[6] & (1 << 3)){
634afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
635afd4e962S[email protected]                 }
636afd4e962S[email protected]             }
637afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
638afd4e962S[email protected]             break;
639afd4e962S[email protected] 
6407fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
6417b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
6427fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
64374d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
64474d716b5S[email protected]             if (hci_stack.bondable && !hci_stack.remote_device_db) break;
64532ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
64664472d52Smatthias.ringwald             hci_run();
647d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
64829d53098Smatthias.ringwald             return;
6497fde4af9Smatthias.ringwald 
6509ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
65129d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
6529ab95c90S[email protected]             conn = connection_for_address(addr);
6539ab95c90S[email protected]             if (!conn) break;
6549ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
6559ab95c90S[email protected]             link_key_type_t link_key_type = packet[24];
6569ab95c90S[email protected]             // Change Connection Encryption keeps link key type
6579ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
6589ab95c90S[email protected]                 conn->link_key_type = link_key_type;
6599ab95c90S[email protected]             }
6609ab95c90S[email protected]             if (!hci_stack.remote_device_db) break;
6619ab95c90S[email protected]             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
66229d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
6637fde4af9Smatthias.ringwald             break;
6649ab95c90S[email protected]         }
6657fde4af9Smatthias.ringwald 
6667fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
6677fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
6684c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
6694c57c146S[email protected]             if (!hci_stack.bondable){
6704c57c146S[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_PIN_CODE_REQUEST);
671f8fb5f6eS[email protected]                 hci_run();
672f8fb5f6eS[email protected]                 return;
6734c57c146S[email protected]             }
674d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
675d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
676d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
677d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
6787fde4af9Smatthias.ringwald             break;
6797fde4af9Smatthias.ringwald 
6801d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
6811d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
682dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
683dbe1a790S[email protected]             break;
684dbe1a790S[email protected] 
685dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
686dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_CONFIRM_REQUEST);
687dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
688dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
689dbe1a790S[email protected]             break;
690dbe1a790S[email protected] 
691dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
692dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_PASSKEY_REQUEST);
693dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
694dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
6951d6b20aeS[email protected]             break;
6961d6b20aeS[email protected] 
697f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
698f0944df2S[email protected]             if (packet[2]) break;   // error status
699f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
700f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
701f0944df2S[email protected]             if (!conn) break;
702f0944df2S[email protected]             if (packet[5]){
703f0944df2S[email protected]                 conn->authentication_flags |= CONNECTION_ENCRYPTED;
704f0944df2S[email protected]             } else {
705536f9994S[email protected]                 conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
706f0944df2S[email protected]             }
707a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
708f0944df2S[email protected]             break;
709f0944df2S[email protected] 
710*1eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
711*1eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
712*1eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
713*1eb2563eS[email protected]             if (!conn) break;
714*1eb2563eS[email protected]             if (gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
715*1eb2563eS[email protected]                 // link key sufficient for requested security
716*1eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
717*1eb2563eS[email protected]                 return;
718*1eb2563eS[email protected]             } else {
719*1eb2563eS[email protected]                 // not enough
720*1eb2563eS[email protected]                hci_emit_security_level(handle, gap_security_level_for_connection(conn));
721*1eb2563eS[email protected]             }
722*1eb2563eS[email protected]             break;
72334d2123cS[email protected] 
724223aafc1Smatthias.ringwald #ifndef EMBEDDED
72574ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
72674ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
72774ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
72874ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
7295a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
7305a06394aSmatthias.ringwald             for (i=0; i<248;i++){
7315a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
7325a06394aSmatthias.ringwald                     packet[9+i] = 0;
7335a06394aSmatthias.ringwald                     break;
734cdc9101dSmatthias.ringwald                 }
7355a06394aSmatthias.ringwald             }
736d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
73774ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
73874ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
73974ec757aSmatthias.ringwald             break;
74074ec757aSmatthias.ringwald 
74174ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
74274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
74374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
74474ec757aSmatthias.ringwald             // first send inq result packet
74574ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
74674ec757aSmatthias.ringwald             // then send cached remote names
74774ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
74874ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
74974ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
75074ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
75174ec757aSmatthias.ringwald                 }
75274ec757aSmatthias.ringwald             }
75374ec757aSmatthias.ringwald             return;
754223aafc1Smatthias.ringwald #endif
75574ec757aSmatthias.ringwald 
7566772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
757fe1ed1b8Smatthias.ringwald             if (!packet[2]){
758fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
7595061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
760fe1ed1b8Smatthias.ringwald                 if (conn) {
761c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
762fe1ed1b8Smatthias.ringwald                 }
763fe1ed1b8Smatthias.ringwald             }
7646772a24cSmatthias.ringwald             break;
7656772a24cSmatthias.ringwald 
766c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
7676c062428S[email protected]             if(hci_stack.control && hci_stack.control->hw_error){
768c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
769c68bdf90Smatthias.ringwald             }
770c68bdf90Smatthias.ringwald             break;
771c68bdf90Smatthias.ringwald 
7725909f7f2Smatthias.ringwald #ifdef HAVE_BLE
7735909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
7745909f7f2Smatthias.ringwald             switch (packet[2]) {
7755909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
7765909f7f2Smatthias.ringwald                     // Connection management
7775909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
7785909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
7795909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
7805909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
7815909f7f2Smatthias.ringwald                     if (packet[3]){
7825909f7f2Smatthias.ringwald                         if (conn){
7835909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
7845909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
7855909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
7865909f7f2Smatthias.ringwald 
7875909f7f2Smatthias.ringwald                         }
7885909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
7895909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
7905909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
7915909f7f2Smatthias.ringwald                         }
7925909f7f2Smatthias.ringwald                         break;
7935909f7f2Smatthias.ringwald                     }
7945909f7f2Smatthias.ringwald                     if (!conn){
7955909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
7965909f7f2Smatthias.ringwald                     }
7975909f7f2Smatthias.ringwald                     if (!conn){
7985909f7f2Smatthias.ringwald                         // no memory
7995909f7f2Smatthias.ringwald                         break;
8005909f7f2Smatthias.ringwald                     }
8015909f7f2Smatthias.ringwald 
8025909f7f2Smatthias.ringwald                     conn->state = OPEN;
8035909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
8045909f7f2Smatthias.ringwald 
8055909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
8065909f7f2Smatthias.ringwald 
8075909f7f2Smatthias.ringwald                     // restart timer
8085909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
8095909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
8105909f7f2Smatthias.ringwald 
8115909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
8125909f7f2Smatthias.ringwald 
8135909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
8145909f7f2Smatthias.ringwald                     break;
8155909f7f2Smatthias.ringwald 
81665a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
81765a46ef3S[email protected] 
8185909f7f2Smatthias.ringwald                 default:
8195909f7f2Smatthias.ringwald                     break;
8205909f7f2Smatthias.ringwald             }
8215909f7f2Smatthias.ringwald             break;
8225909f7f2Smatthias.ringwald #endif
8235909f7f2Smatthias.ringwald 
8246772a24cSmatthias.ringwald         default:
8256772a24cSmatthias.ringwald             break;
826fe1ed1b8Smatthias.ringwald     }
827fe1ed1b8Smatthias.ringwald 
8283429f56bSmatthias.ringwald     // handle BT initialization
8293429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
8303429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
8313429f56bSmatthias.ringwald             // odd: waiting for event
832f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
833c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
834c9af4d3fS[email protected]                 if (hci_stack.substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
8353429f56bSmatthias.ringwald                     hci_stack.substate++;
8363429f56bSmatthias.ringwald                 }
8373429f56bSmatthias.ringwald             }
83822909952Smatthias.ringwald         }
839c9af4d3fS[email protected]     }
84022909952Smatthias.ringwald 
84189db417bSmatthias.ringwald     // help with BT sleep
84289db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
84389db417bSmatthias.ringwald         && hci_stack.substate == 1
84489db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
84589db417bSmatthias.ringwald         hci_stack.substate++;
84689db417bSmatthias.ringwald     }
84789db417bSmatthias.ringwald 
8482718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
84994ab26f8Smatthias.ringwald 
85094ab26f8Smatthias.ringwald 	// execute main loop
85194ab26f8Smatthias.ringwald 	hci_run();
85216833f0aSmatthias.ringwald }
85316833f0aSmatthias.ringwald 
8540a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
85510e830c9Smatthias.ringwald     switch (packet_type) {
85610e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
85710e830c9Smatthias.ringwald             event_handler(packet, size);
85810e830c9Smatthias.ringwald             break;
85910e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
86010e830c9Smatthias.ringwald             acl_handler(packet, size);
86110e830c9Smatthias.ringwald             break;
86210e830c9Smatthias.ringwald         default:
86310e830c9Smatthias.ringwald             break;
86410e830c9Smatthias.ringwald     }
86510e830c9Smatthias.ringwald }
86610e830c9Smatthias.ringwald 
867fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
8682718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
8692718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
87016833f0aSmatthias.ringwald }
87116833f0aSmatthias.ringwald 
8724f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
873475c8125Smatthias.ringwald 
874475c8125Smatthias.ringwald     // reference to use transport layer implementation
87516833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
876475c8125Smatthias.ringwald 
87711e23e5fSmatthias.ringwald     // references to used control implementation
87811e23e5fSmatthias.ringwald     hci_stack.control = control;
87911e23e5fSmatthias.ringwald 
88011e23e5fSmatthias.ringwald     // reference to used config
88111e23e5fSmatthias.ringwald     hci_stack.config = config;
88211e23e5fSmatthias.ringwald 
883fe1ed1b8Smatthias.ringwald     // no connections yet
884fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
8850a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
886c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
887458bf4e8S[email protected]     hci_stack.bondable = 1;
888758b46ceSmatthias.ringwald 
889b031bebbSmatthias.ringwald     // no pending cmds
890b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
891b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
892b031bebbSmatthias.ringwald 
89316833f0aSmatthias.ringwald     // higher level handler
8942718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
89516833f0aSmatthias.ringwald 
896404843c1Smatthias.ringwald     // store and open remote device db
897404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
898404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
899404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
900404843c1Smatthias.ringwald     }
90129d53098Smatthias.ringwald 
9028fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
9038fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
9048fcba05dSmatthias.ringwald 
90516833f0aSmatthias.ringwald     // register packet handlers with transport
90610e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
907f5454fc6Smatthias.ringwald 
908f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
909e2386ba1S[email protected] 
910e2386ba1S[email protected]     // class of device
911e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
912a45d6b9fS[email protected] 
913ae5d8360S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, auto accept
914ae5d8360S[email protected]     hci_stack.ssp_enable = 1;
915ae5d8360S[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
916a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
917ae5d8360S[email protected]     hci_stack.ssp_auto_accept = 1;
91869a97523S[email protected] 
91969a97523S[email protected]     // LE
92069a97523S[email protected]     hci_stack.adv_addr_type = 0;
92169a97523S[email protected]     memset(hci_stack.adv_address, 0, 6);
922475c8125Smatthias.ringwald }
923475c8125Smatthias.ringwald 
924404843c1Smatthias.ringwald void hci_close(){
925404843c1Smatthias.ringwald     // close remote device db
926404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
927404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
928404843c1Smatthias.ringwald     }
929f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
930f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
931f5454fc6Smatthias.ringwald }
932f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
933404843c1Smatthias.ringwald }
934404843c1Smatthias.ringwald 
9358d213e1aSmatthias.ringwald // State-Module-Driver overview
9368d213e1aSmatthias.ringwald // state                    module  low-level
9378d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
9388d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
9398d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
9408d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
941d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
942d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
943c7e0c5f6Smatthias.ringwald 
94440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
9457301ad89Smatthias.ringwald 
946038bc64cSmatthias.ringwald     // power on
947f9a30166Smatthias.ringwald     int err = 0;
948f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
949f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
950f9a30166Smatthias.ringwald     }
951038bc64cSmatthias.ringwald     if (err){
9527d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
953038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
954038bc64cSmatthias.ringwald         return err;
955038bc64cSmatthias.ringwald     }
956038bc64cSmatthias.ringwald 
957038bc64cSmatthias.ringwald     // open low-level device
958038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
959038bc64cSmatthias.ringwald     if (err){
9607d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
961f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
962f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
963f9a30166Smatthias.ringwald         }
964038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
965038bc64cSmatthias.ringwald         return err;
966038bc64cSmatthias.ringwald     }
9678d213e1aSmatthias.ringwald     return 0;
9688d213e1aSmatthias.ringwald }
969038bc64cSmatthias.ringwald 
97040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
9718d213e1aSmatthias.ringwald 
9727b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
9739418f9c9Smatthias.ringwald 
9748d213e1aSmatthias.ringwald     // close low-level device
9758d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
9768d213e1aSmatthias.ringwald 
9777b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
9789418f9c9Smatthias.ringwald 
9798d213e1aSmatthias.ringwald     // power off
9808d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
9818d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
9828d213e1aSmatthias.ringwald     }
9839418f9c9Smatthias.ringwald 
9847b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
9859418f9c9Smatthias.ringwald 
98672ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
98772ea5239Smatthias.ringwald }
98872ea5239Smatthias.ringwald 
98940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
99072ea5239Smatthias.ringwald 
9917b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
9923144bce4Smatthias.ringwald 
993b429b9b7Smatthias.ringwald #if 0
994b429b9b7Smatthias.ringwald     // don't close serial port during sleep
995b429b9b7Smatthias.ringwald 
99672ea5239Smatthias.ringwald     // close low-level device
99772ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
998b429b9b7Smatthias.ringwald #endif
99972ea5239Smatthias.ringwald 
100072ea5239Smatthias.ringwald     // sleep mode
10013144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
100272ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
100372ea5239Smatthias.ringwald     }
1004b429b9b7Smatthias.ringwald 
100572ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
10068d213e1aSmatthias.ringwald }
10078d213e1aSmatthias.ringwald 
100840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1009b429b9b7Smatthias.ringwald 
10107b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1011b429b9b7Smatthias.ringwald 
1012b429b9b7Smatthias.ringwald     // wake on
1013b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
1014b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
1015b429b9b7Smatthias.ringwald     }
1016b429b9b7Smatthias.ringwald 
1017b429b9b7Smatthias.ringwald #if 0
1018b429b9b7Smatthias.ringwald     // open low-level device
1019b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
1020b429b9b7Smatthias.ringwald     if (err){
10217d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
1022b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
1023b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
1024b429b9b7Smatthias.ringwald         }
1025b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1026b429b9b7Smatthias.ringwald         return err;
1027b429b9b7Smatthias.ringwald     }
1028b429b9b7Smatthias.ringwald #endif
1029b429b9b7Smatthias.ringwald 
1030b429b9b7Smatthias.ringwald     return 0;
1031b429b9b7Smatthias.ringwald }
1032b429b9b7Smatthias.ringwald 
1033b429b9b7Smatthias.ringwald 
10348d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1035d661ed19Smatthias.ringwald 
10367b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
1037d661ed19Smatthias.ringwald 
10388d213e1aSmatthias.ringwald     int err = 0;
10398d213e1aSmatthias.ringwald     switch (hci_stack.state){
10408d213e1aSmatthias.ringwald 
10418d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
10428d213e1aSmatthias.ringwald             switch (power_mode){
10438d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10448d213e1aSmatthias.ringwald                     err = hci_power_control_on();
10458d213e1aSmatthias.ringwald                     if (err) return err;
10467301ad89Smatthias.ringwald                     // set up state machine
10477301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
10487301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10497301ad89Smatthias.ringwald                     hci_stack.substate = 0;
10508d213e1aSmatthias.ringwald                     break;
10518d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10528d213e1aSmatthias.ringwald                     // do nothing
10538d213e1aSmatthias.ringwald                     break;
10548d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1055b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
10568d213e1aSmatthias.ringwald                     break;
10578d213e1aSmatthias.ringwald             }
10588d213e1aSmatthias.ringwald             break;
10597301ad89Smatthias.ringwald 
10608d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10618d213e1aSmatthias.ringwald             switch (power_mode){
10628d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10638d213e1aSmatthias.ringwald                     // do nothing
10648d213e1aSmatthias.ringwald                     break;
10658d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10668d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
10678d213e1aSmatthias.ringwald                     hci_power_control_off();
10688d213e1aSmatthias.ringwald                     break;
10698d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1070b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
107172ea5239Smatthias.ringwald                     hci_power_control_sleep();
10728d213e1aSmatthias.ringwald                     break;
10738d213e1aSmatthias.ringwald             }
10748d213e1aSmatthias.ringwald             break;
10757301ad89Smatthias.ringwald 
10768d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
10778d213e1aSmatthias.ringwald             switch (power_mode){
10788d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10798d213e1aSmatthias.ringwald                     // do nothing
10808d213e1aSmatthias.ringwald                     break;
10818d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1082c7e0c5f6Smatthias.ringwald                     // see hci_run
1083c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10848d213e1aSmatthias.ringwald                     break;
10858d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1086b546ac54Smatthias.ringwald                     // see hci_run
1087b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
108889db417bSmatthias.ringwald                     hci_stack.substate = 0;
10898d213e1aSmatthias.ringwald                     break;
10908d213e1aSmatthias.ringwald             }
10918d213e1aSmatthias.ringwald             break;
10927301ad89Smatthias.ringwald 
10938d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
10948d213e1aSmatthias.ringwald             switch (power_mode){
10958d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10968d213e1aSmatthias.ringwald                     // set up state machine
10978d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10988d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
10998d213e1aSmatthias.ringwald                     break;
11008d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11018d213e1aSmatthias.ringwald                     // do nothing
11028d213e1aSmatthias.ringwald                     break;
11038d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1104b546ac54Smatthias.ringwald                     // see hci_run
1105b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
110689db417bSmatthias.ringwald                     hci_stack.substate = 0;
11078d213e1aSmatthias.ringwald                     break;
11088d213e1aSmatthias.ringwald             }
11098d213e1aSmatthias.ringwald             break;
11108d213e1aSmatthias.ringwald 
11118d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
11128d213e1aSmatthias.ringwald             switch (power_mode){
11138d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
111428171530Smatthias.ringwald 
111528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
111628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
111728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11188747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1119da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
112028171530Smatthias.ringwald                         break;
112128171530Smatthias.ringwald                     }
112228171530Smatthias.ringwald #endif
1123b546ac54Smatthias.ringwald                     // set up state machine
1124b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1125b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1126b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
11278d213e1aSmatthias.ringwald                     break;
11288d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1129b546ac54Smatthias.ringwald                     // see hci_run
1130b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
11318d213e1aSmatthias.ringwald                     break;
11328d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1133b546ac54Smatthias.ringwald                     // do nothing
11348d213e1aSmatthias.ringwald                     break;
11358d213e1aSmatthias.ringwald             }
11368d213e1aSmatthias.ringwald             break;
11378d213e1aSmatthias.ringwald 
11388d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
11398d213e1aSmatthias.ringwald             switch (power_mode){
11408d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
114128171530Smatthias.ringwald 
114228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
114328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
114428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11458747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1146da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1147758b46ceSmatthias.ringwald                         hci_update_scan_enable();
114828171530Smatthias.ringwald                         break;
114928171530Smatthias.ringwald                     }
115028171530Smatthias.ringwald #endif
11513144bce4Smatthias.ringwald                     err = hci_power_control_wake();
11523144bce4Smatthias.ringwald                     if (err) return err;
1153b546ac54Smatthias.ringwald                     // set up state machine
1154b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1155b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1156b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
11578d213e1aSmatthias.ringwald                     break;
11588d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11594ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
11608d213e1aSmatthias.ringwald                     break;
11618d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1162b546ac54Smatthias.ringwald                     // do nothing
11638d213e1aSmatthias.ringwald                     break;
11648d213e1aSmatthias.ringwald             }
11658d213e1aSmatthias.ringwald             break;
116611e23e5fSmatthias.ringwald     }
116768d92d03Smatthias.ringwald 
1168038bc64cSmatthias.ringwald     // create internal event
1169ee8bf225Smatthias.ringwald 	hci_emit_state();
1170ee8bf225Smatthias.ringwald 
117168d92d03Smatthias.ringwald 	// trigger next/first action
117268d92d03Smatthias.ringwald 	hci_run();
117368d92d03Smatthias.ringwald 
1174475c8125Smatthias.ringwald     return 0;
1175475c8125Smatthias.ringwald }
1176475c8125Smatthias.ringwald 
1177758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1178758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1179758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1180758b46ceSmatthias.ringwald     hci_run();
1181758b46ceSmatthias.ringwald }
1182758b46ceSmatthias.ringwald 
1183381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1184381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1185381fbed8Smatthias.ringwald 
1186381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1187381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1188381fbed8Smatthias.ringwald         return;
1189381fbed8Smatthias.ringwald     }
1190381fbed8Smatthias.ringwald 
1191381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1192758b46ceSmatthias.ringwald     hci_update_scan_enable();
1193758b46ceSmatthias.ringwald }
1194b031bebbSmatthias.ringwald 
1195758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1196758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1197758b46ceSmatthias.ringwald 
1198758b46ceSmatthias.ringwald     // don't emit event
1199758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1200758b46ceSmatthias.ringwald 
1201758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1202758b46ceSmatthias.ringwald     hci_update_scan_enable();
1203381fbed8Smatthias.ringwald }
1204381fbed8Smatthias.ringwald 
12055061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
12065061f3afS[email protected]     return &hci_stack.local_bd_addr;
12075061f3afS[email protected] }
12085061f3afS[email protected] 
120906b35ec0Smatthias.ringwald void hci_run(){
12108a485f27Smatthias.ringwald 
121132ab9390Smatthias.ringwald     hci_connection_t * connection;
121232ab9390Smatthias.ringwald     linked_item_t * it;
121332ab9390Smatthias.ringwald 
1214ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1215ce4c8fabSmatthias.ringwald 
1216b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1217b031bebbSmatthias.ringwald 
1218b031bebbSmatthias.ringwald     // decline incoming connections
1219ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1220ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1221ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1222ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1223dbe1a790S[email protected]         return;
1224ce4c8fabSmatthias.ringwald     }
1225ce4c8fabSmatthias.ringwald 
1226b031bebbSmatthias.ringwald     // send scan enable
122792368cd3S[email protected]     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){
1228b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1229b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1230dbe1a790S[email protected]         return;
1231b031bebbSmatthias.ringwald     }
1232b031bebbSmatthias.ringwald 
123332ab9390Smatthias.ringwald     // send pending HCI commands
123432ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
123532ab9390Smatthias.ringwald 
1236b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
123732ab9390Smatthias.ringwald 
123832ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
12397b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
124032ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
124134d2123cS[email protected]             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1242dbe1a790S[email protected]             return;
1243c7e0c5f6Smatthias.ringwald         }
1244c7e0c5f6Smatthias.ringwald 
124532ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
124634d2123cS[email protected]             log_info("responding to link key request\n");
124734d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
124832ab9390Smatthias.ringwald             link_key_t link_key;
1249c77e8838S[email protected]             link_key_type_t link_key_type;
125034d2123cS[email protected]             if ( hci_stack.remote_device_db
125134d2123cS[email protected]               && hci_stack.remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
125234d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
12539ab95c90S[email protected]                connection->link_key_type = link_key_type;
125432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
125532ab9390Smatthias.ringwald             } else {
125632ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
125732ab9390Smatthias.ringwald             }
1258dbe1a790S[email protected]             return;
125932ab9390Smatthias.ringwald         }
12601d6b20aeS[email protected] 
12614c57c146S[email protected]         if (connection->authentication_flags & HANDLE_PIN_CODE_REQUEST){
12624c57c146S[email protected]             log_info("denying to pin request\n");
12634c57c146S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_PIN_CODE_REQUEST);
126434d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
12654c57c146S[email protected]             return;
12664c57c146S[email protected]         }
12674c57c146S[email protected] 
1268dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
126934d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
1270e00caf9cS[email protected]             if (hci_stack.bondable && hci_stack.ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){
1271dbe1a790S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement);
1272f8fb5f6eS[email protected]             } else {
1273f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1274f8fb5f6eS[email protected]             }
1275dbe1a790S[email protected]             return;
127632ab9390Smatthias.ringwald         }
127732ab9390Smatthias.ringwald 
1278dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1279dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
128034d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1281dbe1a790S[email protected]             return;
1282dbe1a790S[email protected]         }
1283dbe1a790S[email protected] 
1284dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1285dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
128634d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1287dbe1a790S[email protected]             return;
1288dbe1a790S[email protected]         }
1289afd4e962S[email protected] 
1290afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1291afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
129234d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
12932bd8b7e7S[email protected]             return;
12942bd8b7e7S[email protected]         }
12952bd8b7e7S[email protected] 
12962bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
12972bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
129834d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
129934d2123cS[email protected]             return;
130034d2123cS[email protected]         }
130134d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
130234d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
130334d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
13042bd8b7e7S[email protected]             return;
1305afd4e962S[email protected]         }
1306dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1307dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1308dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1309dce78009S[email protected]             return;
1310dce78009S[email protected]         }
1311dbe1a790S[email protected]     }
1312c7e0c5f6Smatthias.ringwald 
13133429f56bSmatthias.ringwald     switch (hci_stack.state){
13143429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
13157b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
13163429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
13173429f56bSmatthias.ringwald                 // odd: waiting for command completion
131806b35ec0Smatthias.ringwald                 return;
13193429f56bSmatthias.ringwald             }
132090919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1321c7492964Smatthias.ringwald                 case 0: // RESET
132222909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
132324052c2aS[email protected] 
1324c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1325c7492964Smatthias.ringwald                         // skip baud change
1326c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1327c7492964Smatthias.ringwald                     }
13283429f56bSmatthias.ringwald                     break;
1329c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
13307dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
13317dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1332c7492964Smatthias.ringwald                     break;
1333c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1334c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1335c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1336c7492964Smatthias.ringwald                     // break missing here for fall through
1337c7492964Smatthias.ringwald 
1338c7492964Smatthias.ringwald                 case 3:
133924052c2aS[email protected]                     // Custom initialization
1340db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
13417dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1342d62aca9fSmatthias.ringwald                         if (valid_cmd){
13437dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
13447dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1345c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
134690919203Smatthias.ringwald                             break;
134790919203Smatthias.ringwald                         }
1348d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
134990919203Smatthias.ringwald                     }
13502f6c30e1Smatthias.ringwald                     // otherwise continue
1351f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1352f432a6ddSmatthias.ringwald 					break;
1353c7492964Smatthias.ringwald 				case 4:
1354e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1355e2edc0c3Smatthias.ringwald 					break;
1356c7492964Smatthias.ringwald                 case 5:
135765389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
13583429f56bSmatthias.ringwald                     break;
1359c7492964Smatthias.ringwald                 case 6:
1360d7e0e3a8S[email protected]                     if (hci_le_supported()){
1361d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1362d7e0e3a8S[email protected]                     } else {
1363d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1364d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1365d7e0e3a8S[email protected]                     }
1366f5d8d141S[email protected] 
1367f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
136824052c2aS[email protected]                     if (!hci_classic_supported()){
136924052c2aS[email protected]                         if (hci_le_supported()){
1370f5d8d141S[email protected]                             hci_stack.substate = 11 << 1;    // skip all classic command
137124052c2aS[email protected]                         } else {
137224052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
1373d7e0e3a8S[email protected]                             hci_stack.substate = 13 << 1;    // skip all
137424052c2aS[email protected]                         }
1375f5d8d141S[email protected]                     }
1376f5d8d141S[email protected]                     break;
1377f5d8d141S[email protected]                 case 7:
137824052c2aS[email protected]                     if (hci_ssp_supported()){
1379f5d8d141S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1380f5d8d141S[email protected]                         break;
138124052c2aS[email protected]                     }
138224052c2aS[email protected]                     hci_stack.substate += 2;
138324052c2aS[email protected]                     // break missing here for fall through
138424052c2aS[email protected] 
1385f5d8d141S[email protected]                 case 8:
138665389bfcS[email protected]                     // ca. 15 sec
138765389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1388559e517eS[email protected]                     break;
1389f5d8d141S[email protected]                 case 9:
1390e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1391e2386ba1S[email protected]                     break;
1392f5d8d141S[email protected]                 case 10:
1393e2386ba1S[email protected]                     if (hci_stack.local_name){
1394e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1395e2386ba1S[email protected]                     } else {
13968a485f27Smatthias.ringwald                         char hostname[30];
1397e2386ba1S[email protected] #ifdef EMBEDDED
1398e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1399e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1400e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1401e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1402e2386ba1S[email protected] #else
1403e2386ba1S[email protected]                         // hostname for POSIX systems
14048a485f27Smatthias.ringwald                         gethostname(hostname, 30);
14058a485f27Smatthias.ringwald                         hostname[29] = '\0';
1406e2386ba1S[email protected] #endif
14078a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
14088a485f27Smatthias.ringwald                     }
14093c4d4b90Smatthias.ringwald                     break;
1410e0dbca83S[email protected]                 case 11:
1411a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
141224052c2aS[email protected]                     if (!hci_le_supported()){
141324052c2aS[email protected]                         // SKIP LE init for Classic only configuration
141424052c2aS[email protected]                         hci_stack.substate = 13 << 1;
141524052c2aS[email protected]                     }
1416a45d6b9fS[email protected] 					break;
141724052c2aS[email protected] 
141843aa7007S[email protected] #ifdef HAVE_BLE
141924052c2aS[email protected]                 // LE INIT
1420a45d6b9fS[email protected]                 case 12:
142124052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
142224052c2aS[email protected]                     break;
142324052c2aS[email protected]                 case 13:
142424052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
142524052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
142624052c2aS[email protected]                     break;
142743aa7007S[email protected] #endif
142824052c2aS[email protected] 
142924052c2aS[email protected]                 // DONE
143024052c2aS[email protected]                 case 14:
14313429f56bSmatthias.ringwald                     // done.
14323429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1433b360b6adSmatthias.ringwald                     hci_emit_state();
14343429f56bSmatthias.ringwald                     break;
14353429f56bSmatthias.ringwald                 default:
14363429f56bSmatthias.ringwald                     break;
1437475c8125Smatthias.ringwald             }
14383429f56bSmatthias.ringwald             hci_stack.substate++;
14393429f56bSmatthias.ringwald             break;
1440c7e0c5f6Smatthias.ringwald 
1441c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1442c7e0c5f6Smatthias.ringwald 
14437b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1444c7e0c5f6Smatthias.ringwald             // close all open connections
1445c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1446c7e0c5f6Smatthias.ringwald             if (connection){
144732ab9390Smatthias.ringwald 
1448c7e0c5f6Smatthias.ringwald                 // send disconnect
144932ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
145032ab9390Smatthias.ringwald 
145179bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
14526ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1453c7e0c5f6Smatthias.ringwald 
1454c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1455c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1456c7e0c5f6Smatthias.ringwald                 return;
1457c7e0c5f6Smatthias.ringwald             }
14587b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1459c7e0c5f6Smatthias.ringwald 
146072ea5239Smatthias.ringwald             // switch mode
1461c7e0c5f6Smatthias.ringwald             hci_power_control_off();
14629418f9c9Smatthias.ringwald 
14637b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
146472ea5239Smatthias.ringwald             hci_emit_state();
14657b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
146672ea5239Smatthias.ringwald             break;
1467c7e0c5f6Smatthias.ringwald 
146872ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
146989db417bSmatthias.ringwald             switch(hci_stack.substate) {
147089db417bSmatthias.ringwald                 case 0:
14717b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
147272ea5239Smatthias.ringwald                     // close all open connections
147372ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
147466da7044Smatthias.ringwald 
147528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
147666da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
147766da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
147866da7044Smatthias.ringwald                         connection = NULL;
147966da7044Smatthias.ringwald                     }
148066da7044Smatthias.ringwald #endif
148172ea5239Smatthias.ringwald                     if (connection){
148232ab9390Smatthias.ringwald 
148372ea5239Smatthias.ringwald                         // send disconnect
148432ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
148532ab9390Smatthias.ringwald 
148679bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
14876ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
148872ea5239Smatthias.ringwald 
148972ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
149072ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
149172ea5239Smatthias.ringwald                         return;
149272ea5239Smatthias.ringwald                     }
149372ea5239Smatthias.ringwald 
149492368cd3S[email protected]                     if (hci_classic_supported()){
149589db417bSmatthias.ringwald                         // disable page and inquiry scan
149632ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
149732ab9390Smatthias.ringwald 
149892368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
1499758b46ceSmatthias.ringwald                         hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
150089db417bSmatthias.ringwald 
150189db417bSmatthias.ringwald                         // continue in next sub state
150289db417bSmatthias.ringwald                         hci_stack.substate++;
150389db417bSmatthias.ringwald                         break;
150492368cd3S[email protected]                     }
150592368cd3S[email protected]                     // fall through for ble-only chips
150692368cd3S[email protected] 
150789db417bSmatthias.ringwald                 case 2:
15087b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
150928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
151028171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
151128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
151228171530Smatthias.ringwald                         // SLEEP MODE reached
151328171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
151428171530Smatthias.ringwald                         hci_emit_state();
151528171530Smatthias.ringwald                         break;
151628171530Smatthias.ringwald                     }
151728171530Smatthias.ringwald #endif
151872ea5239Smatthias.ringwald                     // switch mode
151989db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1520c7e0c5f6Smatthias.ringwald                     hci_emit_state();
152128171530Smatthias.ringwald                     break;
152228171530Smatthias.ringwald 
152389db417bSmatthias.ringwald                 default:
152489db417bSmatthias.ringwald                     break;
152589db417bSmatthias.ringwald             }
1526c7e0c5f6Smatthias.ringwald             break;
1527c7e0c5f6Smatthias.ringwald 
15283429f56bSmatthias.ringwald         default:
15293429f56bSmatthias.ringwald             break;
15301f504dbdSmatthias.ringwald     }
15313429f56bSmatthias.ringwald }
153216833f0aSmatthias.ringwald 
153331452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1534c8e4258aSmatthias.ringwald     bd_addr_t addr;
1535c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1536c8e4258aSmatthias.ringwald     // house-keeping
1537c8e4258aSmatthias.ringwald 
1538c8e4258aSmatthias.ringwald     // create_connection?
1539c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1540c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1541339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1542c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1543c8e4258aSmatthias.ringwald         if (conn) {
1544c8e4258aSmatthias.ringwald             // if connection exists
1545c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
154617f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
154717f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1548c8e4258aSmatthias.ringwald             }
154917f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1550c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1551c8e4258aSmatthias.ringwald 
155217f1ba2aSmatthias.ringwald         }
1553c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
155417f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
155517f1ba2aSmatthias.ringwald         if (!conn){
155617f1ba2aSmatthias.ringwald             // notify client that alloc failed
155717f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
155817f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
155917f1ba2aSmatthias.ringwald         }
1560c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1561c8e4258aSmatthias.ringwald     }
1562c8e4258aSmatthias.ringwald 
15637fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
15647fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
15657fde4af9Smatthias.ringwald     }
15667fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
15677fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
15687fde4af9Smatthias.ringwald     }
15697fde4af9Smatthias.ringwald 
15708ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
15718ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
15728ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
15738ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
15748ef73945Smatthias.ringwald         }
15758ef73945Smatthias.ringwald     }
1576c8e4258aSmatthias.ringwald 
157769a97523S[email protected] #ifdef HAVE_BLE
157869a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
157969a97523S[email protected]         hci_stack.adv_addr_type = packet[8];
158069a97523S[email protected]     }
158169a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
158269a97523S[email protected]         bt_flip_addr(hci_stack.adv_address, &packet[3]);
158369a97523S[email protected]     }
158469a97523S[email protected] #endif
158569a97523S[email protected] 
158669a97523S[email protected] 
158731452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1588622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
158931452debSmatthias.ringwald }
15908adf0ddaSmatthias.ringwald 
15912bd8b7e7S[email protected] // disconnect because of security block
15922bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
15932bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
15942bd8b7e7S[email protected]     if (!connection) return;
15952bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
15962bd8b7e7S[email protected] }
15972bd8b7e7S[email protected] 
15982bd8b7e7S[email protected] 
1599dbe1a790S[email protected] // Configure Secure Simple Pairing
1600dbe1a790S[email protected] 
1601dbe1a790S[email protected] // enable will enable SSP during init
1602dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
1603dbe1a790S[email protected]     hci_stack.ssp_enable = enable;
1604dbe1a790S[email protected] }
1605dbe1a790S[email protected] 
16062bd8b7e7S[email protected] int hci_local_ssp_activated(){
16072bd8b7e7S[email protected]     return hci_ssp_supported() && hci_stack.ssp_enable;
16082bd8b7e7S[email protected] }
16092bd8b7e7S[email protected] 
1610dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1611dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
1612dbe1a790S[email protected]     hci_stack.ssp_io_capability = io_capability;
1613dbe1a790S[email protected] }
1614dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
1615dbe1a790S[email protected]     hci_stack.ssp_authentication_requirement = authentication_requirement;
1616dbe1a790S[email protected] }
1617dbe1a790S[email protected] 
1618dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1619dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
1620dbe1a790S[email protected]     hci_stack.ssp_auto_accept = auto_accept;
1621dbe1a790S[email protected] }
1622dbe1a790S[email protected] 
16231cd208adSmatthias.ringwald /**
16241cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
16251cd208adSmatthias.ringwald  */
1626fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
16271cd208adSmatthias.ringwald     va_list argptr;
16281cd208adSmatthias.ringwald     va_start(argptr, cmd);
16297dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
16301cd208adSmatthias.ringwald     va_end(argptr);
16317dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
163293b8dc03Smatthias.ringwald }
1633c8e4258aSmatthias.ringwald 
1634ee091cf1Smatthias.ringwald // Create various non-HCI events.
1635ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1636ee091cf1Smatthias.ringwald 
1637c8e4258aSmatthias.ringwald void hci_emit_state(){
1638e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1639425d1371Smatthias.ringwald     uint8_t event[3];
164080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1641425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1642c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1643425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1644425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1645c8e4258aSmatthias.ringwald }
1646c8e4258aSmatthias.ringwald 
164717f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1648425d1371Smatthias.ringwald     uint8_t event[13];
1649c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1650425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
165117f1ba2aSmatthias.ringwald     event[2] = status;
1652c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1653c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1654c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1655c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1656425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1657425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1658c8e4258aSmatthias.ringwald }
1659c8e4258aSmatthias.ringwald 
16603c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1661425d1371Smatthias.ringwald     uint8_t event[6];
16623c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1663e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
16643c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
16653c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
16663c4d4b90Smatthias.ringwald     event[5] = reason;
1667425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1668425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
16693c4d4b90Smatthias.ringwald }
16703c4d4b90Smatthias.ringwald 
1671ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1672e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1673425d1371Smatthias.ringwald     uint8_t event[4];
167480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1675425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1676ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1677425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1678425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1679ee091cf1Smatthias.ringwald }
168043bfb1bdSmatthias.ringwald 
168143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1682e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1683425d1371Smatthias.ringwald     uint8_t event[3];
168480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1685425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
168643bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1687425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1688425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
168943bfb1bdSmatthias.ringwald }
1690038bc64cSmatthias.ringwald 
1691038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1692e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1693425d1371Smatthias.ringwald     uint8_t event[2];
169480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1695425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1696425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1697425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1698038bc64cSmatthias.ringwald }
16991b0e3922Smatthias.ringwald 
170009ba8edeSmatthias.ringwald #ifndef EMBEDDED
17011b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1702e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1703425d1371Smatthias.ringwald     uint8_t event[6];
17041b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1705425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1706425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1707425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1708425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1709425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1710425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
17111b0e3922Smatthias.ringwald }
171209ba8edeSmatthias.ringwald #endif
17131b0e3922Smatthias.ringwald 
17142ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1715e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1716425d1371Smatthias.ringwald     uint8_t event[3];
17172ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1718425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
17192ed6235cSmatthias.ringwald     event[2] = enabled;
1720425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1721e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
17222ed6235cSmatthias.ringwald }
1723627c2f45Smatthias.ringwald 
1724627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1725e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1726627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1727e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1728f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
17292f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1730f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1731e0abb8e7S[email protected] 
1732e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1733e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1734e0abb8e7S[email protected] 
1735e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1736e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1737627c2f45Smatthias.ringwald }
1738381fbed8Smatthias.ringwald 
1739381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1740e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1741425d1371Smatthias.ringwald     uint8_t event[3];
1742381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1743425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1744381fbed8Smatthias.ringwald     event[2] = enabled;
1745425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1746425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1747381fbed8Smatthias.ringwald }
1748458bf4e8S[email protected] 
1749a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1750a00031e2S[email protected]     uint8_t event[5];
1751e00caf9cS[email protected]     int pos = 0;
1752a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
1753e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
1754a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
1755e00caf9cS[email protected]     pos += 2;
1756e00caf9cS[email protected]     event[pos++] = level;
1757e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1758e00caf9cS[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1759e00caf9cS[email protected] }
1760e00caf9cS[email protected] 
17612bd8b7e7S[email protected] // query if remote side supports SSP
17622bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
17632bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
17642bd8b7e7S[email protected]     if (!connection) return 0;
17652bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
17662bd8b7e7S[email protected] }
17672bd8b7e7S[email protected] 
1768458bf4e8S[email protected] // GAP API
1769458bf4e8S[email protected] /**
1770458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
1771458bf4e8S[email protected]  * @praram enabled
1772458bf4e8S[email protected]  */
17734c57c146S[email protected] void gap_set_bondable_mode(int enable){
1774458bf4e8S[email protected]     hci_stack.bondable = enable ? 1 : 0;
1775458bf4e8S[email protected] }
1776cb230b9dS[email protected] 
1777cb230b9dS[email protected] /**
177834d2123cS[email protected]  * @brief map link keys to security levels
1779cb230b9dS[email protected]  */
178034d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
178134d2123cS[email protected]     switch (link_key_type){
17823c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
17833c68dfa9S[email protected]             return LEVEL_4;
17843c68dfa9S[email protected]         case COMBINATION_KEY:
17853c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
17863c68dfa9S[email protected]             return LEVEL_3;
17873c68dfa9S[email protected]         default:
17883c68dfa9S[email protected]             return LEVEL_2;
17893c68dfa9S[email protected]     }
1790cb230b9dS[email protected] }
1791cb230b9dS[email protected] 
1792a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
179334d2123cS[email protected]     if (!connection) return LEVEL_0;
179434d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
179534d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
179634d2123cS[email protected] }
179734d2123cS[email protected] 
179834d2123cS[email protected] 
179934d2123cS[email protected] /**
180034d2123cS[email protected]  * @brief get current security level
180134d2123cS[email protected]  */
180234d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
180334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
180434d2123cS[email protected]     if (!connection) return LEVEL_0;
180534d2123cS[email protected]     return gap_security_level_for_connection(connection);
180634d2123cS[email protected] }
180734d2123cS[email protected] 
1808cb230b9dS[email protected] /**
1809cb230b9dS[email protected]  * @brief request connection to device to
1810cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
1811cb230b9dS[email protected]  */
181234d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
181334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
181434d2123cS[email protected]     if (!connection){
1815a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
181634d2123cS[email protected]         return;
181734d2123cS[email protected]     }
181834d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
181934d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
182034d2123cS[email protected]     if (current_level >= requested_level){
1821a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
182234d2123cS[email protected]         return;
182334d2123cS[email protected]     }
1824a00031e2S[email protected] 
182534d2123cS[email protected]     connection->requested_security_level = requested_level;
1826a00031e2S[email protected] 
1827a00031e2S[email protected]     // would enabling ecnryption suffice?
1828a00031e2S[email protected]     if (hci_stack.remote_device_db){
1829a00031e2S[email protected]         link_key_type_t link_key_type;
1830a00031e2S[email protected]         link_key_t      link_key;
1831a00031e2S[email protected]         if (hci_stack.remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
1832a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
1833a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1834a00031e2S[email protected]                 return;
1835a00031e2S[email protected]             }
1836a00031e2S[email protected]         }
1837a00031e2S[email protected]     }
1838a00031e2S[email protected] 
1839*1eb2563eS[email protected]     // try to authenticate connection
1840*1eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1841a00031e2S[email protected]     // connection->bonding_flags |= BONDING_REQUESTED;
1842e00caf9cS[email protected] }
1843