xref: /btstack/src/hci.c (revision 94be1a66335553a0c3ba1991e3a2560acb25bd87)
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 
48aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
498f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h"
50a484130cSMatthias Ringwald #endif
51a484130cSMatthias Ringwald 
524a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
53d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h"
544a3574a1SMatthias Ringwald #endif
554a3574a1SMatthias Ringwald 
56a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
57472a5742SMatthias Ringwald #include "gap.h"
5845c102fdSMatthias Ringwald #endif
5945c102fdSMatthias Ringwald 
6093b8dc03Smatthias.ringwald #include <stdarg.h>
6193b8dc03Smatthias.ringwald #include <string.h>
6256fe0872Smatthias.ringwald #include <stdio.h>
635838a2edSMatthias Ringwald #include <inttypes.h>
647f2435e6Smatthias.ringwald 
6516ece135SMatthias Ringwald #include "btstack_debug.h"
660e2df43fSMatthias Ringwald #include "btstack_event.h"
674a3574a1SMatthias Ringwald #include "btstack_linked_list.h"
684a3574a1SMatthias Ringwald #include "btstack_memory.h"
694a3574a1SMatthias Ringwald #include "gap.h"
704a3574a1SMatthias Ringwald #include "hci.h"
714a3574a1SMatthias Ringwald #include "hci_cmd.h"
72d8905019Smatthias.ringwald #include "hci_dump.h"
7393b8dc03Smatthias.ringwald 
741b0e3922Smatthias.ringwald 
75169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
76659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200
77ee091cf1Smatthias.ringwald 
78b83d5eabSMatthias Ringwald // prototypes
7935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
80758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
8135454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled);
8235454696SMatthias Ringwald static int  hci_local_ssp_activated(void);
8335454696SMatthias Ringwald static int  hci_remote_ssp_supported(hci_con_handle_t con_handle);
8435454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void);
8535454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status);
86a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
8735454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level);
88ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer);
8996a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
9052db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
9152db98b2SMatthias Ringwald #endif
927586ee35S[email protected] static int  hci_power_control_on(void);
937586ee35S[email protected] static void hci_power_control_off(void);
946da48142SSean Wilson static void hci_state_reset(void);
95fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason);
96b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void);
97b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void);
98b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status);
99b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump);
100b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size);
10195d71764SMatthias Ringwald static void hci_run(void);
10295d71764SMatthias Ringwald static int  hci_is_le_connection(hci_connection_t * connection);
10395d71764SMatthias Ringwald static int  hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type);
1045d509858SMatthias Ringwald 
105a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
10639677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c
10739677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size);
10842ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address);
1095d509858SMatthias Ringwald #endif
110758b46ceSmatthias.ringwald 
11106b35ec0Smatthias.ringwald // the STACK is here
1123a9fb326S[email protected] #ifndef HAVE_MALLOC
1133a9fb326S[email protected] static hci_stack_t   hci_stack_static;
1143a9fb326S[email protected] #endif
1153a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
11616833f0aSmatthias.ringwald 
11766fb9560S[email protected] // test helper
11866fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
11966fb9560S[email protected] 
12096a45072S[email protected] /**
12196a45072S[email protected]  * create connection for given address
12296a45072S[email protected]  *
12396a45072S[email protected]  * @return connection OR NULL, if no memory left
12496a45072S[email protected]  */
12596a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
1261a06f663S[email protected]     log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type);
127bb69aaaeS[email protected]     hci_connection_t * conn = btstack_memory_hci_connection_get();
12896a45072S[email protected]     if (!conn) return NULL;
129c91d150bS[email protected]     memset(conn, 0, sizeof(hci_connection_t));
130058e3d6bSMatthias Ringwald     bd_addr_copy(conn->address, addr);
13196a45072S[email protected]     conn->address_type = addr_type;
13296a45072S[email protected]     conn->con_handle = 0xffff;
13396a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
13496a45072S[email protected]     conn->bonding_flags = 0;
13596a45072S[email protected]     conn->requested_security_level = LEVEL_0;
13652db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
13791a977e8SMatthias Ringwald     btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler);
13891a977e8SMatthias Ringwald     btstack_run_loop_set_timer_context(&conn->timeout, conn);
13996a45072S[email protected]     hci_connection_timestamp(conn);
14052db98b2SMatthias Ringwald #endif
14196a45072S[email protected]     conn->acl_recombination_length = 0;
14296a45072S[email protected]     conn->acl_recombination_pos = 0;
14396a45072S[email protected]     conn->num_acl_packets_sent = 0;
1441a06f663S[email protected]     conn->num_sco_packets_sent = 0;
145da886c03S[email protected]     conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
146665d90f2SMatthias Ringwald     btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn);
14796a45072S[email protected]     return conn;
14896a45072S[email protected] }
14966fb9560S[email protected] 
150da886c03S[email protected] 
151da886c03S[email protected] /**
152da886c03S[email protected]  * get le connection parameter range
153da886c03S[email protected] *
154da886c03S[email protected]  * @return le connection parameter range struct
155da886c03S[email protected]  */
1564ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){
1574ced4e8cSMatthias Ringwald     *range = hci_stack->le_connection_parameter_range;
158da886c03S[email protected] }
159da886c03S[email protected] 
160da886c03S[email protected] /**
161da886c03S[email protected]  * set le connection parameter range
162da886c03S[email protected]  *
163da886c03S[email protected]  */
164da886c03S[email protected] 
1654ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){
1664ced4e8cSMatthias Ringwald     hci_stack->le_connection_parameter_range = *range;
167da886c03S[email protected] }
168da886c03S[email protected] 
169da886c03S[email protected] /**
170da886c03S[email protected]  * get hci connections iterator
171da886c03S[email protected]  *
172da886c03S[email protected]  * @return hci connections iterator
173da886c03S[email protected]  */
174da886c03S[email protected] 
175665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
176665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(it, &hci_stack->connections);
177da886c03S[email protected] }
178da886c03S[email protected] 
17997addcc5Smatthias.ringwald /**
180ee091cf1Smatthias.ringwald  * get connection for a given handle
181ee091cf1Smatthias.ringwald  *
182ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
183ee091cf1Smatthias.ringwald  */
1845061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
185665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
186665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
187665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
188665d90f2SMatthias Ringwald         hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
1893ac2fe56S[email protected]         if ( item->con_handle == con_handle ) {
190da886c03S[email protected]             return item;
191ee091cf1Smatthias.ringwald         }
192ee091cf1Smatthias.ringwald     }
193ee091cf1Smatthias.ringwald     return NULL;
194ee091cf1Smatthias.ringwald }
195ee091cf1Smatthias.ringwald 
19696a45072S[email protected] /**
19796a45072S[email protected]  * get connection for given address
19896a45072S[email protected]  *
19996a45072S[email protected]  * @return connection OR NULL, if not found
20096a45072S[email protected]  */
2012e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t  addr, bd_addr_type_t addr_type){
202665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
203665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
204665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
205665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
20696a45072S[email protected]         if (connection->address_type != addr_type)  continue;
20796a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
20862bda3fbS[email protected]         return connection;
20962bda3fbS[email protected]     }
21062bda3fbS[email protected]     return NULL;
21162bda3fbS[email protected] }
21262bda3fbS[email protected] 
21352db98b2SMatthias Ringwald 
21452db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
21552db98b2SMatthias Ringwald 
216ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){
21791a977e8SMatthias Ringwald     hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer);
218aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
219528a4a3bSMatthias Ringwald     if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
220c785ef68Smatthias.ringwald         // connections might be timed out
221c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
222c785ef68Smatthias.ringwald     }
223f316a845SMatthias Ringwald #else
224528a4a3bSMatthias Ringwald     if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){
2255f26aadcSMatthias Ringwald         // connections might be timed out
2265f26aadcSMatthias Ringwald         hci_emit_l2cap_check_timeout(connection);
2275f26aadcSMatthias Ringwald     }
2285f26aadcSMatthias Ringwald #endif
229c785ef68Smatthias.ringwald }
230ee091cf1Smatthias.ringwald 
231ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
232aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
233528a4a3bSMatthias Ringwald     connection->timestamp = btstack_run_loop_embedded_get_ticks();
234f316a845SMatthias Ringwald #else
235528a4a3bSMatthias Ringwald     connection->timestamp = btstack_run_loop_get_time_ms();
2365f26aadcSMatthias Ringwald #endif
237ee091cf1Smatthias.ringwald }
238ee091cf1Smatthias.ringwald 
23928ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
24028ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
24128ca2b46S[email protected] }
24228ca2b46S[email protected] 
24335454696SMatthias Ringwald 
24428ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
24528ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
24628ca2b46S[email protected] }
24728ca2b46S[email protected] 
24843bfb1bdSmatthias.ringwald /**
24980ca58a0Smatthias.ringwald  * add authentication flags and reset timer
25096a45072S[email protected]  * @note: assumes classic connection
2512e77e513S[email protected]  * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets
2527fde4af9Smatthias.ringwald  */
2537fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
2547fde4af9Smatthias.ringwald     bd_addr_t addr;
255724d70a2SMatthias Ringwald     reverse_bd_addr(bd_addr, addr);
2562e77e513S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2577fde4af9Smatthias.ringwald     if (conn) {
25828ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
25980ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
2607fde4af9Smatthias.ringwald     }
2617fde4af9Smatthias.ringwald }
2627fde4af9Smatthias.ringwald 
26380ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2645061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
26580ca58a0Smatthias.ringwald     if (!conn) return 0;
2666724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2676724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2686724cd9eS[email protected]     return 0;
26980ca58a0Smatthias.ringwald }
27080ca58a0Smatthias.ringwald 
27115a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){
27255597469SMatthias Ringwald     if (!hci_stack->link_key_db) return;
2732bacf595SMatthias Ringwald     log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr));
274a98592bcSMatthias Ringwald     hci_stack->link_key_db->delete_link_key(addr);
275c12e46e7Smatthias.ringwald }
27655597469SMatthias Ringwald 
27755597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){
27855597469SMatthias Ringwald     if (!hci_stack->link_key_db) return;
2792bacf595SMatthias Ringwald     log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type);
28055597469SMatthias Ringwald     hci_stack->link_key_db->put_link_key(addr, link_key, type);
281c12e46e7Smatthias.ringwald }
28235454696SMatthias Ringwald #endif
283c12e46e7Smatthias.ringwald 
28495d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){
2850bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2860bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2870bf6344aS[email protected] }
2880bf6344aS[email protected] 
2897fde4af9Smatthias.ringwald /**
29043bfb1bdSmatthias.ringwald  * count connections
29143bfb1bdSmatthias.ringwald  */
29240d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
29356c253c9Smatthias.ringwald     int count = 0;
294665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
295665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
29643bfb1bdSmatthias.ringwald     return count;
29743bfb1bdSmatthias.ringwald }
298c8e4258aSmatthias.ringwald 
29995d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){
300ee303eddS[email protected] 
301f04a0c31SMatthias Ringwald     unsigned int num_packets_sent_classic = 0;
302f04a0c31SMatthias Ringwald     unsigned int num_packets_sent_le = 0;
303ee303eddS[email protected] 
304665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
305665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
306998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
307ee303eddS[email protected]         if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
308ee303eddS[email protected]             num_packets_sent_classic += connection->num_acl_packets_sent;
309ee303eddS[email protected]         } else {
310ee303eddS[email protected]             num_packets_sent_le += connection->num_acl_packets_sent;
311ee303eddS[email protected]         }
312ee303eddS[email protected]     }
313d999b54eSMatthias Ringwald     log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num);
314ee303eddS[email protected]     int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic;
315ee303eddS[email protected]     int free_slots_le = 0;
316ee303eddS[email protected] 
317ee303eddS[email protected]     if (free_slots_classic < 0){
3189da54300S[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);
319998906cdSmatthias.ringwald         return 0;
320998906cdSmatthias.ringwald     }
321ee303eddS[email protected] 
322ee303eddS[email protected]     if (hci_stack->le_acl_packets_total_num){
323ee303eddS[email protected]         // if we have LE slots, they are used
324ee303eddS[email protected]         free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le;
325ee303eddS[email protected]         if (free_slots_le < 0){
3269da54300S[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);
327ee303eddS[email protected]             return 0;
328998906cdSmatthias.ringwald         }
329ee303eddS[email protected]     } else {
330ee303eddS[email protected]         // otherwise, classic slots are used for LE, too
331ee303eddS[email protected]         free_slots_classic -= num_packets_sent_le;
332ee303eddS[email protected]         if (free_slots_classic < 0){
3339da54300S[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);
334ee303eddS[email protected]             return 0;
335ee303eddS[email protected]         }
336ee303eddS[email protected]     }
337ee303eddS[email protected] 
338ee303eddS[email protected]     switch (address_type){
339ee303eddS[email protected]         case BD_ADDR_TYPE_UNKNOWN:
3402125de09SMatthias Ringwald             log_error("hci_number_free_acl_slots: unknown address type");
341ee303eddS[email protected]             return 0;
342ee303eddS[email protected] 
343ee303eddS[email protected]         case BD_ADDR_TYPE_CLASSIC:
344ee303eddS[email protected]             return free_slots_classic;
345ee303eddS[email protected] 
346ee303eddS[email protected]         default:
347cb00d3aaS[email protected]            if (hci_stack->le_acl_packets_total_num){
348ee303eddS[email protected]                return free_slots_le;
349ee303eddS[email protected]            }
350cb00d3aaS[email protected]            return free_slots_classic;
351cb00d3aaS[email protected]     }
352998906cdSmatthias.ringwald }
353998906cdSmatthias.ringwald 
3542125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){
3552125de09SMatthias Ringwald     // get connection type
3562125de09SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3572125de09SMatthias Ringwald     if (!connection){
3582125de09SMatthias Ringwald         log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle);
3592125de09SMatthias Ringwald         return 0;
3602125de09SMatthias Ringwald     }
3612125de09SMatthias Ringwald     return hci_number_free_acl_slots_for_connection_type(connection->address_type);
3622125de09SMatthias Ringwald }
3632125de09SMatthias Ringwald 
36435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
365701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){
366f04a0c31SMatthias Ringwald     unsigned int num_sco_packets_sent  = 0;
367665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
368665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
369e35edcc1S[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
370e35edcc1S[email protected]         num_sco_packets_sent += connection->num_sco_packets_sent;
371e35edcc1S[email protected]     }
372e35edcc1S[email protected]     if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
373701e3307SMatthias Ringwald         log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num);
37444d0e3d5S[email protected]         return 0;
37544d0e3d5S[email protected]     }
376701e3307SMatthias Ringwald     // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent);
377e35edcc1S[email protected]     return hci_stack->sco_packets_total_num - num_sco_packets_sent;
378e35edcc1S[email protected] }
37935454696SMatthias Ringwald #endif
38044d0e3d5S[email protected] 
381ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
382ac928cc2S[email protected] int hci_can_send_command_packet_now(void){
383ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
384ac928cc2S[email protected] 
385ac928cc2S[email protected]     // check for async hci transport implementations
386ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
387ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
388ac928cc2S[email protected]             return 0;
389ac928cc2S[email protected]         }
390ac928cc2S[email protected]     }
391ac928cc2S[email protected] 
392ac928cc2S[email protected]     return hci_stack->num_cmd_packets > 0;
393ac928cc2S[email protected] }
394ac928cc2S[email protected] 
3959d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){
396ac928cc2S[email protected]     // check for async hci transport implementations
3979d04d3a7SMatthias Ringwald     if (!hci_stack->hci_transport->can_send_packet_now) return 1;
3989d04d3a7SMatthias Ringwald     return hci_stack->hci_transport->can_send_packet_now(packet_type);
399ac928cc2S[email protected] }
4009d04d3a7SMatthias Ringwald 
4019d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){
4029d04d3a7SMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0;
4039d04d3a7SMatthias Ringwald     return hci_number_free_acl_slots_for_connection_type(address_type) > 0;
404ac928cc2S[email protected] }
4059d04d3a7SMatthias Ringwald 
4069d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){
4079d04d3a7SMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
4089d04d3a7SMatthias Ringwald     return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC);
4099d04d3a7SMatthias Ringwald }
4109d04d3a7SMatthias Ringwald 
4119d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
4129d04d3a7SMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0;
413e79abdd6S[email protected]     return hci_number_free_acl_slots_for_handle(con_handle) > 0;
414ac928cc2S[email protected] }
415ac928cc2S[email protected] 
416ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
417ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
418ac928cc2S[email protected]     return hci_can_send_prepared_acl_packet_now(con_handle);
4196b4af23dS[email protected] }
4206b4af23dS[email protected] 
42135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
42235454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){
42335454696SMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
42435454696SMatthias Ringwald     return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC);
42535454696SMatthias Ringwald }
42635454696SMatthias Ringwald 
427701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){
428d057580eSMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0;
429ed361f5fSMatthias Ringwald     if (!hci_stack->synchronous_flow_control_enabled) return 1;
430701e3307SMatthias Ringwald     return hci_number_free_sco_slots() > 0;
43144d0e3d5S[email protected] }
43244d0e3d5S[email protected] 
433701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){
434d057580eSMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
435701e3307SMatthias Ringwald     return hci_can_send_prepared_sco_packet_now();
43644d0e3d5S[email protected] }
43744d0e3d5S[email protected] 
438d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){
439d057580eSMatthias Ringwald     hci_stack->sco_waiting_for_can_send_now = 1;
440d057580eSMatthias Ringwald     hci_notify_if_sco_can_send_now();
441d057580eSMatthias Ringwald }
44235454696SMatthias Ringwald #endif
443d057580eSMatthias Ringwald 
44495d71764SMatthias Ringwald // used for internal checks in l2cap.c
445c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
446c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
447c8b9416aS[email protected] }
448c8b9416aS[email protected] 
4496b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
4506b4af23dS[email protected] int hci_reserve_packet_buffer(void){
4519d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
4529d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
4539d14b626S[email protected]         return 0;
4549d14b626S[email protected]     }
4556b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
4566b4af23dS[email protected]     return 1;
4576b4af23dS[email protected] }
4586b4af23dS[email protected] 
45968a0fcf7S[email protected] void hci_release_packet_buffer(void){
46068a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
46168a0fcf7S[email protected] }
46268a0fcf7S[email protected] 
4636b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
4647f02f414SMatthias Ringwald static int hci_transport_synchronous(void){
4656b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
4666b4af23dS[email protected] }
4676b4af23dS[email protected] 
468452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){
469452cf3bbS[email protected] 
470452cf3bbS[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);
471452cf3bbS[email protected] 
472452cf3bbS[email protected]     // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers
473452cf3bbS[email protected]     uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length;
474452cf3bbS[email protected]     if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){
475452cf3bbS[email protected]         max_acl_data_packet_length = hci_stack->le_data_packets_length;
476452cf3bbS[email protected]     }
477452cf3bbS[email protected] 
478452cf3bbS[email protected]     // testing: reduce buffer to minimum
479452cf3bbS[email protected]     // max_acl_data_packet_length = 52;
480452cf3bbS[email protected] 
481d999b54eSMatthias Ringwald     log_debug("hci_send_acl_packet_fragments entered");
482d999b54eSMatthias Ringwald 
483452cf3bbS[email protected]     int err;
484452cf3bbS[email protected]     // multiple packets could be send on a synchronous HCI transport
485452cf3bbS[email protected]     while (1){
486452cf3bbS[email protected] 
487d999b54eSMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop entered");
488d999b54eSMatthias Ringwald 
489452cf3bbS[email protected]         // get current data
490452cf3bbS[email protected]         const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4;
491452cf3bbS[email protected]         int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
492452cf3bbS[email protected]         int more_fragments = 0;
493452cf3bbS[email protected] 
494452cf3bbS[email protected]         // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
495452cf3bbS[email protected]         if (current_acl_data_packet_length > max_acl_data_packet_length){
496452cf3bbS[email protected]             more_fragments = 1;
497452cf3bbS[email protected]             current_acl_data_packet_length = max_acl_data_packet_length;
498452cf3bbS[email protected]         }
499452cf3bbS[email protected] 
500452cf3bbS[email protected]         // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent)
501452cf3bbS[email protected]         if (acl_header_pos > 0){
502f8fbdce0SMatthias Ringwald             uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
503452cf3bbS[email protected]             handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12);
504f8fbdce0SMatthias Ringwald             little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
505452cf3bbS[email protected]         }
506452cf3bbS[email protected] 
507452cf3bbS[email protected]         // update header len
508f8fbdce0SMatthias Ringwald         little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length);
509452cf3bbS[email protected] 
510452cf3bbS[email protected]         // count packet
511452cf3bbS[email protected]         connection->num_acl_packets_sent++;
512f04a0c31SMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments);
513d999b54eSMatthias Ringwald 
514d999b54eSMatthias Ringwald         // update state for next fragment (if any) as "transport done" might be sent during send_packet already
515d999b54eSMatthias Ringwald         if (more_fragments){
516d999b54eSMatthias Ringwald             // update start of next fragment to send
517d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
518d999b54eSMatthias Ringwald         } else {
519d999b54eSMatthias Ringwald             // done
520d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_pos = 0;
521d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_total_size = 0;
522d999b54eSMatthias Ringwald         }
523452cf3bbS[email protected] 
524452cf3bbS[email protected]         // send packet
525452cf3bbS[email protected]         uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
526452cf3bbS[email protected]         const int size = current_acl_data_packet_length + 4;
5275bb5bc3eS[email protected]         hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
528452cf3bbS[email protected]         err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
529452cf3bbS[email protected] 
530f04a0c31SMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments);
531d999b54eSMatthias Ringwald 
532452cf3bbS[email protected]         // done yet?
533452cf3bbS[email protected]         if (!more_fragments) break;
534452cf3bbS[email protected] 
535452cf3bbS[email protected]         // can send more?
536452cf3bbS[email protected]         if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err;
537452cf3bbS[email protected]     }
538452cf3bbS[email protected] 
539d999b54eSMatthias Ringwald     log_debug("hci_send_acl_packet_fragments loop over");
540452cf3bbS[email protected] 
541d051460cS[email protected]     // release buffer now for synchronous transport
542203bace6S[email protected]     if (hci_transport_synchronous()){
543452cf3bbS[email protected]         hci_release_packet_buffer();
54463fa3374SMatthias Ringwald         // notify upper stack that it might be possible to send again
54563fa3374SMatthias Ringwald         uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
546d6b06661SMatthias Ringwald         hci_emit_event(&event[0], sizeof(event), 0);  // don't dump
547452cf3bbS[email protected]     }
548452cf3bbS[email protected] 
549452cf3bbS[email protected]     return err;
550452cf3bbS[email protected] }
551452cf3bbS[email protected] 
552826f7347S[email protected] // pre: caller has reserved the packet buffer
553826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
5547856c818Smatthias.ringwald 
555452cf3bbS[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
556452cf3bbS[email protected] 
557826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
558826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
559826f7347S[email protected]         return 0;
560826f7347S[email protected]     }
561826f7347S[email protected] 
562d713a683S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
563d713a683S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
564d713a683S[email protected] 
565826f7347S[email protected]     // check for free places on Bluetooth module
566d713a683S[email protected]     if (!hci_can_send_prepared_acl_packet_now(con_handle)) {
567826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
56897b61c7bS[email protected]         hci_release_packet_buffer();
56997b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
57097b61c7bS[email protected]     }
5716218e6f1Smatthias.ringwald 
5725061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
57397b61c7bS[email protected]     if (!connection) {
5745fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
57597b61c7bS[email protected]         hci_release_packet_buffer();
57697b61c7bS[email protected]         return 0;
57797b61c7bS[email protected]     }
57852db98b2SMatthias Ringwald 
57952db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
58056cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
58152db98b2SMatthias Ringwald #endif
58256cf178bSmatthias.ringwald 
583452cf3bbS[email protected]     // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size);
5847856c818Smatthias.ringwald 
585452cf3bbS[email protected]     // setup data
586452cf3bbS[email protected]     hci_stack->acl_fragmentation_total_size = size;
587452cf3bbS[email protected]     hci_stack->acl_fragmentation_pos = 4;   // start of L2CAP packet
5886218e6f1Smatthias.ringwald 
589452cf3bbS[email protected]     return hci_send_acl_packet_fragments(connection);
590ee091cf1Smatthias.ringwald }
591ee091cf1Smatthias.ringwald 
59235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
59344d0e3d5S[email protected] // pre: caller has reserved the packet buffer
59444d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){
59544d0e3d5S[email protected] 
59644d0e3d5S[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
59744d0e3d5S[email protected] 
59844d0e3d5S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
59944d0e3d5S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
60044d0e3d5S[email protected]         return 0;
60144d0e3d5S[email protected]     }
60244d0e3d5S[email protected] 
60344d0e3d5S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
6044b3e1e19SMatthias Ringwald 
6054b3e1e19SMatthias Ringwald     // skip checks in loopback mode
6064b3e1e19SMatthias Ringwald     if (!hci_stack->loopback_mode){
60744d0e3d5S[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);   // same for ACL and SCO
60844d0e3d5S[email protected] 
60944d0e3d5S[email protected]         // check for free places on Bluetooth module
610701e3307SMatthias Ringwald         if (!hci_can_send_prepared_sco_packet_now()) {
61144d0e3d5S[email protected]             log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller");
61244d0e3d5S[email protected]             hci_release_packet_buffer();
61344d0e3d5S[email protected]             return BTSTACK_ACL_BUFFERS_FULL;
61444d0e3d5S[email protected]         }
61544d0e3d5S[email protected] 
616e35edcc1S[email protected]         // track send packet in connection struct
617e35edcc1S[email protected]         hci_connection_t *connection = hci_connection_for_handle( con_handle);
618e35edcc1S[email protected]         if (!connection) {
619e35edcc1S[email protected]             log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle);
620e35edcc1S[email protected]             hci_release_packet_buffer();
621e35edcc1S[email protected]             return 0;
622e35edcc1S[email protected]         }
623e35edcc1S[email protected]         connection->num_sco_packets_sent++;
6244b3e1e19SMatthias Ringwald     }
62544d0e3d5S[email protected] 
62644d0e3d5S[email protected]     hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size);
627543e835cSMatthias Ringwald     int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size);
628543e835cSMatthias Ringwald 
629543e835cSMatthias Ringwald     if (hci_transport_synchronous()){
630543e835cSMatthias Ringwald         hci_release_packet_buffer();
63163fa3374SMatthias Ringwald         // notify upper stack that it might be possible to send again
63263fa3374SMatthias Ringwald         uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
633d6b06661SMatthias Ringwald         hci_emit_event(&event[0], sizeof(event), 0);    // don't dump
634543e835cSMatthias Ringwald     }
635543e835cSMatthias Ringwald 
636543e835cSMatthias Ringwald     return err;
63744d0e3d5S[email protected] }
63835454696SMatthias Ringwald #endif
63944d0e3d5S[email protected] 
64016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
6417856c818Smatthias.ringwald 
642e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
643e76a89eeS[email protected] 
6447856c818Smatthias.ringwald     // get info
6457856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
6465061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
6477856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
6487856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
6497856c818Smatthias.ringwald 
6507856c818Smatthias.ringwald     // ignore non-registered handle
6517856c818Smatthias.ringwald     if (!conn){
6529da54300S[email protected]         log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle);
6537856c818Smatthias.ringwald         return;
6547856c818Smatthias.ringwald     }
6557856c818Smatthias.ringwald 
656e76a89eeS[email protected]     // assert packet is complete
6579ecc3e17S[email protected]     if (acl_length + 4 != size){
658f04a0c31SMatthias Ringwald         log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4);
659e76a89eeS[email protected]         return;
660e76a89eeS[email protected]     }
661e76a89eeS[email protected] 
66252db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
6637856c818Smatthias.ringwald     // update idle timestamp
6647856c818Smatthias.ringwald     hci_connection_timestamp(conn);
66552db98b2SMatthias Ringwald #endif
6667856c818Smatthias.ringwald 
6677856c818Smatthias.ringwald     // handle different packet types
6687856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
6697856c818Smatthias.ringwald 
6707856c818Smatthias.ringwald         case 0x01: // continuation fragment
6717856c818Smatthias.ringwald 
6720ca847afS[email protected]             // sanity checks
6737856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
6749da54300S[email protected]                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle);
6757856c818Smatthias.ringwald                 return;
6767856c818Smatthias.ringwald             }
6770ca847afS[email protected]             if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){
6780ca847afS[email protected]                 log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x",
6790ca847afS[email protected]                     conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
6800ca847afS[email protected]                 conn->acl_recombination_pos = 0;
6810ca847afS[email protected]                 return;
6820ca847afS[email protected]             }
6837856c818Smatthias.ringwald 
6847856c818Smatthias.ringwald             // append fragment payload (header already stored)
685ec6321eeS[email protected]             memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length );
6867856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
6877856c818Smatthias.ringwald 
6889da54300S[email protected]             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length,
689decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
6907856c818Smatthias.ringwald 
6917856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
6927856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
693d6b06661SMatthias Ringwald                 hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos);
6947856c818Smatthias.ringwald                 // reset recombination buffer
6957856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
6967856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
6977856c818Smatthias.ringwald             }
6987856c818Smatthias.ringwald             break;
6997856c818Smatthias.ringwald 
7007856c818Smatthias.ringwald         case 0x02: { // first fragment
7017856c818Smatthias.ringwald 
70223a77e1aS[email protected]             // sanity check
70323a77e1aS[email protected]             if (conn->acl_recombination_pos) {
70423a77e1aS[email protected]                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle);
70523a77e1aS[email protected]                 conn->acl_recombination_pos = 0;
70623a77e1aS[email protected]             }
70723a77e1aS[email protected] 
7087856c818Smatthias.ringwald             // peek into L2CAP packet!
7097856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
7107856c818Smatthias.ringwald 
7119da54300S[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length);
712decc01a8Smatthias.ringwald 
7137856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
7147856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
7157856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
716d6b06661SMatthias Ringwald                 hci_emit_acl_packet(packet, acl_length + 4);
7177856c818Smatthias.ringwald             } else {
7180ca847afS[email protected] 
7190ca847afS[email protected]                 if (acl_length > HCI_ACL_BUFFER_SIZE){
7200ca847afS[email protected]                     log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x",
7210ca847afS[email protected]                         4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
7220ca847afS[email protected]                     return;
7230ca847afS[email protected]                 }
7240ca847afS[email protected] 
7257856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
726ec6321eeS[email protected]                 memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4);
7277856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
7287856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
729f8fbdce0SMatthias Ringwald                 little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4);
7307856c818Smatthias.ringwald             }
7317856c818Smatthias.ringwald             break;
7327856c818Smatthias.ringwald 
7337856c818Smatthias.ringwald         }
7347856c818Smatthias.ringwald         default:
7359da54300S[email protected]             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03);
7367856c818Smatthias.ringwald             return;
7377856c818Smatthias.ringwald     }
73894ab26f8Smatthias.ringwald 
73994ab26f8Smatthias.ringwald     // execute main loop
74094ab26f8Smatthias.ringwald     hci_run();
74116833f0aSmatthias.ringwald }
74222909952Smatthias.ringwald 
74367a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
7449da54300S[email protected]     log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address));
7453c4d4b90Smatthias.ringwald 
746528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&conn->timeout);
747c785ef68Smatthias.ringwald 
748665d90f2SMatthias Ringwald     btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
749a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
7503c4d4b90Smatthias.ringwald 
7513c4d4b90Smatthias.ringwald     // now it's gone
752c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
753c7e0c5f6Smatthias.ringwald }
754c7e0c5f6Smatthias.ringwald 
75535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
75635454696SMatthias Ringwald 
7570c042179S[email protected] static const uint16_t packet_type_sizes[] = {
7588f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
7598f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
7608f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
7618f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
7628f8108aaSmatthias.ringwald };
76365389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
76465389bfcS[email protected]      0, // 3 slot packets
76565389bfcS[email protected]      1, // 5 slot packets
76665389bfcS[email protected]     25, // EDR 2 mpbs
76765389bfcS[email protected]     26, // EDR 3 mbps
76865389bfcS[email protected]     39, // 3 slot EDR packts
76965389bfcS[email protected]     40, // 5 slot EDR packet
77065389bfcS[email protected] };
77165389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
77265389bfcS[email protected]     0x0f00, // 3 slot packets
77365389bfcS[email protected]     0xf000, // 5 slot packets
77465389bfcS[email protected]     0x1102, // EDR 2 mpbs
77565389bfcS[email protected]     0x2204, // EDR 3 mbps
77665389bfcS[email protected]     0x0300, // 3 slot EDR packts
77765389bfcS[email protected]     0x3000, // 5 slot EDR packet
77865389bfcS[email protected] };
7798f8108aaSmatthias.ringwald 
78065389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
78165389bfcS[email protected]     // enable packet types based on size
7828f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
783f16a69bbS[email protected]     unsigned int i;
7848f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
7858f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
7868f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
7878f8108aaSmatthias.ringwald             packet_types |= 1 << i;
7888f8108aaSmatthias.ringwald         }
7898f8108aaSmatthias.ringwald     }
79065389bfcS[email protected]     // disable packet types due to missing local supported features
79165389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
792f04a0c31SMatthias Ringwald         unsigned int bit_idx = packet_type_feature_requirement_bit[i];
79365389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
79465389bfcS[email protected]         if (feature_set) continue;
79565389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
79665389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
79765389bfcS[email protected]     }
7988f8108aaSmatthias.ringwald     // flip bits for "may not be used"
7998f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
8008f8108aaSmatthias.ringwald     return packet_types;
8018f8108aaSmatthias.ringwald }
8028f8108aaSmatthias.ringwald 
8038f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
8043a9fb326S[email protected]     return hci_stack->packet_types;
8058f8108aaSmatthias.ringwald }
80635454696SMatthias Ringwald #endif
8078f8108aaSmatthias.ringwald 
808facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
8097dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
8103a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
8117dc17943Smatthias.ringwald }
8127dc17943Smatthias.ringwald 
813f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
8143a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
8157dc17943Smatthias.ringwald }
8167dc17943Smatthias.ringwald 
8173e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){
8183e68d23dSMatthias Ringwald     // No. 31, byte 3, bit 7
8193e68d23dSMatthias Ringwald     return (hci_stack->local_supported_features[3] & (1 << 7)) != 0;
8203e68d23dSMatthias Ringwald }
8213e68d23dSMatthias Ringwald 
8226ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
8236ac9a97eS[email protected]     // No. 54, byte 6, bit 6
8246ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
8256ac9a97eS[email protected] }
8266ac9a97eS[email protected] 
82715a95bd5SMatthias Ringwald static int gap_ssp_supported(void){
8286ac9a97eS[email protected]     // No. 51, byte 6, bit 3
8293a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
830f5d8d141S[email protected] }
831f5d8d141S[email protected] 
8327f02f414SMatthias Ringwald static int hci_classic_supported(void){
83335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
8346ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
8353a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
83635454696SMatthias Ringwald #else
83735454696SMatthias Ringwald     return 0;
83835454696SMatthias Ringwald #endif
839f5d8d141S[email protected] }
840f5d8d141S[email protected] 
8417f02f414SMatthias Ringwald static int hci_le_supported(void){
842a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
8436ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
8443a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
845f5d8d141S[email protected] #else
846f5d8d141S[email protected]     return 0;
847f5d8d141S[email protected] #endif
848f5d8d141S[email protected] }
849f5d8d141S[email protected] 
85069a97523S[email protected] // get addr type and address used in advertisement packets
85115a95bd5SMatthias Ringwald void gap_advertisements_get_address(uint8_t * addr_type, bd_addr_t  addr){
8523a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
8533a9fb326S[email protected]     if (hci_stack->adv_addr_type){
8543a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
85569a97523S[email protected]     } else {
8563a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
85769a97523S[email protected]     }
85869a97523S[email protected] }
85969a97523S[email protected] 
860a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
86139677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){
862d1dc057bS[email protected]     int offset = 3;
863d1dc057bS[email protected]     int num_reports = packet[offset];
864d1dc057bS[email protected]     offset += 1;
865d1dc057bS[email protected] 
86657c9da5bS[email protected]     int i;
8674f4e0224SMatthias Ringwald     // log_info("HCI: handle adv report with num reports: %d", num_reports);
86803fbe9c6S[email protected]     uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
86957c9da5bS[email protected]     for (i=0; i<num_reports;i++){
870210c6774S[email protected]         uint8_t data_length = packet[offset + 8];
871a0aac9c4S[email protected]         uint8_t event_size = 10 + data_length;
872d1dc057bS[email protected]         int pos = 0;
873045013feSMatthias Ringwald         event[pos++] = GAP_EVENT_ADVERTISING_REPORT;
87457c9da5bS[email protected]         event[pos++] = event_size;
875210c6774S[email protected]         memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address
876d1dc057bS[email protected]         offset += 8;
877d1dc057bS[email protected]         pos += 8;
878d1dc057bS[email protected]         event[pos++] = packet[offset + 1 + data_length]; // rssi
879d1dc057bS[email protected]         event[pos++] = packet[offset++]; //data_length;
880d1dc057bS[email protected]         memcpy(&event[pos], &packet[offset], data_length);
88157c9da5bS[email protected]         pos += data_length;
882d1dc057bS[email protected]         offset += data_length + 1; // rssi
883d6b06661SMatthias Ringwald         hci_emit_event(event, pos, 1);
88457c9da5bS[email protected]     }
88557c9da5bS[email protected] }
886b2f949feS[email protected] #endif
88757c9da5bS[email protected] 
88896b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){
88996b53536SMatthias Ringwald     if (!hci_stack->config) return 0;
8909796ebeaSMatthias Ringwald     uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
89196b53536SMatthias Ringwald     // Limit baud rate for Broadcom chipsets to 3 mbps
892c6d1dbedSMatthias Ringwald     if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){
89396b53536SMatthias Ringwald         baud_rate = 3000000;
89496b53536SMatthias Ringwald     }
89596b53536SMatthias Ringwald     return baud_rate;
89696b53536SMatthias Ringwald }
89796b53536SMatthias Ringwald 
898ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){
8990305bdeaSMatthias Ringwald     switch (hci_stack->substate){
9000305bdeaSMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
9017b0d7667SMatthias Ringwald             log_info("Resend HCI Reset");
9020305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET;
9037b0d7667SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
9040305bdeaSMatthias Ringwald             hci_run();
9050305bdeaSMatthias Ringwald             break;
9069f007422SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET:
9079f007422SMatthias Ringwald             log_info("Resend HCI Reset - CSR Warm Boot with Link Reset");
9089f007422SMatthias Ringwald             if (hci_stack->hci_transport->reset_link){
9099f007422SMatthias Ringwald                 hci_stack->hci_transport->reset_link();
9109f007422SMatthias Ringwald             }
911202c8a4cSMatthias Ringwald             // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT
912e47e68c7SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
913e47e68c7SMatthias Ringwald             log_info("Resend HCI Reset - CSR Warm Boot");
914e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
915e47e68c7SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
916e47e68c7SMatthias Ringwald             hci_run();
917688c2635SMatthias Ringwald             break;
9187224be7eSMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
9197224be7eSMatthias Ringwald             if (hci_stack->hci_transport->set_baudrate){
92096b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
921cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate);
9227dd9d0ecSMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
9237224be7eSMatthias Ringwald             }
924772a36d3SMatthias Ringwald             // For CSR, HCI Reset is sent on new baud rate
9252caefae9SMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO){
926772a36d3SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
927772a36d3SMatthias Ringwald                 hci_run();
928772a36d3SMatthias Ringwald             }
9294696bddbSMatthias Ringwald             break;
9300305bdeaSMatthias Ringwald         default:
9310305bdeaSMatthias Ringwald             break;
9320305bdeaSMatthias Ringwald     }
9330305bdeaSMatthias Ringwald }
9340305bdeaSMatthias Ringwald 
93571de195eSMatthias Ringwald static void hci_initializing_next_state(void){
93674b323a9SMatthias Ringwald     hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1);
93774b323a9SMatthias Ringwald }
93874b323a9SMatthias Ringwald 
93974b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true
94071de195eSMatthias Ringwald static void hci_initializing_run(void){
941148ca237SMatthias Ringwald     log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now());
94274b323a9SMatthias Ringwald     switch (hci_stack->substate){
94374b323a9SMatthias Ringwald         case HCI_INIT_SEND_RESET:
94474b323a9SMatthias Ringwald             hci_state_reset();
945a0cf2f3fSMatthias Ringwald 
946423a3e49SMatthias Ringwald #ifndef HAVE_PLATFORM_IPHONE_OS
9470305bdeaSMatthias Ringwald             // prepare reset if command complete not received in 100ms
948659d758cSMatthias Ringwald             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
949528a4a3bSMatthias Ringwald             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
950528a4a3bSMatthias Ringwald             btstack_run_loop_add_timer(&hci_stack->timeout);
951a0cf2f3fSMatthias Ringwald #endif
9520305bdeaSMatthias Ringwald             // send command
95374b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET;
9540305bdeaSMatthias Ringwald             hci_send_cmd(&hci_reset);
95574b323a9SMatthias Ringwald             break;
95676fcb19bSMatthias Ringwald         case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION:
95776fcb19bSMatthias Ringwald             hci_send_cmd(&hci_read_local_version_information);
95876fcb19bSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION;
95976fcb19bSMatthias Ringwald             break;
960e90bae01SMatthias Ringwald         case HCI_INIT_SEND_READ_LOCAL_NAME:
961e90bae01SMatthias Ringwald             hci_send_cmd(&hci_read_local_name);
962e90bae01SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME;
963e90bae01SMatthias Ringwald             break;
964e47e68c7SMatthias Ringwald         case HCI_INIT_SEND_RESET_CSR_WARM_BOOT:
965e47e68c7SMatthias Ringwald             hci_state_reset();
966e47e68c7SMatthias Ringwald             // prepare reset if command complete not received in 100ms
967659d758cSMatthias Ringwald             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
968528a4a3bSMatthias Ringwald             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
969528a4a3bSMatthias Ringwald             btstack_run_loop_add_timer(&hci_stack->timeout);
970e47e68c7SMatthias Ringwald             // send command
971e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
972e47e68c7SMatthias Ringwald             hci_send_cmd(&hci_reset);
973e47e68c7SMatthias Ringwald             break;
9748d29070eSMatthias Ringwald         case HCI_INIT_SEND_RESET_ST_WARM_BOOT:
9758d29070eSMatthias Ringwald             hci_state_reset();
9768d29070eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT;
9778d29070eSMatthias Ringwald             hci_send_cmd(&hci_reset);
9788d29070eSMatthias Ringwald             break;
979fab26ab3SMatthias Ringwald         case HCI_INIT_SEND_BAUD_CHANGE: {
98096b53536SMatthias Ringwald             uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
9813fb36a29SMatthias Ringwald             hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer);
982f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
98374b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
9840305bdeaSMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
9854696bddbSMatthias Ringwald             // STLC25000D: baudrate change happens within 0.5 s after command was send,
9864696bddbSMatthias Ringwald             // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial)
987c6d1dbedSMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
988659d758cSMatthias Ringwald                 btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
989528a4a3bSMatthias Ringwald                 btstack_run_loop_add_timer(&hci_stack->timeout);
9904696bddbSMatthias Ringwald             }
99174b323a9SMatthias Ringwald             break;
992fab26ab3SMatthias Ringwald         }
993fab26ab3SMatthias Ringwald         case HCI_INIT_SEND_BAUD_CHANGE_BCM: {
99496b53536SMatthias Ringwald             uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
9953fb36a29SMatthias Ringwald             hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer);
996f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
997eb3a5314SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM;
998eb3a5314SMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
999eb3a5314SMatthias Ringwald             break;
1000fab26ab3SMatthias Ringwald         }
100174b323a9SMatthias Ringwald         case HCI_INIT_CUSTOM_INIT:
100274b323a9SMatthias Ringwald             // Custom initialization
10033fb36a29SMatthias Ringwald             if (hci_stack->chipset && hci_stack->chipset->next_command){
10043fb36a29SMatthias Ringwald                 int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer);
100574b323a9SMatthias Ringwald                 if (valid_cmd){
100674b323a9SMatthias Ringwald                     int size = 3 + hci_stack->hci_packet_buffer[2];
1007f8fbdce0SMatthias Ringwald                     hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
100874b323a9SMatthias Ringwald                     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size);
1009e47e68c7SMatthias Ringwald                     switch (valid_cmd) {
1010e47e68c7SMatthias Ringwald                         case 1:
1011e47e68c7SMatthias Ringwald                         default:
1012e47e68c7SMatthias Ringwald                             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT;
1013e47e68c7SMatthias Ringwald                             break;
1014e47e68c7SMatthias Ringwald                         case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete
1015e47e68c7SMatthias Ringwald                             log_info("CSR Warm Boot");
1016659d758cSMatthias Ringwald                             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
1017528a4a3bSMatthias Ringwald                             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
1018528a4a3bSMatthias Ringwald                             btstack_run_loop_add_timer(&hci_stack->timeout);
10192caefae9SMatthias Ringwald                             if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO
1020772a36d3SMatthias Ringwald                                 && hci_stack->config
10213fb36a29SMatthias Ringwald                                 && hci_stack->chipset
10223fb36a29SMatthias Ringwald                                 // && hci_stack->chipset->set_baudrate_command -- there's no such command
1023772a36d3SMatthias Ringwald                                 && hci_stack->hci_transport->set_baudrate
10242caefae9SMatthias Ringwald                                 && hci_transport_uart_get_main_baud_rate()){
1025772a36d3SMatthias Ringwald                                 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
1026772a36d3SMatthias Ringwald                             } else {
10279f007422SMatthias Ringwald                                hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET;
1028772a36d3SMatthias Ringwald                             }
1029e47e68c7SMatthias Ringwald                             break;
1030e47e68c7SMatthias Ringwald                     }
10310305bdeaSMatthias Ringwald                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
103274b323a9SMatthias Ringwald                     break;
103374b323a9SMatthias Ringwald                 }
1034148ca237SMatthias Ringwald                 log_info("Init script done");
103592a0d36dSMatthias Ringwald 
103692a0d36dSMatthias Ringwald                 // Init script download causes baud rate to reset on Broadcom chipsets, restore UART baud rate if needed
1037c6d1dbedSMatthias Ringwald                 if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
103892a0d36dSMatthias Ringwald                     int need_baud_change = hci_stack->config
10393fb36a29SMatthias Ringwald                         && hci_stack->chipset
10403fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_baudrate_command
104192a0d36dSMatthias Ringwald                         && hci_stack->hci_transport->set_baudrate
10429796ebeaSMatthias Ringwald                         && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
104392a0d36dSMatthias Ringwald                     if (need_baud_change) {
10449796ebeaSMatthias Ringwald                         uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init;
1045cd724cb7SMatthias Ringwald                         log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate);
104692a0d36dSMatthias Ringwald                         hci_stack->hci_transport->set_baudrate(baud_rate);
104792a0d36dSMatthias Ringwald                     }
104892a0d36dSMatthias Ringwald                 }
104974b323a9SMatthias Ringwald             }
105074b323a9SMatthias Ringwald             // otherwise continue
1051a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
1052a828a756SMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_commands);
1053a828a756SMatthias Ringwald             break;
1054d58dd308SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS:
1055d58dd308SMatthias Ringwald             log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset");
1056d58dd308SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
1057d58dd308SMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_commands);
1058d58dd308SMatthias Ringwald             break;
105953860077SMatthias Ringwald         case HCI_INIT_SET_BD_ADDR:
106053860077SMatthias Ringwald             log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr));
10613fb36a29SMatthias Ringwald             hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer);
1062f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
106353860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR;
106453860077SMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
106553860077SMatthias Ringwald             break;
106653860077SMatthias Ringwald         case HCI_INIT_READ_BD_ADDR:
106753860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR;
106853860077SMatthias Ringwald             hci_send_cmd(&hci_read_bd_addr);
106953860077SMatthias Ringwald             break;
107074b323a9SMatthias Ringwald         case HCI_INIT_READ_BUFFER_SIZE:
107174b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE;
10720305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_buffer_size);
107374b323a9SMatthias Ringwald             break;
107453860077SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES:
107553860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES;
10760305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_features);
107774b323a9SMatthias Ringwald             break;
107874b323a9SMatthias Ringwald         case HCI_INIT_SET_EVENT_MASK:
10790305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK;
108074b323a9SMatthias Ringwald             if (hci_le_supported()){
108174b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
108274b323a9SMatthias Ringwald             } else {
108374b323a9SMatthias Ringwald                 // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
108474b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
108574b323a9SMatthias Ringwald             }
108674b323a9SMatthias Ringwald             break;
108735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
108874b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE:
108974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE;
10900305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
109174b323a9SMatthias Ringwald             break;
109274b323a9SMatthias Ringwald         case HCI_INIT_WRITE_PAGE_TIMEOUT:
109374b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT;
10940305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_page_timeout, 0x6000);  // ca. 15 sec
109574b323a9SMatthias Ringwald             break;
109674b323a9SMatthias Ringwald         case HCI_INIT_WRITE_CLASS_OF_DEVICE:
109774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE;
10980305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
109974b323a9SMatthias Ringwald             break;
110074b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LOCAL_NAME:
11010305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME;
110274b323a9SMatthias Ringwald             if (hci_stack->local_name){
110374b323a9SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
110474b323a9SMatthias Ringwald             } else {
11057224be7eSMatthias Ringwald                 char local_name[8+17+1];
11067224be7eSMatthias Ringwald                 // BTstack 11:22:33:44:55:66
11077224be7eSMatthias Ringwald                 memcpy(local_name, "BTstack ", 8);
11087224be7eSMatthias Ringwald                 memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17);   // strlen(bd_addr_to_str(...)) = 17
11097224be7eSMatthias Ringwald                 local_name[8+17] = '\0';
111093bcd4d0SMatthias Ringwald                 log_info("---> Name %s", local_name);
111193bcd4d0SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, local_name);
111274b323a9SMatthias Ringwald             }
111374b323a9SMatthias Ringwald             break;
1114ff00ed1cSMatthias Ringwald         case HCI_INIT_WRITE_EIR_DATA:
11158a114470SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA;
1116ff00ed1cSMatthias Ringwald             hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data);
1117ff00ed1cSMatthias Ringwald             break;
1118f6858d14SMatthias Ringwald         case HCI_INIT_WRITE_INQUIRY_MODE:
1119f6858d14SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE;
11208a114470SMatthias Ringwald             hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode);
1121f6858d14SMatthias Ringwald             break;
112274b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SCAN_ENABLE:
112374b323a9SMatthias Ringwald             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
112474b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE;
112574b323a9SMatthias Ringwald             break;
1126483c5078SMatthias Ringwald         // only sent if ENABLE_SCO_OVER_HCI is defined
1127729ed62eSMatthias Ringwald         case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE:
1128729ed62eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE;
1129729ed62eSMatthias Ringwald             hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled
1130729ed62eSMatthias Ringwald             break;
1131483c5078SMatthias Ringwald         case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING:
1132483c5078SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING;
1133483c5078SMatthias Ringwald             hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1);
1134483c5078SMatthias Ringwald             break;
113535454696SMatthias Ringwald #endif
1136a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
113774b323a9SMatthias Ringwald         // LE INIT
113874b323a9SMatthias Ringwald         case HCI_INIT_LE_READ_BUFFER_SIZE:
113974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE;
11400305bdeaSMatthias Ringwald             hci_send_cmd(&hci_le_read_buffer_size);
114174b323a9SMatthias Ringwald             break;
114274b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LE_HOST_SUPPORTED:
114374b323a9SMatthias Ringwald             // LE Supported Host = 1, Simultaneous Host = 0
114474b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED;
11450305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
114674b323a9SMatthias Ringwald             break;
11473b6d4121SMatthias Ringwald         case HCI_INIT_READ_WHITE_LIST_SIZE:
11483b6d4121SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE;
11493b6d4121SMatthias Ringwald             hci_send_cmd(&hci_le_read_white_list_size);
11503b6d4121SMatthias Ringwald             break;
115174b323a9SMatthias Ringwald         case HCI_INIT_LE_SET_SCAN_PARAMETERS:
115274b323a9SMatthias Ringwald             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
115374b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS;
11540305bdeaSMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
115574b323a9SMatthias Ringwald             break;
115674b323a9SMatthias Ringwald #endif
115774b323a9SMatthias Ringwald         default:
115874b323a9SMatthias Ringwald             return;
115974b323a9SMatthias Ringwald     }
116055975f88SMatthias Ringwald }
116155975f88SMatthias Ringwald 
1162a650ba4dSMatthias Ringwald static void hci_init_done(void){
1163a650ba4dSMatthias Ringwald     // done. tell the app
1164a650ba4dSMatthias Ringwald     log_info("hci_init_done -> HCI_STATE_WORKING");
1165a650ba4dSMatthias Ringwald     hci_stack->state = HCI_STATE_WORKING;
1166a650ba4dSMatthias Ringwald     hci_emit_state();
1167a650ba4dSMatthias Ringwald     hci_run();
1168a650ba4dSMatthias Ringwald }
1169a650ba4dSMatthias Ringwald 
11706155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
1171a2481739S[email protected]     uint8_t command_completed = 0;
1172c4633093SMatthias Ringwald 
11730e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){
1174f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
11756155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
1176a2481739S[email protected]             command_completed = 1;
1177148ca237SMatthias Ringwald             log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate);
11786155b3d3S[email protected]         } else {
1179d58dd308SMatthias Ringwald             log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate);
11806155b3d3S[email protected]         }
11816155b3d3S[email protected]     }
11820f97eae7SMatthias Ringwald 
11830e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){
11846155b3d3S[email protected]         uint8_t  status = packet[2];
1185f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,4);
11866155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
11876155b3d3S[email protected]             if (status){
1188a2481739S[email protected]                 command_completed = 1;
1189148ca237SMatthias Ringwald                 log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate);
11906155b3d3S[email protected]             } else {
11916155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
11926155b3d3S[email protected]             }
11936155b3d3S[email protected]         } else {
1194148ca237SMatthias Ringwald             log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
11956155b3d3S[email protected]         }
11966155b3d3S[email protected]     }
11970f97eae7SMatthias Ringwald 
1198e47e68c7SMatthias Ringwald     // Vendor == CSR
11990e2df43fSMatthias Ringwald     if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){
1200e47e68c7SMatthias Ringwald         // TODO: track actual command
1201e47e68c7SMatthias Ringwald         command_completed = 1;
1202e47e68c7SMatthias Ringwald     }
1203a2481739S[email protected] 
12044e9daa6fSMatthias Ringwald     // Vendor == Toshiba
12050e2df43fSMatthias Ringwald     if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){
12064e9daa6fSMatthias Ringwald         // TODO: track actual command
12074e9daa6fSMatthias Ringwald         command_completed = 1;
12084e9daa6fSMatthias Ringwald     }
12094e9daa6fSMatthias Ringwald 
12100f97eae7SMatthias Ringwald     // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661:
12110f97eae7SMatthias Ringwald     // Command complete for HCI Reset arrives after we've resent the HCI Reset command
12120f97eae7SMatthias Ringwald     //
12130f97eae7SMatthias Ringwald     // HCI Reset
12140f97eae7SMatthias Ringwald     // Timeout 100 ms
12150f97eae7SMatthias Ringwald     // HCI Reset
12160f97eae7SMatthias Ringwald     // Command Complete Reset
12170f97eae7SMatthias Ringwald     // HCI Read Local Version Information
12180f97eae7SMatthias Ringwald     // Command Complete Reset - but we expected Command Complete Read Local Version Information
12190f97eae7SMatthias Ringwald     // hang...
12200f97eae7SMatthias Ringwald     //
12210f97eae7SMatthias Ringwald     // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend
12220f97eae7SMatthias Ringwald     if (!command_completed
12230e2df43fSMatthias Ringwald             && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE
12240f97eae7SMatthias Ringwald             && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){
12250f97eae7SMatthias Ringwald 
1226f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
12270f97eae7SMatthias Ringwald         if (opcode == hci_reset.opcode){
12280f97eae7SMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION;
12290f97eae7SMatthias Ringwald             return;
12300f97eae7SMatthias Ringwald         }
12310f97eae7SMatthias Ringwald     }
12320f97eae7SMatthias Ringwald 
12339f007422SMatthias Ringwald     // CSR & H5
12349f007422SMatthias Ringwald     // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend
12359f007422SMatthias Ringwald     if (!command_completed
12369f007422SMatthias Ringwald             && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE
12379f007422SMatthias Ringwald             && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){
12389f007422SMatthias Ringwald 
12399f007422SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
12409f007422SMatthias Ringwald         if (opcode == hci_reset.opcode){
12419f007422SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS;
12429f007422SMatthias Ringwald             return;
12439f007422SMatthias Ringwald         }
12449f007422SMatthias Ringwald     }
12459f007422SMatthias Ringwald 
12469f007422SMatthias 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
12479f007422SMatthias Ringwald     // fix: Correct substate and behave as command below
12489f007422SMatthias Ringwald     if (command_completed){
12499f007422SMatthias Ringwald         switch (hci_stack->substate){
12509f007422SMatthias Ringwald             case HCI_INIT_SEND_RESET:
12519f007422SMatthias Ringwald                 hci_stack->substate = HCI_INIT_W4_SEND_RESET;
12529f007422SMatthias Ringwald                 break;
12539f007422SMatthias Ringwald             case HCI_INIT_SEND_RESET_CSR_WARM_BOOT:
12549f007422SMatthias Ringwald                 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
12559f007422SMatthias Ringwald                 break;
12569f007422SMatthias Ringwald             default:
12579f007422SMatthias Ringwald                 break;
12589f007422SMatthias Ringwald         }
12599f007422SMatthias Ringwald     }
12600f97eae7SMatthias Ringwald 
12610f97eae7SMatthias Ringwald 
1262a2481739S[email protected]     if (!command_completed) return;
1263a2481739S[email protected] 
1264db8bc6ffSMatthias Ringwald     int need_baud_change = hci_stack->config
12653fb36a29SMatthias Ringwald                         && hci_stack->chipset
12663fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_baudrate_command
1267db8bc6ffSMatthias Ringwald                         && hci_stack->hci_transport->set_baudrate
12689796ebeaSMatthias Ringwald                         && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
1269db8bc6ffSMatthias Ringwald 
1270db8bc6ffSMatthias Ringwald     int need_addr_change = hci_stack->custom_bd_addr_set
12713fb36a29SMatthias Ringwald                         && hci_stack->chipset
12723fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_bd_addr_command;
1273a80162e9SMatthias Ringwald 
12745c363727SMatthias Ringwald     switch(hci_stack->substate){
12759f007422SMatthias Ringwald         case HCI_INIT_SEND_RESET:
1276d58dd308SMatthias Ringwald             // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET
12779f007422SMatthias Ringwald             // fix: just correct substate and behave as command below
12789f007422SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET;
12799f007422SMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
12809f007422SMatthias Ringwald             break;
128174b323a9SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
1282528a4a3bSMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
128376fcb19bSMatthias Ringwald             break;
1284e90bae01SMatthias Ringwald         case HCI_INIT_W4_SEND_READ_LOCAL_NAME:
1285e90bae01SMatthias Ringwald             log_info("Received local name, need baud change %d", need_baud_change);
12862f48d920SMatthias Ringwald             if (need_baud_change){
12872f48d920SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
12882f48d920SMatthias Ringwald                 return;
12892f48d920SMatthias Ringwald             }
129053860077SMatthias Ringwald             // skip baud change
129174b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
129274b323a9SMatthias Ringwald             return;
1293a80162e9SMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
12944696bddbSMatthias Ringwald             // for STLC2500D, baud rate change already happened.
1295fab26ab3SMatthias Ringwald             // for others, baud rate gets changed now
12967224be7eSMatthias Ringwald             if ((hci_stack->manufacturer != COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){
129796b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1298cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate);
1299fab26ab3SMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
13004696bddbSMatthias Ringwald             }
130174b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
130274b323a9SMatthias Ringwald             return;
1303a80162e9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
1304528a4a3bSMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
1305a80162e9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1306a80162e9SMatthias Ringwald             return;
130774b323a9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT:
130874b323a9SMatthias Ringwald             // repeat custom init
130974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
131074b323a9SMatthias Ringwald             return;
1311a828a756SMatthias Ringwald         case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS:
1312c6d1dbedSMatthias Ringwald             if (need_baud_change && hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
1313eb3a5314SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM;
1314eb3a5314SMatthias Ringwald                 return;
1315eb3a5314SMatthias Ringwald             }
131653860077SMatthias Ringwald             if (need_addr_change){
131753860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
131853860077SMatthias Ringwald                 return;
131953860077SMatthias Ringwald             }
132053860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
132153860077SMatthias Ringwald             return;
13227224be7eSMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM:
13237224be7eSMatthias Ringwald             if (need_baud_change){
132496b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1325cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate);
1326fab26ab3SMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
13277224be7eSMatthias Ringwald             }
1328eb3a5314SMatthias Ringwald             if (need_addr_change){
1329eb3a5314SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
1330eb3a5314SMatthias Ringwald                 return;
1331eb3a5314SMatthias Ringwald             }
1332eb3a5314SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
1333eb3a5314SMatthias Ringwald             return;
133453860077SMatthias Ringwald         case HCI_INIT_W4_SET_BD_ADDR:
133553860077SMatthias Ringwald             // for STLC2500D, bd addr change only gets active after sending reset command
1336c6d1dbedSMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
133753860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT;
133853860077SMatthias Ringwald                 return;
133953860077SMatthias Ringwald             }
134053860077SMatthias Ringwald             // skipping st warm boot
134153860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
134253860077SMatthias Ringwald             return;
134353860077SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT:
134453860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
134553860077SMatthias Ringwald             return;
134653860077SMatthias Ringwald         case HCI_INIT_W4_READ_BD_ADDR:
134753860077SMatthias Ringwald             // only read buffer size if supported
134853860077SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x01) {
134953860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE;
135053860077SMatthias Ringwald                 return;
135153860077SMatthias Ringwald             }
135253860077SMatthias Ringwald             // skipping read buffer size
135353860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES;
1354a828a756SMatthias Ringwald             return;
135574b323a9SMatthias Ringwald         case HCI_INIT_W4_SET_EVENT_MASK:
13566155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
13576155b3d3S[email protected]             if (!hci_classic_supported()){
13586155b3d3S[email protected]                 if (hci_le_supported()){
135974b323a9SMatthias Ringwald                     hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command
136074b323a9SMatthias Ringwald                     return;
13616155b3d3S[email protected]                 } else {
13626155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
1363a650ba4dSMatthias Ringwald                     hci_init_done();
136474b323a9SMatthias Ringwald                     return;
13656155b3d3S[email protected]                 }
13666155b3d3S[email protected]             }
136715a95bd5SMatthias Ringwald             if (!gap_ssp_supported()){
13685c363727SMatthias Ringwald                 hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT;
136974b323a9SMatthias Ringwald                 return;
13706155b3d3S[email protected]             }
13716155b3d3S[email protected]             break;
1372a828a756SMatthias Ringwald         case HCI_INIT_W4_LE_READ_BUFFER_SIZE:
1373a828a756SMatthias Ringwald             // skip write le host if not supported (e.g. on LE only EM9301)
1374a828a756SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x02) break;
1375a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_LE_SET_SCAN_PARAMETERS;
1376a828a756SMatthias Ringwald             return;
1377ff00ed1cSMatthias Ringwald         case HCI_INIT_W4_WRITE_LOCAL_NAME:
1378ff00ed1cSMatthias Ringwald             // skip write eir data if no eir data set
1379ff00ed1cSMatthias Ringwald             if (hci_stack->eir_data) break;
1380ff00ed1cSMatthias Ringwald             hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE;
1381ff00ed1cSMatthias Ringwald             return;
1382729ed62eSMatthias Ringwald 
138371c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI
1384729ed62eSMatthias Ringwald         case HCI_INIT_W4_WRITE_SCAN_ENABLE:
13853905afbfSMatthias Ringwald             // skip write synchronous flow control if not supported
13863905afbfSMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x04) break;
13873905afbfSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE;
13883905afbfSMatthias Ringwald             // explicit fall through to reduce repetitions
13893905afbfSMatthias Ringwald 
1390729ed62eSMatthias Ringwald         case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE:
13913905afbfSMatthias Ringwald             // skip write default erroneous data reporting if not supported
13923905afbfSMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x08) break;
13933905afbfSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING;
13943905afbfSMatthias Ringwald             // explicit fall through to reduce repetitions
13953905afbfSMatthias Ringwald 
1396f064e0bbSMatthias Ringwald         case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING:
1397729ed62eSMatthias Ringwald             if (!hci_le_supported()){
1398729ed62eSMatthias Ringwald                 // SKIP LE init for Classic only configuration
1399a650ba4dSMatthias Ringwald                 hci_init_done();
1400729ed62eSMatthias Ringwald                 return;
1401729ed62eSMatthias Ringwald             }
1402729ed62eSMatthias Ringwald             break;
1403729ed62eSMatthias Ringwald #else
140474b323a9SMatthias Ringwald         case HCI_INIT_W4_WRITE_SCAN_ENABLE:
14056155b3d3S[email protected]             if (!hci_le_supported()){
14066155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
1407a650ba4dSMatthias Ringwald                 hci_init_done();
140874b323a9SMatthias Ringwald                 return;
14096155b3d3S[email protected]             }
1410729ed62eSMatthias Ringwald #endif
1411729ed62eSMatthias Ringwald             break;
1412a650ba4dSMatthias Ringwald         // Response to command before init done state -> init done
1413a650ba4dSMatthias Ringwald         case (HCI_INIT_DONE-1):
1414a650ba4dSMatthias Ringwald             hci_init_done();
1415a650ba4dSMatthias Ringwald             return;
1416a650ba4dSMatthias Ringwald 
14176155b3d3S[email protected]         default:
141874b323a9SMatthias Ringwald             break;
14196155b3d3S[email protected]     }
142055975f88SMatthias Ringwald     hci_initializing_next_state();
14216155b3d3S[email protected] }
14226155b3d3S[email protected] 
142316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1424e76a89eeS[email protected] 
1425e76a89eeS[email protected]     uint16_t event_length = packet[1];
1426e76a89eeS[email protected] 
1427e76a89eeS[email protected]     // assert packet is complete
1428e76a89eeS[email protected]     if (size != event_length + 2){
1429f04a0c31SMatthias Ringwald         log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2);
1430e76a89eeS[email protected]         return;
1431e76a89eeS[email protected]     }
1432e76a89eeS[email protected] 
14331281a47eSmatthias.ringwald     bd_addr_t addr;
143496a45072S[email protected]     bd_addr_type_t addr_type;
14352d00edd4Smatthias.ringwald     uint8_t link_type;
1436fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
14371f7b95a1Smatthias.ringwald     hci_connection_t * conn;
143856cf178bSmatthias.ringwald     int i;
143922909952Smatthias.ringwald 
144035454696SMatthias Ringwald     // warnings
144135454696SMatthias Ringwald     (void) link_type;
144235454696SMatthias Ringwald 
14430e2df43fSMatthias Ringwald     // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet));
14445909f7f2Smatthias.ringwald 
14450e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
144622909952Smatthias.ringwald 
14476772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
14487ec5eeaaSmatthias.ringwald             // get num cmd packets
14499da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u", hci_stack->num_cmd_packets, packet[2]);
14503a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
14517ec5eeaaSmatthias.ringwald 
14529e263bd7SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
14539e263bd7SMatthias Ringwald                 // terminate, name 248 chars
14549e263bd7SMatthias Ringwald                 packet[6+248] = 0;
14559e263bd7SMatthias Ringwald                 log_info("local name: %s", &packet[6]);
14569e263bd7SMatthias Ringwald             }
1457073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){
1458e2edc0c3Smatthias.ringwald                 // from offset 5
1459e2edc0c3Smatthias.ringwald                 // status
14601d279b20Smatthias.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"
1461f8fbdce0SMatthias Ringwald                 hci_stack->acl_data_packet_length = little_endian_read_16(packet, 6);
1462a8b12447S[email protected]                 hci_stack->sco_data_packet_length = packet[8];
1463f8fbdce0SMatthias Ringwald                 hci_stack->acl_packets_total_num  = little_endian_read_16(packet, 9);
1464f8fbdce0SMatthias Ringwald                 hci_stack->sco_packets_total_num  = little_endian_read_16(packet, 11);
1465a8b12447S[email protected] 
14663a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
1467a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
14683a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
14693a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
14708f8108aaSmatthias.ringwald                     }
14711a06f663S[email protected]                     log_info("hci_read_buffer_size: acl used size %u, count %u / sco size %u, count %u",
14721a06f663S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num,
14731a06f663S[email protected]                              hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num);
1474e2edc0c3Smatthias.ringwald                 }
147556cf178bSmatthias.ringwald             }
1476a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
1477073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){
1478f8fbdce0SMatthias Ringwald                 hci_stack->le_data_packets_length = little_endian_read_16(packet, 6);
1479ee303eddS[email protected]                 hci_stack->le_acl_packets_total_num  = packet[8];
14806c26b087S[email protected]                     // determine usable ACL payload size
14816c26b087S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){
14826c26b087S[email protected]                         hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE;
14836c26b087S[email protected]                     }
14849da54300S[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);
148565a46ef3S[email protected]             }
1486073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){
1487f8fbdce0SMatthias Ringwald                 hci_stack->le_whitelist_capacity = little_endian_read_16(packet, 6);
148815d0a15bSMatthias Ringwald                 log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity);
14893b6d4121SMatthias Ringwald             }
149065a46ef3S[email protected] #endif
1491073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) {
1492724d70a2SMatthias Ringwald                 reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1],
1493724d70a2SMatthias Ringwald 				hci_stack->local_bd_addr);
14949da54300S[email protected]                 log_info("Local Address, Status: 0x%02x: Addr: %s",
14953a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
149633373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC
14971624665aSMatthias Ringwald                 if (hci_stack->link_key_db){
14981624665aSMatthias Ringwald                     hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr);
14991624665aSMatthias Ringwald                 }
150033373e40SMatthias Ringwald #endif
1501188981d2Smatthias.ringwald             }
150235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1503073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){
15043a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
1505381fbed8Smatthias.ringwald             }
150635454696SMatthias Ringwald #endif
150735454696SMatthias Ringwald 
150865389bfcS[email protected]             // Note: HCI init checks
1509073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){
15103a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
151165389bfcS[email protected] 
151235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1513a5a23fc2S[email protected]                 // determine usable ACL packet types based on host buffer size and supported features
1514a5a23fc2S[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]);
15158b96126aSMatthias Ringwald                 log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported());
151635454696SMatthias Ringwald #endif
1517f5d8d141S[email protected]                 // Classic/LE
1518f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
1519559e517eS[email protected]             }
1520073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
1521f8fbdce0SMatthias Ringwald                 // hci_stack->hci_version    = little_endian_read_16(packet, 4);
1522f8fbdce0SMatthias Ringwald                 // hci_stack->hci_revision   = little_endian_read_16(packet, 6);
1523f8fbdce0SMatthias Ringwald                 // hci_stack->lmp_version    = little_endian_read_16(packet, 8);
1524f8fbdce0SMatthias Ringwald                 hci_stack->manufacturer   = little_endian_read_16(packet, 10);
1525f8fbdce0SMatthias Ringwald                 // hci_stack->lmp_subversion = little_endian_read_16(packet, 12);
15264696bddbSMatthias Ringwald                 log_info("Manufacturer: 0x%04x", hci_stack->manufacturer);
1527a6ddbcb9SMatthias Ringwald                 // notify app
1528a6ddbcb9SMatthias Ringwald                 if (hci_stack->local_version_information_callback){
1529a6ddbcb9SMatthias Ringwald                     hci_stack->local_version_information_callback(packet);
1530a6ddbcb9SMatthias Ringwald                 }
15314696bddbSMatthias Ringwald             }
1532073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){
1533a828a756SMatthias Ringwald                 hci_stack->local_supported_commands[0] =
15343905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 |  // bit 0 = Octet 14, bit 7
15353905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 |  // bit 1 = Octet 24, bit 6
15363905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 |  // bit 2 = Octet 10, bit 4
15373905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08);        // bit 3 = Octet 18, bit 3
15383905afbfSMatthias Ringwald                     log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]);
1539a828a756SMatthias Ringwald             }
1540073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){
15415b9b590fSMatthias Ringwald                 if (packet[5] == 0){
15425b9b590fSMatthias Ringwald                     hci_stack->synchronous_flow_control_enabled = 1;
15435b9b590fSMatthias Ringwald                 }
15445b9b590fSMatthias Ringwald             }
154556cf178bSmatthias.ringwald             break;
154656cf178bSmatthias.ringwald 
15477ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
15487ec5eeaaSmatthias.ringwald             // get num cmd packets
15499da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]);
15503a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
15517ec5eeaaSmatthias.ringwald             break;
15527ec5eeaaSmatthias.ringwald 
15532e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
15542e440c8aS[email protected]             int offset = 3;
155556cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
1556f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, offset);
15572e440c8aS[email protected]                 offset += 2;
1558f8fbdce0SMatthias Ringwald                 uint16_t num_packets = little_endian_read_16(packet, offset);
15592e440c8aS[email protected]                 offset += 2;
15602e440c8aS[email protected] 
15615061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
156256cf178bSmatthias.ringwald                 if (!conn){
15639da54300S[email protected]                     log_error("hci_number_completed_packet lists unused con handle %u", handle);
156456cf178bSmatthias.ringwald                     continue;
156556cf178bSmatthias.ringwald                 }
156623bed257S[email protected] 
1567e35edcc1S[email protected]                 if (conn->address_type == BD_ADDR_TYPE_SCO){
156835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1569e35edcc1S[email protected]                     if (conn->num_sco_packets_sent >= num_packets){
1570e35edcc1S[email protected]                         conn->num_sco_packets_sent -= num_packets;
1571e35edcc1S[email protected]                     } else {
1572e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more sco slots freed then sent.");
1573e35edcc1S[email protected]                         conn->num_sco_packets_sent = 0;
1574e35edcc1S[email protected]                     }
1575701e3307SMatthias Ringwald                     hci_notify_if_sco_can_send_now();
157635454696SMatthias Ringwald #endif
1577e35edcc1S[email protected]                 } else {
157823bed257S[email protected]                     if (conn->num_acl_packets_sent >= num_packets){
157956cf178bSmatthias.ringwald                         conn->num_acl_packets_sent -= num_packets;
158023bed257S[email protected]                     } else {
1581e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more acl slots freed then sent.");
158223bed257S[email protected]                         conn->num_acl_packets_sent = 0;
158323bed257S[email protected]                     }
1584e35edcc1S[email protected]                 }
15859da54300S[email protected]                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent);
158656cf178bSmatthias.ringwald             }
15876772a24cSmatthias.ringwald             break;
15882e440c8aS[email protected]         }
158935454696SMatthias Ringwald 
159035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
15911f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
1592724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[2], addr);
159337eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
15942d00edd4Smatthias.ringwald             link_type = packet[11];
15959da54300S[email protected]             log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type);
1596e35edcc1S[email protected]             addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO;
15972e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
15981f7b95a1Smatthias.ringwald             if (!conn) {
15995293c072S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, addr_type);
16001f7b95a1Smatthias.ringwald             }
1601ce4c8fabSmatthias.ringwald             if (!conn) {
1602ce4c8fabSmatthias.ringwald                 // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
16033a9fb326S[email protected]                 hci_stack->decline_reason = 0x0d;
1604058e3d6bSMatthias Ringwald                 bd_addr_copy(hci_stack->decline_addr, addr);
1605ce4c8fabSmatthias.ringwald                 break;
1606ce4c8fabSmatthias.ringwald             }
16075cf766e8SMatthias Ringwald             conn->role  = HCI_ROLE_SLAVE;
160832ab9390Smatthias.ringwald             conn->state = RECEIVED_CONNECTION_REQUEST;
1609f3a16b9aSMatthias Ringwald             // store info about eSCO
1610f3a16b9aSMatthias Ringwald             if (link_type == 0x02){
1611f3a16b9aSMatthias Ringwald                 conn->remote_supported_feature_eSCO = 1;
1612f3a16b9aSMatthias Ringwald             }
161332ab9390Smatthias.ringwald             hci_run();
16141f7b95a1Smatthias.ringwald             break;
16151f7b95a1Smatthias.ringwald 
16166772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1617fe1ed1b8Smatthias.ringwald             // Connection management
1618724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], addr);
16199da54300S[email protected]             log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
162096a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
16212e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
1622fe1ed1b8Smatthias.ringwald             if (conn) {
1623b448a0e7Smatthias.ringwald                 if (!packet[2]){
1624c8e4258aSmatthias.ringwald                     conn->state = OPEN;
1625f8fbdce0SMatthias Ringwald                     conn->con_handle = little_endian_read_16(packet, 3);
1626ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
1627ee091cf1Smatthias.ringwald 
1628c785ef68Smatthias.ringwald                     // restart timer
1629528a4a3bSMatthias Ringwald                     btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1630528a4a3bSMatthias Ringwald                     btstack_run_loop_add_timer(&conn->timeout);
1631c785ef68Smatthias.ringwald 
16329da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
163343bfb1bdSmatthias.ringwald 
163443bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
1635b448a0e7Smatthias.ringwald                 } else {
16361bd5283dS[email protected]                     int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED;
16371bd5283dS[email protected]                     uint8_t status = packet[2];
16381bd5283dS[email protected]                     bd_addr_t bd_address;
16391bd5283dS[email protected]                     memcpy(&bd_address, conn->address, 6);
1640ad83dc6aS[email protected] 
1641b448a0e7Smatthias.ringwald                     // connection failed, remove entry
1642665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
1643a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
1644c12e46e7Smatthias.ringwald 
16451bd5283dS[email protected]                     // notify client if dedicated bonding
16461bd5283dS[email protected]                     if (notify_dedicated_bonding_failed){
16471bd5283dS[email protected]                         log_info("hci notify_dedicated_bonding_failed");
16481bd5283dS[email protected]                         hci_emit_dedicated_bonding_result(bd_address, status);
16491bd5283dS[email protected]                     }
16501bd5283dS[email protected] 
1651c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
1652c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
165315a95bd5SMatthias Ringwald                         gap_drop_link_key_for_bd_addr(addr);
1654c12e46e7Smatthias.ringwald                     }
1655fe1ed1b8Smatthias.ringwald                 }
1656fe1ed1b8Smatthias.ringwald             }
16576772a24cSmatthias.ringwald             break;
1658fe1ed1b8Smatthias.ringwald 
165944d0e3d5S[email protected]         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:
1660724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], addr);
166144d0e3d5S[email protected]             log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
16621a06f663S[email protected]             if (packet[2]){
166344d0e3d5S[email protected]                 // connection failed
166444d0e3d5S[email protected]                 break;
166544d0e3d5S[email protected]             }
16662e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1667e35edcc1S[email protected]             if (!conn) {
1668e35edcc1S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1669e35edcc1S[email protected]             }
1670e35edcc1S[email protected]             if (!conn) {
1671e35edcc1S[email protected]                 break;
1672e35edcc1S[email protected]             }
16731a06f663S[email protected]             conn->state = OPEN;
1674f8fbdce0SMatthias Ringwald             conn->con_handle = little_endian_read_16(packet, 3);
167544d0e3d5S[email protected]             break;
167644d0e3d5S[email protected] 
1677afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1678f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1679afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
1680afd4e962S[email protected]             if (!conn) break;
1681afd4e962S[email protected]             if (!packet[2]){
1682afd4e962S[email protected]                 uint8_t * features = &packet[5];
1683afd4e962S[email protected]                 if (features[6] & (1 << 3)){
1684afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
1685afd4e962S[email protected]                 }
168698a2fd1cSMatthias Ringwald                 if (features[3] & (1<<7)){
168798a2fd1cSMatthias Ringwald                     conn->remote_supported_feature_eSCO = 1;
168898a2fd1cSMatthias Ringwald                 }
1689afd4e962S[email protected]             }
1690afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
169198a2fd1cSMatthias Ringwald             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO);
1692ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1693ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1694ad83dc6aS[email protected]             }
1695afd4e962S[email protected]             break;
1696afd4e962S[email protected] 
16977fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
16989da54300S[email protected]             log_info("HCI_EVENT_LINK_KEY_REQUEST");
16997fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
170074d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
1701a98592bcSMatthias Ringwald             if (hci_stack->bondable && !hci_stack->link_key_db) break;
170232ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
170364472d52Smatthias.ringwald             hci_run();
1704d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
170529d53098Smatthias.ringwald             return;
17067fde4af9Smatthias.ringwald 
17079ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
1708724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[2], addr);
17092e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
17109ab95c90S[email protected]             if (!conn) break;
17119ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
17127bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
17139ab95c90S[email protected]             // Change Connection Encryption keeps link key type
17149ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
17159ab95c90S[email protected]                 conn->link_key_type = link_key_type;
17169ab95c90S[email protected]             }
171755597469SMatthias Ringwald             gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type);
171829d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
17197fde4af9Smatthias.ringwald             break;
17209ab95c90S[email protected]         }
17217fde4af9Smatthias.ringwald 
17227fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
17236724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
17244c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
17253a9fb326S[email protected]             if (!hci_stack->bondable){
1726899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
1727f8fb5f6eS[email protected]                 hci_run();
1728f8fb5f6eS[email protected]                 return;
17294c57c146S[email protected]             }
1730d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
1731a98592bcSMatthias Ringwald             if (!hci_stack->link_key_db) break;
1732a6ef64baSMilanka Ringwald             hci_event_pin_code_request_get_bd_addr(packet, addr);
1733a98592bcSMatthias Ringwald             hci_stack->link_key_db->delete_link_key(addr);
17347fde4af9Smatthias.ringwald             break;
17357fde4af9Smatthias.ringwald 
17361d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
17371d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
1738dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
1739dbe1a790S[email protected]             break;
1740dbe1a790S[email protected] 
1741dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
17426724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
17433a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1744dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
1745dbe1a790S[email protected]             break;
1746dbe1a790S[email protected] 
1747dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
17486724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
17493a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1750dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
17511d6b20aeS[email protected]             break;
175235454696SMatthias Ringwald #endif
17531d6b20aeS[email protected] 
1754f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1755f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1756f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1757f0944df2S[email protected]             if (!conn) break;
1758ad83dc6aS[email protected]             if (packet[2] == 0) {
1759f0944df2S[email protected]                 if (packet[5]){
1760f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1761f0944df2S[email protected]                 } else {
1762536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1763f0944df2S[email protected]                 }
1764ad83dc6aS[email protected]             }
176535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1766a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
176735454696SMatthias Ringwald #endif
1768f0944df2S[email protected]             break;
1769f0944df2S[email protected] 
177035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
17711eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
1772f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
17731eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
17741eb2563eS[email protected]             if (!conn) break;
1775ad83dc6aS[email protected] 
1776ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1777ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1778ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1779ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
17801bd5283dS[email protected]                 conn->bonding_status = packet[2];
1781ad83dc6aS[email protected]                 break;
1782ad83dc6aS[email protected]             }
1783ad83dc6aS[email protected] 
1784b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
17851eb2563eS[email protected]                 // link key sufficient for requested security
17861eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1787ad83dc6aS[email protected]                 break;
1788ad83dc6aS[email protected]             }
17891eb2563eS[email protected]             // not enough
17901eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
17911eb2563eS[email protected]             break;
179235454696SMatthias Ringwald #endif
179334d2123cS[email protected] 
179486805605S[email protected]         // HCI_EVENT_DISCONNECTION_COMPLETE
1795ccda6e14S[email protected]         // has been split, to first notify stack before shutting connection down
1796ccda6e14S[email protected]         // see end of function, too.
1797a4f30ec0S[email protected]         case HCI_EVENT_DISCONNECTION_COMPLETE:
1798a4f30ec0S[email protected]             if (packet[2]) break;   // status != 0
1799f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1800c6a37cfdSMatthias Ringwald             // drop outgoing ACL fragments if it is for closed connection
1801c6a37cfdSMatthias Ringwald             if (hci_stack->acl_fragmentation_total_size > 0) {
1802c6a37cfdSMatthias Ringwald                 if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){
1803c6a37cfdSMatthias Ringwald                     log_info("hci: drop fragmented ACL data for closed connection");
1804c6a37cfdSMatthias Ringwald                      hci_stack->acl_fragmentation_total_size = 0;
1805c6a37cfdSMatthias Ringwald                      hci_stack->acl_fragmentation_pos = 0;
1806c6a37cfdSMatthias Ringwald                 }
1807c6a37cfdSMatthias Ringwald             }
180835454696SMatthias Ringwald 
18099a2e4658SMatthias Ringwald             // re-enable advertisements for le connections if active
1810c6a37cfdSMatthias Ringwald             conn = hci_connection_for_handle(handle);
1811c6a37cfdSMatthias Ringwald             if (!conn) break;
181235454696SMatthias Ringwald #ifdef ENABLE_BLE
18139a2e4658SMatthias Ringwald             if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){
18149a2e4658SMatthias Ringwald                 hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
18159a2e4658SMatthias Ringwald             }
181635454696SMatthias Ringwald #endif
1817ccda6e14S[email protected]             conn->state = RECEIVED_DISCONNECTION_COMPLETE;
1818ccda6e14S[email protected]             break;
18196772a24cSmatthias.ringwald 
1820c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
1821313e5f9cSMatthias Ringwald             log_error("Hardware Error: 0x%02x", packet[2]);
1822d23838ecSMatthias Ringwald             if (hci_stack->hardware_error_callback){
1823c2e1fa60SMatthias Ringwald                 (*hci_stack->hardware_error_callback)(packet[2]);
18247586ee35S[email protected]             } else {
18257586ee35S[email protected]                 // if no special requests, just reboot stack
18267586ee35S[email protected]                 hci_power_control_off();
18277586ee35S[email protected]                 hci_power_control_on();
1828c68bdf90Smatthias.ringwald             }
1829c68bdf90Smatthias.ringwald             break;
1830c68bdf90Smatthias.ringwald 
18315cf766e8SMatthias Ringwald         case HCI_EVENT_ROLE_CHANGE:
18325cf766e8SMatthias Ringwald             if (packet[2]) break;   // status != 0
1833f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
18345cf766e8SMatthias Ringwald             conn = hci_connection_for_handle(handle);
18355cf766e8SMatthias Ringwald             if (!conn) break;       // no conn
18365cf766e8SMatthias Ringwald             conn->role = packet[9];
18375cf766e8SMatthias Ringwald             break;
18385cf766e8SMatthias Ringwald 
183963fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
1840d051460cS[email protected]             // release packet buffer only for asynchronous transport and if there are not further fragements
18414fa24b5fS[email protected]             if (hci_transport_synchronous()) {
184263fa3374SMatthias Ringwald                 log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT");
18434fa24b5fS[email protected]                 return; // instead of break: to avoid re-entering hci_run()
18444fa24b5fS[email protected]             }
1845d051460cS[email protected]             if (hci_stack->acl_fragmentation_total_size) break;
1846d051460cS[email protected]             hci_release_packet_buffer();
1847701e3307SMatthias Ringwald 
184863fa3374SMatthias Ringwald             // L2CAP receives this event via the hci_emit_event below
184963fa3374SMatthias Ringwald 
185035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
185163fa3374SMatthias Ringwald             // For SCO, we do the can_send_now_check here
1852701e3307SMatthias Ringwald             hci_notify_if_sco_can_send_now();
185335454696SMatthias Ringwald #endif
18546b4af23dS[email protected]             break;
18556b4af23dS[email protected] 
185635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
185763fa3374SMatthias Ringwald         case HCI_EVENT_SCO_CAN_SEND_NOW:
185863fa3374SMatthias Ringwald             // For SCO, we do the can_send_now_check here
185963fa3374SMatthias Ringwald             hci_notify_if_sco_can_send_now();
186063fa3374SMatthias Ringwald             return;
186135454696SMatthias Ringwald #endif
186263fa3374SMatthias Ringwald 
1863a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
18645909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
18655909f7f2Smatthias.ringwald             switch (packet[2]){
186657c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
18674f4e0224SMatthias Ringwald                     // log_info("advertising report received");
18687bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
186957c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
18707bdc6798S[email protected]                     break;
18715909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
18725909f7f2Smatthias.ringwald                     // Connection management
1873724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[8], addr);
18747bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
18759da54300S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr));
18762e77e513S[email protected]                     conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
187742ff5ba1SMatthias Ringwald                     // if auto-connect, remove from whitelist in both roles
187842ff5ba1SMatthias Ringwald                     if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){
187942ff5ba1SMatthias Ringwald                         hci_remove_from_whitelist(addr_type, addr);
188042ff5ba1SMatthias Ringwald                     }
188142ff5ba1SMatthias Ringwald                     // handle error: error is reported only to the initiator -> outgoing connection
18825909f7f2Smatthias.ringwald                     if (packet[3]){
188342ff5ba1SMatthias Ringwald                         // outgoing connection establishment is done
188442ff5ba1SMatthias Ringwald                         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
188542ff5ba1SMatthias Ringwald                         // remove entry
18865909f7f2Smatthias.ringwald                         if (conn){
1887665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
18885909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
18895909f7f2Smatthias.ringwald                         }
18905909f7f2Smatthias.ringwald                         break;
18915909f7f2Smatthias.ringwald                     }
189242ff5ba1SMatthias Ringwald                     // on success, both hosts receive connection complete event
18935cf766e8SMatthias Ringwald                     if (packet[6] == HCI_ROLE_MASTER){
189442ff5ba1SMatthias Ringwald                         // if we're master, it was an outgoing connection and we're done with it
189542ff5ba1SMatthias Ringwald                         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
189642ff5ba1SMatthias Ringwald                     } else {
189742ff5ba1SMatthias Ringwald                         // if we're slave, it was an incoming connection, advertisements have stopped
1898171293d3SMatthias Ringwald                         hci_stack->le_advertisements_active = 0;
18995106e6dcSMatthias Ringwald                         // try to re-enable them
19005106e6dcSMatthias Ringwald                         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
190142ff5ba1SMatthias Ringwald                     }
1902171293d3SMatthias Ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
190342ff5ba1SMatthias Ringwald                     if (!conn){
190496a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
19055909f7f2Smatthias.ringwald                     }
190642ff5ba1SMatthias Ringwald                     // no memory, sorry.
19075909f7f2Smatthias.ringwald                     if (!conn){
19085909f7f2Smatthias.ringwald                         break;
19095909f7f2Smatthias.ringwald                     }
19105909f7f2Smatthias.ringwald 
19115909f7f2Smatthias.ringwald                     conn->state = OPEN;
19125cf766e8SMatthias Ringwald                     conn->role  = packet[6];
1913f8fbdce0SMatthias Ringwald                     conn->con_handle = little_endian_read_16(packet, 4);
19145909f7f2Smatthias.ringwald 
19155909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
19165909f7f2Smatthias.ringwald 
19175909f7f2Smatthias.ringwald                     // restart timer
1918528a4a3bSMatthias Ringwald                     // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1919528a4a3bSMatthias Ringwald                     // btstack_run_loop_add_timer(&conn->timeout);
19205909f7f2Smatthias.ringwald 
19219da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
19225909f7f2Smatthias.ringwald 
19235909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
19245909f7f2Smatthias.ringwald                     break;
19255909f7f2Smatthias.ringwald 
1926f8fbdce0SMatthias Ringwald             // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]);
192765a46ef3S[email protected] 
19285909f7f2Smatthias.ringwald                 default:
19295909f7f2Smatthias.ringwald                     break;
19305909f7f2Smatthias.ringwald             }
19315909f7f2Smatthias.ringwald             break;
19325909f7f2Smatthias.ringwald #endif
19336772a24cSmatthias.ringwald         default:
19346772a24cSmatthias.ringwald             break;
1935fe1ed1b8Smatthias.ringwald     }
1936fe1ed1b8Smatthias.ringwald 
19373429f56bSmatthias.ringwald     // handle BT initialization
19383a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
19396155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1940c9af4d3fS[email protected]     }
194122909952Smatthias.ringwald 
194289db417bSmatthias.ringwald     // help with BT sleep
19433a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
194474b323a9SMatthias Ringwald         && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE
1945073bd0faSMatthias Ringwald         && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){
194655975f88SMatthias Ringwald         hci_initializing_next_state();
194789db417bSmatthias.ringwald     }
194889db417bSmatthias.ringwald 
194986805605S[email protected]     // notify upper stack
1950d6b06661SMatthias Ringwald 	hci_emit_event(packet, size, 0);   // don't dump, already happened in packet handler
195194ab26f8Smatthias.ringwald 
195286805605S[email protected]     // moved here to give upper stack a chance to close down everything with hci_connection_t intact
19530e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){
195486805605S[email protected]         if (!packet[2]){
1955f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
195605ae8de3SMatthias Ringwald             hci_connection_t * aConn = hci_connection_for_handle(handle);
195705ae8de3SMatthias Ringwald             if (aConn) {
195805ae8de3SMatthias Ringwald                 uint8_t status = aConn->bonding_status;
195905ae8de3SMatthias Ringwald                 uint16_t flags = aConn->bonding_flags;
196086805605S[email protected]                 bd_addr_t bd_address;
196105ae8de3SMatthias Ringwald                 memcpy(&bd_address, aConn->address, 6);
196205ae8de3SMatthias Ringwald                 hci_shutdown_connection(aConn);
1963bb820044S[email protected]                 // connection struct is gone, don't access anymore
1964bb820044S[email protected]                 if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){
196586805605S[email protected]                     hci_emit_dedicated_bonding_result(bd_address, status);
196686805605S[email protected]                 }
196786805605S[email protected]             }
196886805605S[email protected]         }
196986805605S[email protected]     }
197086805605S[email protected] 
197194ab26f8Smatthias.ringwald 	// execute main loop
197294ab26f8Smatthias.ringwald 	hci_run();
197316833f0aSmatthias.ringwald }
197416833f0aSmatthias.ringwald 
197535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1976c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){
19778abbe8b5SMatthias Ringwald     if (!hci_stack->sco_packet_handler) return;
19783d50b4baSMatthias Ringwald     hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size);
1979c91d150bS[email protected] }
198035454696SMatthias Ringwald #endif
1981c91d150bS[email protected] 
19820a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
19835bb5bc3eS[email protected]     hci_dump_packet(packet_type, 1, packet, size);
198410e830c9Smatthias.ringwald     switch (packet_type) {
198510e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
198610e830c9Smatthias.ringwald             event_handler(packet, size);
198710e830c9Smatthias.ringwald             break;
198810e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
198910e830c9Smatthias.ringwald             acl_handler(packet, size);
199010e830c9Smatthias.ringwald             break;
199135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1992c91d150bS[email protected]         case HCI_SCO_DATA_PACKET:
1993c91d150bS[email protected]             sco_handler(packet, size);
1994202c8a4cSMatthias Ringwald             break;
199535454696SMatthias Ringwald #endif
199610e830c9Smatthias.ringwald         default:
199710e830c9Smatthias.ringwald             break;
199810e830c9Smatthias.ringwald     }
199910e830c9Smatthias.ringwald }
200010e830c9Smatthias.ringwald 
2001d6b06661SMatthias Ringwald /**
2002d6b06661SMatthias Ringwald  * @brief Add event packet handler.
2003d6b06661SMatthias Ringwald  */
2004d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
2005d6b06661SMatthias Ringwald     btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler);
2006d6b06661SMatthias Ringwald }
2007d6b06661SMatthias Ringwald 
2008d6b06661SMatthias Ringwald 
2009fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
20103d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){
2011fb37a842SMatthias Ringwald     hci_stack->acl_packet_handler = handler;
201216833f0aSmatthias.ringwald }
201316833f0aSmatthias.ringwald 
201435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
20158abbe8b5SMatthias Ringwald /**
20168abbe8b5SMatthias Ringwald  * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles.
20178abbe8b5SMatthias Ringwald  */
20183d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){
20198abbe8b5SMatthias Ringwald     hci_stack->sco_packet_handler = handler;
20208abbe8b5SMatthias Ringwald }
202135454696SMatthias Ringwald #endif
20228abbe8b5SMatthias Ringwald 
202371de195eSMatthias Ringwald static void hci_state_reset(void){
2024595bdbfbS[email protected]     // no connections yet
2025595bdbfbS[email protected]     hci_stack->connections = NULL;
202674308b23Smatthias.ringwald 
202774308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
202874308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
202974308b23Smatthias.ringwald     // hci_stack->connectable = 0;
203074308b23Smatthias.ringwald     // hci_stack->bondable = 1;
2031595bdbfbS[email protected] 
203244935e40S[email protected]     // buffer is free
203344935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
203444935e40S[email protected] 
2035595bdbfbS[email protected]     // no pending cmds
2036595bdbfbS[email protected]     hci_stack->decline_reason = 0;
2037595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
2038595bdbfbS[email protected] 
2039595bdbfbS[email protected]     // LE
2040595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
204152b551c3SMatthias Ringwald     hci_stack->le_advertisements_random_address_set = 0;
2042595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
2043595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
2044e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
2045b04dfa37SMatthias Ringwald     hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
2046e83201bcSMatthias Ringwald     hci_stack->le_whitelist = 0;
2047e83201bcSMatthias Ringwald     hci_stack->le_whitelist_capacity = 0;
2048e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_interval_min =          6;
2049e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_interval_max =       3200;
2050e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_latency_min =           0;
2051e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_latency_max =         500;
2052e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_supervision_timeout_min =   10;
2053e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200;
2054595bdbfbS[email protected] }
2055595bdbfbS[email protected] 
205635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
20572d5e09d6SMatthias Ringwald /**
20582d5e09d6SMatthias Ringwald  * @brief Configure Bluetooth hardware control. Has to be called before power on.
20592d5e09d6SMatthias Ringwald  */
20602d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){
20612d5e09d6SMatthias Ringwald     // store and open remote device db
20622d5e09d6SMatthias Ringwald     hci_stack->link_key_db = link_key_db;
20632d5e09d6SMatthias Ringwald     if (hci_stack->link_key_db) {
20642d5e09d6SMatthias Ringwald         hci_stack->link_key_db->open();
20652d5e09d6SMatthias Ringwald     }
20662d5e09d6SMatthias Ringwald }
206735454696SMatthias Ringwald #endif
20682d5e09d6SMatthias Ringwald 
20692d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){
2070475c8125Smatthias.ringwald 
20713a9fb326S[email protected] #ifdef HAVE_MALLOC
20723a9fb326S[email protected]     if (!hci_stack) {
20733a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
20743a9fb326S[email protected]     }
20753a9fb326S[email protected] #else
20763a9fb326S[email protected]     hci_stack = &hci_stack_static;
20773a9fb326S[email protected] #endif
207866fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
20793a9fb326S[email protected] 
2080475c8125Smatthias.ringwald     // reference to use transport layer implementation
20813a9fb326S[email protected]     hci_stack->hci_transport = transport;
2082475c8125Smatthias.ringwald 
208311e23e5fSmatthias.ringwald     // reference to used config
20843a9fb326S[email protected]     hci_stack->config = config;
208511e23e5fSmatthias.ringwald 
208626a9b6daSMatthias Ringwald     // setup pointer for outgoing packet buffer
208726a9b6daSMatthias Ringwald     hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE];
208826a9b6daSMatthias Ringwald 
20898fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
20903a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
20918fcba05dSmatthias.ringwald 
209216833f0aSmatthias.ringwald     // register packet handlers with transport
209310e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
2094f5454fc6Smatthias.ringwald 
20953a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
2096e2386ba1S[email protected] 
2097e2386ba1S[email protected]     // class of device
20983a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
2099a45d6b9fS[email protected] 
2100f20168b8Smatthias.ringwald     // bondable by default
2101f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
2102f20168b8Smatthias.ringwald 
210363048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
21043a9fb326S[email protected]     hci_stack->ssp_enable = 1;
21053a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
21063a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
21073a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
210869a97523S[email protected] 
2109d950d659SMatthias Ringwald     // voice setting - signed 8 bit pcm data with CVSD over the air
2110d950d659SMatthias Ringwald     hci_stack->sco_voice_setting = 0x40;
2111d950d659SMatthias Ringwald 
2112595bdbfbS[email protected]     hci_state_reset();
2113475c8125Smatthias.ringwald }
2114475c8125Smatthias.ringwald 
21153fb36a29SMatthias Ringwald /**
21163fb36a29SMatthias Ringwald  * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information
21173fb36a29SMatthias Ringwald  */
21183fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){
21193fb36a29SMatthias Ringwald     hci_stack->chipset = chipset_driver;
21203fb36a29SMatthias Ringwald 
21213fb36a29SMatthias Ringwald     // reset chipset driver - init is also called on power_up
21223fb36a29SMatthias Ringwald     if (hci_stack->chipset && hci_stack->chipset->init){
21233fb36a29SMatthias Ringwald         hci_stack->chipset->init(hci_stack->config);
21243fb36a29SMatthias Ringwald     }
21253fb36a29SMatthias Ringwald }
21263fb36a29SMatthias Ringwald 
2127fb55bd0aSMatthias Ringwald /**
2128d0b87befSMatthias Ringwald  * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on.
2129fb55bd0aSMatthias Ringwald  */
2130fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){
2131fb55bd0aSMatthias Ringwald     // references to used control implementation
2132fb55bd0aSMatthias Ringwald     hci_stack->control = hardware_control;
2133d0b87befSMatthias Ringwald     // init with transport config
2134d0b87befSMatthias Ringwald     hardware_control->init(hci_stack->config);
2135fb55bd0aSMatthias Ringwald }
2136fb55bd0aSMatthias Ringwald 
213771de195eSMatthias Ringwald void hci_close(void){
2138404843c1Smatthias.ringwald     // close remote device db
2139a98592bcSMatthias Ringwald     if (hci_stack->link_key_db) {
2140a98592bcSMatthias Ringwald         hci_stack->link_key_db->close();
2141404843c1Smatthias.ringwald     }
21427224be7eSMatthias Ringwald 
21437224be7eSMatthias Ringwald     btstack_linked_list_iterator_t lit;
21447224be7eSMatthias Ringwald     btstack_linked_list_iterator_init(&lit, &hci_stack->connections);
21457224be7eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&lit)){
21467224be7eSMatthias Ringwald         // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection
21477224be7eSMatthias Ringwald         hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit);
21487224be7eSMatthias Ringwald         hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host
21497224be7eSMatthias Ringwald         hci_shutdown_connection(connection);
2150f5454fc6Smatthias.ringwald     }
21517224be7eSMatthias Ringwald 
2152f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
21533a9fb326S[email protected] 
21543a9fb326S[email protected] #ifdef HAVE_MALLOC
21553a9fb326S[email protected]     free(hci_stack);
21563a9fb326S[email protected] #endif
21573a9fb326S[email protected]     hci_stack = NULL;
2158404843c1Smatthias.ringwald }
2159404843c1Smatthias.ringwald 
216035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
216160b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){
21629e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
21639e61646fS[email protected] }
216435454696SMatthias Ringwald #endif
21659e61646fS[email protected] 
2166f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h
2167f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){
2168f456b2d0S[email protected]     memcpy(hci_stack->custom_bd_addr, addr, 6);
2169f456b2d0S[email protected]     hci_stack->custom_bd_addr_set = 1;
2170f456b2d0S[email protected] }
2171f456b2d0S[email protected] 
217271de195eSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){
217366fb9560S[email protected]     disable_l2cap_timeouts = 1;
217466fb9560S[email protected] }
21758d213e1aSmatthias.ringwald // State-Module-Driver overview
21768d213e1aSmatthias.ringwald // state                    module  low-level
21778d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
21788d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
21798d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
21808d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
2181d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
2182d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
2183c7e0c5f6Smatthias.ringwald 
218440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
21857301ad89Smatthias.ringwald 
2186038bc64cSmatthias.ringwald     // power on
2187f9a30166Smatthias.ringwald     int err = 0;
21883a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
2189d0b87befSMatthias Ringwald         err = (*hci_stack->control->on)();
2190f9a30166Smatthias.ringwald     }
2191038bc64cSmatthias.ringwald     if (err){
21929da54300S[email protected]         log_error( "POWER_ON failed");
2193038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
2194038bc64cSmatthias.ringwald         return err;
2195038bc64cSmatthias.ringwald     }
2196038bc64cSmatthias.ringwald 
219724b3c629SMatthias Ringwald     // int chipset driver
21983fb36a29SMatthias Ringwald     if (hci_stack->chipset && hci_stack->chipset->init){
21993fb36a29SMatthias Ringwald         hci_stack->chipset->init(hci_stack->config);
22003fb36a29SMatthias Ringwald     }
22013fb36a29SMatthias Ringwald 
220224b3c629SMatthias Ringwald     // init transport
220324b3c629SMatthias Ringwald     if (hci_stack->hci_transport->init){
220424b3c629SMatthias Ringwald         hci_stack->hci_transport->init(hci_stack->config);
220524b3c629SMatthias Ringwald     }
220624b3c629SMatthias Ringwald 
220724b3c629SMatthias Ringwald     // open transport
220824b3c629SMatthias Ringwald     err = hci_stack->hci_transport->open();
2209038bc64cSmatthias.ringwald     if (err){
22109da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
22113a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
2212d0b87befSMatthias Ringwald             (*hci_stack->control->off)();
2213f9a30166Smatthias.ringwald         }
2214038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
2215038bc64cSmatthias.ringwald         return err;
2216038bc64cSmatthias.ringwald     }
22178d213e1aSmatthias.ringwald     return 0;
22188d213e1aSmatthias.ringwald }
2219038bc64cSmatthias.ringwald 
222040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
22218d213e1aSmatthias.ringwald 
22229da54300S[email protected]     log_info("hci_power_control_off");
22239418f9c9Smatthias.ringwald 
22248d213e1aSmatthias.ringwald     // close low-level device
222524b3c629SMatthias Ringwald     hci_stack->hci_transport->close();
22268d213e1aSmatthias.ringwald 
22279da54300S[email protected]     log_info("hci_power_control_off - hci_transport closed");
22289418f9c9Smatthias.ringwald 
22298d213e1aSmatthias.ringwald     // power off
22303a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
2231d0b87befSMatthias Ringwald         (*hci_stack->control->off)();
22328d213e1aSmatthias.ringwald     }
22339418f9c9Smatthias.ringwald 
22349da54300S[email protected]     log_info("hci_power_control_off - control closed");
22359418f9c9Smatthias.ringwald 
22363a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
223772ea5239Smatthias.ringwald }
223872ea5239Smatthias.ringwald 
223940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
224072ea5239Smatthias.ringwald 
22419da54300S[email protected]     log_info("hci_power_control_sleep");
22423144bce4Smatthias.ringwald 
2243b429b9b7Smatthias.ringwald #if 0
2244b429b9b7Smatthias.ringwald     // don't close serial port during sleep
2245b429b9b7Smatthias.ringwald 
224672ea5239Smatthias.ringwald     // close low-level device
22473a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
2248b429b9b7Smatthias.ringwald #endif
224972ea5239Smatthias.ringwald 
225072ea5239Smatthias.ringwald     // sleep mode
22513a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
2252d0b87befSMatthias Ringwald         (*hci_stack->control->sleep)();
225372ea5239Smatthias.ringwald     }
2254b429b9b7Smatthias.ringwald 
22553a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
22568d213e1aSmatthias.ringwald }
22578d213e1aSmatthias.ringwald 
225840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
2259b429b9b7Smatthias.ringwald 
22609da54300S[email protected]     log_info("hci_power_control_wake");
2261b429b9b7Smatthias.ringwald 
2262b429b9b7Smatthias.ringwald     // wake on
22633a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
2264d0b87befSMatthias Ringwald         (*hci_stack->control->wake)();
2265b429b9b7Smatthias.ringwald     }
2266b429b9b7Smatthias.ringwald 
2267b429b9b7Smatthias.ringwald #if 0
2268b429b9b7Smatthias.ringwald     // open low-level device
22693a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
2270b429b9b7Smatthias.ringwald     if (err){
22719da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
22723a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
2273d0b87befSMatthias Ringwald             (*hci_stack->control->off)();
2274b429b9b7Smatthias.ringwald         }
2275b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
2276b429b9b7Smatthias.ringwald         return err;
2277b429b9b7Smatthias.ringwald     }
2278b429b9b7Smatthias.ringwald #endif
2279b429b9b7Smatthias.ringwald 
2280b429b9b7Smatthias.ringwald     return 0;
2281b429b9b7Smatthias.ringwald }
2282b429b9b7Smatthias.ringwald 
228344935e40S[email protected] static void hci_power_transition_to_initializing(void){
228444935e40S[email protected]     // set up state machine
228544935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
228644935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
228744935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
22885c363727SMatthias Ringwald     hci_stack->substate = HCI_INIT_SEND_RESET;
228944935e40S[email protected] }
2290b429b9b7Smatthias.ringwald 
22918d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
2292d661ed19Smatthias.ringwald 
2293f04a0c31SMatthias Ringwald     log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state);
2294d661ed19Smatthias.ringwald 
22958d213e1aSmatthias.ringwald     int err = 0;
22963a9fb326S[email protected]     switch (hci_stack->state){
22978d213e1aSmatthias.ringwald 
22988d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
22998d213e1aSmatthias.ringwald             switch (power_mode){
23008d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
23018d213e1aSmatthias.ringwald                     err = hci_power_control_on();
230297b61c7bS[email protected]                     if (err) {
2303f04a0c31SMatthias Ringwald                         log_error("hci_power_control_on() error %d", err);
230497b61c7bS[email protected]                         return err;
230597b61c7bS[email protected]                     }
230644935e40S[email protected]                     hci_power_transition_to_initializing();
23078d213e1aSmatthias.ringwald                     break;
23088d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
23098d213e1aSmatthias.ringwald                     // do nothing
23108d213e1aSmatthias.ringwald                     break;
23118d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2312b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
23138d213e1aSmatthias.ringwald                     break;
23148d213e1aSmatthias.ringwald             }
23158d213e1aSmatthias.ringwald             break;
23167301ad89Smatthias.ringwald 
23178d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
23188d213e1aSmatthias.ringwald             switch (power_mode){
23198d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
23208d213e1aSmatthias.ringwald                     // do nothing
23218d213e1aSmatthias.ringwald                     break;
23228d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
23238d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
23248d213e1aSmatthias.ringwald                     hci_power_control_off();
23258d213e1aSmatthias.ringwald                     break;
23268d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2327b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
232872ea5239Smatthias.ringwald                     hci_power_control_sleep();
23298d213e1aSmatthias.ringwald                     break;
23308d213e1aSmatthias.ringwald             }
23318d213e1aSmatthias.ringwald             break;
23327301ad89Smatthias.ringwald 
23338d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
23348d213e1aSmatthias.ringwald             switch (power_mode){
23358d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
23368d213e1aSmatthias.ringwald                     // do nothing
23378d213e1aSmatthias.ringwald                     break;
23388d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
2339c7e0c5f6Smatthias.ringwald                     // see hci_run
23403a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
23418d213e1aSmatthias.ringwald                     break;
23428d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2343b546ac54Smatthias.ringwald                     // see hci_run
23443a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
234574b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
23468d213e1aSmatthias.ringwald                     break;
23478d213e1aSmatthias.ringwald             }
23488d213e1aSmatthias.ringwald             break;
23497301ad89Smatthias.ringwald 
23508d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
23518d213e1aSmatthias.ringwald             switch (power_mode){
23528d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
235344935e40S[email protected]                     hci_power_transition_to_initializing();
23548d213e1aSmatthias.ringwald                     break;
23558d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
23568d213e1aSmatthias.ringwald                     // do nothing
23578d213e1aSmatthias.ringwald                     break;
23588d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2359b546ac54Smatthias.ringwald                     // see hci_run
23603a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
236174b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
23628d213e1aSmatthias.ringwald                     break;
23638d213e1aSmatthias.ringwald             }
23648d213e1aSmatthias.ringwald             break;
23658d213e1aSmatthias.ringwald 
23668d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
23678d213e1aSmatthias.ringwald             switch (power_mode){
23688d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
236928171530Smatthias.ringwald 
2370423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
237128171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
2372d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
23733a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
237474b323a9SMatthias Ringwald                         hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE;   // init after sleep
237528171530Smatthias.ringwald                         break;
237628171530Smatthias.ringwald                     }
237728171530Smatthias.ringwald #endif
237844935e40S[email protected]                     hci_power_transition_to_initializing();
23798d213e1aSmatthias.ringwald                     break;
23808d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
2381b546ac54Smatthias.ringwald                     // see hci_run
23823a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
23838d213e1aSmatthias.ringwald                     break;
23848d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2385b546ac54Smatthias.ringwald                     // do nothing
23868d213e1aSmatthias.ringwald                     break;
23878d213e1aSmatthias.ringwald             }
23888d213e1aSmatthias.ringwald             break;
23898d213e1aSmatthias.ringwald 
23908d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
23918d213e1aSmatthias.ringwald             switch (power_mode){
23928d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
239328171530Smatthias.ringwald 
2394423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
239528171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
2396d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
23973a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
23985c363727SMatthias Ringwald                         hci_stack->substate = HCI_INIT_AFTER_SLEEP;
2399758b46ceSmatthias.ringwald                         hci_update_scan_enable();
240028171530Smatthias.ringwald                         break;
240128171530Smatthias.ringwald                     }
240228171530Smatthias.ringwald #endif
24033144bce4Smatthias.ringwald                     err = hci_power_control_wake();
24043144bce4Smatthias.ringwald                     if (err) return err;
240544935e40S[email protected]                     hci_power_transition_to_initializing();
24068d213e1aSmatthias.ringwald                     break;
24078d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
24083a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
24098d213e1aSmatthias.ringwald                     break;
24108d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2411b546ac54Smatthias.ringwald                     // do nothing
24128d213e1aSmatthias.ringwald                     break;
24138d213e1aSmatthias.ringwald             }
24148d213e1aSmatthias.ringwald             break;
241511e23e5fSmatthias.ringwald     }
241668d92d03Smatthias.ringwald 
2417038bc64cSmatthias.ringwald     // create internal event
2418ee8bf225Smatthias.ringwald 	hci_emit_state();
2419ee8bf225Smatthias.ringwald 
242068d92d03Smatthias.ringwald 	// trigger next/first action
242168d92d03Smatthias.ringwald 	hci_run();
242268d92d03Smatthias.ringwald 
2423475c8125Smatthias.ringwald     return 0;
2424475c8125Smatthias.ringwald }
2425475c8125Smatthias.ringwald 
242635454696SMatthias Ringwald 
242735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
242835454696SMatthias Ringwald 
2429758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
2430758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
24313a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
2432758b46ceSmatthias.ringwald     hci_run();
2433758b46ceSmatthias.ringwald }
2434758b46ceSmatthias.ringwald 
243515a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){
2436381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
2437381fbed8Smatthias.ringwald 
24383a9fb326S[email protected]     if (hci_stack->discoverable == enable){
24393a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
2440381fbed8Smatthias.ringwald         return;
2441381fbed8Smatthias.ringwald     }
2442381fbed8Smatthias.ringwald 
24433a9fb326S[email protected]     hci_stack->discoverable = enable;
2444758b46ceSmatthias.ringwald     hci_update_scan_enable();
2445758b46ceSmatthias.ringwald }
2446b031bebbSmatthias.ringwald 
244715a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){
2448758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
2449758b46ceSmatthias.ringwald 
2450758b46ceSmatthias.ringwald     // don't emit event
24513a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
2452758b46ceSmatthias.ringwald 
24533a9fb326S[email protected]     hci_stack->connectable = enable;
2454758b46ceSmatthias.ringwald     hci_update_scan_enable();
2455381fbed8Smatthias.ringwald }
245635454696SMatthias Ringwald #endif
2457381fbed8Smatthias.ringwald 
245815a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){
2459690bd0baS[email protected]     memcpy(address_buffer, hci_stack->local_bd_addr, 6);
24605061f3afS[email protected] }
24615061f3afS[email protected] 
246295d71764SMatthias Ringwald static void hci_run(void){
24638a485f27Smatthias.ringwald 
2464db8bc6ffSMatthias Ringwald     // log_info("hci_run: entered");
2465665d90f2SMatthias Ringwald     btstack_linked_item_t * it;
246632ab9390Smatthias.ringwald 
2467b5d8b22bS[email protected]     // send continuation fragments first, as they block the prepared packet buffer
2468b5d8b22bS[email protected]     if (hci_stack->acl_fragmentation_total_size > 0) {
2469b5d8b22bS[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer);
2470b5d8b22bS[email protected]         hci_connection_t *connection = hci_connection_for_handle(con_handle);
2471b5d8b22bS[email protected]         if (connection) {
247228a0332dSMatthias Ringwald             if (hci_can_send_prepared_acl_packet_now(con_handle)){
2473b5d8b22bS[email protected]                 hci_send_acl_packet_fragments(connection);
2474b5d8b22bS[email protected]                 return;
2475b5d8b22bS[email protected]             }
247628a0332dSMatthias Ringwald         } else {
2477b5d8b22bS[email protected]             // connection gone -> discard further fragments
247828a0332dSMatthias Ringwald             log_info("hci_run: fragmented ACL packet no connection -> discard fragment");
2479b5d8b22bS[email protected]             hci_stack->acl_fragmentation_total_size = 0;
2480b5d8b22bS[email protected]             hci_stack->acl_fragmentation_pos = 0;
2481b5d8b22bS[email protected]         }
2482b5d8b22bS[email protected]     }
2483b5d8b22bS[email protected] 
2484d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()) return;
2485ce4c8fabSmatthias.ringwald 
2486b031bebbSmatthias.ringwald     // global/non-connection oriented commands
2487b031bebbSmatthias.ringwald 
248835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2489b031bebbSmatthias.ringwald     // decline incoming connections
24903a9fb326S[email protected]     if (hci_stack->decline_reason){
24913a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
24923a9fb326S[email protected]         hci_stack->decline_reason = 0;
24933a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
2494dbe1a790S[email protected]         return;
2495ce4c8fabSmatthias.ringwald     }
2496ce4c8fabSmatthias.ringwald 
2497b031bebbSmatthias.ringwald     // send scan enable
24983a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
24993a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
25003a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
2501dbe1a790S[email protected]         return;
2502b031bebbSmatthias.ringwald     }
250335454696SMatthias Ringwald #endif
2504b031bebbSmatthias.ringwald 
2505a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
25067bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
250745c102fdSMatthias Ringwald         // handle le scan
25087bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
25097bdc6798S[email protected]             case LE_START_SCAN:
25107bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
25117bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
25127bdc6798S[email protected]                 return;
25137bdc6798S[email protected] 
25147bdc6798S[email protected]             case LE_STOP_SCAN:
25157bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
25167bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
25177bdc6798S[email protected]                 return;
25187bdc6798S[email protected]             default:
25197bdc6798S[email protected]                 break;
25207bdc6798S[email protected]         }
2521e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
2522e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
2523e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
2524e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
2525e2602ea2Smatthias.ringwald             hci_send_cmd(&hci_le_set_scan_parameters, scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->adv_addr_type, 0);
2526e2602ea2Smatthias.ringwald             return;
2527e2602ea2Smatthias.ringwald         }
252845c102fdSMatthias Ringwald         // le advertisement control
25299a2e4658SMatthias Ringwald         if (hci_stack->le_advertisements_todo){
25309a2e4658SMatthias Ringwald             log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo );
25319a2e4658SMatthias Ringwald         }
253245c102fdSMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){
253345c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE;
253445c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertise_enable, 0);
253545c102fdSMatthias Ringwald             return;
253645c102fdSMatthias Ringwald         }
253745c102fdSMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){
253845c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS;
253945c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertising_parameters,
254045c102fdSMatthias Ringwald                  hci_stack->le_advertisements_interval_min,
254145c102fdSMatthias Ringwald                  hci_stack->le_advertisements_interval_max,
254245c102fdSMatthias Ringwald                  hci_stack->le_advertisements_type,
254345c102fdSMatthias Ringwald                  hci_stack->le_advertisements_own_address_type,
254445c102fdSMatthias Ringwald                  hci_stack->le_advertisements_direct_address_type,
254545c102fdSMatthias Ringwald                  hci_stack->le_advertisements_direct_address,
254645c102fdSMatthias Ringwald                  hci_stack->le_advertisements_channel_map,
254745c102fdSMatthias Ringwald                  hci_stack->le_advertisements_filter_policy);
254845c102fdSMatthias Ringwald             return;
254945c102fdSMatthias Ringwald         }
2550501f56b3SMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){
2551501f56b3SMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA;
255245c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len,
255345c102fdSMatthias Ringwald                 hci_stack->le_advertisements_data);
255445c102fdSMatthias Ringwald             return;
255545c102fdSMatthias Ringwald         }
2556501f56b3SMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){
2557501f56b3SMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA;
2558501f56b3SMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len,
2559501f56b3SMatthias Ringwald                 hci_stack->le_scan_response_data);
2560501f56b3SMatthias Ringwald             return;
2561501f56b3SMatthias Ringwald         }
256252b551c3SMatthias Ringwald         // Random address needs to be set before enabling advertisements
256352b551c3SMatthias Ringwald         if ((hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE)
256452b551c3SMatthias Ringwald         &&  (hci_stack->le_advertisements_own_address_type == 0 || hci_stack->le_advertisements_random_address_set)){
256545c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE;
256645c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertise_enable, 1);
256745c102fdSMatthias Ringwald             return;
256845c102fdSMatthias Ringwald         }
25699956955bSMatthias Ringwald 
25709956955bSMatthias Ringwald         //
25719956955bSMatthias Ringwald         // LE Whitelist Management
25729956955bSMatthias Ringwald         //
25739956955bSMatthias Ringwald 
25749956955bSMatthias Ringwald         // check if whitelist needs modification
2575665d90f2SMatthias Ringwald         btstack_linked_list_iterator_t lit;
25769956955bSMatthias Ringwald         int modification_pending = 0;
2577665d90f2SMatthias Ringwald         btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2578665d90f2SMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&lit)){
2579665d90f2SMatthias Ringwald             whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
25809956955bSMatthias Ringwald             if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){
25819956955bSMatthias Ringwald                 modification_pending = 1;
25829956955bSMatthias Ringwald                 break;
25839956955bSMatthias Ringwald             }
25849956955bSMatthias Ringwald         }
258591915b0bSMatthias Ringwald 
25869956955bSMatthias Ringwald         if (modification_pending){
258791915b0bSMatthias Ringwald             // stop connnecting if modification pending
25889956955bSMatthias Ringwald             if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){
25899956955bSMatthias Ringwald                 hci_send_cmd(&hci_le_create_connection_cancel);
25909956955bSMatthias Ringwald                 return;
25919956955bSMatthias Ringwald             }
25929956955bSMatthias Ringwald 
25939956955bSMatthias Ringwald             // add/remove entries
2594665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2595665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&lit)){
2596665d90f2SMatthias Ringwald                 whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
25979956955bSMatthias Ringwald                 if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){
25989956955bSMatthias Ringwald                     entry->state = LE_WHITELIST_ON_CONTROLLER;
25999956955bSMatthias Ringwald                     hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address);
26009956955bSMatthias Ringwald                     return;
26019956955bSMatthias Ringwald 
26029956955bSMatthias Ringwald                 }
26039956955bSMatthias Ringwald                 if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){
26049ecbe201SMatthias Ringwald                     bd_addr_t address;
26059ecbe201SMatthias Ringwald                     bd_addr_type_t address_type = entry->address_type;
26069ecbe201SMatthias Ringwald                     memcpy(address, entry->address, 6);
2607665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry);
26089956955bSMatthias Ringwald                     btstack_memory_whitelist_entry_free(entry);
26099ecbe201SMatthias Ringwald                     hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address);
26109956955bSMatthias Ringwald                     return;
26119956955bSMatthias Ringwald                 }
26129956955bSMatthias Ringwald             }
261391915b0bSMatthias Ringwald         }
26149956955bSMatthias Ringwald 
26159956955bSMatthias Ringwald         // start connecting
261691915b0bSMatthias Ringwald         if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE &&
2617665d90f2SMatthias Ringwald             !btstack_linked_list_empty(&hci_stack->le_whitelist)){
26189956955bSMatthias Ringwald             bd_addr_t null_addr;
26199956955bSMatthias Ringwald             memset(null_addr, 0, 6);
26209956955bSMatthias Ringwald             hci_send_cmd(&hci_le_create_connection,
26219956955bSMatthias Ringwald                  0x0060,    // scan interval: 60 ms
26229956955bSMatthias Ringwald                  0x0030,    // scan interval: 30 ms
26239956955bSMatthias Ringwald                  1,         // use whitelist
26249956955bSMatthias Ringwald                  0,         // peer address type
26259956955bSMatthias Ringwald                  null_addr,      // peer bd addr
26269956955bSMatthias Ringwald                  hci_stack->adv_addr_type, // our addr type:
26279956955bSMatthias Ringwald                  0x0008,    // conn interval min
26289956955bSMatthias Ringwald                  0x0018,    // conn interval max
26299956955bSMatthias Ringwald                  0,         // conn latency
26309956955bSMatthias Ringwald                  0x0048,    // supervision timeout
26319956955bSMatthias Ringwald                  0x0001,    // min ce length
26329956955bSMatthias Ringwald                  0x0001     // max ce length
26339956955bSMatthias Ringwald                  );
26349956955bSMatthias Ringwald             return;
26359956955bSMatthias Ringwald         }
26367bdc6798S[email protected]     }
2637b2f949feS[email protected] #endif
26387bdc6798S[email protected] 
263932ab9390Smatthias.ringwald     // send pending HCI commands
2640665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
264105ae8de3SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) it;
264232ab9390Smatthias.ringwald 
26430bf6344aS[email protected]         switch(connection->state){
26440bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
26454f3229d8S[email protected]                 switch(connection->address_type){
264635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
26474f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
26489da54300S[email protected]                         log_info("sending hci_create_connection");
2649ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
26504f3229d8S[email protected]                         break;
265135454696SMatthias Ringwald #endif
26524f3229d8S[email protected]                     default:
2653a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
26549da54300S[email protected]                         log_info("sending hci_le_create_connection");
26554f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
2656b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
2657b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
26584f3229d8S[email protected]                                      0,         // don't use whitelist
26594f3229d8S[email protected]                                      connection->address_type, // peer address type
26604f3229d8S[email protected]                                      connection->address,      // peer bd addr
2661b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
2662b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
2663b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
26644f3229d8S[email protected]                                      0,         // conn latency
2665b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
2666b2571179Smatthias.ringwald                                      0x0001,    // min ce length
2667b2571179Smatthias.ringwald                                      0x0001     // max ce length
26684f3229d8S[email protected]                                      );
26694f3229d8S[email protected] 
26704f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
2671b2f949feS[email protected] #endif
26724f3229d8S[email protected]                         break;
26734f3229d8S[email protected]                 }
2674ad83dc6aS[email protected]                 return;
2675ad83dc6aS[email protected] 
267635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
26770bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
267894e4aaa2SMatthias Ringwald                 log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO);
267932ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
26805cf766e8SMatthias Ringwald                 connection->role  = HCI_ROLE_SLAVE;
2681e35edcc1S[email protected]                 if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
268234d2123cS[email protected]                     hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
2683e35edcc1S[email protected]                 }
2684dbe1a790S[email protected]                 return;
268535454696SMatthias Ringwald #endif
26860bf6344aS[email protected] 
2687a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
26880bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
26890bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
26900bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
26910bf6344aS[email protected]                 return;
2692a6725849S[email protected] #endif
26930bf6344aS[email protected]             case SEND_DISCONNECT:
26940bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
26957851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
26960bf6344aS[email protected]                 return;
26970bf6344aS[email protected] 
26980bf6344aS[email protected]             default:
26990bf6344aS[email protected]                 break;
2700c7e0c5f6Smatthias.ringwald         }
2701c7e0c5f6Smatthias.ringwald 
270235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
270332ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
27049da54300S[email protected]             log_info("responding to link key request");
270534d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
270632ab9390Smatthias.ringwald             link_key_t link_key;
2707c77e8838S[email protected]             link_key_type_t link_key_type;
2708a98592bcSMatthias Ringwald             if ( hci_stack->link_key_db
2709a98592bcSMatthias Ringwald               && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type)
271034d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
27119ab95c90S[email protected]                connection->link_key_type = link_key_type;
271232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
271332ab9390Smatthias.ringwald             } else {
271432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
271532ab9390Smatthias.ringwald             }
2716dbe1a790S[email protected]             return;
271732ab9390Smatthias.ringwald         }
27181d6b20aeS[email protected] 
2719899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
27209da54300S[email protected]             log_info("denying to pin request");
2721899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
272234d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
27234c57c146S[email protected]             return;
27244c57c146S[email protected]         }
27254c57c146S[email protected] 
2726dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
272734d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
272882d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
272982d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
2730106d6d11S[email protected]                 // tweak authentication requirements
27313a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
2732106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
27339faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
27349faad3abS[email protected]                 }
27359faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
27369faad3abS[email protected]                     authreq |= 1;
2737106d6d11S[email protected]                 }
27383a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
2739f8fb5f6eS[email protected]             } else {
2740f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
2741f8fb5f6eS[email protected]             }
2742dbe1a790S[email protected]             return;
274332ab9390Smatthias.ringwald         }
274432ab9390Smatthias.ringwald 
2745dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
2746dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
274734d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
2748dbe1a790S[email protected]             return;
2749dbe1a790S[email protected]         }
2750dbe1a790S[email protected] 
2751dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
2752dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
275334d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
2754dbe1a790S[email protected]             return;
2755dbe1a790S[email protected]         }
275635454696SMatthias Ringwald #endif
2757afd4e962S[email protected] 
2758afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
2759afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
276034d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
27612bd8b7e7S[email protected]             return;
27622bd8b7e7S[email protected]         }
27632bd8b7e7S[email protected] 
27642bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
27652bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
276634d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
276734d2123cS[email protected]             return;
276834d2123cS[email protected]         }
2769ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
2770ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
27711bd5283dS[email protected]             connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT;
277252d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
2773ad83dc6aS[email protected]             return;
2774ad83dc6aS[email protected]         }
277534d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
277634d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
277734d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
27782bd8b7e7S[email protected]             return;
2779afd4e962S[email protected]         }
2780dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
2781dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
2782dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
2783dce78009S[email protected]             return;
2784dce78009S[email protected]         }
2785da886c03S[email protected] 
2786a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
2787da886c03S[email protected]         if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){
2788da886c03S[email protected]             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
2789da886c03S[email protected] 
2790c37a3166S[email protected]             uint16_t connection_interval_min = connection->le_conn_interval_min;
2791c37a3166S[email protected]             connection->le_conn_interval_min = 0;
2792c37a3166S[email protected]             hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min,
2793c37a3166S[email protected]                 connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout,
2794c37a3166S[email protected]                 0x0000, 0xffff);
2795c37a3166S[email protected]         }
2796c37a3166S[email protected] #endif
2797dbe1a790S[email protected]     }
2798c7e0c5f6Smatthias.ringwald 
279905ae8de3SMatthias Ringwald     hci_connection_t * connection;
28003a9fb326S[email protected]     switch (hci_stack->state){
28013429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
280274b323a9SMatthias Ringwald             hci_initializing_run();
28033429f56bSmatthias.ringwald             break;
2804c7e0c5f6Smatthias.ringwald 
2805c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
2806c7e0c5f6Smatthias.ringwald 
28079da54300S[email protected]             log_info("HCI_STATE_HALTING");
28089956955bSMatthias Ringwald 
28099956955bSMatthias Ringwald             // free whitelist entries
2810a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
28119956955bSMatthias Ringwald             {
2812665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_t lit;
2813665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2814665d90f2SMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&lit)){
2815665d90f2SMatthias Ringwald                     whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
2816665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry);
28179956955bSMatthias Ringwald                     btstack_memory_whitelist_entry_free(entry);
28189956955bSMatthias Ringwald                 }
28199956955bSMatthias Ringwald             }
28209956955bSMatthias Ringwald #endif
2821c7e0c5f6Smatthias.ringwald             // close all open connections
28223a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
2823c7e0c5f6Smatthias.ringwald             if (connection){
2824711e6c80SMatthias Ringwald                 hci_con_handle_t con_handle = (uint16_t) connection->con_handle;
2825d94d3cafS[email protected]                 if (!hci_can_send_command_packet_now()) return;
282632ab9390Smatthias.ringwald 
28278837e9efSMatthias Ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle);
2828c7e0c5f6Smatthias.ringwald 
28298837e9efSMatthias Ringwald                 // cancel all l2cap connections right away instead of waiting for disconnection complete event ...
28308837e9efSMatthias Ringwald                 hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host
28318837e9efSMatthias Ringwald 
28328837e9efSMatthias Ringwald                 // ... which would be ignored anyway as we shutdown (free) the connection now
2833c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
28348837e9efSMatthias Ringwald 
28358837e9efSMatthias Ringwald                 // finally, send the disconnect command
28368837e9efSMatthias Ringwald                 hci_send_cmd(&hci_disconnect, con_handle, 0x13);  // remote closed connection
2837c7e0c5f6Smatthias.ringwald                 return;
2838c7e0c5f6Smatthias.ringwald             }
28399da54300S[email protected]             log_info("HCI_STATE_HALTING, calling off");
2840c7e0c5f6Smatthias.ringwald 
284172ea5239Smatthias.ringwald             // switch mode
2842c7e0c5f6Smatthias.ringwald             hci_power_control_off();
28439418f9c9Smatthias.ringwald 
28449da54300S[email protected]             log_info("HCI_STATE_HALTING, emitting state");
284572ea5239Smatthias.ringwald             hci_emit_state();
28469da54300S[email protected]             log_info("HCI_STATE_HALTING, done");
284772ea5239Smatthias.ringwald             break;
2848c7e0c5f6Smatthias.ringwald 
284972ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
28503a9fb326S[email protected]             switch(hci_stack->substate) {
285174b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_DISCONNECT:
28529da54300S[email protected]                     log_info("HCI_STATE_FALLING_ASLEEP");
285372ea5239Smatthias.ringwald                     // close all open connections
28543a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
285566da7044Smatthias.ringwald 
2856423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
285766da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
2858d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
285966da7044Smatthias.ringwald                         connection = NULL;
286066da7044Smatthias.ringwald                     }
286166da7044Smatthias.ringwald #endif
286272ea5239Smatthias.ringwald                     if (connection){
286332ab9390Smatthias.ringwald 
286472ea5239Smatthias.ringwald                         // send disconnect
2865d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
286632ab9390Smatthias.ringwald 
28679da54300S[email protected]                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
28686ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
286972ea5239Smatthias.ringwald 
287072ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
287172ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
287272ea5239Smatthias.ringwald                         return;
287372ea5239Smatthias.ringwald                     }
287472ea5239Smatthias.ringwald 
287592368cd3S[email protected]                     if (hci_classic_supported()){
287689db417bSmatthias.ringwald                         // disable page and inquiry scan
2877d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
287832ab9390Smatthias.ringwald 
28799da54300S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans");
28803a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
288189db417bSmatthias.ringwald 
288289db417bSmatthias.ringwald                         // continue in next sub state
288374b323a9SMatthias Ringwald                         hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE;
288489db417bSmatthias.ringwald                         break;
288592368cd3S[email protected]                     }
2886202c8a4cSMatthias Ringwald                     // no break - fall through for ble-only chips
288792368cd3S[email protected] 
288874b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_COMPLETE:
28899da54300S[email protected]                     log_info("HCI_STATE_HALTING, calling sleep");
2890423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
289128171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
2892d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
289328171530Smatthias.ringwald                         // SLEEP MODE reached
28943a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
289528171530Smatthias.ringwald                         hci_emit_state();
289628171530Smatthias.ringwald                         break;
289728171530Smatthias.ringwald                     }
289828171530Smatthias.ringwald #endif
289972ea5239Smatthias.ringwald                     // switch mode
29003a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
2901c7e0c5f6Smatthias.ringwald                     hci_emit_state();
290228171530Smatthias.ringwald                     break;
290328171530Smatthias.ringwald 
290489db417bSmatthias.ringwald                 default:
290589db417bSmatthias.ringwald                     break;
290689db417bSmatthias.ringwald             }
2907c7e0c5f6Smatthias.ringwald             break;
2908c7e0c5f6Smatthias.ringwald 
29093429f56bSmatthias.ringwald         default:
29103429f56bSmatthias.ringwald             break;
29111f504dbdSmatthias.ringwald     }
29123429f56bSmatthias.ringwald }
291316833f0aSmatthias.ringwald 
291431452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
291535454696SMatthias Ringwald     // house-keeping
291635454696SMatthias Ringwald 
291735454696SMatthias Ringwald     if (IS_COMMAND(packet, hci_write_loopback_mode)){
291835454696SMatthias Ringwald         hci_stack->loopback_mode = packet[3];
291935454696SMatthias Ringwald     }
292035454696SMatthias Ringwald 
292135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2922c8e4258aSmatthias.ringwald     bd_addr_t addr;
2923c8e4258aSmatthias.ringwald     hci_connection_t * conn;
2924c8e4258aSmatthias.ringwald 
2925c8e4258aSmatthias.ringwald     // create_connection?
2926c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
2927724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
29289da54300S[email protected]         log_info("Create_connection to %s", bd_addr_to_str(addr));
2929c8e4258aSmatthias.ringwald 
29302e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2931ad83dc6aS[email protected]         if (!conn){
293296a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
293317f1ba2aSmatthias.ringwald             if (!conn){
293417f1ba2aSmatthias.ringwald                 // notify client that alloc failed
29352deddeceSMatthias Ringwald                 hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
293617f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
293717f1ba2aSmatthias.ringwald             }
2938ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
2939ad83dc6aS[email protected]         }
2940ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
2941ad83dc6aS[email protected]         switch (conn->state){
2942ad83dc6aS[email protected]             // if connection active exists
2943ad83dc6aS[email protected]             case OPEN:
294462bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
2945274dad91SMatthias Ringwald                 hci_emit_connection_complete(addr, conn->con_handle, 0);
294662bda3fbS[email protected]                 return 0;
2947ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
2948ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
2949ad83dc6aS[email protected]                 break;
2950ad83dc6aS[email protected]             default:
2951ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
2952ad83dc6aS[email protected]                 return 0;
2953ad83dc6aS[email protected]         }
2954c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
2955c8e4258aSmatthias.ringwald     }
295635454696SMatthias Ringwald 
29577fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
29587fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
29597fde4af9Smatthias.ringwald     }
29607fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
29617fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
29627fde4af9Smatthias.ringwald     }
29637fde4af9Smatthias.ringwald 
29648ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
2965a98592bcSMatthias Ringwald         if (hci_stack->link_key_db){
2966724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
2967a98592bcSMatthias Ringwald             hci_stack->link_key_db->delete_link_key(addr);
29688ef73945Smatthias.ringwald         }
29698ef73945Smatthias.ringwald     }
2970c8e4258aSmatthias.ringwald 
29716724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
29726724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
2973724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
29742e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
29756724cd9eS[email protected]         if (conn){
29766724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
29776724cd9eS[email protected]         }
29786724cd9eS[email protected]     }
29796724cd9eS[email protected] 
29806724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
29816724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
29826724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
29836724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
2984724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
29852e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
29866724cd9eS[email protected]         if (conn){
29876724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
29886724cd9eS[email protected]         }
29896724cd9eS[email protected]     }
299035454696SMatthias Ringwald #endif
29914b3e1e19SMatthias Ringwald 
2992a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
299369a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
29943a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
299569a97523S[email protected]     }
299669a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
299752b551c3SMatthias Ringwald         hci_stack->le_advertisements_random_address_set = 1;
2998724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], hci_stack->adv_address);
299969a97523S[email protected]     }
3000171293d3SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_set_advertise_enable)){
3001171293d3SMatthias Ringwald         hci_stack->le_advertisements_active = packet[3];
3002171293d3SMatthias Ringwald     }
3003b04dfa37SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_create_connection)){
3004b04dfa37SMatthias Ringwald         // white list used?
3005b04dfa37SMatthias Ringwald         uint8_t initiator_filter_policy = packet[7];
3006b04dfa37SMatthias Ringwald         switch (initiator_filter_policy){
3007b04dfa37SMatthias Ringwald             case 0:
3008b04dfa37SMatthias Ringwald                 // whitelist not used
3009b04dfa37SMatthias Ringwald                 hci_stack->le_connecting_state = LE_CONNECTING_DIRECT;
3010b04dfa37SMatthias Ringwald                 break;
3011b04dfa37SMatthias Ringwald             case 1:
3012b04dfa37SMatthias Ringwald                 hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST;
3013b04dfa37SMatthias Ringwald                 break;
3014b04dfa37SMatthias Ringwald             default:
3015b04dfa37SMatthias Ringwald                 log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy);
3016b04dfa37SMatthias Ringwald                 break;
3017b04dfa37SMatthias Ringwald         }
3018b04dfa37SMatthias Ringwald     }
3019b04dfa37SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_create_connection_cancel)){
3020b04dfa37SMatthias Ringwald         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
3021b04dfa37SMatthias Ringwald     }
302269a97523S[email protected] #endif
302369a97523S[email protected] 
30243a9fb326S[email protected]     hci_stack->num_cmd_packets--;
30255bb5bc3eS[email protected] 
30265bb5bc3eS[email protected]     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size);
30276b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
30286b4af23dS[email protected] 
3029d051460cS[email protected]     // release packet buffer for synchronous transport implementations
3030c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
30316b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
30326b4af23dS[email protected]     }
30336b4af23dS[email protected] 
30346b4af23dS[email protected]     return err;
303531452debSmatthias.ringwald }
30368adf0ddaSmatthias.ringwald 
30372bd8b7e7S[email protected] // disconnect because of security block
30382bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
30392bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
30402bd8b7e7S[email protected]     if (!connection) return;
30412bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
30422bd8b7e7S[email protected] }
30432bd8b7e7S[email protected] 
30442bd8b7e7S[email protected] 
3045dbe1a790S[email protected] // Configure Secure Simple Pairing
3046dbe1a790S[email protected] 
304735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
304835454696SMatthias Ringwald 
3049dbe1a790S[email protected] // enable will enable SSP during init
305015a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){
30513a9fb326S[email protected]     hci_stack->ssp_enable = enable;
3052dbe1a790S[email protected] }
3053dbe1a790S[email protected] 
305495d71764SMatthias Ringwald static int hci_local_ssp_activated(void){
305515a95bd5SMatthias Ringwald     return gap_ssp_supported() && hci_stack->ssp_enable;
30562bd8b7e7S[email protected] }
30572bd8b7e7S[email protected] 
3058dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
305915a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){
30603a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
3061dbe1a790S[email protected] }
306215a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){
30633a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
3064dbe1a790S[email protected] }
3065dbe1a790S[email protected] 
3066dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
306715a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){
30683a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
3069dbe1a790S[email protected] }
307035454696SMatthias Ringwald #endif
3071dbe1a790S[email protected] 
3072*94be1a66SMatthias Ringwald // va_list part of hci_send_cmd
3073*94be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){
3074d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()){
30759d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
30769d14b626S[email protected]         return 0;
30779d14b626S[email protected]     }
30789d14b626S[email protected] 
30795127cc62S[email protected]     // for HCI INITIALIZATION
30809da54300S[email protected]     // log_info("hci_send_cmd: opcode %04x", cmd->opcode);
30815127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
30825127cc62S[email protected] 
30839d14b626S[email protected]     hci_reserve_packet_buffer();
30849d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
3085*94be1a66SMatthias Ringwald     uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr);
3086*94be1a66SMatthias Ringwald     return hci_send_cmd_packet(packet, size);
3087*94be1a66SMatthias Ringwald }
30889d14b626S[email protected] 
3089*94be1a66SMatthias Ringwald /**
3090*94be1a66SMatthias Ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
3091*94be1a66SMatthias Ringwald  */
3092*94be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
30931cd208adSmatthias.ringwald     va_list argptr;
30941cd208adSmatthias.ringwald     va_start(argptr, cmd);
3095*94be1a66SMatthias Ringwald     int res = hci_send_cmd_va_arg(cmd, argptr);
30961cd208adSmatthias.ringwald     va_end(argptr);
3097*94be1a66SMatthias Ringwald     return res;
309893b8dc03Smatthias.ringwald }
3099c8e4258aSmatthias.ringwald 
3100ee091cf1Smatthias.ringwald // Create various non-HCI events.
3101ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
3102ee091cf1Smatthias.ringwald 
3103d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){
3104fb37a842SMatthias Ringwald     // dump packet
3105d6b06661SMatthias Ringwald     if (dump) {
3106300c1ba4SMatthias Ringwald         hci_dump_packet( HCI_EVENT_PACKET, 0, event, size);
3107d6b06661SMatthias Ringwald     }
31081ef6bb52SMatthias Ringwald 
3109fb37a842SMatthias Ringwald     // dispatch to all event handlers
31101ef6bb52SMatthias Ringwald     btstack_linked_list_iterator_t it;
31111ef6bb52SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers);
31121ef6bb52SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
31131ef6bb52SMatthias Ringwald         btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
3114d9a7306aSMatthias Ringwald         entry->callback(HCI_EVENT_PACKET, 0, event, size);
31151ef6bb52SMatthias Ringwald     }
3116d6b06661SMatthias Ringwald }
3117d6b06661SMatthias Ringwald 
3118d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){
3119fb37a842SMatthias Ringwald     if (!hci_stack->acl_packet_handler) return;
31203d50b4baSMatthias Ringwald     hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size);
3121d6b06661SMatthias Ringwald }
3122d6b06661SMatthias Ringwald 
312335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
3124701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){
31253bc639ceSMatthias Ringwald     // notify SCO sender if waiting
3126701e3307SMatthias Ringwald     if (!hci_stack->sco_waiting_for_can_send_now) return;
3127701e3307SMatthias Ringwald     if (hci_can_send_sco_packet_now()){
31283bc639ceSMatthias Ringwald         hci_stack->sco_waiting_for_can_send_now = 0;
3129701e3307SMatthias Ringwald         uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 };
3130701e3307SMatthias Ringwald         hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event));
31313d50b4baSMatthias Ringwald         hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
31323bc639ceSMatthias Ringwald     }
31333bc639ceSMatthias Ringwald }
313435454696SMatthias Ringwald #endif
31353bc639ceSMatthias Ringwald 
313671de195eSMatthias Ringwald void hci_emit_state(void){
31373a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
3138425d1371Smatthias.ringwald     uint8_t event[3];
313980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
3140425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
31413a9fb326S[email protected]     event[2] = hci_stack->state;
3142d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3143c8e4258aSmatthias.ringwald }
3144c8e4258aSmatthias.ringwald 
314535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
31462deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){
3147425d1371Smatthias.ringwald     uint8_t event[13];
3148c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
3149425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
315017f1ba2aSmatthias.ringwald     event[2] = status;
31512deddeceSMatthias Ringwald     little_endian_store_16(event, 3, con_handle);
31522deddeceSMatthias Ringwald     reverse_bd_addr(address, &event[5]);
3153c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
3154c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
3155d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3156c8e4258aSmatthias.ringwald }
315752db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
315852db98b2SMatthias Ringwald     if (disable_l2cap_timeouts) return;
315952db98b2SMatthias Ringwald     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
316052db98b2SMatthias Ringwald     uint8_t event[4];
316152db98b2SMatthias Ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
316252db98b2SMatthias Ringwald     event[1] = sizeof(event) - 2;
316352db98b2SMatthias Ringwald     little_endian_store_16(event, 2, conn->con_handle);
316452db98b2SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
316552db98b2SMatthias Ringwald }
316635454696SMatthias Ringwald #endif
3167c8e4258aSmatthias.ringwald 
316835454696SMatthias Ringwald #ifdef ENABLE_BLE
3169fc64f94aSMatthias 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){
31704f3229d8S[email protected]     uint8_t event[21];
31714f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
31724f3229d8S[email protected]     event[1] = sizeof(event) - 2;
31734f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
31744f3229d8S[email protected]     event[3] = status;
3175fc64f94aSMatthias Ringwald     little_endian_store_16(event, 4, con_handle);
31764f3229d8S[email protected]     event[6] = 0; // TODO: role
31776e2e9a6bS[email protected]     event[7] = address_type;
3178724d70a2SMatthias Ringwald     reverse_bd_addr(address, &event[8]);
3179f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, 0); // interval
3180f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 16, 0); // latency
3181f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 18, 0); // supervision timeout
31824f3229d8S[email protected]     event[20] = 0; // master clock accuracy
3183d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
31844f3229d8S[email protected] }
318535454696SMatthias Ringwald #endif
31864f3229d8S[email protected] 
3187fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){
3188425d1371Smatthias.ringwald     uint8_t event[6];
31893c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
3190e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
31913c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
3192fc64f94aSMatthias Ringwald     little_endian_store_16(event, 3, con_handle);
31933c4d4b90Smatthias.ringwald     event[5] = reason;
3194d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
31953c4d4b90Smatthias.ringwald }
31963c4d4b90Smatthias.ringwald 
3197b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){
3198e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
3199425d1371Smatthias.ringwald     uint8_t event[3];
320080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
3201425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
320243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
3203d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
320443bfb1bdSmatthias.ringwald }
3205038bc64cSmatthias.ringwald 
3206b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){
3207e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
3208425d1371Smatthias.ringwald     uint8_t event[2];
320980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
3210425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
3211d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3212038bc64cSmatthias.ringwald }
32131b0e3922Smatthias.ringwald 
321435454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){
321535454696SMatthias Ringwald     log_info("hci_emit_dedicated_bonding_result %u ", status);
321635454696SMatthias Ringwald     uint8_t event[9];
321735454696SMatthias Ringwald     int pos = 0;
321835454696SMatthias Ringwald     event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED;
321935454696SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
322035454696SMatthias Ringwald     event[pos++] = status;
322135454696SMatthias Ringwald     reverse_bd_addr(address, &event[pos]);
3222d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3223381fbed8Smatthias.ringwald }
3224458bf4e8S[email protected] 
322535454696SMatthias Ringwald 
322635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
322735454696SMatthias Ringwald 
3228b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
3229df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
3230a00031e2S[email protected]     uint8_t event[5];
3231e00caf9cS[email protected]     int pos = 0;
32325611a760SMatthias Ringwald     event[pos++] = GAP_EVENT_SECURITY_LEVEL;
3233e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
3234f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
3235e00caf9cS[email protected]     pos += 2;
3236e00caf9cS[email protected]     event[pos++] = level;
3237d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3238e00caf9cS[email protected] }
3239e00caf9cS[email protected] 
324035454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
324135454696SMatthias Ringwald     if (!connection) return LEVEL_0;
324235454696SMatthias Ringwald     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
324335454696SMatthias Ringwald     return gap_security_level_for_link_key_type(connection->link_key_type);
324435454696SMatthias Ringwald }
324535454696SMatthias Ringwald 
324635454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){
324735454696SMatthias Ringwald     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
324835454696SMatthias Ringwald     uint8_t event[3];
324935454696SMatthias Ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
325035454696SMatthias Ringwald     event[1] = sizeof(event) - 2;
325135454696SMatthias Ringwald     event[2] = enabled;
3252d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3253ad83dc6aS[email protected] }
3254ad83dc6aS[email protected] 
325598a2fd1cSMatthias Ringwald // query if remote side supports eSCO
3256073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){
325798a2fd1cSMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
325898a2fd1cSMatthias Ringwald     if (!connection) return 0;
325998a2fd1cSMatthias Ringwald     return connection->remote_supported_feature_eSCO;
326098a2fd1cSMatthias Ringwald }
326198a2fd1cSMatthias Ringwald 
32622bd8b7e7S[email protected] // query if remote side supports SSP
32632bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
32642bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
32652bd8b7e7S[email protected]     if (!connection) return 0;
32662bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
32672bd8b7e7S[email protected] }
32682bd8b7e7S[email protected] 
326915a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){
3270df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
3271df3354fcS[email protected] }
3272df3354fcS[email protected] 
3273458bf4e8S[email protected] // GAP API
3274458bf4e8S[email protected] /**
3275458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
3276458bf4e8S[email protected]  * @praram enabled
3277458bf4e8S[email protected]  */
32784c57c146S[email protected] void gap_set_bondable_mode(int enable){
32793a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
3280458bf4e8S[email protected] }
32814ef6443cSMatthias Ringwald /**
32824ef6443cSMatthias Ringwald  * @brief Get bondable mode.
32834ef6443cSMatthias Ringwald  * @return 1 if bondable
32844ef6443cSMatthias Ringwald  */
32854ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){
32864ef6443cSMatthias Ringwald     return hci_stack->bondable;
32874ef6443cSMatthias Ringwald }
3288cb230b9dS[email protected] 
3289cb230b9dS[email protected] /**
329034d2123cS[email protected]  * @brief map link keys to security levels
3291cb230b9dS[email protected]  */
329234d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
329334d2123cS[email protected]     switch (link_key_type){
32943c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
32953c68dfa9S[email protected]             return LEVEL_4;
32963c68dfa9S[email protected]         case COMBINATION_KEY:
32973c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
32983c68dfa9S[email protected]             return LEVEL_3;
32993c68dfa9S[email protected]         default:
33003c68dfa9S[email protected]             return LEVEL_2;
33013c68dfa9S[email protected]     }
3302cb230b9dS[email protected] }
3303cb230b9dS[email protected] 
3304106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
33055127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
3306106d6d11S[email protected]     return level > LEVEL_2;
3307106d6d11S[email protected] }
3308106d6d11S[email protected] 
330934d2123cS[email protected] /**
331034d2123cS[email protected]  * @brief get current security level
331134d2123cS[email protected]  */
331234d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
331334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
331434d2123cS[email protected]     if (!connection) return LEVEL_0;
331534d2123cS[email protected]     return gap_security_level_for_connection(connection);
331634d2123cS[email protected] }
331734d2123cS[email protected] 
3318cb230b9dS[email protected] /**
3319cb230b9dS[email protected]  * @brief request connection to device to
3320cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
3321cb230b9dS[email protected]  */
332234d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
332334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
332434d2123cS[email protected]     if (!connection){
3325a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
332634d2123cS[email protected]         return;
332734d2123cS[email protected]     }
332834d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
332934d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
333034d2123cS[email protected]     if (current_level >= requested_level){
3331a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
333234d2123cS[email protected]         return;
333334d2123cS[email protected]     }
3334a00031e2S[email protected] 
333534d2123cS[email protected]     connection->requested_security_level = requested_level;
3336a00031e2S[email protected] 
333725bf5872S[email protected] #if 0
333825bf5872S[email protected]     // sending encryption request without a link key results in an error.
333925bf5872S[email protected]     // TODO: figure out how to use it properly
334025bf5872S[email protected] 
3341fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
3342a98592bcSMatthias Ringwald     if (hci_stack->link_key_db){
3343a00031e2S[email protected]         link_key_type_t link_key_type;
3344a00031e2S[email protected]         link_key_t      link_key;
3345a98592bcSMatthias Ringwald         if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){
3346a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
3347a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
3348a00031e2S[email protected]                 return;
3349a00031e2S[email protected]             }
3350a00031e2S[email protected]         }
3351a00031e2S[email protected]     }
335225bf5872S[email protected] #endif
3353a00031e2S[email protected] 
33541eb2563eS[email protected]     // try to authenticate connection
33551eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
3356e80b2cf9S[email protected]     hci_run();
3357e00caf9cS[email protected] }
3358ad83dc6aS[email protected] 
3359ad83dc6aS[email protected] /**
3360ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
3361ad83dc6aS[email protected]  * @param device
3362ad83dc6aS[email protected]  * @param request MITM protection
3363ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
3364ad83dc6aS[email protected]  */
3365ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
3366ad83dc6aS[email protected] 
3367ad83dc6aS[email protected]     // create connection state machine
336896a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
3369ad83dc6aS[email protected] 
3370ad83dc6aS[email protected]     if (!connection){
3371ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
3372ad83dc6aS[email protected]     }
3373ad83dc6aS[email protected] 
3374ad83dc6aS[email protected]     // delete linkn key
337515a95bd5SMatthias Ringwald     gap_drop_link_key_for_bd_addr(device);
3376ad83dc6aS[email protected] 
3377ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
3378ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
3379ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
3380f04a0c31SMatthias Ringwald     log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level);
3381ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
3382ad83dc6aS[email protected] 
3383ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
3384ad83dc6aS[email protected] 
3385ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
3386ad83dc6aS[email protected]     // handle: authentication failure
3387ad83dc6aS[email protected]     // handle: disconnect on done
3388ad83dc6aS[email protected] 
3389ad83dc6aS[email protected]     hci_run();
3390ad83dc6aS[email protected] 
3391ad83dc6aS[email protected]     return 0;
3392ad83dc6aS[email protected] }
339335454696SMatthias Ringwald #endif
33948e618f72S[email protected] 
33958e618f72S[email protected] void gap_set_local_name(const char * local_name){
33968e618f72S[email protected]     hci_stack->local_name = local_name;
33978e618f72S[email protected] }
33988e618f72S[email protected] 
339935454696SMatthias Ringwald 
340035454696SMatthias Ringwald #ifdef ENABLE_BLE
340135454696SMatthias Ringwald 
3402d8e8f12aSMatthias Ringwald void gap_start_scan(void){
3403d8e8f12aSMatthias Ringwald     if (hci_stack->le_scanning_state == LE_SCANNING) return;
34047bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
34057bdc6798S[email protected]     hci_run();
34067bdc6798S[email protected] }
34078e618f72S[email protected] 
3408d8e8f12aSMatthias Ringwald void gap_stop_scan(void){
3409d8e8f12aSMatthias Ringwald     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return;
34107bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
34117bdc6798S[email protected]     hci_run();
34127bdc6798S[email protected] }
34134f3229d8S[email protected] 
3414d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
3415ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
3416ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
3417ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
3418ef11999fSmatthias.ringwald     hci_run();
3419ef11999fSmatthias.ringwald }
34204f3229d8S[email protected] 
3421d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){
34224f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
34234f3229d8S[email protected]     if (!conn){
3424d8e8f12aSMatthias Ringwald         log_info("gap_connect: no connection exists yet, creating context");
34252e77e513S[email protected]         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
34264f3229d8S[email protected]         if (!conn){
34274f3229d8S[email protected]             // notify client that alloc failed
34286e2e9a6bS[email protected]             hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
3429d8e8f12aSMatthias Ringwald             log_info("gap_connect: failed to alloc hci_connection_t");
3430472a5742SMatthias Ringwald             return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller
34314f3229d8S[email protected]         }
34324f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
3433d8e8f12aSMatthias Ringwald         log_info("gap_connect: send create connection next");
3434564fca32S[email protected]         hci_run();
3435616edd56SMatthias Ringwald         return 0;
34364f3229d8S[email protected]     }
34370bf6344aS[email protected] 
34380bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
34390bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
34400bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
34412e77e513S[email protected]         hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED);
3442d8e8f12aSMatthias Ringwald         log_error("gap_connect: classic connection or connect is already being created");
3443616edd56SMatthias Ringwald         return GATT_CLIENT_IN_WRONG_STATE;
34440bf6344aS[email protected]     }
34450bf6344aS[email protected] 
3446d8e8f12aSMatthias Ringwald     log_info("gap_connect: context exists with state %u", conn->state);
34472e77e513S[email protected]     hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0);
34484f3229d8S[email protected]     hci_run();
3449616edd56SMatthias Ringwald     return 0;
34504f3229d8S[email protected] }
34514f3229d8S[email protected] 
34527851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
3453d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){
3454665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
3455665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
34560bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
34570bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
34580bf6344aS[email protected]         switch (conn->state){
3459a6725849S[email protected]             case SEND_CREATE_CONNECTION:
34607851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
34617851196eSmatthias.ringwald                 return conn;
34627851196eSmatthias.ringwald             default:
34637851196eSmatthias.ringwald                 break;
34647851196eSmatthias.ringwald         };
34657851196eSmatthias.ringwald     }
34667851196eSmatthias.ringwald     return NULL;
34677851196eSmatthias.ringwald }
34687851196eSmatthias.ringwald 
3469d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){
3470d8e8f12aSMatthias Ringwald     hci_connection_t * conn = gap_get_outgoing_connection();
3471616edd56SMatthias Ringwald     if (!conn) return 0;
34727851196eSmatthias.ringwald     switch (conn->state){
34737851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
34747851196eSmatthias.ringwald             // skip sending create connection and emit event instead
34752e77e513S[email protected]             hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
3476665d90f2SMatthias Ringwald             btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
34777851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
34780bf6344aS[email protected]             break;
3479a6725849S[email protected]         case SENT_CREATE_CONNECTION:
34807851196eSmatthias.ringwald             // request to send cancel connection
34810bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
34820bf6344aS[email protected]             hci_run();
34830bf6344aS[email protected]             break;
34840bf6344aS[email protected]         default:
34850bf6344aS[email protected]             break;
34860bf6344aS[email protected]     }
3487616edd56SMatthias Ringwald     return 0;
3488e31f89a7S[email protected] }
34894f3229d8S[email protected] 
3490c37a3166S[email protected] /**
3491c37a3166S[email protected]  * @brief Updates the connection parameters for a given LE connection
3492c37a3166S[email protected]  * @param handle
3493c37a3166S[email protected]  * @param conn_interval_min (unit: 1.25ms)
3494c37a3166S[email protected]  * @param conn_interval_max (unit: 1.25ms)
3495c37a3166S[email protected]  * @param conn_latency
3496c37a3166S[email protected]  * @param supervision_timeout (unit: 10ms)
3497c37a3166S[email protected]  * @returns 0 if ok
3498c37a3166S[email protected]  */
3499c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
3500c37a3166S[email protected]     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
3501c37a3166S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3502c37a3166S[email protected]     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3503c37a3166S[email protected]     connection->le_conn_interval_min = conn_interval_min;
3504c37a3166S[email protected]     connection->le_conn_interval_max = conn_interval_max;
3505c37a3166S[email protected]     connection->le_conn_latency = conn_latency;
3506c37a3166S[email protected]     connection->le_supervision_timeout = supervision_timeout;
350784cf6d83SMatthias Ringwald     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
3508cfc59f1bSMatthias Ringwald     hci_run();
3509c37a3166S[email protected]     return 0;
3510c37a3166S[email protected] }
3511c37a3166S[email protected] 
351245c102fdSMatthias Ringwald /**
3513b68d7bc3SMatthias Ringwald  * @brief Request an update of the connection parameter for a given LE connection
3514b68d7bc3SMatthias Ringwald  * @param handle
3515b68d7bc3SMatthias Ringwald  * @param conn_interval_min (unit: 1.25ms)
3516b68d7bc3SMatthias Ringwald  * @param conn_interval_max (unit: 1.25ms)
3517b68d7bc3SMatthias Ringwald  * @param conn_latency
3518b68d7bc3SMatthias Ringwald  * @param supervision_timeout (unit: 10ms)
3519b68d7bc3SMatthias Ringwald  * @returns 0 if ok
3520b68d7bc3SMatthias Ringwald  */
3521b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
3522b68d7bc3SMatthias Ringwald     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
3523b68d7bc3SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3524b68d7bc3SMatthias Ringwald     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3525b68d7bc3SMatthias Ringwald     connection->le_conn_interval_min = conn_interval_min;
3526b68d7bc3SMatthias Ringwald     connection->le_conn_interval_max = conn_interval_max;
3527b68d7bc3SMatthias Ringwald     connection->le_conn_latency = conn_latency;
3528b68d7bc3SMatthias Ringwald     connection->le_supervision_timeout = supervision_timeout;
3529b68d7bc3SMatthias Ringwald     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST;
3530b68d7bc3SMatthias Ringwald     hci_run();
3531b68d7bc3SMatthias Ringwald     return 0;
3532b68d7bc3SMatthias Ringwald }
3533b68d7bc3SMatthias Ringwald 
3534501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){
3535501f56b3SMatthias Ringwald     // disable advertisements before updating adv, scan data, or adv params
3536501f56b3SMatthias Ringwald     if (hci_stack->le_advertisements_active){
3537501f56b3SMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE;
3538501f56b3SMatthias Ringwald     }
3539501f56b3SMatthias Ringwald     hci_run();
3540501f56b3SMatthias Ringwald }
3541501f56b3SMatthias Ringwald 
3542b68d7bc3SMatthias Ringwald /**
354345c102fdSMatthias Ringwald  * @brief Set Advertisement Data
354445c102fdSMatthias Ringwald  * @param advertising_data_length
354545c102fdSMatthias Ringwald  * @param advertising_data (max 31 octets)
354645c102fdSMatthias Ringwald  * @note data is not copied, pointer has to stay valid
354745c102fdSMatthias Ringwald  */
354845c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){
354945c102fdSMatthias Ringwald     hci_stack->le_advertisements_data_len = advertising_data_length;
355045c102fdSMatthias Ringwald     hci_stack->le_advertisements_data = advertising_data;
3551501f56b3SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA;
3552501f56b3SMatthias Ringwald     gap_advertisments_changed();
355345c102fdSMatthias Ringwald }
3554501f56b3SMatthias Ringwald 
3555501f56b3SMatthias Ringwald /**
3556501f56b3SMatthias Ringwald  * @brief Set Scan Response Data
3557501f56b3SMatthias Ringwald  * @param advertising_data_length
3558501f56b3SMatthias Ringwald  * @param advertising_data (max 31 octets)
3559501f56b3SMatthias Ringwald  * @note data is not copied, pointer has to stay valid
3560501f56b3SMatthias Ringwald  */
3561501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){
3562501f56b3SMatthias Ringwald     hci_stack->le_scan_response_data_len = scan_response_data_length;
3563501f56b3SMatthias Ringwald     hci_stack->le_scan_response_data = scan_response_data;
3564501f56b3SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA;
3565501f56b3SMatthias Ringwald     gap_advertisments_changed();
356645c102fdSMatthias Ringwald }
356745c102fdSMatthias Ringwald 
356845c102fdSMatthias Ringwald /**
356945c102fdSMatthias Ringwald  * @brief Set Advertisement Parameters
357045c102fdSMatthias Ringwald  * @param adv_int_min
357145c102fdSMatthias Ringwald  * @param adv_int_max
357245c102fdSMatthias Ringwald  * @param adv_type
357345c102fdSMatthias Ringwald  * @param own_address_type
357445c102fdSMatthias Ringwald  * @param direct_address_type
357545c102fdSMatthias Ringwald  * @param direct_address
357645c102fdSMatthias Ringwald  * @param channel_map
357745c102fdSMatthias Ringwald  * @param filter_policy
357845c102fdSMatthias Ringwald  *
357945c102fdSMatthias Ringwald  * @note internal use. use gap_advertisements_set_params from gap_le.h instead.
358045c102fdSMatthias Ringwald  */
358145c102fdSMatthias Ringwald  void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
358245c102fdSMatthias Ringwald     uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address,
358345c102fdSMatthias Ringwald     uint8_t channel_map, uint8_t filter_policy) {
358445c102fdSMatthias Ringwald 
358545c102fdSMatthias Ringwald     hci_stack->le_advertisements_interval_min = adv_int_min;
358645c102fdSMatthias Ringwald     hci_stack->le_advertisements_interval_max = adv_int_max;
358745c102fdSMatthias Ringwald     hci_stack->le_advertisements_type = adv_type;
358845c102fdSMatthias Ringwald     hci_stack->le_advertisements_own_address_type = own_address_type;
358945c102fdSMatthias Ringwald     hci_stack->le_advertisements_direct_address_type = direct_address_typ;
359045c102fdSMatthias Ringwald     hci_stack->le_advertisements_channel_map = channel_map;
359145c102fdSMatthias Ringwald     hci_stack->le_advertisements_filter_policy = filter_policy;
359245c102fdSMatthias Ringwald     memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6);
359345c102fdSMatthias Ringwald 
359445c102fdSMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS;
3595501f56b3SMatthias Ringwald     gap_advertisments_changed();
359645c102fdSMatthias Ringwald  }
359745c102fdSMatthias Ringwald 
359833373e40SMatthias Ringwald void hci_le_advertisements_set_own_address_type(uint8_t own_address_type){
359933373e40SMatthias Ringwald     hci_stack->le_advertisements_own_address_type = own_address_type;
360033373e40SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS;
360133373e40SMatthias Ringwald     gap_advertisments_changed();
360233373e40SMatthias Ringwald }
360333373e40SMatthias Ringwald 
360445c102fdSMatthias Ringwald /**
360545c102fdSMatthias Ringwald  * @brief Enable/Disable Advertisements
360645c102fdSMatthias Ringwald  * @param enabled
360745c102fdSMatthias Ringwald  */
360845c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){
360945c102fdSMatthias Ringwald     hci_stack->le_advertisements_enabled = enabled;
361045c102fdSMatthias Ringwald     if (enabled && !hci_stack->le_advertisements_active){
361145c102fdSMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
361245c102fdSMatthias Ringwald     }
361345c102fdSMatthias Ringwald     if (!enabled && hci_stack->le_advertisements_active){
361445c102fdSMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE;
361545c102fdSMatthias Ringwald     }
3616cfc59f1bSMatthias Ringwald     hci_run();
361745c102fdSMatthias Ringwald }
361845c102fdSMatthias Ringwald 
361935454696SMatthias Ringwald #endif
362045c102fdSMatthias Ringwald 
3621616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){
36225917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
36235917a5c5S[email protected]     if (!conn){
36247851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
3625616edd56SMatthias Ringwald         return 0;
36265917a5c5S[email protected]     }
36275917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
36285917a5c5S[email protected]     hci_run();
3629616edd56SMatthias Ringwald     return 0;
36304f3229d8S[email protected] }
363104a6ef8cSmatthias.ringwald 
3632a1bf5ae7SMatthias Ringwald /**
3633a1bf5ae7SMatthias Ringwald  * @brief Get connection type
3634a1bf5ae7SMatthias Ringwald  * @param con_handle
3635a1bf5ae7SMatthias Ringwald  * @result connection_type
3636a1bf5ae7SMatthias Ringwald  */
3637a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){
3638a1bf5ae7SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_handle(connection_handle);
3639a1bf5ae7SMatthias Ringwald     if (!conn) return GAP_CONNECTION_INVALID;
3640a1bf5ae7SMatthias Ringwald     switch (conn->address_type){
3641a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_LE_PUBLIC:
3642a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_LE_RANDOM:
3643a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_LE;
3644a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_SCO:
3645a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_SCO;
3646a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_CLASSIC:
3647a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_ACL;
3648a1bf5ae7SMatthias Ringwald         default:
3649a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_INVALID;
3650a1bf5ae7SMatthias Ringwald     }
3651a1bf5ae7SMatthias Ringwald }
3652a1bf5ae7SMatthias Ringwald 
3653a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
36544f551432SMatthias Ringwald 
3655d23838ecSMatthias Ringwald /**
3656ac9c45e0SMatthias Ringwald  * @brief Auto Connection Establishment - Start Connecting to device
3657ac9c45e0SMatthias Ringwald  * @param address_typ
3658ac9c45e0SMatthias Ringwald  * @param address
3659ac9c45e0SMatthias Ringwald  * @returns 0 if ok
3660ac9c45e0SMatthias Ringwald  */
36614f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){
3662e83201bcSMatthias Ringwald     // check capacity
3663665d90f2SMatthias Ringwald     int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist);
366491915b0bSMatthias Ringwald     if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
3665e83201bcSMatthias Ringwald     whitelist_entry_t * entry = btstack_memory_whitelist_entry_get();
3666e83201bcSMatthias Ringwald     if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED;
3667e83201bcSMatthias Ringwald     entry->address_type = address_type;
3668e83201bcSMatthias Ringwald     memcpy(entry->address, address, 6);
3669e83201bcSMatthias Ringwald     entry->state = LE_WHITELIST_ADD_TO_CONTROLLER;
3670665d90f2SMatthias Ringwald     btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry);
3671e83201bcSMatthias Ringwald     hci_run();
3672e83201bcSMatthias Ringwald     return 0;
3673ac9c45e0SMatthias Ringwald }
3674ac9c45e0SMatthias Ringwald 
367542ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){
3676665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3677665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist);
3678665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3679665d90f2SMatthias Ringwald         whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it);
3680e83201bcSMatthias Ringwald         if (entry->address_type != address_type) continue;
3681e83201bcSMatthias Ringwald         if (memcmp(entry->address, address, 6) != 0) continue;
3682e83201bcSMatthias Ringwald         if (entry->state & LE_WHITELIST_ON_CONTROLLER){
3683e83201bcSMatthias Ringwald             // remove from controller if already present
3684e83201bcSMatthias Ringwald             entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER;
3685e83201bcSMatthias Ringwald             continue;
3686e83201bcSMatthias Ringwald         }
3687e83201bcSMatthias Ringwald         // direclty remove entry from whitelist
3688665d90f2SMatthias Ringwald         btstack_linked_list_iterator_remove(&it);
3689e83201bcSMatthias Ringwald         btstack_memory_whitelist_entry_free(entry);
3690e83201bcSMatthias Ringwald     }
369142ff5ba1SMatthias Ringwald }
369242ff5ba1SMatthias Ringwald 
369342ff5ba1SMatthias Ringwald /**
369442ff5ba1SMatthias Ringwald  * @brief Auto Connection Establishment - Stop Connecting to device
369542ff5ba1SMatthias Ringwald  * @param address_typ
369642ff5ba1SMatthias Ringwald  * @param address
369742ff5ba1SMatthias Ringwald  * @returns 0 if ok
369842ff5ba1SMatthias Ringwald  */
369942ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){
370042ff5ba1SMatthias Ringwald     hci_remove_from_whitelist(address_type, address);
3701e83201bcSMatthias Ringwald     hci_run();
3702e83201bcSMatthias Ringwald     return 0;
3703ac9c45e0SMatthias Ringwald }
3704ac9c45e0SMatthias Ringwald 
3705ac9c45e0SMatthias Ringwald /**
3706ac9c45e0SMatthias Ringwald  * @brief Auto Connection Establishment - Stop everything
3707ac9c45e0SMatthias Ringwald  * @note  Convenience function to stop all active auto connection attempts
3708ac9c45e0SMatthias Ringwald  */
3709ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){
3710665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3711665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist);
3712665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3713665d90f2SMatthias Ringwald         whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it);
3714e83201bcSMatthias Ringwald         if (entry->state & LE_WHITELIST_ON_CONTROLLER){
3715e83201bcSMatthias Ringwald             // remove from controller if already present
3716e83201bcSMatthias Ringwald             entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER;
3717e83201bcSMatthias Ringwald             continue;
3718e83201bcSMatthias Ringwald         }
371991915b0bSMatthias Ringwald         // directly remove entry from whitelist
3720665d90f2SMatthias Ringwald         btstack_linked_list_iterator_remove(&it);
3721e83201bcSMatthias Ringwald         btstack_memory_whitelist_entry_free(entry);
3722e83201bcSMatthias Ringwald     }
3723e83201bcSMatthias Ringwald     hci_run();
3724ac9c45e0SMatthias Ringwald }
3725ac9c45e0SMatthias Ringwald 
37264f551432SMatthias Ringwald #endif
37274f551432SMatthias Ringwald 
372835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
3729ac9c45e0SMatthias Ringwald /**
3730ff00ed1cSMatthias Ringwald  * @brief Set Extended Inquiry Response data
3731ff00ed1cSMatthias Ringwald  * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup
3732ff00ed1cSMatthias Ringwald  * @note has to be done before stack starts up
3733ff00ed1cSMatthias Ringwald  */
3734ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){
3735ff00ed1cSMatthias Ringwald     hci_stack->eir_data = data;
3736ff00ed1cSMatthias Ringwald }
3737ff00ed1cSMatthias Ringwald 
3738ff00ed1cSMatthias Ringwald /**
3739f6858d14SMatthias Ringwald  * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on.
3740f6858d14SMatthias Ringwald  * @param inquriy_mode see bluetooth_defines.h
3741f6858d14SMatthias Ringwald  */
3742f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){
3743f6858d14SMatthias Ringwald     hci_stack->inquiry_mode = mode;
3744f6858d14SMatthias Ringwald }
3745f6858d14SMatthias Ringwald 
3746f6858d14SMatthias Ringwald /**
3747d950d659SMatthias Ringwald  * @brief Configure Voice Setting for use with SCO data in HSP/HFP
3748d950d659SMatthias Ringwald  */
3749d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){
3750d950d659SMatthias Ringwald     hci_stack->sco_voice_setting = voice_setting;
3751d950d659SMatthias Ringwald }
3752d950d659SMatthias Ringwald 
3753d950d659SMatthias Ringwald /**
3754d950d659SMatthias Ringwald  * @brief Get SCO Voice Setting
3755d950d659SMatthias Ringwald  * @return current voice setting
3756d950d659SMatthias Ringwald  */
37570cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){
3758d950d659SMatthias Ringwald     return hci_stack->sco_voice_setting;
3759d950d659SMatthias Ringwald }
3760d950d659SMatthias Ringwald 
3761b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting
3762b3aad8daSMatthias Ringwald  *  @note  Using SCO packets of the exact length is required for USB transfer
3763b3aad8daSMatthias Ringwald  *  @return Length of SCO packets in bytes (not audio frames)
3764b3aad8daSMatthias Ringwald  */
3765b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){
3766b3aad8daSMatthias Ringwald     // see Core Spec for H2 USB Transfer.
3767b3aad8daSMatthias Ringwald     if (hci_stack->sco_voice_setting & 0x0020) return 51;
3768b3aad8daSMatthias Ringwald     return 27;
3769b3aad8daSMatthias Ringwald }
377035454696SMatthias Ringwald #endif
3771b3aad8daSMatthias Ringwald 
3772d950d659SMatthias Ringwald /**
3773d23838ecSMatthias Ringwald  * @brief Set callback for Bluetooth Hardware Error
3774d23838ecSMatthias Ringwald  */
3775c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){
3776d23838ecSMatthias Ringwald     hci_stack->hardware_error_callback = fn;
3777d23838ecSMatthias Ringwald }
3778d23838ecSMatthias Ringwald 
3779a6ddbcb9SMatthias Ringwald /**
3780a6ddbcb9SMatthias Ringwald  * @brief Set callback for local information from Bluetooth controller right after HCI Reset
3781a6ddbcb9SMatthias Ringwald  * @note Can be used to select chipset driver dynamically during startup
3782a6ddbcb9SMatthias Ringwald  */
3783a6ddbcb9SMatthias Ringwald void hci_set_local_version_information_callback(void (*fn)(uint8_t * local_version_information)){
3784a6ddbcb9SMatthias Ringwald     hci_stack->local_version_information_callback = fn;
3785a6ddbcb9SMatthias Ringwald }
3786a6ddbcb9SMatthias Ringwald 
378771de195eSMatthias Ringwald void hci_disconnect_all(void){
3788665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3789665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
3790665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3791665d90f2SMatthias Ringwald         hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it);
379204a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
379304a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
379404a6ef8cSmatthias.ringwald     }
3795d31fba26S[email protected]     hci_run();
379604a6ef8cSmatthias.ringwald }
379733373e40SMatthias Ringwald 
379833373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){
379933373e40SMatthias Ringwald     return hci_stack->manufacturer;
380033373e40SMatthias Ringwald }
3801