xref: /btstack/src/hci.c (revision 97b61c7b5a5a3a680fa32c9bcc47653c62553427)
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 
44bde315ceS[email protected] #include "btstack-config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
474c57c146S[email protected] #include "gap.h"
487f2435e6Smatthias.ringwald 
4993b8dc03Smatthias.ringwald #include <stdarg.h>
5093b8dc03Smatthias.ringwald #include <string.h>
5156fe0872Smatthias.ringwald #include <stdio.h>
527f2435e6Smatthias.ringwald 
53549e6ebeSmatthias.ringwald #ifndef EMBEDDED
54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5509ba8edeSmatthias.ringwald #include <btstack/version.h>
56549e6ebeSmatthias.ringwald #endif
57549e6ebeSmatthias.ringwald 
58a3b02b71Smatthias.ringwald #include "btstack_memory.h"
597f2435e6Smatthias.ringwald #include "debug.h"
60d8905019Smatthias.ringwald #include "hci_dump.h"
6193b8dc03Smatthias.ringwald 
62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
631b0e3922Smatthias.ringwald 
64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
65ee091cf1Smatthias.ringwald 
66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
67da5275c5S[email protected] 
6828171530Smatthias.ringwald #ifdef USE_BLUETOOL
6928171530Smatthias.ringwald #include "bt_control_iphone.h"
7028171530Smatthias.ringwald #endif
7128171530Smatthias.ringwald 
72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
7496a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer);
7596a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
767586ee35S[email protected] static int  hci_power_control_on(void);
777586ee35S[email protected] static void hci_power_control_off(void);
78758b46ceSmatthias.ringwald 
7906b35ec0Smatthias.ringwald // the STACK is here
803a9fb326S[email protected] #ifndef HAVE_MALLOC
813a9fb326S[email protected] static hci_stack_t   hci_stack_static;
823a9fb326S[email protected] #endif
833a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8416833f0aSmatthias.ringwald 
8566fb9560S[email protected] // test helper
8666fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
8766fb9560S[email protected] 
8896a45072S[email protected] /**
8996a45072S[email protected]  * create connection for given address
9096a45072S[email protected]  *
9196a45072S[email protected]  * @return connection OR NULL, if no memory left
9296a45072S[email protected]  */
9396a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
9496a45072S[email protected] 
9596a45072S[email protected]     printf("create_connection_for_addr %s\n", bd_addr_to_str(addr));
9696a45072S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
9796a45072S[email protected]     if (!conn) return NULL;
9896a45072S[email protected]     BD_ADDR_COPY(conn->address, addr);
9996a45072S[email protected]     conn->address_type = addr_type;
10096a45072S[email protected]     conn->con_handle = 0xffff;
10196a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
10296a45072S[email protected]     conn->bonding_flags = 0;
10396a45072S[email protected]     conn->requested_security_level = LEVEL_0;
10496a45072S[email protected]     linked_item_set_user(&conn->timeout.item, conn);
10596a45072S[email protected]     conn->timeout.process = hci_connection_timeout_handler;
10696a45072S[email protected]     hci_connection_timestamp(conn);
10796a45072S[email protected]     conn->acl_recombination_length = 0;
10896a45072S[email protected]     conn->acl_recombination_pos = 0;
10996a45072S[email protected]     conn->num_acl_packets_sent = 0;
11096a45072S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
11196a45072S[email protected]     return conn;
11296a45072S[email protected] }
11366fb9560S[email protected] 
11497addcc5Smatthias.ringwald /**
115ee091cf1Smatthias.ringwald  * get connection for a given handle
116ee091cf1Smatthias.ringwald  *
117ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
118ee091cf1Smatthias.ringwald  */
1195061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
120ee091cf1Smatthias.ringwald     linked_item_t *it;
1213a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
122ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
123ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
124ee091cf1Smatthias.ringwald         }
125ee091cf1Smatthias.ringwald     }
126ee091cf1Smatthias.ringwald     return NULL;
127ee091cf1Smatthias.ringwald }
128ee091cf1Smatthias.ringwald 
12996a45072S[email protected] /**
13096a45072S[email protected]  * get connection for given address
13196a45072S[email protected]  *
13296a45072S[email protected]  * @return connection OR NULL, if not found
13396a45072S[email protected]  */
13496a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){
13562bda3fbS[email protected]     linked_item_t *it;
13662bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
13762bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
13896a45072S[email protected]         if (connection->address_type != addr_type)  continue;
13996a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
14062bda3fbS[email protected]         return connection;
14162bda3fbS[email protected]     }
14262bda3fbS[email protected]     return NULL;
14362bda3fbS[email protected] }
14462bda3fbS[email protected] 
1452b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
14628ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
147c785ef68Smatthias.ringwald #ifdef HAVE_TIME
148ee091cf1Smatthias.ringwald     struct timeval tv;
149ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
150c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
151ee091cf1Smatthias.ringwald         // connections might be timed out
152ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
153ee091cf1Smatthias.ringwald     }
1542b12a0b9Smatthias.ringwald #endif
155e5780900Smatthias.ringwald #ifdef HAVE_TICK
156c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
157c785ef68Smatthias.ringwald         // connections might be timed out
158c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
159c785ef68Smatthias.ringwald     }
160c785ef68Smatthias.ringwald #endif
161c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
162c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
163c785ef68Smatthias.ringwald }
164ee091cf1Smatthias.ringwald 
165ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
166c7492964Smatthias.ringwald #ifdef HAVE_TIME
167ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
168c7492964Smatthias.ringwald #endif
169e5780900Smatthias.ringwald #ifdef HAVE_TICK
170c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
171c785ef68Smatthias.ringwald #endif
172ee091cf1Smatthias.ringwald }
173ee091cf1Smatthias.ringwald 
17406b35ec0Smatthias.ringwald 
17528ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
17628ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
17728ca2b46S[email protected] }
17828ca2b46S[email protected] 
17928ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18028ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
18128ca2b46S[email protected] }
18228ca2b46S[email protected] 
18328ca2b46S[email protected] 
18443bfb1bdSmatthias.ringwald /**
18580ca58a0Smatthias.ringwald  * add authentication flags and reset timer
18696a45072S[email protected]  * @note: assumes classic connection
1877fde4af9Smatthias.ringwald  */
1887fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1897fde4af9Smatthias.ringwald     bd_addr_t addr;
1907fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
19196a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1927fde4af9Smatthias.ringwald     if (conn) {
19328ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
19480ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1957fde4af9Smatthias.ringwald     }
1967fde4af9Smatthias.ringwald }
1977fde4af9Smatthias.ringwald 
19880ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
1995061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
20080ca58a0Smatthias.ringwald     if (!conn) return 0;
2016724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2026724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2036724cd9eS[email protected]     return 0;
20480ca58a0Smatthias.ringwald }
20580ca58a0Smatthias.ringwald 
206c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2073a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2083a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
209c12e46e7Smatthias.ringwald     }
210c12e46e7Smatthias.ringwald }
211c12e46e7Smatthias.ringwald 
2120bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){
2130bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2140bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2150bf6344aS[email protected] }
2160bf6344aS[email protected] 
2177fde4af9Smatthias.ringwald 
2187fde4af9Smatthias.ringwald /**
21943bfb1bdSmatthias.ringwald  * count connections
22043bfb1bdSmatthias.ringwald  */
22140d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
22256c253c9Smatthias.ringwald     int count = 0;
22343bfb1bdSmatthias.ringwald     linked_item_t *it;
2243a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
22543bfb1bdSmatthias.ringwald     return count;
22643bfb1bdSmatthias.ringwald }
227c8e4258aSmatthias.ringwald 
22897addcc5Smatthias.ringwald /**
229ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
23016833f0aSmatthias.ringwald  */
2312718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
23216833f0aSmatthias.ringwald }
23316833f0aSmatthias.ringwald 
234998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2355061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
236998906cdSmatthias.ringwald     if (!connection) {
2377d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
238998906cdSmatthias.ringwald         return 0;
239998906cdSmatthias.ringwald     }
240998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
241998906cdSmatthias.ringwald }
242998906cdSmatthias.ringwald 
243998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
2443a9fb326S[email protected]     uint8_t free_slots = hci_stack->total_num_acl_packets;
245998906cdSmatthias.ringwald     linked_item_t *it;
2463a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
247998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
248998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2497d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
250998906cdSmatthias.ringwald             return 0;
251998906cdSmatthias.ringwald         }
252998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
253998906cdSmatthias.ringwald     }
254998906cdSmatthias.ringwald     return free_slots;
255998906cdSmatthias.ringwald }
256998906cdSmatthias.ringwald 
257c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2582b12a0b9Smatthias.ringwald 
2592b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2603a9fb326S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
2613a9fb326S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){
2622b12a0b9Smatthias.ringwald             return 0;
2632b12a0b9Smatthias.ringwald         }
2642b12a0b9Smatthias.ringwald     }
2652b12a0b9Smatthias.ringwald 
2662b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
267c24735b1Smatthias.ringwald     switch (packet_type) {
268c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
269c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
270c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
2713a9fb326S[email protected]             return hci_stack->num_cmd_packets;
272c24735b1Smatthias.ringwald         default:
273c24735b1Smatthias.ringwald             return 0;
274c24735b1Smatthias.ringwald     }
275c24735b1Smatthias.ringwald }
276c24735b1Smatthias.ringwald 
2776b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use
2786b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
2796b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2806b4af23dS[email protected]     return hci_can_send_packet_now(packet_type);
2816b4af23dS[email protected] }
2826b4af23dS[email protected] 
283c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
284c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
285c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
286c8b9416aS[email protected] }
287c8b9416aS[email protected] 
2886b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
2896b4af23dS[email protected] int hci_reserve_packet_buffer(void){
2906b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2916b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
2926b4af23dS[email protected]     return 1;
2936b4af23dS[email protected] }
2946b4af23dS[email protected] 
29568a0fcf7S[email protected] void hci_release_packet_buffer(void){
29668a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
29768a0fcf7S[email protected] }
29868a0fcf7S[email protected] 
2996b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
3006b4af23dS[email protected] int hci_transport_synchronous(void){
3016b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
3026b4af23dS[email protected] }
3036b4af23dS[email protected] 
304ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
3057856c818Smatthias.ringwald 
3066218e6f1Smatthias.ringwald     // check for free places on BT module
307*97b61c7bS[email protected]     if (!hci_number_free_acl_slots()) {
308*97b61c7bS[email protected]         hci_release_packet_buffer();
309*97b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
310*97b61c7bS[email protected]     }
3116218e6f1Smatthias.ringwald 
3127856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3135061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
314*97b61c7bS[email protected]     if (!connection) {
315*97b61c7bS[email protected]         hci_release_packet_buffer();
316*97b61c7bS[email protected]         return 0;
317*97b61c7bS[email protected]     }
31856cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
31956cf178bSmatthias.ringwald 
32056cf178bSmatthias.ringwald     // count packet
32156cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
3227b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
3237856c818Smatthias.ringwald 
32400d8e42eSmatthias.ringwald     // send packet
3253a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
3266218e6f1Smatthias.ringwald 
3276b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
328c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
329*97b61c7bS[email protected]         hci_release_packet_buffer();
3306b4af23dS[email protected]     }
3316b4af23dS[email protected] 
33200d8e42eSmatthias.ringwald     return err;
333ee091cf1Smatthias.ringwald }
334ee091cf1Smatthias.ringwald 
33516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
3367856c818Smatthias.ringwald 
337e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
338e76a89eeS[email protected] 
3397856c818Smatthias.ringwald     // get info
3407856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3415061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
3427856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
3437856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
3447856c818Smatthias.ringwald 
3457856c818Smatthias.ringwald     // ignore non-registered handle
3467856c818Smatthias.ringwald     if (!conn){
3477d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3487856c818Smatthias.ringwald         return;
3497856c818Smatthias.ringwald     }
3507856c818Smatthias.ringwald 
351e76a89eeS[email protected]     // assert packet is complete
3529ecc3e17S[email protected]     if (acl_length + 4 != size){
353e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
354e76a89eeS[email protected]         return;
355e76a89eeS[email protected]     }
356e76a89eeS[email protected] 
3577856c818Smatthias.ringwald     // update idle timestamp
3587856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3597856c818Smatthias.ringwald 
3607856c818Smatthias.ringwald     // handle different packet types
3617856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3627856c818Smatthias.ringwald 
3637856c818Smatthias.ringwald         case 0x01: // continuation fragment
3647856c818Smatthias.ringwald 
3657856c818Smatthias.ringwald             // sanity check
3667856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3677d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3687856c818Smatthias.ringwald                 return;
3697856c818Smatthias.ringwald             }
3707856c818Smatthias.ringwald 
3717856c818Smatthias.ringwald             // append fragment payload (header already stored)
3727856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3737856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3747856c818Smatthias.ringwald 
3757d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
376decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3777856c818Smatthias.ringwald 
3787856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3797856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3807856c818Smatthias.ringwald 
3813a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3827856c818Smatthias.ringwald                 // reset recombination buffer
3837856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3847856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3857856c818Smatthias.ringwald             }
3867856c818Smatthias.ringwald             break;
3877856c818Smatthias.ringwald 
3887856c818Smatthias.ringwald         case 0x02: { // first fragment
3897856c818Smatthias.ringwald 
3907856c818Smatthias.ringwald             // sanity check
3917856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3927d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3937856c818Smatthias.ringwald                 return;
3947856c818Smatthias.ringwald             }
3957856c818Smatthias.ringwald 
3967856c818Smatthias.ringwald             // peek into L2CAP packet!
3977856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3987856c818Smatthias.ringwald 
399e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
400decc01a8Smatthias.ringwald 
4017856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
4027856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
4037856c818Smatthias.ringwald 
4047856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
4053a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
4067856c818Smatthias.ringwald 
4077856c818Smatthias.ringwald             } else {
4087856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
4097856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
4107856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
4117856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
412decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
4137856c818Smatthias.ringwald             }
4147856c818Smatthias.ringwald             break;
4157856c818Smatthias.ringwald 
4167856c818Smatthias.ringwald         }
4177856c818Smatthias.ringwald         default:
4187d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
4197856c818Smatthias.ringwald             return;
4207856c818Smatthias.ringwald     }
42194ab26f8Smatthias.ringwald 
42294ab26f8Smatthias.ringwald     // execute main loop
42394ab26f8Smatthias.ringwald     hci_run();
42416833f0aSmatthias.ringwald }
42522909952Smatthias.ringwald 
42667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
4271f479f8cS[email protected]     log_info("Connection closed: handle 0x%x, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
4283c4d4b90Smatthias.ringwald 
429c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
430c785ef68Smatthias.ringwald 
4313a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
432a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
4333c4d4b90Smatthias.ringwald 
4343c4d4b90Smatthias.ringwald     // now it's gone
435c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
436c7e0c5f6Smatthias.ringwald }
437c7e0c5f6Smatthias.ringwald 
4380c042179S[email protected] static const uint16_t packet_type_sizes[] = {
4398f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
4408f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
4418f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4428f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4438f8108aaSmatthias.ringwald };
44465389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
44565389bfcS[email protected]      0, // 3 slot packets
44665389bfcS[email protected]      1, // 5 slot packets
44765389bfcS[email protected]     25, // EDR 2 mpbs
44865389bfcS[email protected]     26, // EDR 3 mbps
44965389bfcS[email protected]     39, // 3 slot EDR packts
45065389bfcS[email protected]     40, // 5 slot EDR packet
45165389bfcS[email protected] };
45265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
45365389bfcS[email protected]     0x0f00, // 3 slot packets
45465389bfcS[email protected]     0xf000, // 5 slot packets
45565389bfcS[email protected]     0x1102, // EDR 2 mpbs
45665389bfcS[email protected]     0x2204, // EDR 3 mbps
45765389bfcS[email protected]     0x0300, // 3 slot EDR packts
45865389bfcS[email protected]     0x3000, // 5 slot EDR packet
45965389bfcS[email protected] };
4608f8108aaSmatthias.ringwald 
46165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
46265389bfcS[email protected]     // enable packet types based on size
4638f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4648f8108aaSmatthias.ringwald     int i;
4658f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4668f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4678f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4688f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4698f8108aaSmatthias.ringwald         }
4708f8108aaSmatthias.ringwald     }
47165389bfcS[email protected]     // disable packet types due to missing local supported features
47265389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
47365389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
47465389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
47565389bfcS[email protected]         if (feature_set) continue;
47665389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
47765389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
47865389bfcS[email protected]     }
4798f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4808f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4818f8108aaSmatthias.ringwald     return packet_types;
4828f8108aaSmatthias.ringwald }
4838f8108aaSmatthias.ringwald 
4848f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4853a9fb326S[email protected]     return hci_stack->packet_types;
4868f8108aaSmatthias.ringwald }
4878f8108aaSmatthias.ringwald 
488facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
4897dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4903a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
4917dc17943Smatthias.ringwald }
4927dc17943Smatthias.ringwald 
493f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4943a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
4957dc17943Smatthias.ringwald }
4967dc17943Smatthias.ringwald 
4976ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
4986ac9a97eS[email protected]     // No. 54, byte 6, bit 6
4996ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
5006ac9a97eS[email protected] }
5016ac9a97eS[email protected] 
502f5d8d141S[email protected] int hci_ssp_supported(void){
5036ac9a97eS[email protected]     // No. 51, byte 6, bit 3
5043a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
505f5d8d141S[email protected] }
506f5d8d141S[email protected] 
507f5d8d141S[email protected] int hci_classic_supported(void){
5086ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
5093a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
510f5d8d141S[email protected] }
511f5d8d141S[email protected] 
512f5d8d141S[email protected] int hci_le_supported(void){
513f5d8d141S[email protected] #ifdef HAVE_BLE
5146ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
5153a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
516f5d8d141S[email protected] #else
517f5d8d141S[email protected]     return 0;
518f5d8d141S[email protected] #endif
519f5d8d141S[email protected] }
520f5d8d141S[email protected] 
52169a97523S[email protected] // get addr type and address used in advertisement packets
52218904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
5233a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
5243a9fb326S[email protected]     if (hci_stack->adv_addr_type){
5253a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
52669a97523S[email protected]     } else {
5273a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
52869a97523S[email protected]     }
52969a97523S[email protected] }
53069a97523S[email protected] 
531b2f949feS[email protected] #ifdef HAVE_BLE
532e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){
53357c9da5bS[email protected]     int num_reports = packet[3];
53457c9da5bS[email protected]     int i;
53557c9da5bS[email protected]     int total_data_length = 0;
53657c9da5bS[email protected]     int data_offset = 0;
53757c9da5bS[email protected] 
53857c9da5bS[email protected]     for (i=0; i<num_reports;i++){
53957c9da5bS[email protected]         total_data_length += packet[4+num_reports*8+i];
54057c9da5bS[email protected]     }
54157c9da5bS[email protected] 
54257c9da5bS[email protected]     for (i=0; i<num_reports;i++){
54357c9da5bS[email protected]         int pos = 0;
54457c9da5bS[email protected]         uint8_t data_length = packet[4+num_reports*8+i];
5452b552b23S[email protected]         uint8_t event_size = 10 + data_length;
5462b552b23S[email protected]         uint8_t event[2 + event_size ];
5472b552b23S[email protected]         event[pos++] = GAP_LE_ADVERTISING_REPORT;
54857c9da5bS[email protected]         event[pos++] = event_size;
54957c9da5bS[email protected]         event[pos++] = packet[4+i]; // event_type;
55057c9da5bS[email protected]         event[pos++] = packet[4+num_reports+i]; // address_type;
551564fca32S[email protected]         memcpy(&event[pos], &packet[4+num_reports*2+i*6], 6); // bt address
55257c9da5bS[email protected]         pos += 6;
5532b552b23S[email protected]         event[pos++] = packet[4+num_reports*9+total_data_length + i];
55457c9da5bS[email protected]         event[pos++] = data_length;
55588ed79cfS[email protected]         memcpy(&event[pos], &packet[4+num_reports*9+data_offset], data_length);
55657c9da5bS[email protected]         data_offset += data_length;
55757c9da5bS[email protected]         pos += data_length;
55857c9da5bS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
55957c9da5bS[email protected]     }
56057c9da5bS[email protected] }
561b2f949feS[email protected] #endif
56257c9da5bS[email protected] 
56374ec757aSmatthias.ringwald // avoid huge local variables
564223aafc1Smatthias.ringwald #ifndef EMBEDDED
56574ec757aSmatthias.ringwald static device_name_t device_name;
566223aafc1Smatthias.ringwald #endif
56716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
568e76a89eeS[email protected] 
569e76a89eeS[email protected]     uint16_t event_length = packet[1];
570e76a89eeS[email protected] 
571e76a89eeS[email protected]     // assert packet is complete
572e76a89eeS[email protected]     if (size != event_length + 2){
573e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
574e76a89eeS[email protected]         return;
575e76a89eeS[email protected]     }
576e76a89eeS[email protected] 
5771281a47eSmatthias.ringwald     bd_addr_t addr;
57896a45072S[email protected]     bd_addr_type_t addr_type;
5792d00edd4Smatthias.ringwald     uint8_t link_type;
580fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
5811f7b95a1Smatthias.ringwald     hci_connection_t * conn;
58256cf178bSmatthias.ringwald     int i;
58322909952Smatthias.ringwald 
5845909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
5855909f7f2Smatthias.ringwald 
5866772a24cSmatthias.ringwald     switch (packet[0]) {
58722909952Smatthias.ringwald 
5886772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
5897ec5eeaaSmatthias.ringwald             // get num cmd packets
5903a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
5913a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
5927ec5eeaaSmatthias.ringwald 
593e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
594e2edc0c3Smatthias.ringwald                 // from offset 5
595e2edc0c3Smatthias.ringwald                 // status
5961d279b20Smatthias.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"
5973a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
59856cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
5993a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
60056cf178bSmatthias.ringwald                 // ignore: total num SCO packets
6013a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
602a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
6033a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
6043a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
6058f8108aaSmatthias.ringwald                     }
60665389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
6073a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
608e2edc0c3Smatthias.ringwald                 }
60956cf178bSmatthias.ringwald             }
61065a46ef3S[email protected] #ifdef HAVE_BLE
61165a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
6123a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
6133a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
6143a9fb326S[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);
61501f1657cS[email protected] 
61601f1657cS[email protected]                 // use LE buffers if no clasic buffers have been reported
61701f1657cS[email protected]                 if (hci_stack->total_num_acl_packets == 0){
61801f1657cS[email protected]                     log_info("use le buffers instead of classic ones");
61901f1657cS[email protected]                     hci_stack->total_num_acl_packets  = hci_stack->total_num_le_packets;
62001f1657cS[email protected]                     hci_stack->acl_data_packet_length = hci_stack->le_data_packet_length;
62101f1657cS[email protected]                     // determine usable ACL payload size
62201f1657cS[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
62301f1657cS[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
62401f1657cS[email protected]                     }
62501f1657cS[email protected]                 }
62665a46ef3S[email protected]             }
62765a46ef3S[email protected] #endif
628188981d2Smatthias.ringwald             // Dump local address
629188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
6303a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
631188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
6323a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
633188981d2Smatthias.ringwald             }
634381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
6353a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
636381fbed8Smatthias.ringwald             }
63765389bfcS[email protected]             // Note: HCI init checks
638559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
6393a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
640559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
6413a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
6423a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
6433a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
6443a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
64565389bfcS[email protected] 
64665389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
6473a9fb326S[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]);
6483a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
649f5d8d141S[email protected] 
650f5d8d141S[email protected]                 // Classic/LE
651f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
652559e517eS[email protected]             }
65356cf178bSmatthias.ringwald             break;
65456cf178bSmatthias.ringwald 
6557ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
6567ec5eeaaSmatthias.ringwald             // get num cmd packets
6573a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
6583a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
6597ec5eeaaSmatthias.ringwald             break;
6607ec5eeaaSmatthias.ringwald 
66156cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
66256cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
66356cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
66456cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
6655061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
66656cf178bSmatthias.ringwald                 if (!conn){
6677d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
66856cf178bSmatthias.ringwald                     continue;
66956cf178bSmatthias.ringwald                 }
67056cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
6717b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
67256cf178bSmatthias.ringwald             }
6736772a24cSmatthias.ringwald             break;
6746772a24cSmatthias.ringwald 
6751f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
67637eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
67737eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
6782d00edd4Smatthias.ringwald             link_type = packet[11];
679339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
68037eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
68196a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
6821f7b95a1Smatthias.ringwald                 if (!conn) {
68396a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
6841f7b95a1Smatthias.ringwald                 }
685ce4c8fabSmatthias.ringwald                 if (!conn) {
686ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
6873a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
6883a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
689ce4c8fabSmatthias.ringwald                     break;
690ce4c8fabSmatthias.ringwald                 }
69132ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
69232ab9390Smatthias.ringwald                 hci_run();
69337eaa4cfSmatthias.ringwald             } else {
694ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
6953a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
6963a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
69737eaa4cfSmatthias.ringwald             }
6981f7b95a1Smatthias.ringwald             break;
6991f7b95a1Smatthias.ringwald 
7006772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
701fe1ed1b8Smatthias.ringwald             // Connection management
702fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
703339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
70496a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
70596a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
706fe1ed1b8Smatthias.ringwald             if (conn) {
707b448a0e7Smatthias.ringwald                 if (!packet[2]){
708c8e4258aSmatthias.ringwald                     conn->state = OPEN;
709fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
710ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
711ee091cf1Smatthias.ringwald 
712c785ef68Smatthias.ringwald                     // restart timer
713c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
714ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
715c785ef68Smatthias.ringwald 
716339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
71743bfb1bdSmatthias.ringwald 
71843bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
719b448a0e7Smatthias.ringwald                 } else {
720ad83dc6aS[email protected]                     // notify client if dedicated bonding
721ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
722ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
723ad83dc6aS[email protected]                     }
724ad83dc6aS[email protected] 
725b448a0e7Smatthias.ringwald                     // connection failed, remove entry
7263a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
727a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
728c12e46e7Smatthias.ringwald 
729c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
730c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
731c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
732c12e46e7Smatthias.ringwald                     }
733fe1ed1b8Smatthias.ringwald                 }
734fe1ed1b8Smatthias.ringwald             }
7356772a24cSmatthias.ringwald             break;
736fe1ed1b8Smatthias.ringwald 
737afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
738afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
739afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
740afd4e962S[email protected]             if (!conn) break;
741afd4e962S[email protected]             if (!packet[2]){
742afd4e962S[email protected]                 uint8_t * features = &packet[5];
743afd4e962S[email protected]                 if (features[6] & (1 << 3)){
744afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
745afd4e962S[email protected]                 }
746afd4e962S[email protected]             }
747afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
748ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
749ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
750ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
751ad83dc6aS[email protected]             }
752afd4e962S[email protected]             break;
753afd4e962S[email protected] 
7547fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
7557b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
7567fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
75774d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
7583a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
75932ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
76064472d52Smatthias.ringwald             hci_run();
761d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
76229d53098Smatthias.ringwald             return;
7637fde4af9Smatthias.ringwald 
7649ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
76529d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
76696a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
7679ab95c90S[email protected]             if (!conn) break;
7689ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
7697bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
7709ab95c90S[email protected]             // Change Connection Encryption keeps link key type
7719ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
7729ab95c90S[email protected]                 conn->link_key_type = link_key_type;
7739ab95c90S[email protected]             }
7743a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
7753a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
77629d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
7777fde4af9Smatthias.ringwald             break;
7789ab95c90S[email protected]         }
7797fde4af9Smatthias.ringwald 
7807fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
7816724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
7824c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
7833a9fb326S[email protected]             if (!hci_stack->bondable){
784899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
785f8fb5f6eS[email protected]                 hci_run();
786f8fb5f6eS[email protected]                 return;
7874c57c146S[email protected]             }
788d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
7893a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
790d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
7913a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
7927fde4af9Smatthias.ringwald             break;
7937fde4af9Smatthias.ringwald 
7941d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
7951d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
796dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
797dbe1a790S[email protected]             break;
798dbe1a790S[email protected] 
799dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
8006724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
8013a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
802dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
803dbe1a790S[email protected]             break;
804dbe1a790S[email protected] 
805dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
8066724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
8073a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
808dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
8091d6b20aeS[email protected]             break;
8101d6b20aeS[email protected] 
811f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
812f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
813f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
814f0944df2S[email protected]             if (!conn) break;
815ad83dc6aS[email protected]             if (packet[2] == 0) {
816f0944df2S[email protected]                 if (packet[5]){
817f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
818f0944df2S[email protected]                 } else {
819536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
820f0944df2S[email protected]                 }
821ad83dc6aS[email protected]             }
822a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
823f0944df2S[email protected]             break;
824f0944df2S[email protected] 
8251eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
8261eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
8271eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
8281eb2563eS[email protected]             if (!conn) break;
829ad83dc6aS[email protected] 
830ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
831ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
832ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
833ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
834ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
835ad83dc6aS[email protected]                 break;
836ad83dc6aS[email protected]             }
837ad83dc6aS[email protected] 
838b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
8391eb2563eS[email protected]                 // link key sufficient for requested security
8401eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
841ad83dc6aS[email protected]                 break;
842ad83dc6aS[email protected]             }
8431eb2563eS[email protected]             // not enough
8441eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
8451eb2563eS[email protected]             break;
84634d2123cS[email protected] 
847223aafc1Smatthias.ringwald #ifndef EMBEDDED
84874ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
8493a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
85074ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
85174ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
8525a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
8535a06394aSmatthias.ringwald             for (i=0; i<248;i++){
8545a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
8555a06394aSmatthias.ringwald                     packet[9+i] = 0;
8565a06394aSmatthias.ringwald                     break;
857cdc9101dSmatthias.ringwald                 }
8585a06394aSmatthias.ringwald             }
859d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
86074ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
8613a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
86274ec757aSmatthias.ringwald             break;
86374ec757aSmatthias.ringwald 
86474ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
86574ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
8663a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
86774ec757aSmatthias.ringwald             // first send inq result packet
8683a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
86974ec757aSmatthias.ringwald             // then send cached remote names
87074ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
87174ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
8723a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
87374ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
87474ec757aSmatthias.ringwald                 }
87574ec757aSmatthias.ringwald             }
87674ec757aSmatthias.ringwald             return;
877223aafc1Smatthias.ringwald #endif
87874ec757aSmatthias.ringwald 
8796772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
880fe1ed1b8Smatthias.ringwald             if (!packet[2]){
881fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
8825061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
883fe1ed1b8Smatthias.ringwald                 if (conn) {
884c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
885fe1ed1b8Smatthias.ringwald                 }
886fe1ed1b8Smatthias.ringwald             }
8876772a24cSmatthias.ringwald             break;
8886772a24cSmatthias.ringwald 
889c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
8903a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
8913a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
8927586ee35S[email protected]             } else {
8937586ee35S[email protected]                 // if no special requests, just reboot stack
8947586ee35S[email protected]                 hci_power_control_off();
8957586ee35S[email protected]                 hci_power_control_on();
896c68bdf90Smatthias.ringwald             }
897c68bdf90Smatthias.ringwald             break;
898c68bdf90Smatthias.ringwald 
8996b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
9006b4af23dS[email protected]             // free packet buffer for asynchronous transport
9016b4af23dS[email protected]             if (hci_transport_synchronous()) break;
9026b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
9036b4af23dS[email protected]             break;
9046b4af23dS[email protected] 
9055909f7f2Smatthias.ringwald #ifdef HAVE_BLE
9065909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
9075909f7f2Smatthias.ringwald             switch (packet[2]){
90857c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
9097bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
91057c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
9117bdc6798S[email protected]                     break;
9125909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
9135909f7f2Smatthias.ringwald                     // Connection management
9145909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
9157bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
91696a45072S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr));
9175909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
91896a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
9195909f7f2Smatthias.ringwald                     if (packet[3]){
9205909f7f2Smatthias.ringwald                         if (conn){
9215909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
9223a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
9235909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
9245909f7f2Smatthias.ringwald                         }
9255909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
9265909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
9275909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
9285909f7f2Smatthias.ringwald                         }
9295909f7f2Smatthias.ringwald                         break;
9305909f7f2Smatthias.ringwald                     }
9315909f7f2Smatthias.ringwald                     if (!conn){
93296a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
9335909f7f2Smatthias.ringwald                     }
9345909f7f2Smatthias.ringwald                     if (!conn){
9355909f7f2Smatthias.ringwald                         // no memory
9365909f7f2Smatthias.ringwald                         break;
9375909f7f2Smatthias.ringwald                     }
9385909f7f2Smatthias.ringwald 
9395909f7f2Smatthias.ringwald                     conn->state = OPEN;
9405909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
9415909f7f2Smatthias.ringwald 
9425909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
9435909f7f2Smatthias.ringwald 
9445909f7f2Smatthias.ringwald                     // restart timer
9455909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
9465909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
9475909f7f2Smatthias.ringwald 
9485909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
9495909f7f2Smatthias.ringwald 
9505909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
9515909f7f2Smatthias.ringwald                     break;
9525909f7f2Smatthias.ringwald 
95365a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
95465a46ef3S[email protected] 
9555909f7f2Smatthias.ringwald                 default:
9565909f7f2Smatthias.ringwald                     break;
9575909f7f2Smatthias.ringwald             }
9585909f7f2Smatthias.ringwald             break;
9595909f7f2Smatthias.ringwald #endif
9606772a24cSmatthias.ringwald         default:
9616772a24cSmatthias.ringwald             break;
962fe1ed1b8Smatthias.ringwald     }
963fe1ed1b8Smatthias.ringwald 
9643429f56bSmatthias.ringwald     // handle BT initialization
9653a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
9663a9fb326S[email protected]         if (hci_stack->substate % 2){
9673429f56bSmatthias.ringwald             // odd: waiting for event
968f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
969c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
9703a9fb326S[email protected]                 if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
9713a9fb326S[email protected]                     hci_stack->substate++;
972dc277fcaSmatthias.ringwald                 }
973dc277fcaSmatthias.ringwald             }
974dc277fcaSmatthias.ringwald 
975dc277fcaSmatthias.ringwald             // HACK to deal with duplicate HCI Reset Complete events seen on cheapo CSR8510 A10 USB Dongle
976dc277fcaSmatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_reset)){
977dc277fcaSmatthias.ringwald                 if (hci_stack->state == HCI_STATE_INITIALIZING) {
978dc277fcaSmatthias.ringwald                     if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
979dc277fcaSmatthias.ringwald                         hci_stack->substate = 6; // >> 1 = 3
980dc277fcaSmatthias.ringwald                     } else {
981dc277fcaSmatthias.ringwald                         hci_stack->substate = 2; // >> 1 = 1
982dc277fcaSmatthias.ringwald                     }
9833429f56bSmatthias.ringwald                 }
9843429f56bSmatthias.ringwald             }
98522909952Smatthias.ringwald         }
986c9af4d3fS[email protected]     }
98722909952Smatthias.ringwald 
98889db417bSmatthias.ringwald     // help with BT sleep
9893a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
9903a9fb326S[email protected]         && hci_stack->substate == 1
99189db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
9923a9fb326S[email protected]         hci_stack->substate++;
99389db417bSmatthias.ringwald     }
99489db417bSmatthias.ringwald 
9953a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
99694ab26f8Smatthias.ringwald 
99794ab26f8Smatthias.ringwald 	// execute main loop
99894ab26f8Smatthias.ringwald 	hci_run();
99916833f0aSmatthias.ringwald }
100016833f0aSmatthias.ringwald 
10010a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
100210e830c9Smatthias.ringwald     switch (packet_type) {
100310e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
100410e830c9Smatthias.ringwald             event_handler(packet, size);
100510e830c9Smatthias.ringwald             break;
100610e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
100710e830c9Smatthias.ringwald             acl_handler(packet, size);
100810e830c9Smatthias.ringwald             break;
100910e830c9Smatthias.ringwald         default:
101010e830c9Smatthias.ringwald             break;
101110e830c9Smatthias.ringwald     }
101210e830c9Smatthias.ringwald }
101310e830c9Smatthias.ringwald 
1014fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
10152718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
10163a9fb326S[email protected]     hci_stack->packet_handler = handler;
101716833f0aSmatthias.ringwald }
101816833f0aSmatthias.ringwald 
1019595bdbfbS[email protected] void hci_state_reset(){
1020595bdbfbS[email protected]     // no connections yet
1021595bdbfbS[email protected]     hci_stack->connections = NULL;
102274308b23Smatthias.ringwald 
102374308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
102474308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
102574308b23Smatthias.ringwald     // hci_stack->connectable = 0;
102674308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1027595bdbfbS[email protected] 
1028595bdbfbS[email protected]     // no pending cmds
1029595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1030595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1031595bdbfbS[email protected] 
1032595bdbfbS[email protected]     // LE
1033595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1034595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1035595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1036e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1037595bdbfbS[email protected] }
1038595bdbfbS[email protected] 
10394f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1040475c8125Smatthias.ringwald 
10413a9fb326S[email protected] #ifdef HAVE_MALLOC
10423a9fb326S[email protected]     if (!hci_stack) {
10433a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
10443a9fb326S[email protected]     }
10453a9fb326S[email protected] #else
10463a9fb326S[email protected]     hci_stack = &hci_stack_static;
10473a9fb326S[email protected] #endif
104866fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
10493a9fb326S[email protected] 
1050475c8125Smatthias.ringwald     // reference to use transport layer implementation
10513a9fb326S[email protected]     hci_stack->hci_transport = transport;
1052475c8125Smatthias.ringwald 
105311e23e5fSmatthias.ringwald     // references to used control implementation
10543a9fb326S[email protected]     hci_stack->control = control;
105511e23e5fSmatthias.ringwald 
105611e23e5fSmatthias.ringwald     // reference to used config
10573a9fb326S[email protected]     hci_stack->config = config;
105811e23e5fSmatthias.ringwald 
105916833f0aSmatthias.ringwald     // higher level handler
10603a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
106116833f0aSmatthias.ringwald 
1062404843c1Smatthias.ringwald     // store and open remote device db
10633a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
10643a9fb326S[email protected]     if (hci_stack->remote_device_db) {
10653a9fb326S[email protected]         hci_stack->remote_device_db->open();
1066404843c1Smatthias.ringwald     }
106729d53098Smatthias.ringwald 
10688fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
10693a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
10708fcba05dSmatthias.ringwald 
107116833f0aSmatthias.ringwald     // register packet handlers with transport
107210e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1073f5454fc6Smatthias.ringwald 
10743a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1075e2386ba1S[email protected] 
1076e2386ba1S[email protected]     // class of device
10773a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1078a45d6b9fS[email protected] 
1079f20168b8Smatthias.ringwald     // bondable by default
1080f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1081f20168b8Smatthias.ringwald 
108263048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
10833a9fb326S[email protected]     hci_stack->ssp_enable = 1;
10843a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
10853a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
10863a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
108769a97523S[email protected] 
1088595bdbfbS[email protected]     hci_state_reset();
1089475c8125Smatthias.ringwald }
1090475c8125Smatthias.ringwald 
1091404843c1Smatthias.ringwald void hci_close(){
1092404843c1Smatthias.ringwald     // close remote device db
10933a9fb326S[email protected]     if (hci_stack->remote_device_db) {
10943a9fb326S[email protected]         hci_stack->remote_device_db->close();
1095404843c1Smatthias.ringwald     }
10963a9fb326S[email protected]     while (hci_stack->connections) {
1097bc68afe2S[email protected]         // cancel all l2cap connections
1098bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
10993a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1100f5454fc6Smatthias.ringwald     }
1101f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
11023a9fb326S[email protected] 
11033a9fb326S[email protected] #ifdef HAVE_MALLOC
11043a9fb326S[email protected]     free(hci_stack);
11053a9fb326S[email protected] #endif
11063a9fb326S[email protected]     hci_stack = NULL;
1107404843c1Smatthias.ringwald }
1108404843c1Smatthias.ringwald 
11099e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
11109e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
11119e61646fS[email protected] }
11129e61646fS[email protected] 
111366fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
111466fb9560S[email protected]     disable_l2cap_timeouts = 1;
111566fb9560S[email protected] }
11168d213e1aSmatthias.ringwald // State-Module-Driver overview
11178d213e1aSmatthias.ringwald // state                    module  low-level
11188d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
11198d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
11208d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
11218d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1122d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1123d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1124c7e0c5f6Smatthias.ringwald 
112540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
11267301ad89Smatthias.ringwald 
1127038bc64cSmatthias.ringwald     // power on
1128f9a30166Smatthias.ringwald     int err = 0;
11293a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
11303a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1131f9a30166Smatthias.ringwald     }
1132038bc64cSmatthias.ringwald     if (err){
11337d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1134038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1135038bc64cSmatthias.ringwald         return err;
1136038bc64cSmatthias.ringwald     }
1137038bc64cSmatthias.ringwald 
1138038bc64cSmatthias.ringwald     // open low-level device
11393a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1140038bc64cSmatthias.ringwald     if (err){
11417d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
11423a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
11433a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1144f9a30166Smatthias.ringwald         }
1145038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1146038bc64cSmatthias.ringwald         return err;
1147038bc64cSmatthias.ringwald     }
11488d213e1aSmatthias.ringwald     return 0;
11498d213e1aSmatthias.ringwald }
1150038bc64cSmatthias.ringwald 
115140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
11528d213e1aSmatthias.ringwald 
11537b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
11549418f9c9Smatthias.ringwald 
11558d213e1aSmatthias.ringwald     // close low-level device
11563a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
11578d213e1aSmatthias.ringwald 
11587b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
11599418f9c9Smatthias.ringwald 
11608d213e1aSmatthias.ringwald     // power off
11613a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
11623a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
11638d213e1aSmatthias.ringwald     }
11649418f9c9Smatthias.ringwald 
11657b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
11669418f9c9Smatthias.ringwald 
11673a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
116872ea5239Smatthias.ringwald }
116972ea5239Smatthias.ringwald 
117040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
117172ea5239Smatthias.ringwald 
11727b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
11733144bce4Smatthias.ringwald 
1174b429b9b7Smatthias.ringwald #if 0
1175b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1176b429b9b7Smatthias.ringwald 
117772ea5239Smatthias.ringwald     // close low-level device
11783a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1179b429b9b7Smatthias.ringwald #endif
118072ea5239Smatthias.ringwald 
118172ea5239Smatthias.ringwald     // sleep mode
11823a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
11833a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
118472ea5239Smatthias.ringwald     }
1185b429b9b7Smatthias.ringwald 
11863a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
11878d213e1aSmatthias.ringwald }
11888d213e1aSmatthias.ringwald 
118940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1190b429b9b7Smatthias.ringwald 
11917b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1192b429b9b7Smatthias.ringwald 
1193b429b9b7Smatthias.ringwald     // wake on
11943a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
11953a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1196b429b9b7Smatthias.ringwald     }
1197b429b9b7Smatthias.ringwald 
1198b429b9b7Smatthias.ringwald #if 0
1199b429b9b7Smatthias.ringwald     // open low-level device
12003a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1201b429b9b7Smatthias.ringwald     if (err){
12027d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
12033a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
12043a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1205b429b9b7Smatthias.ringwald         }
1206b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1207b429b9b7Smatthias.ringwald         return err;
1208b429b9b7Smatthias.ringwald     }
1209b429b9b7Smatthias.ringwald #endif
1210b429b9b7Smatthias.ringwald 
1211b429b9b7Smatthias.ringwald     return 0;
1212b429b9b7Smatthias.ringwald }
1213b429b9b7Smatthias.ringwald 
1214b429b9b7Smatthias.ringwald 
12158d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1216d661ed19Smatthias.ringwald 
12173a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1218d661ed19Smatthias.ringwald 
12198d213e1aSmatthias.ringwald     int err = 0;
12203a9fb326S[email protected]     switch (hci_stack->state){
12218d213e1aSmatthias.ringwald 
12228d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
12238d213e1aSmatthias.ringwald             switch (power_mode){
12248d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
12258d213e1aSmatthias.ringwald                     err = hci_power_control_on();
1226*97b61c7bS[email protected]                     if (err) {
1227*97b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
1228*97b61c7bS[email protected]                         return err;
1229*97b61c7bS[email protected]                     }
12307301ad89Smatthias.ringwald                     // set up state machine
12313a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12323a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12333a9fb326S[email protected]                     hci_stack->substate = 0;
12348d213e1aSmatthias.ringwald                     break;
12358d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12368d213e1aSmatthias.ringwald                     // do nothing
12378d213e1aSmatthias.ringwald                     break;
12388d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1239b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
12408d213e1aSmatthias.ringwald                     break;
12418d213e1aSmatthias.ringwald             }
12428d213e1aSmatthias.ringwald             break;
12437301ad89Smatthias.ringwald 
12448d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
12458d213e1aSmatthias.ringwald             switch (power_mode){
12468d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
12478d213e1aSmatthias.ringwald                     // do nothing
12488d213e1aSmatthias.ringwald                     break;
12498d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12508d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
12518d213e1aSmatthias.ringwald                     hci_power_control_off();
12528d213e1aSmatthias.ringwald                     break;
12538d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1254b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
125572ea5239Smatthias.ringwald                     hci_power_control_sleep();
12568d213e1aSmatthias.ringwald                     break;
12578d213e1aSmatthias.ringwald             }
12588d213e1aSmatthias.ringwald             break;
12597301ad89Smatthias.ringwald 
12608d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
12618d213e1aSmatthias.ringwald             switch (power_mode){
12628d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
12638d213e1aSmatthias.ringwald                     // do nothing
12648d213e1aSmatthias.ringwald                     break;
12658d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1266c7e0c5f6Smatthias.ringwald                     // see hci_run
12673a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12688d213e1aSmatthias.ringwald                     break;
12698d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1270b546ac54Smatthias.ringwald                     // see hci_run
12713a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
12723a9fb326S[email protected]                     hci_stack->substate = 0;
12738d213e1aSmatthias.ringwald                     break;
12748d213e1aSmatthias.ringwald             }
12758d213e1aSmatthias.ringwald             break;
12767301ad89Smatthias.ringwald 
12778d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
12788d213e1aSmatthias.ringwald             switch (power_mode){
12798d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
12808d213e1aSmatthias.ringwald                     // set up state machine
12813a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12823a9fb326S[email protected]                     hci_stack->substate = 0;
12838d213e1aSmatthias.ringwald                     break;
12848d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12858d213e1aSmatthias.ringwald                     // do nothing
12868d213e1aSmatthias.ringwald                     break;
12878d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1288b546ac54Smatthias.ringwald                     // see hci_run
12893a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
12903a9fb326S[email protected]                     hci_stack->substate = 0;
12918d213e1aSmatthias.ringwald                     break;
12928d213e1aSmatthias.ringwald             }
12938d213e1aSmatthias.ringwald             break;
12948d213e1aSmatthias.ringwald 
12958d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
12968d213e1aSmatthias.ringwald             switch (power_mode){
12978d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
129828171530Smatthias.ringwald 
129928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
130028171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
130128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
13023a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
13033a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
130428171530Smatthias.ringwald                         break;
130528171530Smatthias.ringwald                     }
130628171530Smatthias.ringwald #endif
1307b546ac54Smatthias.ringwald                     // set up state machine
13083a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
13093a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
13103a9fb326S[email protected]                     hci_stack->substate = 0;
13118d213e1aSmatthias.ringwald                     break;
13128d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1313b546ac54Smatthias.ringwald                     // see hci_run
13143a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
13158d213e1aSmatthias.ringwald                     break;
13168d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1317b546ac54Smatthias.ringwald                     // do nothing
13188d213e1aSmatthias.ringwald                     break;
13198d213e1aSmatthias.ringwald             }
13208d213e1aSmatthias.ringwald             break;
13218d213e1aSmatthias.ringwald 
13228d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
13238d213e1aSmatthias.ringwald             switch (power_mode){
13248d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
132528171530Smatthias.ringwald 
132628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
132728171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
132828171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
13293a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
13303a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1331758b46ceSmatthias.ringwald                         hci_update_scan_enable();
133228171530Smatthias.ringwald                         break;
133328171530Smatthias.ringwald                     }
133428171530Smatthias.ringwald #endif
13353144bce4Smatthias.ringwald                     err = hci_power_control_wake();
13363144bce4Smatthias.ringwald                     if (err) return err;
1337b546ac54Smatthias.ringwald                     // set up state machine
13383a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
13393a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
13403a9fb326S[email protected]                     hci_stack->substate = 0;
13418d213e1aSmatthias.ringwald                     break;
13428d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
13433a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
13448d213e1aSmatthias.ringwald                     break;
13458d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1346b546ac54Smatthias.ringwald                     // do nothing
13478d213e1aSmatthias.ringwald                     break;
13488d213e1aSmatthias.ringwald             }
13498d213e1aSmatthias.ringwald             break;
135011e23e5fSmatthias.ringwald     }
135168d92d03Smatthias.ringwald 
1352038bc64cSmatthias.ringwald     // create internal event
1353ee8bf225Smatthias.ringwald 	hci_emit_state();
1354ee8bf225Smatthias.ringwald 
135568d92d03Smatthias.ringwald 	// trigger next/first action
135668d92d03Smatthias.ringwald 	hci_run();
135768d92d03Smatthias.ringwald 
1358475c8125Smatthias.ringwald     return 0;
1359475c8125Smatthias.ringwald }
1360475c8125Smatthias.ringwald 
1361758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1362758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
13633a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1364758b46ceSmatthias.ringwald     hci_run();
1365758b46ceSmatthias.ringwald }
1366758b46ceSmatthias.ringwald 
1367381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1368381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1369381fbed8Smatthias.ringwald 
13703a9fb326S[email protected]     if (hci_stack->discoverable == enable){
13713a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1372381fbed8Smatthias.ringwald         return;
1373381fbed8Smatthias.ringwald     }
1374381fbed8Smatthias.ringwald 
13753a9fb326S[email protected]     hci_stack->discoverable = enable;
1376758b46ceSmatthias.ringwald     hci_update_scan_enable();
1377758b46ceSmatthias.ringwald }
1378b031bebbSmatthias.ringwald 
1379758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1380758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1381758b46ceSmatthias.ringwald 
1382758b46ceSmatthias.ringwald     // don't emit event
13833a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1384758b46ceSmatthias.ringwald 
13853a9fb326S[email protected]     hci_stack->connectable = enable;
1386758b46ceSmatthias.ringwald     hci_update_scan_enable();
1387381fbed8Smatthias.ringwald }
1388381fbed8Smatthias.ringwald 
13895061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
13903a9fb326S[email protected]     return &hci_stack->local_bd_addr;
13915061f3afS[email protected] }
13925061f3afS[email protected] 
139306b35ec0Smatthias.ringwald void hci_run(){
13948a485f27Smatthias.ringwald 
139532ab9390Smatthias.ringwald     hci_connection_t * connection;
139632ab9390Smatthias.ringwald     linked_item_t * it;
139732ab9390Smatthias.ringwald 
139864f0b431S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
1399ce4c8fabSmatthias.ringwald 
1400b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1401b031bebbSmatthias.ringwald 
1402b031bebbSmatthias.ringwald     // decline incoming connections
14033a9fb326S[email protected]     if (hci_stack->decline_reason){
14043a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
14053a9fb326S[email protected]         hci_stack->decline_reason = 0;
14063a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1407dbe1a790S[email protected]         return;
1408ce4c8fabSmatthias.ringwald     }
1409ce4c8fabSmatthias.ringwald 
1410b031bebbSmatthias.ringwald     // send scan enable
14113a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
14123a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
14133a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1414dbe1a790S[email protected]         return;
1415b031bebbSmatthias.ringwald     }
1416b031bebbSmatthias.ringwald 
1417b2f949feS[email protected] #ifdef HAVE_BLE
14187bdc6798S[email protected]     // handle le scan
14197bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
14207bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
14217bdc6798S[email protected]             case LE_START_SCAN:
14227bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
14237bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
14247bdc6798S[email protected]                 return;
14257bdc6798S[email protected] 
14267bdc6798S[email protected]             case LE_STOP_SCAN:
14277bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
14287bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
14297bdc6798S[email protected]                 return;
14307bdc6798S[email protected]             default:
14317bdc6798S[email protected]                 break;
14327bdc6798S[email protected]         }
1433e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
1434e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
1435e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
1436e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
1437e2602ea2Smatthias.ringwald             hci_send_cmd(&hci_le_set_scan_parameters, scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->adv_addr_type, 0);
1438e2602ea2Smatthias.ringwald             return;
1439e2602ea2Smatthias.ringwald         }
14407bdc6798S[email protected]     }
1441b2f949feS[email protected] #endif
14427bdc6798S[email protected] 
144332ab9390Smatthias.ringwald     // send pending HCI commands
14443a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
1445b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
144632ab9390Smatthias.ringwald 
14470bf6344aS[email protected]         switch(connection->state){
14480bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
14494f3229d8S[email protected]                 switch(connection->address_type){
14504f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
1451564fca32S[email protected]                         log_info("sending hci_create_connection\n");
1452ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
14534f3229d8S[email protected]                         break;
14544f3229d8S[email protected]                     default:
1455b2f949feS[email protected] #ifdef HAVE_BLE
1456564fca32S[email protected]                         log_info("sending hci_le_create_connection\n");
14574f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
1458b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
1459b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
14604f3229d8S[email protected]                                      0,         // don't use whitelist
14614f3229d8S[email protected]                                      connection->address_type, // peer address type
14624f3229d8S[email protected]                                      connection->address,      // peer bd addr
1463b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
1464b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
1465b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
14664f3229d8S[email protected]                                      0,         // conn latency
1467b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
1468b2571179Smatthias.ringwald                                      0x0001,    // min ce length
1469b2571179Smatthias.ringwald                                      0x0001     // max ce length
14704f3229d8S[email protected]                                      );
14714f3229d8S[email protected] 
14724f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
1473b2f949feS[email protected] #endif
14744f3229d8S[email protected]                         break;
14754f3229d8S[email protected]                 }
1476ad83dc6aS[email protected]                 return;
1477ad83dc6aS[email protected] 
14780bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
14797b5fbe1fSmatthias.ringwald                 log_info("sending hci_accept_connection_request\n");
148032ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
148134d2123cS[email protected]                 hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1482dbe1a790S[email protected]                 return;
14830bf6344aS[email protected] 
1484a6725849S[email protected] #ifdef HAVE_BLE
14850bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
14860bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
14870bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
14880bf6344aS[email protected]                 return;
1489a6725849S[email protected] #endif
14900bf6344aS[email protected]             case SEND_DISCONNECT:
14910bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
14927851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
14930bf6344aS[email protected]                 return;
14940bf6344aS[email protected] 
14950bf6344aS[email protected]             default:
14960bf6344aS[email protected]                 break;
1497c7e0c5f6Smatthias.ringwald         }
1498c7e0c5f6Smatthias.ringwald 
149932ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
150034d2123cS[email protected]             log_info("responding to link key request\n");
150134d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
150232ab9390Smatthias.ringwald             link_key_t link_key;
1503c77e8838S[email protected]             link_key_type_t link_key_type;
15043a9fb326S[email protected]             if ( hci_stack->remote_device_db
15053a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
150634d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
15079ab95c90S[email protected]                connection->link_key_type = link_key_type;
150832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
150932ab9390Smatthias.ringwald             } else {
151032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
151132ab9390Smatthias.ringwald             }
1512dbe1a790S[email protected]             return;
151332ab9390Smatthias.ringwald         }
15141d6b20aeS[email protected] 
1515899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
15164c57c146S[email protected]             log_info("denying to pin request\n");
1517899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
151834d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
15194c57c146S[email protected]             return;
15204c57c146S[email protected]         }
15214c57c146S[email protected] 
1522dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
152334d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
152482d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
152582d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1526106d6d11S[email protected]                 // tweak authentication requirements
15273a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1528106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
15299faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
15309faad3abS[email protected]                 }
15319faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
15329faad3abS[email protected]                     authreq |= 1;
1533106d6d11S[email protected]                 }
15343a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1535f8fb5f6eS[email protected]             } else {
1536f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1537f8fb5f6eS[email protected]             }
1538dbe1a790S[email protected]             return;
153932ab9390Smatthias.ringwald         }
154032ab9390Smatthias.ringwald 
1541dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1542dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
154334d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1544dbe1a790S[email protected]             return;
1545dbe1a790S[email protected]         }
1546dbe1a790S[email protected] 
1547dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1548dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
154934d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1550dbe1a790S[email protected]             return;
1551dbe1a790S[email protected]         }
1552afd4e962S[email protected] 
1553afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1554afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
155534d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
15562bd8b7e7S[email protected]             return;
15572bd8b7e7S[email protected]         }
15582bd8b7e7S[email protected] 
15592bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
15602bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
156134d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
156234d2123cS[email protected]             return;
156334d2123cS[email protected]         }
1564ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1565ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
156652d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1567ad83dc6aS[email protected]             return;
1568ad83dc6aS[email protected]         }
156934d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
157034d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
157134d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
15722bd8b7e7S[email protected]             return;
1573afd4e962S[email protected]         }
1574dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1575dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1576dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1577dce78009S[email protected]             return;
1578dce78009S[email protected]         }
1579dbe1a790S[email protected]     }
1580c7e0c5f6Smatthias.ringwald 
15813a9fb326S[email protected]     switch (hci_stack->state){
15823429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
15833a9fb326S[email protected]             // log_info("hci_init: substate %u\n", hci_stack->substate);
15843a9fb326S[email protected]             if (hci_stack->substate % 2) {
15853429f56bSmatthias.ringwald                 // odd: waiting for command completion
158606b35ec0Smatthias.ringwald                 return;
15873429f56bSmatthias.ringwald             }
15883a9fb326S[email protected]             switch (hci_stack->substate >> 1){
1589c7492964Smatthias.ringwald                 case 0: // RESET
1590595bdbfbS[email protected]                     hci_state_reset();
159124052c2aS[email protected] 
1592595bdbfbS[email protected]                     hci_send_cmd(&hci_reset);
15933a9fb326S[email protected]                     if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
1594c7492964Smatthias.ringwald                         // skip baud change
15953a9fb326S[email protected]                         hci_stack->substate = 4; // >> 1 = 2
1596c7492964Smatthias.ringwald                     }
15973429f56bSmatthias.ringwald                     break;
1598c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
15993a9fb326S[email protected]                     hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
16003a9fb326S[email protected]                     hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1601c7492964Smatthias.ringwald                     break;
1602c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
16031f479f8cS[email protected]                     log_info("Local baud rate change");
16043a9fb326S[email protected]                     hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
16053a9fb326S[email protected]                     hci_stack->substate += 2;
1606c7492964Smatthias.ringwald                     // break missing here for fall through
1607c7492964Smatthias.ringwald 
1608c7492964Smatthias.ringwald                 case 3:
16091f479f8cS[email protected]                     log_info("Custom init");
161024052c2aS[email protected]                     // Custom initialization
16113a9fb326S[email protected]                     if (hci_stack->control && hci_stack->control->next_cmd){
16123a9fb326S[email protected]                         int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
1613d62aca9fSmatthias.ringwald                         if (valid_cmd){
16143a9fb326S[email protected]                             int size = 3 + hci_stack->hci_packet_buffer[2];
16153a9fb326S[email protected]                             hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
16163a9fb326S[email protected]                             hci_stack->substate = 4; // more init commands
161790919203Smatthias.ringwald                             break;
161890919203Smatthias.ringwald                         }
1619d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
162090919203Smatthias.ringwald                     }
16212f6c30e1Smatthias.ringwald                     // otherwise continue
1622f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1623f432a6ddSmatthias.ringwald 					break;
1624c7492964Smatthias.ringwald 				case 4:
1625e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1626e2edc0c3Smatthias.ringwald 					break;
1627c7492964Smatthias.ringwald                 case 5:
162865389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
16293429f56bSmatthias.ringwald                     break;
1630c7492964Smatthias.ringwald                 case 6:
1631d7e0e3a8S[email protected]                     if (hci_le_supported()){
1632d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1633d7e0e3a8S[email protected]                     } else {
1634d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1635d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1636d7e0e3a8S[email protected]                     }
1637f5d8d141S[email protected] 
1638f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
163924052c2aS[email protected]                     if (!hci_classic_supported()){
164024052c2aS[email protected]                         if (hci_le_supported()){
16413a9fb326S[email protected]                             hci_stack->substate = 11 << 1;    // skip all classic command
164224052c2aS[email protected]                         } else {
164324052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
16448ebb5506Smatthias.ringwald                             hci_stack->substate = 14 << 1;    // skip all
164524052c2aS[email protected]                         }
1646f5d8d141S[email protected]                     }
1647f5d8d141S[email protected]                     break;
1648f5d8d141S[email protected]                 case 7:
164924052c2aS[email protected]                     if (hci_ssp_supported()){
16503a9fb326S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
1651f5d8d141S[email protected]                         break;
165224052c2aS[email protected]                     }
16533a9fb326S[email protected]                     hci_stack->substate += 2;
165424052c2aS[email protected]                     // break missing here for fall through
165524052c2aS[email protected] 
1656f5d8d141S[email protected]                 case 8:
165765389bfcS[email protected]                     // ca. 15 sec
165865389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1659559e517eS[email protected]                     break;
1660f5d8d141S[email protected]                 case 9:
16613a9fb326S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
1662e2386ba1S[email protected]                     break;
1663f5d8d141S[email protected]                 case 10:
16643a9fb326S[email protected]                     if (hci_stack->local_name){
16653a9fb326S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
1666e2386ba1S[email protected]                     } else {
16678a485f27Smatthias.ringwald                         char hostname[30];
1668e2386ba1S[email protected] #ifdef EMBEDDED
1669e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1670e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
16713a9fb326S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
1672e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1673e2386ba1S[email protected] #else
1674e2386ba1S[email protected]                         // hostname for POSIX systems
16758a485f27Smatthias.ringwald                         gethostname(hostname, 30);
16768a485f27Smatthias.ringwald                         hostname[29] = '\0';
1677e2386ba1S[email protected] #endif
16788a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
16798a485f27Smatthias.ringwald                     }
16803c4d4b90Smatthias.ringwald                     break;
1681e0dbca83S[email protected]                 case 11:
16823a9fb326S[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
168324052c2aS[email protected]                     if (!hci_le_supported()){
168424052c2aS[email protected]                         // SKIP LE init for Classic only configuration
16858ebb5506Smatthias.ringwald                         hci_stack->substate = 14 << 1;
168624052c2aS[email protected]                     }
1687a45d6b9fS[email protected] 					break;
168824052c2aS[email protected] 
168943aa7007S[email protected] #ifdef HAVE_BLE
169024052c2aS[email protected]                 // LE INIT
1691a45d6b9fS[email protected]                 case 12:
169224052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
169324052c2aS[email protected]                     break;
169424052c2aS[email protected]                 case 13:
169524052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
169624052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
169724052c2aS[email protected]                     break;
16988ebb5506Smatthias.ringwald                 case 14:
16998ebb5506Smatthias.ringwald                     // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
17008ebb5506Smatthias.ringwald                     hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
17018ebb5506Smatthias.ringwald                     break;
170243aa7007S[email protected] #endif
170324052c2aS[email protected] 
170424052c2aS[email protected]                 // DONE
17058ebb5506Smatthias.ringwald                 case 15:
17063429f56bSmatthias.ringwald                     // done.
17073a9fb326S[email protected]                     hci_stack->state = HCI_STATE_WORKING;
1708b360b6adSmatthias.ringwald                     hci_emit_state();
17093429f56bSmatthias.ringwald                     break;
17103429f56bSmatthias.ringwald                 default:
17113429f56bSmatthias.ringwald                     break;
1712475c8125Smatthias.ringwald             }
17133a9fb326S[email protected]             hci_stack->substate++;
17143429f56bSmatthias.ringwald             break;
1715c7e0c5f6Smatthias.ringwald 
1716c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1717c7e0c5f6Smatthias.ringwald 
17187b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1719c7e0c5f6Smatthias.ringwald             // close all open connections
17203a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1721c7e0c5f6Smatthias.ringwald             if (connection){
172232ab9390Smatthias.ringwald 
1723c7e0c5f6Smatthias.ringwald                 // send disconnect
172464f0b431S[email protected]                 if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
172532ab9390Smatthias.ringwald 
172679bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
17276ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1728c7e0c5f6Smatthias.ringwald 
1729c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1730c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1731c7e0c5f6Smatthias.ringwald                 return;
1732c7e0c5f6Smatthias.ringwald             }
17337b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1734c7e0c5f6Smatthias.ringwald 
173572ea5239Smatthias.ringwald             // switch mode
1736c7e0c5f6Smatthias.ringwald             hci_power_control_off();
17379418f9c9Smatthias.ringwald 
17387b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
173972ea5239Smatthias.ringwald             hci_emit_state();
17407b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
174172ea5239Smatthias.ringwald             break;
1742c7e0c5f6Smatthias.ringwald 
174372ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
17443a9fb326S[email protected]             switch(hci_stack->substate) {
174589db417bSmatthias.ringwald                 case 0:
17467b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
174772ea5239Smatthias.ringwald                     // close all open connections
17483a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
174966da7044Smatthias.ringwald 
175028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
175166da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
175266da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
175366da7044Smatthias.ringwald                         connection = NULL;
175466da7044Smatthias.ringwald                     }
175566da7044Smatthias.ringwald #endif
175672ea5239Smatthias.ringwald                     if (connection){
175732ab9390Smatthias.ringwald 
175872ea5239Smatthias.ringwald                         // send disconnect
175932ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
176032ab9390Smatthias.ringwald 
176179bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
17626ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
176372ea5239Smatthias.ringwald 
176472ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
176572ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
176672ea5239Smatthias.ringwald                         return;
176772ea5239Smatthias.ringwald                     }
176872ea5239Smatthias.ringwald 
176992368cd3S[email protected]                     if (hci_classic_supported()){
177089db417bSmatthias.ringwald                         // disable page and inquiry scan
177164f0b431S[email protected]                         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
177232ab9390Smatthias.ringwald 
177392368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
17743a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
177589db417bSmatthias.ringwald 
177689db417bSmatthias.ringwald                         // continue in next sub state
17773a9fb326S[email protected]                         hci_stack->substate++;
177889db417bSmatthias.ringwald                         break;
177992368cd3S[email protected]                     }
178092368cd3S[email protected]                     // fall through for ble-only chips
178192368cd3S[email protected] 
178289db417bSmatthias.ringwald                 case 2:
17837b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
178428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
178528171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
178628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
178728171530Smatthias.ringwald                         // SLEEP MODE reached
17883a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
178928171530Smatthias.ringwald                         hci_emit_state();
179028171530Smatthias.ringwald                         break;
179128171530Smatthias.ringwald                     }
179228171530Smatthias.ringwald #endif
179372ea5239Smatthias.ringwald                     // switch mode
17943a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1795c7e0c5f6Smatthias.ringwald                     hci_emit_state();
179628171530Smatthias.ringwald                     break;
179728171530Smatthias.ringwald 
179889db417bSmatthias.ringwald                 default:
179989db417bSmatthias.ringwald                     break;
180089db417bSmatthias.ringwald             }
1801c7e0c5f6Smatthias.ringwald             break;
1802c7e0c5f6Smatthias.ringwald 
18033429f56bSmatthias.ringwald         default:
18043429f56bSmatthias.ringwald             break;
18051f504dbdSmatthias.ringwald     }
18063429f56bSmatthias.ringwald }
180716833f0aSmatthias.ringwald 
180831452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1809c8e4258aSmatthias.ringwald     bd_addr_t addr;
1810c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1811c8e4258aSmatthias.ringwald     // house-keeping
1812c8e4258aSmatthias.ringwald 
1813c8e4258aSmatthias.ringwald     // create_connection?
1814c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1815c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1816339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1817c8e4258aSmatthias.ringwald 
181896a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1819ad83dc6aS[email protected]         if (!conn){
182096a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
182117f1ba2aSmatthias.ringwald             if (!conn){
182217f1ba2aSmatthias.ringwald                 // notify client that alloc failed
182317f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
182417f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
182517f1ba2aSmatthias.ringwald             }
1826ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1827ad83dc6aS[email protected]         }
1828ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1829ad83dc6aS[email protected]         switch (conn->state){
1830ad83dc6aS[email protected]             // if connection active exists
1831ad83dc6aS[email protected]             case OPEN:
183262bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1833ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
183462bda3fbS[email protected]                 return 0;
1835ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1836ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1837ad83dc6aS[email protected]                 break;
1838ad83dc6aS[email protected]             default:
1839ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1840ad83dc6aS[email protected]                 return 0;
1841ad83dc6aS[email protected]         }
1842c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1843c8e4258aSmatthias.ringwald     }
1844c8e4258aSmatthias.ringwald 
18457fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
18467fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
18477fde4af9Smatthias.ringwald     }
18487fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
18497fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
18507fde4af9Smatthias.ringwald     }
18517fde4af9Smatthias.ringwald 
18528ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
18533a9fb326S[email protected]         if (hci_stack->remote_device_db){
18548ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
18553a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
18568ef73945Smatthias.ringwald         }
18578ef73945Smatthias.ringwald     }
1858c8e4258aSmatthias.ringwald 
18596724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
18606724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
18616724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
186296a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
18636724cd9eS[email protected]         if (conn){
18646724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
18656724cd9eS[email protected]         }
18666724cd9eS[email protected]     }
18676724cd9eS[email protected] 
18686724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
18696724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
18706724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
18716724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
18726724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
187396a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
18746724cd9eS[email protected]         if (conn){
18756724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
18766724cd9eS[email protected]         }
18776724cd9eS[email protected]     }
18786724cd9eS[email protected] 
187969a97523S[email protected] #ifdef HAVE_BLE
188069a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
18813a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
188269a97523S[email protected]     }
188369a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
18843a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
188569a97523S[email protected]     }
188669a97523S[email protected] #endif
188769a97523S[email protected] 
18883a9fb326S[email protected]     hci_stack->num_cmd_packets--;
18896b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
18906b4af23dS[email protected] 
18916b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
1892c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
18936b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
18946b4af23dS[email protected]     }
18956b4af23dS[email protected] 
18966b4af23dS[email protected]     return err;
189731452debSmatthias.ringwald }
18988adf0ddaSmatthias.ringwald 
18992bd8b7e7S[email protected] // disconnect because of security block
19002bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
19012bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
19022bd8b7e7S[email protected]     if (!connection) return;
19032bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
19042bd8b7e7S[email protected] }
19052bd8b7e7S[email protected] 
19062bd8b7e7S[email protected] 
1907dbe1a790S[email protected] // Configure Secure Simple Pairing
1908dbe1a790S[email protected] 
1909dbe1a790S[email protected] // enable will enable SSP during init
1910dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
19113a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1912dbe1a790S[email protected] }
1913dbe1a790S[email protected] 
19142bd8b7e7S[email protected] int hci_local_ssp_activated(){
19153a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
19162bd8b7e7S[email protected] }
19172bd8b7e7S[email protected] 
1918dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1919dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
19203a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1921dbe1a790S[email protected] }
1922dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
19233a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1924dbe1a790S[email protected] }
1925dbe1a790S[email protected] 
1926dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1927dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
19283a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1929dbe1a790S[email protected] }
1930dbe1a790S[email protected] 
19311cd208adSmatthias.ringwald /**
19321cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
19331cd208adSmatthias.ringwald  */
1934fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
19351cd208adSmatthias.ringwald     va_list argptr;
19361cd208adSmatthias.ringwald     va_start(argptr, cmd);
19373a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
19381cd208adSmatthias.ringwald     va_end(argptr);
19393a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
194093b8dc03Smatthias.ringwald }
1941c8e4258aSmatthias.ringwald 
1942ee091cf1Smatthias.ringwald // Create various non-HCI events.
1943ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1944ee091cf1Smatthias.ringwald 
1945c8e4258aSmatthias.ringwald void hci_emit_state(){
19463a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1947425d1371Smatthias.ringwald     uint8_t event[3];
194880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1949425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
19503a9fb326S[email protected]     event[2] = hci_stack->state;
1951425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19523a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1953c8e4258aSmatthias.ringwald }
1954c8e4258aSmatthias.ringwald 
195517f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1956425d1371Smatthias.ringwald     uint8_t event[13];
1957c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1958425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
195917f1ba2aSmatthias.ringwald     event[2] = status;
1960c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1961c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1962c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1963c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1964425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
19653a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1966c8e4258aSmatthias.ringwald }
1967c8e4258aSmatthias.ringwald 
19684f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){
19694f3229d8S[email protected]     uint8_t event[21];
19704f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
19714f3229d8S[email protected]     event[1] = sizeof(event) - 2;
19724f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
19734f3229d8S[email protected]     event[3] = status;
19744f3229d8S[email protected]     bt_store_16(event, 4, conn->con_handle);
19754f3229d8S[email protected]     event[6] = 0; // TODO: role
19764f3229d8S[email protected]     event[7] = conn->address_type;
19774f3229d8S[email protected]     bt_flip_addr(&event[8], conn->address);
19784f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
19794f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
19804f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
19814f3229d8S[email protected]     event[20] = 0; // master clock accuracy
19824f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
19834f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
19844f3229d8S[email protected] }
19854f3229d8S[email protected] 
19863c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1987425d1371Smatthias.ringwald     uint8_t event[6];
19883c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1989e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
19903c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
19913c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
19923c4d4b90Smatthias.ringwald     event[5] = reason;
1993425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
19943a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
19953c4d4b90Smatthias.ringwald }
19963c4d4b90Smatthias.ringwald 
1997ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
199866fb9560S[email protected]     if (disable_l2cap_timeouts) return;
1999e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2000425d1371Smatthias.ringwald     uint8_t event[4];
200180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2002425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2003ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2004425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20053a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2006ee091cf1Smatthias.ringwald }
200743bfb1bdSmatthias.ringwald 
200843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
2009e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2010425d1371Smatthias.ringwald     uint8_t event[3];
201180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2012425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
201343bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2014425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20153a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
201643bfb1bdSmatthias.ringwald }
2017038bc64cSmatthias.ringwald 
2018038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
2019e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2020425d1371Smatthias.ringwald     uint8_t event[2];
202180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2022425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2023425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20243a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2025038bc64cSmatthias.ringwald }
20261b0e3922Smatthias.ringwald 
202709ba8edeSmatthias.ringwald #ifndef EMBEDDED
20281b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
2029e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2030425d1371Smatthias.ringwald     uint8_t event[6];
20311b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2032425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2033425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2034425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
2035425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
2036425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20373a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20381b0e3922Smatthias.ringwald }
203909ba8edeSmatthias.ringwald #endif
20401b0e3922Smatthias.ringwald 
20412ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2042e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2043425d1371Smatthias.ringwald     uint8_t event[3];
20442ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2045425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
20462ed6235cSmatthias.ringwald     event[2] = enabled;
2047425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20483a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20492ed6235cSmatthias.ringwald }
2050627c2f45Smatthias.ringwald 
2051627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
2052e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2053627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2054e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2055f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
20562f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
2057f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2058e0abb8e7S[email protected] 
2059e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
2060e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
2061e0abb8e7S[email protected] 
2062e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
20633a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2064627c2f45Smatthias.ringwald }
2065381fbed8Smatthias.ringwald 
2066381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2067e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2068425d1371Smatthias.ringwald     uint8_t event[3];
2069381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2070425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2071381fbed8Smatthias.ringwald     event[2] = enabled;
2072425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20733a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2074381fbed8Smatthias.ringwald }
2075458bf4e8S[email protected] 
2076a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2077df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2078a00031e2S[email protected]     uint8_t event[5];
2079e00caf9cS[email protected]     int pos = 0;
2080a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2081e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2082a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2083e00caf9cS[email protected]     pos += 2;
2084e00caf9cS[email protected]     event[pos++] = level;
2085e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20863a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2087e00caf9cS[email protected] }
2088e00caf9cS[email protected] 
2089ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
2090ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2091ad83dc6aS[email protected]     uint8_t event[9];
2092ad83dc6aS[email protected]     int pos = 0;
2093ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2094ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2095ad83dc6aS[email protected]     event[pos++] = status;
2096ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
2097ad83dc6aS[email protected]     pos += 6;
2098ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20993a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2100ad83dc6aS[email protected] }
2101ad83dc6aS[email protected] 
21022bd8b7e7S[email protected] // query if remote side supports SSP
21032bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
21042bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
21052bd8b7e7S[email protected]     if (!connection) return 0;
21062bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
21072bd8b7e7S[email protected] }
21082bd8b7e7S[email protected] 
2109df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2110df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2111df3354fcS[email protected] }
2112df3354fcS[email protected] 
2113458bf4e8S[email protected] // GAP API
2114458bf4e8S[email protected] /**
2115458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2116458bf4e8S[email protected]  * @praram enabled
2117458bf4e8S[email protected]  */
21184c57c146S[email protected] void gap_set_bondable_mode(int enable){
21193a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2120458bf4e8S[email protected] }
2121cb230b9dS[email protected] 
2122cb230b9dS[email protected] /**
212334d2123cS[email protected]  * @brief map link keys to security levels
2124cb230b9dS[email protected]  */
212534d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
212634d2123cS[email protected]     switch (link_key_type){
21273c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
21283c68dfa9S[email protected]             return LEVEL_4;
21293c68dfa9S[email protected]         case COMBINATION_KEY:
21303c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
21313c68dfa9S[email protected]             return LEVEL_3;
21323c68dfa9S[email protected]         default:
21333c68dfa9S[email protected]             return LEVEL_2;
21343c68dfa9S[email protected]     }
2135cb230b9dS[email protected] }
2136cb230b9dS[email protected] 
2137a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
213834d2123cS[email protected]     if (!connection) return LEVEL_0;
213934d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
214034d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
214134d2123cS[email protected] }
214234d2123cS[email protected] 
214334d2123cS[email protected] 
2144106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
214582d8f825S[email protected]     printf("gap_mitm_protection_required_for_security_level %u\n", level);
2146106d6d11S[email protected]     return level > LEVEL_2;
2147106d6d11S[email protected] }
2148106d6d11S[email protected] 
214934d2123cS[email protected] /**
215034d2123cS[email protected]  * @brief get current security level
215134d2123cS[email protected]  */
215234d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
215334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
215434d2123cS[email protected]     if (!connection) return LEVEL_0;
215534d2123cS[email protected]     return gap_security_level_for_connection(connection);
215634d2123cS[email protected] }
215734d2123cS[email protected] 
2158cb230b9dS[email protected] /**
2159cb230b9dS[email protected]  * @brief request connection to device to
2160cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2161cb230b9dS[email protected]  */
216234d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
216334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
216434d2123cS[email protected]     if (!connection){
2165a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
216634d2123cS[email protected]         return;
216734d2123cS[email protected]     }
216834d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
216934d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
217034d2123cS[email protected]     if (current_level >= requested_level){
2171a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
217234d2123cS[email protected]         return;
217334d2123cS[email protected]     }
2174a00031e2S[email protected] 
217534d2123cS[email protected]     connection->requested_security_level = requested_level;
2176a00031e2S[email protected] 
2177fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
21783a9fb326S[email protected]     if (hci_stack->remote_device_db){
2179a00031e2S[email protected]         link_key_type_t link_key_type;
2180a00031e2S[email protected]         link_key_t      link_key;
21813a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2182a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2183a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2184a00031e2S[email protected]                 return;
2185a00031e2S[email protected]             }
2186a00031e2S[email protected]         }
2187a00031e2S[email protected]     }
2188a00031e2S[email protected] 
21891eb2563eS[email protected]     // try to authenticate connection
21901eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2191e80b2cf9S[email protected]     hci_run();
2192e00caf9cS[email protected] }
2193ad83dc6aS[email protected] 
2194ad83dc6aS[email protected] /**
2195ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2196ad83dc6aS[email protected]  * @param device
2197ad83dc6aS[email protected]  * @param request MITM protection
2198ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2199ad83dc6aS[email protected]  */
2200ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2201ad83dc6aS[email protected] 
2202ad83dc6aS[email protected]     // create connection state machine
220396a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2204ad83dc6aS[email protected] 
2205ad83dc6aS[email protected]     if (!connection){
2206ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2207ad83dc6aS[email protected]     }
2208ad83dc6aS[email protected] 
2209ad83dc6aS[email protected]     // delete linkn key
2210ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2211ad83dc6aS[email protected] 
2212ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2213ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2214ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
221582d8f825S[email protected]     printf("gap_dedicated_bonding, mitm %u -> level %u\n", mitm_protection_required, connection->requested_security_level);
2216ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2217ad83dc6aS[email protected] 
2218ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2219ad83dc6aS[email protected] 
2220ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2221ad83dc6aS[email protected]     // handle: authentication failure
2222ad83dc6aS[email protected]     // handle: disconnect on done
2223ad83dc6aS[email protected] 
2224ad83dc6aS[email protected]     hci_run();
2225ad83dc6aS[email protected] 
2226ad83dc6aS[email protected]     return 0;
2227ad83dc6aS[email protected] }
22288e618f72S[email protected] 
22298e618f72S[email protected] void gap_set_local_name(const char * local_name){
22308e618f72S[email protected]     hci_stack->local_name = local_name;
22318e618f72S[email protected] }
22328e618f72S[email protected] 
22337bdc6798S[email protected] le_command_status_t le_central_start_scan(){
223436af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
22357bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
22367bdc6798S[email protected]     hci_run();
22377bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
22387bdc6798S[email protected] }
22398e618f72S[email protected] 
22407bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
224136af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
22427bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
22437bdc6798S[email protected]     hci_run();
22447bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
22457bdc6798S[email protected] }
22464f3229d8S[email protected] 
2247ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2248ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2249ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2250ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2251ef11999fSmatthias.ringwald     hci_run();
2252ef11999fSmatthias.ringwald }
22534f3229d8S[email protected] 
22544f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){
22554f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
22564f3229d8S[email protected]     if (!conn){
22571f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
22584f3229d8S[email protected]         conn = create_connection_for_bd_addr_and_type(*addr, addr_type);
22594f3229d8S[email protected]         if (!conn){
22604f3229d8S[email protected]             // notify client that alloc failed
22614f3229d8S[email protected]             hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
22621f479f8cS[email protected]             log_info("le_central_connect: failed to alloc context");
22634f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
22644f3229d8S[email protected]         }
22654f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
22661f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2267564fca32S[email protected]         hci_run();
22684f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
22694f3229d8S[email protected]     }
22700bf6344aS[email protected] 
22710bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
22720bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
22730bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
22740bf6344aS[email protected]         hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED);
22751f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
22760bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
22770bf6344aS[email protected]     }
22780bf6344aS[email protected] 
22791f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
22804f3229d8S[email protected]     hci_emit_le_connection_complete(conn, 0);
22814f3229d8S[email protected]     hci_run();
22824f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
22834f3229d8S[email protected] }
22844f3229d8S[email protected] 
22857851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
22867851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){
22870bf6344aS[email protected]     linked_item_t *it;
22880bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
22890bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
22900bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
22910bf6344aS[email protected]         switch (conn->state){
2292a6725849S[email protected]             case SEND_CREATE_CONNECTION:
22937851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
22947851196eSmatthias.ringwald                 return conn;
22957851196eSmatthias.ringwald             default:
22967851196eSmatthias.ringwald                 break;
22977851196eSmatthias.ringwald         };
22987851196eSmatthias.ringwald     }
22997851196eSmatthias.ringwald     return NULL;
23007851196eSmatthias.ringwald }
23017851196eSmatthias.ringwald 
23027851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){
23037851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
23047851196eSmatthias.ringwald     switch (conn->state){
23057851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
23067851196eSmatthias.ringwald             // skip sending create connection and emit event instead
23070bf6344aS[email protected]             hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
23087851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
23097851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
23100bf6344aS[email protected]             break;
2311a6725849S[email protected]         case SENT_CREATE_CONNECTION:
23127851196eSmatthias.ringwald             // request to send cancel connection
23130bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
23140bf6344aS[email protected]             hci_run();
23150bf6344aS[email protected]             break;
23160bf6344aS[email protected]         default:
23170bf6344aS[email protected]             break;
23180bf6344aS[email protected]     }
2319e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2320e31f89a7S[email protected] }
23214f3229d8S[email protected] 
23225917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
23235917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
23245917a5c5S[email protected]     if (!conn){
23257851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
23265917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
23275917a5c5S[email protected]     }
23285917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
23295917a5c5S[email protected]     hci_run();
23304f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
23314f3229d8S[email protected] }
233204a6ef8cSmatthias.ringwald 
233304a6ef8cSmatthias.ringwald void hci_disconnect_all(){
233404a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
233504a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
233604a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
233704a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
233804a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
233904a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
234004a6ef8cSmatthias.ringwald     }
2341d31fba26S[email protected]     hci_run();
234204a6ef8cSmatthias.ringwald }
2343