xref: /btstack/src/hci.c (revision fac2e2fe9dfb53334068fe03e7ecf3eeea9026ee)
11f504dbdSmatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
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  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH 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  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
391f504dbdSmatthias.ringwald  *  hci.c
401f504dbdSmatthias.ringwald  *
411f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
421f504dbdSmatthias.ringwald  *
431f504dbdSmatthias.ringwald  */
441f504dbdSmatthias.ringwald 
457907f069SMatthias Ringwald #include "btstack_config.h"
4628171530Smatthias.ringwald 
477f2435e6Smatthias.ringwald 
4806b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC
49aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
508f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h"
51a484130cSMatthias Ringwald #endif
5206b9e820SMatthias Ringwald #endif
53a484130cSMatthias Ringwald 
544a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
55d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h"
564a3574a1SMatthias Ringwald #endif
574a3574a1SMatthias Ringwald 
58a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
59472a5742SMatthias Ringwald #include "gap.h"
6045c102fdSMatthias Ringwald #endif
6145c102fdSMatthias Ringwald 
6293b8dc03Smatthias.ringwald #include <stdarg.h>
6393b8dc03Smatthias.ringwald #include <string.h>
6456fe0872Smatthias.ringwald #include <stdio.h>
655838a2edSMatthias Ringwald #include <inttypes.h>
667f2435e6Smatthias.ringwald 
6716ece135SMatthias Ringwald #include "btstack_debug.h"
680e2df43fSMatthias Ringwald #include "btstack_event.h"
694a3574a1SMatthias Ringwald #include "btstack_linked_list.h"
704a3574a1SMatthias Ringwald #include "btstack_memory.h"
714a3574a1SMatthias Ringwald #include "gap.h"
724a3574a1SMatthias Ringwald #include "hci.h"
734a3574a1SMatthias Ringwald #include "hci_cmd.h"
74d8905019Smatthias.ringwald #include "hci_dump.h"
7593b8dc03Smatthias.ringwald 
761b0e3922Smatthias.ringwald 
77169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
78659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200
79ee091cf1Smatthias.ringwald 
80b83d5eabSMatthias Ringwald // prototypes
8135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
82758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
8335454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled);
8435454696SMatthias Ringwald static int  hci_local_ssp_activated(void);
8535454696SMatthias Ringwald static int  hci_remote_ssp_supported(hci_con_handle_t con_handle);
8635454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void);
8735454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status);
88a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
8935454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level);
90ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer);
9196a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
9252db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
9352db98b2SMatthias Ringwald #endif
947586ee35S[email protected] static int  hci_power_control_on(void);
957586ee35S[email protected] static void hci_power_control_off(void);
966da48142SSean Wilson static void hci_state_reset(void);
97fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason);
98b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void);
99b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void);
100b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status);
101b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump);
102b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size);
10395d71764SMatthias Ringwald static void hci_run(void);
10495d71764SMatthias Ringwald static int  hci_is_le_connection(hci_connection_t * connection);
10595d71764SMatthias Ringwald static int  hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type);
1065d509858SMatthias Ringwald 
107a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
108e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
10939677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c
11039677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size);
11142ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address);
1125d509858SMatthias Ringwald #endif
113d70217a2SMatthias Ringwald #endif
114758b46ceSmatthias.ringwald 
11506b35ec0Smatthias.ringwald // the STACK is here
1163a9fb326S[email protected] #ifndef HAVE_MALLOC
1173a9fb326S[email protected] static hci_stack_t   hci_stack_static;
1183a9fb326S[email protected] #endif
1193a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
12016833f0aSmatthias.ringwald 
1211c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC
12266fb9560S[email protected] // test helper
12366fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
1241c9e5e9dSMatthias Ringwald #endif
12566fb9560S[email protected] 
12696a45072S[email protected] /**
12796a45072S[email protected]  * create connection for given address
12896a45072S[email protected]  *
12996a45072S[email protected]  * @return connection OR NULL, if no memory left
13096a45072S[email protected]  */
13196a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
1321a06f663S[email protected]     log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type);
133bb69aaaeS[email protected]     hci_connection_t * conn = btstack_memory_hci_connection_get();
13496a45072S[email protected]     if (!conn) return NULL;
135c91d150bS[email protected]     memset(conn, 0, sizeof(hci_connection_t));
136058e3d6bSMatthias Ringwald     bd_addr_copy(conn->address, addr);
13796a45072S[email protected]     conn->address_type = addr_type;
13896a45072S[email protected]     conn->con_handle = 0xffff;
13996a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
14096a45072S[email protected]     conn->bonding_flags = 0;
14196a45072S[email protected]     conn->requested_security_level = LEVEL_0;
14252db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
14391a977e8SMatthias Ringwald     btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler);
14491a977e8SMatthias Ringwald     btstack_run_loop_set_timer_context(&conn->timeout, conn);
14596a45072S[email protected]     hci_connection_timestamp(conn);
14652db98b2SMatthias Ringwald #endif
14796a45072S[email protected]     conn->acl_recombination_length = 0;
14896a45072S[email protected]     conn->acl_recombination_pos = 0;
14996a45072S[email protected]     conn->num_acl_packets_sent = 0;
1501a06f663S[email protected]     conn->num_sco_packets_sent = 0;
151da886c03S[email protected]     conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
152665d90f2SMatthias Ringwald     btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn);
15396a45072S[email protected]     return conn;
15496a45072S[email protected] }
15566fb9560S[email protected] 
156da886c03S[email protected] 
157da886c03S[email protected] /**
158da886c03S[email protected]  * get le connection parameter range
159da886c03S[email protected] *
160da886c03S[email protected]  * @return le connection parameter range struct
161da886c03S[email protected]  */
1624ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){
1634ced4e8cSMatthias Ringwald     *range = hci_stack->le_connection_parameter_range;
164da886c03S[email protected] }
165da886c03S[email protected] 
166da886c03S[email protected] /**
167da886c03S[email protected]  * set le connection parameter range
168da886c03S[email protected]  *
169da886c03S[email protected]  */
170da886c03S[email protected] 
1714ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){
1724ced4e8cSMatthias Ringwald     hci_stack->le_connection_parameter_range = *range;
173da886c03S[email protected] }
174da886c03S[email protected] 
175da886c03S[email protected] /**
176da886c03S[email protected]  * get hci connections iterator
177da886c03S[email protected]  *
178da886c03S[email protected]  * @return hci connections iterator
179da886c03S[email protected]  */
180da886c03S[email protected] 
181665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
182665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(it, &hci_stack->connections);
183da886c03S[email protected] }
184da886c03S[email protected] 
18597addcc5Smatthias.ringwald /**
186ee091cf1Smatthias.ringwald  * get connection for a given handle
187ee091cf1Smatthias.ringwald  *
188ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
189ee091cf1Smatthias.ringwald  */
1905061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
191665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
192665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
193665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
194665d90f2SMatthias Ringwald         hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
1953ac2fe56S[email protected]         if ( item->con_handle == con_handle ) {
196da886c03S[email protected]             return item;
197ee091cf1Smatthias.ringwald         }
198ee091cf1Smatthias.ringwald     }
199ee091cf1Smatthias.ringwald     return NULL;
200ee091cf1Smatthias.ringwald }
201ee091cf1Smatthias.ringwald 
20296a45072S[email protected] /**
20396a45072S[email protected]  * get connection for given address
20496a45072S[email protected]  *
20596a45072S[email protected]  * @return connection OR NULL, if not found
20696a45072S[email protected]  */
2072e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t  addr, bd_addr_type_t addr_type){
208665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
209665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
210665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
211665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
21296a45072S[email protected]         if (connection->address_type != addr_type)  continue;
21396a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
21462bda3fbS[email protected]         return connection;
21562bda3fbS[email protected]     }
21662bda3fbS[email protected]     return NULL;
21762bda3fbS[email protected] }
21862bda3fbS[email protected] 
21952db98b2SMatthias Ringwald 
22052db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
22152db98b2SMatthias Ringwald 
222ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){
22391a977e8SMatthias Ringwald     hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer);
224aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
225528a4a3bSMatthias Ringwald     if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
226c785ef68Smatthias.ringwald         // connections might be timed out
227c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
228c785ef68Smatthias.ringwald     }
229f316a845SMatthias Ringwald #else
230528a4a3bSMatthias Ringwald     if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){
2315f26aadcSMatthias Ringwald         // connections might be timed out
2325f26aadcSMatthias Ringwald         hci_emit_l2cap_check_timeout(connection);
2335f26aadcSMatthias Ringwald     }
2345f26aadcSMatthias Ringwald #endif
235c785ef68Smatthias.ringwald }
236ee091cf1Smatthias.ringwald 
237ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
238aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
239528a4a3bSMatthias Ringwald     connection->timestamp = btstack_run_loop_embedded_get_ticks();
240f316a845SMatthias Ringwald #else
241528a4a3bSMatthias Ringwald     connection->timestamp = btstack_run_loop_get_time_ms();
2425f26aadcSMatthias Ringwald #endif
243ee091cf1Smatthias.ringwald }
244ee091cf1Smatthias.ringwald 
24528ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
24628ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
24728ca2b46S[email protected] }
24828ca2b46S[email protected] 
24935454696SMatthias Ringwald 
25028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
25128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
25228ca2b46S[email protected] }
25328ca2b46S[email protected] 
25443bfb1bdSmatthias.ringwald /**
25580ca58a0Smatthias.ringwald  * add authentication flags and reset timer
25696a45072S[email protected]  * @note: assumes classic connection
2572e77e513S[email protected]  * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets
2587fde4af9Smatthias.ringwald  */
2597fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
2607fde4af9Smatthias.ringwald     bd_addr_t addr;
261724d70a2SMatthias Ringwald     reverse_bd_addr(bd_addr, addr);
2622e77e513S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2637fde4af9Smatthias.ringwald     if (conn) {
26428ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
26580ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
2667fde4af9Smatthias.ringwald     }
2677fde4af9Smatthias.ringwald }
2687fde4af9Smatthias.ringwald 
26980ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2705061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
27180ca58a0Smatthias.ringwald     if (!conn) return 0;
2726724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2736724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2746724cd9eS[email protected]     return 0;
27580ca58a0Smatthias.ringwald }
27680ca58a0Smatthias.ringwald 
27715a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){
27855597469SMatthias Ringwald     if (!hci_stack->link_key_db) return;
2792bacf595SMatthias Ringwald     log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr));
280a98592bcSMatthias Ringwald     hci_stack->link_key_db->delete_link_key(addr);
281c12e46e7Smatthias.ringwald }
28255597469SMatthias Ringwald 
28355597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){
28455597469SMatthias Ringwald     if (!hci_stack->link_key_db) return;
2852bacf595SMatthias Ringwald     log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type);
28655597469SMatthias Ringwald     hci_stack->link_key_db->put_link_key(addr, link_key, type);
287c12e46e7Smatthias.ringwald }
28835454696SMatthias Ringwald #endif
289c12e46e7Smatthias.ringwald 
29095d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){
2910bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2920bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2930bf6344aS[email protected] }
2940bf6344aS[email protected] 
2957fde4af9Smatthias.ringwald /**
29643bfb1bdSmatthias.ringwald  * count connections
29743bfb1bdSmatthias.ringwald  */
29840d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
29956c253c9Smatthias.ringwald     int count = 0;
300665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
301665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
30243bfb1bdSmatthias.ringwald     return count;
30343bfb1bdSmatthias.ringwald }
304c8e4258aSmatthias.ringwald 
30595d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){
306ee303eddS[email protected] 
307f04a0c31SMatthias Ringwald     unsigned int num_packets_sent_classic = 0;
308f04a0c31SMatthias Ringwald     unsigned int num_packets_sent_le = 0;
309ee303eddS[email protected] 
310665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
311665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
312998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
313ee303eddS[email protected]         if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
314ee303eddS[email protected]             num_packets_sent_classic += connection->num_acl_packets_sent;
315ee303eddS[email protected]         } else {
316ee303eddS[email protected]             num_packets_sent_le += connection->num_acl_packets_sent;
317ee303eddS[email protected]         }
318ee303eddS[email protected]     }
319d999b54eSMatthias Ringwald     log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num);
320ee303eddS[email protected]     int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic;
321ee303eddS[email protected]     int free_slots_le = 0;
322ee303eddS[email protected] 
323ee303eddS[email protected]     if (free_slots_classic < 0){
3249da54300S[email protected]         log_error("hci_number_free_acl_slots: outgoing classic packets (%u) > total classic packets (%u)", num_packets_sent_classic, hci_stack->acl_packets_total_num);
325998906cdSmatthias.ringwald         return 0;
326998906cdSmatthias.ringwald     }
327ee303eddS[email protected] 
328ee303eddS[email protected]     if (hci_stack->le_acl_packets_total_num){
329ee303eddS[email protected]         // if we have LE slots, they are used
330ee303eddS[email protected]         free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le;
331ee303eddS[email protected]         if (free_slots_le < 0){
3329da54300S[email protected]             log_error("hci_number_free_acl_slots: outgoing le packets (%u) > total le packets (%u)", num_packets_sent_le, hci_stack->le_acl_packets_total_num);
333ee303eddS[email protected]             return 0;
334998906cdSmatthias.ringwald         }
335ee303eddS[email protected]     } else {
336ee303eddS[email protected]         // otherwise, classic slots are used for LE, too
337ee303eddS[email protected]         free_slots_classic -= num_packets_sent_le;
338ee303eddS[email protected]         if (free_slots_classic < 0){
3399da54300S[email protected]             log_error("hci_number_free_acl_slots: outgoing classic + le packets (%u + %u) > total packets (%u)", num_packets_sent_classic, num_packets_sent_le, hci_stack->acl_packets_total_num);
340ee303eddS[email protected]             return 0;
341ee303eddS[email protected]         }
342ee303eddS[email protected]     }
343ee303eddS[email protected] 
344ee303eddS[email protected]     switch (address_type){
345ee303eddS[email protected]         case BD_ADDR_TYPE_UNKNOWN:
3462125de09SMatthias Ringwald             log_error("hci_number_free_acl_slots: unknown address type");
347ee303eddS[email protected]             return 0;
348ee303eddS[email protected] 
349ee303eddS[email protected]         case BD_ADDR_TYPE_CLASSIC:
350ee303eddS[email protected]             return free_slots_classic;
351ee303eddS[email protected] 
352ee303eddS[email protected]         default:
353cb00d3aaS[email protected]            if (hci_stack->le_acl_packets_total_num){
354ee303eddS[email protected]                return free_slots_le;
355ee303eddS[email protected]            }
356cb00d3aaS[email protected]            return free_slots_classic;
357cb00d3aaS[email protected]     }
358998906cdSmatthias.ringwald }
359998906cdSmatthias.ringwald 
3602125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){
3612125de09SMatthias Ringwald     // get connection type
3622125de09SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3632125de09SMatthias Ringwald     if (!connection){
3642125de09SMatthias Ringwald         log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle);
3652125de09SMatthias Ringwald         return 0;
3662125de09SMatthias Ringwald     }
3672125de09SMatthias Ringwald     return hci_number_free_acl_slots_for_connection_type(connection->address_type);
3682125de09SMatthias Ringwald }
3692125de09SMatthias Ringwald 
37035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
371701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){
372f04a0c31SMatthias Ringwald     unsigned int num_sco_packets_sent  = 0;
373665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
374665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
375e35edcc1S[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
376e35edcc1S[email protected]         num_sco_packets_sent += connection->num_sco_packets_sent;
377e35edcc1S[email protected]     }
378e35edcc1S[email protected]     if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
379701e3307SMatthias Ringwald         log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num);
38044d0e3d5S[email protected]         return 0;
38144d0e3d5S[email protected]     }
382701e3307SMatthias Ringwald     // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent);
383e35edcc1S[email protected]     return hci_stack->sco_packets_total_num - num_sco_packets_sent;
384e35edcc1S[email protected] }
38535454696SMatthias Ringwald #endif
38644d0e3d5S[email protected] 
387ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
388ac928cc2S[email protected] int hci_can_send_command_packet_now(void){
389ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
390ac928cc2S[email protected] 
391ac928cc2S[email protected]     // check for async hci transport implementations
392ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
393ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
394ac928cc2S[email protected]             return 0;
395ac928cc2S[email protected]         }
396ac928cc2S[email protected]     }
397ac928cc2S[email protected] 
398ac928cc2S[email protected]     return hci_stack->num_cmd_packets > 0;
399ac928cc2S[email protected] }
400ac928cc2S[email protected] 
4019d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){
402ac928cc2S[email protected]     // check for async hci transport implementations
4039d04d3a7SMatthias Ringwald     if (!hci_stack->hci_transport->can_send_packet_now) return 1;
4049d04d3a7SMatthias Ringwald     return hci_stack->hci_transport->can_send_packet_now(packet_type);
405ac928cc2S[email protected] }
4069d04d3a7SMatthias Ringwald 
4079d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){
4089d04d3a7SMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0;
4099d04d3a7SMatthias Ringwald     return hci_number_free_acl_slots_for_connection_type(address_type) > 0;
410ac928cc2S[email protected] }
4119d04d3a7SMatthias Ringwald 
4129d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){
4139d04d3a7SMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
4149d04d3a7SMatthias Ringwald     return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC);
4159d04d3a7SMatthias Ringwald }
4169d04d3a7SMatthias Ringwald 
4179d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
4189d04d3a7SMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0;
419e79abdd6S[email protected]     return hci_number_free_acl_slots_for_handle(con_handle) > 0;
420ac928cc2S[email protected] }
421ac928cc2S[email protected] 
422ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
423ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
424ac928cc2S[email protected]     return hci_can_send_prepared_acl_packet_now(con_handle);
4256b4af23dS[email protected] }
4266b4af23dS[email protected] 
42735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
42835454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){
42935454696SMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
43035454696SMatthias Ringwald     return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC);
43135454696SMatthias Ringwald }
43235454696SMatthias Ringwald 
433701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){
434d057580eSMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0;
435ed361f5fSMatthias Ringwald     if (!hci_stack->synchronous_flow_control_enabled) return 1;
436701e3307SMatthias Ringwald     return hci_number_free_sco_slots() > 0;
43744d0e3d5S[email protected] }
43844d0e3d5S[email protected] 
439701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){
440d057580eSMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
441701e3307SMatthias Ringwald     return hci_can_send_prepared_sco_packet_now();
44244d0e3d5S[email protected] }
44344d0e3d5S[email protected] 
444d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){
445d057580eSMatthias Ringwald     hci_stack->sco_waiting_for_can_send_now = 1;
446d057580eSMatthias Ringwald     hci_notify_if_sco_can_send_now();
447d057580eSMatthias Ringwald }
44835454696SMatthias Ringwald #endif
449d057580eSMatthias Ringwald 
45095d71764SMatthias Ringwald // used for internal checks in l2cap.c
451c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
452c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
453c8b9416aS[email protected] }
454c8b9416aS[email protected] 
4556b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
4566b4af23dS[email protected] int hci_reserve_packet_buffer(void){
4579d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
4589d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
4599d14b626S[email protected]         return 0;
4609d14b626S[email protected]     }
4616b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
4626b4af23dS[email protected]     return 1;
4636b4af23dS[email protected] }
4646b4af23dS[email protected] 
46568a0fcf7S[email protected] void hci_release_packet_buffer(void){
46668a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
46768a0fcf7S[email protected] }
46868a0fcf7S[email protected] 
4696b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
4707f02f414SMatthias Ringwald static int hci_transport_synchronous(void){
4716b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
4726b4af23dS[email protected] }
4736b4af23dS[email protected] 
474452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){
475452cf3bbS[email protected] 
476452cf3bbS[email protected]     // log_info("hci_send_acl_packet_fragments  %u/%u (con 0x%04x)", hci_stack->acl_fragmentation_pos, hci_stack->acl_fragmentation_total_size, connection->con_handle);
477452cf3bbS[email protected] 
478452cf3bbS[email protected]     // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers
479452cf3bbS[email protected]     uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length;
480452cf3bbS[email protected]     if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){
481452cf3bbS[email protected]         max_acl_data_packet_length = hci_stack->le_data_packets_length;
482452cf3bbS[email protected]     }
483452cf3bbS[email protected] 
484452cf3bbS[email protected]     // testing: reduce buffer to minimum
485452cf3bbS[email protected]     // max_acl_data_packet_length = 52;
486452cf3bbS[email protected] 
487d999b54eSMatthias Ringwald     log_debug("hci_send_acl_packet_fragments entered");
488d999b54eSMatthias Ringwald 
489452cf3bbS[email protected]     int err;
490452cf3bbS[email protected]     // multiple packets could be send on a synchronous HCI transport
491452cf3bbS[email protected]     while (1){
492452cf3bbS[email protected] 
493d999b54eSMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop entered");
494d999b54eSMatthias Ringwald 
495452cf3bbS[email protected]         // get current data
496452cf3bbS[email protected]         const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4;
497452cf3bbS[email protected]         int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
498452cf3bbS[email protected]         int more_fragments = 0;
499452cf3bbS[email protected] 
500452cf3bbS[email protected]         // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
501452cf3bbS[email protected]         if (current_acl_data_packet_length > max_acl_data_packet_length){
502452cf3bbS[email protected]             more_fragments = 1;
503452cf3bbS[email protected]             current_acl_data_packet_length = max_acl_data_packet_length;
504452cf3bbS[email protected]         }
505452cf3bbS[email protected] 
506452cf3bbS[email protected]         // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent)
507452cf3bbS[email protected]         if (acl_header_pos > 0){
508f8fbdce0SMatthias Ringwald             uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
509452cf3bbS[email protected]             handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12);
510f8fbdce0SMatthias Ringwald             little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
511452cf3bbS[email protected]         }
512452cf3bbS[email protected] 
513452cf3bbS[email protected]         // update header len
514f8fbdce0SMatthias Ringwald         little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length);
515452cf3bbS[email protected] 
516452cf3bbS[email protected]         // count packet
517452cf3bbS[email protected]         connection->num_acl_packets_sent++;
518f04a0c31SMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments);
519d999b54eSMatthias Ringwald 
520d999b54eSMatthias Ringwald         // update state for next fragment (if any) as "transport done" might be sent during send_packet already
521d999b54eSMatthias Ringwald         if (more_fragments){
522d999b54eSMatthias Ringwald             // update start of next fragment to send
523d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
524d999b54eSMatthias Ringwald         } else {
525d999b54eSMatthias Ringwald             // done
526d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_pos = 0;
527d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_total_size = 0;
528d999b54eSMatthias Ringwald         }
529452cf3bbS[email protected] 
530452cf3bbS[email protected]         // send packet
531452cf3bbS[email protected]         uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
532452cf3bbS[email protected]         const int size = current_acl_data_packet_length + 4;
5335bb5bc3eS[email protected]         hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
534452cf3bbS[email protected]         err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
535452cf3bbS[email protected] 
536f04a0c31SMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments);
537d999b54eSMatthias Ringwald 
538452cf3bbS[email protected]         // done yet?
539452cf3bbS[email protected]         if (!more_fragments) break;
540452cf3bbS[email protected] 
541452cf3bbS[email protected]         // can send more?
542452cf3bbS[email protected]         if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err;
543452cf3bbS[email protected]     }
544452cf3bbS[email protected] 
545d999b54eSMatthias Ringwald     log_debug("hci_send_acl_packet_fragments loop over");
546452cf3bbS[email protected] 
547d051460cS[email protected]     // release buffer now for synchronous transport
548203bace6S[email protected]     if (hci_transport_synchronous()){
549452cf3bbS[email protected]         hci_release_packet_buffer();
55063fa3374SMatthias Ringwald         // notify upper stack that it might be possible to send again
55163fa3374SMatthias Ringwald         uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
552d6b06661SMatthias Ringwald         hci_emit_event(&event[0], sizeof(event), 0);  // don't dump
553452cf3bbS[email protected]     }
554452cf3bbS[email protected] 
555452cf3bbS[email protected]     return err;
556452cf3bbS[email protected] }
557452cf3bbS[email protected] 
558826f7347S[email protected] // pre: caller has reserved the packet buffer
559826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
5607856c818Smatthias.ringwald 
561452cf3bbS[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
562452cf3bbS[email protected] 
563826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
564826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
565826f7347S[email protected]         return 0;
566826f7347S[email protected]     }
567826f7347S[email protected] 
568d713a683S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
569d713a683S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
570d713a683S[email protected] 
571826f7347S[email protected]     // check for free places on Bluetooth module
572d713a683S[email protected]     if (!hci_can_send_prepared_acl_packet_now(con_handle)) {
573826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
57497b61c7bS[email protected]         hci_release_packet_buffer();
57597b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
57697b61c7bS[email protected]     }
5776218e6f1Smatthias.ringwald 
5785061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
57997b61c7bS[email protected]     if (!connection) {
5805fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
58197b61c7bS[email protected]         hci_release_packet_buffer();
58297b61c7bS[email protected]         return 0;
58397b61c7bS[email protected]     }
58452db98b2SMatthias Ringwald 
58552db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
58656cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
58752db98b2SMatthias Ringwald #endif
58856cf178bSmatthias.ringwald 
589452cf3bbS[email protected]     // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size);
5907856c818Smatthias.ringwald 
591452cf3bbS[email protected]     // setup data
592452cf3bbS[email protected]     hci_stack->acl_fragmentation_total_size = size;
593452cf3bbS[email protected]     hci_stack->acl_fragmentation_pos = 4;   // start of L2CAP packet
5946218e6f1Smatthias.ringwald 
595452cf3bbS[email protected]     return hci_send_acl_packet_fragments(connection);
596ee091cf1Smatthias.ringwald }
597ee091cf1Smatthias.ringwald 
59835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
59944d0e3d5S[email protected] // pre: caller has reserved the packet buffer
60044d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){
60144d0e3d5S[email protected] 
60244d0e3d5S[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
60344d0e3d5S[email protected] 
60444d0e3d5S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
60544d0e3d5S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
60644d0e3d5S[email protected]         return 0;
60744d0e3d5S[email protected]     }
60844d0e3d5S[email protected] 
60944d0e3d5S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
6104b3e1e19SMatthias Ringwald 
6114b3e1e19SMatthias Ringwald     // skip checks in loopback mode
6124b3e1e19SMatthias Ringwald     if (!hci_stack->loopback_mode){
61344d0e3d5S[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);   // same for ACL and SCO
61444d0e3d5S[email protected] 
61544d0e3d5S[email protected]         // check for free places on Bluetooth module
616701e3307SMatthias Ringwald         if (!hci_can_send_prepared_sco_packet_now()) {
61744d0e3d5S[email protected]             log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller");
61844d0e3d5S[email protected]             hci_release_packet_buffer();
61944d0e3d5S[email protected]             return BTSTACK_ACL_BUFFERS_FULL;
62044d0e3d5S[email protected]         }
62144d0e3d5S[email protected] 
622e35edcc1S[email protected]         // track send packet in connection struct
623e35edcc1S[email protected]         hci_connection_t *connection = hci_connection_for_handle( con_handle);
624e35edcc1S[email protected]         if (!connection) {
625e35edcc1S[email protected]             log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle);
626e35edcc1S[email protected]             hci_release_packet_buffer();
627e35edcc1S[email protected]             return 0;
628e35edcc1S[email protected]         }
629e35edcc1S[email protected]         connection->num_sco_packets_sent++;
6304b3e1e19SMatthias Ringwald     }
63144d0e3d5S[email protected] 
63244d0e3d5S[email protected]     hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size);
633543e835cSMatthias Ringwald     int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size);
634543e835cSMatthias Ringwald 
635543e835cSMatthias Ringwald     if (hci_transport_synchronous()){
636543e835cSMatthias Ringwald         hci_release_packet_buffer();
63763fa3374SMatthias Ringwald         // notify upper stack that it might be possible to send again
63863fa3374SMatthias Ringwald         uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
639d6b06661SMatthias Ringwald         hci_emit_event(&event[0], sizeof(event), 0);    // don't dump
640543e835cSMatthias Ringwald     }
641543e835cSMatthias Ringwald 
642543e835cSMatthias Ringwald     return err;
64344d0e3d5S[email protected] }
64435454696SMatthias Ringwald #endif
64544d0e3d5S[email protected] 
64616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
6477856c818Smatthias.ringwald 
648e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
649e76a89eeS[email protected] 
6507856c818Smatthias.ringwald     // get info
6517856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
6525061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
6537856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
6547856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
6557856c818Smatthias.ringwald 
6567856c818Smatthias.ringwald     // ignore non-registered handle
6577856c818Smatthias.ringwald     if (!conn){
6589da54300S[email protected]         log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle);
6597856c818Smatthias.ringwald         return;
6607856c818Smatthias.ringwald     }
6617856c818Smatthias.ringwald 
662e76a89eeS[email protected]     // assert packet is complete
6639ecc3e17S[email protected]     if (acl_length + 4 != size){
664f04a0c31SMatthias Ringwald         log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4);
665e76a89eeS[email protected]         return;
666e76a89eeS[email protected]     }
667e76a89eeS[email protected] 
66852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
6697856c818Smatthias.ringwald     // update idle timestamp
6707856c818Smatthias.ringwald     hci_connection_timestamp(conn);
67152db98b2SMatthias Ringwald #endif
6727856c818Smatthias.ringwald 
6737856c818Smatthias.ringwald     // handle different packet types
6747856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
6757856c818Smatthias.ringwald 
6767856c818Smatthias.ringwald         case 0x01: // continuation fragment
6777856c818Smatthias.ringwald 
6780ca847afS[email protected]             // sanity checks
6797856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
6809da54300S[email protected]                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle);
6817856c818Smatthias.ringwald                 return;
6827856c818Smatthias.ringwald             }
6830ca847afS[email protected]             if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){
6840ca847afS[email protected]                 log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x",
6850ca847afS[email protected]                     conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
6860ca847afS[email protected]                 conn->acl_recombination_pos = 0;
6870ca847afS[email protected]                 return;
6880ca847afS[email protected]             }
6897856c818Smatthias.ringwald 
6907856c818Smatthias.ringwald             // append fragment payload (header already stored)
691ec6321eeS[email protected]             memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length );
6927856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
6937856c818Smatthias.ringwald 
6949da54300S[email protected]             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length,
695decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
6967856c818Smatthias.ringwald 
6977856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
6987856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
699d6b06661SMatthias Ringwald                 hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos);
7007856c818Smatthias.ringwald                 // reset recombination buffer
7017856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
7027856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
7037856c818Smatthias.ringwald             }
7047856c818Smatthias.ringwald             break;
7057856c818Smatthias.ringwald 
7067856c818Smatthias.ringwald         case 0x02: { // first fragment
7077856c818Smatthias.ringwald 
70823a77e1aS[email protected]             // sanity check
70923a77e1aS[email protected]             if (conn->acl_recombination_pos) {
71023a77e1aS[email protected]                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle);
71123a77e1aS[email protected]                 conn->acl_recombination_pos = 0;
71223a77e1aS[email protected]             }
71323a77e1aS[email protected] 
7147856c818Smatthias.ringwald             // peek into L2CAP packet!
7157856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
7167856c818Smatthias.ringwald 
7179da54300S[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length);
718decc01a8Smatthias.ringwald 
7197856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
7207856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
7217856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
722d6b06661SMatthias Ringwald                 hci_emit_acl_packet(packet, acl_length + 4);
7237856c818Smatthias.ringwald             } else {
7240ca847afS[email protected] 
7250ca847afS[email protected]                 if (acl_length > HCI_ACL_BUFFER_SIZE){
7260ca847afS[email protected]                     log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x",
7270ca847afS[email protected]                         4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
7280ca847afS[email protected]                     return;
7290ca847afS[email protected]                 }
7300ca847afS[email protected] 
7317856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
732ec6321eeS[email protected]                 memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4);
7337856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
7347856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
735f8fbdce0SMatthias Ringwald                 little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4);
7367856c818Smatthias.ringwald             }
7377856c818Smatthias.ringwald             break;
7387856c818Smatthias.ringwald 
7397856c818Smatthias.ringwald         }
7407856c818Smatthias.ringwald         default:
7419da54300S[email protected]             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03);
7427856c818Smatthias.ringwald             return;
7437856c818Smatthias.ringwald     }
74494ab26f8Smatthias.ringwald 
74594ab26f8Smatthias.ringwald     // execute main loop
74694ab26f8Smatthias.ringwald     hci_run();
74716833f0aSmatthias.ringwald }
74822909952Smatthias.ringwald 
74967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
7509da54300S[email protected]     log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address));
7513c4d4b90Smatthias.ringwald 
752528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&conn->timeout);
753c785ef68Smatthias.ringwald 
754665d90f2SMatthias Ringwald     btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
755a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
7563c4d4b90Smatthias.ringwald 
7573c4d4b90Smatthias.ringwald     // now it's gone
758c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
759c7e0c5f6Smatthias.ringwald }
760c7e0c5f6Smatthias.ringwald 
76135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
76235454696SMatthias Ringwald 
7630c042179S[email protected] static const uint16_t packet_type_sizes[] = {
7648f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
7658f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
7668f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
7678f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
7688f8108aaSmatthias.ringwald };
76965389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
77065389bfcS[email protected]      0, // 3 slot packets
77165389bfcS[email protected]      1, // 5 slot packets
77265389bfcS[email protected]     25, // EDR 2 mpbs
77365389bfcS[email protected]     26, // EDR 3 mbps
77465389bfcS[email protected]     39, // 3 slot EDR packts
77565389bfcS[email protected]     40, // 5 slot EDR packet
77665389bfcS[email protected] };
77765389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
77865389bfcS[email protected]     0x0f00, // 3 slot packets
77965389bfcS[email protected]     0xf000, // 5 slot packets
78065389bfcS[email protected]     0x1102, // EDR 2 mpbs
78165389bfcS[email protected]     0x2204, // EDR 3 mbps
78265389bfcS[email protected]     0x0300, // 3 slot EDR packts
78365389bfcS[email protected]     0x3000, // 5 slot EDR packet
78465389bfcS[email protected] };
7858f8108aaSmatthias.ringwald 
78665389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
78765389bfcS[email protected]     // enable packet types based on size
7888f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
789f16a69bbS[email protected]     unsigned int i;
7908f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
7918f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
7928f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
7938f8108aaSmatthias.ringwald             packet_types |= 1 << i;
7948f8108aaSmatthias.ringwald         }
7958f8108aaSmatthias.ringwald     }
79665389bfcS[email protected]     // disable packet types due to missing local supported features
79765389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
798f04a0c31SMatthias Ringwald         unsigned int bit_idx = packet_type_feature_requirement_bit[i];
79965389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
80065389bfcS[email protected]         if (feature_set) continue;
80165389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
80265389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
80365389bfcS[email protected]     }
8048f8108aaSmatthias.ringwald     // flip bits for "may not be used"
8058f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
8068f8108aaSmatthias.ringwald     return packet_types;
8078f8108aaSmatthias.ringwald }
8088f8108aaSmatthias.ringwald 
8098f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
8103a9fb326S[email protected]     return hci_stack->packet_types;
8118f8108aaSmatthias.ringwald }
81235454696SMatthias Ringwald #endif
8138f8108aaSmatthias.ringwald 
814facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
8157dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
8163a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
8177dc17943Smatthias.ringwald }
8187dc17943Smatthias.ringwald 
819f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
8203a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
8217dc17943Smatthias.ringwald }
8227dc17943Smatthias.ringwald 
82306b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC
8243e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){
8253e68d23dSMatthias Ringwald     // No. 31, byte 3, bit 7
8263e68d23dSMatthias Ringwald     return (hci_stack->local_supported_features[3] & (1 << 7)) != 0;
8273e68d23dSMatthias Ringwald }
82806b9e820SMatthias Ringwald #endif
8293e68d23dSMatthias Ringwald 
8306ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
8316ac9a97eS[email protected]     // No. 54, byte 6, bit 6
8326ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
8336ac9a97eS[email protected] }
8346ac9a97eS[email protected] 
83515a95bd5SMatthias Ringwald static int gap_ssp_supported(void){
8366ac9a97eS[email protected]     // No. 51, byte 6, bit 3
8373a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
838f5d8d141S[email protected] }
839f5d8d141S[email protected] 
8407f02f414SMatthias Ringwald static int hci_classic_supported(void){
84135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
8426ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
8433a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
84435454696SMatthias Ringwald #else
84535454696SMatthias Ringwald     return 0;
84635454696SMatthias Ringwald #endif
847f5d8d141S[email protected] }
848f5d8d141S[email protected] 
8497f02f414SMatthias Ringwald static int hci_le_supported(void){
850a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
8516ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
8523a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
853f5d8d141S[email protected] #else
854f5d8d141S[email protected]     return 0;
855f5d8d141S[email protected] #endif
856f5d8d141S[email protected] }
857f5d8d141S[email protected] 
858b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE
859b95a5a35SMatthias Ringwald 
860b95a5a35SMatthias Ringwald /**
861b95a5a35SMatthias Ringwald  * @brief Get addr type and address used for LE in Advertisements, Scan Responses,
862b95a5a35SMatthias Ringwald  */
863b95a5a35SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){
864b95a5a35SMatthias Ringwald     *addr_type = hci_stack->le_own_addr_type;
865b95a5a35SMatthias Ringwald     if (hci_stack->le_own_addr_type){
866b95a5a35SMatthias Ringwald         memcpy(addr, hci_stack->le_random_address, 6);
86769a97523S[email protected]     } else {
8683a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
86969a97523S[email protected]     }
87069a97523S[email protected] }
87169a97523S[email protected] 
872e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
87339677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){
8749ec2630cSMatthias Ringwald 
8759ec2630cSMatthias Ringwald     UNUSED(size);
8769ec2630cSMatthias Ringwald 
877d1dc057bS[email protected]     int offset = 3;
878d1dc057bS[email protected]     int num_reports = packet[offset];
879d1dc057bS[email protected]     offset += 1;
880d1dc057bS[email protected] 
88157c9da5bS[email protected]     int i;
8824f4e0224SMatthias Ringwald     // log_info("HCI: handle adv report with num reports: %d", num_reports);
88303fbe9c6S[email protected]     uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
88457c9da5bS[email protected]     for (i=0; i<num_reports;i++){
885210c6774S[email protected]         uint8_t data_length = packet[offset + 8];
886a0aac9c4S[email protected]         uint8_t event_size = 10 + data_length;
887d1dc057bS[email protected]         int pos = 0;
888045013feSMatthias Ringwald         event[pos++] = GAP_EVENT_ADVERTISING_REPORT;
88957c9da5bS[email protected]         event[pos++] = event_size;
890210c6774S[email protected]         memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address
891d1dc057bS[email protected]         offset += 8;
892d1dc057bS[email protected]         pos += 8;
893d1dc057bS[email protected]         event[pos++] = packet[offset + 1 + data_length]; // rssi
894d1dc057bS[email protected]         event[pos++] = packet[offset++]; //data_length;
895d1dc057bS[email protected]         memcpy(&event[pos], &packet[offset], data_length);
89657c9da5bS[email protected]         pos += data_length;
897d1dc057bS[email protected]         offset += data_length + 1; // rssi
898d6b06661SMatthias Ringwald         hci_emit_event(event, pos, 1);
89957c9da5bS[email protected]     }
90057c9da5bS[email protected] }
901b2f949feS[email protected] #endif
902e8c8828eSMatthias Ringwald #endif
90357c9da5bS[email protected] 
904cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
90506b9e820SMatthias Ringwald 
90696b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){
90796b53536SMatthias Ringwald     if (!hci_stack->config) return 0;
9089796ebeaSMatthias Ringwald     uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
90996b53536SMatthias Ringwald     // Limit baud rate for Broadcom chipsets to 3 mbps
910c6d1dbedSMatthias Ringwald     if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){
91196b53536SMatthias Ringwald         baud_rate = 3000000;
91296b53536SMatthias Ringwald     }
91396b53536SMatthias Ringwald     return baud_rate;
91496b53536SMatthias Ringwald }
91596b53536SMatthias Ringwald 
916ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){
9179ec2630cSMatthias Ringwald     UNUSED(ds);
9189ec2630cSMatthias Ringwald 
9190305bdeaSMatthias Ringwald     switch (hci_stack->substate){
9200305bdeaSMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
9217b0d7667SMatthias Ringwald             log_info("Resend HCI Reset");
9220305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET;
9237b0d7667SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
9240305bdeaSMatthias Ringwald             hci_run();
9250305bdeaSMatthias Ringwald             break;
9269f007422SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET:
9279f007422SMatthias Ringwald             log_info("Resend HCI Reset - CSR Warm Boot with Link Reset");
9289f007422SMatthias Ringwald             if (hci_stack->hci_transport->reset_link){
9299f007422SMatthias Ringwald                 hci_stack->hci_transport->reset_link();
9309f007422SMatthias Ringwald             }
931202c8a4cSMatthias Ringwald             // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT
932e47e68c7SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
933e47e68c7SMatthias Ringwald             log_info("Resend HCI Reset - CSR Warm Boot");
934e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
935e47e68c7SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
936e47e68c7SMatthias Ringwald             hci_run();
937688c2635SMatthias Ringwald             break;
9387224be7eSMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
9397224be7eSMatthias Ringwald             if (hci_stack->hci_transport->set_baudrate){
94096b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
941cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate);
9427dd9d0ecSMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
9437224be7eSMatthias Ringwald             }
944772a36d3SMatthias Ringwald             // For CSR, HCI Reset is sent on new baud rate
9452caefae9SMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO){
946772a36d3SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
947772a36d3SMatthias Ringwald                 hci_run();
948772a36d3SMatthias Ringwald             }
9494696bddbSMatthias Ringwald             break;
9500305bdeaSMatthias Ringwald         default:
9510305bdeaSMatthias Ringwald             break;
9520305bdeaSMatthias Ringwald     }
9530305bdeaSMatthias Ringwald }
95406b9e820SMatthias Ringwald #endif
9550305bdeaSMatthias Ringwald 
95671de195eSMatthias Ringwald static void hci_initializing_next_state(void){
95774b323a9SMatthias Ringwald     hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1);
95874b323a9SMatthias Ringwald }
95974b323a9SMatthias Ringwald 
96074b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true
96171de195eSMatthias Ringwald static void hci_initializing_run(void){
962148ca237SMatthias Ringwald     log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now());
96374b323a9SMatthias Ringwald     switch (hci_stack->substate){
96474b323a9SMatthias Ringwald         case HCI_INIT_SEND_RESET:
96574b323a9SMatthias Ringwald             hci_state_reset();
966a0cf2f3fSMatthias Ringwald 
96706b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
9680305bdeaSMatthias Ringwald             // prepare reset if command complete not received in 100ms
969659d758cSMatthias Ringwald             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
970528a4a3bSMatthias Ringwald             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
971528a4a3bSMatthias Ringwald             btstack_run_loop_add_timer(&hci_stack->timeout);
972a0cf2f3fSMatthias Ringwald #endif
9730305bdeaSMatthias Ringwald             // send command
97474b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET;
9750305bdeaSMatthias Ringwald             hci_send_cmd(&hci_reset);
97674b323a9SMatthias Ringwald             break;
97776fcb19bSMatthias Ringwald         case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION:
97876fcb19bSMatthias Ringwald             hci_send_cmd(&hci_read_local_version_information);
97976fcb19bSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION;
98076fcb19bSMatthias Ringwald             break;
981e90bae01SMatthias Ringwald         case HCI_INIT_SEND_READ_LOCAL_NAME:
982e90bae01SMatthias Ringwald             hci_send_cmd(&hci_read_local_name);
983e90bae01SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME;
984e90bae01SMatthias Ringwald             break;
98506b9e820SMatthias Ringwald 
98606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
987e47e68c7SMatthias Ringwald         case HCI_INIT_SEND_RESET_CSR_WARM_BOOT:
988e47e68c7SMatthias Ringwald             hci_state_reset();
989e47e68c7SMatthias Ringwald             // prepare reset if command complete not received in 100ms
990659d758cSMatthias Ringwald             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
991528a4a3bSMatthias Ringwald             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
992528a4a3bSMatthias Ringwald             btstack_run_loop_add_timer(&hci_stack->timeout);
993e47e68c7SMatthias Ringwald             // send command
994e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
995e47e68c7SMatthias Ringwald             hci_send_cmd(&hci_reset);
996e47e68c7SMatthias Ringwald             break;
9978d29070eSMatthias Ringwald         case HCI_INIT_SEND_RESET_ST_WARM_BOOT:
9988d29070eSMatthias Ringwald             hci_state_reset();
9998d29070eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT;
10008d29070eSMatthias Ringwald             hci_send_cmd(&hci_reset);
10018d29070eSMatthias Ringwald             break;
1002fab26ab3SMatthias Ringwald         case HCI_INIT_SEND_BAUD_CHANGE: {
100396b53536SMatthias Ringwald             uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
10043fb36a29SMatthias Ringwald             hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer);
1005f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
100674b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
10070305bdeaSMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
10084696bddbSMatthias Ringwald             // STLC25000D: baudrate change happens within 0.5 s after command was send,
10094696bddbSMatthias Ringwald             // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial)
1010c6d1dbedSMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
1011659d758cSMatthias Ringwald                 btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
1012528a4a3bSMatthias Ringwald                 btstack_run_loop_add_timer(&hci_stack->timeout);
10134696bddbSMatthias Ringwald             }
101474b323a9SMatthias Ringwald             break;
1015fab26ab3SMatthias Ringwald         }
1016fab26ab3SMatthias Ringwald         case HCI_INIT_SEND_BAUD_CHANGE_BCM: {
101796b53536SMatthias Ringwald             uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
10183fb36a29SMatthias Ringwald             hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer);
1019f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
1020eb3a5314SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM;
1021eb3a5314SMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1022eb3a5314SMatthias Ringwald             break;
1023fab26ab3SMatthias Ringwald         }
102474b323a9SMatthias Ringwald         case HCI_INIT_CUSTOM_INIT:
102574b323a9SMatthias Ringwald             // Custom initialization
10263fb36a29SMatthias Ringwald             if (hci_stack->chipset && hci_stack->chipset->next_command){
10273fb36a29SMatthias Ringwald                 int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer);
102874b323a9SMatthias Ringwald                 if (valid_cmd){
102974b323a9SMatthias Ringwald                     int size = 3 + hci_stack->hci_packet_buffer[2];
1030f8fbdce0SMatthias Ringwald                     hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
103174b323a9SMatthias Ringwald                     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size);
1032e47e68c7SMatthias Ringwald                     switch (valid_cmd) {
1033e47e68c7SMatthias Ringwald                         case 1:
1034e47e68c7SMatthias Ringwald                         default:
1035e47e68c7SMatthias Ringwald                             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT;
1036e47e68c7SMatthias Ringwald                             break;
1037e47e68c7SMatthias Ringwald                         case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete
1038e47e68c7SMatthias Ringwald                             log_info("CSR Warm Boot");
1039659d758cSMatthias Ringwald                             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
1040528a4a3bSMatthias Ringwald                             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
1041528a4a3bSMatthias Ringwald                             btstack_run_loop_add_timer(&hci_stack->timeout);
10422caefae9SMatthias Ringwald                             if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO
1043772a36d3SMatthias Ringwald                                 && hci_stack->config
10443fb36a29SMatthias Ringwald                                 && hci_stack->chipset
10453fb36a29SMatthias Ringwald                                 // && hci_stack->chipset->set_baudrate_command -- there's no such command
1046772a36d3SMatthias Ringwald                                 && hci_stack->hci_transport->set_baudrate
10472caefae9SMatthias Ringwald                                 && hci_transport_uart_get_main_baud_rate()){
1048772a36d3SMatthias Ringwald                                 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
1049772a36d3SMatthias Ringwald                             } else {
10509f007422SMatthias Ringwald                                hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET;
1051772a36d3SMatthias Ringwald                             }
1052e47e68c7SMatthias Ringwald                             break;
1053e47e68c7SMatthias Ringwald                     }
10540305bdeaSMatthias Ringwald                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
105574b323a9SMatthias Ringwald                     break;
105674b323a9SMatthias Ringwald                 }
1057148ca237SMatthias Ringwald                 log_info("Init script done");
105892a0d36dSMatthias Ringwald 
105992a0d36dSMatthias Ringwald                 // Init script download causes baud rate to reset on Broadcom chipsets, restore UART baud rate if needed
1060c6d1dbedSMatthias Ringwald                 if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
106192a0d36dSMatthias Ringwald                     int need_baud_change = hci_stack->config
10623fb36a29SMatthias Ringwald                         && hci_stack->chipset
10633fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_baudrate_command
106492a0d36dSMatthias Ringwald                         && hci_stack->hci_transport->set_baudrate
10659796ebeaSMatthias Ringwald                         && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
106692a0d36dSMatthias Ringwald                     if (need_baud_change) {
10679796ebeaSMatthias Ringwald                         uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init;
1068cd724cb7SMatthias Ringwald                         log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate);
106992a0d36dSMatthias Ringwald                         hci_stack->hci_transport->set_baudrate(baud_rate);
107092a0d36dSMatthias Ringwald                     }
107192a0d36dSMatthias Ringwald                 }
107274b323a9SMatthias Ringwald             }
107374b323a9SMatthias Ringwald             // otherwise continue
1074a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
1075a828a756SMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_commands);
1076a828a756SMatthias Ringwald             break;
107753860077SMatthias Ringwald         case HCI_INIT_SET_BD_ADDR:
107853860077SMatthias Ringwald             log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr));
10793fb36a29SMatthias Ringwald             hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer);
1080f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
108153860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR;
108253860077SMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
108353860077SMatthias Ringwald             break;
108406b9e820SMatthias Ringwald #endif
108506b9e820SMatthias Ringwald 
108606b9e820SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS:
108706b9e820SMatthias Ringwald             log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset");
108806b9e820SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
108906b9e820SMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_commands);
109006b9e820SMatthias Ringwald             break;
109153860077SMatthias Ringwald         case HCI_INIT_READ_BD_ADDR:
109253860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR;
109353860077SMatthias Ringwald             hci_send_cmd(&hci_read_bd_addr);
109453860077SMatthias Ringwald             break;
109574b323a9SMatthias Ringwald         case HCI_INIT_READ_BUFFER_SIZE:
109674b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE;
10970305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_buffer_size);
109874b323a9SMatthias Ringwald             break;
109953860077SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES:
110053860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES;
11010305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_features);
110274b323a9SMatthias Ringwald             break;
110374b323a9SMatthias Ringwald         case HCI_INIT_SET_EVENT_MASK:
11040305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK;
110574b323a9SMatthias Ringwald             if (hci_le_supported()){
110674b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
110774b323a9SMatthias Ringwald             } else {
110874b323a9SMatthias Ringwald                 // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
110974b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
111074b323a9SMatthias Ringwald             }
111174b323a9SMatthias Ringwald             break;
111235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
111374b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE:
111474b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE;
11150305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
111674b323a9SMatthias Ringwald             break;
111774b323a9SMatthias Ringwald         case HCI_INIT_WRITE_PAGE_TIMEOUT:
111874b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT;
11190305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_page_timeout, 0x6000);  // ca. 15 sec
112074b323a9SMatthias Ringwald             break;
112174b323a9SMatthias Ringwald         case HCI_INIT_WRITE_CLASS_OF_DEVICE:
112274b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE;
11230305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
112474b323a9SMatthias Ringwald             break;
112574b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LOCAL_NAME:
11260305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME;
112774b323a9SMatthias Ringwald             if (hci_stack->local_name){
112874b323a9SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
112974b323a9SMatthias Ringwald             } else {
11307224be7eSMatthias Ringwald                 char local_name[8+17+1];
11317224be7eSMatthias Ringwald                 // BTstack 11:22:33:44:55:66
11327224be7eSMatthias Ringwald                 memcpy(local_name, "BTstack ", 8);
11337224be7eSMatthias Ringwald                 memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17);   // strlen(bd_addr_to_str(...)) = 17
11347224be7eSMatthias Ringwald                 local_name[8+17] = '\0';
113593bcd4d0SMatthias Ringwald                 log_info("---> Name %s", local_name);
113693bcd4d0SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, local_name);
113774b323a9SMatthias Ringwald             }
113874b323a9SMatthias Ringwald             break;
1139ff00ed1cSMatthias Ringwald         case HCI_INIT_WRITE_EIR_DATA:
11408a114470SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA;
1141ff00ed1cSMatthias Ringwald             hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data);
1142ff00ed1cSMatthias Ringwald             break;
1143f6858d14SMatthias Ringwald         case HCI_INIT_WRITE_INQUIRY_MODE:
1144f6858d14SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE;
11458a114470SMatthias Ringwald             hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode);
1146f6858d14SMatthias Ringwald             break;
114774b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SCAN_ENABLE:
114874b323a9SMatthias Ringwald             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
114974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE;
115074b323a9SMatthias Ringwald             break;
1151483c5078SMatthias Ringwald         // only sent if ENABLE_SCO_OVER_HCI is defined
1152729ed62eSMatthias Ringwald         case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE:
1153729ed62eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE;
1154729ed62eSMatthias Ringwald             hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled
1155729ed62eSMatthias Ringwald             break;
1156483c5078SMatthias Ringwald         case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING:
1157483c5078SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING;
1158483c5078SMatthias Ringwald             hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1);
1159483c5078SMatthias Ringwald             break;
1160a42798c3SMatthias Ringwald         // only sent if ENABLE_SCO_OVER_HCI and manufacturer is Broadcom
1161a42798c3SMatthias Ringwald         case HCI_INIT_BCM_WRITE_SCO_PCM_INT:
1162a42798c3SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT;
1163a42798c3SMatthias Ringwald             log_info("BCM: Route SCO data via HCI transport");
1164a42798c3SMatthias Ringwald             hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0);
1165a42798c3SMatthias Ringwald             break;
1166a42798c3SMatthias Ringwald 
116735454696SMatthias Ringwald #endif
1168a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
116974b323a9SMatthias Ringwald         // LE INIT
117074b323a9SMatthias Ringwald         case HCI_INIT_LE_READ_BUFFER_SIZE:
117174b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE;
11720305bdeaSMatthias Ringwald             hci_send_cmd(&hci_le_read_buffer_size);
117374b323a9SMatthias Ringwald             break;
117474b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LE_HOST_SUPPORTED:
117574b323a9SMatthias Ringwald             // LE Supported Host = 1, Simultaneous Host = 0
117674b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED;
11770305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
117874b323a9SMatthias Ringwald             break;
1179b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
11803b6d4121SMatthias Ringwald         case HCI_INIT_READ_WHITE_LIST_SIZE:
11813b6d4121SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE;
11823b6d4121SMatthias Ringwald             hci_send_cmd(&hci_le_read_white_list_size);
11833b6d4121SMatthias Ringwald             break;
118474b323a9SMatthias Ringwald         case HCI_INIT_LE_SET_SCAN_PARAMETERS:
1185b95a5a35SMatthias Ringwald             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, own address type, accept all advs
118674b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS;
1187b95a5a35SMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, hci_stack->le_own_addr_type, 0);
118874b323a9SMatthias Ringwald             break;
118974b323a9SMatthias Ringwald #endif
1190b95a5a35SMatthias Ringwald #endif
119174b323a9SMatthias Ringwald         default:
119274b323a9SMatthias Ringwald             return;
119374b323a9SMatthias Ringwald     }
119455975f88SMatthias Ringwald }
119555975f88SMatthias Ringwald 
1196a650ba4dSMatthias Ringwald static void hci_init_done(void){
1197a650ba4dSMatthias Ringwald     // done. tell the app
1198a650ba4dSMatthias Ringwald     log_info("hci_init_done -> HCI_STATE_WORKING");
1199a650ba4dSMatthias Ringwald     hci_stack->state = HCI_STATE_WORKING;
1200a650ba4dSMatthias Ringwald     hci_emit_state();
1201a650ba4dSMatthias Ringwald     hci_run();
1202a650ba4dSMatthias Ringwald }
1203a650ba4dSMatthias Ringwald 
12046155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
12059ec2630cSMatthias Ringwald     UNUSED(size);
12069ec2630cSMatthias Ringwald 
1207a2481739S[email protected]     uint8_t command_completed = 0;
1208c4633093SMatthias Ringwald 
12090e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){
1210f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
12116155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
1212a2481739S[email protected]             command_completed = 1;
1213148ca237SMatthias Ringwald             log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate);
12146155b3d3S[email protected]         } else {
1215d58dd308SMatthias Ringwald             log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate);
12166155b3d3S[email protected]         }
12176155b3d3S[email protected]     }
12180f97eae7SMatthias Ringwald 
12190e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){
12206155b3d3S[email protected]         uint8_t  status = packet[2];
1221f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,4);
12226155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
12236155b3d3S[email protected]             if (status){
1224a2481739S[email protected]                 command_completed = 1;
1225148ca237SMatthias Ringwald                 log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate);
12266155b3d3S[email protected]             } else {
12276155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
12286155b3d3S[email protected]             }
12296155b3d3S[email protected]         } else {
1230148ca237SMatthias Ringwald             log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
12316155b3d3S[email protected]         }
12326155b3d3S[email protected]     }
12330f97eae7SMatthias Ringwald 
123406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
123506b9e820SMatthias Ringwald 
1236e47e68c7SMatthias Ringwald     // Vendor == CSR
12370e2df43fSMatthias Ringwald     if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){
1238e47e68c7SMatthias Ringwald         // TODO: track actual command
1239e47e68c7SMatthias Ringwald         command_completed = 1;
1240e47e68c7SMatthias Ringwald     }
1241a2481739S[email protected] 
12424e9daa6fSMatthias Ringwald     // Vendor == Toshiba
12430e2df43fSMatthias Ringwald     if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){
12444e9daa6fSMatthias Ringwald         // TODO: track actual command
12454e9daa6fSMatthias Ringwald         command_completed = 1;
12464e9daa6fSMatthias Ringwald     }
12474e9daa6fSMatthias Ringwald 
12480f97eae7SMatthias Ringwald     // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661:
12490f97eae7SMatthias Ringwald     // Command complete for HCI Reset arrives after we've resent the HCI Reset command
12500f97eae7SMatthias Ringwald     //
12510f97eae7SMatthias Ringwald     // HCI Reset
12520f97eae7SMatthias Ringwald     // Timeout 100 ms
12530f97eae7SMatthias Ringwald     // HCI Reset
12540f97eae7SMatthias Ringwald     // Command Complete Reset
12550f97eae7SMatthias Ringwald     // HCI Read Local Version Information
12560f97eae7SMatthias Ringwald     // Command Complete Reset - but we expected Command Complete Read Local Version Information
12570f97eae7SMatthias Ringwald     // hang...
12580f97eae7SMatthias Ringwald     //
12590f97eae7SMatthias Ringwald     // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend
12600f97eae7SMatthias Ringwald     if (!command_completed
12610e2df43fSMatthias Ringwald             && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE
12620f97eae7SMatthias Ringwald             && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){
12630f97eae7SMatthias Ringwald 
1264f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
12650f97eae7SMatthias Ringwald         if (opcode == hci_reset.opcode){
12660f97eae7SMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION;
12670f97eae7SMatthias Ringwald             return;
12680f97eae7SMatthias Ringwald         }
12690f97eae7SMatthias Ringwald     }
12700f97eae7SMatthias Ringwald 
12719f007422SMatthias Ringwald     // CSR & H5
12729f007422SMatthias Ringwald     // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend
12739f007422SMatthias Ringwald     if (!command_completed
12749f007422SMatthias Ringwald             && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE
12759f007422SMatthias Ringwald             && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){
12769f007422SMatthias Ringwald 
12779f007422SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
12789f007422SMatthias Ringwald         if (opcode == hci_reset.opcode){
12799f007422SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS;
12809f007422SMatthias Ringwald             return;
12819f007422SMatthias Ringwald         }
12829f007422SMatthias Ringwald     }
12839f007422SMatthias Ringwald 
12849f007422SMatthias Ringwald     // on CSR with BCSP/H5, the reset resend timeout leads to substate == HCI_INIT_SEND_RESET or HCI_INIT_SEND_RESET_CSR_WARM_BOOT
12859f007422SMatthias Ringwald     // fix: Correct substate and behave as command below
12869f007422SMatthias Ringwald     if (command_completed){
12879f007422SMatthias Ringwald         switch (hci_stack->substate){
12889f007422SMatthias Ringwald             case HCI_INIT_SEND_RESET:
12899f007422SMatthias Ringwald                 hci_stack->substate = HCI_INIT_W4_SEND_RESET;
12909f007422SMatthias Ringwald                 break;
12919f007422SMatthias Ringwald             case HCI_INIT_SEND_RESET_CSR_WARM_BOOT:
12929f007422SMatthias Ringwald                 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
12939f007422SMatthias Ringwald                 break;
12949f007422SMatthias Ringwald             default:
12959f007422SMatthias Ringwald                 break;
12969f007422SMatthias Ringwald         }
12979f007422SMatthias Ringwald     }
12980f97eae7SMatthias Ringwald 
129906b9e820SMatthias Ringwald #endif
13000f97eae7SMatthias Ringwald 
1301a2481739S[email protected]     if (!command_completed) return;
1302a2481739S[email protected] 
130306b9e820SMatthias Ringwald     int need_baud_change = 0;
130406b9e820SMatthias Ringwald     int need_addr_change = 0;
130506b9e820SMatthias Ringwald 
130606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
130706b9e820SMatthias Ringwald     need_baud_change = hci_stack->config
13083fb36a29SMatthias Ringwald                         && hci_stack->chipset
13093fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_baudrate_command
1310db8bc6ffSMatthias Ringwald                         && hci_stack->hci_transport->set_baudrate
13119796ebeaSMatthias Ringwald                         && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
1312db8bc6ffSMatthias Ringwald 
131306b9e820SMatthias Ringwald     need_addr_change = hci_stack->custom_bd_addr_set
13143fb36a29SMatthias Ringwald                         && hci_stack->chipset
13153fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_bd_addr_command;
131606b9e820SMatthias Ringwald #endif
1317a80162e9SMatthias Ringwald 
13185c363727SMatthias Ringwald     switch(hci_stack->substate){
131906b9e820SMatthias Ringwald 
132006b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
13219f007422SMatthias Ringwald         case HCI_INIT_SEND_RESET:
1322d58dd308SMatthias Ringwald             // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET
13239f007422SMatthias Ringwald             // fix: just correct substate and behave as command below
13249f007422SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET;
13259f007422SMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
13269f007422SMatthias Ringwald             break;
132774b323a9SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
1328528a4a3bSMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
132976fcb19bSMatthias Ringwald             break;
1330e90bae01SMatthias Ringwald         case HCI_INIT_W4_SEND_READ_LOCAL_NAME:
1331e90bae01SMatthias Ringwald             log_info("Received local name, need baud change %d", need_baud_change);
13322f48d920SMatthias Ringwald             if (need_baud_change){
13332f48d920SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
13342f48d920SMatthias Ringwald                 return;
13352f48d920SMatthias Ringwald             }
133653860077SMatthias Ringwald             // skip baud change
133774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
133874b323a9SMatthias Ringwald             return;
1339a80162e9SMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
13404696bddbSMatthias Ringwald             // for STLC2500D, baud rate change already happened.
1341fab26ab3SMatthias Ringwald             // for others, baud rate gets changed now
13427224be7eSMatthias Ringwald             if ((hci_stack->manufacturer != COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){
134396b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1344cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate);
1345fab26ab3SMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
13464696bddbSMatthias Ringwald             }
134774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
134874b323a9SMatthias Ringwald             return;
1349a80162e9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
1350528a4a3bSMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
1351a80162e9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1352a80162e9SMatthias Ringwald             return;
135374b323a9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT:
135474b323a9SMatthias Ringwald             // repeat custom init
135574b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
135674b323a9SMatthias Ringwald             return;
135706b9e820SMatthias Ringwald #else
135806b9e820SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
135906b9e820SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS;
136006b9e820SMatthias Ringwald             return ;
136106b9e820SMatthias Ringwald #endif
136206b9e820SMatthias Ringwald 
1363a828a756SMatthias Ringwald         case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS:
1364c6d1dbedSMatthias Ringwald             if (need_baud_change && hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
1365eb3a5314SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM;
1366eb3a5314SMatthias Ringwald                 return;
1367eb3a5314SMatthias Ringwald             }
136853860077SMatthias Ringwald             if (need_addr_change){
136953860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
137053860077SMatthias Ringwald                 return;
137153860077SMatthias Ringwald             }
137253860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
137353860077SMatthias Ringwald             return;
137406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
13757224be7eSMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM:
13767224be7eSMatthias Ringwald             if (need_baud_change){
137796b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1378cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate);
1379fab26ab3SMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
13807224be7eSMatthias Ringwald             }
1381eb3a5314SMatthias Ringwald             if (need_addr_change){
1382eb3a5314SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
1383eb3a5314SMatthias Ringwald                 return;
1384eb3a5314SMatthias Ringwald             }
1385eb3a5314SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
1386eb3a5314SMatthias Ringwald             return;
138753860077SMatthias Ringwald         case HCI_INIT_W4_SET_BD_ADDR:
138853860077SMatthias Ringwald             // for STLC2500D, bd addr change only gets active after sending reset command
1389c6d1dbedSMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
139053860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT;
139153860077SMatthias Ringwald                 return;
139253860077SMatthias Ringwald             }
139353860077SMatthias Ringwald             // skipping st warm boot
139453860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
139553860077SMatthias Ringwald             return;
139653860077SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT:
139753860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
139853860077SMatthias Ringwald             return;
139906b9e820SMatthias Ringwald #endif
140053860077SMatthias Ringwald         case HCI_INIT_W4_READ_BD_ADDR:
140153860077SMatthias Ringwald             // only read buffer size if supported
140253860077SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x01) {
140353860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE;
140453860077SMatthias Ringwald                 return;
140553860077SMatthias Ringwald             }
140653860077SMatthias Ringwald             // skipping read buffer size
140753860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES;
1408a828a756SMatthias Ringwald             return;
140974b323a9SMatthias Ringwald         case HCI_INIT_W4_SET_EVENT_MASK:
14106155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
14116155b3d3S[email protected]             if (!hci_classic_supported()){
14122c68f164SMatthias Ringwald #ifdef ENABLE_BLE
14136155b3d3S[email protected]                 if (hci_le_supported()){
141474b323a9SMatthias Ringwald                     hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command
141574b323a9SMatthias Ringwald                     return;
14162c68f164SMatthias Ringwald                 }
14172c68f164SMatthias Ringwald #endif
14186155b3d3S[email protected]                 log_error("Neither BR/EDR nor LE supported");
1419a650ba4dSMatthias Ringwald                 hci_init_done();
142074b323a9SMatthias Ringwald                 return;
14216155b3d3S[email protected]             }
142215a95bd5SMatthias Ringwald             if (!gap_ssp_supported()){
14235c363727SMatthias Ringwald                 hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT;
142474b323a9SMatthias Ringwald                 return;
14256155b3d3S[email protected]             }
14266155b3d3S[email protected]             break;
1427903ea03aSMatthias Ringwald #ifdef ENABLE_BLE
1428a828a756SMatthias Ringwald         case HCI_INIT_W4_LE_READ_BUFFER_SIZE:
1429a828a756SMatthias Ringwald             // skip write le host if not supported (e.g. on LE only EM9301)
1430a828a756SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x02) break;
1431903ea03aSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
1432903ea03aSMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE;
1433903ea03aSMatthias Ringwald #else
1434903ea03aSMatthias Ringwald             hci_init_done();
1435903ea03aSMatthias Ringwald #endif
1436a828a756SMatthias Ringwald             return;
1437903ea03aSMatthias Ringwald #endif
1438ff00ed1cSMatthias Ringwald         case HCI_INIT_W4_WRITE_LOCAL_NAME:
1439ff00ed1cSMatthias Ringwald             // skip write eir data if no eir data set
1440ff00ed1cSMatthias Ringwald             if (hci_stack->eir_data) break;
1441ff00ed1cSMatthias Ringwald             hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE;
1442ff00ed1cSMatthias Ringwald             return;
1443729ed62eSMatthias Ringwald 
144471c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI
1445729ed62eSMatthias Ringwald         case HCI_INIT_W4_WRITE_SCAN_ENABLE:
14463905afbfSMatthias Ringwald             // skip write synchronous flow control if not supported
14473905afbfSMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x04) break;
14483905afbfSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE;
14493905afbfSMatthias Ringwald             // explicit fall through to reduce repetitions
14503905afbfSMatthias Ringwald 
1451729ed62eSMatthias Ringwald         case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE:
14523905afbfSMatthias Ringwald             // skip write default erroneous data reporting if not supported
14533905afbfSMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x08) break;
14543905afbfSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING;
14553905afbfSMatthias Ringwald             // explicit fall through to reduce repetitions
14563905afbfSMatthias Ringwald 
1457f064e0bbSMatthias Ringwald         case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING:
1458a42798c3SMatthias Ringwald             // skip bcm set sco pcm config on non-Broadcom chipsets
1459a42798c3SMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION) break;
1460a42798c3SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT;
1461a42798c3SMatthias Ringwald             // explicit fall through to reduce repetitions
1462a42798c3SMatthias Ringwald 
1463a42798c3SMatthias Ringwald         case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT:
1464729ed62eSMatthias Ringwald             if (!hci_le_supported()){
1465729ed62eSMatthias Ringwald                 // SKIP LE init for Classic only configuration
1466a650ba4dSMatthias Ringwald                 hci_init_done();
1467729ed62eSMatthias Ringwald                 return;
1468729ed62eSMatthias Ringwald             }
1469729ed62eSMatthias Ringwald             break;
1470729ed62eSMatthias Ringwald #else
147174b323a9SMatthias Ringwald         case HCI_INIT_W4_WRITE_SCAN_ENABLE:
14726155b3d3S[email protected]             if (!hci_le_supported()){
14736155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
1474a650ba4dSMatthias Ringwald                 hci_init_done();
147574b323a9SMatthias Ringwald                 return;
14766155b3d3S[email protected]             }
1477729ed62eSMatthias Ringwald #endif
1478729ed62eSMatthias Ringwald             break;
1479a650ba4dSMatthias Ringwald         // Response to command before init done state -> init done
1480a650ba4dSMatthias Ringwald         case (HCI_INIT_DONE-1):
1481a650ba4dSMatthias Ringwald             hci_init_done();
1482a650ba4dSMatthias Ringwald             return;
1483a650ba4dSMatthias Ringwald 
14846155b3d3S[email protected]         default:
148574b323a9SMatthias Ringwald             break;
14866155b3d3S[email protected]     }
148755975f88SMatthias Ringwald     hci_initializing_next_state();
14886155b3d3S[email protected] }
14896155b3d3S[email protected] 
149016833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1491e76a89eeS[email protected] 
1492e76a89eeS[email protected]     uint16_t event_length = packet[1];
1493e76a89eeS[email protected] 
1494e76a89eeS[email protected]     // assert packet is complete
1495e76a89eeS[email protected]     if (size != event_length + 2){
1496f04a0c31SMatthias Ringwald         log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2);
1497e76a89eeS[email protected]         return;
1498e76a89eeS[email protected]     }
1499e76a89eeS[email protected] 
15001281a47eSmatthias.ringwald     bd_addr_t addr;
150196a45072S[email protected]     bd_addr_type_t addr_type;
15022d00edd4Smatthias.ringwald     uint8_t link_type;
1503fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
15041f7b95a1Smatthias.ringwald     hci_connection_t * conn;
150556cf178bSmatthias.ringwald     int i;
150622909952Smatthias.ringwald 
150735454696SMatthias Ringwald     // warnings
150835454696SMatthias Ringwald     (void) link_type;
150935454696SMatthias Ringwald 
15100e2df43fSMatthias Ringwald     // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet));
15115909f7f2Smatthias.ringwald 
15120e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
151322909952Smatthias.ringwald 
15146772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
15156bef4003SMatthias Ringwald             // get num cmd packets - limit to 1 to reduce complexity
15166bef4003SMatthias Ringwald             hci_stack->num_cmd_packets = packet[2] ? 1 : 0;
15177ec5eeaaSmatthias.ringwald 
15189e263bd7SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
151906b9e820SMatthias Ringwald                 if (packet[5]) break;
15209e263bd7SMatthias Ringwald                 // terminate, name 248 chars
15219e263bd7SMatthias Ringwald                 packet[6+248] = 0;
15229e263bd7SMatthias Ringwald                 log_info("local name: %s", &packet[6]);
15239e263bd7SMatthias Ringwald             }
1524073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){
1525e2edc0c3Smatthias.ringwald                 // from offset 5
1526e2edc0c3Smatthias.ringwald                 // status
15271d279b20Smatthias.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"
1528f8fbdce0SMatthias Ringwald                 hci_stack->acl_data_packet_length = little_endian_read_16(packet, 6);
1529a8b12447S[email protected]                 hci_stack->sco_data_packet_length = packet[8];
1530f8fbdce0SMatthias Ringwald                 hci_stack->acl_packets_total_num  = little_endian_read_16(packet, 9);
1531f8fbdce0SMatthias Ringwald                 hci_stack->sco_packets_total_num  = little_endian_read_16(packet, 11);
1532a8b12447S[email protected] 
15333a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
1534a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
15353a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
15363a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
15378f8108aaSmatthias.ringwald                     }
15381a06f663S[email protected]                     log_info("hci_read_buffer_size: acl used size %u, count %u / sco size %u, count %u",
15391a06f663S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num,
15401a06f663S[email protected]                              hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num);
1541e2edc0c3Smatthias.ringwald                 }
154256cf178bSmatthias.ringwald             }
1543a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
1544073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){
1545f8fbdce0SMatthias Ringwald                 hci_stack->le_data_packets_length = little_endian_read_16(packet, 6);
1546ee303eddS[email protected]                 hci_stack->le_acl_packets_total_num  = packet[8];
15476c26b087S[email protected]                     // determine usable ACL payload size
15486c26b087S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){
15496c26b087S[email protected]                         hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE;
15506c26b087S[email protected]                     }
15519da54300S[email protected]                 log_info("hci_le_read_buffer_size: size %u, count %u", hci_stack->le_data_packets_length, hci_stack->le_acl_packets_total_num);
155265a46ef3S[email protected]             }
1553d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
1554073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){
1555e691bb38SMatthias Ringwald                 hci_stack->le_whitelist_capacity = packet[6];
155615d0a15bSMatthias Ringwald                 log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity);
15573b6d4121SMatthias Ringwald             }
155865a46ef3S[email protected] #endif
1559d70217a2SMatthias Ringwald #endif
1560073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) {
1561724d70a2SMatthias Ringwald                 reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1],
1562724d70a2SMatthias Ringwald 				hci_stack->local_bd_addr);
15639da54300S[email protected]                 log_info("Local Address, Status: 0x%02x: Addr: %s",
15643a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
156533373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC
15661624665aSMatthias Ringwald                 if (hci_stack->link_key_db){
15671624665aSMatthias Ringwald                     hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr);
15681624665aSMatthias Ringwald                 }
156933373e40SMatthias Ringwald #endif
1570188981d2Smatthias.ringwald             }
157135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1572073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){
15733a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
1574381fbed8Smatthias.ringwald             }
157535454696SMatthias Ringwald #endif
157635454696SMatthias Ringwald 
157765389bfcS[email protected]             // Note: HCI init checks
1578073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){
15793a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
158065389bfcS[email protected] 
158135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1582a5a23fc2S[email protected]                 // determine usable ACL packet types based on host buffer size and supported features
1583a5a23fc2S[email protected]                 hci_stack->packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(HCI_ACL_PAYLOAD_SIZE, &hci_stack->local_supported_features[0]);
15848b96126aSMatthias Ringwald                 log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported());
158535454696SMatthias Ringwald #endif
1586f5d8d141S[email protected]                 // Classic/LE
1587f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
1588559e517eS[email protected]             }
1589073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
1590f8fbdce0SMatthias Ringwald                 // hci_stack->hci_version    = little_endian_read_16(packet, 4);
1591f8fbdce0SMatthias Ringwald                 // hci_stack->hci_revision   = little_endian_read_16(packet, 6);
1592f8fbdce0SMatthias Ringwald                 // hci_stack->lmp_version    = little_endian_read_16(packet, 8);
1593f8fbdce0SMatthias Ringwald                 hci_stack->manufacturer   = little_endian_read_16(packet, 10);
1594f8fbdce0SMatthias Ringwald                 // hci_stack->lmp_subversion = little_endian_read_16(packet, 12);
15954696bddbSMatthias Ringwald                 log_info("Manufacturer: 0x%04x", hci_stack->manufacturer);
15964696bddbSMatthias Ringwald             }
1597073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){
1598a828a756SMatthias Ringwald                 hci_stack->local_supported_commands[0] =
15993905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 |  // bit 0 = Octet 14, bit 7
16003905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 |  // bit 1 = Octet 24, bit 6
16013905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 |  // bit 2 = Octet 10, bit 4
16023905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08);        // bit 3 = Octet 18, bit 3
16033905afbfSMatthias Ringwald                     log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]);
1604a828a756SMatthias Ringwald             }
1605e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC
1606073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){
16075b9b590fSMatthias Ringwald                 if (packet[5] == 0){
16085b9b590fSMatthias Ringwald                     hci_stack->synchronous_flow_control_enabled = 1;
16095b9b590fSMatthias Ringwald                 }
16105b9b590fSMatthias Ringwald             }
1611e8c8828eSMatthias Ringwald #endif
161256cf178bSmatthias.ringwald             break;
161356cf178bSmatthias.ringwald 
16147ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
16156bef4003SMatthias Ringwald             // get num cmd packets - limit to 1 to reduce complexity
16166bef4003SMatthias Ringwald             hci_stack->num_cmd_packets = packet[3] ? 1 : 0;
16177ec5eeaaSmatthias.ringwald             break;
16187ec5eeaaSmatthias.ringwald 
16192e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
16202e440c8aS[email protected]             int offset = 3;
162156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
1622f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, offset);
16232e440c8aS[email protected]                 offset += 2;
1624f8fbdce0SMatthias Ringwald                 uint16_t num_packets = little_endian_read_16(packet, offset);
16252e440c8aS[email protected]                 offset += 2;
16262e440c8aS[email protected] 
16275061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
162856cf178bSmatthias.ringwald                 if (!conn){
16299da54300S[email protected]                     log_error("hci_number_completed_packet lists unused con handle %u", handle);
163056cf178bSmatthias.ringwald                     continue;
163156cf178bSmatthias.ringwald                 }
163223bed257S[email protected] 
1633e35edcc1S[email protected]                 if (conn->address_type == BD_ADDR_TYPE_SCO){
163435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1635e35edcc1S[email protected]                     if (conn->num_sco_packets_sent >= num_packets){
1636e35edcc1S[email protected]                         conn->num_sco_packets_sent -= num_packets;
1637e35edcc1S[email protected]                     } else {
1638e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more sco slots freed then sent.");
1639e35edcc1S[email protected]                         conn->num_sco_packets_sent = 0;
1640e35edcc1S[email protected]                     }
1641701e3307SMatthias Ringwald                     hci_notify_if_sco_can_send_now();
164235454696SMatthias Ringwald #endif
1643e35edcc1S[email protected]                 } else {
164423bed257S[email protected]                     if (conn->num_acl_packets_sent >= num_packets){
164556cf178bSmatthias.ringwald                         conn->num_acl_packets_sent -= num_packets;
164623bed257S[email protected]                     } else {
1647e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more acl slots freed then sent.");
164823bed257S[email protected]                         conn->num_acl_packets_sent = 0;
164923bed257S[email protected]                     }
1650e35edcc1S[email protected]                 }
16519da54300S[email protected]                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent);
165256cf178bSmatthias.ringwald             }
16536772a24cSmatthias.ringwald             break;
16542e440c8aS[email protected]         }
165535454696SMatthias Ringwald 
165635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
16571f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
1658724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[2], addr);
165937eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
16602d00edd4Smatthias.ringwald             link_type = packet[11];
16619da54300S[email protected]             log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type);
1662e35edcc1S[email protected]             addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO;
16632e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
16641f7b95a1Smatthias.ringwald             if (!conn) {
16655293c072S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, addr_type);
16661f7b95a1Smatthias.ringwald             }
1667ce4c8fabSmatthias.ringwald             if (!conn) {
1668ce4c8fabSmatthias.ringwald                 // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
16693a9fb326S[email protected]                 hci_stack->decline_reason = 0x0d;
1670058e3d6bSMatthias Ringwald                 bd_addr_copy(hci_stack->decline_addr, addr);
1671ce4c8fabSmatthias.ringwald                 break;
1672ce4c8fabSmatthias.ringwald             }
16735cf766e8SMatthias Ringwald             conn->role  = HCI_ROLE_SLAVE;
167432ab9390Smatthias.ringwald             conn->state = RECEIVED_CONNECTION_REQUEST;
1675f3a16b9aSMatthias Ringwald             // store info about eSCO
1676f3a16b9aSMatthias Ringwald             if (link_type == 0x02){
1677f3a16b9aSMatthias Ringwald                 conn->remote_supported_feature_eSCO = 1;
1678f3a16b9aSMatthias Ringwald             }
167932ab9390Smatthias.ringwald             hci_run();
16801f7b95a1Smatthias.ringwald             break;
16811f7b95a1Smatthias.ringwald 
16826772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1683fe1ed1b8Smatthias.ringwald             // Connection management
1684724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], addr);
16859da54300S[email protected]             log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
168696a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
16872e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
1688fe1ed1b8Smatthias.ringwald             if (conn) {
1689b448a0e7Smatthias.ringwald                 if (!packet[2]){
1690c8e4258aSmatthias.ringwald                     conn->state = OPEN;
1691f8fbdce0SMatthias Ringwald                     conn->con_handle = little_endian_read_16(packet, 3);
1692ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
1693ee091cf1Smatthias.ringwald 
1694c785ef68Smatthias.ringwald                     // restart timer
1695528a4a3bSMatthias Ringwald                     btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1696528a4a3bSMatthias Ringwald                     btstack_run_loop_add_timer(&conn->timeout);
1697c785ef68Smatthias.ringwald 
16989da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
169943bfb1bdSmatthias.ringwald 
170043bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
1701b448a0e7Smatthias.ringwald                 } else {
17021bd5283dS[email protected]                     int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED;
17031bd5283dS[email protected]                     uint8_t status = packet[2];
17041bd5283dS[email protected]                     bd_addr_t bd_address;
17051bd5283dS[email protected]                     memcpy(&bd_address, conn->address, 6);
1706ad83dc6aS[email protected] 
1707b448a0e7Smatthias.ringwald                     // connection failed, remove entry
1708665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
1709a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
1710c12e46e7Smatthias.ringwald 
17111bd5283dS[email protected]                     // notify client if dedicated bonding
17121bd5283dS[email protected]                     if (notify_dedicated_bonding_failed){
17131bd5283dS[email protected]                         log_info("hci notify_dedicated_bonding_failed");
17141bd5283dS[email protected]                         hci_emit_dedicated_bonding_result(bd_address, status);
17151bd5283dS[email protected]                     }
17161bd5283dS[email protected] 
1717c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
1718c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
171915a95bd5SMatthias Ringwald                         gap_drop_link_key_for_bd_addr(addr);
1720c12e46e7Smatthias.ringwald                     }
1721fe1ed1b8Smatthias.ringwald                 }
1722fe1ed1b8Smatthias.ringwald             }
17236772a24cSmatthias.ringwald             break;
1724fe1ed1b8Smatthias.ringwald 
172544d0e3d5S[email protected]         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:
1726724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], addr);
172744d0e3d5S[email protected]             log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
17281a06f663S[email protected]             if (packet[2]){
172944d0e3d5S[email protected]                 // connection failed
173044d0e3d5S[email protected]                 break;
173144d0e3d5S[email protected]             }
17322e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1733e35edcc1S[email protected]             if (!conn) {
1734e35edcc1S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1735e35edcc1S[email protected]             }
1736e35edcc1S[email protected]             if (!conn) {
1737e35edcc1S[email protected]                 break;
1738e35edcc1S[email protected]             }
17391a06f663S[email protected]             conn->state = OPEN;
1740f8fbdce0SMatthias Ringwald             conn->con_handle = little_endian_read_16(packet, 3);
174144d0e3d5S[email protected]             break;
174244d0e3d5S[email protected] 
1743afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1744f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1745afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
1746afd4e962S[email protected]             if (!conn) break;
1747afd4e962S[email protected]             if (!packet[2]){
1748afd4e962S[email protected]                 uint8_t * features = &packet[5];
1749afd4e962S[email protected]                 if (features[6] & (1 << 3)){
1750afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
1751afd4e962S[email protected]                 }
175298a2fd1cSMatthias Ringwald                 if (features[3] & (1<<7)){
175398a2fd1cSMatthias Ringwald                     conn->remote_supported_feature_eSCO = 1;
175498a2fd1cSMatthias Ringwald                 }
1755afd4e962S[email protected]             }
1756afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
175798a2fd1cSMatthias Ringwald             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO);
1758ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1759ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1760ad83dc6aS[email protected]             }
1761afd4e962S[email protected]             break;
1762afd4e962S[email protected] 
17637fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
17649da54300S[email protected]             log_info("HCI_EVENT_LINK_KEY_REQUEST");
17657fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
176674d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
1767a98592bcSMatthias Ringwald             if (hci_stack->bondable && !hci_stack->link_key_db) break;
176832ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
176964472d52Smatthias.ringwald             hci_run();
1770d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
177129d53098Smatthias.ringwald             return;
17727fde4af9Smatthias.ringwald 
17739ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
1774724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[2], addr);
17752e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
17769ab95c90S[email protected]             if (!conn) break;
17779ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
17787bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
17799ab95c90S[email protected]             // Change Connection Encryption keeps link key type
17809ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
17819ab95c90S[email protected]                 conn->link_key_type = link_key_type;
17829ab95c90S[email protected]             }
178355597469SMatthias Ringwald             gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type);
178429d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
17857fde4af9Smatthias.ringwald             break;
17869ab95c90S[email protected]         }
17877fde4af9Smatthias.ringwald 
17887fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
17896724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
17904c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
17913a9fb326S[email protected]             if (!hci_stack->bondable){
1792899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
1793f8fb5f6eS[email protected]                 hci_run();
1794f8fb5f6eS[email protected]                 return;
17954c57c146S[email protected]             }
1796d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
1797a98592bcSMatthias Ringwald             if (!hci_stack->link_key_db) break;
1798a6ef64baSMilanka Ringwald             hci_event_pin_code_request_get_bd_addr(packet, addr);
1799a98592bcSMatthias Ringwald             hci_stack->link_key_db->delete_link_key(addr);
18007fde4af9Smatthias.ringwald             break;
18017fde4af9Smatthias.ringwald 
18021d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
18031d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
1804dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
1805dbe1a790S[email protected]             break;
1806dbe1a790S[email protected] 
1807dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
18086724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
18093a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1810dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
1811dbe1a790S[email protected]             break;
1812dbe1a790S[email protected] 
1813dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
18146724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
18153a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1816dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
18171d6b20aeS[email protected]             break;
181835454696SMatthias Ringwald #endif
18191d6b20aeS[email protected] 
1820f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1821f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1822f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1823f0944df2S[email protected]             if (!conn) break;
1824ad83dc6aS[email protected]             if (packet[2] == 0) {
1825f0944df2S[email protected]                 if (packet[5]){
1826f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1827f0944df2S[email protected]                 } else {
1828536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1829f0944df2S[email protected]                 }
1830ad83dc6aS[email protected]             }
183135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1832a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
183335454696SMatthias Ringwald #endif
1834f0944df2S[email protected]             break;
1835f0944df2S[email protected] 
183635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
18371eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
1838f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
18391eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
18401eb2563eS[email protected]             if (!conn) break;
1841ad83dc6aS[email protected] 
1842ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1843ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1844ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1845ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
18461bd5283dS[email protected]                 conn->bonding_status = packet[2];
1847ad83dc6aS[email protected]                 break;
1848ad83dc6aS[email protected]             }
1849ad83dc6aS[email protected] 
1850b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
18511eb2563eS[email protected]                 // link key sufficient for requested security
18521eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1853ad83dc6aS[email protected]                 break;
1854ad83dc6aS[email protected]             }
18551eb2563eS[email protected]             // not enough
18561eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
18571eb2563eS[email protected]             break;
185835454696SMatthias Ringwald #endif
185934d2123cS[email protected] 
186086805605S[email protected]         // HCI_EVENT_DISCONNECTION_COMPLETE
1861ccda6e14S[email protected]         // has been split, to first notify stack before shutting connection down
1862ccda6e14S[email protected]         // see end of function, too.
1863a4f30ec0S[email protected]         case HCI_EVENT_DISCONNECTION_COMPLETE:
1864a4f30ec0S[email protected]             if (packet[2]) break;   // status != 0
1865f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1866c6a37cfdSMatthias Ringwald             // drop outgoing ACL fragments if it is for closed connection
1867c6a37cfdSMatthias Ringwald             if (hci_stack->acl_fragmentation_total_size > 0) {
1868c6a37cfdSMatthias Ringwald                 if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){
1869c6a37cfdSMatthias Ringwald                     log_info("hci: drop fragmented ACL data for closed connection");
1870c6a37cfdSMatthias Ringwald                      hci_stack->acl_fragmentation_total_size = 0;
1871c6a37cfdSMatthias Ringwald                      hci_stack->acl_fragmentation_pos = 0;
1872c6a37cfdSMatthias Ringwald                 }
1873c6a37cfdSMatthias Ringwald             }
187435454696SMatthias Ringwald 
18759a2e4658SMatthias Ringwald             // re-enable advertisements for le connections if active
1876c6a37cfdSMatthias Ringwald             conn = hci_connection_for_handle(handle);
1877c6a37cfdSMatthias Ringwald             if (!conn) break;
187835454696SMatthias Ringwald #ifdef ENABLE_BLE
1879d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL
18809a2e4658SMatthias Ringwald             if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){
18819a2e4658SMatthias Ringwald                 hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
18829a2e4658SMatthias Ringwald             }
188335454696SMatthias Ringwald #endif
1884d70217a2SMatthias Ringwald #endif
1885ccda6e14S[email protected]             conn->state = RECEIVED_DISCONNECTION_COMPLETE;
1886ccda6e14S[email protected]             break;
18876772a24cSmatthias.ringwald 
1888c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
1889313e5f9cSMatthias Ringwald             log_error("Hardware Error: 0x%02x", packet[2]);
1890d23838ecSMatthias Ringwald             if (hci_stack->hardware_error_callback){
1891c2e1fa60SMatthias Ringwald                 (*hci_stack->hardware_error_callback)(packet[2]);
18927586ee35S[email protected]             } else {
18937586ee35S[email protected]                 // if no special requests, just reboot stack
18947586ee35S[email protected]                 hci_power_control_off();
18957586ee35S[email protected]                 hci_power_control_on();
1896c68bdf90Smatthias.ringwald             }
1897c68bdf90Smatthias.ringwald             break;
1898c68bdf90Smatthias.ringwald 
18990e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC
19005cf766e8SMatthias Ringwald         case HCI_EVENT_ROLE_CHANGE:
19015cf766e8SMatthias Ringwald             if (packet[2]) break;   // status != 0
1902f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
19035cf766e8SMatthias Ringwald             conn = hci_connection_for_handle(handle);
19045cf766e8SMatthias Ringwald             if (!conn) break;       // no conn
19055cf766e8SMatthias Ringwald             conn->role = packet[9];
19065cf766e8SMatthias Ringwald             break;
19070e6f3837SMatthias Ringwald #endif
19085cf766e8SMatthias Ringwald 
190963fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
1910d051460cS[email protected]             // release packet buffer only for asynchronous transport and if there are not further fragements
19114fa24b5fS[email protected]             if (hci_transport_synchronous()) {
191263fa3374SMatthias Ringwald                 log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT");
19134fa24b5fS[email protected]                 return; // instead of break: to avoid re-entering hci_run()
19144fa24b5fS[email protected]             }
1915d051460cS[email protected]             if (hci_stack->acl_fragmentation_total_size) break;
1916d051460cS[email protected]             hci_release_packet_buffer();
1917701e3307SMatthias Ringwald 
191863fa3374SMatthias Ringwald             // L2CAP receives this event via the hci_emit_event below
191963fa3374SMatthias Ringwald 
192035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
192163fa3374SMatthias Ringwald             // For SCO, we do the can_send_now_check here
1922701e3307SMatthias Ringwald             hci_notify_if_sco_can_send_now();
192335454696SMatthias Ringwald #endif
19246b4af23dS[email protected]             break;
19256b4af23dS[email protected] 
192635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
192763fa3374SMatthias Ringwald         case HCI_EVENT_SCO_CAN_SEND_NOW:
192863fa3374SMatthias Ringwald             // For SCO, we do the can_send_now_check here
192963fa3374SMatthias Ringwald             hci_notify_if_sco_can_send_now();
193063fa3374SMatthias Ringwald             return;
193135454696SMatthias Ringwald #endif
193263fa3374SMatthias Ringwald 
1933a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
19345909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
19355909f7f2Smatthias.ringwald             switch (packet[2]){
1936d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
193757c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
19384f4e0224SMatthias Ringwald                     // log_info("advertising report received");
19397bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
194057c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
19417bdc6798S[email protected]                     break;
1942d70217a2SMatthias Ringwald #endif
19435909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
19445909f7f2Smatthias.ringwald                     // Connection management
1945724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[8], addr);
19467bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
19479da54300S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr));
19482e77e513S[email protected]                     conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
1949d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
195042ff5ba1SMatthias Ringwald                     // if auto-connect, remove from whitelist in both roles
195142ff5ba1SMatthias Ringwald                     if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){
195242ff5ba1SMatthias Ringwald                         hci_remove_from_whitelist(addr_type, addr);
195342ff5ba1SMatthias Ringwald                     }
195442ff5ba1SMatthias Ringwald                     // handle error: error is reported only to the initiator -> outgoing connection
19555909f7f2Smatthias.ringwald                     if (packet[3]){
195642ff5ba1SMatthias Ringwald                         // outgoing connection establishment is done
195742ff5ba1SMatthias Ringwald                         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
195842ff5ba1SMatthias Ringwald                         // remove entry
19595909f7f2Smatthias.ringwald                         if (conn){
1960665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
19615909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
19625909f7f2Smatthias.ringwald                         }
19635909f7f2Smatthias.ringwald                         break;
19645909f7f2Smatthias.ringwald                     }
1965d70217a2SMatthias Ringwald #endif
196642ff5ba1SMatthias Ringwald                     // on success, both hosts receive connection complete event
19675cf766e8SMatthias Ringwald                     if (packet[6] == HCI_ROLE_MASTER){
1968d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
196942ff5ba1SMatthias Ringwald                         // if we're master, it was an outgoing connection and we're done with it
197042ff5ba1SMatthias Ringwald                         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
1971d70217a2SMatthias Ringwald #endif
197242ff5ba1SMatthias Ringwald                     } else {
1973d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL
197442ff5ba1SMatthias Ringwald                         // if we're slave, it was an incoming connection, advertisements have stopped
1975171293d3SMatthias Ringwald                         hci_stack->le_advertisements_active = 0;
19765106e6dcSMatthias Ringwald                         // try to re-enable them
19775106e6dcSMatthias Ringwald                         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
1978d70217a2SMatthias Ringwald #endif
197942ff5ba1SMatthias Ringwald                     }
1980171293d3SMatthias Ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
198142ff5ba1SMatthias Ringwald                     if (!conn){
198296a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
19835909f7f2Smatthias.ringwald                     }
198442ff5ba1SMatthias Ringwald                     // no memory, sorry.
19855909f7f2Smatthias.ringwald                     if (!conn){
19865909f7f2Smatthias.ringwald                         break;
19875909f7f2Smatthias.ringwald                     }
19885909f7f2Smatthias.ringwald 
19895909f7f2Smatthias.ringwald                     conn->state = OPEN;
19905cf766e8SMatthias Ringwald                     conn->role  = packet[6];
1991f8fbdce0SMatthias Ringwald                     conn->con_handle = little_endian_read_16(packet, 4);
19925909f7f2Smatthias.ringwald 
19935909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
19945909f7f2Smatthias.ringwald 
19955909f7f2Smatthias.ringwald                     // restart timer
1996528a4a3bSMatthias Ringwald                     // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1997528a4a3bSMatthias Ringwald                     // btstack_run_loop_add_timer(&conn->timeout);
19985909f7f2Smatthias.ringwald 
19999da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
20005909f7f2Smatthias.ringwald 
20015909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
20025909f7f2Smatthias.ringwald                     break;
20035909f7f2Smatthias.ringwald 
2004f8fbdce0SMatthias Ringwald             // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]);
200565a46ef3S[email protected] 
20065909f7f2Smatthias.ringwald                 default:
20075909f7f2Smatthias.ringwald                     break;
20085909f7f2Smatthias.ringwald             }
20095909f7f2Smatthias.ringwald             break;
20105909f7f2Smatthias.ringwald #endif
20116772a24cSmatthias.ringwald         default:
20126772a24cSmatthias.ringwald             break;
2013fe1ed1b8Smatthias.ringwald     }
2014fe1ed1b8Smatthias.ringwald 
20153429f56bSmatthias.ringwald     // handle BT initialization
20163a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
20176155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
2018c9af4d3fS[email protected]     }
201922909952Smatthias.ringwald 
202089db417bSmatthias.ringwald     // help with BT sleep
20213a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
202274b323a9SMatthias Ringwald         && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE
2023073bd0faSMatthias Ringwald         && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){
202455975f88SMatthias Ringwald         hci_initializing_next_state();
202589db417bSmatthias.ringwald     }
202689db417bSmatthias.ringwald 
202786805605S[email protected]     // notify upper stack
2028d6b06661SMatthias Ringwald 	hci_emit_event(packet, size, 0);   // don't dump, already happened in packet handler
202994ab26f8Smatthias.ringwald 
203086805605S[email protected]     // moved here to give upper stack a chance to close down everything with hci_connection_t intact
20310e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){
203286805605S[email protected]         if (!packet[2]){
2033f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
203405ae8de3SMatthias Ringwald             hci_connection_t * aConn = hci_connection_for_handle(handle);
203505ae8de3SMatthias Ringwald             if (aConn) {
203605ae8de3SMatthias Ringwald                 uint8_t status = aConn->bonding_status;
203705ae8de3SMatthias Ringwald                 uint16_t flags = aConn->bonding_flags;
203886805605S[email protected]                 bd_addr_t bd_address;
203905ae8de3SMatthias Ringwald                 memcpy(&bd_address, aConn->address, 6);
204005ae8de3SMatthias Ringwald                 hci_shutdown_connection(aConn);
2041bb820044S[email protected]                 // connection struct is gone, don't access anymore
2042bb820044S[email protected]                 if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){
204386805605S[email protected]                     hci_emit_dedicated_bonding_result(bd_address, status);
204486805605S[email protected]                 }
204586805605S[email protected]             }
204686805605S[email protected]         }
204786805605S[email protected]     }
204886805605S[email protected] 
204994ab26f8Smatthias.ringwald 	// execute main loop
205094ab26f8Smatthias.ringwald 	hci_run();
205116833f0aSmatthias.ringwald }
205216833f0aSmatthias.ringwald 
205335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2054c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){
20558abbe8b5SMatthias Ringwald     if (!hci_stack->sco_packet_handler) return;
20563d50b4baSMatthias Ringwald     hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size);
2057c91d150bS[email protected] }
205835454696SMatthias Ringwald #endif
2059c91d150bS[email protected] 
20600a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
20615bb5bc3eS[email protected]     hci_dump_packet(packet_type, 1, packet, size);
206210e830c9Smatthias.ringwald     switch (packet_type) {
206310e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
206410e830c9Smatthias.ringwald             event_handler(packet, size);
206510e830c9Smatthias.ringwald             break;
206610e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
206710e830c9Smatthias.ringwald             acl_handler(packet, size);
206810e830c9Smatthias.ringwald             break;
206935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2070c91d150bS[email protected]         case HCI_SCO_DATA_PACKET:
2071c91d150bS[email protected]             sco_handler(packet, size);
2072202c8a4cSMatthias Ringwald             break;
207335454696SMatthias Ringwald #endif
207410e830c9Smatthias.ringwald         default:
207510e830c9Smatthias.ringwald             break;
207610e830c9Smatthias.ringwald     }
207710e830c9Smatthias.ringwald }
207810e830c9Smatthias.ringwald 
2079d6b06661SMatthias Ringwald /**
2080d6b06661SMatthias Ringwald  * @brief Add event packet handler.
2081d6b06661SMatthias Ringwald  */
2082d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
2083d6b06661SMatthias Ringwald     btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler);
2084d6b06661SMatthias Ringwald }
2085d6b06661SMatthias Ringwald 
2086d6b06661SMatthias Ringwald 
2087fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
20883d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){
2089fb37a842SMatthias Ringwald     hci_stack->acl_packet_handler = handler;
209016833f0aSmatthias.ringwald }
209116833f0aSmatthias.ringwald 
209235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
20938abbe8b5SMatthias Ringwald /**
20948abbe8b5SMatthias Ringwald  * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles.
20958abbe8b5SMatthias Ringwald  */
20963d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){
20978abbe8b5SMatthias Ringwald     hci_stack->sco_packet_handler = handler;
20988abbe8b5SMatthias Ringwald }
209935454696SMatthias Ringwald #endif
21008abbe8b5SMatthias Ringwald 
210171de195eSMatthias Ringwald static void hci_state_reset(void){
2102595bdbfbS[email protected]     // no connections yet
2103595bdbfbS[email protected]     hci_stack->connections = NULL;
210474308b23Smatthias.ringwald 
210574308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
210674308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
210774308b23Smatthias.ringwald     // hci_stack->connectable = 0;
210874308b23Smatthias.ringwald     // hci_stack->bondable = 1;
2109b95a5a35SMatthias Ringwald     // hci_stack->own_addr_type = 0;
2110595bdbfbS[email protected] 
211144935e40S[email protected]     // buffer is free
211244935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
211344935e40S[email protected] 
2114595bdbfbS[email protected]     // no pending cmds
2115595bdbfbS[email protected]     hci_stack->decline_reason = 0;
2116595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
2117595bdbfbS[email protected] 
2118595bdbfbS[email protected]     // LE
2119b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE
2120b95a5a35SMatthias Ringwald     memset(hci_stack->le_random_address, 0, 6);
2121b95a5a35SMatthias Ringwald     hci_stack->le_random_address_set = 0;
2122d70217a2SMatthias Ringwald #endif
2123d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
2124595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
2125e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
2126b04dfa37SMatthias Ringwald     hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
2127e83201bcSMatthias Ringwald     hci_stack->le_whitelist = 0;
2128e83201bcSMatthias Ringwald     hci_stack->le_whitelist_capacity = 0;
2129d70217a2SMatthias Ringwald #endif
2130d70217a2SMatthias Ringwald 
2131e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_interval_min =          6;
2132e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_interval_max =       3200;
2133e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_latency_min =           0;
2134e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_latency_max =         500;
2135e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_supervision_timeout_min =   10;
2136e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200;
2137595bdbfbS[email protected] }
2138595bdbfbS[email protected] 
213935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
21402d5e09d6SMatthias Ringwald /**
21412d5e09d6SMatthias Ringwald  * @brief Configure Bluetooth hardware control. Has to be called before power on.
21422d5e09d6SMatthias Ringwald  */
21432d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){
21442d5e09d6SMatthias Ringwald     // store and open remote device db
21452d5e09d6SMatthias Ringwald     hci_stack->link_key_db = link_key_db;
21462d5e09d6SMatthias Ringwald     if (hci_stack->link_key_db) {
21472d5e09d6SMatthias Ringwald         hci_stack->link_key_db->open();
21482d5e09d6SMatthias Ringwald     }
21492d5e09d6SMatthias Ringwald }
215035454696SMatthias Ringwald #endif
21512d5e09d6SMatthias Ringwald 
21522d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){
2153475c8125Smatthias.ringwald 
21543a9fb326S[email protected] #ifdef HAVE_MALLOC
21553a9fb326S[email protected]     if (!hci_stack) {
21563a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
21573a9fb326S[email protected]     }
21583a9fb326S[email protected] #else
21593a9fb326S[email protected]     hci_stack = &hci_stack_static;
21603a9fb326S[email protected] #endif
216166fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
21623a9fb326S[email protected] 
2163475c8125Smatthias.ringwald     // reference to use transport layer implementation
21643a9fb326S[email protected]     hci_stack->hci_transport = transport;
2165475c8125Smatthias.ringwald 
216611e23e5fSmatthias.ringwald     // reference to used config
21673a9fb326S[email protected]     hci_stack->config = config;
216811e23e5fSmatthias.ringwald 
216926a9b6daSMatthias Ringwald     // setup pointer for outgoing packet buffer
217026a9b6daSMatthias Ringwald     hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE];
217126a9b6daSMatthias Ringwald 
21728fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
21733a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
21748fcba05dSmatthias.ringwald 
217516833f0aSmatthias.ringwald     // register packet handlers with transport
217610e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
2177f5454fc6Smatthias.ringwald 
21783a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
2179e2386ba1S[email protected] 
2180e2386ba1S[email protected]     // class of device
21813a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
2182a45d6b9fS[email protected] 
2183f20168b8Smatthias.ringwald     // bondable by default
2184f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
2185f20168b8Smatthias.ringwald 
218663048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
21873a9fb326S[email protected]     hci_stack->ssp_enable = 1;
21883a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
21893a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
21903a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
219169a97523S[email protected] 
2192*fac2e2feSMatthias Ringwald     // voice setting - signed 16 bit pcm data with CVSD over the air
2193*fac2e2feSMatthias Ringwald     hci_stack->sco_voice_setting = 0x60;
2194d950d659SMatthias Ringwald 
2195595bdbfbS[email protected]     hci_state_reset();
2196475c8125Smatthias.ringwald }
2197475c8125Smatthias.ringwald 
21983fb36a29SMatthias Ringwald /**
21993fb36a29SMatthias Ringwald  * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information
22003fb36a29SMatthias Ringwald  */
22013fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){
22023fb36a29SMatthias Ringwald     hci_stack->chipset = chipset_driver;
22033fb36a29SMatthias Ringwald 
22043fb36a29SMatthias Ringwald     // reset chipset driver - init is also called on power_up
22053fb36a29SMatthias Ringwald     if (hci_stack->chipset && hci_stack->chipset->init){
22063fb36a29SMatthias Ringwald         hci_stack->chipset->init(hci_stack->config);
22073fb36a29SMatthias Ringwald     }
22083fb36a29SMatthias Ringwald }
22093fb36a29SMatthias Ringwald 
2210fb55bd0aSMatthias Ringwald /**
2211d0b87befSMatthias Ringwald  * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on.
2212fb55bd0aSMatthias Ringwald  */
2213fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){
2214fb55bd0aSMatthias Ringwald     // references to used control implementation
2215fb55bd0aSMatthias Ringwald     hci_stack->control = hardware_control;
2216d0b87befSMatthias Ringwald     // init with transport config
2217d0b87befSMatthias Ringwald     hardware_control->init(hci_stack->config);
2218fb55bd0aSMatthias Ringwald }
2219fb55bd0aSMatthias Ringwald 
222071de195eSMatthias Ringwald void hci_close(void){
2221404843c1Smatthias.ringwald     // close remote device db
2222a98592bcSMatthias Ringwald     if (hci_stack->link_key_db) {
2223a98592bcSMatthias Ringwald         hci_stack->link_key_db->close();
2224404843c1Smatthias.ringwald     }
22257224be7eSMatthias Ringwald 
22267224be7eSMatthias Ringwald     btstack_linked_list_iterator_t lit;
22277224be7eSMatthias Ringwald     btstack_linked_list_iterator_init(&lit, &hci_stack->connections);
22287224be7eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&lit)){
22297224be7eSMatthias Ringwald         // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection
22307224be7eSMatthias Ringwald         hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit);
22317224be7eSMatthias Ringwald         hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host
22327224be7eSMatthias Ringwald         hci_shutdown_connection(connection);
2233f5454fc6Smatthias.ringwald     }
22347224be7eSMatthias Ringwald 
2235f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
22363a9fb326S[email protected] 
22373a9fb326S[email protected] #ifdef HAVE_MALLOC
22383a9fb326S[email protected]     free(hci_stack);
22393a9fb326S[email protected] #endif
22403a9fb326S[email protected]     hci_stack = NULL;
2241404843c1Smatthias.ringwald }
2242404843c1Smatthias.ringwald 
224335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
224460b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){
22459e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
22469e61646fS[email protected] }
224776f27cffSMatthias Ringwald 
224876f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){
224976f27cffSMatthias Ringwald     disable_l2cap_timeouts = 1;
225076f27cffSMatthias Ringwald }
225135454696SMatthias Ringwald #endif
22529e61646fS[email protected] 
225376f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
2254f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h
2255f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){
2256f456b2d0S[email protected]     memcpy(hci_stack->custom_bd_addr, addr, 6);
2257f456b2d0S[email protected]     hci_stack->custom_bd_addr_set = 1;
2258f456b2d0S[email protected] }
225976f27cffSMatthias Ringwald #endif
2260f456b2d0S[email protected] 
22618d213e1aSmatthias.ringwald // State-Module-Driver overview
22628d213e1aSmatthias.ringwald // state                    module  low-level
22638d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
22648d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
22658d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
22668d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
2267d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
2268d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
2269c7e0c5f6Smatthias.ringwald 
227040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
22717301ad89Smatthias.ringwald 
2272038bc64cSmatthias.ringwald     // power on
2273f9a30166Smatthias.ringwald     int err = 0;
22743a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
2275d0b87befSMatthias Ringwald         err = (*hci_stack->control->on)();
2276f9a30166Smatthias.ringwald     }
2277038bc64cSmatthias.ringwald     if (err){
22789da54300S[email protected]         log_error( "POWER_ON failed");
2279038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
2280038bc64cSmatthias.ringwald         return err;
2281038bc64cSmatthias.ringwald     }
2282038bc64cSmatthias.ringwald 
228324b3c629SMatthias Ringwald     // int chipset driver
22843fb36a29SMatthias Ringwald     if (hci_stack->chipset && hci_stack->chipset->init){
22853fb36a29SMatthias Ringwald         hci_stack->chipset->init(hci_stack->config);
22863fb36a29SMatthias Ringwald     }
22873fb36a29SMatthias Ringwald 
228824b3c629SMatthias Ringwald     // init transport
228924b3c629SMatthias Ringwald     if (hci_stack->hci_transport->init){
229024b3c629SMatthias Ringwald         hci_stack->hci_transport->init(hci_stack->config);
229124b3c629SMatthias Ringwald     }
229224b3c629SMatthias Ringwald 
229324b3c629SMatthias Ringwald     // open transport
229424b3c629SMatthias Ringwald     err = hci_stack->hci_transport->open();
2295038bc64cSmatthias.ringwald     if (err){
22969da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
22973a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
2298d0b87befSMatthias Ringwald             (*hci_stack->control->off)();
2299f9a30166Smatthias.ringwald         }
2300038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
2301038bc64cSmatthias.ringwald         return err;
2302038bc64cSmatthias.ringwald     }
23038d213e1aSmatthias.ringwald     return 0;
23048d213e1aSmatthias.ringwald }
2305038bc64cSmatthias.ringwald 
230640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
23078d213e1aSmatthias.ringwald 
23089da54300S[email protected]     log_info("hci_power_control_off");
23099418f9c9Smatthias.ringwald 
23108d213e1aSmatthias.ringwald     // close low-level device
231124b3c629SMatthias Ringwald     hci_stack->hci_transport->close();
23128d213e1aSmatthias.ringwald 
23139da54300S[email protected]     log_info("hci_power_control_off - hci_transport closed");
23149418f9c9Smatthias.ringwald 
23158d213e1aSmatthias.ringwald     // power off
23163a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
2317d0b87befSMatthias Ringwald         (*hci_stack->control->off)();
23188d213e1aSmatthias.ringwald     }
23199418f9c9Smatthias.ringwald 
23209da54300S[email protected]     log_info("hci_power_control_off - control closed");
23219418f9c9Smatthias.ringwald 
23223a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
232372ea5239Smatthias.ringwald }
232472ea5239Smatthias.ringwald 
232540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
232672ea5239Smatthias.ringwald 
23279da54300S[email protected]     log_info("hci_power_control_sleep");
23283144bce4Smatthias.ringwald 
2329b429b9b7Smatthias.ringwald #if 0
2330b429b9b7Smatthias.ringwald     // don't close serial port during sleep
2331b429b9b7Smatthias.ringwald 
233272ea5239Smatthias.ringwald     // close low-level device
23333a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
2334b429b9b7Smatthias.ringwald #endif
233572ea5239Smatthias.ringwald 
233672ea5239Smatthias.ringwald     // sleep mode
23373a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
2338d0b87befSMatthias Ringwald         (*hci_stack->control->sleep)();
233972ea5239Smatthias.ringwald     }
2340b429b9b7Smatthias.ringwald 
23413a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
23428d213e1aSmatthias.ringwald }
23438d213e1aSmatthias.ringwald 
234440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
2345b429b9b7Smatthias.ringwald 
23469da54300S[email protected]     log_info("hci_power_control_wake");
2347b429b9b7Smatthias.ringwald 
2348b429b9b7Smatthias.ringwald     // wake on
23493a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
2350d0b87befSMatthias Ringwald         (*hci_stack->control->wake)();
2351b429b9b7Smatthias.ringwald     }
2352b429b9b7Smatthias.ringwald 
2353b429b9b7Smatthias.ringwald #if 0
2354b429b9b7Smatthias.ringwald     // open low-level device
23553a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
2356b429b9b7Smatthias.ringwald     if (err){
23579da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
23583a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
2359d0b87befSMatthias Ringwald             (*hci_stack->control->off)();
2360b429b9b7Smatthias.ringwald         }
2361b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
2362b429b9b7Smatthias.ringwald         return err;
2363b429b9b7Smatthias.ringwald     }
2364b429b9b7Smatthias.ringwald #endif
2365b429b9b7Smatthias.ringwald 
2366b429b9b7Smatthias.ringwald     return 0;
2367b429b9b7Smatthias.ringwald }
2368b429b9b7Smatthias.ringwald 
236944935e40S[email protected] static void hci_power_transition_to_initializing(void){
237044935e40S[email protected]     // set up state machine
237144935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
237244935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
237344935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
23745c363727SMatthias Ringwald     hci_stack->substate = HCI_INIT_SEND_RESET;
237544935e40S[email protected] }
2376b429b9b7Smatthias.ringwald 
23778d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
2378d661ed19Smatthias.ringwald 
2379f04a0c31SMatthias Ringwald     log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state);
2380d661ed19Smatthias.ringwald 
23818d213e1aSmatthias.ringwald     int err = 0;
23823a9fb326S[email protected]     switch (hci_stack->state){
23838d213e1aSmatthias.ringwald 
23848d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
23858d213e1aSmatthias.ringwald             switch (power_mode){
23868d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
23878d213e1aSmatthias.ringwald                     err = hci_power_control_on();
238897b61c7bS[email protected]                     if (err) {
2389f04a0c31SMatthias Ringwald                         log_error("hci_power_control_on() error %d", err);
239097b61c7bS[email protected]                         return err;
239197b61c7bS[email protected]                     }
239244935e40S[email protected]                     hci_power_transition_to_initializing();
23938d213e1aSmatthias.ringwald                     break;
23948d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
23958d213e1aSmatthias.ringwald                     // do nothing
23968d213e1aSmatthias.ringwald                     break;
23978d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2398b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
23998d213e1aSmatthias.ringwald                     break;
24008d213e1aSmatthias.ringwald             }
24018d213e1aSmatthias.ringwald             break;
24027301ad89Smatthias.ringwald 
24038d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
24048d213e1aSmatthias.ringwald             switch (power_mode){
24058d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
24068d213e1aSmatthias.ringwald                     // do nothing
24078d213e1aSmatthias.ringwald                     break;
24088d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
24098d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
24108d213e1aSmatthias.ringwald                     hci_power_control_off();
24118d213e1aSmatthias.ringwald                     break;
24128d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2413b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
241472ea5239Smatthias.ringwald                     hci_power_control_sleep();
24158d213e1aSmatthias.ringwald                     break;
24168d213e1aSmatthias.ringwald             }
24178d213e1aSmatthias.ringwald             break;
24187301ad89Smatthias.ringwald 
24198d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
24208d213e1aSmatthias.ringwald             switch (power_mode){
24218d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
24228d213e1aSmatthias.ringwald                     // do nothing
24238d213e1aSmatthias.ringwald                     break;
24248d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
2425c7e0c5f6Smatthias.ringwald                     // see hci_run
24263a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
24278d213e1aSmatthias.ringwald                     break;
24288d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2429b546ac54Smatthias.ringwald                     // see hci_run
24303a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
243174b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
24328d213e1aSmatthias.ringwald                     break;
24338d213e1aSmatthias.ringwald             }
24348d213e1aSmatthias.ringwald             break;
24357301ad89Smatthias.ringwald 
24368d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
24378d213e1aSmatthias.ringwald             switch (power_mode){
24388d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
243944935e40S[email protected]                     hci_power_transition_to_initializing();
24408d213e1aSmatthias.ringwald                     break;
24418d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
24428d213e1aSmatthias.ringwald                     // do nothing
24438d213e1aSmatthias.ringwald                     break;
24448d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2445b546ac54Smatthias.ringwald                     // see hci_run
24463a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
244774b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
24488d213e1aSmatthias.ringwald                     break;
24498d213e1aSmatthias.ringwald             }
24508d213e1aSmatthias.ringwald             break;
24518d213e1aSmatthias.ringwald 
24528d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
24538d213e1aSmatthias.ringwald             switch (power_mode){
24548d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
245528171530Smatthias.ringwald 
2456423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
245728171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
2458d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
24593a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
246074b323a9SMatthias Ringwald                         hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE;   // init after sleep
246128171530Smatthias.ringwald                         break;
246228171530Smatthias.ringwald                     }
246328171530Smatthias.ringwald #endif
246444935e40S[email protected]                     hci_power_transition_to_initializing();
24658d213e1aSmatthias.ringwald                     break;
24668d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
2467b546ac54Smatthias.ringwald                     // see hci_run
24683a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
24698d213e1aSmatthias.ringwald                     break;
24708d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2471b546ac54Smatthias.ringwald                     // do nothing
24728d213e1aSmatthias.ringwald                     break;
24738d213e1aSmatthias.ringwald             }
24748d213e1aSmatthias.ringwald             break;
24758d213e1aSmatthias.ringwald 
24768d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
24778d213e1aSmatthias.ringwald             switch (power_mode){
24788d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
247928171530Smatthias.ringwald 
2480423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
248128171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
2482d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
24833a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
24845c363727SMatthias Ringwald                         hci_stack->substate = HCI_INIT_AFTER_SLEEP;
2485758b46ceSmatthias.ringwald                         hci_update_scan_enable();
248628171530Smatthias.ringwald                         break;
248728171530Smatthias.ringwald                     }
248828171530Smatthias.ringwald #endif
24893144bce4Smatthias.ringwald                     err = hci_power_control_wake();
24903144bce4Smatthias.ringwald                     if (err) return err;
249144935e40S[email protected]                     hci_power_transition_to_initializing();
24928d213e1aSmatthias.ringwald                     break;
24938d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
24943a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
24958d213e1aSmatthias.ringwald                     break;
24968d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2497b546ac54Smatthias.ringwald                     // do nothing
24988d213e1aSmatthias.ringwald                     break;
24998d213e1aSmatthias.ringwald             }
25008d213e1aSmatthias.ringwald             break;
250111e23e5fSmatthias.ringwald     }
250268d92d03Smatthias.ringwald 
2503038bc64cSmatthias.ringwald     // create internal event
2504ee8bf225Smatthias.ringwald 	hci_emit_state();
2505ee8bf225Smatthias.ringwald 
250668d92d03Smatthias.ringwald 	// trigger next/first action
250768d92d03Smatthias.ringwald 	hci_run();
250868d92d03Smatthias.ringwald 
2509475c8125Smatthias.ringwald     return 0;
2510475c8125Smatthias.ringwald }
2511475c8125Smatthias.ringwald 
251235454696SMatthias Ringwald 
251335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
251435454696SMatthias Ringwald 
2515758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
2516758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
25173a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
2518758b46ceSmatthias.ringwald     hci_run();
2519758b46ceSmatthias.ringwald }
2520758b46ceSmatthias.ringwald 
252115a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){
2522381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
2523381fbed8Smatthias.ringwald 
25243a9fb326S[email protected]     if (hci_stack->discoverable == enable){
25253a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
2526381fbed8Smatthias.ringwald         return;
2527381fbed8Smatthias.ringwald     }
2528381fbed8Smatthias.ringwald 
25293a9fb326S[email protected]     hci_stack->discoverable = enable;
2530758b46ceSmatthias.ringwald     hci_update_scan_enable();
2531758b46ceSmatthias.ringwald }
2532b031bebbSmatthias.ringwald 
253315a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){
2534758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
2535758b46ceSmatthias.ringwald 
2536758b46ceSmatthias.ringwald     // don't emit event
25373a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
2538758b46ceSmatthias.ringwald 
25393a9fb326S[email protected]     hci_stack->connectable = enable;
2540758b46ceSmatthias.ringwald     hci_update_scan_enable();
2541381fbed8Smatthias.ringwald }
254235454696SMatthias Ringwald #endif
2543381fbed8Smatthias.ringwald 
254415a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){
2545690bd0baS[email protected]     memcpy(address_buffer, hci_stack->local_bd_addr, 6);
25465061f3afS[email protected] }
25475061f3afS[email protected] 
254895d71764SMatthias Ringwald static void hci_run(void){
25498a485f27Smatthias.ringwald 
2550db8bc6ffSMatthias Ringwald     // log_info("hci_run: entered");
2551665d90f2SMatthias Ringwald     btstack_linked_item_t * it;
255232ab9390Smatthias.ringwald 
2553b5d8b22bS[email protected]     // send continuation fragments first, as they block the prepared packet buffer
2554b5d8b22bS[email protected]     if (hci_stack->acl_fragmentation_total_size > 0) {
2555b5d8b22bS[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer);
2556b5d8b22bS[email protected]         hci_connection_t *connection = hci_connection_for_handle(con_handle);
2557b5d8b22bS[email protected]         if (connection) {
255828a0332dSMatthias Ringwald             if (hci_can_send_prepared_acl_packet_now(con_handle)){
2559b5d8b22bS[email protected]                 hci_send_acl_packet_fragments(connection);
2560b5d8b22bS[email protected]                 return;
2561b5d8b22bS[email protected]             }
256228a0332dSMatthias Ringwald         } else {
2563b5d8b22bS[email protected]             // connection gone -> discard further fragments
256428a0332dSMatthias Ringwald             log_info("hci_run: fragmented ACL packet no connection -> discard fragment");
2565b5d8b22bS[email protected]             hci_stack->acl_fragmentation_total_size = 0;
2566b5d8b22bS[email protected]             hci_stack->acl_fragmentation_pos = 0;
2567b5d8b22bS[email protected]         }
2568b5d8b22bS[email protected]     }
2569b5d8b22bS[email protected] 
2570d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()) return;
2571ce4c8fabSmatthias.ringwald 
2572b031bebbSmatthias.ringwald     // global/non-connection oriented commands
2573b031bebbSmatthias.ringwald 
257435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2575b031bebbSmatthias.ringwald     // decline incoming connections
25763a9fb326S[email protected]     if (hci_stack->decline_reason){
25773a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
25783a9fb326S[email protected]         hci_stack->decline_reason = 0;
25793a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
2580dbe1a790S[email protected]         return;
2581ce4c8fabSmatthias.ringwald     }
2582ce4c8fabSmatthias.ringwald 
2583b031bebbSmatthias.ringwald     // send scan enable
25843a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
25853a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
25863a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
2587dbe1a790S[email protected]         return;
2588b031bebbSmatthias.ringwald     }
258935454696SMatthias Ringwald #endif
2590b031bebbSmatthias.ringwald 
2591a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
2592b95a5a35SMatthias Ringwald     // advertisements, active scanning, and creating connections requires randaom address to be set if using private address
2593b95a5a35SMatthias Ringwald     if ((hci_stack->state == HCI_STATE_WORKING)
2594b95a5a35SMatthias Ringwald     && (hci_stack->le_own_addr_type == BD_ADDR_TYPE_LE_PUBLIC || hci_stack->le_random_address_set)){
2595d70217a2SMatthias Ringwald 
2596d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
259745c102fdSMatthias Ringwald         // handle le scan
25987bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
25997bdc6798S[email protected]             case LE_START_SCAN:
26007bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
26017bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
26027bdc6798S[email protected]                 return;
26037bdc6798S[email protected] 
26047bdc6798S[email protected]             case LE_STOP_SCAN:
26057bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
26067bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
26077bdc6798S[email protected]                 return;
26087bdc6798S[email protected]             default:
26097bdc6798S[email protected]                 break;
26107bdc6798S[email protected]         }
2611e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
2612e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
2613e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
2614e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
2615b95a5a35SMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_parameters, scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->le_own_addr_type, 0);
2616e2602ea2Smatthias.ringwald             return;
2617e2602ea2Smatthias.ringwald         }
2618d70217a2SMatthias Ringwald #endif
2619d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL
262045c102fdSMatthias Ringwald         // le advertisement control
26219a2e4658SMatthias Ringwald         if (hci_stack->le_advertisements_todo){
26229a2e4658SMatthias Ringwald             log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo );
26239a2e4658SMatthias Ringwald         }
262445c102fdSMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){
262545c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE;
262645c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertise_enable, 0);
262745c102fdSMatthias Ringwald             return;
262845c102fdSMatthias Ringwald         }
262945c102fdSMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){
263045c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS;
263145c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertising_parameters,
263245c102fdSMatthias Ringwald                  hci_stack->le_advertisements_interval_min,
263345c102fdSMatthias Ringwald                  hci_stack->le_advertisements_interval_max,
263445c102fdSMatthias Ringwald                  hci_stack->le_advertisements_type,
2635b95a5a35SMatthias Ringwald                  hci_stack->le_own_addr_type,
263645c102fdSMatthias Ringwald                  hci_stack->le_advertisements_direct_address_type,
263745c102fdSMatthias Ringwald                  hci_stack->le_advertisements_direct_address,
263845c102fdSMatthias Ringwald                  hci_stack->le_advertisements_channel_map,
263945c102fdSMatthias Ringwald                  hci_stack->le_advertisements_filter_policy);
264045c102fdSMatthias Ringwald             return;
264145c102fdSMatthias Ringwald         }
2642501f56b3SMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){
2643501f56b3SMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA;
2644b95a5a35SMatthias Ringwald             hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, hci_stack->le_advertisements_data);
264545c102fdSMatthias Ringwald             return;
264645c102fdSMatthias Ringwald         }
2647501f56b3SMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){
2648501f56b3SMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA;
2649501f56b3SMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len,
2650501f56b3SMatthias Ringwald                 hci_stack->le_scan_response_data);
2651501f56b3SMatthias Ringwald             return;
2652501f56b3SMatthias Ringwald         }
2653b95a5a35SMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){
265445c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE;
265545c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertise_enable, 1);
265645c102fdSMatthias Ringwald             return;
265745c102fdSMatthias Ringwald         }
2658d70217a2SMatthias Ringwald #endif
26599956955bSMatthias Ringwald 
2660d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
26619956955bSMatthias Ringwald         //
26629956955bSMatthias Ringwald         // LE Whitelist Management
26639956955bSMatthias Ringwald         //
26649956955bSMatthias Ringwald 
26659956955bSMatthias Ringwald         // check if whitelist needs modification
2666665d90f2SMatthias Ringwald         btstack_linked_list_iterator_t lit;
26679956955bSMatthias Ringwald         int modification_pending = 0;
2668665d90f2SMatthias Ringwald         btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2669665d90f2SMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&lit)){
2670665d90f2SMatthias Ringwald             whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
26719956955bSMatthias Ringwald             if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){
26729956955bSMatthias Ringwald                 modification_pending = 1;
26739956955bSMatthias Ringwald                 break;
26749956955bSMatthias Ringwald             }
26759956955bSMatthias Ringwald         }
267691915b0bSMatthias Ringwald 
26779956955bSMatthias Ringwald         if (modification_pending){
267891915b0bSMatthias Ringwald             // stop connnecting if modification pending
26799956955bSMatthias Ringwald             if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){
26809956955bSMatthias Ringwald                 hci_send_cmd(&hci_le_create_connection_cancel);
26819956955bSMatthias Ringwald                 return;
26829956955bSMatthias Ringwald             }
26839956955bSMatthias Ringwald 
26849956955bSMatthias Ringwald             // add/remove entries
2685665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2686665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&lit)){
2687665d90f2SMatthias Ringwald                 whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
26889956955bSMatthias Ringwald                 if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){
26899956955bSMatthias Ringwald                     entry->state = LE_WHITELIST_ON_CONTROLLER;
26909956955bSMatthias Ringwald                     hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address);
26919956955bSMatthias Ringwald                     return;
26929956955bSMatthias Ringwald 
26939956955bSMatthias Ringwald                 }
26949956955bSMatthias Ringwald                 if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){
26959ecbe201SMatthias Ringwald                     bd_addr_t address;
26969ecbe201SMatthias Ringwald                     bd_addr_type_t address_type = entry->address_type;
26979ecbe201SMatthias Ringwald                     memcpy(address, entry->address, 6);
2698665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry);
26999956955bSMatthias Ringwald                     btstack_memory_whitelist_entry_free(entry);
27009ecbe201SMatthias Ringwald                     hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address);
27019956955bSMatthias Ringwald                     return;
27029956955bSMatthias Ringwald                 }
27039956955bSMatthias Ringwald             }
270491915b0bSMatthias Ringwald         }
27059956955bSMatthias Ringwald 
27069956955bSMatthias Ringwald         // start connecting
270791915b0bSMatthias Ringwald         if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE &&
2708665d90f2SMatthias Ringwald             !btstack_linked_list_empty(&hci_stack->le_whitelist)){
27099956955bSMatthias Ringwald             bd_addr_t null_addr;
27109956955bSMatthias Ringwald             memset(null_addr, 0, 6);
27119956955bSMatthias Ringwald             hci_send_cmd(&hci_le_create_connection,
27129956955bSMatthias Ringwald                  0x0060,    // scan interval: 60 ms
27139956955bSMatthias Ringwald                  0x0030,    // scan interval: 30 ms
27149956955bSMatthias Ringwald                  1,         // use whitelist
27159956955bSMatthias Ringwald                  0,         // peer address type
27169956955bSMatthias Ringwald                  null_addr, // peer bd addr
2717b95a5a35SMatthias Ringwald                  hci_stack->le_own_addr_type, // our addr type:
27189956955bSMatthias Ringwald                  0x0008,    // conn interval min
27199956955bSMatthias Ringwald                  0x0018,    // conn interval max
27209956955bSMatthias Ringwald                  0,         // conn latency
27219956955bSMatthias Ringwald                  0x0048,    // supervision timeout
27229956955bSMatthias Ringwald                  0x0001,    // min ce length
27239956955bSMatthias Ringwald                  0x0001     // max ce length
27249956955bSMatthias Ringwald                  );
27259956955bSMatthias Ringwald             return;
27269956955bSMatthias Ringwald         }
2727d70217a2SMatthias Ringwald #endif
27287bdc6798S[email protected]     }
2729b2f949feS[email protected] #endif
27307bdc6798S[email protected] 
273132ab9390Smatthias.ringwald     // send pending HCI commands
2732665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
273305ae8de3SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) it;
273432ab9390Smatthias.ringwald 
27350bf6344aS[email protected]         switch(connection->state){
27360bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
27374f3229d8S[email protected]                 switch(connection->address_type){
273835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
27394f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
27409da54300S[email protected]                         log_info("sending hci_create_connection");
2741ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
27424f3229d8S[email protected]                         break;
274335454696SMatthias Ringwald #endif
27444f3229d8S[email protected]                     default:
2745a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
2746d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
27479da54300S[email protected]                         log_info("sending hci_le_create_connection");
27484f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
2749b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
2750b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
27514f3229d8S[email protected]                                      0,         // don't use whitelist
27524f3229d8S[email protected]                                      connection->address_type, // peer address type
27534f3229d8S[email protected]                                      connection->address,      // peer bd addr
2754b95a5a35SMatthias Ringwald                                      hci_stack->le_own_addr_type,  // our addr type:
2755b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
2756b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
27574f3229d8S[email protected]                                      0,         // conn latency
2758b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
2759b2571179Smatthias.ringwald                                      0x0001,    // min ce length
2760b2571179Smatthias.ringwald                                      0x0001     // max ce length
27614f3229d8S[email protected]                                      );
27624f3229d8S[email protected] 
27634f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
2764b2f949feS[email protected] #endif
2765d70217a2SMatthias Ringwald #endif
27664f3229d8S[email protected]                         break;
27674f3229d8S[email protected]                 }
2768ad83dc6aS[email protected]                 return;
2769ad83dc6aS[email protected] 
277035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
27710bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
277294e4aaa2SMatthias Ringwald                 log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO);
277332ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
27745cf766e8SMatthias Ringwald                 connection->role  = HCI_ROLE_SLAVE;
2775e35edcc1S[email protected]                 if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
277634d2123cS[email protected]                     hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
2777e35edcc1S[email protected]                 }
2778dbe1a790S[email protected]                 return;
277935454696SMatthias Ringwald #endif
27800bf6344aS[email protected] 
2781a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
2782d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
27830bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
27840bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
27850bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
27860bf6344aS[email protected]                 return;
2787a6725849S[email protected] #endif
2788d70217a2SMatthias Ringwald #endif
27890bf6344aS[email protected]             case SEND_DISCONNECT:
27900bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
27917851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
27920bf6344aS[email protected]                 return;
27930bf6344aS[email protected] 
27940bf6344aS[email protected]             default:
27950bf6344aS[email protected]                 break;
2796c7e0c5f6Smatthias.ringwald         }
2797c7e0c5f6Smatthias.ringwald 
279835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
279932ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
28009da54300S[email protected]             log_info("responding to link key request");
280134d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
280232ab9390Smatthias.ringwald             link_key_t link_key;
2803c77e8838S[email protected]             link_key_type_t link_key_type;
2804a98592bcSMatthias Ringwald             if ( hci_stack->link_key_db
2805a98592bcSMatthias Ringwald               && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type)
280634d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
28079ab95c90S[email protected]                connection->link_key_type = link_key_type;
280832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
280932ab9390Smatthias.ringwald             } else {
281032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
281132ab9390Smatthias.ringwald             }
2812dbe1a790S[email protected]             return;
281332ab9390Smatthias.ringwald         }
28141d6b20aeS[email protected] 
2815899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
28169da54300S[email protected]             log_info("denying to pin request");
2817899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
281834d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
28194c57c146S[email protected]             return;
28204c57c146S[email protected]         }
28214c57c146S[email protected] 
2822dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
282334d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
282482d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
282582d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
2826106d6d11S[email protected]                 // tweak authentication requirements
28273a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
2828106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
28299faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
28309faad3abS[email protected]                 }
28319faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
28329faad3abS[email protected]                     authreq |= 1;
2833106d6d11S[email protected]                 }
28343a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
2835f8fb5f6eS[email protected]             } else {
2836f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
2837f8fb5f6eS[email protected]             }
2838dbe1a790S[email protected]             return;
283932ab9390Smatthias.ringwald         }
284032ab9390Smatthias.ringwald 
2841dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
2842dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
284334d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
2844dbe1a790S[email protected]             return;
2845dbe1a790S[email protected]         }
2846dbe1a790S[email protected] 
2847dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
2848dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
284934d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
2850dbe1a790S[email protected]             return;
2851dbe1a790S[email protected]         }
2852afd4e962S[email protected] 
2853afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
2854afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
285534d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
28562bd8b7e7S[email protected]             return;
28572bd8b7e7S[email protected]         }
28582bd8b7e7S[email protected] 
2859ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
2860ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
28611bd5283dS[email protected]             connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT;
286252d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
2863ad83dc6aS[email protected]             return;
2864ad83dc6aS[email protected]         }
286576f27cffSMatthias Ringwald 
286634d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
286734d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
286834d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
28692bd8b7e7S[email protected]             return;
2870afd4e962S[email protected]         }
287176f27cffSMatthias Ringwald 
2872dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
2873dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
2874dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
2875dce78009S[email protected]             return;
2876dce78009S[email protected]         }
287776f27cffSMatthias Ringwald #endif
287876f27cffSMatthias Ringwald 
287976f27cffSMatthias Ringwald         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
288076f27cffSMatthias Ringwald             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
288176f27cffSMatthias Ringwald             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
288276f27cffSMatthias Ringwald             return;
288376f27cffSMatthias Ringwald         }
2884da886c03S[email protected] 
2885a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
2886da886c03S[email protected]         if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){
2887da886c03S[email protected]             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
2888da886c03S[email protected] 
2889c37a3166S[email protected]             uint16_t connection_interval_min = connection->le_conn_interval_min;
2890c37a3166S[email protected]             connection->le_conn_interval_min = 0;
2891c37a3166S[email protected]             hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min,
2892c37a3166S[email protected]                 connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout,
2893c37a3166S[email protected]                 0x0000, 0xffff);
2894c37a3166S[email protected]         }
2895c37a3166S[email protected] #endif
2896dbe1a790S[email protected]     }
2897c7e0c5f6Smatthias.ringwald 
289805ae8de3SMatthias Ringwald     hci_connection_t * connection;
28993a9fb326S[email protected]     switch (hci_stack->state){
29003429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
290174b323a9SMatthias Ringwald             hci_initializing_run();
29023429f56bSmatthias.ringwald             break;
2903c7e0c5f6Smatthias.ringwald 
2904c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
2905c7e0c5f6Smatthias.ringwald 
29069da54300S[email protected]             log_info("HCI_STATE_HALTING");
29079956955bSMatthias Ringwald 
29089956955bSMatthias Ringwald             // free whitelist entries
2909a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
2910d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
29119956955bSMatthias Ringwald             {
2912665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_t lit;
2913665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2914665d90f2SMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&lit)){
2915665d90f2SMatthias Ringwald                     whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
2916665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry);
29179956955bSMatthias Ringwald                     btstack_memory_whitelist_entry_free(entry);
29189956955bSMatthias Ringwald                 }
29199956955bSMatthias Ringwald             }
29209956955bSMatthias Ringwald #endif
2921d70217a2SMatthias Ringwald #endif
2922c7e0c5f6Smatthias.ringwald             // close all open connections
29233a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
2924c7e0c5f6Smatthias.ringwald             if (connection){
2925711e6c80SMatthias Ringwald                 hci_con_handle_t con_handle = (uint16_t) connection->con_handle;
2926d94d3cafS[email protected]                 if (!hci_can_send_command_packet_now()) return;
292732ab9390Smatthias.ringwald 
29288837e9efSMatthias Ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle);
2929c7e0c5f6Smatthias.ringwald 
29308837e9efSMatthias Ringwald                 // cancel all l2cap connections right away instead of waiting for disconnection complete event ...
29318837e9efSMatthias Ringwald                 hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host
29328837e9efSMatthias Ringwald 
29338837e9efSMatthias Ringwald                 // ... which would be ignored anyway as we shutdown (free) the connection now
2934c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
29358837e9efSMatthias Ringwald 
29368837e9efSMatthias Ringwald                 // finally, send the disconnect command
29378837e9efSMatthias Ringwald                 hci_send_cmd(&hci_disconnect, con_handle, 0x13);  // remote closed connection
2938c7e0c5f6Smatthias.ringwald                 return;
2939c7e0c5f6Smatthias.ringwald             }
29409da54300S[email protected]             log_info("HCI_STATE_HALTING, calling off");
2941c7e0c5f6Smatthias.ringwald 
294272ea5239Smatthias.ringwald             // switch mode
2943c7e0c5f6Smatthias.ringwald             hci_power_control_off();
29449418f9c9Smatthias.ringwald 
29459da54300S[email protected]             log_info("HCI_STATE_HALTING, emitting state");
294672ea5239Smatthias.ringwald             hci_emit_state();
29479da54300S[email protected]             log_info("HCI_STATE_HALTING, done");
294872ea5239Smatthias.ringwald             break;
2949c7e0c5f6Smatthias.ringwald 
295072ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
29513a9fb326S[email protected]             switch(hci_stack->substate) {
295274b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_DISCONNECT:
29539da54300S[email protected]                     log_info("HCI_STATE_FALLING_ASLEEP");
295472ea5239Smatthias.ringwald                     // close all open connections
29553a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
295666da7044Smatthias.ringwald 
2957423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
295866da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
2959d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
296066da7044Smatthias.ringwald                         connection = NULL;
296166da7044Smatthias.ringwald                     }
296266da7044Smatthias.ringwald #endif
296372ea5239Smatthias.ringwald                     if (connection){
296432ab9390Smatthias.ringwald 
296572ea5239Smatthias.ringwald                         // send disconnect
2966d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
296732ab9390Smatthias.ringwald 
29689da54300S[email protected]                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
29696ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
297072ea5239Smatthias.ringwald 
297172ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
297272ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
297372ea5239Smatthias.ringwald                         return;
297472ea5239Smatthias.ringwald                     }
297572ea5239Smatthias.ringwald 
297692368cd3S[email protected]                     if (hci_classic_supported()){
297789db417bSmatthias.ringwald                         // disable page and inquiry scan
2978d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
297932ab9390Smatthias.ringwald 
29809da54300S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans");
29813a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
298289db417bSmatthias.ringwald 
298389db417bSmatthias.ringwald                         // continue in next sub state
298474b323a9SMatthias Ringwald                         hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE;
298589db417bSmatthias.ringwald                         break;
298692368cd3S[email protected]                     }
2987202c8a4cSMatthias Ringwald                     // no break - fall through for ble-only chips
298892368cd3S[email protected] 
298974b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_COMPLETE:
29909da54300S[email protected]                     log_info("HCI_STATE_HALTING, calling sleep");
2991423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
299228171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
2993d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
299428171530Smatthias.ringwald                         // SLEEP MODE reached
29953a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
299628171530Smatthias.ringwald                         hci_emit_state();
299728171530Smatthias.ringwald                         break;
299828171530Smatthias.ringwald                     }
299928171530Smatthias.ringwald #endif
300072ea5239Smatthias.ringwald                     // switch mode
30013a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
3002c7e0c5f6Smatthias.ringwald                     hci_emit_state();
300328171530Smatthias.ringwald                     break;
300428171530Smatthias.ringwald 
300589db417bSmatthias.ringwald                 default:
300689db417bSmatthias.ringwald                     break;
300789db417bSmatthias.ringwald             }
3008c7e0c5f6Smatthias.ringwald             break;
3009c7e0c5f6Smatthias.ringwald 
30103429f56bSmatthias.ringwald         default:
30113429f56bSmatthias.ringwald             break;
30121f504dbdSmatthias.ringwald     }
30133429f56bSmatthias.ringwald }
301416833f0aSmatthias.ringwald 
301531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
301635454696SMatthias Ringwald     // house-keeping
301735454696SMatthias Ringwald 
301835454696SMatthias Ringwald     if (IS_COMMAND(packet, hci_write_loopback_mode)){
301935454696SMatthias Ringwald         hci_stack->loopback_mode = packet[3];
302035454696SMatthias Ringwald     }
302135454696SMatthias Ringwald 
302235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
3023c8e4258aSmatthias.ringwald     bd_addr_t addr;
3024c8e4258aSmatthias.ringwald     hci_connection_t * conn;
3025c8e4258aSmatthias.ringwald 
3026c8e4258aSmatthias.ringwald     // create_connection?
3027c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
3028724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
30299da54300S[email protected]         log_info("Create_connection to %s", bd_addr_to_str(addr));
3030c8e4258aSmatthias.ringwald 
30312e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
3032ad83dc6aS[email protected]         if (!conn){
303396a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
303417f1ba2aSmatthias.ringwald             if (!conn){
303517f1ba2aSmatthias.ringwald                 // notify client that alloc failed
30362deddeceSMatthias Ringwald                 hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
303717f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
303817f1ba2aSmatthias.ringwald             }
3039ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
3040ad83dc6aS[email protected]         }
3041ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
3042ad83dc6aS[email protected]         switch (conn->state){
3043ad83dc6aS[email protected]             // if connection active exists
3044ad83dc6aS[email protected]             case OPEN:
304562bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
3046274dad91SMatthias Ringwald                 hci_emit_connection_complete(addr, conn->con_handle, 0);
304762bda3fbS[email protected]                 return 0;
3048ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
3049ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
3050ad83dc6aS[email protected]                 break;
3051ad83dc6aS[email protected]             default:
3052ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
3053ad83dc6aS[email protected]                 return 0;
3054ad83dc6aS[email protected]         }
3055c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
3056c8e4258aSmatthias.ringwald     }
305735454696SMatthias Ringwald 
30587fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
30597fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
30607fde4af9Smatthias.ringwald     }
30617fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
30627fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
30637fde4af9Smatthias.ringwald     }
30647fde4af9Smatthias.ringwald 
30658ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
3066a98592bcSMatthias Ringwald         if (hci_stack->link_key_db){
3067724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
3068a98592bcSMatthias Ringwald             hci_stack->link_key_db->delete_link_key(addr);
30698ef73945Smatthias.ringwald         }
30708ef73945Smatthias.ringwald     }
3071c8e4258aSmatthias.ringwald 
30726724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
30736724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
3074724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
30752e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
30766724cd9eS[email protected]         if (conn){
30776724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
30786724cd9eS[email protected]         }
30796724cd9eS[email protected]     }
30806724cd9eS[email protected] 
30816724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
30826724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
30836724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
30846724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
3085724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
30862e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
30876724cd9eS[email protected]         if (conn){
30886724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
30896724cd9eS[email protected]         }
30906724cd9eS[email protected]     }
309135454696SMatthias Ringwald #endif
30924b3e1e19SMatthias Ringwald 
3093a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3094d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL
309569a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
3096b95a5a35SMatthias Ringwald         hci_stack->le_random_address_set = 1;
3097b95a5a35SMatthias Ringwald         reverse_bd_addr(&packet[3], hci_stack->le_random_address);
3098d70217a2SMatthias Ringwald     }
3099171293d3SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_set_advertise_enable)){
3100171293d3SMatthias Ringwald         hci_stack->le_advertisements_active = packet[3];
3101171293d3SMatthias Ringwald     }
3102d70217a2SMatthias Ringwald #endif
3103d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
3104b04dfa37SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_create_connection)){
3105b04dfa37SMatthias Ringwald         // white list used?
3106b04dfa37SMatthias Ringwald         uint8_t initiator_filter_policy = packet[7];
3107b04dfa37SMatthias Ringwald         switch (initiator_filter_policy){
3108b04dfa37SMatthias Ringwald             case 0:
3109b04dfa37SMatthias Ringwald                 // whitelist not used
3110b04dfa37SMatthias Ringwald                 hci_stack->le_connecting_state = LE_CONNECTING_DIRECT;
3111b04dfa37SMatthias Ringwald                 break;
3112b04dfa37SMatthias Ringwald             case 1:
3113b04dfa37SMatthias Ringwald                 hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST;
3114b04dfa37SMatthias Ringwald                 break;
3115b04dfa37SMatthias Ringwald             default:
3116b04dfa37SMatthias Ringwald                 log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy);
3117b04dfa37SMatthias Ringwald                 break;
3118b04dfa37SMatthias Ringwald         }
3119b04dfa37SMatthias Ringwald     }
3120b04dfa37SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_create_connection_cancel)){
3121b04dfa37SMatthias Ringwald         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
3122b04dfa37SMatthias Ringwald     }
312369a97523S[email protected] #endif
3124d70217a2SMatthias Ringwald #endif
312569a97523S[email protected] 
31263a9fb326S[email protected]     hci_stack->num_cmd_packets--;
31275bb5bc3eS[email protected] 
31285bb5bc3eS[email protected]     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size);
31296b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
31306b4af23dS[email protected] 
3131d051460cS[email protected]     // release packet buffer for synchronous transport implementations
3132c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
31336b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
31346b4af23dS[email protected]     }
31356b4af23dS[email protected] 
31366b4af23dS[email protected]     return err;
313731452debSmatthias.ringwald }
31388adf0ddaSmatthias.ringwald 
31392bd8b7e7S[email protected] // disconnect because of security block
31402bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
31412bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
31422bd8b7e7S[email protected]     if (!connection) return;
31432bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
31442bd8b7e7S[email protected] }
31452bd8b7e7S[email protected] 
31462bd8b7e7S[email protected] 
3147dbe1a790S[email protected] // Configure Secure Simple Pairing
3148dbe1a790S[email protected] 
314935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
315035454696SMatthias Ringwald 
3151dbe1a790S[email protected] // enable will enable SSP during init
315215a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){
31533a9fb326S[email protected]     hci_stack->ssp_enable = enable;
3154dbe1a790S[email protected] }
3155dbe1a790S[email protected] 
315695d71764SMatthias Ringwald static int hci_local_ssp_activated(void){
315715a95bd5SMatthias Ringwald     return gap_ssp_supported() && hci_stack->ssp_enable;
31582bd8b7e7S[email protected] }
31592bd8b7e7S[email protected] 
3160dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
316115a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){
31623a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
3163dbe1a790S[email protected] }
316415a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){
31653a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
3166dbe1a790S[email protected] }
3167dbe1a790S[email protected] 
3168dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
316915a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){
31703a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
3171dbe1a790S[email protected] }
317235454696SMatthias Ringwald #endif
3173dbe1a790S[email protected] 
317494be1a66SMatthias Ringwald // va_list part of hci_send_cmd
317594be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){
3176d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()){
31779d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
31789d14b626S[email protected]         return 0;
31799d14b626S[email protected]     }
31809d14b626S[email protected] 
31815127cc62S[email protected]     // for HCI INITIALIZATION
31829da54300S[email protected]     // log_info("hci_send_cmd: opcode %04x", cmd->opcode);
31835127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
31845127cc62S[email protected] 
31859d14b626S[email protected]     hci_reserve_packet_buffer();
31869d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
318794be1a66SMatthias Ringwald     uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr);
318894be1a66SMatthias Ringwald     return hci_send_cmd_packet(packet, size);
318994be1a66SMatthias Ringwald }
31909d14b626S[email protected] 
319194be1a66SMatthias Ringwald /**
319294be1a66SMatthias Ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
319394be1a66SMatthias Ringwald  */
319494be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
31951cd208adSmatthias.ringwald     va_list argptr;
31961cd208adSmatthias.ringwald     va_start(argptr, cmd);
319794be1a66SMatthias Ringwald     int res = hci_send_cmd_va_arg(cmd, argptr);
31981cd208adSmatthias.ringwald     va_end(argptr);
319994be1a66SMatthias Ringwald     return res;
320093b8dc03Smatthias.ringwald }
3201c8e4258aSmatthias.ringwald 
3202ee091cf1Smatthias.ringwald // Create various non-HCI events.
3203ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
3204ee091cf1Smatthias.ringwald 
3205d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){
3206fb37a842SMatthias Ringwald     // dump packet
3207d6b06661SMatthias Ringwald     if (dump) {
3208300c1ba4SMatthias Ringwald         hci_dump_packet( HCI_EVENT_PACKET, 0, event, size);
3209d6b06661SMatthias Ringwald     }
32101ef6bb52SMatthias Ringwald 
3211fb37a842SMatthias Ringwald     // dispatch to all event handlers
32121ef6bb52SMatthias Ringwald     btstack_linked_list_iterator_t it;
32131ef6bb52SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers);
32141ef6bb52SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
32151ef6bb52SMatthias Ringwald         btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
3216d9a7306aSMatthias Ringwald         entry->callback(HCI_EVENT_PACKET, 0, event, size);
32171ef6bb52SMatthias Ringwald     }
3218d6b06661SMatthias Ringwald }
3219d6b06661SMatthias Ringwald 
3220d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){
3221fb37a842SMatthias Ringwald     if (!hci_stack->acl_packet_handler) return;
32223d50b4baSMatthias Ringwald     hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size);
3223d6b06661SMatthias Ringwald }
3224d6b06661SMatthias Ringwald 
322535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
3226701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){
32273bc639ceSMatthias Ringwald     // notify SCO sender if waiting
3228701e3307SMatthias Ringwald     if (!hci_stack->sco_waiting_for_can_send_now) return;
3229701e3307SMatthias Ringwald     if (hci_can_send_sco_packet_now()){
32303bc639ceSMatthias Ringwald         hci_stack->sco_waiting_for_can_send_now = 0;
3231701e3307SMatthias Ringwald         uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 };
3232701e3307SMatthias Ringwald         hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event));
32333d50b4baSMatthias Ringwald         hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
32343bc639ceSMatthias Ringwald     }
32353bc639ceSMatthias Ringwald }
323635454696SMatthias Ringwald #endif
32373bc639ceSMatthias Ringwald 
323871de195eSMatthias Ringwald void hci_emit_state(void){
32393a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
3240425d1371Smatthias.ringwald     uint8_t event[3];
324180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
3242425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
32433a9fb326S[email protected]     event[2] = hci_stack->state;
3244d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3245c8e4258aSmatthias.ringwald }
3246c8e4258aSmatthias.ringwald 
324735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
32482deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){
3249425d1371Smatthias.ringwald     uint8_t event[13];
3250c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
3251425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
325217f1ba2aSmatthias.ringwald     event[2] = status;
32532deddeceSMatthias Ringwald     little_endian_store_16(event, 3, con_handle);
32542deddeceSMatthias Ringwald     reverse_bd_addr(address, &event[5]);
3255c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
3256c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
3257d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3258c8e4258aSmatthias.ringwald }
325952db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
326052db98b2SMatthias Ringwald     if (disable_l2cap_timeouts) return;
326152db98b2SMatthias Ringwald     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
326252db98b2SMatthias Ringwald     uint8_t event[4];
326352db98b2SMatthias Ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
326452db98b2SMatthias Ringwald     event[1] = sizeof(event) - 2;
326552db98b2SMatthias Ringwald     little_endian_store_16(event, 2, conn->con_handle);
326652db98b2SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
326752db98b2SMatthias Ringwald }
326835454696SMatthias Ringwald #endif
3269c8e4258aSmatthias.ringwald 
327035454696SMatthias Ringwald #ifdef ENABLE_BLE
3271d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
3272fc64f94aSMatthias Ringwald static void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){
32734f3229d8S[email protected]     uint8_t event[21];
32744f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
32754f3229d8S[email protected]     event[1] = sizeof(event) - 2;
32764f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
32774f3229d8S[email protected]     event[3] = status;
3278fc64f94aSMatthias Ringwald     little_endian_store_16(event, 4, con_handle);
32794f3229d8S[email protected]     event[6] = 0; // TODO: role
32806e2e9a6bS[email protected]     event[7] = address_type;
3281724d70a2SMatthias Ringwald     reverse_bd_addr(address, &event[8]);
3282f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, 0); // interval
3283f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 16, 0); // latency
3284f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 18, 0); // supervision timeout
32854f3229d8S[email protected]     event[20] = 0; // master clock accuracy
3286d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
32874f3229d8S[email protected] }
328835454696SMatthias Ringwald #endif
3289d70217a2SMatthias Ringwald #endif
32904f3229d8S[email protected] 
3291fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){
3292425d1371Smatthias.ringwald     uint8_t event[6];
32933c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
3294e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
32953c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
3296fc64f94aSMatthias Ringwald     little_endian_store_16(event, 3, con_handle);
32973c4d4b90Smatthias.ringwald     event[5] = reason;
3298d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
32993c4d4b90Smatthias.ringwald }
33003c4d4b90Smatthias.ringwald 
3301b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){
3302e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
3303425d1371Smatthias.ringwald     uint8_t event[3];
330480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
3305425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
330643bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
3307d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
330843bfb1bdSmatthias.ringwald }
3309038bc64cSmatthias.ringwald 
3310b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){
3311e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
3312425d1371Smatthias.ringwald     uint8_t event[2];
331380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
3314425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
3315d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3316038bc64cSmatthias.ringwald }
33171b0e3922Smatthias.ringwald 
331835454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){
331935454696SMatthias Ringwald     log_info("hci_emit_dedicated_bonding_result %u ", status);
332035454696SMatthias Ringwald     uint8_t event[9];
332135454696SMatthias Ringwald     int pos = 0;
332235454696SMatthias Ringwald     event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED;
332335454696SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
332435454696SMatthias Ringwald     event[pos++] = status;
332535454696SMatthias Ringwald     reverse_bd_addr(address, &event[pos]);
3326d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3327381fbed8Smatthias.ringwald }
3328458bf4e8S[email protected] 
332935454696SMatthias Ringwald 
333035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
333135454696SMatthias Ringwald 
3332b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
3333df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
3334a00031e2S[email protected]     uint8_t event[5];
3335e00caf9cS[email protected]     int pos = 0;
33365611a760SMatthias Ringwald     event[pos++] = GAP_EVENT_SECURITY_LEVEL;
3337e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
3338f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
3339e00caf9cS[email protected]     pos += 2;
3340e00caf9cS[email protected]     event[pos++] = level;
3341d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3342e00caf9cS[email protected] }
3343e00caf9cS[email protected] 
334435454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
334535454696SMatthias Ringwald     if (!connection) return LEVEL_0;
334635454696SMatthias Ringwald     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
334735454696SMatthias Ringwald     return gap_security_level_for_link_key_type(connection->link_key_type);
334835454696SMatthias Ringwald }
334935454696SMatthias Ringwald 
335035454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){
335135454696SMatthias Ringwald     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
335235454696SMatthias Ringwald     uint8_t event[3];
335335454696SMatthias Ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
335435454696SMatthias Ringwald     event[1] = sizeof(event) - 2;
335535454696SMatthias Ringwald     event[2] = enabled;
3356d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3357ad83dc6aS[email protected] }
3358ad83dc6aS[email protected] 
335906b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC
336098a2fd1cSMatthias Ringwald // query if remote side supports eSCO
3361073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){
336298a2fd1cSMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
336398a2fd1cSMatthias Ringwald     if (!connection) return 0;
336498a2fd1cSMatthias Ringwald     return connection->remote_supported_feature_eSCO;
336598a2fd1cSMatthias Ringwald }
336698a2fd1cSMatthias Ringwald 
33672bd8b7e7S[email protected] // query if remote side supports SSP
33682bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
33692bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
33702bd8b7e7S[email protected]     if (!connection) return 0;
33712bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
33722bd8b7e7S[email protected] }
33732bd8b7e7S[email protected] 
337415a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){
3375df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
3376df3354fcS[email protected] }
337706b9e820SMatthias Ringwald #endif
3378df3354fcS[email protected] 
3379458bf4e8S[email protected] // GAP API
3380458bf4e8S[email protected] /**
3381458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
3382458bf4e8S[email protected]  * @praram enabled
3383458bf4e8S[email protected]  */
33844c57c146S[email protected] void gap_set_bondable_mode(int enable){
33853a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
3386458bf4e8S[email protected] }
33874ef6443cSMatthias Ringwald /**
33884ef6443cSMatthias Ringwald  * @brief Get bondable mode.
33894ef6443cSMatthias Ringwald  * @return 1 if bondable
33904ef6443cSMatthias Ringwald  */
33914ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){
33924ef6443cSMatthias Ringwald     return hci_stack->bondable;
33934ef6443cSMatthias Ringwald }
3394cb230b9dS[email protected] 
3395cb230b9dS[email protected] /**
339634d2123cS[email protected]  * @brief map link keys to security levels
3397cb230b9dS[email protected]  */
339834d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
339934d2123cS[email protected]     switch (link_key_type){
34003c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
34013c68dfa9S[email protected]             return LEVEL_4;
34023c68dfa9S[email protected]         case COMBINATION_KEY:
34033c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
34043c68dfa9S[email protected]             return LEVEL_3;
34053c68dfa9S[email protected]         default:
34063c68dfa9S[email protected]             return LEVEL_2;
34073c68dfa9S[email protected]     }
3408cb230b9dS[email protected] }
3409cb230b9dS[email protected] 
3410106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
34115127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
3412106d6d11S[email protected]     return level > LEVEL_2;
3413106d6d11S[email protected] }
3414106d6d11S[email protected] 
341534d2123cS[email protected] /**
341634d2123cS[email protected]  * @brief get current security level
341734d2123cS[email protected]  */
341834d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
341934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
342034d2123cS[email protected]     if (!connection) return LEVEL_0;
342134d2123cS[email protected]     return gap_security_level_for_connection(connection);
342234d2123cS[email protected] }
342334d2123cS[email protected] 
3424cb230b9dS[email protected] /**
3425cb230b9dS[email protected]  * @brief request connection to device to
3426cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
3427cb230b9dS[email protected]  */
342834d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
342934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
343034d2123cS[email protected]     if (!connection){
3431a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
343234d2123cS[email protected]         return;
343334d2123cS[email protected]     }
343434d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
343534d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
343634d2123cS[email protected]     if (current_level >= requested_level){
3437a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
343834d2123cS[email protected]         return;
343934d2123cS[email protected]     }
3440a00031e2S[email protected] 
344134d2123cS[email protected]     connection->requested_security_level = requested_level;
3442a00031e2S[email protected] 
344325bf5872S[email protected] #if 0
344425bf5872S[email protected]     // sending encryption request without a link key results in an error.
344525bf5872S[email protected]     // TODO: figure out how to use it properly
344625bf5872S[email protected] 
3447fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
3448a98592bcSMatthias Ringwald     if (hci_stack->link_key_db){
3449a00031e2S[email protected]         link_key_type_t link_key_type;
3450a00031e2S[email protected]         link_key_t      link_key;
3451a98592bcSMatthias Ringwald         if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){
3452a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
3453a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
3454a00031e2S[email protected]                 return;
3455a00031e2S[email protected]             }
3456a00031e2S[email protected]         }
3457a00031e2S[email protected]     }
345825bf5872S[email protected] #endif
3459a00031e2S[email protected] 
34601eb2563eS[email protected]     // try to authenticate connection
34611eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
3462e80b2cf9S[email protected]     hci_run();
3463e00caf9cS[email protected] }
3464ad83dc6aS[email protected] 
3465ad83dc6aS[email protected] /**
3466ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
3467ad83dc6aS[email protected]  * @param device
3468ad83dc6aS[email protected]  * @param request MITM protection
3469ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
3470ad83dc6aS[email protected]  */
3471ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
3472ad83dc6aS[email protected] 
3473ad83dc6aS[email protected]     // create connection state machine
347496a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
3475ad83dc6aS[email protected] 
3476ad83dc6aS[email protected]     if (!connection){
3477ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
3478ad83dc6aS[email protected]     }
3479ad83dc6aS[email protected] 
3480ad83dc6aS[email protected]     // delete linkn key
348115a95bd5SMatthias Ringwald     gap_drop_link_key_for_bd_addr(device);
3482ad83dc6aS[email protected] 
3483ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
3484ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
3485ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
3486f04a0c31SMatthias Ringwald     log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level);
3487ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
3488ad83dc6aS[email protected] 
3489ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
3490ad83dc6aS[email protected] 
3491ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
3492ad83dc6aS[email protected]     // handle: authentication failure
3493ad83dc6aS[email protected]     // handle: disconnect on done
3494ad83dc6aS[email protected] 
3495ad83dc6aS[email protected]     hci_run();
3496ad83dc6aS[email protected] 
3497ad83dc6aS[email protected]     return 0;
3498ad83dc6aS[email protected] }
349935454696SMatthias Ringwald #endif
35008e618f72S[email protected] 
35018e618f72S[email protected] void gap_set_local_name(const char * local_name){
35028e618f72S[email protected]     hci_stack->local_name = local_name;
35038e618f72S[email protected] }
35048e618f72S[email protected] 
350535454696SMatthias Ringwald 
350635454696SMatthias Ringwald #ifdef ENABLE_BLE
350735454696SMatthias Ringwald 
3508d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
3509d8e8f12aSMatthias Ringwald void gap_start_scan(void){
3510d8e8f12aSMatthias Ringwald     if (hci_stack->le_scanning_state == LE_SCANNING) return;
35117bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
35127bdc6798S[email protected]     hci_run();
35137bdc6798S[email protected] }
35148e618f72S[email protected] 
3515d8e8f12aSMatthias Ringwald void gap_stop_scan(void){
3516d8e8f12aSMatthias Ringwald     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return;
35177bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
35187bdc6798S[email protected]     hci_run();
35197bdc6798S[email protected] }
35204f3229d8S[email protected] 
3521d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
3522ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
3523ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
3524ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
3525ef11999fSmatthias.ringwald     hci_run();
3526ef11999fSmatthias.ringwald }
35274f3229d8S[email protected] 
3528d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){
35294f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
35304f3229d8S[email protected]     if (!conn){
3531d8e8f12aSMatthias Ringwald         log_info("gap_connect: no connection exists yet, creating context");
35322e77e513S[email protected]         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
35334f3229d8S[email protected]         if (!conn){
35344f3229d8S[email protected]             // notify client that alloc failed
35356e2e9a6bS[email protected]             hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
3536d8e8f12aSMatthias Ringwald             log_info("gap_connect: failed to alloc hci_connection_t");
3537472a5742SMatthias Ringwald             return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller
35384f3229d8S[email protected]         }
35394f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
3540d8e8f12aSMatthias Ringwald         log_info("gap_connect: send create connection next");
3541564fca32S[email protected]         hci_run();
3542616edd56SMatthias Ringwald         return 0;
35434f3229d8S[email protected]     }
35440bf6344aS[email protected] 
35450bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
35460bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
35470bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
35482e77e513S[email protected]         hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED);
3549d8e8f12aSMatthias Ringwald         log_error("gap_connect: classic connection or connect is already being created");
3550616edd56SMatthias Ringwald         return GATT_CLIENT_IN_WRONG_STATE;
35510bf6344aS[email protected]     }
35520bf6344aS[email protected] 
3553d8e8f12aSMatthias Ringwald     log_info("gap_connect: context exists with state %u", conn->state);
35542e77e513S[email protected]     hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0);
35554f3229d8S[email protected]     hci_run();
3556616edd56SMatthias Ringwald     return 0;
35574f3229d8S[email protected] }
35584f3229d8S[email protected] 
35597851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
3560d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){
3561665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
3562665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
35630bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
35640bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
35650bf6344aS[email protected]         switch (conn->state){
3566a6725849S[email protected]             case SEND_CREATE_CONNECTION:
35677851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
35687851196eSmatthias.ringwald                 return conn;
35697851196eSmatthias.ringwald             default:
35707851196eSmatthias.ringwald                 break;
35717851196eSmatthias.ringwald         };
35727851196eSmatthias.ringwald     }
35737851196eSmatthias.ringwald     return NULL;
35747851196eSmatthias.ringwald }
35757851196eSmatthias.ringwald 
3576d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){
3577d8e8f12aSMatthias Ringwald     hci_connection_t * conn = gap_get_outgoing_connection();
3578616edd56SMatthias Ringwald     if (!conn) return 0;
35797851196eSmatthias.ringwald     switch (conn->state){
35807851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
35817851196eSmatthias.ringwald             // skip sending create connection and emit event instead
35822e77e513S[email protected]             hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
3583665d90f2SMatthias Ringwald             btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
35847851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
35850bf6344aS[email protected]             break;
3586a6725849S[email protected]         case SENT_CREATE_CONNECTION:
35877851196eSmatthias.ringwald             // request to send cancel connection
35880bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
35890bf6344aS[email protected]             hci_run();
35900bf6344aS[email protected]             break;
35910bf6344aS[email protected]         default:
35920bf6344aS[email protected]             break;
35930bf6344aS[email protected]     }
3594616edd56SMatthias Ringwald     return 0;
3595e31f89a7S[email protected] }
3596d70217a2SMatthias Ringwald #endif
35974f3229d8S[email protected] 
3598c37a3166S[email protected] /**
3599c37a3166S[email protected]  * @brief Updates the connection parameters for a given LE connection
3600c37a3166S[email protected]  * @param handle
3601c37a3166S[email protected]  * @param conn_interval_min (unit: 1.25ms)
3602c37a3166S[email protected]  * @param conn_interval_max (unit: 1.25ms)
3603c37a3166S[email protected]  * @param conn_latency
3604c37a3166S[email protected]  * @param supervision_timeout (unit: 10ms)
3605c37a3166S[email protected]  * @returns 0 if ok
3606c37a3166S[email protected]  */
3607c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
3608c37a3166S[email protected]     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
3609c37a3166S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3610c37a3166S[email protected]     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3611c37a3166S[email protected]     connection->le_conn_interval_min = conn_interval_min;
3612c37a3166S[email protected]     connection->le_conn_interval_max = conn_interval_max;
3613c37a3166S[email protected]     connection->le_conn_latency = conn_latency;
3614c37a3166S[email protected]     connection->le_supervision_timeout = supervision_timeout;
361584cf6d83SMatthias Ringwald     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
3616cfc59f1bSMatthias Ringwald     hci_run();
3617c37a3166S[email protected]     return 0;
3618c37a3166S[email protected] }
3619c37a3166S[email protected] 
362045c102fdSMatthias Ringwald /**
3621b68d7bc3SMatthias Ringwald  * @brief Request an update of the connection parameter for a given LE connection
3622b68d7bc3SMatthias Ringwald  * @param handle
3623b68d7bc3SMatthias Ringwald  * @param conn_interval_min (unit: 1.25ms)
3624b68d7bc3SMatthias Ringwald  * @param conn_interval_max (unit: 1.25ms)
3625b68d7bc3SMatthias Ringwald  * @param conn_latency
3626b68d7bc3SMatthias Ringwald  * @param supervision_timeout (unit: 10ms)
3627b68d7bc3SMatthias Ringwald  * @returns 0 if ok
3628b68d7bc3SMatthias Ringwald  */
3629b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
3630b68d7bc3SMatthias Ringwald     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
3631b68d7bc3SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3632b68d7bc3SMatthias Ringwald     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3633b68d7bc3SMatthias Ringwald     connection->le_conn_interval_min = conn_interval_min;
3634b68d7bc3SMatthias Ringwald     connection->le_conn_interval_max = conn_interval_max;
3635b68d7bc3SMatthias Ringwald     connection->le_conn_latency = conn_latency;
3636b68d7bc3SMatthias Ringwald     connection->le_supervision_timeout = supervision_timeout;
3637b68d7bc3SMatthias Ringwald     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST;
3638b68d7bc3SMatthias Ringwald     hci_run();
3639b68d7bc3SMatthias Ringwald     return 0;
3640b68d7bc3SMatthias Ringwald }
3641b68d7bc3SMatthias Ringwald 
3642d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL
3643d70217a2SMatthias Ringwald 
3644501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){
3645501f56b3SMatthias Ringwald     // disable advertisements before updating adv, scan data, or adv params
3646501f56b3SMatthias Ringwald     if (hci_stack->le_advertisements_active){
3647501f56b3SMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE;
3648501f56b3SMatthias Ringwald     }
3649501f56b3SMatthias Ringwald     hci_run();
3650501f56b3SMatthias Ringwald }
3651501f56b3SMatthias Ringwald 
3652b68d7bc3SMatthias Ringwald /**
365345c102fdSMatthias Ringwald  * @brief Set Advertisement Data
365445c102fdSMatthias Ringwald  * @param advertising_data_length
365545c102fdSMatthias Ringwald  * @param advertising_data (max 31 octets)
365645c102fdSMatthias Ringwald  * @note data is not copied, pointer has to stay valid
365745c102fdSMatthias Ringwald  */
365845c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){
365945c102fdSMatthias Ringwald     hci_stack->le_advertisements_data_len = advertising_data_length;
366045c102fdSMatthias Ringwald     hci_stack->le_advertisements_data = advertising_data;
3661501f56b3SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA;
3662501f56b3SMatthias Ringwald     gap_advertisments_changed();
366345c102fdSMatthias Ringwald }
3664501f56b3SMatthias Ringwald 
3665501f56b3SMatthias Ringwald /**
3666501f56b3SMatthias Ringwald  * @brief Set Scan Response Data
3667501f56b3SMatthias Ringwald  * @param advertising_data_length
3668501f56b3SMatthias Ringwald  * @param advertising_data (max 31 octets)
3669501f56b3SMatthias Ringwald  * @note data is not copied, pointer has to stay valid
3670501f56b3SMatthias Ringwald  */
3671501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){
3672501f56b3SMatthias Ringwald     hci_stack->le_scan_response_data_len = scan_response_data_length;
3673501f56b3SMatthias Ringwald     hci_stack->le_scan_response_data = scan_response_data;
3674501f56b3SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA;
3675501f56b3SMatthias Ringwald     gap_advertisments_changed();
367645c102fdSMatthias Ringwald }
367745c102fdSMatthias Ringwald 
367845c102fdSMatthias Ringwald /**
367945c102fdSMatthias Ringwald  * @brief Set Advertisement Parameters
368045c102fdSMatthias Ringwald  * @param adv_int_min
368145c102fdSMatthias Ringwald  * @param adv_int_max
368245c102fdSMatthias Ringwald  * @param adv_type
368345c102fdSMatthias Ringwald  * @param direct_address_type
368445c102fdSMatthias Ringwald  * @param direct_address
368545c102fdSMatthias Ringwald  * @param channel_map
368645c102fdSMatthias Ringwald  * @param filter_policy
368745c102fdSMatthias Ringwald  *
368845c102fdSMatthias Ringwald  * @note internal use. use gap_advertisements_set_params from gap_le.h instead.
368945c102fdSMatthias Ringwald  */
369045c102fdSMatthias Ringwald  void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
3691b95a5a35SMatthias Ringwald     uint8_t direct_address_typ, bd_addr_t direct_address,
369245c102fdSMatthias Ringwald     uint8_t channel_map, uint8_t filter_policy) {
369345c102fdSMatthias Ringwald 
369445c102fdSMatthias Ringwald     hci_stack->le_advertisements_interval_min = adv_int_min;
369545c102fdSMatthias Ringwald     hci_stack->le_advertisements_interval_max = adv_int_max;
369645c102fdSMatthias Ringwald     hci_stack->le_advertisements_type = adv_type;
369745c102fdSMatthias Ringwald     hci_stack->le_advertisements_direct_address_type = direct_address_typ;
369845c102fdSMatthias Ringwald     hci_stack->le_advertisements_channel_map = channel_map;
369945c102fdSMatthias Ringwald     hci_stack->le_advertisements_filter_policy = filter_policy;
370045c102fdSMatthias Ringwald     memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6);
370145c102fdSMatthias Ringwald 
370245c102fdSMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS;
3703501f56b3SMatthias Ringwald     gap_advertisments_changed();
370445c102fdSMatthias Ringwald  }
370545c102fdSMatthias Ringwald 
370645c102fdSMatthias Ringwald /**
370745c102fdSMatthias Ringwald  * @brief Enable/Disable Advertisements
370845c102fdSMatthias Ringwald  * @param enabled
370945c102fdSMatthias Ringwald  */
371045c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){
371145c102fdSMatthias Ringwald     hci_stack->le_advertisements_enabled = enabled;
371245c102fdSMatthias Ringwald     if (enabled && !hci_stack->le_advertisements_active){
371345c102fdSMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
371445c102fdSMatthias Ringwald     }
371545c102fdSMatthias Ringwald     if (!enabled && hci_stack->le_advertisements_active){
371645c102fdSMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE;
371745c102fdSMatthias Ringwald     }
3718cfc59f1bSMatthias Ringwald     hci_run();
371945c102fdSMatthias Ringwald }
372045c102fdSMatthias Ringwald 
372135454696SMatthias Ringwald #endif
372206e5cf96SMatthias Ringwald 
372306e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){
372406e5cf96SMatthias Ringwald     log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type);
372506e5cf96SMatthias Ringwald     if (own_address_type == hci_stack->le_own_addr_type) return;
372606e5cf96SMatthias Ringwald     hci_stack->le_own_addr_type = own_address_type;
372706e5cf96SMatthias Ringwald 
372864068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL
372906e5cf96SMatthias Ringwald     // update advertisement parameters, too
373006e5cf96SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS;
373106e5cf96SMatthias Ringwald     gap_advertisments_changed();
373264068776SMatthias Ringwald #endif
373364068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
373406e5cf96SMatthias Ringwald     // note: we don't update scan parameters or modify ongoing connection attempts
373564068776SMatthias Ringwald #endif
373606e5cf96SMatthias Ringwald }
373706e5cf96SMatthias Ringwald 
3738d70217a2SMatthias Ringwald #endif
373945c102fdSMatthias Ringwald 
3740616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){
37415917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
37425917a5c5S[email protected]     if (!conn){
37437851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
3744616edd56SMatthias Ringwald         return 0;
37455917a5c5S[email protected]     }
37465917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
37475917a5c5S[email protected]     hci_run();
3748616edd56SMatthias Ringwald     return 0;
37494f3229d8S[email protected] }
375004a6ef8cSmatthias.ringwald 
3751a1bf5ae7SMatthias Ringwald /**
3752a1bf5ae7SMatthias Ringwald  * @brief Get connection type
3753a1bf5ae7SMatthias Ringwald  * @param con_handle
3754a1bf5ae7SMatthias Ringwald  * @result connection_type
3755a1bf5ae7SMatthias Ringwald  */
3756a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){
3757a1bf5ae7SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_handle(connection_handle);
3758a1bf5ae7SMatthias Ringwald     if (!conn) return GAP_CONNECTION_INVALID;
3759a1bf5ae7SMatthias Ringwald     switch (conn->address_type){
3760a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_LE_PUBLIC:
3761a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_LE_RANDOM:
3762a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_LE;
3763a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_SCO:
3764a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_SCO;
3765a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_CLASSIC:
3766a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_ACL;
3767a1bf5ae7SMatthias Ringwald         default:
3768a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_INVALID;
3769a1bf5ae7SMatthias Ringwald     }
3770a1bf5ae7SMatthias Ringwald }
3771a1bf5ae7SMatthias Ringwald 
3772a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
37734f551432SMatthias Ringwald 
3774d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL
3775d23838ecSMatthias Ringwald /**
3776ac9c45e0SMatthias Ringwald  * @brief Auto Connection Establishment - Start Connecting to device
3777ac9c45e0SMatthias Ringwald  * @param address_typ
3778ac9c45e0SMatthias Ringwald  * @param address
3779ac9c45e0SMatthias Ringwald  * @returns 0 if ok
3780ac9c45e0SMatthias Ringwald  */
37814f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){
3782e83201bcSMatthias Ringwald     // check capacity
3783665d90f2SMatthias Ringwald     int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist);
378491915b0bSMatthias Ringwald     if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
3785e83201bcSMatthias Ringwald     whitelist_entry_t * entry = btstack_memory_whitelist_entry_get();
3786e83201bcSMatthias Ringwald     if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED;
3787e83201bcSMatthias Ringwald     entry->address_type = address_type;
3788e83201bcSMatthias Ringwald     memcpy(entry->address, address, 6);
3789e83201bcSMatthias Ringwald     entry->state = LE_WHITELIST_ADD_TO_CONTROLLER;
3790665d90f2SMatthias Ringwald     btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry);
3791e83201bcSMatthias Ringwald     hci_run();
3792e83201bcSMatthias Ringwald     return 0;
3793ac9c45e0SMatthias Ringwald }
3794ac9c45e0SMatthias Ringwald 
379542ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){
3796665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3797665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist);
3798665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3799665d90f2SMatthias Ringwald         whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it);
3800e83201bcSMatthias Ringwald         if (entry->address_type != address_type) continue;
3801e83201bcSMatthias Ringwald         if (memcmp(entry->address, address, 6) != 0) continue;
3802e83201bcSMatthias Ringwald         if (entry->state & LE_WHITELIST_ON_CONTROLLER){
3803e83201bcSMatthias Ringwald             // remove from controller if already present
3804e83201bcSMatthias Ringwald             entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER;
3805e83201bcSMatthias Ringwald             continue;
3806e83201bcSMatthias Ringwald         }
3807e83201bcSMatthias Ringwald         // direclty remove entry from whitelist
3808665d90f2SMatthias Ringwald         btstack_linked_list_iterator_remove(&it);
3809e83201bcSMatthias Ringwald         btstack_memory_whitelist_entry_free(entry);
3810e83201bcSMatthias Ringwald     }
381142ff5ba1SMatthias Ringwald }
381242ff5ba1SMatthias Ringwald 
381342ff5ba1SMatthias Ringwald /**
381442ff5ba1SMatthias Ringwald  * @brief Auto Connection Establishment - Stop Connecting to device
381542ff5ba1SMatthias Ringwald  * @param address_typ
381642ff5ba1SMatthias Ringwald  * @param address
381742ff5ba1SMatthias Ringwald  * @returns 0 if ok
381842ff5ba1SMatthias Ringwald  */
381942ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){
382042ff5ba1SMatthias Ringwald     hci_remove_from_whitelist(address_type, address);
3821e83201bcSMatthias Ringwald     hci_run();
3822e83201bcSMatthias Ringwald     return 0;
3823ac9c45e0SMatthias Ringwald }
3824ac9c45e0SMatthias Ringwald 
3825ac9c45e0SMatthias Ringwald /**
3826ac9c45e0SMatthias Ringwald  * @brief Auto Connection Establishment - Stop everything
3827ac9c45e0SMatthias Ringwald  * @note  Convenience function to stop all active auto connection attempts
3828ac9c45e0SMatthias Ringwald  */
3829ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){
3830665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3831665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist);
3832665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3833665d90f2SMatthias Ringwald         whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it);
3834e83201bcSMatthias Ringwald         if (entry->state & LE_WHITELIST_ON_CONTROLLER){
3835e83201bcSMatthias Ringwald             // remove from controller if already present
3836e83201bcSMatthias Ringwald             entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER;
3837e83201bcSMatthias Ringwald             continue;
3838e83201bcSMatthias Ringwald         }
383991915b0bSMatthias Ringwald         // directly remove entry from whitelist
3840665d90f2SMatthias Ringwald         btstack_linked_list_iterator_remove(&it);
3841e83201bcSMatthias Ringwald         btstack_memory_whitelist_entry_free(entry);
3842e83201bcSMatthias Ringwald     }
3843e83201bcSMatthias Ringwald     hci_run();
3844ac9c45e0SMatthias Ringwald }
3845d70217a2SMatthias Ringwald #endif
38464f551432SMatthias Ringwald #endif
38474f551432SMatthias Ringwald 
384835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
3849ac9c45e0SMatthias Ringwald /**
3850ff00ed1cSMatthias Ringwald  * @brief Set Extended Inquiry Response data
3851ff00ed1cSMatthias Ringwald  * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup
3852ff00ed1cSMatthias Ringwald  * @note has to be done before stack starts up
3853ff00ed1cSMatthias Ringwald  */
3854ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){
3855ff00ed1cSMatthias Ringwald     hci_stack->eir_data = data;
3856ff00ed1cSMatthias Ringwald }
3857ff00ed1cSMatthias Ringwald 
3858ff00ed1cSMatthias Ringwald /**
3859f6858d14SMatthias Ringwald  * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on.
3860f6858d14SMatthias Ringwald  * @param inquriy_mode see bluetooth_defines.h
3861f6858d14SMatthias Ringwald  */
3862f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){
3863f6858d14SMatthias Ringwald     hci_stack->inquiry_mode = mode;
3864f6858d14SMatthias Ringwald }
3865f6858d14SMatthias Ringwald 
3866f6858d14SMatthias Ringwald /**
3867d950d659SMatthias Ringwald  * @brief Configure Voice Setting for use with SCO data in HSP/HFP
3868d950d659SMatthias Ringwald  */
3869d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){
3870d950d659SMatthias Ringwald     hci_stack->sco_voice_setting = voice_setting;
3871d950d659SMatthias Ringwald }
3872d950d659SMatthias Ringwald 
3873d950d659SMatthias Ringwald /**
3874d950d659SMatthias Ringwald  * @brief Get SCO Voice Setting
3875d950d659SMatthias Ringwald  * @return current voice setting
3876d950d659SMatthias Ringwald  */
38770cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){
3878d950d659SMatthias Ringwald     return hci_stack->sco_voice_setting;
3879d950d659SMatthias Ringwald }
3880d950d659SMatthias Ringwald 
3881b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting
3882b3aad8daSMatthias Ringwald  *  @note  Using SCO packets of the exact length is required for USB transfer
3883b3aad8daSMatthias Ringwald  *  @return Length of SCO packets in bytes (not audio frames)
3884b3aad8daSMatthias Ringwald  */
3885b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){
3886b3aad8daSMatthias Ringwald     // see Core Spec for H2 USB Transfer.
3887b3aad8daSMatthias Ringwald     if (hci_stack->sco_voice_setting & 0x0020) return 51;
3888b3aad8daSMatthias Ringwald     return 27;
3889b3aad8daSMatthias Ringwald }
389035454696SMatthias Ringwald #endif
3891b3aad8daSMatthias Ringwald 
3892d950d659SMatthias Ringwald /**
3893d23838ecSMatthias Ringwald  * @brief Set callback for Bluetooth Hardware Error
3894d23838ecSMatthias Ringwald  */
3895c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){
3896d23838ecSMatthias Ringwald     hci_stack->hardware_error_callback = fn;
3897d23838ecSMatthias Ringwald }
3898d23838ecSMatthias Ringwald 
389971de195eSMatthias Ringwald void hci_disconnect_all(void){
3900665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3901665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
3902665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3903665d90f2SMatthias Ringwald         hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it);
390404a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
390504a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
390604a6ef8cSmatthias.ringwald     }
3907d31fba26S[email protected]     hci_run();
390804a6ef8cSmatthias.ringwald }
390933373e40SMatthias Ringwald 
391033373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){
391133373e40SMatthias Ringwald     return hci_stack->manufacturer;
391233373e40SMatthias Ringwald }
3913