xref: /btstack/src/hci.c (revision 536f9994f0de145a66828353297a96197abe1e5c)
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);
73758b46ceSmatthias.ringwald 
7406b35ec0Smatthias.ringwald // the STACK is here
7516833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
7616833f0aSmatthias.ringwald 
7797addcc5Smatthias.ringwald /**
78ee091cf1Smatthias.ringwald  * get connection for a given handle
79ee091cf1Smatthias.ringwald  *
80ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
81ee091cf1Smatthias.ringwald  */
825061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
83ee091cf1Smatthias.ringwald     linked_item_t *it;
84ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
85ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
86ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
87ee091cf1Smatthias.ringwald         }
88ee091cf1Smatthias.ringwald     }
89ee091cf1Smatthias.ringwald     return NULL;
90ee091cf1Smatthias.ringwald }
91ee091cf1Smatthias.ringwald 
922b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
9328ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
94c785ef68Smatthias.ringwald #ifdef HAVE_TIME
95ee091cf1Smatthias.ringwald     struct timeval tv;
96ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
97c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
98ee091cf1Smatthias.ringwald         // connections might be timed out
99ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
100ee091cf1Smatthias.ringwald     }
1012b12a0b9Smatthias.ringwald #endif
102e5780900Smatthias.ringwald #ifdef HAVE_TICK
103c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
104c785ef68Smatthias.ringwald         // connections might be timed out
105c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
106c785ef68Smatthias.ringwald     }
107c785ef68Smatthias.ringwald #endif
108c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
109c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
110c785ef68Smatthias.ringwald }
111ee091cf1Smatthias.ringwald 
112ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
113c7492964Smatthias.ringwald #ifdef HAVE_TIME
114ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
115c7492964Smatthias.ringwald #endif
116e5780900Smatthias.ringwald #ifdef HAVE_TICK
117c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
118c785ef68Smatthias.ringwald #endif
119ee091cf1Smatthias.ringwald }
120ee091cf1Smatthias.ringwald 
121ee091cf1Smatthias.ringwald /**
122c8e4258aSmatthias.ringwald  * create connection for given address
123c8e4258aSmatthias.ringwald  *
12417f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
125c8e4258aSmatthias.ringwald  */
126c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
12728ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
128c8e4258aSmatthias.ringwald     if (!conn) return NULL;
129c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
130c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1317d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
132afd4e962S[email protected]     conn->bonding_flags = 0;
133ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
134ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
135ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
136d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1377856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
13856cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
139c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
140c8e4258aSmatthias.ringwald     return conn;
141c8e4258aSmatthias.ringwald }
142c8e4258aSmatthias.ringwald 
143c8e4258aSmatthias.ringwald /**
14406b35ec0Smatthias.ringwald  * get connection for given address
14597addcc5Smatthias.ringwald  *
14697addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
14797addcc5Smatthias.ringwald  */
148fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
14906b35ec0Smatthias.ringwald     linked_item_t *it;
15006b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
15106b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
15206b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
15306b35ec0Smatthias.ringwald         }
15406b35ec0Smatthias.ringwald     }
15506b35ec0Smatthias.ringwald     return NULL;
15606b35ec0Smatthias.ringwald }
15706b35ec0Smatthias.ringwald 
15828ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
15928ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
16028ca2b46S[email protected] }
16128ca2b46S[email protected] 
16228ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
16328ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
16428ca2b46S[email protected] }
16528ca2b46S[email protected] 
16628ca2b46S[email protected] 
16743bfb1bdSmatthias.ringwald /**
16880ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1697fde4af9Smatthias.ringwald  */
1707fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1717fde4af9Smatthias.ringwald     bd_addr_t addr;
1727fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1737fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1747fde4af9Smatthias.ringwald     if (conn) {
17528ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
17680ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1777fde4af9Smatthias.ringwald     }
1787fde4af9Smatthias.ringwald }
1797fde4af9Smatthias.ringwald 
18080ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
1815061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
18280ca58a0Smatthias.ringwald     if (!conn) return 0;
18380ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
18480ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
18580ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
18680ca58a0Smatthias.ringwald     return 1;
18780ca58a0Smatthias.ringwald }
18880ca58a0Smatthias.ringwald 
189c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
190c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
191c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
192c12e46e7Smatthias.ringwald     }
193c12e46e7Smatthias.ringwald }
194c12e46e7Smatthias.ringwald 
1957fde4af9Smatthias.ringwald 
1967fde4af9Smatthias.ringwald /**
19743bfb1bdSmatthias.ringwald  * count connections
19843bfb1bdSmatthias.ringwald  */
19940d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
20056c253c9Smatthias.ringwald     int count = 0;
20143bfb1bdSmatthias.ringwald     linked_item_t *it;
20243bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
20343bfb1bdSmatthias.ringwald     return count;
20443bfb1bdSmatthias.ringwald }
205c8e4258aSmatthias.ringwald 
20697addcc5Smatthias.ringwald /**
207ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
20816833f0aSmatthias.ringwald  */
2092718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
21016833f0aSmatthias.ringwald }
21116833f0aSmatthias.ringwald 
212998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2135061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
214998906cdSmatthias.ringwald     if (!connection) {
2157d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
216998906cdSmatthias.ringwald         return 0;
217998906cdSmatthias.ringwald     }
218998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
219998906cdSmatthias.ringwald }
220998906cdSmatthias.ringwald 
221998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
222998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
223998906cdSmatthias.ringwald     linked_item_t *it;
224998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
225998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
226998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2277d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
228998906cdSmatthias.ringwald             return 0;
229998906cdSmatthias.ringwald         }
230998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
231998906cdSmatthias.ringwald     }
232998906cdSmatthias.ringwald     return free_slots;
233998906cdSmatthias.ringwald }
234998906cdSmatthias.ringwald 
235c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2362b12a0b9Smatthias.ringwald 
2372b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2382b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2392b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2402b12a0b9Smatthias.ringwald             return 0;
2412b12a0b9Smatthias.ringwald         }
2422b12a0b9Smatthias.ringwald     }
2432b12a0b9Smatthias.ringwald 
2442b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
245c24735b1Smatthias.ringwald     switch (packet_type) {
246c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
247c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
248c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
249de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
250c24735b1Smatthias.ringwald         default:
251c24735b1Smatthias.ringwald             return 0;
252c24735b1Smatthias.ringwald     }
253c24735b1Smatthias.ringwald }
254c24735b1Smatthias.ringwald 
255ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2567856c818Smatthias.ringwald 
2576218e6f1Smatthias.ringwald     // check for free places on BT module
2585932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2596218e6f1Smatthias.ringwald 
2607856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2615061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
26256cf178bSmatthias.ringwald     if (!connection) return 0;
26356cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
26456cf178bSmatthias.ringwald 
26556cf178bSmatthias.ringwald     // count packet
26656cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2677b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2687856c818Smatthias.ringwald 
26900d8e42eSmatthias.ringwald     // send packet
27000d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2716218e6f1Smatthias.ringwald 
27200d8e42eSmatthias.ringwald     return err;
273ee091cf1Smatthias.ringwald }
274ee091cf1Smatthias.ringwald 
27516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2767856c818Smatthias.ringwald 
277e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
278e76a89eeS[email protected] 
2797856c818Smatthias.ringwald     // get info
2807856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2815061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
2827856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2837856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2847856c818Smatthias.ringwald 
2857856c818Smatthias.ringwald     // ignore non-registered handle
2867856c818Smatthias.ringwald     if (!conn){
2877d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2887856c818Smatthias.ringwald         return;
2897856c818Smatthias.ringwald     }
2907856c818Smatthias.ringwald 
291e76a89eeS[email protected]     // assert packet is complete
2929ecc3e17S[email protected]     if (acl_length + 4 != size){
293e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
294e76a89eeS[email protected]         return;
295e76a89eeS[email protected]     }
296e76a89eeS[email protected] 
2977856c818Smatthias.ringwald     // update idle timestamp
2987856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2997856c818Smatthias.ringwald 
3007856c818Smatthias.ringwald     // handle different packet types
3017856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3027856c818Smatthias.ringwald 
3037856c818Smatthias.ringwald         case 0x01: // continuation fragment
3047856c818Smatthias.ringwald 
3057856c818Smatthias.ringwald             // sanity check
3067856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3077d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3087856c818Smatthias.ringwald                 return;
3097856c818Smatthias.ringwald             }
3107856c818Smatthias.ringwald 
3117856c818Smatthias.ringwald             // append fragment payload (header already stored)
3127856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3137856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3147856c818Smatthias.ringwald 
3157d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
316decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3177856c818Smatthias.ringwald 
3187856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3197856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3207856c818Smatthias.ringwald 
3212718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3227856c818Smatthias.ringwald                 // reset recombination buffer
3237856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3247856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3257856c818Smatthias.ringwald             }
3267856c818Smatthias.ringwald             break;
3277856c818Smatthias.ringwald 
3287856c818Smatthias.ringwald         case 0x02: { // first fragment
3297856c818Smatthias.ringwald 
3307856c818Smatthias.ringwald             // sanity check
3317856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3327d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3337856c818Smatthias.ringwald                 return;
3347856c818Smatthias.ringwald             }
3357856c818Smatthias.ringwald 
3367856c818Smatthias.ringwald             // peek into L2CAP packet!
3377856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3387856c818Smatthias.ringwald 
339e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
340decc01a8Smatthias.ringwald 
3417856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3427856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3437856c818Smatthias.ringwald 
3447856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3452718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3467856c818Smatthias.ringwald 
3477856c818Smatthias.ringwald             } else {
3487856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3497856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3507856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3517856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
352decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3537856c818Smatthias.ringwald             }
3547856c818Smatthias.ringwald             break;
3557856c818Smatthias.ringwald 
3567856c818Smatthias.ringwald         }
3577856c818Smatthias.ringwald         default:
3587d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3597856c818Smatthias.ringwald             return;
3607856c818Smatthias.ringwald     }
36194ab26f8Smatthias.ringwald 
36294ab26f8Smatthias.ringwald     // execute main loop
36394ab26f8Smatthias.ringwald     hci_run();
36416833f0aSmatthias.ringwald }
36522909952Smatthias.ringwald 
36667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
367339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3683c4d4b90Smatthias.ringwald 
3693c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3703c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3713c4d4b90Smatthias.ringwald 
372c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
373c785ef68Smatthias.ringwald 
374c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
375a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3763c4d4b90Smatthias.ringwald 
3773c4d4b90Smatthias.ringwald     // now it's gone
378c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
379c7e0c5f6Smatthias.ringwald }
380c7e0c5f6Smatthias.ringwald 
3810c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3828f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3838f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3848f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3858f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3868f8108aaSmatthias.ringwald };
38765389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
38865389bfcS[email protected]      0, // 3 slot packets
38965389bfcS[email protected]      1, // 5 slot packets
39065389bfcS[email protected]     25, // EDR 2 mpbs
39165389bfcS[email protected]     26, // EDR 3 mbps
39265389bfcS[email protected]     39, // 3 slot EDR packts
39365389bfcS[email protected]     40, // 5 slot EDR packet
39465389bfcS[email protected] };
39565389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
39665389bfcS[email protected]     0x0f00, // 3 slot packets
39765389bfcS[email protected]     0xf000, // 5 slot packets
39865389bfcS[email protected]     0x1102, // EDR 2 mpbs
39965389bfcS[email protected]     0x2204, // EDR 3 mbps
40065389bfcS[email protected]     0x0300, // 3 slot EDR packts
40165389bfcS[email protected]     0x3000, // 5 slot EDR packet
40265389bfcS[email protected] };
4038f8108aaSmatthias.ringwald 
40465389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
40565389bfcS[email protected]     // enable packet types based on size
4068f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4078f8108aaSmatthias.ringwald     int i;
4088f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4098f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4108f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4118f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4128f8108aaSmatthias.ringwald         }
4138f8108aaSmatthias.ringwald     }
41465389bfcS[email protected]     // disable packet types due to missing local supported features
41565389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
41665389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
41765389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
41865389bfcS[email protected]         if (feature_set) continue;
41965389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
42065389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
42165389bfcS[email protected]     }
4228f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4238f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4248f8108aaSmatthias.ringwald     return packet_types;
4258f8108aaSmatthias.ringwald }
4268f8108aaSmatthias.ringwald 
4278f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4288f8108aaSmatthias.ringwald     return hci_stack.packet_types;
4298f8108aaSmatthias.ringwald }
4308f8108aaSmatthias.ringwald 
4317dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4327dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4337dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
4347dc17943Smatthias.ringwald }
4357dc17943Smatthias.ringwald 
436f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4377dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
4387dc17943Smatthias.ringwald }
4397dc17943Smatthias.ringwald 
440f5d8d141S[email protected] int hci_ssp_supported(void){
441f5d8d141S[email protected]     // No 51, byte 6, bit 3
442f5d8d141S[email protected]     return (hci_stack.local_supported_features[6] & (1 << 3)) != 0;
443f5d8d141S[email protected] }
444f5d8d141S[email protected] 
445f5d8d141S[email protected] int hci_classic_supported(void){
446f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
447f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 5)) == 0;
448f5d8d141S[email protected] }
449f5d8d141S[email protected] 
450f5d8d141S[email protected] int hci_le_supported(void){
451f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
452f5d8d141S[email protected] #ifdef HAVE_BLE
453f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 6)) != 0;
454f5d8d141S[email protected] #else
455f5d8d141S[email protected]     return 0;
456f5d8d141S[email protected] #endif
457f5d8d141S[email protected] }
458f5d8d141S[email protected] 
45969a97523S[email protected] // get addr type and address used in advertisement packets
46018904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
46169a97523S[email protected]     *addr_type = hci_stack.adv_addr_type;
46269a97523S[email protected]     if (hci_stack.adv_addr_type){
46369a97523S[email protected]         memcpy(addr, hci_stack.adv_address, 6);
46469a97523S[email protected]     } else {
46569a97523S[email protected]         memcpy(addr, hci_stack.local_bd_addr, 6);
46669a97523S[email protected]     }
46769a97523S[email protected] }
46869a97523S[email protected] 
46974ec757aSmatthias.ringwald // avoid huge local variables
470223aafc1Smatthias.ringwald #ifndef EMBEDDED
47174ec757aSmatthias.ringwald static device_name_t device_name;
472223aafc1Smatthias.ringwald #endif
47316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
474e76a89eeS[email protected] 
475e76a89eeS[email protected]     uint16_t event_length = packet[1];
476e76a89eeS[email protected] 
477e76a89eeS[email protected]     // assert packet is complete
478e76a89eeS[email protected]     if (size != event_length + 2){
479e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
480e76a89eeS[email protected]         return;
481e76a89eeS[email protected]     }
482e76a89eeS[email protected] 
4831281a47eSmatthias.ringwald     bd_addr_t addr;
4842d00edd4Smatthias.ringwald     uint8_t link_type;
485fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
4861f7b95a1Smatthias.ringwald     hci_connection_t * conn;
48756cf178bSmatthias.ringwald     int i;
48822909952Smatthias.ringwald 
4895909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4905909f7f2Smatthias.ringwald 
4916772a24cSmatthias.ringwald     switch (packet[0]) {
49222909952Smatthias.ringwald 
4936772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4947ec5eeaaSmatthias.ringwald             // get num cmd packets
4957b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4967ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4977ec5eeaaSmatthias.ringwald 
498e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
499e2edc0c3Smatthias.ringwald                 // from offset 5
500e2edc0c3Smatthias.ringwald                 // status
5011d279b20Smatthias.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"
502e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
50356cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
504e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
50556cf178bSmatthias.ringwald                 // ignore: total num SCO packets
50656cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
507a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5088fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
5098fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
5108f8108aaSmatthias.ringwald                     }
51165389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
51265389bfcS[email protected]                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
513e2edc0c3Smatthias.ringwald                 }
51456cf178bSmatthias.ringwald             }
51565a46ef3S[email protected] #ifdef HAVE_BLE
51665a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
51765a46ef3S[email protected]                 hci_stack.le_data_packet_length = READ_BT_16(packet, 6);
51865a46ef3S[email protected]                 hci_stack.total_num_le_packets  = packet[8];
51965a46ef3S[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);
52065a46ef3S[email protected]             }
52165a46ef3S[email protected] #endif
522188981d2Smatthias.ringwald             // Dump local address
523188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
524e2386ba1S[email protected]                 bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
525188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
526e2386ba1S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr));
527188981d2Smatthias.ringwald             }
528381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
529381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
530381fbed8Smatthias.ringwald             }
53165389bfcS[email protected]             // Note: HCI init checks
532559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
53365389bfcS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
534559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
535559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
536559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
537559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
538559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
53965389bfcS[email protected] 
54065389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
54165389bfcS[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]);
542f5d8d141S[email protected]                 log_info("packet types %04x", hci_stack.packet_types);
543f5d8d141S[email protected] 
544f5d8d141S[email protected]                 // Classic/LE
545f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
546559e517eS[email protected]             }
54756cf178bSmatthias.ringwald             break;
54856cf178bSmatthias.ringwald 
5497ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5507ec5eeaaSmatthias.ringwald             // get num cmd packets
5517b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
5527ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
5537ec5eeaaSmatthias.ringwald             break;
5547ec5eeaaSmatthias.ringwald 
55556cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
55656cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
55756cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
55856cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
5595061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
56056cf178bSmatthias.ringwald                 if (!conn){
5617d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
56256cf178bSmatthias.ringwald                     continue;
56356cf178bSmatthias.ringwald                 }
56456cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5657b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
56656cf178bSmatthias.ringwald             }
5676772a24cSmatthias.ringwald             break;
5686772a24cSmatthias.ringwald 
5691f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
57037eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
57137eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5722d00edd4Smatthias.ringwald             link_type = packet[11];
573339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
57437eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5751f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5761f7b95a1Smatthias.ringwald                 if (!conn) {
5771f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5781f7b95a1Smatthias.ringwald                 }
579ce4c8fabSmatthias.ringwald                 if (!conn) {
580ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
581ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
582ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
583ce4c8fabSmatthias.ringwald                     break;
584ce4c8fabSmatthias.ringwald                 }
58532ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
58632ab9390Smatthias.ringwald                 hci_run();
58737eaa4cfSmatthias.ringwald             } else {
588ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
589ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
590ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
59137eaa4cfSmatthias.ringwald             }
5921f7b95a1Smatthias.ringwald             break;
5931f7b95a1Smatthias.ringwald 
5946772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
595fe1ed1b8Smatthias.ringwald             // Connection management
596fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
597339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
5981f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
599fe1ed1b8Smatthias.ringwald             if (conn) {
600b448a0e7Smatthias.ringwald                 if (!packet[2]){
601c8e4258aSmatthias.ringwald                     conn->state = OPEN;
602fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
603afd4e962S[email protected]                     conn->bonding_flags = BONDING_REQUEST_REMOTE_FEATURES;
604ee091cf1Smatthias.ringwald 
605c785ef68Smatthias.ringwald                     // restart timer
606c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
607ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
608c785ef68Smatthias.ringwald 
609339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
61043bfb1bdSmatthias.ringwald 
61143bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
612b448a0e7Smatthias.ringwald                 } else {
613b448a0e7Smatthias.ringwald                     // connection failed, remove entry
614b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
615a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
616c12e46e7Smatthias.ringwald 
617c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
618c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
619c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
620c12e46e7Smatthias.ringwald                     }
621fe1ed1b8Smatthias.ringwald                 }
622fe1ed1b8Smatthias.ringwald             }
6236772a24cSmatthias.ringwald             break;
624fe1ed1b8Smatthias.ringwald 
625afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
626afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
627afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
628afd4e962S[email protected]             if (!conn) break;
629afd4e962S[email protected]             if (!packet[2]){
630afd4e962S[email protected]                 uint8_t * features = &packet[5];
631afd4e962S[email protected]                 if (features[6] & (1 << 3)){
632afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
633afd4e962S[email protected]                 }
634afd4e962S[email protected]             }
635afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
636afd4e962S[email protected]             break;
637afd4e962S[email protected] 
6387fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
6397b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
6407fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
64174d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
64274d716b5S[email protected]             if (hci_stack.bondable && !hci_stack.remote_device_db) break;
64332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
64464472d52Smatthias.ringwald             hci_run();
645d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
64629d53098Smatthias.ringwald             return;
6477fde4af9Smatthias.ringwald 
6487fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
6497fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
65074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
65129d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
652c77e8838S[email protected]             // @TODO determine link key type
653c77e8838S[email protected]             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], LINK_KEY_TYPE_UNAUTHENTICATED);
65429d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
6557fde4af9Smatthias.ringwald             break;
6567fde4af9Smatthias.ringwald 
6577fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
6587fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
6594c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
6604c57c146S[email protected]             if (!hci_stack.bondable){
6614c57c146S[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_PIN_CODE_REQUEST);
662f8fb5f6eS[email protected]                 hci_run();
663f8fb5f6eS[email protected]                 return;
6644c57c146S[email protected]             }
665d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
666d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
667d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
668d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
6697fde4af9Smatthias.ringwald             break;
6707fde4af9Smatthias.ringwald 
6711d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
6721d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
673f8fb5f6eS[email protected]             if (!hci_stack.bondable || hci_stack.ssp_io_capability == SSP_IO_CAPABILITY_UNKNOWN) break;
674dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
675dbe1a790S[email protected]             break;
676dbe1a790S[email protected] 
677dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
678dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_CONFIRM_REQUEST);
679dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
680dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
681dbe1a790S[email protected]             break;
682dbe1a790S[email protected] 
683dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
684dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_PASSKEY_REQUEST);
685dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
686dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
6871d6b20aeS[email protected]             break;
6881d6b20aeS[email protected] 
689f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
690f0944df2S[email protected]             if (packet[2]) break;   // error status
691f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
692f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
693f0944df2S[email protected]             if (!conn) break;
694f0944df2S[email protected]             if (packet[5]){
695f0944df2S[email protected]                 conn->authentication_flags |= CONNECTION_ENCRYPTED;
696f0944df2S[email protected]             } else {
697*536f9994S[email protected]                 conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
698f0944df2S[email protected]             }
699f0944df2S[email protected]             break;
700f0944df2S[email protected] 
701223aafc1Smatthias.ringwald #ifndef EMBEDDED
70274ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
70374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
70474ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
70574ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
7065a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
7075a06394aSmatthias.ringwald             for (i=0; i<248;i++){
7085a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
7095a06394aSmatthias.ringwald                     packet[9+i] = 0;
7105a06394aSmatthias.ringwald                     break;
711cdc9101dSmatthias.ringwald                 }
7125a06394aSmatthias.ringwald             }
713d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
71474ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
71574ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
71674ec757aSmatthias.ringwald             break;
71774ec757aSmatthias.ringwald 
71874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
71974ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
72074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
72174ec757aSmatthias.ringwald             // first send inq result packet
72274ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
72374ec757aSmatthias.ringwald             // then send cached remote names
72474ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
72574ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
72674ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
72774ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
72874ec757aSmatthias.ringwald                 }
72974ec757aSmatthias.ringwald             }
73074ec757aSmatthias.ringwald             return;
731223aafc1Smatthias.ringwald #endif
73274ec757aSmatthias.ringwald 
7336772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
734fe1ed1b8Smatthias.ringwald             if (!packet[2]){
735fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
7365061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
737fe1ed1b8Smatthias.ringwald                 if (conn) {
738c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
739fe1ed1b8Smatthias.ringwald                 }
740fe1ed1b8Smatthias.ringwald             }
7416772a24cSmatthias.ringwald             break;
7426772a24cSmatthias.ringwald 
743c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
7446c062428S[email protected]             if(hci_stack.control && hci_stack.control->hw_error){
745c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
746c68bdf90Smatthias.ringwald             }
747c68bdf90Smatthias.ringwald             break;
748c68bdf90Smatthias.ringwald 
7495909f7f2Smatthias.ringwald #ifdef HAVE_BLE
7505909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
7515909f7f2Smatthias.ringwald             switch (packet[2]) {
7525909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
7535909f7f2Smatthias.ringwald                     // Connection management
7545909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
7555909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
7565909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
7575909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
7585909f7f2Smatthias.ringwald                     if (packet[3]){
7595909f7f2Smatthias.ringwald                         if (conn){
7605909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
7615909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
7625909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
7635909f7f2Smatthias.ringwald 
7645909f7f2Smatthias.ringwald                         }
7655909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
7665909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
7675909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
7685909f7f2Smatthias.ringwald                         }
7695909f7f2Smatthias.ringwald                         break;
7705909f7f2Smatthias.ringwald                     }
7715909f7f2Smatthias.ringwald                     if (!conn){
7725909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
7735909f7f2Smatthias.ringwald                     }
7745909f7f2Smatthias.ringwald                     if (!conn){
7755909f7f2Smatthias.ringwald                         // no memory
7765909f7f2Smatthias.ringwald                         break;
7775909f7f2Smatthias.ringwald                     }
7785909f7f2Smatthias.ringwald 
7795909f7f2Smatthias.ringwald                     conn->state = OPEN;
7805909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
7815909f7f2Smatthias.ringwald 
7825909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
7835909f7f2Smatthias.ringwald 
7845909f7f2Smatthias.ringwald                     // restart timer
7855909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
7865909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
7875909f7f2Smatthias.ringwald 
7885909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
7895909f7f2Smatthias.ringwald 
7905909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
7915909f7f2Smatthias.ringwald                     break;
7925909f7f2Smatthias.ringwald 
79365a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
79465a46ef3S[email protected] 
7955909f7f2Smatthias.ringwald                 default:
7965909f7f2Smatthias.ringwald                     break;
7975909f7f2Smatthias.ringwald             }
7985909f7f2Smatthias.ringwald             break;
7995909f7f2Smatthias.ringwald #endif
8005909f7f2Smatthias.ringwald 
8016772a24cSmatthias.ringwald         default:
8026772a24cSmatthias.ringwald             break;
803fe1ed1b8Smatthias.ringwald     }
804fe1ed1b8Smatthias.ringwald 
8053429f56bSmatthias.ringwald     // handle BT initialization
8063429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
8073429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
8083429f56bSmatthias.ringwald             // odd: waiting for event
809f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
810c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
811c9af4d3fS[email protected]                 if (hci_stack.substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
8123429f56bSmatthias.ringwald                     hci_stack.substate++;
8133429f56bSmatthias.ringwald                 }
8143429f56bSmatthias.ringwald             }
81522909952Smatthias.ringwald         }
816c9af4d3fS[email protected]     }
81722909952Smatthias.ringwald 
81889db417bSmatthias.ringwald     // help with BT sleep
81989db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
82089db417bSmatthias.ringwald         && hci_stack.substate == 1
82189db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
82289db417bSmatthias.ringwald         hci_stack.substate++;
82389db417bSmatthias.ringwald     }
82489db417bSmatthias.ringwald 
8252718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
82694ab26f8Smatthias.ringwald 
82794ab26f8Smatthias.ringwald 	// execute main loop
82894ab26f8Smatthias.ringwald 	hci_run();
82916833f0aSmatthias.ringwald }
83016833f0aSmatthias.ringwald 
8310a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
83210e830c9Smatthias.ringwald     switch (packet_type) {
83310e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
83410e830c9Smatthias.ringwald             event_handler(packet, size);
83510e830c9Smatthias.ringwald             break;
83610e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
83710e830c9Smatthias.ringwald             acl_handler(packet, size);
83810e830c9Smatthias.ringwald             break;
83910e830c9Smatthias.ringwald         default:
84010e830c9Smatthias.ringwald             break;
84110e830c9Smatthias.ringwald     }
84210e830c9Smatthias.ringwald }
84310e830c9Smatthias.ringwald 
844fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
8452718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
8462718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
84716833f0aSmatthias.ringwald }
84816833f0aSmatthias.ringwald 
8494f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
850475c8125Smatthias.ringwald 
851475c8125Smatthias.ringwald     // reference to use transport layer implementation
85216833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
853475c8125Smatthias.ringwald 
85411e23e5fSmatthias.ringwald     // references to used control implementation
85511e23e5fSmatthias.ringwald     hci_stack.control = control;
85611e23e5fSmatthias.ringwald 
85711e23e5fSmatthias.ringwald     // reference to used config
85811e23e5fSmatthias.ringwald     hci_stack.config = config;
85911e23e5fSmatthias.ringwald 
860fe1ed1b8Smatthias.ringwald     // no connections yet
861fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
8620a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
863c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
864458bf4e8S[email protected]     hci_stack.bondable = 1;
865758b46ceSmatthias.ringwald 
866b031bebbSmatthias.ringwald     // no pending cmds
867b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
868b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
869b031bebbSmatthias.ringwald 
87016833f0aSmatthias.ringwald     // higher level handler
8712718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
87216833f0aSmatthias.ringwald 
873404843c1Smatthias.ringwald     // store and open remote device db
874404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
875404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
876404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
877404843c1Smatthias.ringwald     }
87829d53098Smatthias.ringwald 
8798fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
8808fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
8818fcba05dSmatthias.ringwald 
88216833f0aSmatthias.ringwald     // register packet handlers with transport
88310e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
884f5454fc6Smatthias.ringwald 
885f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
886e2386ba1S[email protected] 
887e2386ba1S[email protected]     // class of device
888e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
889a45d6b9fS[email protected] 
890ae5d8360S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, auto accept
891ae5d8360S[email protected]     hci_stack.ssp_enable = 1;
892ae5d8360S[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
893a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
894ae5d8360S[email protected]     hci_stack.ssp_auto_accept = 1;
89569a97523S[email protected] 
89669a97523S[email protected]     // LE
89769a97523S[email protected]     hci_stack.adv_addr_type = 0;
89869a97523S[email protected]     memset(hci_stack.adv_address, 0, 6);
899475c8125Smatthias.ringwald }
900475c8125Smatthias.ringwald 
901404843c1Smatthias.ringwald void hci_close(){
902404843c1Smatthias.ringwald     // close remote device db
903404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
904404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
905404843c1Smatthias.ringwald     }
906f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
907f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
908f5454fc6Smatthias.ringwald }
909f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
910404843c1Smatthias.ringwald }
911404843c1Smatthias.ringwald 
9128d213e1aSmatthias.ringwald // State-Module-Driver overview
9138d213e1aSmatthias.ringwald // state                    module  low-level
9148d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
9158d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
9168d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
9178d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
918d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
919d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
920c7e0c5f6Smatthias.ringwald 
92140d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
9227301ad89Smatthias.ringwald 
923038bc64cSmatthias.ringwald     // power on
924f9a30166Smatthias.ringwald     int err = 0;
925f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
926f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
927f9a30166Smatthias.ringwald     }
928038bc64cSmatthias.ringwald     if (err){
9297d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
930038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
931038bc64cSmatthias.ringwald         return err;
932038bc64cSmatthias.ringwald     }
933038bc64cSmatthias.ringwald 
934038bc64cSmatthias.ringwald     // open low-level device
935038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
936038bc64cSmatthias.ringwald     if (err){
9377d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
938f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
939f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
940f9a30166Smatthias.ringwald         }
941038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
942038bc64cSmatthias.ringwald         return err;
943038bc64cSmatthias.ringwald     }
9448d213e1aSmatthias.ringwald     return 0;
9458d213e1aSmatthias.ringwald }
946038bc64cSmatthias.ringwald 
94740d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
9488d213e1aSmatthias.ringwald 
9497b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
9509418f9c9Smatthias.ringwald 
9518d213e1aSmatthias.ringwald     // close low-level device
9528d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
9538d213e1aSmatthias.ringwald 
9547b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
9559418f9c9Smatthias.ringwald 
9568d213e1aSmatthias.ringwald     // power off
9578d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
9588d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
9598d213e1aSmatthias.ringwald     }
9609418f9c9Smatthias.ringwald 
9617b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
9629418f9c9Smatthias.ringwald 
96372ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
96472ea5239Smatthias.ringwald }
96572ea5239Smatthias.ringwald 
96640d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
96772ea5239Smatthias.ringwald 
9687b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
9693144bce4Smatthias.ringwald 
970b429b9b7Smatthias.ringwald #if 0
971b429b9b7Smatthias.ringwald     // don't close serial port during sleep
972b429b9b7Smatthias.ringwald 
97372ea5239Smatthias.ringwald     // close low-level device
97472ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
975b429b9b7Smatthias.ringwald #endif
97672ea5239Smatthias.ringwald 
97772ea5239Smatthias.ringwald     // sleep mode
9783144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
97972ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
98072ea5239Smatthias.ringwald     }
981b429b9b7Smatthias.ringwald 
98272ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
9838d213e1aSmatthias.ringwald }
9848d213e1aSmatthias.ringwald 
98540d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
986b429b9b7Smatthias.ringwald 
9877b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
988b429b9b7Smatthias.ringwald 
989b429b9b7Smatthias.ringwald     // wake on
990b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
991b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
992b429b9b7Smatthias.ringwald     }
993b429b9b7Smatthias.ringwald 
994b429b9b7Smatthias.ringwald #if 0
995b429b9b7Smatthias.ringwald     // open low-level device
996b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
997b429b9b7Smatthias.ringwald     if (err){
9987d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
999b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
1000b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
1001b429b9b7Smatthias.ringwald         }
1002b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1003b429b9b7Smatthias.ringwald         return err;
1004b429b9b7Smatthias.ringwald     }
1005b429b9b7Smatthias.ringwald #endif
1006b429b9b7Smatthias.ringwald 
1007b429b9b7Smatthias.ringwald     return 0;
1008b429b9b7Smatthias.ringwald }
1009b429b9b7Smatthias.ringwald 
1010b429b9b7Smatthias.ringwald 
10118d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1012d661ed19Smatthias.ringwald 
10137b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
1014d661ed19Smatthias.ringwald 
10158d213e1aSmatthias.ringwald     int err = 0;
10168d213e1aSmatthias.ringwald     switch (hci_stack.state){
10178d213e1aSmatthias.ringwald 
10188d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
10198d213e1aSmatthias.ringwald             switch (power_mode){
10208d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10218d213e1aSmatthias.ringwald                     err = hci_power_control_on();
10228d213e1aSmatthias.ringwald                     if (err) return err;
10237301ad89Smatthias.ringwald                     // set up state machine
10247301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
10257301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10267301ad89Smatthias.ringwald                     hci_stack.substate = 0;
10278d213e1aSmatthias.ringwald                     break;
10288d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10298d213e1aSmatthias.ringwald                     // do nothing
10308d213e1aSmatthias.ringwald                     break;
10318d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1032b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
10338d213e1aSmatthias.ringwald                     break;
10348d213e1aSmatthias.ringwald             }
10358d213e1aSmatthias.ringwald             break;
10367301ad89Smatthias.ringwald 
10378d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10388d213e1aSmatthias.ringwald             switch (power_mode){
10398d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10408d213e1aSmatthias.ringwald                     // do nothing
10418d213e1aSmatthias.ringwald                     break;
10428d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10438d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
10448d213e1aSmatthias.ringwald                     hci_power_control_off();
10458d213e1aSmatthias.ringwald                     break;
10468d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1047b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
104872ea5239Smatthias.ringwald                     hci_power_control_sleep();
10498d213e1aSmatthias.ringwald                     break;
10508d213e1aSmatthias.ringwald             }
10518d213e1aSmatthias.ringwald             break;
10527301ad89Smatthias.ringwald 
10538d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
10548d213e1aSmatthias.ringwald             switch (power_mode){
10558d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10568d213e1aSmatthias.ringwald                     // do nothing
10578d213e1aSmatthias.ringwald                     break;
10588d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1059c7e0c5f6Smatthias.ringwald                     // see hci_run
1060c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10618d213e1aSmatthias.ringwald                     break;
10628d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1063b546ac54Smatthias.ringwald                     // see hci_run
1064b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
106589db417bSmatthias.ringwald                     hci_stack.substate = 0;
10668d213e1aSmatthias.ringwald                     break;
10678d213e1aSmatthias.ringwald             }
10688d213e1aSmatthias.ringwald             break;
10697301ad89Smatthias.ringwald 
10708d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
10718d213e1aSmatthias.ringwald             switch (power_mode){
10728d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10738d213e1aSmatthias.ringwald                     // set up state machine
10748d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10758d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
10768d213e1aSmatthias.ringwald                     break;
10778d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10788d213e1aSmatthias.ringwald                     // do nothing
10798d213e1aSmatthias.ringwald                     break;
10808d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1081b546ac54Smatthias.ringwald                     // see hci_run
1082b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
108389db417bSmatthias.ringwald                     hci_stack.substate = 0;
10848d213e1aSmatthias.ringwald                     break;
10858d213e1aSmatthias.ringwald             }
10868d213e1aSmatthias.ringwald             break;
10878d213e1aSmatthias.ringwald 
10888d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
10898d213e1aSmatthias.ringwald             switch (power_mode){
10908d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
109128171530Smatthias.ringwald 
109228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
109328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
109428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10958747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1096da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
109728171530Smatthias.ringwald                         break;
109828171530Smatthias.ringwald                     }
109928171530Smatthias.ringwald #endif
1100b546ac54Smatthias.ringwald                     // set up state machine
1101b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1102b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1103b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
11048d213e1aSmatthias.ringwald                     break;
11058d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1106b546ac54Smatthias.ringwald                     // see hci_run
1107b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
11088d213e1aSmatthias.ringwald                     break;
11098d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1110b546ac54Smatthias.ringwald                     // do nothing
11118d213e1aSmatthias.ringwald                     break;
11128d213e1aSmatthias.ringwald             }
11138d213e1aSmatthias.ringwald             break;
11148d213e1aSmatthias.ringwald 
11158d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
11168d213e1aSmatthias.ringwald             switch (power_mode){
11178d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
111828171530Smatthias.ringwald 
111928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
112028171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
112128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11228747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1123da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1124758b46ceSmatthias.ringwald                         hci_update_scan_enable();
112528171530Smatthias.ringwald                         break;
112628171530Smatthias.ringwald                     }
112728171530Smatthias.ringwald #endif
11283144bce4Smatthias.ringwald                     err = hci_power_control_wake();
11293144bce4Smatthias.ringwald                     if (err) return err;
1130b546ac54Smatthias.ringwald                     // set up state machine
1131b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1132b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1133b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
11348d213e1aSmatthias.ringwald                     break;
11358d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11364ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
11378d213e1aSmatthias.ringwald                     break;
11388d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1139b546ac54Smatthias.ringwald                     // do nothing
11408d213e1aSmatthias.ringwald                     break;
11418d213e1aSmatthias.ringwald             }
11428d213e1aSmatthias.ringwald             break;
114311e23e5fSmatthias.ringwald     }
114468d92d03Smatthias.ringwald 
1145038bc64cSmatthias.ringwald     // create internal event
1146ee8bf225Smatthias.ringwald 	hci_emit_state();
1147ee8bf225Smatthias.ringwald 
114868d92d03Smatthias.ringwald 	// trigger next/first action
114968d92d03Smatthias.ringwald 	hci_run();
115068d92d03Smatthias.ringwald 
1151475c8125Smatthias.ringwald     return 0;
1152475c8125Smatthias.ringwald }
1153475c8125Smatthias.ringwald 
1154758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1155758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1156758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1157758b46ceSmatthias.ringwald     hci_run();
1158758b46ceSmatthias.ringwald }
1159758b46ceSmatthias.ringwald 
1160381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1161381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1162381fbed8Smatthias.ringwald 
1163381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1164381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1165381fbed8Smatthias.ringwald         return;
1166381fbed8Smatthias.ringwald     }
1167381fbed8Smatthias.ringwald 
1168381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1169758b46ceSmatthias.ringwald     hci_update_scan_enable();
1170758b46ceSmatthias.ringwald }
1171b031bebbSmatthias.ringwald 
1172758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1173758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1174758b46ceSmatthias.ringwald 
1175758b46ceSmatthias.ringwald     // don't emit event
1176758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1177758b46ceSmatthias.ringwald 
1178758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1179758b46ceSmatthias.ringwald     hci_update_scan_enable();
1180381fbed8Smatthias.ringwald }
1181381fbed8Smatthias.ringwald 
11825061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
11835061f3afS[email protected]     return &hci_stack.local_bd_addr;
11845061f3afS[email protected] }
11855061f3afS[email protected] 
118606b35ec0Smatthias.ringwald void hci_run(){
11878a485f27Smatthias.ringwald 
118832ab9390Smatthias.ringwald     hci_connection_t * connection;
118932ab9390Smatthias.ringwald     linked_item_t * it;
119032ab9390Smatthias.ringwald 
1191ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1192ce4c8fabSmatthias.ringwald 
1193b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1194b031bebbSmatthias.ringwald 
1195b031bebbSmatthias.ringwald     // decline incoming connections
1196ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1197ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1198ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1199ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1200dbe1a790S[email protected]         return;
1201ce4c8fabSmatthias.ringwald     }
1202ce4c8fabSmatthias.ringwald 
1203b031bebbSmatthias.ringwald     // send scan enable
120492368cd3S[email protected]     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){
1205b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1206b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1207dbe1a790S[email protected]         return;
1208b031bebbSmatthias.ringwald     }
1209b031bebbSmatthias.ringwald 
121032ab9390Smatthias.ringwald     // send pending HCI commands
121132ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
121232ab9390Smatthias.ringwald 
1213b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
121432ab9390Smatthias.ringwald 
121532ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
12167b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
121732ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
121832ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1219dbe1a790S[email protected]             return;
1220c7e0c5f6Smatthias.ringwald         }
1221c7e0c5f6Smatthias.ringwald 
122232ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
122332ab9390Smatthias.ringwald             link_key_t link_key;
1224c77e8838S[email protected]             link_key_type_t link_key_type;
12257b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
1226c77e8838S[email protected]             if ( hci_stack.bondable && hci_stack.remote_device_db && hci_stack.remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
122732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
1228*536f9994S[email protected]                if (link_key_type == LINK_KEY_TYPE_AUTHENTICATED){
1229*536f9994S[email protected]                     connectionSetAuthenticationFlags(connection, HAVE_AUTHENTICATED_LINK_KEY);
1230*536f9994S[email protected]                }
123132ab9390Smatthias.ringwald             } else {
123232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
123332ab9390Smatthias.ringwald             }
123428ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
1235dbe1a790S[email protected]             return;
123632ab9390Smatthias.ringwald         }
12371d6b20aeS[email protected] 
12384c57c146S[email protected]         if (connection->authentication_flags & HANDLE_PIN_CODE_REQUEST){
12394c57c146S[email protected]             log_info("denying to pin request\n");
12404c57c146S[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
12414c57c146S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_PIN_CODE_REQUEST);
12424c57c146S[email protected]             return;
12434c57c146S[email protected]         }
12444c57c146S[email protected] 
1245dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
1246f8fb5f6eS[email protected]             if (hci_stack.bondable){
1247dbe1a790S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement);
1248f8fb5f6eS[email protected]             } else {
1249f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1250f8fb5f6eS[email protected]             }
1251dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
1252dbe1a790S[email protected]             return;
125332ab9390Smatthias.ringwald         }
125432ab9390Smatthias.ringwald 
1255dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1256dbe1a790S[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1257dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
1258dbe1a790S[email protected]             return;
1259dbe1a790S[email protected]         }
1260dbe1a790S[email protected] 
1261dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1262dbe1a790S[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1263dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
1264dbe1a790S[email protected]             return;
1265dbe1a790S[email protected]         }
1266afd4e962S[email protected] 
1267afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1268afd4e962S[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
1269afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
1270afd4e962S[email protected]         }
1271dbe1a790S[email protected]     }
1272c7e0c5f6Smatthias.ringwald 
12733429f56bSmatthias.ringwald     switch (hci_stack.state){
12743429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
12757b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
12763429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
12773429f56bSmatthias.ringwald                 // odd: waiting for command completion
127806b35ec0Smatthias.ringwald                 return;
12793429f56bSmatthias.ringwald             }
128090919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1281c7492964Smatthias.ringwald                 case 0: // RESET
128222909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
128324052c2aS[email protected] 
1284c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1285c7492964Smatthias.ringwald                         // skip baud change
1286c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1287c7492964Smatthias.ringwald                     }
12883429f56bSmatthias.ringwald                     break;
1289c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
12907dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
12917dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1292c7492964Smatthias.ringwald                     break;
1293c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1294c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1295c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1296c7492964Smatthias.ringwald                     // break missing here for fall through
1297c7492964Smatthias.ringwald 
1298c7492964Smatthias.ringwald                 case 3:
129924052c2aS[email protected]                     // Custom initialization
1300db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
13017dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1302d62aca9fSmatthias.ringwald                         if (valid_cmd){
13037dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
13047dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1305c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
130690919203Smatthias.ringwald                             break;
130790919203Smatthias.ringwald                         }
1308d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
130990919203Smatthias.ringwald                     }
13102f6c30e1Smatthias.ringwald                     // otherwise continue
1311f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1312f432a6ddSmatthias.ringwald 					break;
1313c7492964Smatthias.ringwald 				case 4:
1314e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1315e2edc0c3Smatthias.ringwald 					break;
1316c7492964Smatthias.ringwald                 case 5:
131765389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
13183429f56bSmatthias.ringwald                     break;
1319c7492964Smatthias.ringwald                 case 6:
1320d7e0e3a8S[email protected]                     if (hci_le_supported()){
1321d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1322d7e0e3a8S[email protected]                     } else {
1323d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1324d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1325d7e0e3a8S[email protected]                     }
1326f5d8d141S[email protected] 
1327f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
132824052c2aS[email protected]                     if (!hci_classic_supported()){
132924052c2aS[email protected]                         if (hci_le_supported()){
1330f5d8d141S[email protected]                             hci_stack.substate = 11 << 1;    // skip all classic command
133124052c2aS[email protected]                         } else {
133224052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
1333d7e0e3a8S[email protected]                             hci_stack.substate = 13 << 1;    // skip all
133424052c2aS[email protected]                         }
1335f5d8d141S[email protected]                     }
1336f5d8d141S[email protected]                     break;
1337f5d8d141S[email protected]                 case 7:
133824052c2aS[email protected]                     if (hci_ssp_supported()){
1339f5d8d141S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1340f5d8d141S[email protected]                         break;
134124052c2aS[email protected]                     }
134224052c2aS[email protected]                     hci_stack.substate += 2;
134324052c2aS[email protected]                     // break missing here for fall through
134424052c2aS[email protected] 
1345f5d8d141S[email protected]                 case 8:
134665389bfcS[email protected]                     // ca. 15 sec
134765389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1348559e517eS[email protected]                     break;
1349f5d8d141S[email protected]                 case 9:
1350e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1351e2386ba1S[email protected]                     break;
1352f5d8d141S[email protected]                 case 10:
1353e2386ba1S[email protected]                     if (hci_stack.local_name){
1354e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1355e2386ba1S[email protected]                     } else {
13568a485f27Smatthias.ringwald                         char hostname[30];
1357e2386ba1S[email protected] #ifdef EMBEDDED
1358e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1359e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1360e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1361e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1362e2386ba1S[email protected] #else
1363e2386ba1S[email protected]                         // hostname for POSIX systems
13648a485f27Smatthias.ringwald                         gethostname(hostname, 30);
13658a485f27Smatthias.ringwald                         hostname[29] = '\0';
1366e2386ba1S[email protected] #endif
13678a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
13688a485f27Smatthias.ringwald                     }
13693c4d4b90Smatthias.ringwald                     break;
1370e0dbca83S[email protected]                 case 11:
1371a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
137224052c2aS[email protected]                     if (!hci_le_supported()){
137324052c2aS[email protected]                         // SKIP LE init for Classic only configuration
137424052c2aS[email protected]                         hci_stack.substate = 13 << 1;
137524052c2aS[email protected]                     }
1376a45d6b9fS[email protected] 					break;
137724052c2aS[email protected] 
137843aa7007S[email protected] #ifdef HAVE_BLE
137924052c2aS[email protected]                 // LE INIT
1380a45d6b9fS[email protected]                 case 12:
138124052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
138224052c2aS[email protected]                     break;
138324052c2aS[email protected]                 case 13:
138424052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
138524052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
138624052c2aS[email protected]                     break;
138743aa7007S[email protected] #endif
138824052c2aS[email protected] 
138924052c2aS[email protected]                 // DONE
139024052c2aS[email protected]                 case 14:
13913429f56bSmatthias.ringwald                     // done.
13923429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1393b360b6adSmatthias.ringwald                     hci_emit_state();
13943429f56bSmatthias.ringwald                     break;
13953429f56bSmatthias.ringwald                 default:
13963429f56bSmatthias.ringwald                     break;
1397475c8125Smatthias.ringwald             }
13983429f56bSmatthias.ringwald             hci_stack.substate++;
13993429f56bSmatthias.ringwald             break;
1400c7e0c5f6Smatthias.ringwald 
1401c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1402c7e0c5f6Smatthias.ringwald 
14037b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1404c7e0c5f6Smatthias.ringwald             // close all open connections
1405c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1406c7e0c5f6Smatthias.ringwald             if (connection){
140732ab9390Smatthias.ringwald 
1408c7e0c5f6Smatthias.ringwald                 // send disconnect
140932ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
141032ab9390Smatthias.ringwald 
141179bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
14126ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1413c7e0c5f6Smatthias.ringwald 
1414c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1415c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1416c7e0c5f6Smatthias.ringwald                 return;
1417c7e0c5f6Smatthias.ringwald             }
14187b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1419c7e0c5f6Smatthias.ringwald 
142072ea5239Smatthias.ringwald             // switch mode
1421c7e0c5f6Smatthias.ringwald             hci_power_control_off();
14229418f9c9Smatthias.ringwald 
14237b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
142472ea5239Smatthias.ringwald             hci_emit_state();
14257b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
142672ea5239Smatthias.ringwald             break;
1427c7e0c5f6Smatthias.ringwald 
142872ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
142989db417bSmatthias.ringwald             switch(hci_stack.substate) {
143089db417bSmatthias.ringwald                 case 0:
14317b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
143272ea5239Smatthias.ringwald                     // close all open connections
143372ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
143466da7044Smatthias.ringwald 
143528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
143666da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
143766da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
143866da7044Smatthias.ringwald                         connection = NULL;
143966da7044Smatthias.ringwald                     }
144066da7044Smatthias.ringwald #endif
144172ea5239Smatthias.ringwald                     if (connection){
144232ab9390Smatthias.ringwald 
144372ea5239Smatthias.ringwald                         // send disconnect
144432ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
144532ab9390Smatthias.ringwald 
144679bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
14476ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
144872ea5239Smatthias.ringwald 
144972ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
145072ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
145172ea5239Smatthias.ringwald                         return;
145272ea5239Smatthias.ringwald                     }
145372ea5239Smatthias.ringwald 
145492368cd3S[email protected]                     if (hci_classic_supported()){
145589db417bSmatthias.ringwald                         // disable page and inquiry scan
145632ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
145732ab9390Smatthias.ringwald 
145892368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
1459758b46ceSmatthias.ringwald                         hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
146089db417bSmatthias.ringwald 
146189db417bSmatthias.ringwald                         // continue in next sub state
146289db417bSmatthias.ringwald                         hci_stack.substate++;
146389db417bSmatthias.ringwald                         break;
146492368cd3S[email protected]                     }
146592368cd3S[email protected]                     // fall through for ble-only chips
146692368cd3S[email protected] 
146789db417bSmatthias.ringwald                 case 2:
14687b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
146928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
147028171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
147128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
147228171530Smatthias.ringwald                         // SLEEP MODE reached
147328171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
147428171530Smatthias.ringwald                         hci_emit_state();
147528171530Smatthias.ringwald                         break;
147628171530Smatthias.ringwald                     }
147728171530Smatthias.ringwald #endif
147872ea5239Smatthias.ringwald                     // switch mode
147989db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1480c7e0c5f6Smatthias.ringwald                     hci_emit_state();
148128171530Smatthias.ringwald                     break;
148228171530Smatthias.ringwald 
148389db417bSmatthias.ringwald                 default:
148489db417bSmatthias.ringwald                     break;
148589db417bSmatthias.ringwald             }
1486c7e0c5f6Smatthias.ringwald             break;
1487c7e0c5f6Smatthias.ringwald 
14883429f56bSmatthias.ringwald         default:
14893429f56bSmatthias.ringwald             break;
14901f504dbdSmatthias.ringwald     }
14913429f56bSmatthias.ringwald }
149216833f0aSmatthias.ringwald 
149331452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1494c8e4258aSmatthias.ringwald     bd_addr_t addr;
1495c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1496c8e4258aSmatthias.ringwald     // house-keeping
1497c8e4258aSmatthias.ringwald 
1498c8e4258aSmatthias.ringwald     // create_connection?
1499c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1500c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1501339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1502c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1503c8e4258aSmatthias.ringwald         if (conn) {
1504c8e4258aSmatthias.ringwald             // if connection exists
1505c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
150617f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
150717f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1508c8e4258aSmatthias.ringwald             }
150917f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1510c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1511c8e4258aSmatthias.ringwald 
151217f1ba2aSmatthias.ringwald         }
1513c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
151417f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
151517f1ba2aSmatthias.ringwald         if (!conn){
151617f1ba2aSmatthias.ringwald             // notify client that alloc failed
151717f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
151817f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
151917f1ba2aSmatthias.ringwald         }
1520c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1521c8e4258aSmatthias.ringwald     }
1522c8e4258aSmatthias.ringwald 
15237fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
15247fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
15257fde4af9Smatthias.ringwald     }
15267fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
15277fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
15287fde4af9Smatthias.ringwald     }
1529*536f9994S[email protected]     // Link key generated from PIN implies MITM protecion and authentication
1530*536f9994S[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
1531*536f9994S[email protected]         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], HAVE_AUTHENTICATED_LINK_KEY);
1532*536f9994S[email protected]     }
1533f0944df2S[email protected]     // if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
1534f0944df2S[email protected]     //     hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
1535f0944df2S[email protected]     // }
15367fde4af9Smatthias.ringwald 
15378ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
15388ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
15398ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
15408ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
15418ef73945Smatthias.ringwald         }
15428ef73945Smatthias.ringwald     }
1543c8e4258aSmatthias.ringwald 
154469a97523S[email protected] #ifdef HAVE_BLE
154569a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
154669a97523S[email protected]         hci_stack.adv_addr_type = packet[8];
154769a97523S[email protected]     }
154869a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
154969a97523S[email protected]         bt_flip_addr(hci_stack.adv_address, &packet[3]);
155069a97523S[email protected]     }
155169a97523S[email protected] #endif
155269a97523S[email protected] 
155369a97523S[email protected] 
155431452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1555622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
155631452debSmatthias.ringwald }
15578adf0ddaSmatthias.ringwald 
1558dbe1a790S[email protected] // Configure Secure Simple Pairing
1559dbe1a790S[email protected] 
1560dbe1a790S[email protected] // enable will enable SSP during init
1561dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
1562dbe1a790S[email protected]     hci_stack.ssp_enable = enable;
1563dbe1a790S[email protected] }
1564dbe1a790S[email protected] 
1565dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1566dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
1567dbe1a790S[email protected]     hci_stack.ssp_io_capability = io_capability;
1568dbe1a790S[email protected] }
1569dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
1570dbe1a790S[email protected]     hci_stack.ssp_authentication_requirement = authentication_requirement;
1571dbe1a790S[email protected] }
1572dbe1a790S[email protected] 
1573dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1574dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
1575dbe1a790S[email protected]     hci_stack.ssp_auto_accept = auto_accept;
1576dbe1a790S[email protected] }
1577dbe1a790S[email protected] 
15781cd208adSmatthias.ringwald /**
15791cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
15801cd208adSmatthias.ringwald  */
1581fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
15821cd208adSmatthias.ringwald     va_list argptr;
15831cd208adSmatthias.ringwald     va_start(argptr, cmd);
15847dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
15851cd208adSmatthias.ringwald     va_end(argptr);
15867dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
158793b8dc03Smatthias.ringwald }
1588c8e4258aSmatthias.ringwald 
1589ee091cf1Smatthias.ringwald // Create various non-HCI events.
1590ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1591ee091cf1Smatthias.ringwald 
1592c8e4258aSmatthias.ringwald void hci_emit_state(){
1593e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1594425d1371Smatthias.ringwald     uint8_t event[3];
159580d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1596425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1597c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1598425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1599425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1600c8e4258aSmatthias.ringwald }
1601c8e4258aSmatthias.ringwald 
160217f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1603425d1371Smatthias.ringwald     uint8_t event[13];
1604c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1605425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
160617f1ba2aSmatthias.ringwald     event[2] = status;
1607c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1608c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1609c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1610c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1611425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1612425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1613c8e4258aSmatthias.ringwald }
1614c8e4258aSmatthias.ringwald 
16153c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1616425d1371Smatthias.ringwald     uint8_t event[6];
16173c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1618e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
16193c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
16203c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
16213c4d4b90Smatthias.ringwald     event[5] = reason;
1622425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1623425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
16243c4d4b90Smatthias.ringwald }
16253c4d4b90Smatthias.ringwald 
1626ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1627e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1628425d1371Smatthias.ringwald     uint8_t event[4];
162980d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1630425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1631ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1632425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1633425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1634ee091cf1Smatthias.ringwald }
163543bfb1bdSmatthias.ringwald 
163643bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1637e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1638425d1371Smatthias.ringwald     uint8_t event[3];
163980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1640425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
164143bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1642425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1643425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
164443bfb1bdSmatthias.ringwald }
1645038bc64cSmatthias.ringwald 
1646038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1647e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1648425d1371Smatthias.ringwald     uint8_t event[2];
164980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1650425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1651425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1652425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1653038bc64cSmatthias.ringwald }
16541b0e3922Smatthias.ringwald 
165509ba8edeSmatthias.ringwald #ifndef EMBEDDED
16561b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1657e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1658425d1371Smatthias.ringwald     uint8_t event[6];
16591b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1660425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1661425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1662425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1663425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1664425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1665425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
16661b0e3922Smatthias.ringwald }
166709ba8edeSmatthias.ringwald #endif
16681b0e3922Smatthias.ringwald 
16692ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1670e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1671425d1371Smatthias.ringwald     uint8_t event[3];
16722ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1673425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
16742ed6235cSmatthias.ringwald     event[2] = enabled;
1675425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1676e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
16772ed6235cSmatthias.ringwald }
1678627c2f45Smatthias.ringwald 
1679627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1680e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1681627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1682e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1683f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
16842f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1685f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1686e0abb8e7S[email protected] 
1687e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1688e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1689e0abb8e7S[email protected] 
1690e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1691e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1692627c2f45Smatthias.ringwald }
1693381fbed8Smatthias.ringwald 
1694381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1695e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1696425d1371Smatthias.ringwald     uint8_t event[3];
1697381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1698425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1699381fbed8Smatthias.ringwald     event[2] = enabled;
1700425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1701425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1702381fbed8Smatthias.ringwald }
1703458bf4e8S[email protected] 
1704458bf4e8S[email protected] // GAP API
1705458bf4e8S[email protected] /**
1706458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
1707458bf4e8S[email protected]  * @praram enabled
1708458bf4e8S[email protected]  */
17094c57c146S[email protected] void gap_set_bondable_mode(int enable){
1710458bf4e8S[email protected]     hci_stack.bondable = enable ? 1 : 0;
1711458bf4e8S[email protected] }
1712cb230b9dS[email protected] 
1713cb230b9dS[email protected] /**
1714cb230b9dS[email protected]  * @brief get current security level
1715cb230b9dS[email protected]  */
1716cb230b9dS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
1717cb230b9dS[email protected]     return LEVEL_0;
1718cb230b9dS[email protected] }
1719cb230b9dS[email protected] 
1720cb230b9dS[email protected] /**
1721cb230b9dS[email protected]  * @brief request connection to device to
1722cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
1723cb230b9dS[email protected]  */
1724cb230b9dS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1725cb230b9dS[email protected] }
1726