xref: /btstack/src/hci.c (revision 9faad3ab0d8c778d63fab9f22b12438d135a21bf)
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;
1856724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
1866724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
1876724cd9eS[email protected]     return 0;
18880ca58a0Smatthias.ringwald }
18980ca58a0Smatthias.ringwald 
190c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
191c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
192c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
193c12e46e7Smatthias.ringwald     }
194c12e46e7Smatthias.ringwald }
195c12e46e7Smatthias.ringwald 
1967fde4af9Smatthias.ringwald 
1977fde4af9Smatthias.ringwald /**
19843bfb1bdSmatthias.ringwald  * count connections
19943bfb1bdSmatthias.ringwald  */
20040d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
20156c253c9Smatthias.ringwald     int count = 0;
20243bfb1bdSmatthias.ringwald     linked_item_t *it;
20343bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
20443bfb1bdSmatthias.ringwald     return count;
20543bfb1bdSmatthias.ringwald }
206c8e4258aSmatthias.ringwald 
20797addcc5Smatthias.ringwald /**
208ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
20916833f0aSmatthias.ringwald  */
2102718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
21116833f0aSmatthias.ringwald }
21216833f0aSmatthias.ringwald 
213998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2145061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
215998906cdSmatthias.ringwald     if (!connection) {
2167d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
217998906cdSmatthias.ringwald         return 0;
218998906cdSmatthias.ringwald     }
219998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
220998906cdSmatthias.ringwald }
221998906cdSmatthias.ringwald 
222998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
223998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
224998906cdSmatthias.ringwald     linked_item_t *it;
225998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
226998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
227998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2287d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
229998906cdSmatthias.ringwald             return 0;
230998906cdSmatthias.ringwald         }
231998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
232998906cdSmatthias.ringwald     }
233998906cdSmatthias.ringwald     return free_slots;
234998906cdSmatthias.ringwald }
235998906cdSmatthias.ringwald 
236c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2372b12a0b9Smatthias.ringwald 
2382b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2392b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2402b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2412b12a0b9Smatthias.ringwald             return 0;
2422b12a0b9Smatthias.ringwald         }
2432b12a0b9Smatthias.ringwald     }
2442b12a0b9Smatthias.ringwald 
2452b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
246c24735b1Smatthias.ringwald     switch (packet_type) {
247c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
248c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
249c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
250de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
251c24735b1Smatthias.ringwald         default:
252c24735b1Smatthias.ringwald             return 0;
253c24735b1Smatthias.ringwald     }
254c24735b1Smatthias.ringwald }
255c24735b1Smatthias.ringwald 
256ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2577856c818Smatthias.ringwald 
2586218e6f1Smatthias.ringwald     // check for free places on BT module
2595932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2606218e6f1Smatthias.ringwald 
2617856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2625061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
26356cf178bSmatthias.ringwald     if (!connection) return 0;
26456cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
26556cf178bSmatthias.ringwald 
26656cf178bSmatthias.ringwald     // count packet
26756cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2687b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2697856c818Smatthias.ringwald 
27000d8e42eSmatthias.ringwald     // send packet
27100d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2726218e6f1Smatthias.ringwald 
27300d8e42eSmatthias.ringwald     return err;
274ee091cf1Smatthias.ringwald }
275ee091cf1Smatthias.ringwald 
27616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2777856c818Smatthias.ringwald 
278e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
279e76a89eeS[email protected] 
2807856c818Smatthias.ringwald     // get info
2817856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2825061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
2837856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2847856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2857856c818Smatthias.ringwald 
2867856c818Smatthias.ringwald     // ignore non-registered handle
2877856c818Smatthias.ringwald     if (!conn){
2887d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2897856c818Smatthias.ringwald         return;
2907856c818Smatthias.ringwald     }
2917856c818Smatthias.ringwald 
292e76a89eeS[email protected]     // assert packet is complete
2939ecc3e17S[email protected]     if (acl_length + 4 != size){
294e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
295e76a89eeS[email protected]         return;
296e76a89eeS[email protected]     }
297e76a89eeS[email protected] 
2987856c818Smatthias.ringwald     // update idle timestamp
2997856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3007856c818Smatthias.ringwald 
3017856c818Smatthias.ringwald     // handle different packet types
3027856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3037856c818Smatthias.ringwald 
3047856c818Smatthias.ringwald         case 0x01: // continuation fragment
3057856c818Smatthias.ringwald 
3067856c818Smatthias.ringwald             // sanity check
3077856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3087d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3097856c818Smatthias.ringwald                 return;
3107856c818Smatthias.ringwald             }
3117856c818Smatthias.ringwald 
3127856c818Smatthias.ringwald             // append fragment payload (header already stored)
3137856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3147856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3157856c818Smatthias.ringwald 
3167d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
317decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3187856c818Smatthias.ringwald 
3197856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3207856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3217856c818Smatthias.ringwald 
3222718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3237856c818Smatthias.ringwald                 // reset recombination buffer
3247856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3257856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3267856c818Smatthias.ringwald             }
3277856c818Smatthias.ringwald             break;
3287856c818Smatthias.ringwald 
3297856c818Smatthias.ringwald         case 0x02: { // first fragment
3307856c818Smatthias.ringwald 
3317856c818Smatthias.ringwald             // sanity check
3327856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3337d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3347856c818Smatthias.ringwald                 return;
3357856c818Smatthias.ringwald             }
3367856c818Smatthias.ringwald 
3377856c818Smatthias.ringwald             // peek into L2CAP packet!
3387856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3397856c818Smatthias.ringwald 
340e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
341decc01a8Smatthias.ringwald 
3427856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3437856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3447856c818Smatthias.ringwald 
3457856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3462718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3477856c818Smatthias.ringwald 
3487856c818Smatthias.ringwald             } else {
3497856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3507856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3517856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3527856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
353decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3547856c818Smatthias.ringwald             }
3557856c818Smatthias.ringwald             break;
3567856c818Smatthias.ringwald 
3577856c818Smatthias.ringwald         }
3587856c818Smatthias.ringwald         default:
3597d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3607856c818Smatthias.ringwald             return;
3617856c818Smatthias.ringwald     }
36294ab26f8Smatthias.ringwald 
36394ab26f8Smatthias.ringwald     // execute main loop
36494ab26f8Smatthias.ringwald     hci_run();
36516833f0aSmatthias.ringwald }
36622909952Smatthias.ringwald 
36767a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
368339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3693c4d4b90Smatthias.ringwald 
3703c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3713c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3723c4d4b90Smatthias.ringwald 
373c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
374c785ef68Smatthias.ringwald 
375c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
376a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3773c4d4b90Smatthias.ringwald 
3783c4d4b90Smatthias.ringwald     // now it's gone
379c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
380c7e0c5f6Smatthias.ringwald }
381c7e0c5f6Smatthias.ringwald 
3820c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3838f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3848f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3858f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3868f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3878f8108aaSmatthias.ringwald };
38865389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
38965389bfcS[email protected]      0, // 3 slot packets
39065389bfcS[email protected]      1, // 5 slot packets
39165389bfcS[email protected]     25, // EDR 2 mpbs
39265389bfcS[email protected]     26, // EDR 3 mbps
39365389bfcS[email protected]     39, // 3 slot EDR packts
39465389bfcS[email protected]     40, // 5 slot EDR packet
39565389bfcS[email protected] };
39665389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
39765389bfcS[email protected]     0x0f00, // 3 slot packets
39865389bfcS[email protected]     0xf000, // 5 slot packets
39965389bfcS[email protected]     0x1102, // EDR 2 mpbs
40065389bfcS[email protected]     0x2204, // EDR 3 mbps
40165389bfcS[email protected]     0x0300, // 3 slot EDR packts
40265389bfcS[email protected]     0x3000, // 5 slot EDR packet
40365389bfcS[email protected] };
4048f8108aaSmatthias.ringwald 
40565389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
40665389bfcS[email protected]     // enable packet types based on size
4078f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4088f8108aaSmatthias.ringwald     int i;
4098f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4108f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4118f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4128f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4138f8108aaSmatthias.ringwald         }
4148f8108aaSmatthias.ringwald     }
41565389bfcS[email protected]     // disable packet types due to missing local supported features
41665389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
41765389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
41865389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
41965389bfcS[email protected]         if (feature_set) continue;
42065389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
42165389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
42265389bfcS[email protected]     }
4238f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4248f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4258f8108aaSmatthias.ringwald     return packet_types;
4268f8108aaSmatthias.ringwald }
4278f8108aaSmatthias.ringwald 
4288f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4298f8108aaSmatthias.ringwald     return hci_stack.packet_types;
4308f8108aaSmatthias.ringwald }
4318f8108aaSmatthias.ringwald 
4327dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4337dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4347dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
4357dc17943Smatthias.ringwald }
4367dc17943Smatthias.ringwald 
437f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4387dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
4397dc17943Smatthias.ringwald }
4407dc17943Smatthias.ringwald 
441f5d8d141S[email protected] int hci_ssp_supported(void){
442f5d8d141S[email protected]     // No 51, byte 6, bit 3
443f5d8d141S[email protected]     return (hci_stack.local_supported_features[6] & (1 << 3)) != 0;
444f5d8d141S[email protected] }
445f5d8d141S[email protected] 
446f5d8d141S[email protected] int hci_classic_supported(void){
447f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
448f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 5)) == 0;
449f5d8d141S[email protected] }
450f5d8d141S[email protected] 
451f5d8d141S[email protected] int hci_le_supported(void){
452f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
453f5d8d141S[email protected] #ifdef HAVE_BLE
454f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 6)) != 0;
455f5d8d141S[email protected] #else
456f5d8d141S[email protected]     return 0;
457f5d8d141S[email protected] #endif
458f5d8d141S[email protected] }
459f5d8d141S[email protected] 
46069a97523S[email protected] // get addr type and address used in advertisement packets
46118904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
46269a97523S[email protected]     *addr_type = hci_stack.adv_addr_type;
46369a97523S[email protected]     if (hci_stack.adv_addr_type){
46469a97523S[email protected]         memcpy(addr, hci_stack.adv_address, 6);
46569a97523S[email protected]     } else {
46669a97523S[email protected]         memcpy(addr, hci_stack.local_bd_addr, 6);
46769a97523S[email protected]     }
46869a97523S[email protected] }
46969a97523S[email protected] 
47074ec757aSmatthias.ringwald // avoid huge local variables
471223aafc1Smatthias.ringwald #ifndef EMBEDDED
47274ec757aSmatthias.ringwald static device_name_t device_name;
473223aafc1Smatthias.ringwald #endif
47416833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
475e76a89eeS[email protected] 
476e76a89eeS[email protected]     uint16_t event_length = packet[1];
477e76a89eeS[email protected] 
478e76a89eeS[email protected]     // assert packet is complete
479e76a89eeS[email protected]     if (size != event_length + 2){
480e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
481e76a89eeS[email protected]         return;
482e76a89eeS[email protected]     }
483e76a89eeS[email protected] 
4841281a47eSmatthias.ringwald     bd_addr_t addr;
4852d00edd4Smatthias.ringwald     uint8_t link_type;
486fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
4871f7b95a1Smatthias.ringwald     hci_connection_t * conn;
48856cf178bSmatthias.ringwald     int i;
48922909952Smatthias.ringwald 
4905909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4915909f7f2Smatthias.ringwald 
4926772a24cSmatthias.ringwald     switch (packet[0]) {
49322909952Smatthias.ringwald 
4946772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4957ec5eeaaSmatthias.ringwald             // get num cmd packets
4967b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4977ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4987ec5eeaaSmatthias.ringwald 
499e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
500e2edc0c3Smatthias.ringwald                 // from offset 5
501e2edc0c3Smatthias.ringwald                 // status
5021d279b20Smatthias.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"
503e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
50456cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
505e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
50656cf178bSmatthias.ringwald                 // ignore: total num SCO packets
50756cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
508a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5098fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
5108fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
5118f8108aaSmatthias.ringwald                     }
51265389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
51365389bfcS[email protected]                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
514e2edc0c3Smatthias.ringwald                 }
51556cf178bSmatthias.ringwald             }
51665a46ef3S[email protected] #ifdef HAVE_BLE
51765a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
51865a46ef3S[email protected]                 hci_stack.le_data_packet_length = READ_BT_16(packet, 6);
51965a46ef3S[email protected]                 hci_stack.total_num_le_packets  = packet[8];
52065a46ef3S[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);
52165a46ef3S[email protected]             }
52265a46ef3S[email protected] #endif
523188981d2Smatthias.ringwald             // Dump local address
524188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
525e2386ba1S[email protected]                 bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
526188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
527e2386ba1S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr));
528188981d2Smatthias.ringwald             }
529381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
530381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
531381fbed8Smatthias.ringwald             }
53265389bfcS[email protected]             // Note: HCI init checks
533559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
53465389bfcS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
535559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
536559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
537559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
538559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
539559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
54065389bfcS[email protected] 
54165389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
54265389bfcS[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]);
543f5d8d141S[email protected]                 log_info("packet types %04x", hci_stack.packet_types);
544f5d8d141S[email protected] 
545f5d8d141S[email protected]                 // Classic/LE
546f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
547559e517eS[email protected]             }
54856cf178bSmatthias.ringwald             break;
54956cf178bSmatthias.ringwald 
5507ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5517ec5eeaaSmatthias.ringwald             // get num cmd packets
5527b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
5537ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
5547ec5eeaaSmatthias.ringwald             break;
5557ec5eeaaSmatthias.ringwald 
55656cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
55756cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
55856cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
55956cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
5605061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
56156cf178bSmatthias.ringwald                 if (!conn){
5627d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
56356cf178bSmatthias.ringwald                     continue;
56456cf178bSmatthias.ringwald                 }
56556cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5667b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
56756cf178bSmatthias.ringwald             }
5686772a24cSmatthias.ringwald             break;
5696772a24cSmatthias.ringwald 
5701f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
57137eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
57237eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5732d00edd4Smatthias.ringwald             link_type = packet[11];
574339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
57537eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5761f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5771f7b95a1Smatthias.ringwald                 if (!conn) {
5781f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5791f7b95a1Smatthias.ringwald                 }
580ce4c8fabSmatthias.ringwald                 if (!conn) {
581ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
582ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
583ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
584ce4c8fabSmatthias.ringwald                     break;
585ce4c8fabSmatthias.ringwald                 }
58632ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
58732ab9390Smatthias.ringwald                 hci_run();
58837eaa4cfSmatthias.ringwald             } else {
589ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
590ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
591ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
59237eaa4cfSmatthias.ringwald             }
5931f7b95a1Smatthias.ringwald             break;
5941f7b95a1Smatthias.ringwald 
5956772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
596fe1ed1b8Smatthias.ringwald             // Connection management
597fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
598339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
5991f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
600fe1ed1b8Smatthias.ringwald             if (conn) {
601b448a0e7Smatthias.ringwald                 if (!packet[2]){
602c8e4258aSmatthias.ringwald                     conn->state = OPEN;
603fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
604ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
605ee091cf1Smatthias.ringwald 
606c785ef68Smatthias.ringwald                     // restart timer
607c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
608ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
609c785ef68Smatthias.ringwald 
610339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
61143bfb1bdSmatthias.ringwald 
61243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
613b448a0e7Smatthias.ringwald                 } else {
614ad83dc6aS[email protected]                     // notify client if dedicated bonding
615ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
616ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
617ad83dc6aS[email protected]                     }
618ad83dc6aS[email protected] 
619b448a0e7Smatthias.ringwald                     // connection failed, remove entry
620b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
621a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
622c12e46e7Smatthias.ringwald 
623c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
624c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
625c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
626c12e46e7Smatthias.ringwald                     }
627fe1ed1b8Smatthias.ringwald                 }
628fe1ed1b8Smatthias.ringwald             }
6296772a24cSmatthias.ringwald             break;
630fe1ed1b8Smatthias.ringwald 
631afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
632afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
633afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
634afd4e962S[email protected]             if (!conn) break;
635afd4e962S[email protected]             if (!packet[2]){
636afd4e962S[email protected]                 uint8_t * features = &packet[5];
637afd4e962S[email protected]                 if (features[6] & (1 << 3)){
638afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
639afd4e962S[email protected]                 }
640afd4e962S[email protected]             }
641afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
642ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
643ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
644ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
645ad83dc6aS[email protected]             }
646afd4e962S[email protected]             break;
647afd4e962S[email protected] 
6487fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
6497b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
6507fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
65174d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
65274d716b5S[email protected]             if (hci_stack.bondable && !hci_stack.remote_device_db) break;
65332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
65464472d52Smatthias.ringwald             hci_run();
655d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
65629d53098Smatthias.ringwald             return;
6577fde4af9Smatthias.ringwald 
6589ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
65929d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
6609ab95c90S[email protected]             conn = connection_for_address(addr);
6619ab95c90S[email protected]             if (!conn) break;
6629ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
6639ab95c90S[email protected]             link_key_type_t link_key_type = packet[24];
6649ab95c90S[email protected]             // Change Connection Encryption keeps link key type
6659ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
6669ab95c90S[email protected]                 conn->link_key_type = link_key_type;
6679ab95c90S[email protected]             }
6689ab95c90S[email protected]             if (!hci_stack.remote_device_db) break;
6699ab95c90S[email protected]             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
67029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
6717fde4af9Smatthias.ringwald             break;
6729ab95c90S[email protected]         }
6737fde4af9Smatthias.ringwald 
6747fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
6756724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
6764c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
6774c57c146S[email protected]             if (!hci_stack.bondable){
678899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
679f8fb5f6eS[email protected]                 hci_run();
680f8fb5f6eS[email protected]                 return;
6814c57c146S[email protected]             }
682d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
683d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
684d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
685d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
6867fde4af9Smatthias.ringwald             break;
6877fde4af9Smatthias.ringwald 
6881d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
6891d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
690dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
691dbe1a790S[email protected]             break;
692dbe1a790S[email protected] 
693dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
6946724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
695dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
696dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
697dbe1a790S[email protected]             break;
698dbe1a790S[email protected] 
699dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
7006724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
701dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
702dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
7031d6b20aeS[email protected]             break;
7041d6b20aeS[email protected] 
705f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
706f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
707f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
708f0944df2S[email protected]             if (!conn) break;
709ad83dc6aS[email protected]             if (packet[2] == 0) {
710f0944df2S[email protected]                 if (packet[5]){
711f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
712f0944df2S[email protected]                 } else {
713536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
714f0944df2S[email protected]                 }
715ad83dc6aS[email protected]             }
716a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
717f0944df2S[email protected]             break;
718f0944df2S[email protected] 
7191eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
7201eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
7211eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
7221eb2563eS[email protected]             if (!conn) break;
723ad83dc6aS[email protected] 
724ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
725ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
726ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
727ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
728ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
729ad83dc6aS[email protected]                 break;
730ad83dc6aS[email protected]             }
731ad83dc6aS[email protected] 
732b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
7331eb2563eS[email protected]                 // link key sufficient for requested security
7341eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
735ad83dc6aS[email protected]                 break;
736ad83dc6aS[email protected]             }
7371eb2563eS[email protected]             // not enough
7381eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
7391eb2563eS[email protected]             break;
74034d2123cS[email protected] 
741223aafc1Smatthias.ringwald #ifndef EMBEDDED
74274ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
74374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
74474ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
74574ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
7465a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
7475a06394aSmatthias.ringwald             for (i=0; i<248;i++){
7485a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
7495a06394aSmatthias.ringwald                     packet[9+i] = 0;
7505a06394aSmatthias.ringwald                     break;
751cdc9101dSmatthias.ringwald                 }
7525a06394aSmatthias.ringwald             }
753d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
75474ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
75574ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
75674ec757aSmatthias.ringwald             break;
75774ec757aSmatthias.ringwald 
75874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
75974ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
76074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
76174ec757aSmatthias.ringwald             // first send inq result packet
76274ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
76374ec757aSmatthias.ringwald             // then send cached remote names
76474ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
76574ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
76674ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
76774ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
76874ec757aSmatthias.ringwald                 }
76974ec757aSmatthias.ringwald             }
77074ec757aSmatthias.ringwald             return;
771223aafc1Smatthias.ringwald #endif
77274ec757aSmatthias.ringwald 
7736772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
774fe1ed1b8Smatthias.ringwald             if (!packet[2]){
775fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
7765061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
777fe1ed1b8Smatthias.ringwald                 if (conn) {
778c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
779fe1ed1b8Smatthias.ringwald                 }
780fe1ed1b8Smatthias.ringwald             }
7816772a24cSmatthias.ringwald             break;
7826772a24cSmatthias.ringwald 
783c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
7846c062428S[email protected]             if(hci_stack.control && hci_stack.control->hw_error){
785c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
786c68bdf90Smatthias.ringwald             }
787c68bdf90Smatthias.ringwald             break;
788c68bdf90Smatthias.ringwald 
7895909f7f2Smatthias.ringwald #ifdef HAVE_BLE
7905909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
7915909f7f2Smatthias.ringwald             switch (packet[2]) {
7925909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
7935909f7f2Smatthias.ringwald                     // Connection management
7945909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
7955909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
7965909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
7975909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
7985909f7f2Smatthias.ringwald                     if (packet[3]){
7995909f7f2Smatthias.ringwald                         if (conn){
8005909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
8015909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
8025909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
8035909f7f2Smatthias.ringwald 
8045909f7f2Smatthias.ringwald                         }
8055909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
8065909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
8075909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
8085909f7f2Smatthias.ringwald                         }
8095909f7f2Smatthias.ringwald                         break;
8105909f7f2Smatthias.ringwald                     }
8115909f7f2Smatthias.ringwald                     if (!conn){
8125909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
8135909f7f2Smatthias.ringwald                     }
8145909f7f2Smatthias.ringwald                     if (!conn){
8155909f7f2Smatthias.ringwald                         // no memory
8165909f7f2Smatthias.ringwald                         break;
8175909f7f2Smatthias.ringwald                     }
8185909f7f2Smatthias.ringwald 
8195909f7f2Smatthias.ringwald                     conn->state = OPEN;
8205909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
8215909f7f2Smatthias.ringwald 
8225909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
8235909f7f2Smatthias.ringwald 
8245909f7f2Smatthias.ringwald                     // restart timer
8255909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
8265909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
8275909f7f2Smatthias.ringwald 
8285909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
8295909f7f2Smatthias.ringwald 
8305909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
8315909f7f2Smatthias.ringwald                     break;
8325909f7f2Smatthias.ringwald 
83365a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
83465a46ef3S[email protected] 
8355909f7f2Smatthias.ringwald                 default:
8365909f7f2Smatthias.ringwald                     break;
8375909f7f2Smatthias.ringwald             }
8385909f7f2Smatthias.ringwald             break;
8395909f7f2Smatthias.ringwald #endif
8405909f7f2Smatthias.ringwald 
8416772a24cSmatthias.ringwald         default:
8426772a24cSmatthias.ringwald             break;
843fe1ed1b8Smatthias.ringwald     }
844fe1ed1b8Smatthias.ringwald 
8453429f56bSmatthias.ringwald     // handle BT initialization
8463429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
8473429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
8483429f56bSmatthias.ringwald             // odd: waiting for event
849f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
850c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
851c9af4d3fS[email protected]                 if (hci_stack.substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
8523429f56bSmatthias.ringwald                     hci_stack.substate++;
8533429f56bSmatthias.ringwald                 }
8543429f56bSmatthias.ringwald             }
85522909952Smatthias.ringwald         }
856c9af4d3fS[email protected]     }
85722909952Smatthias.ringwald 
85889db417bSmatthias.ringwald     // help with BT sleep
85989db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
86089db417bSmatthias.ringwald         && hci_stack.substate == 1
86189db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
86289db417bSmatthias.ringwald         hci_stack.substate++;
86389db417bSmatthias.ringwald     }
86489db417bSmatthias.ringwald 
8652718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
86694ab26f8Smatthias.ringwald 
86794ab26f8Smatthias.ringwald 	// execute main loop
86894ab26f8Smatthias.ringwald 	hci_run();
86916833f0aSmatthias.ringwald }
87016833f0aSmatthias.ringwald 
8710a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
87210e830c9Smatthias.ringwald     switch (packet_type) {
87310e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
87410e830c9Smatthias.ringwald             event_handler(packet, size);
87510e830c9Smatthias.ringwald             break;
87610e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
87710e830c9Smatthias.ringwald             acl_handler(packet, size);
87810e830c9Smatthias.ringwald             break;
87910e830c9Smatthias.ringwald         default:
88010e830c9Smatthias.ringwald             break;
88110e830c9Smatthias.ringwald     }
88210e830c9Smatthias.ringwald }
88310e830c9Smatthias.ringwald 
884fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
8852718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
8862718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
88716833f0aSmatthias.ringwald }
88816833f0aSmatthias.ringwald 
8894f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
890475c8125Smatthias.ringwald 
891475c8125Smatthias.ringwald     // reference to use transport layer implementation
89216833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
893475c8125Smatthias.ringwald 
89411e23e5fSmatthias.ringwald     // references to used control implementation
89511e23e5fSmatthias.ringwald     hci_stack.control = control;
89611e23e5fSmatthias.ringwald 
89711e23e5fSmatthias.ringwald     // reference to used config
89811e23e5fSmatthias.ringwald     hci_stack.config = config;
89911e23e5fSmatthias.ringwald 
900fe1ed1b8Smatthias.ringwald     // no connections yet
901fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
9020a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
903c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
904458bf4e8S[email protected]     hci_stack.bondable = 1;
905758b46ceSmatthias.ringwald 
906b031bebbSmatthias.ringwald     // no pending cmds
907b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
908b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
909b031bebbSmatthias.ringwald 
91016833f0aSmatthias.ringwald     // higher level handler
9112718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
91216833f0aSmatthias.ringwald 
913404843c1Smatthias.ringwald     // store and open remote device db
914404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
915404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
916404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
917404843c1Smatthias.ringwald     }
91829d53098Smatthias.ringwald 
9198fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
9208fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
9218fcba05dSmatthias.ringwald 
92216833f0aSmatthias.ringwald     // register packet handlers with transport
92310e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
924f5454fc6Smatthias.ringwald 
925f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
926e2386ba1S[email protected] 
927e2386ba1S[email protected]     // class of device
928e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
929a45d6b9fS[email protected] 
930ae5d8360S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, auto accept
931ae5d8360S[email protected]     hci_stack.ssp_enable = 1;
932ae5d8360S[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
933a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
934ae5d8360S[email protected]     hci_stack.ssp_auto_accept = 1;
93569a97523S[email protected] 
93669a97523S[email protected]     // LE
93769a97523S[email protected]     hci_stack.adv_addr_type = 0;
93869a97523S[email protected]     memset(hci_stack.adv_address, 0, 6);
939475c8125Smatthias.ringwald }
940475c8125Smatthias.ringwald 
941404843c1Smatthias.ringwald void hci_close(){
942404843c1Smatthias.ringwald     // close remote device db
943404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
944404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
945404843c1Smatthias.ringwald     }
946f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
947f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
948f5454fc6Smatthias.ringwald }
949f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
950404843c1Smatthias.ringwald }
951404843c1Smatthias.ringwald 
9528d213e1aSmatthias.ringwald // State-Module-Driver overview
9538d213e1aSmatthias.ringwald // state                    module  low-level
9548d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
9558d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
9568d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
9578d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
958d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
959d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
960c7e0c5f6Smatthias.ringwald 
96140d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
9627301ad89Smatthias.ringwald 
963038bc64cSmatthias.ringwald     // power on
964f9a30166Smatthias.ringwald     int err = 0;
965f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
966f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
967f9a30166Smatthias.ringwald     }
968038bc64cSmatthias.ringwald     if (err){
9697d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
970038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
971038bc64cSmatthias.ringwald         return err;
972038bc64cSmatthias.ringwald     }
973038bc64cSmatthias.ringwald 
974038bc64cSmatthias.ringwald     // open low-level device
975038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
976038bc64cSmatthias.ringwald     if (err){
9777d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
978f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
979f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
980f9a30166Smatthias.ringwald         }
981038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
982038bc64cSmatthias.ringwald         return err;
983038bc64cSmatthias.ringwald     }
9848d213e1aSmatthias.ringwald     return 0;
9858d213e1aSmatthias.ringwald }
986038bc64cSmatthias.ringwald 
98740d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
9888d213e1aSmatthias.ringwald 
9897b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
9909418f9c9Smatthias.ringwald 
9918d213e1aSmatthias.ringwald     // close low-level device
9928d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
9938d213e1aSmatthias.ringwald 
9947b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
9959418f9c9Smatthias.ringwald 
9968d213e1aSmatthias.ringwald     // power off
9978d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
9988d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
9998d213e1aSmatthias.ringwald     }
10009418f9c9Smatthias.ringwald 
10017b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
10029418f9c9Smatthias.ringwald 
100372ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
100472ea5239Smatthias.ringwald }
100572ea5239Smatthias.ringwald 
100640d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
100772ea5239Smatthias.ringwald 
10087b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
10093144bce4Smatthias.ringwald 
1010b429b9b7Smatthias.ringwald #if 0
1011b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1012b429b9b7Smatthias.ringwald 
101372ea5239Smatthias.ringwald     // close low-level device
101472ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
1015b429b9b7Smatthias.ringwald #endif
101672ea5239Smatthias.ringwald 
101772ea5239Smatthias.ringwald     // sleep mode
10183144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
101972ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
102072ea5239Smatthias.ringwald     }
1021b429b9b7Smatthias.ringwald 
102272ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
10238d213e1aSmatthias.ringwald }
10248d213e1aSmatthias.ringwald 
102540d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1026b429b9b7Smatthias.ringwald 
10277b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1028b429b9b7Smatthias.ringwald 
1029b429b9b7Smatthias.ringwald     // wake on
1030b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
1031b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
1032b429b9b7Smatthias.ringwald     }
1033b429b9b7Smatthias.ringwald 
1034b429b9b7Smatthias.ringwald #if 0
1035b429b9b7Smatthias.ringwald     // open low-level device
1036b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
1037b429b9b7Smatthias.ringwald     if (err){
10387d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
1039b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
1040b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
1041b429b9b7Smatthias.ringwald         }
1042b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1043b429b9b7Smatthias.ringwald         return err;
1044b429b9b7Smatthias.ringwald     }
1045b429b9b7Smatthias.ringwald #endif
1046b429b9b7Smatthias.ringwald 
1047b429b9b7Smatthias.ringwald     return 0;
1048b429b9b7Smatthias.ringwald }
1049b429b9b7Smatthias.ringwald 
1050b429b9b7Smatthias.ringwald 
10518d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1052d661ed19Smatthias.ringwald 
10537b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
1054d661ed19Smatthias.ringwald 
10558d213e1aSmatthias.ringwald     int err = 0;
10568d213e1aSmatthias.ringwald     switch (hci_stack.state){
10578d213e1aSmatthias.ringwald 
10588d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
10598d213e1aSmatthias.ringwald             switch (power_mode){
10608d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10618d213e1aSmatthias.ringwald                     err = hci_power_control_on();
10628d213e1aSmatthias.ringwald                     if (err) return err;
10637301ad89Smatthias.ringwald                     // set up state machine
10647301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
10657301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10667301ad89Smatthias.ringwald                     hci_stack.substate = 0;
10678d213e1aSmatthias.ringwald                     break;
10688d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10698d213e1aSmatthias.ringwald                     // do nothing
10708d213e1aSmatthias.ringwald                     break;
10718d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1072b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
10738d213e1aSmatthias.ringwald                     break;
10748d213e1aSmatthias.ringwald             }
10758d213e1aSmatthias.ringwald             break;
10767301ad89Smatthias.ringwald 
10778d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10788d213e1aSmatthias.ringwald             switch (power_mode){
10798d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10808d213e1aSmatthias.ringwald                     // do nothing
10818d213e1aSmatthias.ringwald                     break;
10828d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10838d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
10848d213e1aSmatthias.ringwald                     hci_power_control_off();
10858d213e1aSmatthias.ringwald                     break;
10868d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1087b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
108872ea5239Smatthias.ringwald                     hci_power_control_sleep();
10898d213e1aSmatthias.ringwald                     break;
10908d213e1aSmatthias.ringwald             }
10918d213e1aSmatthias.ringwald             break;
10927301ad89Smatthias.ringwald 
10938d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
10948d213e1aSmatthias.ringwald             switch (power_mode){
10958d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10968d213e1aSmatthias.ringwald                     // do nothing
10978d213e1aSmatthias.ringwald                     break;
10988d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1099c7e0c5f6Smatthias.ringwald                     // see hci_run
1100c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
11018d213e1aSmatthias.ringwald                     break;
11028d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1103b546ac54Smatthias.ringwald                     // see hci_run
1104b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
110589db417bSmatthias.ringwald                     hci_stack.substate = 0;
11068d213e1aSmatthias.ringwald                     break;
11078d213e1aSmatthias.ringwald             }
11088d213e1aSmatthias.ringwald             break;
11097301ad89Smatthias.ringwald 
11108d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
11118d213e1aSmatthias.ringwald             switch (power_mode){
11128d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11138d213e1aSmatthias.ringwald                     // set up state machine
11148d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
11158d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
11168d213e1aSmatthias.ringwald                     break;
11178d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11188d213e1aSmatthias.ringwald                     // do nothing
11198d213e1aSmatthias.ringwald                     break;
11208d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1121b546ac54Smatthias.ringwald                     // see hci_run
1122b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
112389db417bSmatthias.ringwald                     hci_stack.substate = 0;
11248d213e1aSmatthias.ringwald                     break;
11258d213e1aSmatthias.ringwald             }
11268d213e1aSmatthias.ringwald             break;
11278d213e1aSmatthias.ringwald 
11288d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
11298d213e1aSmatthias.ringwald             switch (power_mode){
11308d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
113128171530Smatthias.ringwald 
113228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
113328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
113428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11358747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1136da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
113728171530Smatthias.ringwald                         break;
113828171530Smatthias.ringwald                     }
113928171530Smatthias.ringwald #endif
1140b546ac54Smatthias.ringwald                     // set up state machine
1141b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1142b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1143b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
11448d213e1aSmatthias.ringwald                     break;
11458d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1146b546ac54Smatthias.ringwald                     // see hci_run
1147b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
11488d213e1aSmatthias.ringwald                     break;
11498d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1150b546ac54Smatthias.ringwald                     // do nothing
11518d213e1aSmatthias.ringwald                     break;
11528d213e1aSmatthias.ringwald             }
11538d213e1aSmatthias.ringwald             break;
11548d213e1aSmatthias.ringwald 
11558d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
11568d213e1aSmatthias.ringwald             switch (power_mode){
11578d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
115828171530Smatthias.ringwald 
115928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
116028171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
116128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11628747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1163da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1164758b46ceSmatthias.ringwald                         hci_update_scan_enable();
116528171530Smatthias.ringwald                         break;
116628171530Smatthias.ringwald                     }
116728171530Smatthias.ringwald #endif
11683144bce4Smatthias.ringwald                     err = hci_power_control_wake();
11693144bce4Smatthias.ringwald                     if (err) return err;
1170b546ac54Smatthias.ringwald                     // set up state machine
1171b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1172b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1173b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
11748d213e1aSmatthias.ringwald                     break;
11758d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11764ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
11778d213e1aSmatthias.ringwald                     break;
11788d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1179b546ac54Smatthias.ringwald                     // do nothing
11808d213e1aSmatthias.ringwald                     break;
11818d213e1aSmatthias.ringwald             }
11828d213e1aSmatthias.ringwald             break;
118311e23e5fSmatthias.ringwald     }
118468d92d03Smatthias.ringwald 
1185038bc64cSmatthias.ringwald     // create internal event
1186ee8bf225Smatthias.ringwald 	hci_emit_state();
1187ee8bf225Smatthias.ringwald 
118868d92d03Smatthias.ringwald 	// trigger next/first action
118968d92d03Smatthias.ringwald 	hci_run();
119068d92d03Smatthias.ringwald 
1191475c8125Smatthias.ringwald     return 0;
1192475c8125Smatthias.ringwald }
1193475c8125Smatthias.ringwald 
1194758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1195758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1196758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1197758b46ceSmatthias.ringwald     hci_run();
1198758b46ceSmatthias.ringwald }
1199758b46ceSmatthias.ringwald 
1200381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1201381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1202381fbed8Smatthias.ringwald 
1203381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1204381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1205381fbed8Smatthias.ringwald         return;
1206381fbed8Smatthias.ringwald     }
1207381fbed8Smatthias.ringwald 
1208381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1209758b46ceSmatthias.ringwald     hci_update_scan_enable();
1210758b46ceSmatthias.ringwald }
1211b031bebbSmatthias.ringwald 
1212758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1213758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1214758b46ceSmatthias.ringwald 
1215758b46ceSmatthias.ringwald     // don't emit event
1216758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1217758b46ceSmatthias.ringwald 
1218758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1219758b46ceSmatthias.ringwald     hci_update_scan_enable();
1220381fbed8Smatthias.ringwald }
1221381fbed8Smatthias.ringwald 
12225061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
12235061f3afS[email protected]     return &hci_stack.local_bd_addr;
12245061f3afS[email protected] }
12255061f3afS[email protected] 
122606b35ec0Smatthias.ringwald void hci_run(){
12278a485f27Smatthias.ringwald 
122832ab9390Smatthias.ringwald     hci_connection_t * connection;
122932ab9390Smatthias.ringwald     linked_item_t * it;
123032ab9390Smatthias.ringwald 
1231ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1232ce4c8fabSmatthias.ringwald 
1233b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1234b031bebbSmatthias.ringwald 
1235b031bebbSmatthias.ringwald     // decline incoming connections
1236ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1237ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1238ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1239ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1240dbe1a790S[email protected]         return;
1241ce4c8fabSmatthias.ringwald     }
1242ce4c8fabSmatthias.ringwald 
1243b031bebbSmatthias.ringwald     // send scan enable
124492368cd3S[email protected]     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){
1245b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1246b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1247dbe1a790S[email protected]         return;
1248b031bebbSmatthias.ringwald     }
1249b031bebbSmatthias.ringwald 
125032ab9390Smatthias.ringwald     // send pending HCI commands
125132ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
125232ab9390Smatthias.ringwald 
1253b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
125432ab9390Smatthias.ringwald 
1255ad83dc6aS[email protected]         if (connection->state == SEND_CREATE_CONNECTION){
1256ad83dc6aS[email protected]             log_info("sending hci_create_connection\n");
1257ad83dc6aS[email protected]             hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
1258ad83dc6aS[email protected]             return;
1259ad83dc6aS[email protected]         }
1260ad83dc6aS[email protected] 
126132ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
12627b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
126332ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
126434d2123cS[email protected]             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1265dbe1a790S[email protected]             return;
1266c7e0c5f6Smatthias.ringwald         }
1267c7e0c5f6Smatthias.ringwald 
126832ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
126934d2123cS[email protected]             log_info("responding to link key request\n");
127034d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
127132ab9390Smatthias.ringwald             link_key_t link_key;
1272c77e8838S[email protected]             link_key_type_t link_key_type;
127334d2123cS[email protected]             if ( hci_stack.remote_device_db
127434d2123cS[email protected]               && hci_stack.remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
127534d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
12769ab95c90S[email protected]                connection->link_key_type = link_key_type;
127732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
127832ab9390Smatthias.ringwald             } else {
127932ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
128032ab9390Smatthias.ringwald             }
1281dbe1a790S[email protected]             return;
128232ab9390Smatthias.ringwald         }
12831d6b20aeS[email protected] 
1284899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
12854c57c146S[email protected]             log_info("denying to pin request\n");
1286899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
128734d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
12884c57c146S[email protected]             return;
12894c57c146S[email protected]         }
12904c57c146S[email protected] 
1291dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
129234d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
1293e00caf9cS[email protected]             if (hci_stack.bondable && hci_stack.ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){
1294106d6d11S[email protected]                 // tweak authentication requirements
1295106d6d11S[email protected]                 uint8_t authreq = hci_stack.ssp_authentication_requirement;
1296106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
1297*9faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
1298*9faad3abS[email protected]                 }
1299*9faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
1300*9faad3abS[email protected]                     authreq |= 1;
1301106d6d11S[email protected]                 }
1302106d6d11S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, authreq);
1303f8fb5f6eS[email protected]             } else {
1304f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1305f8fb5f6eS[email protected]             }
1306dbe1a790S[email protected]             return;
130732ab9390Smatthias.ringwald         }
130832ab9390Smatthias.ringwald 
1309dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1310dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
131134d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1312dbe1a790S[email protected]             return;
1313dbe1a790S[email protected]         }
1314dbe1a790S[email protected] 
1315dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1316dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
131734d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1318dbe1a790S[email protected]             return;
1319dbe1a790S[email protected]         }
1320afd4e962S[email protected] 
1321afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1322afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
132334d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
13242bd8b7e7S[email protected]             return;
13252bd8b7e7S[email protected]         }
13262bd8b7e7S[email protected] 
13272bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
13282bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
132934d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
133034d2123cS[email protected]             return;
133134d2123cS[email protected]         }
1332ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1333ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
1334ad83dc6aS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0);  // authentication done
1335ad83dc6aS[email protected]             return;
1336ad83dc6aS[email protected]         }
133734d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
133834d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
133934d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
13402bd8b7e7S[email protected]             return;
1341afd4e962S[email protected]         }
1342dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1343dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1344dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1345dce78009S[email protected]             return;
1346dce78009S[email protected]         }
1347dbe1a790S[email protected]     }
1348c7e0c5f6Smatthias.ringwald 
13493429f56bSmatthias.ringwald     switch (hci_stack.state){
13503429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
13517b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
13523429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
13533429f56bSmatthias.ringwald                 // odd: waiting for command completion
135406b35ec0Smatthias.ringwald                 return;
13553429f56bSmatthias.ringwald             }
135690919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1357c7492964Smatthias.ringwald                 case 0: // RESET
135822909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
135924052c2aS[email protected] 
1360c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1361c7492964Smatthias.ringwald                         // skip baud change
1362c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1363c7492964Smatthias.ringwald                     }
13643429f56bSmatthias.ringwald                     break;
1365c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
13667dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
13677dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1368c7492964Smatthias.ringwald                     break;
1369c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1370c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1371c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1372c7492964Smatthias.ringwald                     // break missing here for fall through
1373c7492964Smatthias.ringwald 
1374c7492964Smatthias.ringwald                 case 3:
137524052c2aS[email protected]                     // Custom initialization
1376db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
13777dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1378d62aca9fSmatthias.ringwald                         if (valid_cmd){
13797dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
13807dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1381c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
138290919203Smatthias.ringwald                             break;
138390919203Smatthias.ringwald                         }
1384d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
138590919203Smatthias.ringwald                     }
13862f6c30e1Smatthias.ringwald                     // otherwise continue
1387f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1388f432a6ddSmatthias.ringwald 					break;
1389c7492964Smatthias.ringwald 				case 4:
1390e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1391e2edc0c3Smatthias.ringwald 					break;
1392c7492964Smatthias.ringwald                 case 5:
139365389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
13943429f56bSmatthias.ringwald                     break;
1395c7492964Smatthias.ringwald                 case 6:
1396d7e0e3a8S[email protected]                     if (hci_le_supported()){
1397d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1398d7e0e3a8S[email protected]                     } else {
1399d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1400d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1401d7e0e3a8S[email protected]                     }
1402f5d8d141S[email protected] 
1403f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
140424052c2aS[email protected]                     if (!hci_classic_supported()){
140524052c2aS[email protected]                         if (hci_le_supported()){
1406f5d8d141S[email protected]                             hci_stack.substate = 11 << 1;    // skip all classic command
140724052c2aS[email protected]                         } else {
140824052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
1409d7e0e3a8S[email protected]                             hci_stack.substate = 13 << 1;    // skip all
141024052c2aS[email protected]                         }
1411f5d8d141S[email protected]                     }
1412f5d8d141S[email protected]                     break;
1413f5d8d141S[email protected]                 case 7:
141424052c2aS[email protected]                     if (hci_ssp_supported()){
1415f5d8d141S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1416f5d8d141S[email protected]                         break;
141724052c2aS[email protected]                     }
141824052c2aS[email protected]                     hci_stack.substate += 2;
141924052c2aS[email protected]                     // break missing here for fall through
142024052c2aS[email protected] 
1421f5d8d141S[email protected]                 case 8:
142265389bfcS[email protected]                     // ca. 15 sec
142365389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1424559e517eS[email protected]                     break;
1425f5d8d141S[email protected]                 case 9:
1426e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1427e2386ba1S[email protected]                     break;
1428f5d8d141S[email protected]                 case 10:
1429e2386ba1S[email protected]                     if (hci_stack.local_name){
1430e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1431e2386ba1S[email protected]                     } else {
14328a485f27Smatthias.ringwald                         char hostname[30];
1433e2386ba1S[email protected] #ifdef EMBEDDED
1434e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1435e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1436e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1437e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1438e2386ba1S[email protected] #else
1439e2386ba1S[email protected]                         // hostname for POSIX systems
14408a485f27Smatthias.ringwald                         gethostname(hostname, 30);
14418a485f27Smatthias.ringwald                         hostname[29] = '\0';
1442e2386ba1S[email protected] #endif
14438a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
14448a485f27Smatthias.ringwald                     }
14453c4d4b90Smatthias.ringwald                     break;
1446e0dbca83S[email protected]                 case 11:
1447a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
144824052c2aS[email protected]                     if (!hci_le_supported()){
144924052c2aS[email protected]                         // SKIP LE init for Classic only configuration
145024052c2aS[email protected]                         hci_stack.substate = 13 << 1;
145124052c2aS[email protected]                     }
1452a45d6b9fS[email protected] 					break;
145324052c2aS[email protected] 
145443aa7007S[email protected] #ifdef HAVE_BLE
145524052c2aS[email protected]                 // LE INIT
1456a45d6b9fS[email protected]                 case 12:
145724052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
145824052c2aS[email protected]                     break;
145924052c2aS[email protected]                 case 13:
146024052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
146124052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
146224052c2aS[email protected]                     break;
146343aa7007S[email protected] #endif
146424052c2aS[email protected] 
146524052c2aS[email protected]                 // DONE
146624052c2aS[email protected]                 case 14:
14673429f56bSmatthias.ringwald                     // done.
14683429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1469b360b6adSmatthias.ringwald                     hci_emit_state();
14703429f56bSmatthias.ringwald                     break;
14713429f56bSmatthias.ringwald                 default:
14723429f56bSmatthias.ringwald                     break;
1473475c8125Smatthias.ringwald             }
14743429f56bSmatthias.ringwald             hci_stack.substate++;
14753429f56bSmatthias.ringwald             break;
1476c7e0c5f6Smatthias.ringwald 
1477c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1478c7e0c5f6Smatthias.ringwald 
14797b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1480c7e0c5f6Smatthias.ringwald             // close all open connections
1481c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1482c7e0c5f6Smatthias.ringwald             if (connection){
148332ab9390Smatthias.ringwald 
1484c7e0c5f6Smatthias.ringwald                 // send disconnect
148532ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
148632ab9390Smatthias.ringwald 
148779bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
14886ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1489c7e0c5f6Smatthias.ringwald 
1490c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1491c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1492c7e0c5f6Smatthias.ringwald                 return;
1493c7e0c5f6Smatthias.ringwald             }
14947b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1495c7e0c5f6Smatthias.ringwald 
149672ea5239Smatthias.ringwald             // switch mode
1497c7e0c5f6Smatthias.ringwald             hci_power_control_off();
14989418f9c9Smatthias.ringwald 
14997b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
150072ea5239Smatthias.ringwald             hci_emit_state();
15017b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
150272ea5239Smatthias.ringwald             break;
1503c7e0c5f6Smatthias.ringwald 
150472ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
150589db417bSmatthias.ringwald             switch(hci_stack.substate) {
150689db417bSmatthias.ringwald                 case 0:
15077b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
150872ea5239Smatthias.ringwald                     // close all open connections
150972ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
151066da7044Smatthias.ringwald 
151128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
151266da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
151366da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
151466da7044Smatthias.ringwald                         connection = NULL;
151566da7044Smatthias.ringwald                     }
151666da7044Smatthias.ringwald #endif
151772ea5239Smatthias.ringwald                     if (connection){
151832ab9390Smatthias.ringwald 
151972ea5239Smatthias.ringwald                         // send disconnect
152032ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
152132ab9390Smatthias.ringwald 
152279bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
15236ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
152472ea5239Smatthias.ringwald 
152572ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
152672ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
152772ea5239Smatthias.ringwald                         return;
152872ea5239Smatthias.ringwald                     }
152972ea5239Smatthias.ringwald 
153092368cd3S[email protected]                     if (hci_classic_supported()){
153189db417bSmatthias.ringwald                         // disable page and inquiry scan
153232ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
153332ab9390Smatthias.ringwald 
153492368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
1535758b46ceSmatthias.ringwald                         hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
153689db417bSmatthias.ringwald 
153789db417bSmatthias.ringwald                         // continue in next sub state
153889db417bSmatthias.ringwald                         hci_stack.substate++;
153989db417bSmatthias.ringwald                         break;
154092368cd3S[email protected]                     }
154192368cd3S[email protected]                     // fall through for ble-only chips
154292368cd3S[email protected] 
154389db417bSmatthias.ringwald                 case 2:
15447b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
154528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
154628171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
154728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
154828171530Smatthias.ringwald                         // SLEEP MODE reached
154928171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
155028171530Smatthias.ringwald                         hci_emit_state();
155128171530Smatthias.ringwald                         break;
155228171530Smatthias.ringwald                     }
155328171530Smatthias.ringwald #endif
155472ea5239Smatthias.ringwald                     // switch mode
155589db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1556c7e0c5f6Smatthias.ringwald                     hci_emit_state();
155728171530Smatthias.ringwald                     break;
155828171530Smatthias.ringwald 
155989db417bSmatthias.ringwald                 default:
156089db417bSmatthias.ringwald                     break;
156189db417bSmatthias.ringwald             }
1562c7e0c5f6Smatthias.ringwald             break;
1563c7e0c5f6Smatthias.ringwald 
15643429f56bSmatthias.ringwald         default:
15653429f56bSmatthias.ringwald             break;
15661f504dbdSmatthias.ringwald     }
15673429f56bSmatthias.ringwald }
156816833f0aSmatthias.ringwald 
156931452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1570c8e4258aSmatthias.ringwald     bd_addr_t addr;
1571c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1572c8e4258aSmatthias.ringwald     // house-keeping
1573c8e4258aSmatthias.ringwald 
1574c8e4258aSmatthias.ringwald     // create_connection?
1575c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1576c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1577339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1578c8e4258aSmatthias.ringwald 
1579ad83dc6aS[email protected]         conn = connection_for_address(addr);
1580ad83dc6aS[email protected]         if (!conn){
158117f1ba2aSmatthias.ringwald             conn = create_connection_for_addr(addr);
158217f1ba2aSmatthias.ringwald             if (!conn){
158317f1ba2aSmatthias.ringwald                 // notify client that alloc failed
158417f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
158517f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
158617f1ba2aSmatthias.ringwald             }
1587ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1588ad83dc6aS[email protected]         }
1589ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1590ad83dc6aS[email protected]         switch (conn->state){
1591ad83dc6aS[email protected]             // if connection active exists
1592ad83dc6aS[email protected]             case OPEN:
1593ad83dc6aS[email protected]                 // and OPEN, emit connection complete command
1594ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
1595ad83dc6aS[email protected]                 break;
1596ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1597ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1598ad83dc6aS[email protected]                 break;
1599ad83dc6aS[email protected]             default:
1600ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1601ad83dc6aS[email protected]                 return 0;
1602ad83dc6aS[email protected]         }
1603c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1604c8e4258aSmatthias.ringwald     }
1605c8e4258aSmatthias.ringwald 
16067fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
16077fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
16087fde4af9Smatthias.ringwald     }
16097fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
16107fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
16117fde4af9Smatthias.ringwald     }
16127fde4af9Smatthias.ringwald 
16138ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
16148ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
16158ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
16168ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
16178ef73945Smatthias.ringwald         }
16188ef73945Smatthias.ringwald     }
1619c8e4258aSmatthias.ringwald 
16206724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
16216724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
16226724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
16236724cd9eS[email protected]         conn = connection_for_address(addr);
16246724cd9eS[email protected]         if (conn){
16256724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
16266724cd9eS[email protected]         }
16276724cd9eS[email protected]     }
16286724cd9eS[email protected] 
16296724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
16306724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
16316724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
16326724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
16336724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
16346724cd9eS[email protected]         conn = connection_for_address(addr);
16356724cd9eS[email protected]         if (conn){
16366724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
16376724cd9eS[email protected]         }
16386724cd9eS[email protected]     }
16396724cd9eS[email protected] 
164069a97523S[email protected] #ifdef HAVE_BLE
164169a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
164269a97523S[email protected]         hci_stack.adv_addr_type = packet[8];
164369a97523S[email protected]     }
164469a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
164569a97523S[email protected]         bt_flip_addr(hci_stack.adv_address, &packet[3]);
164669a97523S[email protected]     }
164769a97523S[email protected] #endif
164869a97523S[email protected] 
164969a97523S[email protected] 
165031452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1651622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
165231452debSmatthias.ringwald }
16538adf0ddaSmatthias.ringwald 
16542bd8b7e7S[email protected] // disconnect because of security block
16552bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
16562bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
16572bd8b7e7S[email protected]     if (!connection) return;
16582bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
16592bd8b7e7S[email protected] }
16602bd8b7e7S[email protected] 
16612bd8b7e7S[email protected] 
1662dbe1a790S[email protected] // Configure Secure Simple Pairing
1663dbe1a790S[email protected] 
1664dbe1a790S[email protected] // enable will enable SSP during init
1665dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
1666dbe1a790S[email protected]     hci_stack.ssp_enable = enable;
1667dbe1a790S[email protected] }
1668dbe1a790S[email protected] 
16692bd8b7e7S[email protected] int hci_local_ssp_activated(){
16702bd8b7e7S[email protected]     return hci_ssp_supported() && hci_stack.ssp_enable;
16712bd8b7e7S[email protected] }
16722bd8b7e7S[email protected] 
1673dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1674dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
1675dbe1a790S[email protected]     hci_stack.ssp_io_capability = io_capability;
1676dbe1a790S[email protected] }
1677dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
1678dbe1a790S[email protected]     hci_stack.ssp_authentication_requirement = authentication_requirement;
1679dbe1a790S[email protected] }
1680dbe1a790S[email protected] 
1681dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1682dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
1683dbe1a790S[email protected]     hci_stack.ssp_auto_accept = auto_accept;
1684dbe1a790S[email protected] }
1685dbe1a790S[email protected] 
16861cd208adSmatthias.ringwald /**
16871cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
16881cd208adSmatthias.ringwald  */
1689fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
16901cd208adSmatthias.ringwald     va_list argptr;
16911cd208adSmatthias.ringwald     va_start(argptr, cmd);
16927dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
16931cd208adSmatthias.ringwald     va_end(argptr);
16947dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
169593b8dc03Smatthias.ringwald }
1696c8e4258aSmatthias.ringwald 
1697ee091cf1Smatthias.ringwald // Create various non-HCI events.
1698ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1699ee091cf1Smatthias.ringwald 
1700c8e4258aSmatthias.ringwald void hci_emit_state(){
1701e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1702425d1371Smatthias.ringwald     uint8_t event[3];
170380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1704425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1705c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1706425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1707425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1708c8e4258aSmatthias.ringwald }
1709c8e4258aSmatthias.ringwald 
171017f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1711425d1371Smatthias.ringwald     uint8_t event[13];
1712c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1713425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
171417f1ba2aSmatthias.ringwald     event[2] = status;
1715c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1716c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1717c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1718c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1719425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1720425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1721c8e4258aSmatthias.ringwald }
1722c8e4258aSmatthias.ringwald 
17233c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1724425d1371Smatthias.ringwald     uint8_t event[6];
17253c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1726e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
17273c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
17283c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
17293c4d4b90Smatthias.ringwald     event[5] = reason;
1730425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1731425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
17323c4d4b90Smatthias.ringwald }
17333c4d4b90Smatthias.ringwald 
1734ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1735e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1736425d1371Smatthias.ringwald     uint8_t event[4];
173780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1738425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1739ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1740425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1741425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1742ee091cf1Smatthias.ringwald }
174343bfb1bdSmatthias.ringwald 
174443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1745e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1746425d1371Smatthias.ringwald     uint8_t event[3];
174780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1748425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
174943bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1750425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1751425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
175243bfb1bdSmatthias.ringwald }
1753038bc64cSmatthias.ringwald 
1754038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1755e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1756425d1371Smatthias.ringwald     uint8_t event[2];
175780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1758425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1759425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1760425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1761038bc64cSmatthias.ringwald }
17621b0e3922Smatthias.ringwald 
176309ba8edeSmatthias.ringwald #ifndef EMBEDDED
17641b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1765e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1766425d1371Smatthias.ringwald     uint8_t event[6];
17671b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1768425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1769425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1770425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1771425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1772425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1773425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
17741b0e3922Smatthias.ringwald }
177509ba8edeSmatthias.ringwald #endif
17761b0e3922Smatthias.ringwald 
17772ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1778e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1779425d1371Smatthias.ringwald     uint8_t event[3];
17802ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1781425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
17822ed6235cSmatthias.ringwald     event[2] = enabled;
1783425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1784e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
17852ed6235cSmatthias.ringwald }
1786627c2f45Smatthias.ringwald 
1787627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1788e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1789627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1790e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1791f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
17922f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1793f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1794e0abb8e7S[email protected] 
1795e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1796e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1797e0abb8e7S[email protected] 
1798e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1799e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1800627c2f45Smatthias.ringwald }
1801381fbed8Smatthias.ringwald 
1802381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1803e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1804425d1371Smatthias.ringwald     uint8_t event[3];
1805381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1806425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1807381fbed8Smatthias.ringwald     event[2] = enabled;
1808425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1809425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1810381fbed8Smatthias.ringwald }
1811458bf4e8S[email protected] 
1812a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1813df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
1814a00031e2S[email protected]     uint8_t event[5];
1815e00caf9cS[email protected]     int pos = 0;
1816a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
1817e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
1818a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
1819e00caf9cS[email protected]     pos += 2;
1820e00caf9cS[email protected]     event[pos++] = level;
1821e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1822e00caf9cS[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1823e00caf9cS[email protected] }
1824e00caf9cS[email protected] 
1825ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
1826ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
1827ad83dc6aS[email protected]     uint8_t event[9];
1828ad83dc6aS[email protected]     int pos = 0;
1829ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
1830ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
1831ad83dc6aS[email protected]     event[pos++] = status;
1832ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
1833ad83dc6aS[email protected]     pos += 6;
1834ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1835ad83dc6aS[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1836ad83dc6aS[email protected] }
1837ad83dc6aS[email protected] 
18382bd8b7e7S[email protected] // query if remote side supports SSP
18392bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
18402bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
18412bd8b7e7S[email protected]     if (!connection) return 0;
18422bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
18432bd8b7e7S[email protected] }
18442bd8b7e7S[email protected] 
1845df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
1846df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
1847df3354fcS[email protected] }
1848df3354fcS[email protected] 
1849458bf4e8S[email protected] // GAP API
1850458bf4e8S[email protected] /**
1851458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
1852458bf4e8S[email protected]  * @praram enabled
1853458bf4e8S[email protected]  */
18544c57c146S[email protected] void gap_set_bondable_mode(int enable){
1855458bf4e8S[email protected]     hci_stack.bondable = enable ? 1 : 0;
1856458bf4e8S[email protected] }
1857cb230b9dS[email protected] 
1858cb230b9dS[email protected] /**
185934d2123cS[email protected]  * @brief map link keys to security levels
1860cb230b9dS[email protected]  */
186134d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
186234d2123cS[email protected]     switch (link_key_type){
18633c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
18643c68dfa9S[email protected]             return LEVEL_4;
18653c68dfa9S[email protected]         case COMBINATION_KEY:
18663c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
18673c68dfa9S[email protected]             return LEVEL_3;
18683c68dfa9S[email protected]         default:
18693c68dfa9S[email protected]             return LEVEL_2;
18703c68dfa9S[email protected]     }
1871cb230b9dS[email protected] }
1872cb230b9dS[email protected] 
1873a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
187434d2123cS[email protected]     if (!connection) return LEVEL_0;
187534d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
187634d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
187734d2123cS[email protected] }
187834d2123cS[email protected] 
187934d2123cS[email protected] 
1880106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
1881106d6d11S[email protected]     return level > LEVEL_2;
1882106d6d11S[email protected] }
1883106d6d11S[email protected] 
188434d2123cS[email protected] /**
188534d2123cS[email protected]  * @brief get current security level
188634d2123cS[email protected]  */
188734d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
188834d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
188934d2123cS[email protected]     if (!connection) return LEVEL_0;
189034d2123cS[email protected]     return gap_security_level_for_connection(connection);
189134d2123cS[email protected] }
189234d2123cS[email protected] 
1893cb230b9dS[email protected] /**
1894cb230b9dS[email protected]  * @brief request connection to device to
1895cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
1896cb230b9dS[email protected]  */
189734d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
189834d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
189934d2123cS[email protected]     if (!connection){
1900a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
190134d2123cS[email protected]         return;
190234d2123cS[email protected]     }
190334d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
190434d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
190534d2123cS[email protected]     if (current_level >= requested_level){
1906a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
190734d2123cS[email protected]         return;
190834d2123cS[email protected]     }
1909a00031e2S[email protected] 
191034d2123cS[email protected]     connection->requested_security_level = requested_level;
1911a00031e2S[email protected] 
1912fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
1913a00031e2S[email protected]     if (hci_stack.remote_device_db){
1914a00031e2S[email protected]         link_key_type_t link_key_type;
1915a00031e2S[email protected]         link_key_t      link_key;
1916a00031e2S[email protected]         if (hci_stack.remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
1917a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
1918a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1919a00031e2S[email protected]                 return;
1920a00031e2S[email protected]             }
1921a00031e2S[email protected]         }
1922a00031e2S[email protected]     }
1923a00031e2S[email protected] 
19241eb2563eS[email protected]     // try to authenticate connection
19251eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1926e00caf9cS[email protected] }
1927ad83dc6aS[email protected] 
1928ad83dc6aS[email protected] /**
1929ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
1930ad83dc6aS[email protected]  * @param device
1931ad83dc6aS[email protected]  * @param request MITM protection
1932ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
1933ad83dc6aS[email protected]  */
1934ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
1935ad83dc6aS[email protected] 
1936ad83dc6aS[email protected] 
1937ad83dc6aS[email protected]     printf("gap_dedicated_bonding clled\n");
1938ad83dc6aS[email protected]     // create connection state machine
1939ad83dc6aS[email protected]     hci_connection_t * connection = create_connection_for_addr(device);
1940ad83dc6aS[email protected] 
1941ad83dc6aS[email protected]     if (!connection){
1942ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
1943ad83dc6aS[email protected]     }
1944ad83dc6aS[email protected] 
1945ad83dc6aS[email protected]     printf("gap_dedicated_bonding 2\n");
1946ad83dc6aS[email protected] 
1947ad83dc6aS[email protected]     // delete linkn key
1948ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
1949ad83dc6aS[email protected] 
1950ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
1951ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
1952ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
1953ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
1954ad83dc6aS[email protected] 
1955ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
1956ad83dc6aS[email protected] 
1957ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
1958ad83dc6aS[email protected]     // handle: authentication failure
1959ad83dc6aS[email protected]     // handle: disconnect on done
1960ad83dc6aS[email protected] 
1961ad83dc6aS[email protected]     hci_run();
1962ad83dc6aS[email protected] 
1963ad83dc6aS[email protected]     return 0;
1964ad83dc6aS[email protected] }
1965