xref: /btstack/src/hci.c (revision cd77dd385068cb29ed6b86bbac26b7d2c5e24958)
11f504dbdSmatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
391f504dbdSmatthias.ringwald  *  hci.c
401f504dbdSmatthias.ringwald  *
411f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
421f504dbdSmatthias.ringwald  *
431f504dbdSmatthias.ringwald  */
441f504dbdSmatthias.ringwald 
457907f069SMatthias Ringwald #include "btstack_config.h"
4628171530Smatthias.ringwald 
477f2435e6Smatthias.ringwald 
4806b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC
49aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
508f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h"
51a484130cSMatthias Ringwald #endif
5206b9e820SMatthias Ringwald #endif
53a484130cSMatthias Ringwald 
544a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
55d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h"
564a3574a1SMatthias Ringwald #endif
574a3574a1SMatthias Ringwald 
58a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
59472a5742SMatthias Ringwald #include "gap.h"
6045c102fdSMatthias Ringwald #endif
6145c102fdSMatthias Ringwald 
6293b8dc03Smatthias.ringwald #include <stdarg.h>
6393b8dc03Smatthias.ringwald #include <string.h>
6456fe0872Smatthias.ringwald #include <stdio.h>
655838a2edSMatthias Ringwald #include <inttypes.h>
667f2435e6Smatthias.ringwald 
6716ece135SMatthias Ringwald #include "btstack_debug.h"
680e2df43fSMatthias Ringwald #include "btstack_event.h"
694a3574a1SMatthias Ringwald #include "btstack_linked_list.h"
704a3574a1SMatthias Ringwald #include "btstack_memory.h"
714a3574a1SMatthias Ringwald #include "gap.h"
724a3574a1SMatthias Ringwald #include "hci.h"
734a3574a1SMatthias Ringwald #include "hci_cmd.h"
74d8905019Smatthias.ringwald #include "hci_dump.h"
7593b8dc03Smatthias.ringwald 
761b0e3922Smatthias.ringwald 
77169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
78659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200
79ee091cf1Smatthias.ringwald 
80b83d5eabSMatthias Ringwald // prototypes
8135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
82758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
8335454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled);
8435454696SMatthias Ringwald static int  hci_local_ssp_activated(void);
8535454696SMatthias Ringwald static int  hci_remote_ssp_supported(hci_con_handle_t con_handle);
8635454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void);
8735454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status);
88a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
8935454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level);
90ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer);
9196a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
9252db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
9352db98b2SMatthias Ringwald #endif
947586ee35S[email protected] static int  hci_power_control_on(void);
957586ee35S[email protected] static void hci_power_control_off(void);
966da48142SSean Wilson static void hci_state_reset(void);
97fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason);
98b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void);
99b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void);
100b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status);
101b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump);
102b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size);
10395d71764SMatthias Ringwald static void hci_run(void);
10495d71764SMatthias Ringwald static int  hci_is_le_connection(hci_connection_t * connection);
10595d71764SMatthias Ringwald static int  hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type);
1065d509858SMatthias Ringwald 
107a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
10839677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c
10939677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size);
11042ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address);
1115d509858SMatthias Ringwald #endif
112758b46ceSmatthias.ringwald 
11306b35ec0Smatthias.ringwald // the STACK is here
1143a9fb326S[email protected] #ifndef HAVE_MALLOC
1153a9fb326S[email protected] static hci_stack_t   hci_stack_static;
1163a9fb326S[email protected] #endif
1173a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
11816833f0aSmatthias.ringwald 
11966fb9560S[email protected] // test helper
12066fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
12166fb9560S[email protected] 
12296a45072S[email protected] /**
12396a45072S[email protected]  * create connection for given address
12496a45072S[email protected]  *
12596a45072S[email protected]  * @return connection OR NULL, if no memory left
12696a45072S[email protected]  */
12796a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
1281a06f663S[email protected]     log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type);
129bb69aaaeS[email protected]     hci_connection_t * conn = btstack_memory_hci_connection_get();
13096a45072S[email protected]     if (!conn) return NULL;
131c91d150bS[email protected]     memset(conn, 0, sizeof(hci_connection_t));
132058e3d6bSMatthias Ringwald     bd_addr_copy(conn->address, addr);
13396a45072S[email protected]     conn->address_type = addr_type;
13496a45072S[email protected]     conn->con_handle = 0xffff;
13596a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
13696a45072S[email protected]     conn->bonding_flags = 0;
13796a45072S[email protected]     conn->requested_security_level = LEVEL_0;
13852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
13991a977e8SMatthias Ringwald     btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler);
14091a977e8SMatthias Ringwald     btstack_run_loop_set_timer_context(&conn->timeout, conn);
14196a45072S[email protected]     hci_connection_timestamp(conn);
14252db98b2SMatthias Ringwald #endif
14396a45072S[email protected]     conn->acl_recombination_length = 0;
14496a45072S[email protected]     conn->acl_recombination_pos = 0;
14596a45072S[email protected]     conn->num_acl_packets_sent = 0;
1461a06f663S[email protected]     conn->num_sco_packets_sent = 0;
147da886c03S[email protected]     conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
148665d90f2SMatthias Ringwald     btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn);
14996a45072S[email protected]     return conn;
15096a45072S[email protected] }
15166fb9560S[email protected] 
152da886c03S[email protected] 
153da886c03S[email protected] /**
154da886c03S[email protected]  * get le connection parameter range
155da886c03S[email protected] *
156da886c03S[email protected]  * @return le connection parameter range struct
157da886c03S[email protected]  */
1584ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){
1594ced4e8cSMatthias Ringwald     *range = hci_stack->le_connection_parameter_range;
160da886c03S[email protected] }
161da886c03S[email protected] 
162da886c03S[email protected] /**
163da886c03S[email protected]  * set le connection parameter range
164da886c03S[email protected]  *
165da886c03S[email protected]  */
166da886c03S[email protected] 
1674ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){
1684ced4e8cSMatthias Ringwald     hci_stack->le_connection_parameter_range = *range;
169da886c03S[email protected] }
170da886c03S[email protected] 
171da886c03S[email protected] /**
172da886c03S[email protected]  * get hci connections iterator
173da886c03S[email protected]  *
174da886c03S[email protected]  * @return hci connections iterator
175da886c03S[email protected]  */
176da886c03S[email protected] 
177665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
178665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(it, &hci_stack->connections);
179da886c03S[email protected] }
180da886c03S[email protected] 
18197addcc5Smatthias.ringwald /**
182ee091cf1Smatthias.ringwald  * get connection for a given handle
183ee091cf1Smatthias.ringwald  *
184ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
185ee091cf1Smatthias.ringwald  */
1865061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
187665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
188665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
189665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
190665d90f2SMatthias Ringwald         hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
1913ac2fe56S[email protected]         if ( item->con_handle == con_handle ) {
192da886c03S[email protected]             return item;
193ee091cf1Smatthias.ringwald         }
194ee091cf1Smatthias.ringwald     }
195ee091cf1Smatthias.ringwald     return NULL;
196ee091cf1Smatthias.ringwald }
197ee091cf1Smatthias.ringwald 
19896a45072S[email protected] /**
19996a45072S[email protected]  * get connection for given address
20096a45072S[email protected]  *
20196a45072S[email protected]  * @return connection OR NULL, if not found
20296a45072S[email protected]  */
2032e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t  addr, bd_addr_type_t addr_type){
204665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
205665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
206665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
207665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
20896a45072S[email protected]         if (connection->address_type != addr_type)  continue;
20996a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
21062bda3fbS[email protected]         return connection;
21162bda3fbS[email protected]     }
21262bda3fbS[email protected]     return NULL;
21362bda3fbS[email protected] }
21462bda3fbS[email protected] 
21552db98b2SMatthias Ringwald 
21652db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
21752db98b2SMatthias Ringwald 
218ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){
21991a977e8SMatthias Ringwald     hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer);
220aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
221528a4a3bSMatthias Ringwald     if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
222c785ef68Smatthias.ringwald         // connections might be timed out
223c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
224c785ef68Smatthias.ringwald     }
225f316a845SMatthias Ringwald #else
226528a4a3bSMatthias Ringwald     if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){
2275f26aadcSMatthias Ringwald         // connections might be timed out
2285f26aadcSMatthias Ringwald         hci_emit_l2cap_check_timeout(connection);
2295f26aadcSMatthias Ringwald     }
2305f26aadcSMatthias Ringwald #endif
231c785ef68Smatthias.ringwald }
232ee091cf1Smatthias.ringwald 
233ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
234aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK
235528a4a3bSMatthias Ringwald     connection->timestamp = btstack_run_loop_embedded_get_ticks();
236f316a845SMatthias Ringwald #else
237528a4a3bSMatthias Ringwald     connection->timestamp = btstack_run_loop_get_time_ms();
2385f26aadcSMatthias Ringwald #endif
239ee091cf1Smatthias.ringwald }
240ee091cf1Smatthias.ringwald 
24128ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
24228ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
24328ca2b46S[email protected] }
24428ca2b46S[email protected] 
24535454696SMatthias Ringwald 
24628ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
24728ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
24828ca2b46S[email protected] }
24928ca2b46S[email protected] 
25043bfb1bdSmatthias.ringwald /**
25180ca58a0Smatthias.ringwald  * add authentication flags and reset timer
25296a45072S[email protected]  * @note: assumes classic connection
2532e77e513S[email protected]  * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets
2547fde4af9Smatthias.ringwald  */
2557fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
2567fde4af9Smatthias.ringwald     bd_addr_t addr;
257724d70a2SMatthias Ringwald     reverse_bd_addr(bd_addr, addr);
2582e77e513S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2597fde4af9Smatthias.ringwald     if (conn) {
26028ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
26180ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
2627fde4af9Smatthias.ringwald     }
2637fde4af9Smatthias.ringwald }
2647fde4af9Smatthias.ringwald 
26580ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2665061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
26780ca58a0Smatthias.ringwald     if (!conn) return 0;
2686724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2696724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2706724cd9eS[email protected]     return 0;
27180ca58a0Smatthias.ringwald }
27280ca58a0Smatthias.ringwald 
27315a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){
27455597469SMatthias Ringwald     if (!hci_stack->link_key_db) return;
2752bacf595SMatthias Ringwald     log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr));
276a98592bcSMatthias Ringwald     hci_stack->link_key_db->delete_link_key(addr);
277c12e46e7Smatthias.ringwald }
27855597469SMatthias Ringwald 
27955597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){
28055597469SMatthias Ringwald     if (!hci_stack->link_key_db) return;
2812bacf595SMatthias Ringwald     log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type);
28255597469SMatthias Ringwald     hci_stack->link_key_db->put_link_key(addr, link_key, type);
283c12e46e7Smatthias.ringwald }
28435454696SMatthias Ringwald #endif
285c12e46e7Smatthias.ringwald 
28695d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){
2870bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2880bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2890bf6344aS[email protected] }
2900bf6344aS[email protected] 
2917fde4af9Smatthias.ringwald /**
29243bfb1bdSmatthias.ringwald  * count connections
29343bfb1bdSmatthias.ringwald  */
29440d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
29556c253c9Smatthias.ringwald     int count = 0;
296665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
297665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
29843bfb1bdSmatthias.ringwald     return count;
29943bfb1bdSmatthias.ringwald }
300c8e4258aSmatthias.ringwald 
30195d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){
302ee303eddS[email protected] 
303f04a0c31SMatthias Ringwald     unsigned int num_packets_sent_classic = 0;
304f04a0c31SMatthias Ringwald     unsigned int num_packets_sent_le = 0;
305ee303eddS[email protected] 
306665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
307665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
308998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
309ee303eddS[email protected]         if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
310ee303eddS[email protected]             num_packets_sent_classic += connection->num_acl_packets_sent;
311ee303eddS[email protected]         } else {
312ee303eddS[email protected]             num_packets_sent_le += connection->num_acl_packets_sent;
313ee303eddS[email protected]         }
314ee303eddS[email protected]     }
315d999b54eSMatthias Ringwald     log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num);
316ee303eddS[email protected]     int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic;
317ee303eddS[email protected]     int free_slots_le = 0;
318ee303eddS[email protected] 
319ee303eddS[email protected]     if (free_slots_classic < 0){
3209da54300S[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);
321998906cdSmatthias.ringwald         return 0;
322998906cdSmatthias.ringwald     }
323ee303eddS[email protected] 
324ee303eddS[email protected]     if (hci_stack->le_acl_packets_total_num){
325ee303eddS[email protected]         // if we have LE slots, they are used
326ee303eddS[email protected]         free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le;
327ee303eddS[email protected]         if (free_slots_le < 0){
3289da54300S[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);
329ee303eddS[email protected]             return 0;
330998906cdSmatthias.ringwald         }
331ee303eddS[email protected]     } else {
332ee303eddS[email protected]         // otherwise, classic slots are used for LE, too
333ee303eddS[email protected]         free_slots_classic -= num_packets_sent_le;
334ee303eddS[email protected]         if (free_slots_classic < 0){
3359da54300S[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);
336ee303eddS[email protected]             return 0;
337ee303eddS[email protected]         }
338ee303eddS[email protected]     }
339ee303eddS[email protected] 
340ee303eddS[email protected]     switch (address_type){
341ee303eddS[email protected]         case BD_ADDR_TYPE_UNKNOWN:
3422125de09SMatthias Ringwald             log_error("hci_number_free_acl_slots: unknown address type");
343ee303eddS[email protected]             return 0;
344ee303eddS[email protected] 
345ee303eddS[email protected]         case BD_ADDR_TYPE_CLASSIC:
346ee303eddS[email protected]             return free_slots_classic;
347ee303eddS[email protected] 
348ee303eddS[email protected]         default:
349cb00d3aaS[email protected]            if (hci_stack->le_acl_packets_total_num){
350ee303eddS[email protected]                return free_slots_le;
351ee303eddS[email protected]            }
352cb00d3aaS[email protected]            return free_slots_classic;
353cb00d3aaS[email protected]     }
354998906cdSmatthias.ringwald }
355998906cdSmatthias.ringwald 
3562125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){
3572125de09SMatthias Ringwald     // get connection type
3582125de09SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3592125de09SMatthias Ringwald     if (!connection){
3602125de09SMatthias Ringwald         log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle);
3612125de09SMatthias Ringwald         return 0;
3622125de09SMatthias Ringwald     }
3632125de09SMatthias Ringwald     return hci_number_free_acl_slots_for_connection_type(connection->address_type);
3642125de09SMatthias Ringwald }
3652125de09SMatthias Ringwald 
36635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
367701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){
368f04a0c31SMatthias Ringwald     unsigned int num_sco_packets_sent  = 0;
369665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
370665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
371e35edcc1S[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
372e35edcc1S[email protected]         num_sco_packets_sent += connection->num_sco_packets_sent;
373e35edcc1S[email protected]     }
374e35edcc1S[email protected]     if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
375701e3307SMatthias Ringwald         log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num);
37644d0e3d5S[email protected]         return 0;
37744d0e3d5S[email protected]     }
378701e3307SMatthias Ringwald     // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent);
379e35edcc1S[email protected]     return hci_stack->sco_packets_total_num - num_sco_packets_sent;
380e35edcc1S[email protected] }
38135454696SMatthias Ringwald #endif
38244d0e3d5S[email protected] 
383ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
384ac928cc2S[email protected] int hci_can_send_command_packet_now(void){
385ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
386ac928cc2S[email protected] 
387ac928cc2S[email protected]     // check for async hci transport implementations
388ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
389ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
390ac928cc2S[email protected]             return 0;
391ac928cc2S[email protected]         }
392ac928cc2S[email protected]     }
393ac928cc2S[email protected] 
394ac928cc2S[email protected]     return hci_stack->num_cmd_packets > 0;
395ac928cc2S[email protected] }
396ac928cc2S[email protected] 
3979d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){
398ac928cc2S[email protected]     // check for async hci transport implementations
3999d04d3a7SMatthias Ringwald     if (!hci_stack->hci_transport->can_send_packet_now) return 1;
4009d04d3a7SMatthias Ringwald     return hci_stack->hci_transport->can_send_packet_now(packet_type);
401ac928cc2S[email protected] }
4029d04d3a7SMatthias Ringwald 
4039d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){
4049d04d3a7SMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0;
4059d04d3a7SMatthias Ringwald     return hci_number_free_acl_slots_for_connection_type(address_type) > 0;
406ac928cc2S[email protected] }
4079d04d3a7SMatthias Ringwald 
4089d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){
4099d04d3a7SMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
4109d04d3a7SMatthias Ringwald     return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC);
4119d04d3a7SMatthias Ringwald }
4129d04d3a7SMatthias Ringwald 
4139d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
4149d04d3a7SMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0;
415e79abdd6S[email protected]     return hci_number_free_acl_slots_for_handle(con_handle) > 0;
416ac928cc2S[email protected] }
417ac928cc2S[email protected] 
418ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
419ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
420ac928cc2S[email protected]     return hci_can_send_prepared_acl_packet_now(con_handle);
4216b4af23dS[email protected] }
4226b4af23dS[email protected] 
42335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
42435454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){
42535454696SMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
42635454696SMatthias Ringwald     return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC);
42735454696SMatthias Ringwald }
42835454696SMatthias Ringwald 
429701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){
430d057580eSMatthias Ringwald     if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0;
431ed361f5fSMatthias Ringwald     if (!hci_stack->synchronous_flow_control_enabled) return 1;
432701e3307SMatthias Ringwald     return hci_number_free_sco_slots() > 0;
43344d0e3d5S[email protected] }
43444d0e3d5S[email protected] 
435701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){
436d057580eSMatthias Ringwald     if (hci_stack->hci_packet_buffer_reserved) return 0;
437701e3307SMatthias Ringwald     return hci_can_send_prepared_sco_packet_now();
43844d0e3d5S[email protected] }
43944d0e3d5S[email protected] 
440d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){
441d057580eSMatthias Ringwald     hci_stack->sco_waiting_for_can_send_now = 1;
442d057580eSMatthias Ringwald     hci_notify_if_sco_can_send_now();
443d057580eSMatthias Ringwald }
44435454696SMatthias Ringwald #endif
445d057580eSMatthias Ringwald 
44695d71764SMatthias Ringwald // used for internal checks in l2cap.c
447c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
448c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
449c8b9416aS[email protected] }
450c8b9416aS[email protected] 
4516b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
4526b4af23dS[email protected] int hci_reserve_packet_buffer(void){
4539d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
4549d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
4559d14b626S[email protected]         return 0;
4569d14b626S[email protected]     }
4576b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
4586b4af23dS[email protected]     return 1;
4596b4af23dS[email protected] }
4606b4af23dS[email protected] 
46168a0fcf7S[email protected] void hci_release_packet_buffer(void){
46268a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
46368a0fcf7S[email protected] }
46468a0fcf7S[email protected] 
4656b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
4667f02f414SMatthias Ringwald static int hci_transport_synchronous(void){
4676b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
4686b4af23dS[email protected] }
4696b4af23dS[email protected] 
470452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){
471452cf3bbS[email protected] 
472452cf3bbS[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);
473452cf3bbS[email protected] 
474452cf3bbS[email protected]     // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers
475452cf3bbS[email protected]     uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length;
476452cf3bbS[email protected]     if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){
477452cf3bbS[email protected]         max_acl_data_packet_length = hci_stack->le_data_packets_length;
478452cf3bbS[email protected]     }
479452cf3bbS[email protected] 
480452cf3bbS[email protected]     // testing: reduce buffer to minimum
481452cf3bbS[email protected]     // max_acl_data_packet_length = 52;
482452cf3bbS[email protected] 
483d999b54eSMatthias Ringwald     log_debug("hci_send_acl_packet_fragments entered");
484d999b54eSMatthias Ringwald 
485452cf3bbS[email protected]     int err;
486452cf3bbS[email protected]     // multiple packets could be send on a synchronous HCI transport
487452cf3bbS[email protected]     while (1){
488452cf3bbS[email protected] 
489d999b54eSMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop entered");
490d999b54eSMatthias Ringwald 
491452cf3bbS[email protected]         // get current data
492452cf3bbS[email protected]         const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4;
493452cf3bbS[email protected]         int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
494452cf3bbS[email protected]         int more_fragments = 0;
495452cf3bbS[email protected] 
496452cf3bbS[email protected]         // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
497452cf3bbS[email protected]         if (current_acl_data_packet_length > max_acl_data_packet_length){
498452cf3bbS[email protected]             more_fragments = 1;
499452cf3bbS[email protected]             current_acl_data_packet_length = max_acl_data_packet_length;
500452cf3bbS[email protected]         }
501452cf3bbS[email protected] 
502452cf3bbS[email protected]         // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent)
503452cf3bbS[email protected]         if (acl_header_pos > 0){
504f8fbdce0SMatthias Ringwald             uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
505452cf3bbS[email protected]             handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12);
506f8fbdce0SMatthias Ringwald             little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
507452cf3bbS[email protected]         }
508452cf3bbS[email protected] 
509452cf3bbS[email protected]         // update header len
510f8fbdce0SMatthias Ringwald         little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length);
511452cf3bbS[email protected] 
512452cf3bbS[email protected]         // count packet
513452cf3bbS[email protected]         connection->num_acl_packets_sent++;
514f04a0c31SMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments);
515d999b54eSMatthias Ringwald 
516d999b54eSMatthias Ringwald         // update state for next fragment (if any) as "transport done" might be sent during send_packet already
517d999b54eSMatthias Ringwald         if (more_fragments){
518d999b54eSMatthias Ringwald             // update start of next fragment to send
519d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
520d999b54eSMatthias Ringwald         } else {
521d999b54eSMatthias Ringwald             // done
522d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_pos = 0;
523d999b54eSMatthias Ringwald             hci_stack->acl_fragmentation_total_size = 0;
524d999b54eSMatthias Ringwald         }
525452cf3bbS[email protected] 
526452cf3bbS[email protected]         // send packet
527452cf3bbS[email protected]         uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
528452cf3bbS[email protected]         const int size = current_acl_data_packet_length + 4;
5295bb5bc3eS[email protected]         hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
530452cf3bbS[email protected]         err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
531452cf3bbS[email protected] 
532f04a0c31SMatthias Ringwald         log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments);
533d999b54eSMatthias Ringwald 
534452cf3bbS[email protected]         // done yet?
535452cf3bbS[email protected]         if (!more_fragments) break;
536452cf3bbS[email protected] 
537452cf3bbS[email protected]         // can send more?
538452cf3bbS[email protected]         if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err;
539452cf3bbS[email protected]     }
540452cf3bbS[email protected] 
541d999b54eSMatthias Ringwald     log_debug("hci_send_acl_packet_fragments loop over");
542452cf3bbS[email protected] 
543d051460cS[email protected]     // release buffer now for synchronous transport
544203bace6S[email protected]     if (hci_transport_synchronous()){
545452cf3bbS[email protected]         hci_release_packet_buffer();
54663fa3374SMatthias Ringwald         // notify upper stack that it might be possible to send again
54763fa3374SMatthias Ringwald         uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
548d6b06661SMatthias Ringwald         hci_emit_event(&event[0], sizeof(event), 0);  // don't dump
549452cf3bbS[email protected]     }
550452cf3bbS[email protected] 
551452cf3bbS[email protected]     return err;
552452cf3bbS[email protected] }
553452cf3bbS[email protected] 
554826f7347S[email protected] // pre: caller has reserved the packet buffer
555826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
5567856c818Smatthias.ringwald 
557452cf3bbS[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
558452cf3bbS[email protected] 
559826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
560826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
561826f7347S[email protected]         return 0;
562826f7347S[email protected]     }
563826f7347S[email protected] 
564d713a683S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
565d713a683S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
566d713a683S[email protected] 
567826f7347S[email protected]     // check for free places on Bluetooth module
568d713a683S[email protected]     if (!hci_can_send_prepared_acl_packet_now(con_handle)) {
569826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
57097b61c7bS[email protected]         hci_release_packet_buffer();
57197b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
57297b61c7bS[email protected]     }
5736218e6f1Smatthias.ringwald 
5745061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
57597b61c7bS[email protected]     if (!connection) {
5765fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
57797b61c7bS[email protected]         hci_release_packet_buffer();
57897b61c7bS[email protected]         return 0;
57997b61c7bS[email protected]     }
58052db98b2SMatthias Ringwald 
58152db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
58256cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
58352db98b2SMatthias Ringwald #endif
58456cf178bSmatthias.ringwald 
585452cf3bbS[email protected]     // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size);
5867856c818Smatthias.ringwald 
587452cf3bbS[email protected]     // setup data
588452cf3bbS[email protected]     hci_stack->acl_fragmentation_total_size = size;
589452cf3bbS[email protected]     hci_stack->acl_fragmentation_pos = 4;   // start of L2CAP packet
5906218e6f1Smatthias.ringwald 
591452cf3bbS[email protected]     return hci_send_acl_packet_fragments(connection);
592ee091cf1Smatthias.ringwald }
593ee091cf1Smatthias.ringwald 
59435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
59544d0e3d5S[email protected] // pre: caller has reserved the packet buffer
59644d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){
59744d0e3d5S[email protected] 
59844d0e3d5S[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
59944d0e3d5S[email protected] 
60044d0e3d5S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
60144d0e3d5S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
60244d0e3d5S[email protected]         return 0;
60344d0e3d5S[email protected]     }
60444d0e3d5S[email protected] 
60544d0e3d5S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
6064b3e1e19SMatthias Ringwald 
6074b3e1e19SMatthias Ringwald     // skip checks in loopback mode
6084b3e1e19SMatthias Ringwald     if (!hci_stack->loopback_mode){
60944d0e3d5S[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);   // same for ACL and SCO
61044d0e3d5S[email protected] 
61144d0e3d5S[email protected]         // check for free places on Bluetooth module
612701e3307SMatthias Ringwald         if (!hci_can_send_prepared_sco_packet_now()) {
61344d0e3d5S[email protected]             log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller");
61444d0e3d5S[email protected]             hci_release_packet_buffer();
61544d0e3d5S[email protected]             return BTSTACK_ACL_BUFFERS_FULL;
61644d0e3d5S[email protected]         }
61744d0e3d5S[email protected] 
618e35edcc1S[email protected]         // track send packet in connection struct
619e35edcc1S[email protected]         hci_connection_t *connection = hci_connection_for_handle( con_handle);
620e35edcc1S[email protected]         if (!connection) {
621e35edcc1S[email protected]             log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle);
622e35edcc1S[email protected]             hci_release_packet_buffer();
623e35edcc1S[email protected]             return 0;
624e35edcc1S[email protected]         }
625e35edcc1S[email protected]         connection->num_sco_packets_sent++;
6264b3e1e19SMatthias Ringwald     }
62744d0e3d5S[email protected] 
62844d0e3d5S[email protected]     hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size);
629543e835cSMatthias Ringwald     int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size);
630543e835cSMatthias Ringwald 
631543e835cSMatthias Ringwald     if (hci_transport_synchronous()){
632543e835cSMatthias Ringwald         hci_release_packet_buffer();
63363fa3374SMatthias Ringwald         // notify upper stack that it might be possible to send again
63463fa3374SMatthias Ringwald         uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
635d6b06661SMatthias Ringwald         hci_emit_event(&event[0], sizeof(event), 0);    // don't dump
636543e835cSMatthias Ringwald     }
637543e835cSMatthias Ringwald 
638543e835cSMatthias Ringwald     return err;
63944d0e3d5S[email protected] }
64035454696SMatthias Ringwald #endif
64144d0e3d5S[email protected] 
64216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
6437856c818Smatthias.ringwald 
644e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
645e76a89eeS[email protected] 
6467856c818Smatthias.ringwald     // get info
6477856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
6485061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
6497856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
6507856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
6517856c818Smatthias.ringwald 
6527856c818Smatthias.ringwald     // ignore non-registered handle
6537856c818Smatthias.ringwald     if (!conn){
6549da54300S[email protected]         log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle);
6557856c818Smatthias.ringwald         return;
6567856c818Smatthias.ringwald     }
6577856c818Smatthias.ringwald 
658e76a89eeS[email protected]     // assert packet is complete
6599ecc3e17S[email protected]     if (acl_length + 4 != size){
660f04a0c31SMatthias Ringwald         log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4);
661e76a89eeS[email protected]         return;
662e76a89eeS[email protected]     }
663e76a89eeS[email protected] 
66452db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC
6657856c818Smatthias.ringwald     // update idle timestamp
6667856c818Smatthias.ringwald     hci_connection_timestamp(conn);
66752db98b2SMatthias Ringwald #endif
6687856c818Smatthias.ringwald 
6697856c818Smatthias.ringwald     // handle different packet types
6707856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
6717856c818Smatthias.ringwald 
6727856c818Smatthias.ringwald         case 0x01: // continuation fragment
6737856c818Smatthias.ringwald 
6740ca847afS[email protected]             // sanity checks
6757856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
6769da54300S[email protected]                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle);
6777856c818Smatthias.ringwald                 return;
6787856c818Smatthias.ringwald             }
6790ca847afS[email protected]             if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){
6800ca847afS[email protected]                 log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x",
6810ca847afS[email protected]                     conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
6820ca847afS[email protected]                 conn->acl_recombination_pos = 0;
6830ca847afS[email protected]                 return;
6840ca847afS[email protected]             }
6857856c818Smatthias.ringwald 
6867856c818Smatthias.ringwald             // append fragment payload (header already stored)
687ec6321eeS[email protected]             memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length );
6887856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
6897856c818Smatthias.ringwald 
6909da54300S[email protected]             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length,
691decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
6927856c818Smatthias.ringwald 
6937856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
6947856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
695d6b06661SMatthias Ringwald                 hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos);
6967856c818Smatthias.ringwald                 // reset recombination buffer
6977856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
6987856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
6997856c818Smatthias.ringwald             }
7007856c818Smatthias.ringwald             break;
7017856c818Smatthias.ringwald 
7027856c818Smatthias.ringwald         case 0x02: { // first fragment
7037856c818Smatthias.ringwald 
70423a77e1aS[email protected]             // sanity check
70523a77e1aS[email protected]             if (conn->acl_recombination_pos) {
70623a77e1aS[email protected]                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle);
70723a77e1aS[email protected]                 conn->acl_recombination_pos = 0;
70823a77e1aS[email protected]             }
70923a77e1aS[email protected] 
7107856c818Smatthias.ringwald             // peek into L2CAP packet!
7117856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
7127856c818Smatthias.ringwald 
7139da54300S[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length);
714decc01a8Smatthias.ringwald 
7157856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
7167856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
7177856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
718d6b06661SMatthias Ringwald                 hci_emit_acl_packet(packet, acl_length + 4);
7197856c818Smatthias.ringwald             } else {
7200ca847afS[email protected] 
7210ca847afS[email protected]                 if (acl_length > HCI_ACL_BUFFER_SIZE){
7220ca847afS[email protected]                     log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x",
7230ca847afS[email protected]                         4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
7240ca847afS[email protected]                     return;
7250ca847afS[email protected]                 }
7260ca847afS[email protected] 
7277856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
728ec6321eeS[email protected]                 memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4);
7297856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
7307856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
731f8fbdce0SMatthias Ringwald                 little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4);
7327856c818Smatthias.ringwald             }
7337856c818Smatthias.ringwald             break;
7347856c818Smatthias.ringwald 
7357856c818Smatthias.ringwald         }
7367856c818Smatthias.ringwald         default:
7379da54300S[email protected]             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03);
7387856c818Smatthias.ringwald             return;
7397856c818Smatthias.ringwald     }
74094ab26f8Smatthias.ringwald 
74194ab26f8Smatthias.ringwald     // execute main loop
74294ab26f8Smatthias.ringwald     hci_run();
74316833f0aSmatthias.ringwald }
74422909952Smatthias.ringwald 
74567a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
7469da54300S[email protected]     log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address));
7473c4d4b90Smatthias.ringwald 
748528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&conn->timeout);
749c785ef68Smatthias.ringwald 
750665d90f2SMatthias Ringwald     btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
751a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
7523c4d4b90Smatthias.ringwald 
7533c4d4b90Smatthias.ringwald     // now it's gone
754c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
755c7e0c5f6Smatthias.ringwald }
756c7e0c5f6Smatthias.ringwald 
75735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
75835454696SMatthias Ringwald 
7590c042179S[email protected] static const uint16_t packet_type_sizes[] = {
7608f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
7618f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
7628f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
7638f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
7648f8108aaSmatthias.ringwald };
76565389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
76665389bfcS[email protected]      0, // 3 slot packets
76765389bfcS[email protected]      1, // 5 slot packets
76865389bfcS[email protected]     25, // EDR 2 mpbs
76965389bfcS[email protected]     26, // EDR 3 mbps
77065389bfcS[email protected]     39, // 3 slot EDR packts
77165389bfcS[email protected]     40, // 5 slot EDR packet
77265389bfcS[email protected] };
77365389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
77465389bfcS[email protected]     0x0f00, // 3 slot packets
77565389bfcS[email protected]     0xf000, // 5 slot packets
77665389bfcS[email protected]     0x1102, // EDR 2 mpbs
77765389bfcS[email protected]     0x2204, // EDR 3 mbps
77865389bfcS[email protected]     0x0300, // 3 slot EDR packts
77965389bfcS[email protected]     0x3000, // 5 slot EDR packet
78065389bfcS[email protected] };
7818f8108aaSmatthias.ringwald 
78265389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
78365389bfcS[email protected]     // enable packet types based on size
7848f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
785f16a69bbS[email protected]     unsigned int i;
7868f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
7878f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
7888f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
7898f8108aaSmatthias.ringwald             packet_types |= 1 << i;
7908f8108aaSmatthias.ringwald         }
7918f8108aaSmatthias.ringwald     }
79265389bfcS[email protected]     // disable packet types due to missing local supported features
79365389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
794f04a0c31SMatthias Ringwald         unsigned int bit_idx = packet_type_feature_requirement_bit[i];
79565389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
79665389bfcS[email protected]         if (feature_set) continue;
79765389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
79865389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
79965389bfcS[email protected]     }
8008f8108aaSmatthias.ringwald     // flip bits for "may not be used"
8018f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
8028f8108aaSmatthias.ringwald     return packet_types;
8038f8108aaSmatthias.ringwald }
8048f8108aaSmatthias.ringwald 
8058f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
8063a9fb326S[email protected]     return hci_stack->packet_types;
8078f8108aaSmatthias.ringwald }
80835454696SMatthias Ringwald #endif
8098f8108aaSmatthias.ringwald 
810facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
8117dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
8123a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
8137dc17943Smatthias.ringwald }
8147dc17943Smatthias.ringwald 
815f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
8163a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
8177dc17943Smatthias.ringwald }
8187dc17943Smatthias.ringwald 
81906b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC
8203e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){
8213e68d23dSMatthias Ringwald     // No. 31, byte 3, bit 7
8223e68d23dSMatthias Ringwald     return (hci_stack->local_supported_features[3] & (1 << 7)) != 0;
8233e68d23dSMatthias Ringwald }
82406b9e820SMatthias Ringwald #endif
8253e68d23dSMatthias Ringwald 
8266ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
8276ac9a97eS[email protected]     // No. 54, byte 6, bit 6
8286ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
8296ac9a97eS[email protected] }
8306ac9a97eS[email protected] 
83115a95bd5SMatthias Ringwald static int gap_ssp_supported(void){
8326ac9a97eS[email protected]     // No. 51, byte 6, bit 3
8333a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
834f5d8d141S[email protected] }
835f5d8d141S[email protected] 
8367f02f414SMatthias Ringwald static int hci_classic_supported(void){
83735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
8386ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
8393a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
84035454696SMatthias Ringwald #else
84135454696SMatthias Ringwald     return 0;
84235454696SMatthias Ringwald #endif
843f5d8d141S[email protected] }
844f5d8d141S[email protected] 
8457f02f414SMatthias Ringwald static int hci_le_supported(void){
846a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
8476ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
8483a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
849f5d8d141S[email protected] #else
850f5d8d141S[email protected]     return 0;
851f5d8d141S[email protected] #endif
852f5d8d141S[email protected] }
853f5d8d141S[email protected] 
85469a97523S[email protected] // get addr type and address used in advertisement packets
85515a95bd5SMatthias Ringwald void gap_advertisements_get_address(uint8_t * addr_type, bd_addr_t  addr){
8563a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
8573a9fb326S[email protected]     if (hci_stack->adv_addr_type){
8583a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
85969a97523S[email protected]     } else {
8603a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
86169a97523S[email protected]     }
86269a97523S[email protected] }
86369a97523S[email protected] 
864a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
86539677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){
8669ec2630cSMatthias Ringwald 
8679ec2630cSMatthias Ringwald     UNUSED(size);
8689ec2630cSMatthias Ringwald 
869d1dc057bS[email protected]     int offset = 3;
870d1dc057bS[email protected]     int num_reports = packet[offset];
871d1dc057bS[email protected]     offset += 1;
872d1dc057bS[email protected] 
87357c9da5bS[email protected]     int i;
8744f4e0224SMatthias Ringwald     // log_info("HCI: handle adv report with num reports: %d", num_reports);
87503fbe9c6S[email protected]     uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
87657c9da5bS[email protected]     for (i=0; i<num_reports;i++){
877210c6774S[email protected]         uint8_t data_length = packet[offset + 8];
878a0aac9c4S[email protected]         uint8_t event_size = 10 + data_length;
879d1dc057bS[email protected]         int pos = 0;
880045013feSMatthias Ringwald         event[pos++] = GAP_EVENT_ADVERTISING_REPORT;
88157c9da5bS[email protected]         event[pos++] = event_size;
882210c6774S[email protected]         memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address
883d1dc057bS[email protected]         offset += 8;
884d1dc057bS[email protected]         pos += 8;
885d1dc057bS[email protected]         event[pos++] = packet[offset + 1 + data_length]; // rssi
886d1dc057bS[email protected]         event[pos++] = packet[offset++]; //data_length;
887d1dc057bS[email protected]         memcpy(&event[pos], &packet[offset], data_length);
88857c9da5bS[email protected]         pos += data_length;
889d1dc057bS[email protected]         offset += data_length + 1; // rssi
890d6b06661SMatthias Ringwald         hci_emit_event(event, pos, 1);
89157c9da5bS[email protected]     }
89257c9da5bS[email protected] }
893b2f949feS[email protected] #endif
89457c9da5bS[email protected] 
895*cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
89606b9e820SMatthias Ringwald 
89796b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){
89896b53536SMatthias Ringwald     if (!hci_stack->config) return 0;
8999796ebeaSMatthias Ringwald     uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
90096b53536SMatthias Ringwald     // Limit baud rate for Broadcom chipsets to 3 mbps
901c6d1dbedSMatthias Ringwald     if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){
90296b53536SMatthias Ringwald         baud_rate = 3000000;
90396b53536SMatthias Ringwald     }
90496b53536SMatthias Ringwald     return baud_rate;
90596b53536SMatthias Ringwald }
90696b53536SMatthias Ringwald 
907ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){
9089ec2630cSMatthias Ringwald     UNUSED(ds);
9099ec2630cSMatthias Ringwald 
9100305bdeaSMatthias Ringwald     switch (hci_stack->substate){
9110305bdeaSMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
9127b0d7667SMatthias Ringwald             log_info("Resend HCI Reset");
9130305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET;
9147b0d7667SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
9150305bdeaSMatthias Ringwald             hci_run();
9160305bdeaSMatthias Ringwald             break;
9179f007422SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET:
9189f007422SMatthias Ringwald             log_info("Resend HCI Reset - CSR Warm Boot with Link Reset");
9199f007422SMatthias Ringwald             if (hci_stack->hci_transport->reset_link){
9209f007422SMatthias Ringwald                 hci_stack->hci_transport->reset_link();
9219f007422SMatthias Ringwald             }
922202c8a4cSMatthias Ringwald             // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT
923e47e68c7SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
924e47e68c7SMatthias Ringwald             log_info("Resend HCI Reset - CSR Warm Boot");
925e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
926e47e68c7SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
927e47e68c7SMatthias Ringwald             hci_run();
928688c2635SMatthias Ringwald             break;
9297224be7eSMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
9307224be7eSMatthias Ringwald             if (hci_stack->hci_transport->set_baudrate){
93196b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
932cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate);
9337dd9d0ecSMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
9347224be7eSMatthias Ringwald             }
935772a36d3SMatthias Ringwald             // For CSR, HCI Reset is sent on new baud rate
9362caefae9SMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO){
937772a36d3SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
938772a36d3SMatthias Ringwald                 hci_run();
939772a36d3SMatthias Ringwald             }
9404696bddbSMatthias Ringwald             break;
9410305bdeaSMatthias Ringwald         default:
9420305bdeaSMatthias Ringwald             break;
9430305bdeaSMatthias Ringwald     }
9440305bdeaSMatthias Ringwald }
94506b9e820SMatthias Ringwald #endif
9460305bdeaSMatthias Ringwald 
94771de195eSMatthias Ringwald static void hci_initializing_next_state(void){
94874b323a9SMatthias Ringwald     hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1);
94974b323a9SMatthias Ringwald }
95074b323a9SMatthias Ringwald 
95174b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true
95271de195eSMatthias Ringwald static void hci_initializing_run(void){
953148ca237SMatthias Ringwald     log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now());
95474b323a9SMatthias Ringwald     switch (hci_stack->substate){
95574b323a9SMatthias Ringwald         case HCI_INIT_SEND_RESET:
95674b323a9SMatthias Ringwald             hci_state_reset();
957a0cf2f3fSMatthias Ringwald 
95806b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
9590305bdeaSMatthias Ringwald             // prepare reset if command complete not received in 100ms
960659d758cSMatthias Ringwald             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
961528a4a3bSMatthias Ringwald             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
962528a4a3bSMatthias Ringwald             btstack_run_loop_add_timer(&hci_stack->timeout);
963a0cf2f3fSMatthias Ringwald #endif
9640305bdeaSMatthias Ringwald             // send command
96574b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET;
9660305bdeaSMatthias Ringwald             hci_send_cmd(&hci_reset);
96774b323a9SMatthias Ringwald             break;
96876fcb19bSMatthias Ringwald         case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION:
96976fcb19bSMatthias Ringwald             hci_send_cmd(&hci_read_local_version_information);
97076fcb19bSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION;
97176fcb19bSMatthias Ringwald             break;
972e90bae01SMatthias Ringwald         case HCI_INIT_SEND_READ_LOCAL_NAME:
973e90bae01SMatthias Ringwald             hci_send_cmd(&hci_read_local_name);
974e90bae01SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME;
975e90bae01SMatthias Ringwald             break;
97606b9e820SMatthias Ringwald 
97706b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
978e47e68c7SMatthias Ringwald         case HCI_INIT_SEND_RESET_CSR_WARM_BOOT:
979e47e68c7SMatthias Ringwald             hci_state_reset();
980e47e68c7SMatthias Ringwald             // prepare reset if command complete not received in 100ms
981659d758cSMatthias Ringwald             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
982528a4a3bSMatthias Ringwald             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
983528a4a3bSMatthias Ringwald             btstack_run_loop_add_timer(&hci_stack->timeout);
984e47e68c7SMatthias Ringwald             // send command
985e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
986e47e68c7SMatthias Ringwald             hci_send_cmd(&hci_reset);
987e47e68c7SMatthias Ringwald             break;
9888d29070eSMatthias Ringwald         case HCI_INIT_SEND_RESET_ST_WARM_BOOT:
9898d29070eSMatthias Ringwald             hci_state_reset();
9908d29070eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT;
9918d29070eSMatthias Ringwald             hci_send_cmd(&hci_reset);
9928d29070eSMatthias Ringwald             break;
993fab26ab3SMatthias Ringwald         case HCI_INIT_SEND_BAUD_CHANGE: {
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);
99774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
9980305bdeaSMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
9994696bddbSMatthias Ringwald             // STLC25000D: baudrate change happens within 0.5 s after command was send,
10004696bddbSMatthias Ringwald             // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial)
1001c6d1dbedSMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
1002659d758cSMatthias Ringwald                 btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
1003528a4a3bSMatthias Ringwald                 btstack_run_loop_add_timer(&hci_stack->timeout);
10044696bddbSMatthias Ringwald             }
100574b323a9SMatthias Ringwald             break;
1006fab26ab3SMatthias Ringwald         }
1007fab26ab3SMatthias Ringwald         case HCI_INIT_SEND_BAUD_CHANGE_BCM: {
100896b53536SMatthias Ringwald             uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
10093fb36a29SMatthias Ringwald             hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer);
1010f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
1011eb3a5314SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM;
1012eb3a5314SMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1013eb3a5314SMatthias Ringwald             break;
1014fab26ab3SMatthias Ringwald         }
101574b323a9SMatthias Ringwald         case HCI_INIT_CUSTOM_INIT:
101674b323a9SMatthias Ringwald             // Custom initialization
10173fb36a29SMatthias Ringwald             if (hci_stack->chipset && hci_stack->chipset->next_command){
10183fb36a29SMatthias Ringwald                 int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer);
101974b323a9SMatthias Ringwald                 if (valid_cmd){
102074b323a9SMatthias Ringwald                     int size = 3 + hci_stack->hci_packet_buffer[2];
1021f8fbdce0SMatthias Ringwald                     hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
102274b323a9SMatthias Ringwald                     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size);
1023e47e68c7SMatthias Ringwald                     switch (valid_cmd) {
1024e47e68c7SMatthias Ringwald                         case 1:
1025e47e68c7SMatthias Ringwald                         default:
1026e47e68c7SMatthias Ringwald                             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT;
1027e47e68c7SMatthias Ringwald                             break;
1028e47e68c7SMatthias Ringwald                         case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete
1029e47e68c7SMatthias Ringwald                             log_info("CSR Warm Boot");
1030659d758cSMatthias Ringwald                             btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS);
1031528a4a3bSMatthias Ringwald                             btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
1032528a4a3bSMatthias Ringwald                             btstack_run_loop_add_timer(&hci_stack->timeout);
10332caefae9SMatthias Ringwald                             if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO
1034772a36d3SMatthias Ringwald                                 && hci_stack->config
10353fb36a29SMatthias Ringwald                                 && hci_stack->chipset
10363fb36a29SMatthias Ringwald                                 // && hci_stack->chipset->set_baudrate_command -- there's no such command
1037772a36d3SMatthias Ringwald                                 && hci_stack->hci_transport->set_baudrate
10382caefae9SMatthias Ringwald                                 && hci_transport_uart_get_main_baud_rate()){
1039772a36d3SMatthias Ringwald                                 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
1040772a36d3SMatthias Ringwald                             } else {
10419f007422SMatthias Ringwald                                hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET;
1042772a36d3SMatthias Ringwald                             }
1043e47e68c7SMatthias Ringwald                             break;
1044e47e68c7SMatthias Ringwald                     }
10450305bdeaSMatthias Ringwald                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
104674b323a9SMatthias Ringwald                     break;
104774b323a9SMatthias Ringwald                 }
1048148ca237SMatthias Ringwald                 log_info("Init script done");
104992a0d36dSMatthias Ringwald 
105092a0d36dSMatthias Ringwald                 // Init script download causes baud rate to reset on Broadcom chipsets, restore UART baud rate if needed
1051c6d1dbedSMatthias Ringwald                 if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
105292a0d36dSMatthias Ringwald                     int need_baud_change = hci_stack->config
10533fb36a29SMatthias Ringwald                         && hci_stack->chipset
10543fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_baudrate_command
105592a0d36dSMatthias Ringwald                         && hci_stack->hci_transport->set_baudrate
10569796ebeaSMatthias Ringwald                         && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
105792a0d36dSMatthias Ringwald                     if (need_baud_change) {
10589796ebeaSMatthias Ringwald                         uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init;
1059cd724cb7SMatthias Ringwald                         log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate);
106092a0d36dSMatthias Ringwald                         hci_stack->hci_transport->set_baudrate(baud_rate);
106192a0d36dSMatthias Ringwald                     }
106292a0d36dSMatthias Ringwald                 }
106374b323a9SMatthias Ringwald             }
106474b323a9SMatthias Ringwald             // otherwise continue
1065a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
1066a828a756SMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_commands);
1067a828a756SMatthias Ringwald             break;
106853860077SMatthias Ringwald         case HCI_INIT_SET_BD_ADDR:
106953860077SMatthias Ringwald             log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr));
10703fb36a29SMatthias Ringwald             hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer);
1071f8fbdce0SMatthias Ringwald             hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
107253860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR;
107353860077SMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
107453860077SMatthias Ringwald             break;
107506b9e820SMatthias Ringwald #endif
107606b9e820SMatthias Ringwald 
107706b9e820SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS:
107806b9e820SMatthias Ringwald             log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset");
107906b9e820SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
108006b9e820SMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_commands);
108106b9e820SMatthias Ringwald             break;
108253860077SMatthias Ringwald         case HCI_INIT_READ_BD_ADDR:
108353860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR;
108453860077SMatthias Ringwald             hci_send_cmd(&hci_read_bd_addr);
108553860077SMatthias Ringwald             break;
108674b323a9SMatthias Ringwald         case HCI_INIT_READ_BUFFER_SIZE:
108774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE;
10880305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_buffer_size);
108974b323a9SMatthias Ringwald             break;
109053860077SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES:
109153860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES;
10920305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_features);
109374b323a9SMatthias Ringwald             break;
109474b323a9SMatthias Ringwald         case HCI_INIT_SET_EVENT_MASK:
10950305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK;
109674b323a9SMatthias Ringwald             if (hci_le_supported()){
109774b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
109874b323a9SMatthias Ringwald             } else {
109974b323a9SMatthias Ringwald                 // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
110074b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
110174b323a9SMatthias Ringwald             }
110274b323a9SMatthias Ringwald             break;
110335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
110474b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE:
110574b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE;
11060305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
110774b323a9SMatthias Ringwald             break;
110874b323a9SMatthias Ringwald         case HCI_INIT_WRITE_PAGE_TIMEOUT:
110974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT;
11100305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_page_timeout, 0x6000);  // ca. 15 sec
111174b323a9SMatthias Ringwald             break;
111274b323a9SMatthias Ringwald         case HCI_INIT_WRITE_CLASS_OF_DEVICE:
111374b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE;
11140305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
111574b323a9SMatthias Ringwald             break;
111674b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LOCAL_NAME:
11170305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME;
111874b323a9SMatthias Ringwald             if (hci_stack->local_name){
111974b323a9SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
112074b323a9SMatthias Ringwald             } else {
11217224be7eSMatthias Ringwald                 char local_name[8+17+1];
11227224be7eSMatthias Ringwald                 // BTstack 11:22:33:44:55:66
11237224be7eSMatthias Ringwald                 memcpy(local_name, "BTstack ", 8);
11247224be7eSMatthias Ringwald                 memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17);   // strlen(bd_addr_to_str(...)) = 17
11257224be7eSMatthias Ringwald                 local_name[8+17] = '\0';
112693bcd4d0SMatthias Ringwald                 log_info("---> Name %s", local_name);
112793bcd4d0SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, local_name);
112874b323a9SMatthias Ringwald             }
112974b323a9SMatthias Ringwald             break;
1130ff00ed1cSMatthias Ringwald         case HCI_INIT_WRITE_EIR_DATA:
11318a114470SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA;
1132ff00ed1cSMatthias Ringwald             hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data);
1133ff00ed1cSMatthias Ringwald             break;
1134f6858d14SMatthias Ringwald         case HCI_INIT_WRITE_INQUIRY_MODE:
1135f6858d14SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE;
11368a114470SMatthias Ringwald             hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode);
1137f6858d14SMatthias Ringwald             break;
113874b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SCAN_ENABLE:
113974b323a9SMatthias Ringwald             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
114074b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE;
114174b323a9SMatthias Ringwald             break;
1142483c5078SMatthias Ringwald         // only sent if ENABLE_SCO_OVER_HCI is defined
1143729ed62eSMatthias Ringwald         case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE:
1144729ed62eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE;
1145729ed62eSMatthias Ringwald             hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled
1146729ed62eSMatthias Ringwald             break;
1147483c5078SMatthias Ringwald         case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING:
1148483c5078SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING;
1149483c5078SMatthias Ringwald             hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1);
1150483c5078SMatthias Ringwald             break;
115135454696SMatthias Ringwald #endif
1152a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
115374b323a9SMatthias Ringwald         // LE INIT
115474b323a9SMatthias Ringwald         case HCI_INIT_LE_READ_BUFFER_SIZE:
115574b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE;
11560305bdeaSMatthias Ringwald             hci_send_cmd(&hci_le_read_buffer_size);
115774b323a9SMatthias Ringwald             break;
115874b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LE_HOST_SUPPORTED:
115974b323a9SMatthias Ringwald             // LE Supported Host = 1, Simultaneous Host = 0
116074b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED;
11610305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
116274b323a9SMatthias Ringwald             break;
11633b6d4121SMatthias Ringwald         case HCI_INIT_READ_WHITE_LIST_SIZE:
11643b6d4121SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE;
11653b6d4121SMatthias Ringwald             hci_send_cmd(&hci_le_read_white_list_size);
11663b6d4121SMatthias Ringwald             break;
116774b323a9SMatthias Ringwald         case HCI_INIT_LE_SET_SCAN_PARAMETERS:
116874b323a9SMatthias Ringwald             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
116974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS;
11700305bdeaSMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
117174b323a9SMatthias Ringwald             break;
117274b323a9SMatthias Ringwald #endif
117374b323a9SMatthias Ringwald         default:
117474b323a9SMatthias Ringwald             return;
117574b323a9SMatthias Ringwald     }
117655975f88SMatthias Ringwald }
117755975f88SMatthias Ringwald 
1178a650ba4dSMatthias Ringwald static void hci_init_done(void){
1179a650ba4dSMatthias Ringwald     // done. tell the app
1180a650ba4dSMatthias Ringwald     log_info("hci_init_done -> HCI_STATE_WORKING");
1181a650ba4dSMatthias Ringwald     hci_stack->state = HCI_STATE_WORKING;
1182a650ba4dSMatthias Ringwald     hci_emit_state();
1183a650ba4dSMatthias Ringwald     hci_run();
1184a650ba4dSMatthias Ringwald }
1185a650ba4dSMatthias Ringwald 
11866155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
11879ec2630cSMatthias Ringwald     UNUSED(size);
11889ec2630cSMatthias Ringwald 
1189a2481739S[email protected]     uint8_t command_completed = 0;
1190c4633093SMatthias Ringwald 
11910e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){
1192f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
11936155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
1194a2481739S[email protected]             command_completed = 1;
1195148ca237SMatthias Ringwald             log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate);
11966155b3d3S[email protected]         } else {
1197d58dd308SMatthias Ringwald             log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate);
11986155b3d3S[email protected]         }
11996155b3d3S[email protected]     }
12000f97eae7SMatthias Ringwald 
12010e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){
12026155b3d3S[email protected]         uint8_t  status = packet[2];
1203f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,4);
12046155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
12056155b3d3S[email protected]             if (status){
1206a2481739S[email protected]                 command_completed = 1;
1207148ca237SMatthias Ringwald                 log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate);
12086155b3d3S[email protected]             } else {
12096155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
12106155b3d3S[email protected]             }
12116155b3d3S[email protected]         } else {
1212148ca237SMatthias Ringwald             log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
12136155b3d3S[email protected]         }
12146155b3d3S[email protected]     }
12150f97eae7SMatthias Ringwald 
121606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
121706b9e820SMatthias Ringwald 
1218e47e68c7SMatthias Ringwald     // Vendor == CSR
12190e2df43fSMatthias Ringwald     if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){
1220e47e68c7SMatthias Ringwald         // TODO: track actual command
1221e47e68c7SMatthias Ringwald         command_completed = 1;
1222e47e68c7SMatthias Ringwald     }
1223a2481739S[email protected] 
12244e9daa6fSMatthias Ringwald     // Vendor == Toshiba
12250e2df43fSMatthias Ringwald     if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){
12264e9daa6fSMatthias Ringwald         // TODO: track actual command
12274e9daa6fSMatthias Ringwald         command_completed = 1;
12284e9daa6fSMatthias Ringwald     }
12294e9daa6fSMatthias Ringwald 
12300f97eae7SMatthias Ringwald     // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661:
12310f97eae7SMatthias Ringwald     // Command complete for HCI Reset arrives after we've resent the HCI Reset command
12320f97eae7SMatthias Ringwald     //
12330f97eae7SMatthias Ringwald     // HCI Reset
12340f97eae7SMatthias Ringwald     // Timeout 100 ms
12350f97eae7SMatthias Ringwald     // HCI Reset
12360f97eae7SMatthias Ringwald     // Command Complete Reset
12370f97eae7SMatthias Ringwald     // HCI Read Local Version Information
12380f97eae7SMatthias Ringwald     // Command Complete Reset - but we expected Command Complete Read Local Version Information
12390f97eae7SMatthias Ringwald     // hang...
12400f97eae7SMatthias Ringwald     //
12410f97eae7SMatthias Ringwald     // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend
12420f97eae7SMatthias Ringwald     if (!command_completed
12430e2df43fSMatthias Ringwald             && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE
12440f97eae7SMatthias Ringwald             && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){
12450f97eae7SMatthias Ringwald 
1246f8fbdce0SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
12470f97eae7SMatthias Ringwald         if (opcode == hci_reset.opcode){
12480f97eae7SMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION;
12490f97eae7SMatthias Ringwald             return;
12500f97eae7SMatthias Ringwald         }
12510f97eae7SMatthias Ringwald     }
12520f97eae7SMatthias Ringwald 
12539f007422SMatthias Ringwald     // CSR & H5
12549f007422SMatthias Ringwald     // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend
12559f007422SMatthias Ringwald     if (!command_completed
12569f007422SMatthias Ringwald             && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE
12579f007422SMatthias Ringwald             && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){
12589f007422SMatthias Ringwald 
12599f007422SMatthias Ringwald         uint16_t opcode = little_endian_read_16(packet,3);
12609f007422SMatthias Ringwald         if (opcode == hci_reset.opcode){
12619f007422SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS;
12629f007422SMatthias Ringwald             return;
12639f007422SMatthias Ringwald         }
12649f007422SMatthias Ringwald     }
12659f007422SMatthias Ringwald 
12669f007422SMatthias 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
12679f007422SMatthias Ringwald     // fix: Correct substate and behave as command below
12689f007422SMatthias Ringwald     if (command_completed){
12699f007422SMatthias Ringwald         switch (hci_stack->substate){
12709f007422SMatthias Ringwald             case HCI_INIT_SEND_RESET:
12719f007422SMatthias Ringwald                 hci_stack->substate = HCI_INIT_W4_SEND_RESET;
12729f007422SMatthias Ringwald                 break;
12739f007422SMatthias Ringwald             case HCI_INIT_SEND_RESET_CSR_WARM_BOOT:
12749f007422SMatthias Ringwald                 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
12759f007422SMatthias Ringwald                 break;
12769f007422SMatthias Ringwald             default:
12779f007422SMatthias Ringwald                 break;
12789f007422SMatthias Ringwald         }
12799f007422SMatthias Ringwald     }
12800f97eae7SMatthias Ringwald 
128106b9e820SMatthias Ringwald #endif
12820f97eae7SMatthias Ringwald 
1283a2481739S[email protected]     if (!command_completed) return;
1284a2481739S[email protected] 
128506b9e820SMatthias Ringwald     int need_baud_change = 0;
128606b9e820SMatthias Ringwald     int need_addr_change = 0;
128706b9e820SMatthias Ringwald 
128806b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
128906b9e820SMatthias Ringwald     need_baud_change = hci_stack->config
12903fb36a29SMatthias Ringwald                         && hci_stack->chipset
12913fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_baudrate_command
1292db8bc6ffSMatthias Ringwald                         && hci_stack->hci_transport->set_baudrate
12939796ebeaSMatthias Ringwald                         && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
1294db8bc6ffSMatthias Ringwald 
129506b9e820SMatthias Ringwald     need_addr_change = hci_stack->custom_bd_addr_set
12963fb36a29SMatthias Ringwald                         && hci_stack->chipset
12973fb36a29SMatthias Ringwald                         && hci_stack->chipset->set_bd_addr_command;
129806b9e820SMatthias Ringwald #endif
1299a80162e9SMatthias Ringwald 
13005c363727SMatthias Ringwald     switch(hci_stack->substate){
130106b9e820SMatthias Ringwald 
130206b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
13039f007422SMatthias Ringwald         case HCI_INIT_SEND_RESET:
1304d58dd308SMatthias Ringwald             // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET
13059f007422SMatthias Ringwald             // fix: just correct substate and behave as command below
13069f007422SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET;
13079f007422SMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
13089f007422SMatthias Ringwald             break;
130974b323a9SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
1310528a4a3bSMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
131176fcb19bSMatthias Ringwald             break;
1312e90bae01SMatthias Ringwald         case HCI_INIT_W4_SEND_READ_LOCAL_NAME:
1313e90bae01SMatthias Ringwald             log_info("Received local name, need baud change %d", need_baud_change);
13142f48d920SMatthias Ringwald             if (need_baud_change){
13152f48d920SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
13162f48d920SMatthias Ringwald                 return;
13172f48d920SMatthias Ringwald             }
131853860077SMatthias Ringwald             // skip baud change
131974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
132074b323a9SMatthias Ringwald             return;
1321a80162e9SMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
13224696bddbSMatthias Ringwald             // for STLC2500D, baud rate change already happened.
1323fab26ab3SMatthias Ringwald             // for others, baud rate gets changed now
13247224be7eSMatthias Ringwald             if ((hci_stack->manufacturer != COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){
132596b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1326cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate);
1327fab26ab3SMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
13284696bddbSMatthias Ringwald             }
132974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
133074b323a9SMatthias Ringwald             return;
1331a80162e9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
1332528a4a3bSMatthias Ringwald             btstack_run_loop_remove_timer(&hci_stack->timeout);
1333a80162e9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1334a80162e9SMatthias Ringwald             return;
133574b323a9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT:
133674b323a9SMatthias Ringwald             // repeat custom init
133774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
133874b323a9SMatthias Ringwald             return;
133906b9e820SMatthias Ringwald #else
134006b9e820SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
134106b9e820SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS;
134206b9e820SMatthias Ringwald             return ;
134306b9e820SMatthias Ringwald #endif
134406b9e820SMatthias Ringwald 
1345a828a756SMatthias Ringwald         case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS:
1346c6d1dbedSMatthias Ringwald             if (need_baud_change && hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
1347eb3a5314SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM;
1348eb3a5314SMatthias Ringwald                 return;
1349eb3a5314SMatthias Ringwald             }
135053860077SMatthias Ringwald             if (need_addr_change){
135153860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
135253860077SMatthias Ringwald                 return;
135353860077SMatthias Ringwald             }
135453860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
135553860077SMatthias Ringwald             return;
135606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API)
13577224be7eSMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM:
13587224be7eSMatthias Ringwald             if (need_baud_change){
135996b53536SMatthias Ringwald                 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1360cd724cb7SMatthias Ringwald                 log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate);
1361fab26ab3SMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(baud_rate);
13627224be7eSMatthias Ringwald             }
1363eb3a5314SMatthias Ringwald             if (need_addr_change){
1364eb3a5314SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
1365eb3a5314SMatthias Ringwald                 return;
1366eb3a5314SMatthias Ringwald             }
1367eb3a5314SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
1368eb3a5314SMatthias Ringwald             return;
136953860077SMatthias Ringwald         case HCI_INIT_W4_SET_BD_ADDR:
137053860077SMatthias Ringwald             // for STLC2500D, bd addr change only gets active after sending reset command
1371c6d1dbedSMatthias Ringwald             if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
137253860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT;
137353860077SMatthias Ringwald                 return;
137453860077SMatthias Ringwald             }
137553860077SMatthias Ringwald             // skipping st warm boot
137653860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
137753860077SMatthias Ringwald             return;
137853860077SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT:
137953860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_BD_ADDR;
138053860077SMatthias Ringwald             return;
138106b9e820SMatthias Ringwald #endif
138253860077SMatthias Ringwald         case HCI_INIT_W4_READ_BD_ADDR:
138353860077SMatthias Ringwald             // only read buffer size if supported
138453860077SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x01) {
138553860077SMatthias Ringwald                 hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE;
138653860077SMatthias Ringwald                 return;
138753860077SMatthias Ringwald             }
138853860077SMatthias Ringwald             // skipping read buffer size
138953860077SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES;
1390a828a756SMatthias Ringwald             return;
139174b323a9SMatthias Ringwald         case HCI_INIT_W4_SET_EVENT_MASK:
13926155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
13936155b3d3S[email protected]             if (!hci_classic_supported()){
13946155b3d3S[email protected]                 if (hci_le_supported()){
139574b323a9SMatthias Ringwald                     hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command
139674b323a9SMatthias Ringwald                     return;
13976155b3d3S[email protected]                 } else {
13986155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
1399a650ba4dSMatthias Ringwald                     hci_init_done();
140074b323a9SMatthias Ringwald                     return;
14016155b3d3S[email protected]                 }
14026155b3d3S[email protected]             }
140315a95bd5SMatthias Ringwald             if (!gap_ssp_supported()){
14045c363727SMatthias Ringwald                 hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT;
140574b323a9SMatthias Ringwald                 return;
14066155b3d3S[email protected]             }
14076155b3d3S[email protected]             break;
1408a828a756SMatthias Ringwald         case HCI_INIT_W4_LE_READ_BUFFER_SIZE:
1409a828a756SMatthias Ringwald             // skip write le host if not supported (e.g. on LE only EM9301)
1410a828a756SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x02) break;
1411a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_LE_SET_SCAN_PARAMETERS;
1412a828a756SMatthias Ringwald             return;
1413ff00ed1cSMatthias Ringwald         case HCI_INIT_W4_WRITE_LOCAL_NAME:
1414ff00ed1cSMatthias Ringwald             // skip write eir data if no eir data set
1415ff00ed1cSMatthias Ringwald             if (hci_stack->eir_data) break;
1416ff00ed1cSMatthias Ringwald             hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE;
1417ff00ed1cSMatthias Ringwald             return;
1418729ed62eSMatthias Ringwald 
141971c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI
1420729ed62eSMatthias Ringwald         case HCI_INIT_W4_WRITE_SCAN_ENABLE:
14213905afbfSMatthias Ringwald             // skip write synchronous flow control if not supported
14223905afbfSMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x04) break;
14233905afbfSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE;
14243905afbfSMatthias Ringwald             // explicit fall through to reduce repetitions
14253905afbfSMatthias Ringwald 
1426729ed62eSMatthias Ringwald         case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE:
14273905afbfSMatthias Ringwald             // skip write default erroneous data reporting if not supported
14283905afbfSMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x08) break;
14293905afbfSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING;
14303905afbfSMatthias Ringwald             // explicit fall through to reduce repetitions
14313905afbfSMatthias Ringwald 
1432f064e0bbSMatthias Ringwald         case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING:
1433729ed62eSMatthias Ringwald             if (!hci_le_supported()){
1434729ed62eSMatthias Ringwald                 // SKIP LE init for Classic only configuration
1435a650ba4dSMatthias Ringwald                 hci_init_done();
1436729ed62eSMatthias Ringwald                 return;
1437729ed62eSMatthias Ringwald             }
1438729ed62eSMatthias Ringwald             break;
1439729ed62eSMatthias Ringwald #else
144074b323a9SMatthias Ringwald         case HCI_INIT_W4_WRITE_SCAN_ENABLE:
14416155b3d3S[email protected]             if (!hci_le_supported()){
14426155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
1443a650ba4dSMatthias Ringwald                 hci_init_done();
144474b323a9SMatthias Ringwald                 return;
14456155b3d3S[email protected]             }
1446729ed62eSMatthias Ringwald #endif
1447729ed62eSMatthias Ringwald             break;
1448a650ba4dSMatthias Ringwald         // Response to command before init done state -> init done
1449a650ba4dSMatthias Ringwald         case (HCI_INIT_DONE-1):
1450a650ba4dSMatthias Ringwald             hci_init_done();
1451a650ba4dSMatthias Ringwald             return;
1452a650ba4dSMatthias Ringwald 
14536155b3d3S[email protected]         default:
145474b323a9SMatthias Ringwald             break;
14556155b3d3S[email protected]     }
145655975f88SMatthias Ringwald     hci_initializing_next_state();
14576155b3d3S[email protected] }
14586155b3d3S[email protected] 
145916833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1460e76a89eeS[email protected] 
1461e76a89eeS[email protected]     uint16_t event_length = packet[1];
1462e76a89eeS[email protected] 
1463e76a89eeS[email protected]     // assert packet is complete
1464e76a89eeS[email protected]     if (size != event_length + 2){
1465f04a0c31SMatthias Ringwald         log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2);
1466e76a89eeS[email protected]         return;
1467e76a89eeS[email protected]     }
1468e76a89eeS[email protected] 
14691281a47eSmatthias.ringwald     bd_addr_t addr;
147096a45072S[email protected]     bd_addr_type_t addr_type;
14712d00edd4Smatthias.ringwald     uint8_t link_type;
1472fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
14731f7b95a1Smatthias.ringwald     hci_connection_t * conn;
147456cf178bSmatthias.ringwald     int i;
147522909952Smatthias.ringwald 
147635454696SMatthias Ringwald     // warnings
147735454696SMatthias Ringwald     (void) link_type;
147835454696SMatthias Ringwald 
14790e2df43fSMatthias Ringwald     // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet));
14805909f7f2Smatthias.ringwald 
14810e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
148222909952Smatthias.ringwald 
14836772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
14846bef4003SMatthias Ringwald             // get num cmd packets - limit to 1 to reduce complexity
14856bef4003SMatthias Ringwald             hci_stack->num_cmd_packets = packet[2] ? 1 : 0;
14867ec5eeaaSmatthias.ringwald 
14879e263bd7SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
148806b9e820SMatthias Ringwald                 if (packet[5]) break;
14899e263bd7SMatthias Ringwald                 // terminate, name 248 chars
14909e263bd7SMatthias Ringwald                 packet[6+248] = 0;
14919e263bd7SMatthias Ringwald                 log_info("local name: %s", &packet[6]);
14929e263bd7SMatthias Ringwald             }
1493073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){
1494e2edc0c3Smatthias.ringwald                 // from offset 5
1495e2edc0c3Smatthias.ringwald                 // status
14961d279b20Smatthias.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"
1497f8fbdce0SMatthias Ringwald                 hci_stack->acl_data_packet_length = little_endian_read_16(packet, 6);
1498a8b12447S[email protected]                 hci_stack->sco_data_packet_length = packet[8];
1499f8fbdce0SMatthias Ringwald                 hci_stack->acl_packets_total_num  = little_endian_read_16(packet, 9);
1500f8fbdce0SMatthias Ringwald                 hci_stack->sco_packets_total_num  = little_endian_read_16(packet, 11);
1501a8b12447S[email protected] 
15023a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
1503a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
15043a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
15053a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
15068f8108aaSmatthias.ringwald                     }
15071a06f663S[email protected]                     log_info("hci_read_buffer_size: acl used size %u, count %u / sco size %u, count %u",
15081a06f663S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num,
15091a06f663S[email protected]                              hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num);
1510e2edc0c3Smatthias.ringwald                 }
151156cf178bSmatthias.ringwald             }
1512a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
1513073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){
1514f8fbdce0SMatthias Ringwald                 hci_stack->le_data_packets_length = little_endian_read_16(packet, 6);
1515ee303eddS[email protected]                 hci_stack->le_acl_packets_total_num  = packet[8];
15166c26b087S[email protected]                     // determine usable ACL payload size
15176c26b087S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){
15186c26b087S[email protected]                         hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE;
15196c26b087S[email protected]                     }
15209da54300S[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);
152165a46ef3S[email protected]             }
1522073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){
1523f8fbdce0SMatthias Ringwald                 hci_stack->le_whitelist_capacity = little_endian_read_16(packet, 6);
152415d0a15bSMatthias Ringwald                 log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity);
15253b6d4121SMatthias Ringwald             }
152665a46ef3S[email protected] #endif
1527073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) {
1528724d70a2SMatthias Ringwald                 reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1],
1529724d70a2SMatthias Ringwald 				hci_stack->local_bd_addr);
15309da54300S[email protected]                 log_info("Local Address, Status: 0x%02x: Addr: %s",
15313a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
153233373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC
15331624665aSMatthias Ringwald                 if (hci_stack->link_key_db){
15341624665aSMatthias Ringwald                     hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr);
15351624665aSMatthias Ringwald                 }
153633373e40SMatthias Ringwald #endif
1537188981d2Smatthias.ringwald             }
153835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1539073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){
15403a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
1541381fbed8Smatthias.ringwald             }
154235454696SMatthias Ringwald #endif
154335454696SMatthias Ringwald 
154465389bfcS[email protected]             // Note: HCI init checks
1545073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){
15463a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
154765389bfcS[email protected] 
154835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1549a5a23fc2S[email protected]                 // determine usable ACL packet types based on host buffer size and supported features
1550a5a23fc2S[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]);
15518b96126aSMatthias Ringwald                 log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported());
155235454696SMatthias Ringwald #endif
1553f5d8d141S[email protected]                 // Classic/LE
1554f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
1555559e517eS[email protected]             }
1556073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
1557f8fbdce0SMatthias Ringwald                 // hci_stack->hci_version    = little_endian_read_16(packet, 4);
1558f8fbdce0SMatthias Ringwald                 // hci_stack->hci_revision   = little_endian_read_16(packet, 6);
1559f8fbdce0SMatthias Ringwald                 // hci_stack->lmp_version    = little_endian_read_16(packet, 8);
1560f8fbdce0SMatthias Ringwald                 hci_stack->manufacturer   = little_endian_read_16(packet, 10);
1561f8fbdce0SMatthias Ringwald                 // hci_stack->lmp_subversion = little_endian_read_16(packet, 12);
15624696bddbSMatthias Ringwald                 log_info("Manufacturer: 0x%04x", hci_stack->manufacturer);
15634696bddbSMatthias Ringwald             }
1564073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){
1565a828a756SMatthias Ringwald                 hci_stack->local_supported_commands[0] =
15663905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 |  // bit 0 = Octet 14, bit 7
15673905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 |  // bit 1 = Octet 24, bit 6
15683905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 |  // bit 2 = Octet 10, bit 4
15693905afbfSMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08);        // bit 3 = Octet 18, bit 3
15703905afbfSMatthias Ringwald                     log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]);
1571a828a756SMatthias Ringwald             }
1572073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){
15735b9b590fSMatthias Ringwald                 if (packet[5] == 0){
15745b9b590fSMatthias Ringwald                     hci_stack->synchronous_flow_control_enabled = 1;
15755b9b590fSMatthias Ringwald                 }
15765b9b590fSMatthias Ringwald             }
157756cf178bSmatthias.ringwald             break;
157856cf178bSmatthias.ringwald 
15797ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
15806bef4003SMatthias Ringwald             // get num cmd packets - limit to 1 to reduce complexity
15816bef4003SMatthias Ringwald             hci_stack->num_cmd_packets = packet[3] ? 1 : 0;
15827ec5eeaaSmatthias.ringwald             break;
15837ec5eeaaSmatthias.ringwald 
15842e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
15852e440c8aS[email protected]             int offset = 3;
158656cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
1587f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, offset);
15882e440c8aS[email protected]                 offset += 2;
1589f8fbdce0SMatthias Ringwald                 uint16_t num_packets = little_endian_read_16(packet, offset);
15902e440c8aS[email protected]                 offset += 2;
15912e440c8aS[email protected] 
15925061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
159356cf178bSmatthias.ringwald                 if (!conn){
15949da54300S[email protected]                     log_error("hci_number_completed_packet lists unused con handle %u", handle);
159556cf178bSmatthias.ringwald                     continue;
159656cf178bSmatthias.ringwald                 }
159723bed257S[email protected] 
1598e35edcc1S[email protected]                 if (conn->address_type == BD_ADDR_TYPE_SCO){
159935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1600e35edcc1S[email protected]                     if (conn->num_sco_packets_sent >= num_packets){
1601e35edcc1S[email protected]                         conn->num_sco_packets_sent -= num_packets;
1602e35edcc1S[email protected]                     } else {
1603e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more sco slots freed then sent.");
1604e35edcc1S[email protected]                         conn->num_sco_packets_sent = 0;
1605e35edcc1S[email protected]                     }
1606701e3307SMatthias Ringwald                     hci_notify_if_sco_can_send_now();
160735454696SMatthias Ringwald #endif
1608e35edcc1S[email protected]                 } else {
160923bed257S[email protected]                     if (conn->num_acl_packets_sent >= num_packets){
161056cf178bSmatthias.ringwald                         conn->num_acl_packets_sent -= num_packets;
161123bed257S[email protected]                     } else {
1612e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more acl slots freed then sent.");
161323bed257S[email protected]                         conn->num_acl_packets_sent = 0;
161423bed257S[email protected]                     }
1615e35edcc1S[email protected]                 }
16169da54300S[email protected]                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent);
161756cf178bSmatthias.ringwald             }
16186772a24cSmatthias.ringwald             break;
16192e440c8aS[email protected]         }
162035454696SMatthias Ringwald 
162135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
16221f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
1623724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[2], addr);
162437eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
16252d00edd4Smatthias.ringwald             link_type = packet[11];
16269da54300S[email protected]             log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type);
1627e35edcc1S[email protected]             addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO;
16282e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
16291f7b95a1Smatthias.ringwald             if (!conn) {
16305293c072S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, addr_type);
16311f7b95a1Smatthias.ringwald             }
1632ce4c8fabSmatthias.ringwald             if (!conn) {
1633ce4c8fabSmatthias.ringwald                 // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
16343a9fb326S[email protected]                 hci_stack->decline_reason = 0x0d;
1635058e3d6bSMatthias Ringwald                 bd_addr_copy(hci_stack->decline_addr, addr);
1636ce4c8fabSmatthias.ringwald                 break;
1637ce4c8fabSmatthias.ringwald             }
16385cf766e8SMatthias Ringwald             conn->role  = HCI_ROLE_SLAVE;
163932ab9390Smatthias.ringwald             conn->state = RECEIVED_CONNECTION_REQUEST;
1640f3a16b9aSMatthias Ringwald             // store info about eSCO
1641f3a16b9aSMatthias Ringwald             if (link_type == 0x02){
1642f3a16b9aSMatthias Ringwald                 conn->remote_supported_feature_eSCO = 1;
1643f3a16b9aSMatthias Ringwald             }
164432ab9390Smatthias.ringwald             hci_run();
16451f7b95a1Smatthias.ringwald             break;
16461f7b95a1Smatthias.ringwald 
16476772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1648fe1ed1b8Smatthias.ringwald             // Connection management
1649724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], addr);
16509da54300S[email protected]             log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
165196a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
16522e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
1653fe1ed1b8Smatthias.ringwald             if (conn) {
1654b448a0e7Smatthias.ringwald                 if (!packet[2]){
1655c8e4258aSmatthias.ringwald                     conn->state = OPEN;
1656f8fbdce0SMatthias Ringwald                     conn->con_handle = little_endian_read_16(packet, 3);
1657ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
1658ee091cf1Smatthias.ringwald 
1659c785ef68Smatthias.ringwald                     // restart timer
1660528a4a3bSMatthias Ringwald                     btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1661528a4a3bSMatthias Ringwald                     btstack_run_loop_add_timer(&conn->timeout);
1662c785ef68Smatthias.ringwald 
16639da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
166443bfb1bdSmatthias.ringwald 
166543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
1666b448a0e7Smatthias.ringwald                 } else {
16671bd5283dS[email protected]                     int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED;
16681bd5283dS[email protected]                     uint8_t status = packet[2];
16691bd5283dS[email protected]                     bd_addr_t bd_address;
16701bd5283dS[email protected]                     memcpy(&bd_address, conn->address, 6);
1671ad83dc6aS[email protected] 
1672b448a0e7Smatthias.ringwald                     // connection failed, remove entry
1673665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
1674a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
1675c12e46e7Smatthias.ringwald 
16761bd5283dS[email protected]                     // notify client if dedicated bonding
16771bd5283dS[email protected]                     if (notify_dedicated_bonding_failed){
16781bd5283dS[email protected]                         log_info("hci notify_dedicated_bonding_failed");
16791bd5283dS[email protected]                         hci_emit_dedicated_bonding_result(bd_address, status);
16801bd5283dS[email protected]                     }
16811bd5283dS[email protected] 
1682c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
1683c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
168415a95bd5SMatthias Ringwald                         gap_drop_link_key_for_bd_addr(addr);
1685c12e46e7Smatthias.ringwald                     }
1686fe1ed1b8Smatthias.ringwald                 }
1687fe1ed1b8Smatthias.ringwald             }
16886772a24cSmatthias.ringwald             break;
1689fe1ed1b8Smatthias.ringwald 
169044d0e3d5S[email protected]         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:
1691724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], addr);
169244d0e3d5S[email protected]             log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
16931a06f663S[email protected]             if (packet[2]){
169444d0e3d5S[email protected]                 // connection failed
169544d0e3d5S[email protected]                 break;
169644d0e3d5S[email protected]             }
16972e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1698e35edcc1S[email protected]             if (!conn) {
1699e35edcc1S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1700e35edcc1S[email protected]             }
1701e35edcc1S[email protected]             if (!conn) {
1702e35edcc1S[email protected]                 break;
1703e35edcc1S[email protected]             }
17041a06f663S[email protected]             conn->state = OPEN;
1705f8fbdce0SMatthias Ringwald             conn->con_handle = little_endian_read_16(packet, 3);
170644d0e3d5S[email protected]             break;
170744d0e3d5S[email protected] 
1708afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1709f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1710afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
1711afd4e962S[email protected]             if (!conn) break;
1712afd4e962S[email protected]             if (!packet[2]){
1713afd4e962S[email protected]                 uint8_t * features = &packet[5];
1714afd4e962S[email protected]                 if (features[6] & (1 << 3)){
1715afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
1716afd4e962S[email protected]                 }
171798a2fd1cSMatthias Ringwald                 if (features[3] & (1<<7)){
171898a2fd1cSMatthias Ringwald                     conn->remote_supported_feature_eSCO = 1;
171998a2fd1cSMatthias Ringwald                 }
1720afd4e962S[email protected]             }
1721afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
172298a2fd1cSMatthias Ringwald             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO);
1723ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1724ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1725ad83dc6aS[email protected]             }
1726afd4e962S[email protected]             break;
1727afd4e962S[email protected] 
17287fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
17299da54300S[email protected]             log_info("HCI_EVENT_LINK_KEY_REQUEST");
17307fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
173174d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
1732a98592bcSMatthias Ringwald             if (hci_stack->bondable && !hci_stack->link_key_db) break;
173332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
173464472d52Smatthias.ringwald             hci_run();
1735d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
173629d53098Smatthias.ringwald             return;
17377fde4af9Smatthias.ringwald 
17389ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
1739724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[2], addr);
17402e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
17419ab95c90S[email protected]             if (!conn) break;
17429ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
17437bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
17449ab95c90S[email protected]             // Change Connection Encryption keeps link key type
17459ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
17469ab95c90S[email protected]                 conn->link_key_type = link_key_type;
17479ab95c90S[email protected]             }
174855597469SMatthias Ringwald             gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type);
174929d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
17507fde4af9Smatthias.ringwald             break;
17519ab95c90S[email protected]         }
17527fde4af9Smatthias.ringwald 
17537fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
17546724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
17554c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
17563a9fb326S[email protected]             if (!hci_stack->bondable){
1757899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
1758f8fb5f6eS[email protected]                 hci_run();
1759f8fb5f6eS[email protected]                 return;
17604c57c146S[email protected]             }
1761d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
1762a98592bcSMatthias Ringwald             if (!hci_stack->link_key_db) break;
1763a6ef64baSMilanka Ringwald             hci_event_pin_code_request_get_bd_addr(packet, addr);
1764a98592bcSMatthias Ringwald             hci_stack->link_key_db->delete_link_key(addr);
17657fde4af9Smatthias.ringwald             break;
17667fde4af9Smatthias.ringwald 
17671d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
17681d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
1769dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
1770dbe1a790S[email protected]             break;
1771dbe1a790S[email protected] 
1772dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
17736724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
17743a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1775dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
1776dbe1a790S[email protected]             break;
1777dbe1a790S[email protected] 
1778dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
17796724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
17803a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1781dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
17821d6b20aeS[email protected]             break;
178335454696SMatthias Ringwald #endif
17841d6b20aeS[email protected] 
1785f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1786f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1787f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1788f0944df2S[email protected]             if (!conn) break;
1789ad83dc6aS[email protected]             if (packet[2] == 0) {
1790f0944df2S[email protected]                 if (packet[5]){
1791f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1792f0944df2S[email protected]                 } else {
1793536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1794f0944df2S[email protected]                 }
1795ad83dc6aS[email protected]             }
179635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1797a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
179835454696SMatthias Ringwald #endif
1799f0944df2S[email protected]             break;
1800f0944df2S[email protected] 
180135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
18021eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
1803f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
18041eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
18051eb2563eS[email protected]             if (!conn) break;
1806ad83dc6aS[email protected] 
1807ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1808ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1809ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1810ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
18111bd5283dS[email protected]                 conn->bonding_status = packet[2];
1812ad83dc6aS[email protected]                 break;
1813ad83dc6aS[email protected]             }
1814ad83dc6aS[email protected] 
1815b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
18161eb2563eS[email protected]                 // link key sufficient for requested security
18171eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1818ad83dc6aS[email protected]                 break;
1819ad83dc6aS[email protected]             }
18201eb2563eS[email protected]             // not enough
18211eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
18221eb2563eS[email protected]             break;
182335454696SMatthias Ringwald #endif
182434d2123cS[email protected] 
182586805605S[email protected]         // HCI_EVENT_DISCONNECTION_COMPLETE
1826ccda6e14S[email protected]         // has been split, to first notify stack before shutting connection down
1827ccda6e14S[email protected]         // see end of function, too.
1828a4f30ec0S[email protected]         case HCI_EVENT_DISCONNECTION_COMPLETE:
1829a4f30ec0S[email protected]             if (packet[2]) break;   // status != 0
1830f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1831c6a37cfdSMatthias Ringwald             // drop outgoing ACL fragments if it is for closed connection
1832c6a37cfdSMatthias Ringwald             if (hci_stack->acl_fragmentation_total_size > 0) {
1833c6a37cfdSMatthias Ringwald                 if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){
1834c6a37cfdSMatthias Ringwald                     log_info("hci: drop fragmented ACL data for closed connection");
1835c6a37cfdSMatthias Ringwald                      hci_stack->acl_fragmentation_total_size = 0;
1836c6a37cfdSMatthias Ringwald                      hci_stack->acl_fragmentation_pos = 0;
1837c6a37cfdSMatthias Ringwald                 }
1838c6a37cfdSMatthias Ringwald             }
183935454696SMatthias Ringwald 
18409a2e4658SMatthias Ringwald             // re-enable advertisements for le connections if active
1841c6a37cfdSMatthias Ringwald             conn = hci_connection_for_handle(handle);
1842c6a37cfdSMatthias Ringwald             if (!conn) break;
184335454696SMatthias Ringwald #ifdef ENABLE_BLE
18449a2e4658SMatthias Ringwald             if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){
18459a2e4658SMatthias Ringwald                 hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
18469a2e4658SMatthias Ringwald             }
184735454696SMatthias Ringwald #endif
1848ccda6e14S[email protected]             conn->state = RECEIVED_DISCONNECTION_COMPLETE;
1849ccda6e14S[email protected]             break;
18506772a24cSmatthias.ringwald 
1851c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
1852313e5f9cSMatthias Ringwald             log_error("Hardware Error: 0x%02x", packet[2]);
1853d23838ecSMatthias Ringwald             if (hci_stack->hardware_error_callback){
1854c2e1fa60SMatthias Ringwald                 (*hci_stack->hardware_error_callback)(packet[2]);
18557586ee35S[email protected]             } else {
18567586ee35S[email protected]                 // if no special requests, just reboot stack
18577586ee35S[email protected]                 hci_power_control_off();
18587586ee35S[email protected]                 hci_power_control_on();
1859c68bdf90Smatthias.ringwald             }
1860c68bdf90Smatthias.ringwald             break;
1861c68bdf90Smatthias.ringwald 
18625cf766e8SMatthias Ringwald         case HCI_EVENT_ROLE_CHANGE:
18635cf766e8SMatthias Ringwald             if (packet[2]) break;   // status != 0
1864f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
18655cf766e8SMatthias Ringwald             conn = hci_connection_for_handle(handle);
18665cf766e8SMatthias Ringwald             if (!conn) break;       // no conn
18675cf766e8SMatthias Ringwald             conn->role = packet[9];
18685cf766e8SMatthias Ringwald             break;
18695cf766e8SMatthias Ringwald 
187063fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
1871d051460cS[email protected]             // release packet buffer only for asynchronous transport and if there are not further fragements
18724fa24b5fS[email protected]             if (hci_transport_synchronous()) {
187363fa3374SMatthias Ringwald                 log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT");
18744fa24b5fS[email protected]                 return; // instead of break: to avoid re-entering hci_run()
18754fa24b5fS[email protected]             }
1876d051460cS[email protected]             if (hci_stack->acl_fragmentation_total_size) break;
1877d051460cS[email protected]             hci_release_packet_buffer();
1878701e3307SMatthias Ringwald 
187963fa3374SMatthias Ringwald             // L2CAP receives this event via the hci_emit_event below
188063fa3374SMatthias Ringwald 
188135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
188263fa3374SMatthias Ringwald             // For SCO, we do the can_send_now_check here
1883701e3307SMatthias Ringwald             hci_notify_if_sco_can_send_now();
188435454696SMatthias Ringwald #endif
18856b4af23dS[email protected]             break;
18866b4af23dS[email protected] 
188735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
188863fa3374SMatthias Ringwald         case HCI_EVENT_SCO_CAN_SEND_NOW:
188963fa3374SMatthias Ringwald             // For SCO, we do the can_send_now_check here
189063fa3374SMatthias Ringwald             hci_notify_if_sco_can_send_now();
189163fa3374SMatthias Ringwald             return;
189235454696SMatthias Ringwald #endif
189363fa3374SMatthias Ringwald 
1894a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
18955909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
18965909f7f2Smatthias.ringwald             switch (packet[2]){
189757c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
18984f4e0224SMatthias Ringwald                     // log_info("advertising report received");
18997bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
190057c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
19017bdc6798S[email protected]                     break;
19025909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
19035909f7f2Smatthias.ringwald                     // Connection management
1904724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[8], addr);
19057bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
19069da54300S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr));
19072e77e513S[email protected]                     conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
190842ff5ba1SMatthias Ringwald                     // if auto-connect, remove from whitelist in both roles
190942ff5ba1SMatthias Ringwald                     if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){
191042ff5ba1SMatthias Ringwald                         hci_remove_from_whitelist(addr_type, addr);
191142ff5ba1SMatthias Ringwald                     }
191242ff5ba1SMatthias Ringwald                     // handle error: error is reported only to the initiator -> outgoing connection
19135909f7f2Smatthias.ringwald                     if (packet[3]){
191442ff5ba1SMatthias Ringwald                         // outgoing connection establishment is done
191542ff5ba1SMatthias Ringwald                         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
191642ff5ba1SMatthias Ringwald                         // remove entry
19175909f7f2Smatthias.ringwald                         if (conn){
1918665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
19195909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
19205909f7f2Smatthias.ringwald                         }
19215909f7f2Smatthias.ringwald                         break;
19225909f7f2Smatthias.ringwald                     }
192342ff5ba1SMatthias Ringwald                     // on success, both hosts receive connection complete event
19245cf766e8SMatthias Ringwald                     if (packet[6] == HCI_ROLE_MASTER){
192542ff5ba1SMatthias Ringwald                         // if we're master, it was an outgoing connection and we're done with it
192642ff5ba1SMatthias Ringwald                         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
192742ff5ba1SMatthias Ringwald                     } else {
192842ff5ba1SMatthias Ringwald                         // if we're slave, it was an incoming connection, advertisements have stopped
1929171293d3SMatthias Ringwald                         hci_stack->le_advertisements_active = 0;
19305106e6dcSMatthias Ringwald                         // try to re-enable them
19315106e6dcSMatthias Ringwald                         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
193242ff5ba1SMatthias Ringwald                     }
1933171293d3SMatthias Ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
193442ff5ba1SMatthias Ringwald                     if (!conn){
193596a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
19365909f7f2Smatthias.ringwald                     }
193742ff5ba1SMatthias Ringwald                     // no memory, sorry.
19385909f7f2Smatthias.ringwald                     if (!conn){
19395909f7f2Smatthias.ringwald                         break;
19405909f7f2Smatthias.ringwald                     }
19415909f7f2Smatthias.ringwald 
19425909f7f2Smatthias.ringwald                     conn->state = OPEN;
19435cf766e8SMatthias Ringwald                     conn->role  = packet[6];
1944f8fbdce0SMatthias Ringwald                     conn->con_handle = little_endian_read_16(packet, 4);
19455909f7f2Smatthias.ringwald 
19465909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
19475909f7f2Smatthias.ringwald 
19485909f7f2Smatthias.ringwald                     // restart timer
1949528a4a3bSMatthias Ringwald                     // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1950528a4a3bSMatthias Ringwald                     // btstack_run_loop_add_timer(&conn->timeout);
19515909f7f2Smatthias.ringwald 
19529da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
19535909f7f2Smatthias.ringwald 
19545909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
19555909f7f2Smatthias.ringwald                     break;
19565909f7f2Smatthias.ringwald 
1957f8fbdce0SMatthias Ringwald             // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]);
195865a46ef3S[email protected] 
19595909f7f2Smatthias.ringwald                 default:
19605909f7f2Smatthias.ringwald                     break;
19615909f7f2Smatthias.ringwald             }
19625909f7f2Smatthias.ringwald             break;
19635909f7f2Smatthias.ringwald #endif
19646772a24cSmatthias.ringwald         default:
19656772a24cSmatthias.ringwald             break;
1966fe1ed1b8Smatthias.ringwald     }
1967fe1ed1b8Smatthias.ringwald 
19683429f56bSmatthias.ringwald     // handle BT initialization
19693a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
19706155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1971c9af4d3fS[email protected]     }
197222909952Smatthias.ringwald 
197389db417bSmatthias.ringwald     // help with BT sleep
19743a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
197574b323a9SMatthias Ringwald         && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE
1976073bd0faSMatthias Ringwald         && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){
197755975f88SMatthias Ringwald         hci_initializing_next_state();
197889db417bSmatthias.ringwald     }
197989db417bSmatthias.ringwald 
198086805605S[email protected]     // notify upper stack
1981d6b06661SMatthias Ringwald 	hci_emit_event(packet, size, 0);   // don't dump, already happened in packet handler
198294ab26f8Smatthias.ringwald 
198386805605S[email protected]     // moved here to give upper stack a chance to close down everything with hci_connection_t intact
19840e2df43fSMatthias Ringwald     if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){
198586805605S[email protected]         if (!packet[2]){
1986f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
198705ae8de3SMatthias Ringwald             hci_connection_t * aConn = hci_connection_for_handle(handle);
198805ae8de3SMatthias Ringwald             if (aConn) {
198905ae8de3SMatthias Ringwald                 uint8_t status = aConn->bonding_status;
199005ae8de3SMatthias Ringwald                 uint16_t flags = aConn->bonding_flags;
199186805605S[email protected]                 bd_addr_t bd_address;
199205ae8de3SMatthias Ringwald                 memcpy(&bd_address, aConn->address, 6);
199305ae8de3SMatthias Ringwald                 hci_shutdown_connection(aConn);
1994bb820044S[email protected]                 // connection struct is gone, don't access anymore
1995bb820044S[email protected]                 if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){
199686805605S[email protected]                     hci_emit_dedicated_bonding_result(bd_address, status);
199786805605S[email protected]                 }
199886805605S[email protected]             }
199986805605S[email protected]         }
200086805605S[email protected]     }
200186805605S[email protected] 
200294ab26f8Smatthias.ringwald 	// execute main loop
200394ab26f8Smatthias.ringwald 	hci_run();
200416833f0aSmatthias.ringwald }
200516833f0aSmatthias.ringwald 
200635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2007c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){
20088abbe8b5SMatthias Ringwald     if (!hci_stack->sco_packet_handler) return;
20093d50b4baSMatthias Ringwald     hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size);
2010c91d150bS[email protected] }
201135454696SMatthias Ringwald #endif
2012c91d150bS[email protected] 
20130a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
20145bb5bc3eS[email protected]     hci_dump_packet(packet_type, 1, packet, size);
201510e830c9Smatthias.ringwald     switch (packet_type) {
201610e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
201710e830c9Smatthias.ringwald             event_handler(packet, size);
201810e830c9Smatthias.ringwald             break;
201910e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
202010e830c9Smatthias.ringwald             acl_handler(packet, size);
202110e830c9Smatthias.ringwald             break;
202235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2023c91d150bS[email protected]         case HCI_SCO_DATA_PACKET:
2024c91d150bS[email protected]             sco_handler(packet, size);
2025202c8a4cSMatthias Ringwald             break;
202635454696SMatthias Ringwald #endif
202710e830c9Smatthias.ringwald         default:
202810e830c9Smatthias.ringwald             break;
202910e830c9Smatthias.ringwald     }
203010e830c9Smatthias.ringwald }
203110e830c9Smatthias.ringwald 
2032d6b06661SMatthias Ringwald /**
2033d6b06661SMatthias Ringwald  * @brief Add event packet handler.
2034d6b06661SMatthias Ringwald  */
2035d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
2036d6b06661SMatthias Ringwald     btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler);
2037d6b06661SMatthias Ringwald }
2038d6b06661SMatthias Ringwald 
2039d6b06661SMatthias Ringwald 
2040fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
20413d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){
2042fb37a842SMatthias Ringwald     hci_stack->acl_packet_handler = handler;
204316833f0aSmatthias.ringwald }
204416833f0aSmatthias.ringwald 
204535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
20468abbe8b5SMatthias Ringwald /**
20478abbe8b5SMatthias Ringwald  * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles.
20488abbe8b5SMatthias Ringwald  */
20493d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){
20508abbe8b5SMatthias Ringwald     hci_stack->sco_packet_handler = handler;
20518abbe8b5SMatthias Ringwald }
205235454696SMatthias Ringwald #endif
20538abbe8b5SMatthias Ringwald 
205471de195eSMatthias Ringwald static void hci_state_reset(void){
2055595bdbfbS[email protected]     // no connections yet
2056595bdbfbS[email protected]     hci_stack->connections = NULL;
205774308b23Smatthias.ringwald 
205874308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
205974308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
206074308b23Smatthias.ringwald     // hci_stack->connectable = 0;
206174308b23Smatthias.ringwald     // hci_stack->bondable = 1;
2062595bdbfbS[email protected] 
206344935e40S[email protected]     // buffer is free
206444935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
206544935e40S[email protected] 
2066595bdbfbS[email protected]     // no pending cmds
2067595bdbfbS[email protected]     hci_stack->decline_reason = 0;
2068595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
2069595bdbfbS[email protected] 
2070595bdbfbS[email protected]     // LE
2071595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
207252b551c3SMatthias Ringwald     hci_stack->le_advertisements_random_address_set = 0;
2073595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
2074595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
2075e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
2076b04dfa37SMatthias Ringwald     hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
2077e83201bcSMatthias Ringwald     hci_stack->le_whitelist = 0;
2078e83201bcSMatthias Ringwald     hci_stack->le_whitelist_capacity = 0;
2079e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_interval_min =          6;
2080e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_interval_max =       3200;
2081e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_latency_min =           0;
2082e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_conn_latency_max =         500;
2083e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_supervision_timeout_min =   10;
2084e21192abSMatthias Ringwald     hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200;
2085595bdbfbS[email protected] }
2086595bdbfbS[email protected] 
208735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
20882d5e09d6SMatthias Ringwald /**
20892d5e09d6SMatthias Ringwald  * @brief Configure Bluetooth hardware control. Has to be called before power on.
20902d5e09d6SMatthias Ringwald  */
20912d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){
20922d5e09d6SMatthias Ringwald     // store and open remote device db
20932d5e09d6SMatthias Ringwald     hci_stack->link_key_db = link_key_db;
20942d5e09d6SMatthias Ringwald     if (hci_stack->link_key_db) {
20952d5e09d6SMatthias Ringwald         hci_stack->link_key_db->open();
20962d5e09d6SMatthias Ringwald     }
20972d5e09d6SMatthias Ringwald }
209835454696SMatthias Ringwald #endif
20992d5e09d6SMatthias Ringwald 
21002d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){
2101475c8125Smatthias.ringwald 
21023a9fb326S[email protected] #ifdef HAVE_MALLOC
21033a9fb326S[email protected]     if (!hci_stack) {
21043a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
21053a9fb326S[email protected]     }
21063a9fb326S[email protected] #else
21073a9fb326S[email protected]     hci_stack = &hci_stack_static;
21083a9fb326S[email protected] #endif
210966fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
21103a9fb326S[email protected] 
2111475c8125Smatthias.ringwald     // reference to use transport layer implementation
21123a9fb326S[email protected]     hci_stack->hci_transport = transport;
2113475c8125Smatthias.ringwald 
211411e23e5fSmatthias.ringwald     // reference to used config
21153a9fb326S[email protected]     hci_stack->config = config;
211611e23e5fSmatthias.ringwald 
211726a9b6daSMatthias Ringwald     // setup pointer for outgoing packet buffer
211826a9b6daSMatthias Ringwald     hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE];
211926a9b6daSMatthias Ringwald 
21208fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
21213a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
21228fcba05dSmatthias.ringwald 
212316833f0aSmatthias.ringwald     // register packet handlers with transport
212410e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
2125f5454fc6Smatthias.ringwald 
21263a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
2127e2386ba1S[email protected] 
2128e2386ba1S[email protected]     // class of device
21293a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
2130a45d6b9fS[email protected] 
2131f20168b8Smatthias.ringwald     // bondable by default
2132f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
2133f20168b8Smatthias.ringwald 
213463048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
21353a9fb326S[email protected]     hci_stack->ssp_enable = 1;
21363a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
21373a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
21383a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
213969a97523S[email protected] 
2140d950d659SMatthias Ringwald     // voice setting - signed 8 bit pcm data with CVSD over the air
2141d950d659SMatthias Ringwald     hci_stack->sco_voice_setting = 0x40;
2142d950d659SMatthias Ringwald 
2143595bdbfbS[email protected]     hci_state_reset();
2144475c8125Smatthias.ringwald }
2145475c8125Smatthias.ringwald 
21463fb36a29SMatthias Ringwald /**
21473fb36a29SMatthias Ringwald  * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information
21483fb36a29SMatthias Ringwald  */
21493fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){
21503fb36a29SMatthias Ringwald     hci_stack->chipset = chipset_driver;
21513fb36a29SMatthias Ringwald 
21523fb36a29SMatthias Ringwald     // reset chipset driver - init is also called on power_up
21533fb36a29SMatthias Ringwald     if (hci_stack->chipset && hci_stack->chipset->init){
21543fb36a29SMatthias Ringwald         hci_stack->chipset->init(hci_stack->config);
21553fb36a29SMatthias Ringwald     }
21563fb36a29SMatthias Ringwald }
21573fb36a29SMatthias Ringwald 
2158fb55bd0aSMatthias Ringwald /**
2159d0b87befSMatthias Ringwald  * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on.
2160fb55bd0aSMatthias Ringwald  */
2161fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){
2162fb55bd0aSMatthias Ringwald     // references to used control implementation
2163fb55bd0aSMatthias Ringwald     hci_stack->control = hardware_control;
2164d0b87befSMatthias Ringwald     // init with transport config
2165d0b87befSMatthias Ringwald     hardware_control->init(hci_stack->config);
2166fb55bd0aSMatthias Ringwald }
2167fb55bd0aSMatthias Ringwald 
216871de195eSMatthias Ringwald void hci_close(void){
2169404843c1Smatthias.ringwald     // close remote device db
2170a98592bcSMatthias Ringwald     if (hci_stack->link_key_db) {
2171a98592bcSMatthias Ringwald         hci_stack->link_key_db->close();
2172404843c1Smatthias.ringwald     }
21737224be7eSMatthias Ringwald 
21747224be7eSMatthias Ringwald     btstack_linked_list_iterator_t lit;
21757224be7eSMatthias Ringwald     btstack_linked_list_iterator_init(&lit, &hci_stack->connections);
21767224be7eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&lit)){
21777224be7eSMatthias Ringwald         // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection
21787224be7eSMatthias Ringwald         hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit);
21797224be7eSMatthias Ringwald         hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host
21807224be7eSMatthias Ringwald         hci_shutdown_connection(connection);
2181f5454fc6Smatthias.ringwald     }
21827224be7eSMatthias Ringwald 
2183f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
21843a9fb326S[email protected] 
21853a9fb326S[email protected] #ifdef HAVE_MALLOC
21863a9fb326S[email protected]     free(hci_stack);
21873a9fb326S[email protected] #endif
21883a9fb326S[email protected]     hci_stack = NULL;
2189404843c1Smatthias.ringwald }
2190404843c1Smatthias.ringwald 
219135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
219260b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){
21939e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
21949e61646fS[email protected] }
219535454696SMatthias Ringwald #endif
21969e61646fS[email protected] 
2197f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h
2198f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){
2199f456b2d0S[email protected]     memcpy(hci_stack->custom_bd_addr, addr, 6);
2200f456b2d0S[email protected]     hci_stack->custom_bd_addr_set = 1;
2201f456b2d0S[email protected] }
2202f456b2d0S[email protected] 
220371de195eSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){
220466fb9560S[email protected]     disable_l2cap_timeouts = 1;
220566fb9560S[email protected] }
22068d213e1aSmatthias.ringwald // State-Module-Driver overview
22078d213e1aSmatthias.ringwald // state                    module  low-level
22088d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
22098d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
22108d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
22118d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
2212d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
2213d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
2214c7e0c5f6Smatthias.ringwald 
221540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
22167301ad89Smatthias.ringwald 
2217038bc64cSmatthias.ringwald     // power on
2218f9a30166Smatthias.ringwald     int err = 0;
22193a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
2220d0b87befSMatthias Ringwald         err = (*hci_stack->control->on)();
2221f9a30166Smatthias.ringwald     }
2222038bc64cSmatthias.ringwald     if (err){
22239da54300S[email protected]         log_error( "POWER_ON failed");
2224038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
2225038bc64cSmatthias.ringwald         return err;
2226038bc64cSmatthias.ringwald     }
2227038bc64cSmatthias.ringwald 
222824b3c629SMatthias Ringwald     // int chipset driver
22293fb36a29SMatthias Ringwald     if (hci_stack->chipset && hci_stack->chipset->init){
22303fb36a29SMatthias Ringwald         hci_stack->chipset->init(hci_stack->config);
22313fb36a29SMatthias Ringwald     }
22323fb36a29SMatthias Ringwald 
223324b3c629SMatthias Ringwald     // init transport
223424b3c629SMatthias Ringwald     if (hci_stack->hci_transport->init){
223524b3c629SMatthias Ringwald         hci_stack->hci_transport->init(hci_stack->config);
223624b3c629SMatthias Ringwald     }
223724b3c629SMatthias Ringwald 
223824b3c629SMatthias Ringwald     // open transport
223924b3c629SMatthias Ringwald     err = hci_stack->hci_transport->open();
2240038bc64cSmatthias.ringwald     if (err){
22419da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
22423a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
2243d0b87befSMatthias Ringwald             (*hci_stack->control->off)();
2244f9a30166Smatthias.ringwald         }
2245038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
2246038bc64cSmatthias.ringwald         return err;
2247038bc64cSmatthias.ringwald     }
22488d213e1aSmatthias.ringwald     return 0;
22498d213e1aSmatthias.ringwald }
2250038bc64cSmatthias.ringwald 
225140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
22528d213e1aSmatthias.ringwald 
22539da54300S[email protected]     log_info("hci_power_control_off");
22549418f9c9Smatthias.ringwald 
22558d213e1aSmatthias.ringwald     // close low-level device
225624b3c629SMatthias Ringwald     hci_stack->hci_transport->close();
22578d213e1aSmatthias.ringwald 
22589da54300S[email protected]     log_info("hci_power_control_off - hci_transport closed");
22599418f9c9Smatthias.ringwald 
22608d213e1aSmatthias.ringwald     // power off
22613a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
2262d0b87befSMatthias Ringwald         (*hci_stack->control->off)();
22638d213e1aSmatthias.ringwald     }
22649418f9c9Smatthias.ringwald 
22659da54300S[email protected]     log_info("hci_power_control_off - control closed");
22669418f9c9Smatthias.ringwald 
22673a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
226872ea5239Smatthias.ringwald }
226972ea5239Smatthias.ringwald 
227040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
227172ea5239Smatthias.ringwald 
22729da54300S[email protected]     log_info("hci_power_control_sleep");
22733144bce4Smatthias.ringwald 
2274b429b9b7Smatthias.ringwald #if 0
2275b429b9b7Smatthias.ringwald     // don't close serial port during sleep
2276b429b9b7Smatthias.ringwald 
227772ea5239Smatthias.ringwald     // close low-level device
22783a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
2279b429b9b7Smatthias.ringwald #endif
228072ea5239Smatthias.ringwald 
228172ea5239Smatthias.ringwald     // sleep mode
22823a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
2283d0b87befSMatthias Ringwald         (*hci_stack->control->sleep)();
228472ea5239Smatthias.ringwald     }
2285b429b9b7Smatthias.ringwald 
22863a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
22878d213e1aSmatthias.ringwald }
22888d213e1aSmatthias.ringwald 
228940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
2290b429b9b7Smatthias.ringwald 
22919da54300S[email protected]     log_info("hci_power_control_wake");
2292b429b9b7Smatthias.ringwald 
2293b429b9b7Smatthias.ringwald     // wake on
22943a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
2295d0b87befSMatthias Ringwald         (*hci_stack->control->wake)();
2296b429b9b7Smatthias.ringwald     }
2297b429b9b7Smatthias.ringwald 
2298b429b9b7Smatthias.ringwald #if 0
2299b429b9b7Smatthias.ringwald     // open low-level device
23003a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
2301b429b9b7Smatthias.ringwald     if (err){
23029da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
23033a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
2304d0b87befSMatthias Ringwald             (*hci_stack->control->off)();
2305b429b9b7Smatthias.ringwald         }
2306b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
2307b429b9b7Smatthias.ringwald         return err;
2308b429b9b7Smatthias.ringwald     }
2309b429b9b7Smatthias.ringwald #endif
2310b429b9b7Smatthias.ringwald 
2311b429b9b7Smatthias.ringwald     return 0;
2312b429b9b7Smatthias.ringwald }
2313b429b9b7Smatthias.ringwald 
231444935e40S[email protected] static void hci_power_transition_to_initializing(void){
231544935e40S[email protected]     // set up state machine
231644935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
231744935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
231844935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
23195c363727SMatthias Ringwald     hci_stack->substate = HCI_INIT_SEND_RESET;
232044935e40S[email protected] }
2321b429b9b7Smatthias.ringwald 
23228d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
2323d661ed19Smatthias.ringwald 
2324f04a0c31SMatthias Ringwald     log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state);
2325d661ed19Smatthias.ringwald 
23268d213e1aSmatthias.ringwald     int err = 0;
23273a9fb326S[email protected]     switch (hci_stack->state){
23288d213e1aSmatthias.ringwald 
23298d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
23308d213e1aSmatthias.ringwald             switch (power_mode){
23318d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
23328d213e1aSmatthias.ringwald                     err = hci_power_control_on();
233397b61c7bS[email protected]                     if (err) {
2334f04a0c31SMatthias Ringwald                         log_error("hci_power_control_on() error %d", err);
233597b61c7bS[email protected]                         return err;
233697b61c7bS[email protected]                     }
233744935e40S[email protected]                     hci_power_transition_to_initializing();
23388d213e1aSmatthias.ringwald                     break;
23398d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
23408d213e1aSmatthias.ringwald                     // do nothing
23418d213e1aSmatthias.ringwald                     break;
23428d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2343b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
23448d213e1aSmatthias.ringwald                     break;
23458d213e1aSmatthias.ringwald             }
23468d213e1aSmatthias.ringwald             break;
23477301ad89Smatthias.ringwald 
23488d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
23498d213e1aSmatthias.ringwald             switch (power_mode){
23508d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
23518d213e1aSmatthias.ringwald                     // do nothing
23528d213e1aSmatthias.ringwald                     break;
23538d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
23548d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
23558d213e1aSmatthias.ringwald                     hci_power_control_off();
23568d213e1aSmatthias.ringwald                     break;
23578d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2358b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
235972ea5239Smatthias.ringwald                     hci_power_control_sleep();
23608d213e1aSmatthias.ringwald                     break;
23618d213e1aSmatthias.ringwald             }
23628d213e1aSmatthias.ringwald             break;
23637301ad89Smatthias.ringwald 
23648d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
23658d213e1aSmatthias.ringwald             switch (power_mode){
23668d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
23678d213e1aSmatthias.ringwald                     // do nothing
23688d213e1aSmatthias.ringwald                     break;
23698d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
2370c7e0c5f6Smatthias.ringwald                     // see hci_run
23713a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
23728d213e1aSmatthias.ringwald                     break;
23738d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2374b546ac54Smatthias.ringwald                     // see hci_run
23753a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
237674b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
23778d213e1aSmatthias.ringwald                     break;
23788d213e1aSmatthias.ringwald             }
23798d213e1aSmatthias.ringwald             break;
23807301ad89Smatthias.ringwald 
23818d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
23828d213e1aSmatthias.ringwald             switch (power_mode){
23838d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
238444935e40S[email protected]                     hci_power_transition_to_initializing();
23858d213e1aSmatthias.ringwald                     break;
23868d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
23878d213e1aSmatthias.ringwald                     // do nothing
23888d213e1aSmatthias.ringwald                     break;
23898d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2390b546ac54Smatthias.ringwald                     // see hci_run
23913a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
239274b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
23938d213e1aSmatthias.ringwald                     break;
23948d213e1aSmatthias.ringwald             }
23958d213e1aSmatthias.ringwald             break;
23968d213e1aSmatthias.ringwald 
23978d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
23988d213e1aSmatthias.ringwald             switch (power_mode){
23998d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
240028171530Smatthias.ringwald 
2401423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
240228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
2403d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
24043a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
240574b323a9SMatthias Ringwald                         hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE;   // init after sleep
240628171530Smatthias.ringwald                         break;
240728171530Smatthias.ringwald                     }
240828171530Smatthias.ringwald #endif
240944935e40S[email protected]                     hci_power_transition_to_initializing();
24108d213e1aSmatthias.ringwald                     break;
24118d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
2412b546ac54Smatthias.ringwald                     // see hci_run
24133a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
24148d213e1aSmatthias.ringwald                     break;
24158d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2416b546ac54Smatthias.ringwald                     // do nothing
24178d213e1aSmatthias.ringwald                     break;
24188d213e1aSmatthias.ringwald             }
24198d213e1aSmatthias.ringwald             break;
24208d213e1aSmatthias.ringwald 
24218d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
24228d213e1aSmatthias.ringwald             switch (power_mode){
24238d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
242428171530Smatthias.ringwald 
2425423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
242628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
2427d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
24283a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
24295c363727SMatthias Ringwald                         hci_stack->substate = HCI_INIT_AFTER_SLEEP;
2430758b46ceSmatthias.ringwald                         hci_update_scan_enable();
243128171530Smatthias.ringwald                         break;
243228171530Smatthias.ringwald                     }
243328171530Smatthias.ringwald #endif
24343144bce4Smatthias.ringwald                     err = hci_power_control_wake();
24353144bce4Smatthias.ringwald                     if (err) return err;
243644935e40S[email protected]                     hci_power_transition_to_initializing();
24378d213e1aSmatthias.ringwald                     break;
24388d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
24393a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
24408d213e1aSmatthias.ringwald                     break;
24418d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2442b546ac54Smatthias.ringwald                     // do nothing
24438d213e1aSmatthias.ringwald                     break;
24448d213e1aSmatthias.ringwald             }
24458d213e1aSmatthias.ringwald             break;
244611e23e5fSmatthias.ringwald     }
244768d92d03Smatthias.ringwald 
2448038bc64cSmatthias.ringwald     // create internal event
2449ee8bf225Smatthias.ringwald 	hci_emit_state();
2450ee8bf225Smatthias.ringwald 
245168d92d03Smatthias.ringwald 	// trigger next/first action
245268d92d03Smatthias.ringwald 	hci_run();
245368d92d03Smatthias.ringwald 
2454475c8125Smatthias.ringwald     return 0;
2455475c8125Smatthias.ringwald }
2456475c8125Smatthias.ringwald 
245735454696SMatthias Ringwald 
245835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
245935454696SMatthias Ringwald 
2460758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
2461758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
24623a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
2463758b46ceSmatthias.ringwald     hci_run();
2464758b46ceSmatthias.ringwald }
2465758b46ceSmatthias.ringwald 
246615a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){
2467381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
2468381fbed8Smatthias.ringwald 
24693a9fb326S[email protected]     if (hci_stack->discoverable == enable){
24703a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
2471381fbed8Smatthias.ringwald         return;
2472381fbed8Smatthias.ringwald     }
2473381fbed8Smatthias.ringwald 
24743a9fb326S[email protected]     hci_stack->discoverable = enable;
2475758b46ceSmatthias.ringwald     hci_update_scan_enable();
2476758b46ceSmatthias.ringwald }
2477b031bebbSmatthias.ringwald 
247815a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){
2479758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
2480758b46ceSmatthias.ringwald 
2481758b46ceSmatthias.ringwald     // don't emit event
24823a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
2483758b46ceSmatthias.ringwald 
24843a9fb326S[email protected]     hci_stack->connectable = enable;
2485758b46ceSmatthias.ringwald     hci_update_scan_enable();
2486381fbed8Smatthias.ringwald }
248735454696SMatthias Ringwald #endif
2488381fbed8Smatthias.ringwald 
248915a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){
2490690bd0baS[email protected]     memcpy(address_buffer, hci_stack->local_bd_addr, 6);
24915061f3afS[email protected] }
24925061f3afS[email protected] 
249395d71764SMatthias Ringwald static void hci_run(void){
24948a485f27Smatthias.ringwald 
2495db8bc6ffSMatthias Ringwald     // log_info("hci_run: entered");
2496665d90f2SMatthias Ringwald     btstack_linked_item_t * it;
249732ab9390Smatthias.ringwald 
2498b5d8b22bS[email protected]     // send continuation fragments first, as they block the prepared packet buffer
2499b5d8b22bS[email protected]     if (hci_stack->acl_fragmentation_total_size > 0) {
2500b5d8b22bS[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer);
2501b5d8b22bS[email protected]         hci_connection_t *connection = hci_connection_for_handle(con_handle);
2502b5d8b22bS[email protected]         if (connection) {
250328a0332dSMatthias Ringwald             if (hci_can_send_prepared_acl_packet_now(con_handle)){
2504b5d8b22bS[email protected]                 hci_send_acl_packet_fragments(connection);
2505b5d8b22bS[email protected]                 return;
2506b5d8b22bS[email protected]             }
250728a0332dSMatthias Ringwald         } else {
2508b5d8b22bS[email protected]             // connection gone -> discard further fragments
250928a0332dSMatthias Ringwald             log_info("hci_run: fragmented ACL packet no connection -> discard fragment");
2510b5d8b22bS[email protected]             hci_stack->acl_fragmentation_total_size = 0;
2511b5d8b22bS[email protected]             hci_stack->acl_fragmentation_pos = 0;
2512b5d8b22bS[email protected]         }
2513b5d8b22bS[email protected]     }
2514b5d8b22bS[email protected] 
2515d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()) return;
2516ce4c8fabSmatthias.ringwald 
2517b031bebbSmatthias.ringwald     // global/non-connection oriented commands
2518b031bebbSmatthias.ringwald 
251935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2520b031bebbSmatthias.ringwald     // decline incoming connections
25213a9fb326S[email protected]     if (hci_stack->decline_reason){
25223a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
25233a9fb326S[email protected]         hci_stack->decline_reason = 0;
25243a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
2525dbe1a790S[email protected]         return;
2526ce4c8fabSmatthias.ringwald     }
2527ce4c8fabSmatthias.ringwald 
2528b031bebbSmatthias.ringwald     // send scan enable
25293a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
25303a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
25313a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
2532dbe1a790S[email protected]         return;
2533b031bebbSmatthias.ringwald     }
253435454696SMatthias Ringwald #endif
2535b031bebbSmatthias.ringwald 
2536a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
25377bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
253845c102fdSMatthias Ringwald         // handle le scan
25397bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
25407bdc6798S[email protected]             case LE_START_SCAN:
25417bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
25427bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
25437bdc6798S[email protected]                 return;
25447bdc6798S[email protected] 
25457bdc6798S[email protected]             case LE_STOP_SCAN:
25467bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
25477bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
25487bdc6798S[email protected]                 return;
25497bdc6798S[email protected]             default:
25507bdc6798S[email protected]                 break;
25517bdc6798S[email protected]         }
2552e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
2553e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
2554e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
2555e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
2556e2602ea2Smatthias.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);
2557e2602ea2Smatthias.ringwald             return;
2558e2602ea2Smatthias.ringwald         }
255945c102fdSMatthias Ringwald         // le advertisement control
25609a2e4658SMatthias Ringwald         if (hci_stack->le_advertisements_todo){
25619a2e4658SMatthias Ringwald             log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo );
25629a2e4658SMatthias Ringwald         }
256345c102fdSMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){
256445c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE;
256545c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertise_enable, 0);
256645c102fdSMatthias Ringwald             return;
256745c102fdSMatthias Ringwald         }
256845c102fdSMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){
256945c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS;
257045c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertising_parameters,
257145c102fdSMatthias Ringwald                  hci_stack->le_advertisements_interval_min,
257245c102fdSMatthias Ringwald                  hci_stack->le_advertisements_interval_max,
257345c102fdSMatthias Ringwald                  hci_stack->le_advertisements_type,
257445c102fdSMatthias Ringwald                  hci_stack->le_advertisements_own_address_type,
257545c102fdSMatthias Ringwald                  hci_stack->le_advertisements_direct_address_type,
257645c102fdSMatthias Ringwald                  hci_stack->le_advertisements_direct_address,
257745c102fdSMatthias Ringwald                  hci_stack->le_advertisements_channel_map,
257845c102fdSMatthias Ringwald                  hci_stack->le_advertisements_filter_policy);
257945c102fdSMatthias Ringwald             return;
258045c102fdSMatthias Ringwald         }
2581501f56b3SMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){
2582501f56b3SMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA;
258345c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len,
258445c102fdSMatthias Ringwald                 hci_stack->le_advertisements_data);
258545c102fdSMatthias Ringwald             return;
258645c102fdSMatthias Ringwald         }
2587501f56b3SMatthias Ringwald         if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){
2588501f56b3SMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA;
2589501f56b3SMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len,
2590501f56b3SMatthias Ringwald                 hci_stack->le_scan_response_data);
2591501f56b3SMatthias Ringwald             return;
2592501f56b3SMatthias Ringwald         }
259352b551c3SMatthias Ringwald         // Random address needs to be set before enabling advertisements
259452b551c3SMatthias Ringwald         if ((hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE)
259552b551c3SMatthias Ringwald         &&  (hci_stack->le_advertisements_own_address_type == 0 || hci_stack->le_advertisements_random_address_set)){
259645c102fdSMatthias Ringwald             hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE;
259745c102fdSMatthias Ringwald             hci_send_cmd(&hci_le_set_advertise_enable, 1);
259845c102fdSMatthias Ringwald             return;
259945c102fdSMatthias Ringwald         }
26009956955bSMatthias Ringwald 
26019956955bSMatthias Ringwald         //
26029956955bSMatthias Ringwald         // LE Whitelist Management
26039956955bSMatthias Ringwald         //
26049956955bSMatthias Ringwald 
26059956955bSMatthias Ringwald         // check if whitelist needs modification
2606665d90f2SMatthias Ringwald         btstack_linked_list_iterator_t lit;
26079956955bSMatthias Ringwald         int modification_pending = 0;
2608665d90f2SMatthias Ringwald         btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2609665d90f2SMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&lit)){
2610665d90f2SMatthias Ringwald             whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
26119956955bSMatthias Ringwald             if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){
26129956955bSMatthias Ringwald                 modification_pending = 1;
26139956955bSMatthias Ringwald                 break;
26149956955bSMatthias Ringwald             }
26159956955bSMatthias Ringwald         }
261691915b0bSMatthias Ringwald 
26179956955bSMatthias Ringwald         if (modification_pending){
261891915b0bSMatthias Ringwald             // stop connnecting if modification pending
26199956955bSMatthias Ringwald             if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){
26209956955bSMatthias Ringwald                 hci_send_cmd(&hci_le_create_connection_cancel);
26219956955bSMatthias Ringwald                 return;
26229956955bSMatthias Ringwald             }
26239956955bSMatthias Ringwald 
26249956955bSMatthias Ringwald             // add/remove entries
2625665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2626665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&lit)){
2627665d90f2SMatthias Ringwald                 whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
26289956955bSMatthias Ringwald                 if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){
26299956955bSMatthias Ringwald                     entry->state = LE_WHITELIST_ON_CONTROLLER;
26309956955bSMatthias Ringwald                     hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address);
26319956955bSMatthias Ringwald                     return;
26329956955bSMatthias Ringwald 
26339956955bSMatthias Ringwald                 }
26349956955bSMatthias Ringwald                 if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){
26359ecbe201SMatthias Ringwald                     bd_addr_t address;
26369ecbe201SMatthias Ringwald                     bd_addr_type_t address_type = entry->address_type;
26379ecbe201SMatthias Ringwald                     memcpy(address, entry->address, 6);
2638665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry);
26399956955bSMatthias Ringwald                     btstack_memory_whitelist_entry_free(entry);
26409ecbe201SMatthias Ringwald                     hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address);
26419956955bSMatthias Ringwald                     return;
26429956955bSMatthias Ringwald                 }
26439956955bSMatthias Ringwald             }
264491915b0bSMatthias Ringwald         }
26459956955bSMatthias Ringwald 
26469956955bSMatthias Ringwald         // start connecting
264791915b0bSMatthias Ringwald         if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE &&
2648665d90f2SMatthias Ringwald             !btstack_linked_list_empty(&hci_stack->le_whitelist)){
26499956955bSMatthias Ringwald             bd_addr_t null_addr;
26509956955bSMatthias Ringwald             memset(null_addr, 0, 6);
26519956955bSMatthias Ringwald             hci_send_cmd(&hci_le_create_connection,
26529956955bSMatthias Ringwald                  0x0060,    // scan interval: 60 ms
26539956955bSMatthias Ringwald                  0x0030,    // scan interval: 30 ms
26549956955bSMatthias Ringwald                  1,         // use whitelist
26559956955bSMatthias Ringwald                  0,         // peer address type
26569956955bSMatthias Ringwald                  null_addr,      // peer bd addr
26579956955bSMatthias Ringwald                  hci_stack->adv_addr_type, // our addr type:
26589956955bSMatthias Ringwald                  0x0008,    // conn interval min
26599956955bSMatthias Ringwald                  0x0018,    // conn interval max
26609956955bSMatthias Ringwald                  0,         // conn latency
26619956955bSMatthias Ringwald                  0x0048,    // supervision timeout
26629956955bSMatthias Ringwald                  0x0001,    // min ce length
26639956955bSMatthias Ringwald                  0x0001     // max ce length
26649956955bSMatthias Ringwald                  );
26659956955bSMatthias Ringwald             return;
26669956955bSMatthias Ringwald         }
26677bdc6798S[email protected]     }
2668b2f949feS[email protected] #endif
26697bdc6798S[email protected] 
267032ab9390Smatthias.ringwald     // send pending HCI commands
2671665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
267205ae8de3SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) it;
267332ab9390Smatthias.ringwald 
26740bf6344aS[email protected]         switch(connection->state){
26750bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
26764f3229d8S[email protected]                 switch(connection->address_type){
267735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
26784f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
26799da54300S[email protected]                         log_info("sending hci_create_connection");
2680ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
26814f3229d8S[email protected]                         break;
268235454696SMatthias Ringwald #endif
26834f3229d8S[email protected]                     default:
2684a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
26859da54300S[email protected]                         log_info("sending hci_le_create_connection");
26864f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
2687b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
2688b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
26894f3229d8S[email protected]                                      0,         // don't use whitelist
26904f3229d8S[email protected]                                      connection->address_type, // peer address type
26914f3229d8S[email protected]                                      connection->address,      // peer bd addr
2692b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
2693b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
2694b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
26954f3229d8S[email protected]                                      0,         // conn latency
2696b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
2697b2571179Smatthias.ringwald                                      0x0001,    // min ce length
2698b2571179Smatthias.ringwald                                      0x0001     // max ce length
26994f3229d8S[email protected]                                      );
27004f3229d8S[email protected] 
27014f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
2702b2f949feS[email protected] #endif
27034f3229d8S[email protected]                         break;
27044f3229d8S[email protected]                 }
2705ad83dc6aS[email protected]                 return;
2706ad83dc6aS[email protected] 
270735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
27080bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
270994e4aaa2SMatthias Ringwald                 log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO);
271032ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
27115cf766e8SMatthias Ringwald                 connection->role  = HCI_ROLE_SLAVE;
2712e35edcc1S[email protected]                 if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
271334d2123cS[email protected]                     hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
2714e35edcc1S[email protected]                 }
2715dbe1a790S[email protected]                 return;
271635454696SMatthias Ringwald #endif
27170bf6344aS[email protected] 
2718a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
27190bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
27200bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
27210bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
27220bf6344aS[email protected]                 return;
2723a6725849S[email protected] #endif
27240bf6344aS[email protected]             case SEND_DISCONNECT:
27250bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
27267851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
27270bf6344aS[email protected]                 return;
27280bf6344aS[email protected] 
27290bf6344aS[email protected]             default:
27300bf6344aS[email protected]                 break;
2731c7e0c5f6Smatthias.ringwald         }
2732c7e0c5f6Smatthias.ringwald 
273335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
273432ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
27359da54300S[email protected]             log_info("responding to link key request");
273634d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
273732ab9390Smatthias.ringwald             link_key_t link_key;
2738c77e8838S[email protected]             link_key_type_t link_key_type;
2739a98592bcSMatthias Ringwald             if ( hci_stack->link_key_db
2740a98592bcSMatthias Ringwald               && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type)
274134d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
27429ab95c90S[email protected]                connection->link_key_type = link_key_type;
274332ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
274432ab9390Smatthias.ringwald             } else {
274532ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
274632ab9390Smatthias.ringwald             }
2747dbe1a790S[email protected]             return;
274832ab9390Smatthias.ringwald         }
27491d6b20aeS[email protected] 
2750899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
27519da54300S[email protected]             log_info("denying to pin request");
2752899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
275334d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
27544c57c146S[email protected]             return;
27554c57c146S[email protected]         }
27564c57c146S[email protected] 
2757dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
275834d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
275982d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
276082d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
2761106d6d11S[email protected]                 // tweak authentication requirements
27623a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
2763106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
27649faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
27659faad3abS[email protected]                 }
27669faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
27679faad3abS[email protected]                     authreq |= 1;
2768106d6d11S[email protected]                 }
27693a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
2770f8fb5f6eS[email protected]             } else {
2771f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
2772f8fb5f6eS[email protected]             }
2773dbe1a790S[email protected]             return;
277432ab9390Smatthias.ringwald         }
277532ab9390Smatthias.ringwald 
2776dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
2777dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
277834d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
2779dbe1a790S[email protected]             return;
2780dbe1a790S[email protected]         }
2781dbe1a790S[email protected] 
2782dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
2783dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
278434d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
2785dbe1a790S[email protected]             return;
2786dbe1a790S[email protected]         }
278735454696SMatthias Ringwald #endif
2788afd4e962S[email protected] 
2789afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
2790afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
279134d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
27922bd8b7e7S[email protected]             return;
27932bd8b7e7S[email protected]         }
27942bd8b7e7S[email protected] 
27952bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
27962bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
279734d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
279834d2123cS[email protected]             return;
279934d2123cS[email protected]         }
2800ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
2801ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
28021bd5283dS[email protected]             connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT;
280352d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
2804ad83dc6aS[email protected]             return;
2805ad83dc6aS[email protected]         }
280634d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
280734d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
280834d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
28092bd8b7e7S[email protected]             return;
2810afd4e962S[email protected]         }
2811dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
2812dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
2813dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
2814dce78009S[email protected]             return;
2815dce78009S[email protected]         }
2816da886c03S[email protected] 
2817a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
2818da886c03S[email protected]         if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){
2819da886c03S[email protected]             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
2820da886c03S[email protected] 
2821c37a3166S[email protected]             uint16_t connection_interval_min = connection->le_conn_interval_min;
2822c37a3166S[email protected]             connection->le_conn_interval_min = 0;
2823c37a3166S[email protected]             hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min,
2824c37a3166S[email protected]                 connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout,
2825c37a3166S[email protected]                 0x0000, 0xffff);
2826c37a3166S[email protected]         }
2827c37a3166S[email protected] #endif
2828dbe1a790S[email protected]     }
2829c7e0c5f6Smatthias.ringwald 
283005ae8de3SMatthias Ringwald     hci_connection_t * connection;
28313a9fb326S[email protected]     switch (hci_stack->state){
28323429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
283374b323a9SMatthias Ringwald             hci_initializing_run();
28343429f56bSmatthias.ringwald             break;
2835c7e0c5f6Smatthias.ringwald 
2836c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
2837c7e0c5f6Smatthias.ringwald 
28389da54300S[email protected]             log_info("HCI_STATE_HALTING");
28399956955bSMatthias Ringwald 
28409956955bSMatthias Ringwald             // free whitelist entries
2841a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
28429956955bSMatthias Ringwald             {
2843665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_t lit;
2844665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist);
2845665d90f2SMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&lit)){
2846665d90f2SMatthias Ringwald                     whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit);
2847665d90f2SMatthias Ringwald                     btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry);
28489956955bSMatthias Ringwald                     btstack_memory_whitelist_entry_free(entry);
28499956955bSMatthias Ringwald                 }
28509956955bSMatthias Ringwald             }
28519956955bSMatthias Ringwald #endif
2852c7e0c5f6Smatthias.ringwald             // close all open connections
28533a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
2854c7e0c5f6Smatthias.ringwald             if (connection){
2855711e6c80SMatthias Ringwald                 hci_con_handle_t con_handle = (uint16_t) connection->con_handle;
2856d94d3cafS[email protected]                 if (!hci_can_send_command_packet_now()) return;
285732ab9390Smatthias.ringwald 
28588837e9efSMatthias Ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle);
2859c7e0c5f6Smatthias.ringwald 
28608837e9efSMatthias Ringwald                 // cancel all l2cap connections right away instead of waiting for disconnection complete event ...
28618837e9efSMatthias Ringwald                 hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host
28628837e9efSMatthias Ringwald 
28638837e9efSMatthias Ringwald                 // ... which would be ignored anyway as we shutdown (free) the connection now
2864c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
28658837e9efSMatthias Ringwald 
28668837e9efSMatthias Ringwald                 // finally, send the disconnect command
28678837e9efSMatthias Ringwald                 hci_send_cmd(&hci_disconnect, con_handle, 0x13);  // remote closed connection
2868c7e0c5f6Smatthias.ringwald                 return;
2869c7e0c5f6Smatthias.ringwald             }
28709da54300S[email protected]             log_info("HCI_STATE_HALTING, calling off");
2871c7e0c5f6Smatthias.ringwald 
287272ea5239Smatthias.ringwald             // switch mode
2873c7e0c5f6Smatthias.ringwald             hci_power_control_off();
28749418f9c9Smatthias.ringwald 
28759da54300S[email protected]             log_info("HCI_STATE_HALTING, emitting state");
287672ea5239Smatthias.ringwald             hci_emit_state();
28779da54300S[email protected]             log_info("HCI_STATE_HALTING, done");
287872ea5239Smatthias.ringwald             break;
2879c7e0c5f6Smatthias.ringwald 
288072ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
28813a9fb326S[email protected]             switch(hci_stack->substate) {
288274b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_DISCONNECT:
28839da54300S[email protected]                     log_info("HCI_STATE_FALLING_ASLEEP");
288472ea5239Smatthias.ringwald                     // close all open connections
28853a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
288666da7044Smatthias.ringwald 
2887423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
288866da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
2889d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
289066da7044Smatthias.ringwald                         connection = NULL;
289166da7044Smatthias.ringwald                     }
289266da7044Smatthias.ringwald #endif
289372ea5239Smatthias.ringwald                     if (connection){
289432ab9390Smatthias.ringwald 
289572ea5239Smatthias.ringwald                         // send disconnect
2896d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
289732ab9390Smatthias.ringwald 
28989da54300S[email protected]                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
28996ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
290072ea5239Smatthias.ringwald 
290172ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
290272ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
290372ea5239Smatthias.ringwald                         return;
290472ea5239Smatthias.ringwald                     }
290572ea5239Smatthias.ringwald 
290692368cd3S[email protected]                     if (hci_classic_supported()){
290789db417bSmatthias.ringwald                         // disable page and inquiry scan
2908d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
290932ab9390Smatthias.ringwald 
29109da54300S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans");
29113a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
291289db417bSmatthias.ringwald 
291389db417bSmatthias.ringwald                         // continue in next sub state
291474b323a9SMatthias Ringwald                         hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE;
291589db417bSmatthias.ringwald                         break;
291692368cd3S[email protected]                     }
2917202c8a4cSMatthias Ringwald                     // no break - fall through for ble-only chips
291892368cd3S[email protected] 
291974b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_COMPLETE:
29209da54300S[email protected]                     log_info("HCI_STATE_HALTING, calling sleep");
2921423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS
292228171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
2923d0b87befSMatthias Ringwald                     if (btstack_control_iphone_power_management_enabled()){
292428171530Smatthias.ringwald                         // SLEEP MODE reached
29253a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
292628171530Smatthias.ringwald                         hci_emit_state();
292728171530Smatthias.ringwald                         break;
292828171530Smatthias.ringwald                     }
292928171530Smatthias.ringwald #endif
293072ea5239Smatthias.ringwald                     // switch mode
29313a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
2932c7e0c5f6Smatthias.ringwald                     hci_emit_state();
293328171530Smatthias.ringwald                     break;
293428171530Smatthias.ringwald 
293589db417bSmatthias.ringwald                 default:
293689db417bSmatthias.ringwald                     break;
293789db417bSmatthias.ringwald             }
2938c7e0c5f6Smatthias.ringwald             break;
2939c7e0c5f6Smatthias.ringwald 
29403429f56bSmatthias.ringwald         default:
29413429f56bSmatthias.ringwald             break;
29421f504dbdSmatthias.ringwald     }
29433429f56bSmatthias.ringwald }
294416833f0aSmatthias.ringwald 
294531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
294635454696SMatthias Ringwald     // house-keeping
294735454696SMatthias Ringwald 
294835454696SMatthias Ringwald     if (IS_COMMAND(packet, hci_write_loopback_mode)){
294935454696SMatthias Ringwald         hci_stack->loopback_mode = packet[3];
295035454696SMatthias Ringwald     }
295135454696SMatthias Ringwald 
295235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
2953c8e4258aSmatthias.ringwald     bd_addr_t addr;
2954c8e4258aSmatthias.ringwald     hci_connection_t * conn;
2955c8e4258aSmatthias.ringwald 
2956c8e4258aSmatthias.ringwald     // create_connection?
2957c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
2958724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
29599da54300S[email protected]         log_info("Create_connection to %s", bd_addr_to_str(addr));
2960c8e4258aSmatthias.ringwald 
29612e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2962ad83dc6aS[email protected]         if (!conn){
296396a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
296417f1ba2aSmatthias.ringwald             if (!conn){
296517f1ba2aSmatthias.ringwald                 // notify client that alloc failed
29662deddeceSMatthias Ringwald                 hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
296717f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
296817f1ba2aSmatthias.ringwald             }
2969ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
2970ad83dc6aS[email protected]         }
2971ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
2972ad83dc6aS[email protected]         switch (conn->state){
2973ad83dc6aS[email protected]             // if connection active exists
2974ad83dc6aS[email protected]             case OPEN:
297562bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
2976274dad91SMatthias Ringwald                 hci_emit_connection_complete(addr, conn->con_handle, 0);
297762bda3fbS[email protected]                 return 0;
2978ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
2979ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
2980ad83dc6aS[email protected]                 break;
2981ad83dc6aS[email protected]             default:
2982ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
2983ad83dc6aS[email protected]                 return 0;
2984ad83dc6aS[email protected]         }
2985c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
2986c8e4258aSmatthias.ringwald     }
298735454696SMatthias Ringwald 
29887fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
29897fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
29907fde4af9Smatthias.ringwald     }
29917fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
29927fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
29937fde4af9Smatthias.ringwald     }
29947fde4af9Smatthias.ringwald 
29958ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
2996a98592bcSMatthias Ringwald         if (hci_stack->link_key_db){
2997724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[3], addr);
2998a98592bcSMatthias Ringwald             hci_stack->link_key_db->delete_link_key(addr);
29998ef73945Smatthias.ringwald         }
30008ef73945Smatthias.ringwald     }
3001c8e4258aSmatthias.ringwald 
30026724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
30036724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
3004724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
30052e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
30066724cd9eS[email protected]         if (conn){
30076724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
30086724cd9eS[email protected]         }
30096724cd9eS[email protected]     }
30106724cd9eS[email protected] 
30116724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
30126724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
30136724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
30146724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
3015724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], addr);
30162e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
30176724cd9eS[email protected]         if (conn){
30186724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
30196724cd9eS[email protected]         }
30206724cd9eS[email protected]     }
302135454696SMatthias Ringwald #endif
30224b3e1e19SMatthias Ringwald 
3023a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
302469a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
30253a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
302669a97523S[email protected]     }
302769a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
302852b551c3SMatthias Ringwald         hci_stack->le_advertisements_random_address_set = 1;
3029724d70a2SMatthias Ringwald         reverse_bd_addr(&packet[3], hci_stack->adv_address);
303069a97523S[email protected]     }
3031171293d3SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_set_advertise_enable)){
3032171293d3SMatthias Ringwald         hci_stack->le_advertisements_active = packet[3];
3033171293d3SMatthias Ringwald     }
3034b04dfa37SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_create_connection)){
3035b04dfa37SMatthias Ringwald         // white list used?
3036b04dfa37SMatthias Ringwald         uint8_t initiator_filter_policy = packet[7];
3037b04dfa37SMatthias Ringwald         switch (initiator_filter_policy){
3038b04dfa37SMatthias Ringwald             case 0:
3039b04dfa37SMatthias Ringwald                 // whitelist not used
3040b04dfa37SMatthias Ringwald                 hci_stack->le_connecting_state = LE_CONNECTING_DIRECT;
3041b04dfa37SMatthias Ringwald                 break;
3042b04dfa37SMatthias Ringwald             case 1:
3043b04dfa37SMatthias Ringwald                 hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST;
3044b04dfa37SMatthias Ringwald                 break;
3045b04dfa37SMatthias Ringwald             default:
3046b04dfa37SMatthias Ringwald                 log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy);
3047b04dfa37SMatthias Ringwald                 break;
3048b04dfa37SMatthias Ringwald         }
3049b04dfa37SMatthias Ringwald     }
3050b04dfa37SMatthias Ringwald     if (IS_COMMAND(packet, hci_le_create_connection_cancel)){
3051b04dfa37SMatthias Ringwald         hci_stack->le_connecting_state = LE_CONNECTING_IDLE;
3052b04dfa37SMatthias Ringwald     }
305369a97523S[email protected] #endif
305469a97523S[email protected] 
30553a9fb326S[email protected]     hci_stack->num_cmd_packets--;
30565bb5bc3eS[email protected] 
30575bb5bc3eS[email protected]     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size);
30586b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
30596b4af23dS[email protected] 
3060d051460cS[email protected]     // release packet buffer for synchronous transport implementations
3061c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
30626b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
30636b4af23dS[email protected]     }
30646b4af23dS[email protected] 
30656b4af23dS[email protected]     return err;
306631452debSmatthias.ringwald }
30678adf0ddaSmatthias.ringwald 
30682bd8b7e7S[email protected] // disconnect because of security block
30692bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
30702bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
30712bd8b7e7S[email protected]     if (!connection) return;
30722bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
30732bd8b7e7S[email protected] }
30742bd8b7e7S[email protected] 
30752bd8b7e7S[email protected] 
3076dbe1a790S[email protected] // Configure Secure Simple Pairing
3077dbe1a790S[email protected] 
307835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
307935454696SMatthias Ringwald 
3080dbe1a790S[email protected] // enable will enable SSP during init
308115a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){
30823a9fb326S[email protected]     hci_stack->ssp_enable = enable;
3083dbe1a790S[email protected] }
3084dbe1a790S[email protected] 
308595d71764SMatthias Ringwald static int hci_local_ssp_activated(void){
308615a95bd5SMatthias Ringwald     return gap_ssp_supported() && hci_stack->ssp_enable;
30872bd8b7e7S[email protected] }
30882bd8b7e7S[email protected] 
3089dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
309015a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){
30913a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
3092dbe1a790S[email protected] }
309315a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){
30943a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
3095dbe1a790S[email protected] }
3096dbe1a790S[email protected] 
3097dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
309815a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){
30993a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
3100dbe1a790S[email protected] }
310135454696SMatthias Ringwald #endif
3102dbe1a790S[email protected] 
310394be1a66SMatthias Ringwald // va_list part of hci_send_cmd
310494be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){
3105d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()){
31069d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
31079d14b626S[email protected]         return 0;
31089d14b626S[email protected]     }
31099d14b626S[email protected] 
31105127cc62S[email protected]     // for HCI INITIALIZATION
31119da54300S[email protected]     // log_info("hci_send_cmd: opcode %04x", cmd->opcode);
31125127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
31135127cc62S[email protected] 
31149d14b626S[email protected]     hci_reserve_packet_buffer();
31159d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
311694be1a66SMatthias Ringwald     uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr);
311794be1a66SMatthias Ringwald     return hci_send_cmd_packet(packet, size);
311894be1a66SMatthias Ringwald }
31199d14b626S[email protected] 
312094be1a66SMatthias Ringwald /**
312194be1a66SMatthias Ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
312294be1a66SMatthias Ringwald  */
312394be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
31241cd208adSmatthias.ringwald     va_list argptr;
31251cd208adSmatthias.ringwald     va_start(argptr, cmd);
312694be1a66SMatthias Ringwald     int res = hci_send_cmd_va_arg(cmd, argptr);
31271cd208adSmatthias.ringwald     va_end(argptr);
312894be1a66SMatthias Ringwald     return res;
312993b8dc03Smatthias.ringwald }
3130c8e4258aSmatthias.ringwald 
3131ee091cf1Smatthias.ringwald // Create various non-HCI events.
3132ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
3133ee091cf1Smatthias.ringwald 
3134d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){
3135fb37a842SMatthias Ringwald     // dump packet
3136d6b06661SMatthias Ringwald     if (dump) {
3137300c1ba4SMatthias Ringwald         hci_dump_packet( HCI_EVENT_PACKET, 0, event, size);
3138d6b06661SMatthias Ringwald     }
31391ef6bb52SMatthias Ringwald 
3140fb37a842SMatthias Ringwald     // dispatch to all event handlers
31411ef6bb52SMatthias Ringwald     btstack_linked_list_iterator_t it;
31421ef6bb52SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers);
31431ef6bb52SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
31441ef6bb52SMatthias Ringwald         btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
3145d9a7306aSMatthias Ringwald         entry->callback(HCI_EVENT_PACKET, 0, event, size);
31461ef6bb52SMatthias Ringwald     }
3147d6b06661SMatthias Ringwald }
3148d6b06661SMatthias Ringwald 
3149d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){
3150fb37a842SMatthias Ringwald     if (!hci_stack->acl_packet_handler) return;
31513d50b4baSMatthias Ringwald     hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size);
3152d6b06661SMatthias Ringwald }
3153d6b06661SMatthias Ringwald 
315435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
3155701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){
31563bc639ceSMatthias Ringwald     // notify SCO sender if waiting
3157701e3307SMatthias Ringwald     if (!hci_stack->sco_waiting_for_can_send_now) return;
3158701e3307SMatthias Ringwald     if (hci_can_send_sco_packet_now()){
31593bc639ceSMatthias Ringwald         hci_stack->sco_waiting_for_can_send_now = 0;
3160701e3307SMatthias Ringwald         uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 };
3161701e3307SMatthias Ringwald         hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event));
31623d50b4baSMatthias Ringwald         hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
31633bc639ceSMatthias Ringwald     }
31643bc639ceSMatthias Ringwald }
316535454696SMatthias Ringwald #endif
31663bc639ceSMatthias Ringwald 
316771de195eSMatthias Ringwald void hci_emit_state(void){
31683a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
3169425d1371Smatthias.ringwald     uint8_t event[3];
317080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
3171425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
31723a9fb326S[email protected]     event[2] = hci_stack->state;
3173d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3174c8e4258aSmatthias.ringwald }
3175c8e4258aSmatthias.ringwald 
317635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
31772deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){
3178425d1371Smatthias.ringwald     uint8_t event[13];
3179c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
3180425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
318117f1ba2aSmatthias.ringwald     event[2] = status;
31822deddeceSMatthias Ringwald     little_endian_store_16(event, 3, con_handle);
31832deddeceSMatthias Ringwald     reverse_bd_addr(address, &event[5]);
3184c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
3185c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
3186d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3187c8e4258aSmatthias.ringwald }
318852db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
318952db98b2SMatthias Ringwald     if (disable_l2cap_timeouts) return;
319052db98b2SMatthias Ringwald     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
319152db98b2SMatthias Ringwald     uint8_t event[4];
319252db98b2SMatthias Ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
319352db98b2SMatthias Ringwald     event[1] = sizeof(event) - 2;
319452db98b2SMatthias Ringwald     little_endian_store_16(event, 2, conn->con_handle);
319552db98b2SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
319652db98b2SMatthias Ringwald }
319735454696SMatthias Ringwald #endif
3198c8e4258aSmatthias.ringwald 
319935454696SMatthias Ringwald #ifdef ENABLE_BLE
3200fc64f94aSMatthias 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){
32014f3229d8S[email protected]     uint8_t event[21];
32024f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
32034f3229d8S[email protected]     event[1] = sizeof(event) - 2;
32044f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
32054f3229d8S[email protected]     event[3] = status;
3206fc64f94aSMatthias Ringwald     little_endian_store_16(event, 4, con_handle);
32074f3229d8S[email protected]     event[6] = 0; // TODO: role
32086e2e9a6bS[email protected]     event[7] = address_type;
3209724d70a2SMatthias Ringwald     reverse_bd_addr(address, &event[8]);
3210f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, 0); // interval
3211f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 16, 0); // latency
3212f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 18, 0); // supervision timeout
32134f3229d8S[email protected]     event[20] = 0; // master clock accuracy
3214d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
32154f3229d8S[email protected] }
321635454696SMatthias Ringwald #endif
32174f3229d8S[email protected] 
3218fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){
3219425d1371Smatthias.ringwald     uint8_t event[6];
32203c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
3221e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
32223c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
3223fc64f94aSMatthias Ringwald     little_endian_store_16(event, 3, con_handle);
32243c4d4b90Smatthias.ringwald     event[5] = reason;
3225d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
32263c4d4b90Smatthias.ringwald }
32273c4d4b90Smatthias.ringwald 
3228b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){
3229e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
3230425d1371Smatthias.ringwald     uint8_t event[3];
323180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
3232425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
323343bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
3234d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
323543bfb1bdSmatthias.ringwald }
3236038bc64cSmatthias.ringwald 
3237b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){
3238e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
3239425d1371Smatthias.ringwald     uint8_t event[2];
324080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
3241425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
3242d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3243038bc64cSmatthias.ringwald }
32441b0e3922Smatthias.ringwald 
324535454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){
324635454696SMatthias Ringwald     log_info("hci_emit_dedicated_bonding_result %u ", status);
324735454696SMatthias Ringwald     uint8_t event[9];
324835454696SMatthias Ringwald     int pos = 0;
324935454696SMatthias Ringwald     event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED;
325035454696SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
325135454696SMatthias Ringwald     event[pos++] = status;
325235454696SMatthias Ringwald     reverse_bd_addr(address, &event[pos]);
3253d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3254381fbed8Smatthias.ringwald }
3255458bf4e8S[email protected] 
325635454696SMatthias Ringwald 
325735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
325835454696SMatthias Ringwald 
3259b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
3260df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
3261a00031e2S[email protected]     uint8_t event[5];
3262e00caf9cS[email protected]     int pos = 0;
32635611a760SMatthias Ringwald     event[pos++] = GAP_EVENT_SECURITY_LEVEL;
3264e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
3265f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
3266e00caf9cS[email protected]     pos += 2;
3267e00caf9cS[email protected]     event[pos++] = level;
3268d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3269e00caf9cS[email protected] }
3270e00caf9cS[email protected] 
327135454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
327235454696SMatthias Ringwald     if (!connection) return LEVEL_0;
327335454696SMatthias Ringwald     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
327435454696SMatthias Ringwald     return gap_security_level_for_link_key_type(connection->link_key_type);
327535454696SMatthias Ringwald }
327635454696SMatthias Ringwald 
327735454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){
327835454696SMatthias Ringwald     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
327935454696SMatthias Ringwald     uint8_t event[3];
328035454696SMatthias Ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
328135454696SMatthias Ringwald     event[1] = sizeof(event) - 2;
328235454696SMatthias Ringwald     event[2] = enabled;
3283d6b06661SMatthias Ringwald     hci_emit_event(event, sizeof(event), 1);
3284ad83dc6aS[email protected] }
3285ad83dc6aS[email protected] 
328606b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC
328798a2fd1cSMatthias Ringwald // query if remote side supports eSCO
3288073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){
328998a2fd1cSMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
329098a2fd1cSMatthias Ringwald     if (!connection) return 0;
329198a2fd1cSMatthias Ringwald     return connection->remote_supported_feature_eSCO;
329298a2fd1cSMatthias Ringwald }
329398a2fd1cSMatthias Ringwald 
32942bd8b7e7S[email protected] // query if remote side supports SSP
32952bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
32962bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
32972bd8b7e7S[email protected]     if (!connection) return 0;
32982bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
32992bd8b7e7S[email protected] }
33002bd8b7e7S[email protected] 
330115a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){
3302df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
3303df3354fcS[email protected] }
330406b9e820SMatthias Ringwald #endif
3305df3354fcS[email protected] 
3306458bf4e8S[email protected] // GAP API
3307458bf4e8S[email protected] /**
3308458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
3309458bf4e8S[email protected]  * @praram enabled
3310458bf4e8S[email protected]  */
33114c57c146S[email protected] void gap_set_bondable_mode(int enable){
33123a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
3313458bf4e8S[email protected] }
33144ef6443cSMatthias Ringwald /**
33154ef6443cSMatthias Ringwald  * @brief Get bondable mode.
33164ef6443cSMatthias Ringwald  * @return 1 if bondable
33174ef6443cSMatthias Ringwald  */
33184ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){
33194ef6443cSMatthias Ringwald     return hci_stack->bondable;
33204ef6443cSMatthias Ringwald }
3321cb230b9dS[email protected] 
3322cb230b9dS[email protected] /**
332334d2123cS[email protected]  * @brief map link keys to security levels
3324cb230b9dS[email protected]  */
332534d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
332634d2123cS[email protected]     switch (link_key_type){
33273c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
33283c68dfa9S[email protected]             return LEVEL_4;
33293c68dfa9S[email protected]         case COMBINATION_KEY:
33303c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
33313c68dfa9S[email protected]             return LEVEL_3;
33323c68dfa9S[email protected]         default:
33333c68dfa9S[email protected]             return LEVEL_2;
33343c68dfa9S[email protected]     }
3335cb230b9dS[email protected] }
3336cb230b9dS[email protected] 
3337106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
33385127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
3339106d6d11S[email protected]     return level > LEVEL_2;
3340106d6d11S[email protected] }
3341106d6d11S[email protected] 
334234d2123cS[email protected] /**
334334d2123cS[email protected]  * @brief get current security level
334434d2123cS[email protected]  */
334534d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
334634d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
334734d2123cS[email protected]     if (!connection) return LEVEL_0;
334834d2123cS[email protected]     return gap_security_level_for_connection(connection);
334934d2123cS[email protected] }
335034d2123cS[email protected] 
3351cb230b9dS[email protected] /**
3352cb230b9dS[email protected]  * @brief request connection to device to
3353cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
3354cb230b9dS[email protected]  */
335534d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
335634d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
335734d2123cS[email protected]     if (!connection){
3358a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
335934d2123cS[email protected]         return;
336034d2123cS[email protected]     }
336134d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
336234d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
336334d2123cS[email protected]     if (current_level >= requested_level){
3364a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
336534d2123cS[email protected]         return;
336634d2123cS[email protected]     }
3367a00031e2S[email protected] 
336834d2123cS[email protected]     connection->requested_security_level = requested_level;
3369a00031e2S[email protected] 
337025bf5872S[email protected] #if 0
337125bf5872S[email protected]     // sending encryption request without a link key results in an error.
337225bf5872S[email protected]     // TODO: figure out how to use it properly
337325bf5872S[email protected] 
3374fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
3375a98592bcSMatthias Ringwald     if (hci_stack->link_key_db){
3376a00031e2S[email protected]         link_key_type_t link_key_type;
3377a00031e2S[email protected]         link_key_t      link_key;
3378a98592bcSMatthias Ringwald         if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){
3379a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
3380a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
3381a00031e2S[email protected]                 return;
3382a00031e2S[email protected]             }
3383a00031e2S[email protected]         }
3384a00031e2S[email protected]     }
338525bf5872S[email protected] #endif
3386a00031e2S[email protected] 
33871eb2563eS[email protected]     // try to authenticate connection
33881eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
3389e80b2cf9S[email protected]     hci_run();
3390e00caf9cS[email protected] }
3391ad83dc6aS[email protected] 
3392ad83dc6aS[email protected] /**
3393ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
3394ad83dc6aS[email protected]  * @param device
3395ad83dc6aS[email protected]  * @param request MITM protection
3396ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
3397ad83dc6aS[email protected]  */
3398ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
3399ad83dc6aS[email protected] 
3400ad83dc6aS[email protected]     // create connection state machine
340196a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
3402ad83dc6aS[email protected] 
3403ad83dc6aS[email protected]     if (!connection){
3404ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
3405ad83dc6aS[email protected]     }
3406ad83dc6aS[email protected] 
3407ad83dc6aS[email protected]     // delete linkn key
340815a95bd5SMatthias Ringwald     gap_drop_link_key_for_bd_addr(device);
3409ad83dc6aS[email protected] 
3410ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
3411ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
3412ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
3413f04a0c31SMatthias Ringwald     log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level);
3414ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
3415ad83dc6aS[email protected] 
3416ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
3417ad83dc6aS[email protected] 
3418ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
3419ad83dc6aS[email protected]     // handle: authentication failure
3420ad83dc6aS[email protected]     // handle: disconnect on done
3421ad83dc6aS[email protected] 
3422ad83dc6aS[email protected]     hci_run();
3423ad83dc6aS[email protected] 
3424ad83dc6aS[email protected]     return 0;
3425ad83dc6aS[email protected] }
342635454696SMatthias Ringwald #endif
34278e618f72S[email protected] 
34288e618f72S[email protected] void gap_set_local_name(const char * local_name){
34298e618f72S[email protected]     hci_stack->local_name = local_name;
34308e618f72S[email protected] }
34318e618f72S[email protected] 
343235454696SMatthias Ringwald 
343335454696SMatthias Ringwald #ifdef ENABLE_BLE
343435454696SMatthias Ringwald 
3435d8e8f12aSMatthias Ringwald void gap_start_scan(void){
3436d8e8f12aSMatthias Ringwald     if (hci_stack->le_scanning_state == LE_SCANNING) return;
34377bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
34387bdc6798S[email protected]     hci_run();
34397bdc6798S[email protected] }
34408e618f72S[email protected] 
3441d8e8f12aSMatthias Ringwald void gap_stop_scan(void){
3442d8e8f12aSMatthias Ringwald     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return;
34437bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
34447bdc6798S[email protected]     hci_run();
34457bdc6798S[email protected] }
34464f3229d8S[email protected] 
3447d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
3448ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
3449ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
3450ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
3451ef11999fSmatthias.ringwald     hci_run();
3452ef11999fSmatthias.ringwald }
34534f3229d8S[email protected] 
3454d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){
34554f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
34564f3229d8S[email protected]     if (!conn){
3457d8e8f12aSMatthias Ringwald         log_info("gap_connect: no connection exists yet, creating context");
34582e77e513S[email protected]         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
34594f3229d8S[email protected]         if (!conn){
34604f3229d8S[email protected]             // notify client that alloc failed
34616e2e9a6bS[email protected]             hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
3462d8e8f12aSMatthias Ringwald             log_info("gap_connect: failed to alloc hci_connection_t");
3463472a5742SMatthias Ringwald             return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller
34644f3229d8S[email protected]         }
34654f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
3466d8e8f12aSMatthias Ringwald         log_info("gap_connect: send create connection next");
3467564fca32S[email protected]         hci_run();
3468616edd56SMatthias Ringwald         return 0;
34694f3229d8S[email protected]     }
34700bf6344aS[email protected] 
34710bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
34720bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
34730bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
34742e77e513S[email protected]         hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED);
3475d8e8f12aSMatthias Ringwald         log_error("gap_connect: classic connection or connect is already being created");
3476616edd56SMatthias Ringwald         return GATT_CLIENT_IN_WRONG_STATE;
34770bf6344aS[email protected]     }
34780bf6344aS[email protected] 
3479d8e8f12aSMatthias Ringwald     log_info("gap_connect: context exists with state %u", conn->state);
34802e77e513S[email protected]     hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0);
34814f3229d8S[email protected]     hci_run();
3482616edd56SMatthias Ringwald     return 0;
34834f3229d8S[email protected] }
34844f3229d8S[email protected] 
34857851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
3486d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){
3487665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
3488665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
34890bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
34900bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
34910bf6344aS[email protected]         switch (conn->state){
3492a6725849S[email protected]             case SEND_CREATE_CONNECTION:
34937851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
34947851196eSmatthias.ringwald                 return conn;
34957851196eSmatthias.ringwald             default:
34967851196eSmatthias.ringwald                 break;
34977851196eSmatthias.ringwald         };
34987851196eSmatthias.ringwald     }
34997851196eSmatthias.ringwald     return NULL;
35007851196eSmatthias.ringwald }
35017851196eSmatthias.ringwald 
3502d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){
3503d8e8f12aSMatthias Ringwald     hci_connection_t * conn = gap_get_outgoing_connection();
3504616edd56SMatthias Ringwald     if (!conn) return 0;
35057851196eSmatthias.ringwald     switch (conn->state){
35067851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
35077851196eSmatthias.ringwald             // skip sending create connection and emit event instead
35082e77e513S[email protected]             hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
3509665d90f2SMatthias Ringwald             btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn);
35107851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
35110bf6344aS[email protected]             break;
3512a6725849S[email protected]         case SENT_CREATE_CONNECTION:
35137851196eSmatthias.ringwald             // request to send cancel connection
35140bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
35150bf6344aS[email protected]             hci_run();
35160bf6344aS[email protected]             break;
35170bf6344aS[email protected]         default:
35180bf6344aS[email protected]             break;
35190bf6344aS[email protected]     }
3520616edd56SMatthias Ringwald     return 0;
3521e31f89a7S[email protected] }
35224f3229d8S[email protected] 
3523c37a3166S[email protected] /**
3524c37a3166S[email protected]  * @brief Updates the connection parameters for a given LE connection
3525c37a3166S[email protected]  * @param handle
3526c37a3166S[email protected]  * @param conn_interval_min (unit: 1.25ms)
3527c37a3166S[email protected]  * @param conn_interval_max (unit: 1.25ms)
3528c37a3166S[email protected]  * @param conn_latency
3529c37a3166S[email protected]  * @param supervision_timeout (unit: 10ms)
3530c37a3166S[email protected]  * @returns 0 if ok
3531c37a3166S[email protected]  */
3532c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
3533c37a3166S[email protected]     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
3534c37a3166S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3535c37a3166S[email protected]     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3536c37a3166S[email protected]     connection->le_conn_interval_min = conn_interval_min;
3537c37a3166S[email protected]     connection->le_conn_interval_max = conn_interval_max;
3538c37a3166S[email protected]     connection->le_conn_latency = conn_latency;
3539c37a3166S[email protected]     connection->le_supervision_timeout = supervision_timeout;
354084cf6d83SMatthias Ringwald     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
3541cfc59f1bSMatthias Ringwald     hci_run();
3542c37a3166S[email protected]     return 0;
3543c37a3166S[email protected] }
3544c37a3166S[email protected] 
354545c102fdSMatthias Ringwald /**
3546b68d7bc3SMatthias Ringwald  * @brief Request an update of the connection parameter for a given LE connection
3547b68d7bc3SMatthias Ringwald  * @param handle
3548b68d7bc3SMatthias Ringwald  * @param conn_interval_min (unit: 1.25ms)
3549b68d7bc3SMatthias Ringwald  * @param conn_interval_max (unit: 1.25ms)
3550b68d7bc3SMatthias Ringwald  * @param conn_latency
3551b68d7bc3SMatthias Ringwald  * @param supervision_timeout (unit: 10ms)
3552b68d7bc3SMatthias Ringwald  * @returns 0 if ok
3553b68d7bc3SMatthias Ringwald  */
3554b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
3555b68d7bc3SMatthias Ringwald     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
3556b68d7bc3SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
3557b68d7bc3SMatthias Ringwald     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3558b68d7bc3SMatthias Ringwald     connection->le_conn_interval_min = conn_interval_min;
3559b68d7bc3SMatthias Ringwald     connection->le_conn_interval_max = conn_interval_max;
3560b68d7bc3SMatthias Ringwald     connection->le_conn_latency = conn_latency;
3561b68d7bc3SMatthias Ringwald     connection->le_supervision_timeout = supervision_timeout;
3562b68d7bc3SMatthias Ringwald     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST;
3563b68d7bc3SMatthias Ringwald     hci_run();
3564b68d7bc3SMatthias Ringwald     return 0;
3565b68d7bc3SMatthias Ringwald }
3566b68d7bc3SMatthias Ringwald 
3567501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){
3568501f56b3SMatthias Ringwald     // disable advertisements before updating adv, scan data, or adv params
3569501f56b3SMatthias Ringwald     if (hci_stack->le_advertisements_active){
3570501f56b3SMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE;
3571501f56b3SMatthias Ringwald     }
3572501f56b3SMatthias Ringwald     hci_run();
3573501f56b3SMatthias Ringwald }
3574501f56b3SMatthias Ringwald 
3575b68d7bc3SMatthias Ringwald /**
357645c102fdSMatthias Ringwald  * @brief Set Advertisement Data
357745c102fdSMatthias Ringwald  * @param advertising_data_length
357845c102fdSMatthias Ringwald  * @param advertising_data (max 31 octets)
357945c102fdSMatthias Ringwald  * @note data is not copied, pointer has to stay valid
358045c102fdSMatthias Ringwald  */
358145c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){
358245c102fdSMatthias Ringwald     hci_stack->le_advertisements_data_len = advertising_data_length;
358345c102fdSMatthias Ringwald     hci_stack->le_advertisements_data = advertising_data;
3584501f56b3SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA;
3585501f56b3SMatthias Ringwald     gap_advertisments_changed();
358645c102fdSMatthias Ringwald }
3587501f56b3SMatthias Ringwald 
3588501f56b3SMatthias Ringwald /**
3589501f56b3SMatthias Ringwald  * @brief Set Scan Response Data
3590501f56b3SMatthias Ringwald  * @param advertising_data_length
3591501f56b3SMatthias Ringwald  * @param advertising_data (max 31 octets)
3592501f56b3SMatthias Ringwald  * @note data is not copied, pointer has to stay valid
3593501f56b3SMatthias Ringwald  */
3594501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){
3595501f56b3SMatthias Ringwald     hci_stack->le_scan_response_data_len = scan_response_data_length;
3596501f56b3SMatthias Ringwald     hci_stack->le_scan_response_data = scan_response_data;
3597501f56b3SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA;
3598501f56b3SMatthias Ringwald     gap_advertisments_changed();
359945c102fdSMatthias Ringwald }
360045c102fdSMatthias Ringwald 
360145c102fdSMatthias Ringwald /**
360245c102fdSMatthias Ringwald  * @brief Set Advertisement Parameters
360345c102fdSMatthias Ringwald  * @param adv_int_min
360445c102fdSMatthias Ringwald  * @param adv_int_max
360545c102fdSMatthias Ringwald  * @param adv_type
360645c102fdSMatthias Ringwald  * @param own_address_type
360745c102fdSMatthias Ringwald  * @param direct_address_type
360845c102fdSMatthias Ringwald  * @param direct_address
360945c102fdSMatthias Ringwald  * @param channel_map
361045c102fdSMatthias Ringwald  * @param filter_policy
361145c102fdSMatthias Ringwald  *
361245c102fdSMatthias Ringwald  * @note internal use. use gap_advertisements_set_params from gap_le.h instead.
361345c102fdSMatthias Ringwald  */
361445c102fdSMatthias Ringwald  void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
361545c102fdSMatthias Ringwald     uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address,
361645c102fdSMatthias Ringwald     uint8_t channel_map, uint8_t filter_policy) {
361745c102fdSMatthias Ringwald 
361845c102fdSMatthias Ringwald     hci_stack->le_advertisements_interval_min = adv_int_min;
361945c102fdSMatthias Ringwald     hci_stack->le_advertisements_interval_max = adv_int_max;
362045c102fdSMatthias Ringwald     hci_stack->le_advertisements_type = adv_type;
362145c102fdSMatthias Ringwald     hci_stack->le_advertisements_own_address_type = own_address_type;
362245c102fdSMatthias Ringwald     hci_stack->le_advertisements_direct_address_type = direct_address_typ;
362345c102fdSMatthias Ringwald     hci_stack->le_advertisements_channel_map = channel_map;
362445c102fdSMatthias Ringwald     hci_stack->le_advertisements_filter_policy = filter_policy;
362545c102fdSMatthias Ringwald     memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6);
362645c102fdSMatthias Ringwald 
362745c102fdSMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS;
3628501f56b3SMatthias Ringwald     gap_advertisments_changed();
362945c102fdSMatthias Ringwald  }
363045c102fdSMatthias Ringwald 
363133373e40SMatthias Ringwald void hci_le_advertisements_set_own_address_type(uint8_t own_address_type){
363233373e40SMatthias Ringwald     hci_stack->le_advertisements_own_address_type = own_address_type;
363333373e40SMatthias Ringwald     hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS;
363433373e40SMatthias Ringwald     gap_advertisments_changed();
363533373e40SMatthias Ringwald }
363633373e40SMatthias Ringwald 
363745c102fdSMatthias Ringwald /**
363845c102fdSMatthias Ringwald  * @brief Enable/Disable Advertisements
363945c102fdSMatthias Ringwald  * @param enabled
364045c102fdSMatthias Ringwald  */
364145c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){
364245c102fdSMatthias Ringwald     hci_stack->le_advertisements_enabled = enabled;
364345c102fdSMatthias Ringwald     if (enabled && !hci_stack->le_advertisements_active){
364445c102fdSMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE;
364545c102fdSMatthias Ringwald     }
364645c102fdSMatthias Ringwald     if (!enabled && hci_stack->le_advertisements_active){
364745c102fdSMatthias Ringwald         hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE;
364845c102fdSMatthias Ringwald     }
3649cfc59f1bSMatthias Ringwald     hci_run();
365045c102fdSMatthias Ringwald }
365145c102fdSMatthias Ringwald 
365235454696SMatthias Ringwald #endif
365345c102fdSMatthias Ringwald 
3654616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){
36555917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
36565917a5c5S[email protected]     if (!conn){
36577851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
3658616edd56SMatthias Ringwald         return 0;
36595917a5c5S[email protected]     }
36605917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
36615917a5c5S[email protected]     hci_run();
3662616edd56SMatthias Ringwald     return 0;
36634f3229d8S[email protected] }
366404a6ef8cSmatthias.ringwald 
3665a1bf5ae7SMatthias Ringwald /**
3666a1bf5ae7SMatthias Ringwald  * @brief Get connection type
3667a1bf5ae7SMatthias Ringwald  * @param con_handle
3668a1bf5ae7SMatthias Ringwald  * @result connection_type
3669a1bf5ae7SMatthias Ringwald  */
3670a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){
3671a1bf5ae7SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_handle(connection_handle);
3672a1bf5ae7SMatthias Ringwald     if (!conn) return GAP_CONNECTION_INVALID;
3673a1bf5ae7SMatthias Ringwald     switch (conn->address_type){
3674a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_LE_PUBLIC:
3675a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_LE_RANDOM:
3676a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_LE;
3677a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_SCO:
3678a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_SCO;
3679a1bf5ae7SMatthias Ringwald         case BD_ADDR_TYPE_CLASSIC:
3680a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_ACL;
3681a1bf5ae7SMatthias Ringwald         default:
3682a1bf5ae7SMatthias Ringwald             return GAP_CONNECTION_INVALID;
3683a1bf5ae7SMatthias Ringwald     }
3684a1bf5ae7SMatthias Ringwald }
3685a1bf5ae7SMatthias Ringwald 
3686a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
36874f551432SMatthias Ringwald 
3688d23838ecSMatthias Ringwald /**
3689ac9c45e0SMatthias Ringwald  * @brief Auto Connection Establishment - Start Connecting to device
3690ac9c45e0SMatthias Ringwald  * @param address_typ
3691ac9c45e0SMatthias Ringwald  * @param address
3692ac9c45e0SMatthias Ringwald  * @returns 0 if ok
3693ac9c45e0SMatthias Ringwald  */
36944f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){
3695e83201bcSMatthias Ringwald     // check capacity
3696665d90f2SMatthias Ringwald     int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist);
369791915b0bSMatthias Ringwald     if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
3698e83201bcSMatthias Ringwald     whitelist_entry_t * entry = btstack_memory_whitelist_entry_get();
3699e83201bcSMatthias Ringwald     if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED;
3700e83201bcSMatthias Ringwald     entry->address_type = address_type;
3701e83201bcSMatthias Ringwald     memcpy(entry->address, address, 6);
3702e83201bcSMatthias Ringwald     entry->state = LE_WHITELIST_ADD_TO_CONTROLLER;
3703665d90f2SMatthias Ringwald     btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry);
3704e83201bcSMatthias Ringwald     hci_run();
3705e83201bcSMatthias Ringwald     return 0;
3706ac9c45e0SMatthias Ringwald }
3707ac9c45e0SMatthias Ringwald 
370842ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){
3709665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3710665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist);
3711665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3712665d90f2SMatthias Ringwald         whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it);
3713e83201bcSMatthias Ringwald         if (entry->address_type != address_type) continue;
3714e83201bcSMatthias Ringwald         if (memcmp(entry->address, address, 6) != 0) continue;
3715e83201bcSMatthias Ringwald         if (entry->state & LE_WHITELIST_ON_CONTROLLER){
3716e83201bcSMatthias Ringwald             // remove from controller if already present
3717e83201bcSMatthias Ringwald             entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER;
3718e83201bcSMatthias Ringwald             continue;
3719e83201bcSMatthias Ringwald         }
3720e83201bcSMatthias Ringwald         // direclty remove entry from whitelist
3721665d90f2SMatthias Ringwald         btstack_linked_list_iterator_remove(&it);
3722e83201bcSMatthias Ringwald         btstack_memory_whitelist_entry_free(entry);
3723e83201bcSMatthias Ringwald     }
372442ff5ba1SMatthias Ringwald }
372542ff5ba1SMatthias Ringwald 
372642ff5ba1SMatthias Ringwald /**
372742ff5ba1SMatthias Ringwald  * @brief Auto Connection Establishment - Stop Connecting to device
372842ff5ba1SMatthias Ringwald  * @param address_typ
372942ff5ba1SMatthias Ringwald  * @param address
373042ff5ba1SMatthias Ringwald  * @returns 0 if ok
373142ff5ba1SMatthias Ringwald  */
373242ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){
373342ff5ba1SMatthias Ringwald     hci_remove_from_whitelist(address_type, address);
3734e83201bcSMatthias Ringwald     hci_run();
3735e83201bcSMatthias Ringwald     return 0;
3736ac9c45e0SMatthias Ringwald }
3737ac9c45e0SMatthias Ringwald 
3738ac9c45e0SMatthias Ringwald /**
3739ac9c45e0SMatthias Ringwald  * @brief Auto Connection Establishment - Stop everything
3740ac9c45e0SMatthias Ringwald  * @note  Convenience function to stop all active auto connection attempts
3741ac9c45e0SMatthias Ringwald  */
3742ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){
3743665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3744665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist);
3745665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3746665d90f2SMatthias Ringwald         whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it);
3747e83201bcSMatthias Ringwald         if (entry->state & LE_WHITELIST_ON_CONTROLLER){
3748e83201bcSMatthias Ringwald             // remove from controller if already present
3749e83201bcSMatthias Ringwald             entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER;
3750e83201bcSMatthias Ringwald             continue;
3751e83201bcSMatthias Ringwald         }
375291915b0bSMatthias Ringwald         // directly remove entry from whitelist
3753665d90f2SMatthias Ringwald         btstack_linked_list_iterator_remove(&it);
3754e83201bcSMatthias Ringwald         btstack_memory_whitelist_entry_free(entry);
3755e83201bcSMatthias Ringwald     }
3756e83201bcSMatthias Ringwald     hci_run();
3757ac9c45e0SMatthias Ringwald }
3758ac9c45e0SMatthias Ringwald 
37594f551432SMatthias Ringwald #endif
37604f551432SMatthias Ringwald 
376135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
3762ac9c45e0SMatthias Ringwald /**
3763ff00ed1cSMatthias Ringwald  * @brief Set Extended Inquiry Response data
3764ff00ed1cSMatthias Ringwald  * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup
3765ff00ed1cSMatthias Ringwald  * @note has to be done before stack starts up
3766ff00ed1cSMatthias Ringwald  */
3767ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){
3768ff00ed1cSMatthias Ringwald     hci_stack->eir_data = data;
3769ff00ed1cSMatthias Ringwald }
3770ff00ed1cSMatthias Ringwald 
3771ff00ed1cSMatthias Ringwald /**
3772f6858d14SMatthias Ringwald  * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on.
3773f6858d14SMatthias Ringwald  * @param inquriy_mode see bluetooth_defines.h
3774f6858d14SMatthias Ringwald  */
3775f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){
3776f6858d14SMatthias Ringwald     hci_stack->inquiry_mode = mode;
3777f6858d14SMatthias Ringwald }
3778f6858d14SMatthias Ringwald 
3779f6858d14SMatthias Ringwald /**
3780d950d659SMatthias Ringwald  * @brief Configure Voice Setting for use with SCO data in HSP/HFP
3781d950d659SMatthias Ringwald  */
3782d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){
3783d950d659SMatthias Ringwald     hci_stack->sco_voice_setting = voice_setting;
3784d950d659SMatthias Ringwald }
3785d950d659SMatthias Ringwald 
3786d950d659SMatthias Ringwald /**
3787d950d659SMatthias Ringwald  * @brief Get SCO Voice Setting
3788d950d659SMatthias Ringwald  * @return current voice setting
3789d950d659SMatthias Ringwald  */
37900cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){
3791d950d659SMatthias Ringwald     return hci_stack->sco_voice_setting;
3792d950d659SMatthias Ringwald }
3793d950d659SMatthias Ringwald 
3794b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting
3795b3aad8daSMatthias Ringwald  *  @note  Using SCO packets of the exact length is required for USB transfer
3796b3aad8daSMatthias Ringwald  *  @return Length of SCO packets in bytes (not audio frames)
3797b3aad8daSMatthias Ringwald  */
3798b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){
3799b3aad8daSMatthias Ringwald     // see Core Spec for H2 USB Transfer.
3800b3aad8daSMatthias Ringwald     if (hci_stack->sco_voice_setting & 0x0020) return 51;
3801b3aad8daSMatthias Ringwald     return 27;
3802b3aad8daSMatthias Ringwald }
380335454696SMatthias Ringwald #endif
3804b3aad8daSMatthias Ringwald 
3805d950d659SMatthias Ringwald /**
3806d23838ecSMatthias Ringwald  * @brief Set callback for Bluetooth Hardware Error
3807d23838ecSMatthias Ringwald  */
3808c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){
3809d23838ecSMatthias Ringwald     hci_stack->hardware_error_callback = fn;
3810d23838ecSMatthias Ringwald }
3811d23838ecSMatthias Ringwald 
381271de195eSMatthias Ringwald void hci_disconnect_all(void){
3813665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
3814665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hci_stack->connections);
3815665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3816665d90f2SMatthias Ringwald         hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it);
381704a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
381804a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
381904a6ef8cSmatthias.ringwald     }
3820d31fba26S[email protected]     hci_run();
382104a6ef8cSmatthias.ringwald }
382233373e40SMatthias Ringwald 
382333373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){
382433373e40SMatthias Ringwald     return hci_stack->manufacturer;
382533373e40SMatthias Ringwald }
3826