xref: /btstack/src/hci.c (revision a828a7565a062d3130d2e911dfe30ffeb2c7383d)
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 
45bde315ceS[email protected] #include "btstack-config.h"
4628171530Smatthias.ringwald 
477f2435e6Smatthias.ringwald #include "hci.h"
484c57c146S[email protected] #include "gap.h"
497f2435e6Smatthias.ringwald 
5093b8dc03Smatthias.ringwald #include <stdarg.h>
5193b8dc03Smatthias.ringwald #include <string.h>
5256fe0872Smatthias.ringwald #include <stdio.h>
537f2435e6Smatthias.ringwald 
54549e6ebeSmatthias.ringwald #ifndef EMBEDDED
55fe475138S[email protected] #ifdef _WIN32
56fe475138S[email protected] #include "Winsock2.h"
57fe475138S[email protected] #else
58549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
59fe475138S[email protected] #endif
6009ba8edeSmatthias.ringwald #include <btstack/version.h>
61549e6ebeSmatthias.ringwald #endif
62549e6ebeSmatthias.ringwald 
63a3b02b71Smatthias.ringwald #include "btstack_memory.h"
647f2435e6Smatthias.ringwald #include "debug.h"
65d8905019Smatthias.ringwald #include "hci_dump.h"
6693b8dc03Smatthias.ringwald 
67da886c03S[email protected] #include <btstack/linked_list.h>
68ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
691b0e3922Smatthias.ringwald 
70169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
71ee091cf1Smatthias.ringwald 
7228171530Smatthias.ringwald #ifdef USE_BLUETOOL
733d7a3f67S[email protected] #include "../platforms/ios/src/bt_control_iphone.h"
7428171530Smatthias.ringwald #endif
7528171530Smatthias.ringwald 
76758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
77a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
7896a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer);
7996a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
807586ee35S[email protected] static int  hci_power_control_on(void);
817586ee35S[email protected] static void hci_power_control_off(void);
826da48142SSean Wilson static void hci_state_reset(void);
83758b46ceSmatthias.ringwald 
8406b35ec0Smatthias.ringwald // the STACK is here
853a9fb326S[email protected] #ifndef HAVE_MALLOC
863a9fb326S[email protected] static hci_stack_t   hci_stack_static;
873a9fb326S[email protected] #endif
883a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8916833f0aSmatthias.ringwald 
9066fb9560S[email protected] // test helper
9166fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
9266fb9560S[email protected] 
9396a45072S[email protected] /**
9496a45072S[email protected]  * create connection for given address
9596a45072S[email protected]  *
9696a45072S[email protected]  * @return connection OR NULL, if no memory left
9796a45072S[email protected]  */
9896a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
991a06f663S[email protected]     log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type);
100bb69aaaeS[email protected]     hci_connection_t * conn = btstack_memory_hci_connection_get();
10196a45072S[email protected]     if (!conn) return NULL;
102c91d150bS[email protected]     memset(conn, 0, sizeof(hci_connection_t));
10396a45072S[email protected]     BD_ADDR_COPY(conn->address, addr);
10496a45072S[email protected]     conn->address_type = addr_type;
10596a45072S[email protected]     conn->con_handle = 0xffff;
10696a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
10796a45072S[email protected]     conn->bonding_flags = 0;
10896a45072S[email protected]     conn->requested_security_level = LEVEL_0;
10996a45072S[email protected]     linked_item_set_user(&conn->timeout.item, conn);
11096a45072S[email protected]     conn->timeout.process = hci_connection_timeout_handler;
11196a45072S[email protected]     hci_connection_timestamp(conn);
11296a45072S[email protected]     conn->acl_recombination_length = 0;
11396a45072S[email protected]     conn->acl_recombination_pos = 0;
11496a45072S[email protected]     conn->num_acl_packets_sent = 0;
1151a06f663S[email protected]     conn->num_sco_packets_sent = 0;
116da886c03S[email protected]     conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
11796a45072S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
11896a45072S[email protected]     return conn;
11996a45072S[email protected] }
12066fb9560S[email protected] 
121da886c03S[email protected] 
122da886c03S[email protected] /**
123da886c03S[email protected]  * get le connection parameter range
124da886c03S[email protected] *
125da886c03S[email protected]  * @return le connection parameter range struct
126da886c03S[email protected]  */
12771de195eSMatthias Ringwald le_connection_parameter_range_t gap_le_get_connection_parameter_range(void){
128da886c03S[email protected]     return hci_stack->le_connection_parameter_range;
129da886c03S[email protected] }
130da886c03S[email protected] 
131da886c03S[email protected] /**
132da886c03S[email protected]  * set le connection parameter range
133da886c03S[email protected]  *
134da886c03S[email protected]  */
135da886c03S[email protected] 
136da886c03S[email protected] void gap_le_set_connection_parameter_range(le_connection_parameter_range_t range){
137da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_interval_min;
138da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_interval_max;
139da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_latency_min;
140da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_latency_max;
141da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_min = range.le_supervision_timeout_min;
142da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_max = range.le_supervision_timeout_max;
143da886c03S[email protected] }
144da886c03S[email protected] 
145da886c03S[email protected] /**
146da886c03S[email protected]  * get hci connections iterator
147da886c03S[email protected]  *
148da886c03S[email protected]  * @return hci connections iterator
149da886c03S[email protected]  */
150da886c03S[email protected] 
151da886c03S[email protected] void hci_connections_get_iterator(linked_list_iterator_t *it){
152da886c03S[email protected]     linked_list_iterator_init(it, &hci_stack->connections);
153da886c03S[email protected] }
154da886c03S[email protected] 
15597addcc5Smatthias.ringwald /**
156ee091cf1Smatthias.ringwald  * get connection for a given handle
157ee091cf1Smatthias.ringwald  *
158ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
159ee091cf1Smatthias.ringwald  */
1605061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
161da886c03S[email protected]     linked_list_iterator_t it;
162da886c03S[email protected]     linked_list_iterator_init(&it, &hci_stack->connections);
163da886c03S[email protected]     while (linked_list_iterator_has_next(&it)){
164da886c03S[email protected]         hci_connection_t * item = (hci_connection_t *) linked_list_iterator_next(&it);
1653ac2fe56S[email protected]         if ( item->con_handle == con_handle ) {
166da886c03S[email protected]             return item;
167ee091cf1Smatthias.ringwald         }
168ee091cf1Smatthias.ringwald     }
169ee091cf1Smatthias.ringwald     return NULL;
170ee091cf1Smatthias.ringwald }
171ee091cf1Smatthias.ringwald 
17296a45072S[email protected] /**
17396a45072S[email protected]  * get connection for given address
17496a45072S[email protected]  *
17596a45072S[email protected]  * @return connection OR NULL, if not found
17696a45072S[email protected]  */
1772e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t  addr, bd_addr_type_t addr_type){
178da886c03S[email protected]     linked_list_iterator_t it;
179da886c03S[email protected]     linked_list_iterator_init(&it, &hci_stack->connections);
180da886c03S[email protected]     while (linked_list_iterator_has_next(&it)){
181da886c03S[email protected]         hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it);
18296a45072S[email protected]         if (connection->address_type != addr_type)  continue;
18396a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
18462bda3fbS[email protected]         return connection;
18562bda3fbS[email protected]     }
18662bda3fbS[email protected]     return NULL;
18762bda3fbS[email protected] }
18862bda3fbS[email protected] 
1892b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
19028ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
191c785ef68Smatthias.ringwald #ifdef HAVE_TIME
192ee091cf1Smatthias.ringwald     struct timeval tv;
193ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
194c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
195ee091cf1Smatthias.ringwald         // connections might be timed out
196ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
197ee091cf1Smatthias.ringwald     }
1982b12a0b9Smatthias.ringwald #endif
199e5780900Smatthias.ringwald #ifdef HAVE_TICK
200c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
201c785ef68Smatthias.ringwald         // connections might be timed out
202c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
203c785ef68Smatthias.ringwald     }
204c785ef68Smatthias.ringwald #endif
205c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
206c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
207c785ef68Smatthias.ringwald }
208ee091cf1Smatthias.ringwald 
209ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
210c7492964Smatthias.ringwald #ifdef HAVE_TIME
211ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
212c7492964Smatthias.ringwald #endif
213e5780900Smatthias.ringwald #ifdef HAVE_TICK
214c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
215c785ef68Smatthias.ringwald #endif
216ee091cf1Smatthias.ringwald }
217ee091cf1Smatthias.ringwald 
21806b35ec0Smatthias.ringwald 
21928ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
22028ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
22128ca2b46S[email protected] }
22228ca2b46S[email protected] 
22328ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
22428ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
22528ca2b46S[email protected] }
22628ca2b46S[email protected] 
22728ca2b46S[email protected] 
22843bfb1bdSmatthias.ringwald /**
22980ca58a0Smatthias.ringwald  * add authentication flags and reset timer
23096a45072S[email protected]  * @note: assumes classic connection
2312e77e513S[email protected]  * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets
2327fde4af9Smatthias.ringwald  */
2337fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
2347fde4af9Smatthias.ringwald     bd_addr_t addr;
2352e77e513S[email protected]     bt_flip_addr(addr, bd_addr);
2362e77e513S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2377fde4af9Smatthias.ringwald     if (conn) {
23828ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
23980ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
2407fde4af9Smatthias.ringwald     }
2417fde4af9Smatthias.ringwald }
2427fde4af9Smatthias.ringwald 
24380ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2445061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
24580ca58a0Smatthias.ringwald     if (!conn) return 0;
2466724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2476724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2486724cd9eS[email protected]     return 0;
24980ca58a0Smatthias.ringwald }
25080ca58a0Smatthias.ringwald 
2512e77e513S[email protected] void hci_drop_link_key_for_bd_addr(bd_addr_t addr){
2523a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2533a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
254c12e46e7Smatthias.ringwald     }
255c12e46e7Smatthias.ringwald }
256c12e46e7Smatthias.ringwald 
2570bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){
2580bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2590bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2600bf6344aS[email protected] }
2610bf6344aS[email protected] 
2627fde4af9Smatthias.ringwald 
2637fde4af9Smatthias.ringwald /**
26443bfb1bdSmatthias.ringwald  * count connections
26543bfb1bdSmatthias.ringwald  */
26640d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
26756c253c9Smatthias.ringwald     int count = 0;
26843bfb1bdSmatthias.ringwald     linked_item_t *it;
2693a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
27043bfb1bdSmatthias.ringwald     return count;
27143bfb1bdSmatthias.ringwald }
272c8e4258aSmatthias.ringwald 
27397addcc5Smatthias.ringwald /**
274ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
27516833f0aSmatthias.ringwald  */
2762718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
27716833f0aSmatthias.ringwald }
27816833f0aSmatthias.ringwald 
279998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2805061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
281998906cdSmatthias.ringwald     if (!connection) {
2829da54300S[email protected]         log_error("hci_number_outgoing_packets: connection for handle %u does not exist!", handle);
283998906cdSmatthias.ringwald         return 0;
284998906cdSmatthias.ringwald     }
285998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
286998906cdSmatthias.ringwald }
287998906cdSmatthias.ringwald 
288e79abdd6S[email protected] uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){
289ee303eddS[email protected] 
290ee303eddS[email protected]     int num_packets_sent_classic = 0;
291ee303eddS[email protected]     int num_packets_sent_le = 0;
292ee303eddS[email protected] 
293ee303eddS[email protected]     bd_addr_type_t address_type = BD_ADDR_TYPE_UNKNOWN;
294ee303eddS[email protected] 
295998906cdSmatthias.ringwald     linked_item_t *it;
2963a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
297998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
298ee303eddS[email protected]         if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
299ee303eddS[email protected]             num_packets_sent_classic += connection->num_acl_packets_sent;
300ee303eddS[email protected]         } else {
301ee303eddS[email protected]             num_packets_sent_le += connection->num_acl_packets_sent;
302ee303eddS[email protected]         }
303ccda6e14S[email protected]         // ignore connections that are not open, e.g., in state RECEIVED_DISCONNECTION_COMPLETE
304ccda6e14S[email protected]         if (connection->con_handle == con_handle && connection->state == OPEN){
305ee303eddS[email protected]             address_type = connection->address_type;
306ee303eddS[email protected]         }
307ee303eddS[email protected]     }
308ee303eddS[email protected] 
309ee303eddS[email protected]     int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic;
310ee303eddS[email protected]     int free_slots_le = 0;
311ee303eddS[email protected] 
312ee303eddS[email protected]     if (free_slots_classic < 0){
3139da54300S[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);
314998906cdSmatthias.ringwald         return 0;
315998906cdSmatthias.ringwald     }
316ee303eddS[email protected] 
317ee303eddS[email protected]     if (hci_stack->le_acl_packets_total_num){
318ee303eddS[email protected]         // if we have LE slots, they are used
319ee303eddS[email protected]         free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le;
320ee303eddS[email protected]         if (free_slots_le < 0){
3219da54300S[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);
322ee303eddS[email protected]             return 0;
323998906cdSmatthias.ringwald         }
324ee303eddS[email protected]     } else {
325ee303eddS[email protected]         // otherwise, classic slots are used for LE, too
326ee303eddS[email protected]         free_slots_classic -= num_packets_sent_le;
327ee303eddS[email protected]         if (free_slots_classic < 0){
3289da54300S[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);
329ee303eddS[email protected]             return 0;
330ee303eddS[email protected]         }
331ee303eddS[email protected]     }
332ee303eddS[email protected] 
333ee303eddS[email protected]     switch (address_type){
334ee303eddS[email protected]         case BD_ADDR_TYPE_UNKNOWN:
3359da54300S[email protected]             log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle);
336ee303eddS[email protected]             return 0;
337ee303eddS[email protected] 
338ee303eddS[email protected]         case BD_ADDR_TYPE_CLASSIC:
339ee303eddS[email protected]             return free_slots_classic;
340ee303eddS[email protected] 
341ee303eddS[email protected]         default:
342cb00d3aaS[email protected]            if (hci_stack->le_acl_packets_total_num){
343ee303eddS[email protected]                return free_slots_le;
344ee303eddS[email protected]            }
345cb00d3aaS[email protected]            return free_slots_classic;
346cb00d3aaS[email protected]     }
347998906cdSmatthias.ringwald }
348998906cdSmatthias.ringwald 
34944d0e3d5S[email protected] int hci_number_free_sco_slots_for_handle(hci_con_handle_t handle){
350e35edcc1S[email protected]     int num_sco_packets_sent = 0;
351e35edcc1S[email protected]     linked_item_t *it;
352e35edcc1S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
353e35edcc1S[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
354e35edcc1S[email protected]         num_sco_packets_sent += connection->num_sco_packets_sent;
355e35edcc1S[email protected]     }
356e35edcc1S[email protected]     if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
3572b65942fSMatthias Ringwald         log_info("hci_number_free_sco_slots_for_handle: outgoing packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num);
35844d0e3d5S[email protected]         return 0;
35944d0e3d5S[email protected]     }
360e35edcc1S[email protected]     return hci_stack->sco_packets_total_num - num_sco_packets_sent;
361e35edcc1S[email protected] }
36244d0e3d5S[email protected] 
363ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
364ac928cc2S[email protected] int hci_can_send_command_packet_now(void){
365ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
366ac928cc2S[email protected] 
367ac928cc2S[email protected]     // check for async hci transport implementations
368ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
369ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
370ac928cc2S[email protected]             return 0;
371ac928cc2S[email protected]         }
372ac928cc2S[email protected]     }
373ac928cc2S[email protected] 
374ac928cc2S[email protected]     return hci_stack->num_cmd_packets > 0;
375ac928cc2S[email protected] }
376ac928cc2S[email protected] 
377ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
378ac928cc2S[email protected]     // check for async hci transport implementations
379ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
380ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){
381ac928cc2S[email protected]             return 0;
382ac928cc2S[email protected]         }
383ac928cc2S[email protected]     }
384e79abdd6S[email protected]     return hci_number_free_acl_slots_for_handle(con_handle) > 0;
385ac928cc2S[email protected] }
386ac928cc2S[email protected] 
387ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
388ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
389ac928cc2S[email protected]     return hci_can_send_prepared_acl_packet_now(con_handle);
3906b4af23dS[email protected] }
3916b4af23dS[email protected] 
39244d0e3d5S[email protected] int hci_can_send_prepared_sco_packet_now(hci_con_handle_t con_handle){
39344d0e3d5S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
39444d0e3d5S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_SCO_DATA_PACKET)){
39544d0e3d5S[email protected]             return 0;
39644d0e3d5S[email protected]         }
39744d0e3d5S[email protected]     }
39844d0e3d5S[email protected]     return hci_number_free_sco_slots_for_handle(con_handle) > 0;
39944d0e3d5S[email protected] }
40044d0e3d5S[email protected] 
40144d0e3d5S[email protected] int hci_can_send_sco_packet_now(hci_con_handle_t con_handle){
40244d0e3d5S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
40344d0e3d5S[email protected]     return hci_can_send_prepared_sco_packet_now(con_handle);
40444d0e3d5S[email protected] }
40544d0e3d5S[email protected] 
406c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
407c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
408c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
409c8b9416aS[email protected] }
410c8b9416aS[email protected] 
4116b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
4126b4af23dS[email protected] int hci_reserve_packet_buffer(void){
4139d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
4149d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
4159d14b626S[email protected]         return 0;
4169d14b626S[email protected]     }
4176b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
4186b4af23dS[email protected]     return 1;
4196b4af23dS[email protected] }
4206b4af23dS[email protected] 
42168a0fcf7S[email protected] void hci_release_packet_buffer(void){
42268a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
42368a0fcf7S[email protected] }
42468a0fcf7S[email protected] 
4256b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
4266b4af23dS[email protected] int hci_transport_synchronous(void){
4276b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
4286b4af23dS[email protected] }
4296b4af23dS[email protected] 
4306c26b087S[email protected] uint16_t hci_max_acl_le_data_packet_length(void){
4316c26b087S[email protected]     return hci_stack->le_data_packets_length > 0 ? hci_stack->le_data_packets_length : hci_stack->acl_data_packet_length;
4326c26b087S[email protected] }
4336c26b087S[email protected] 
434452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){
435452cf3bbS[email protected] 
436452cf3bbS[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);
437452cf3bbS[email protected] 
438452cf3bbS[email protected]     // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers
439452cf3bbS[email protected]     uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length;
440452cf3bbS[email protected]     if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){
441452cf3bbS[email protected]         max_acl_data_packet_length = hci_stack->le_data_packets_length;
442452cf3bbS[email protected]     }
443452cf3bbS[email protected] 
444452cf3bbS[email protected]     // testing: reduce buffer to minimum
445452cf3bbS[email protected]     // max_acl_data_packet_length = 52;
446452cf3bbS[email protected] 
447452cf3bbS[email protected]     int err;
448452cf3bbS[email protected]     // multiple packets could be send on a synchronous HCI transport
449452cf3bbS[email protected]     while (1){
450452cf3bbS[email protected] 
451452cf3bbS[email protected]         // get current data
452452cf3bbS[email protected]         const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4;
453452cf3bbS[email protected]         int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
454452cf3bbS[email protected]         int more_fragments = 0;
455452cf3bbS[email protected] 
456452cf3bbS[email protected]         // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
457452cf3bbS[email protected]         if (current_acl_data_packet_length > max_acl_data_packet_length){
458452cf3bbS[email protected]             more_fragments = 1;
459452cf3bbS[email protected]             current_acl_data_packet_length = max_acl_data_packet_length;
460452cf3bbS[email protected]         }
461452cf3bbS[email protected] 
462452cf3bbS[email protected]         // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent)
463452cf3bbS[email protected]         if (acl_header_pos > 0){
464452cf3bbS[email protected]             uint16_t handle_and_flags = READ_BT_16(hci_stack->hci_packet_buffer, 0);
465452cf3bbS[email protected]             handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12);
466452cf3bbS[email protected]             bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
467452cf3bbS[email protected]         }
468452cf3bbS[email protected] 
469452cf3bbS[email protected]         // update header len
470452cf3bbS[email protected]         bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length);
471452cf3bbS[email protected] 
472452cf3bbS[email protected]         // count packet
473452cf3bbS[email protected]         connection->num_acl_packets_sent++;
474452cf3bbS[email protected] 
475452cf3bbS[email protected]         // send packet
476452cf3bbS[email protected]         uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
477452cf3bbS[email protected]         const int size = current_acl_data_packet_length + 4;
4785bb5bc3eS[email protected]         hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
479452cf3bbS[email protected]         err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
480452cf3bbS[email protected] 
481452cf3bbS[email protected]         // done yet?
482452cf3bbS[email protected]         if (!more_fragments) break;
483452cf3bbS[email protected] 
484452cf3bbS[email protected]         // update start of next fragment to send
485452cf3bbS[email protected]         hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
486452cf3bbS[email protected] 
487452cf3bbS[email protected]         // can send more?
488452cf3bbS[email protected]         if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err;
489452cf3bbS[email protected]     }
490452cf3bbS[email protected] 
491452cf3bbS[email protected]     // done
492452cf3bbS[email protected]     hci_stack->acl_fragmentation_pos = 0;
493452cf3bbS[email protected]     hci_stack->acl_fragmentation_total_size = 0;
494452cf3bbS[email protected] 
495d051460cS[email protected]     // release buffer now for synchronous transport
496203bace6S[email protected]     if (hci_transport_synchronous()){
497452cf3bbS[email protected]         hci_release_packet_buffer();
498690bd0baS[email protected]         // notify upper stack that iit might be possible to send again
499690bd0baS[email protected]         uint8_t event[] = { DAEMON_EVENT_HCI_PACKET_SENT, 0};
500690bd0baS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
501452cf3bbS[email protected]     }
502452cf3bbS[email protected] 
503452cf3bbS[email protected]     return err;
504452cf3bbS[email protected] }
505452cf3bbS[email protected] 
506826f7347S[email protected] // pre: caller has reserved the packet buffer
507826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
5087856c818Smatthias.ringwald 
509452cf3bbS[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
510452cf3bbS[email protected] 
511826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
512826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
513826f7347S[email protected]         return 0;
514826f7347S[email protected]     }
515826f7347S[email protected] 
516d713a683S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
517d713a683S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
518d713a683S[email protected] 
519826f7347S[email protected]     // check for free places on Bluetooth module
520d713a683S[email protected]     if (!hci_can_send_prepared_acl_packet_now(con_handle)) {
521826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
52297b61c7bS[email protected]         hci_release_packet_buffer();
52397b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
52497b61c7bS[email protected]     }
5256218e6f1Smatthias.ringwald 
5265061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
52797b61c7bS[email protected]     if (!connection) {
5285fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
52997b61c7bS[email protected]         hci_release_packet_buffer();
53097b61c7bS[email protected]         return 0;
53197b61c7bS[email protected]     }
53256cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
53356cf178bSmatthias.ringwald 
534452cf3bbS[email protected]     // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size);
5357856c818Smatthias.ringwald 
536452cf3bbS[email protected]     // setup data
537452cf3bbS[email protected]     hci_stack->acl_fragmentation_total_size = size;
538452cf3bbS[email protected]     hci_stack->acl_fragmentation_pos = 4;   // start of L2CAP packet
5396218e6f1Smatthias.ringwald 
540452cf3bbS[email protected]     return hci_send_acl_packet_fragments(connection);
541ee091cf1Smatthias.ringwald }
542ee091cf1Smatthias.ringwald 
54344d0e3d5S[email protected] // pre: caller has reserved the packet buffer
54444d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){
54544d0e3d5S[email protected] 
54644d0e3d5S[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
54744d0e3d5S[email protected] 
54844d0e3d5S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
54944d0e3d5S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
55044d0e3d5S[email protected]         return 0;
55144d0e3d5S[email protected]     }
55244d0e3d5S[email protected] 
55344d0e3d5S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
55444d0e3d5S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);   // same for ACL and SCO
55544d0e3d5S[email protected] 
55644d0e3d5S[email protected]     // check for free places on Bluetooth module
55744d0e3d5S[email protected]     if (!hci_can_send_prepared_sco_packet_now(con_handle)) {
55844d0e3d5S[email protected]         log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller");
55944d0e3d5S[email protected]         hci_release_packet_buffer();
56044d0e3d5S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
56144d0e3d5S[email protected]     }
56244d0e3d5S[email protected] 
563e35edcc1S[email protected]     // track send packet in connection struct
564e35edcc1S[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
565e35edcc1S[email protected]     if (!connection) {
566e35edcc1S[email protected]         log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle);
567e35edcc1S[email protected]         hci_release_packet_buffer();
568e35edcc1S[email protected]         return 0;
569e35edcc1S[email protected]     }
570e35edcc1S[email protected]     connection->num_sco_packets_sent++;
57144d0e3d5S[email protected] 
57244d0e3d5S[email protected]     hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size);
57344d0e3d5S[email protected]     return hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size);
57444d0e3d5S[email protected] }
57544d0e3d5S[email protected] 
57616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
5777856c818Smatthias.ringwald 
578e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
579e76a89eeS[email protected] 
5807856c818Smatthias.ringwald     // get info
5817856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
5825061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
5837856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
5847856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
5857856c818Smatthias.ringwald 
5867856c818Smatthias.ringwald     // ignore non-registered handle
5877856c818Smatthias.ringwald     if (!conn){
5889da54300S[email protected]         log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle);
5897856c818Smatthias.ringwald         return;
5907856c818Smatthias.ringwald     }
5917856c818Smatthias.ringwald 
592e76a89eeS[email protected]     // assert packet is complete
5939ecc3e17S[email protected]     if (acl_length + 4 != size){
594e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
595e76a89eeS[email protected]         return;
596e76a89eeS[email protected]     }
597e76a89eeS[email protected] 
5987856c818Smatthias.ringwald     // update idle timestamp
5997856c818Smatthias.ringwald     hci_connection_timestamp(conn);
6007856c818Smatthias.ringwald 
6017856c818Smatthias.ringwald     // handle different packet types
6027856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
6037856c818Smatthias.ringwald 
6047856c818Smatthias.ringwald         case 0x01: // continuation fragment
6057856c818Smatthias.ringwald 
6060ca847afS[email protected]             // sanity checks
6077856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
6089da54300S[email protected]                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle);
6097856c818Smatthias.ringwald                 return;
6107856c818Smatthias.ringwald             }
6110ca847afS[email protected]             if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){
6120ca847afS[email protected]                 log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x",
6130ca847afS[email protected]                     conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
6140ca847afS[email protected]                 conn->acl_recombination_pos = 0;
6150ca847afS[email protected]                 return;
6160ca847afS[email protected]             }
6177856c818Smatthias.ringwald 
6187856c818Smatthias.ringwald             // append fragment payload (header already stored)
619ec6321eeS[email protected]             memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length );
6207856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
6217856c818Smatthias.ringwald 
6229da54300S[email protected]             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length,
623decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
6247856c818Smatthias.ringwald 
6257856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
6267856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
6277856c818Smatthias.ringwald 
628ec6321eeS[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, &conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos);
6297856c818Smatthias.ringwald                 // reset recombination buffer
6307856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
6317856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
6327856c818Smatthias.ringwald             }
6337856c818Smatthias.ringwald             break;
6347856c818Smatthias.ringwald 
6357856c818Smatthias.ringwald         case 0x02: { // first fragment
6367856c818Smatthias.ringwald 
63723a77e1aS[email protected]             // sanity check
63823a77e1aS[email protected]             if (conn->acl_recombination_pos) {
63923a77e1aS[email protected]                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle);
64023a77e1aS[email protected]                 conn->acl_recombination_pos = 0;
64123a77e1aS[email protected]             }
64223a77e1aS[email protected] 
6437856c818Smatthias.ringwald             // peek into L2CAP packet!
6447856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
6457856c818Smatthias.ringwald 
6469da54300S[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length);
647decc01a8Smatthias.ringwald 
6487856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
6497856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
6507856c818Smatthias.ringwald 
6517856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
6523a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
6537856c818Smatthias.ringwald 
6547856c818Smatthias.ringwald             } else {
6550ca847afS[email protected] 
6560ca847afS[email protected]                 if (acl_length > HCI_ACL_BUFFER_SIZE){
6570ca847afS[email protected]                     log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x",
6580ca847afS[email protected]                         4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
6590ca847afS[email protected]                     return;
6600ca847afS[email protected]                 }
6610ca847afS[email protected] 
6627856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
663ec6321eeS[email protected]                 memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4);
6647856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
6657856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
666ec6321eeS[email protected]                 bt_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4);
6677856c818Smatthias.ringwald             }
6687856c818Smatthias.ringwald             break;
6697856c818Smatthias.ringwald 
6707856c818Smatthias.ringwald         }
6717856c818Smatthias.ringwald         default:
6729da54300S[email protected]             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03);
6737856c818Smatthias.ringwald             return;
6747856c818Smatthias.ringwald     }
67594ab26f8Smatthias.ringwald 
67694ab26f8Smatthias.ringwald     // execute main loop
67794ab26f8Smatthias.ringwald     hci_run();
67816833f0aSmatthias.ringwald }
67922909952Smatthias.ringwald 
68067a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
6819da54300S[email protected]     log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address));
6823c4d4b90Smatthias.ringwald 
683c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
684c785ef68Smatthias.ringwald 
6853a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
686a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
6873c4d4b90Smatthias.ringwald 
6883c4d4b90Smatthias.ringwald     // now it's gone
689c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
690c7e0c5f6Smatthias.ringwald }
691c7e0c5f6Smatthias.ringwald 
6920c042179S[email protected] static const uint16_t packet_type_sizes[] = {
6938f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
6948f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
6958f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
6968f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
6978f8108aaSmatthias.ringwald };
69865389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
69965389bfcS[email protected]      0, // 3 slot packets
70065389bfcS[email protected]      1, // 5 slot packets
70165389bfcS[email protected]     25, // EDR 2 mpbs
70265389bfcS[email protected]     26, // EDR 3 mbps
70365389bfcS[email protected]     39, // 3 slot EDR packts
70465389bfcS[email protected]     40, // 5 slot EDR packet
70565389bfcS[email protected] };
70665389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
70765389bfcS[email protected]     0x0f00, // 3 slot packets
70865389bfcS[email protected]     0xf000, // 5 slot packets
70965389bfcS[email protected]     0x1102, // EDR 2 mpbs
71065389bfcS[email protected]     0x2204, // EDR 3 mbps
71165389bfcS[email protected]     0x0300, // 3 slot EDR packts
71265389bfcS[email protected]     0x3000, // 5 slot EDR packet
71365389bfcS[email protected] };
7148f8108aaSmatthias.ringwald 
71565389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
71665389bfcS[email protected]     // enable packet types based on size
7178f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
718f16a69bbS[email protected]     unsigned int i;
7198f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
7208f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
7218f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
7228f8108aaSmatthias.ringwald             packet_types |= 1 << i;
7238f8108aaSmatthias.ringwald         }
7248f8108aaSmatthias.ringwald     }
72565389bfcS[email protected]     // disable packet types due to missing local supported features
72665389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
72765389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
72865389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
72965389bfcS[email protected]         if (feature_set) continue;
73065389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
73165389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
73265389bfcS[email protected]     }
7338f8108aaSmatthias.ringwald     // flip bits for "may not be used"
7348f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
7358f8108aaSmatthias.ringwald     return packet_types;
7368f8108aaSmatthias.ringwald }
7378f8108aaSmatthias.ringwald 
7388f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
7393a9fb326S[email protected]     return hci_stack->packet_types;
7408f8108aaSmatthias.ringwald }
7418f8108aaSmatthias.ringwald 
742facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
7437dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
7443a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
7457dc17943Smatthias.ringwald }
7467dc17943Smatthias.ringwald 
747f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
7483a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
7497dc17943Smatthias.ringwald }
7507dc17943Smatthias.ringwald 
7516ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
7526ac9a97eS[email protected]     // No. 54, byte 6, bit 6
7536ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
7546ac9a97eS[email protected] }
7556ac9a97eS[email protected] 
756f5d8d141S[email protected] int hci_ssp_supported(void){
7576ac9a97eS[email protected]     // No. 51, byte 6, bit 3
7583a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
759f5d8d141S[email protected] }
760f5d8d141S[email protected] 
761f5d8d141S[email protected] int hci_classic_supported(void){
7626ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
7633a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
764f5d8d141S[email protected] }
765f5d8d141S[email protected] 
766f5d8d141S[email protected] int hci_le_supported(void){
767f5d8d141S[email protected] #ifdef HAVE_BLE
7686ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
7693a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
770f5d8d141S[email protected] #else
771f5d8d141S[email protected]     return 0;
772f5d8d141S[email protected] #endif
773f5d8d141S[email protected] }
774f5d8d141S[email protected] 
77569a97523S[email protected] // get addr type and address used in advertisement packets
7762e77e513S[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t  addr){
7773a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
7783a9fb326S[email protected]     if (hci_stack->adv_addr_type){
7793a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
78069a97523S[email protected]     } else {
7813a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
78269a97523S[email protected]     }
78369a97523S[email protected] }
78469a97523S[email protected] 
785b2f949feS[email protected] #ifdef HAVE_BLE
786e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){
787d1dc057bS[email protected]     int offset = 3;
788d1dc057bS[email protected]     int num_reports = packet[offset];
789d1dc057bS[email protected]     offset += 1;
790d1dc057bS[email protected] 
79157c9da5bS[email protected]     int i;
7924f4b43f3S[email protected]     log_info("HCI: handle adv report with num reports: %d", num_reports);
79303fbe9c6S[email protected]     uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
79457c9da5bS[email protected]     for (i=0; i<num_reports;i++){
795210c6774S[email protected]         uint8_t data_length = packet[offset + 8];
796a0aac9c4S[email protected]         uint8_t event_size = 10 + data_length;
797d1dc057bS[email protected]         int pos = 0;
7982b552b23S[email protected]         event[pos++] = GAP_LE_ADVERTISING_REPORT;
79957c9da5bS[email protected]         event[pos++] = event_size;
800210c6774S[email protected]         memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address
801d1dc057bS[email protected]         offset += 8;
802d1dc057bS[email protected]         pos += 8;
803d1dc057bS[email protected]         event[pos++] = packet[offset + 1 + data_length]; // rssi
804d1dc057bS[email protected]         event[pos++] = packet[offset++]; //data_length;
805d1dc057bS[email protected]         memcpy(&event[pos], &packet[offset], data_length);
80657c9da5bS[email protected]         pos += data_length;
807d1dc057bS[email protected]         offset += data_length + 1; // rssi
80803fbe9c6S[email protected]         hci_dump_packet( HCI_EVENT_PACKET, 0, event, pos);
80903fbe9c6S[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, pos);
81057c9da5bS[email protected]     }
81157c9da5bS[email protected] }
812b2f949feS[email protected] #endif
81357c9da5bS[email protected] 
8140305bdeaSMatthias Ringwald static void hci_initialization_timeout_handler(timer_source_t * ds){
8150305bdeaSMatthias Ringwald     switch (hci_stack->substate){
8160305bdeaSMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
8177b0d7667SMatthias Ringwald             log_info("Resend HCI Reset");
8180305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET;
8197b0d7667SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
8200305bdeaSMatthias Ringwald             hci_run();
8210305bdeaSMatthias Ringwald             break;
822e47e68c7SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
823e47e68c7SMatthias Ringwald             log_info("Resend HCI Reset - CSR Warm Boot");
824e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
825e47e68c7SMatthias Ringwald             hci_stack->num_cmd_packets = 1;
826e47e68c7SMatthias Ringwald             hci_run();
8274696bddbSMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
8284696bddbSMatthias Ringwald             log_info("Local baud rate change to %u", ((hci_uart_config_t *)hci_stack->config)->baudrate_main);
8294696bddbSMatthias Ringwald             hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
8304696bddbSMatthias Ringwald             break;
8310305bdeaSMatthias Ringwald         default:
8320305bdeaSMatthias Ringwald             break;
8330305bdeaSMatthias Ringwald     }
8340305bdeaSMatthias Ringwald }
8350305bdeaSMatthias Ringwald 
83671de195eSMatthias Ringwald static void hci_initializing_next_state(void){
83774b323a9SMatthias Ringwald     hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1);
83874b323a9SMatthias Ringwald }
83974b323a9SMatthias Ringwald 
84074b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true
84171de195eSMatthias Ringwald static void hci_initializing_run(void){
842db8bc6ffSMatthias Ringwald     log_info("hci_initializing_run: substate %u", hci_stack->substate);
84374b323a9SMatthias Ringwald     switch (hci_stack->substate){
84474b323a9SMatthias Ringwald         case HCI_INIT_SEND_RESET:
84574b323a9SMatthias Ringwald             hci_state_reset();
8460305bdeaSMatthias Ringwald             // prepare reset if command complete not received in 100ms
8470305bdeaSMatthias Ringwald             run_loop_set_timer(&hci_stack->timeout, 100);
8480305bdeaSMatthias Ringwald             run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
8490305bdeaSMatthias Ringwald             run_loop_add_timer(&hci_stack->timeout);
8500305bdeaSMatthias Ringwald             // send command
85174b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET;
8520305bdeaSMatthias Ringwald             hci_send_cmd(&hci_reset);
85374b323a9SMatthias Ringwald             break;
85476fcb19bSMatthias Ringwald         case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION:
85576fcb19bSMatthias Ringwald             hci_send_cmd(&hci_read_local_version_information);
85676fcb19bSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION;
85776fcb19bSMatthias Ringwald             break;
858e47e68c7SMatthias Ringwald         case HCI_INIT_SEND_RESET_CSR_WARM_BOOT:
859e47e68c7SMatthias Ringwald             hci_state_reset();
860e47e68c7SMatthias Ringwald             // prepare reset if command complete not received in 100ms
861e47e68c7SMatthias Ringwald             run_loop_set_timer(&hci_stack->timeout, 100);
862e47e68c7SMatthias Ringwald             run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
863e47e68c7SMatthias Ringwald             run_loop_add_timer(&hci_stack->timeout);
864e47e68c7SMatthias Ringwald             // send command
865e47e68c7SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
866e47e68c7SMatthias Ringwald             hci_send_cmd(&hci_reset);
867e47e68c7SMatthias Ringwald             break;
8688d29070eSMatthias Ringwald         case HCI_INIT_SEND_RESET_ST_WARM_BOOT:
8698d29070eSMatthias Ringwald             hci_state_reset();
8708d29070eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT;
8718d29070eSMatthias Ringwald             hci_send_cmd(&hci_reset);
8728d29070eSMatthias Ringwald             break;
8738d29070eSMatthias Ringwald         case HCI_INIT_SET_BD_ADDR:
8748d29070eSMatthias Ringwald             log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr));
8758d29070eSMatthias Ringwald             hci_stack->control->set_bd_addr_cmd(hci_stack->config, hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer);
8768d29070eSMatthias Ringwald             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
8778d29070eSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR;
8788d29070eSMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
8798d29070eSMatthias Ringwald             break;
88074b323a9SMatthias Ringwald         case HCI_INIT_SEND_BAUD_CHANGE:
88174b323a9SMatthias Ringwald             hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
88274b323a9SMatthias Ringwald             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
88374b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
8840305bdeaSMatthias Ringwald             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
8854696bddbSMatthias Ringwald             // STLC25000D: baudrate change happens within 0.5 s after command was send,
8864696bddbSMatthias Ringwald             // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial)
8874696bddbSMatthias Ringwald             if (hci_stack->manufacturer == 0x0030){
8884696bddbSMatthias Ringwald                 run_loop_set_timer(&hci_stack->timeout, 100);
8894696bddbSMatthias Ringwald                 run_loop_add_timer(&hci_stack->timeout);
8904696bddbSMatthias Ringwald             }
89174b323a9SMatthias Ringwald             break;
89274b323a9SMatthias Ringwald         case HCI_INIT_CUSTOM_INIT:
89374b323a9SMatthias Ringwald             log_info("Custom init");
89474b323a9SMatthias Ringwald             // Custom initialization
89574b323a9SMatthias Ringwald             if (hci_stack->control && hci_stack->control->next_cmd){
89674b323a9SMatthias Ringwald                 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
89774b323a9SMatthias Ringwald                 if (valid_cmd){
89874b323a9SMatthias Ringwald                     int size = 3 + hci_stack->hci_packet_buffer[2];
89974b323a9SMatthias Ringwald                     hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
90074b323a9SMatthias Ringwald                     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size);
901e47e68c7SMatthias Ringwald                     switch (valid_cmd) {
902e47e68c7SMatthias Ringwald                         case 1:
903e47e68c7SMatthias Ringwald                         default:
904e47e68c7SMatthias Ringwald                             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT;
905e47e68c7SMatthias Ringwald                             break;
906e47e68c7SMatthias Ringwald                         case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete
907e47e68c7SMatthias Ringwald                             log_info("CSR Warm Boot");
908e47e68c7SMatthias Ringwald                             run_loop_set_timer(&hci_stack->timeout, 100);
909e47e68c7SMatthias Ringwald                             run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
910e47e68c7SMatthias Ringwald                             run_loop_add_timer(&hci_stack->timeout);
911e47e68c7SMatthias Ringwald                             hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
912e47e68c7SMatthias Ringwald                             break;
913e47e68c7SMatthias Ringwald                     }
9140305bdeaSMatthias Ringwald                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
91574b323a9SMatthias Ringwald                     break;
91674b323a9SMatthias Ringwald                 }
91774b323a9SMatthias Ringwald                log_info("hci_run: init script done");
91874b323a9SMatthias Ringwald             }
91974b323a9SMatthias Ringwald             // otherwise continue
92074b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR;
9210305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_bd_addr);
92274b323a9SMatthias Ringwald             break;
923*a828a756SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS:
924*a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
925*a828a756SMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_commands);
926*a828a756SMatthias Ringwald             break;
92774b323a9SMatthias Ringwald         case HCI_INIT_READ_BUFFER_SIZE:
92874b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE;
9290305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_buffer_size);
93074b323a9SMatthias Ringwald             break;
93174b323a9SMatthias Ringwald         case HCI_INIT_READ_LOCAL_SUPPORTED_FEATUES:
93274b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATUES;
9330305bdeaSMatthias Ringwald             hci_send_cmd(&hci_read_local_supported_features);
93474b323a9SMatthias Ringwald             break;
93574b323a9SMatthias Ringwald         case HCI_INIT_SET_EVENT_MASK:
9360305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK;
93774b323a9SMatthias Ringwald             if (hci_le_supported()){
93874b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
93974b323a9SMatthias Ringwald             } else {
94074b323a9SMatthias Ringwald                 // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
94174b323a9SMatthias Ringwald                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
94274b323a9SMatthias Ringwald             }
94374b323a9SMatthias Ringwald             break;
94474b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE:
94574b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE;
9460305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
94774b323a9SMatthias Ringwald             break;
94874b323a9SMatthias Ringwald         case HCI_INIT_WRITE_PAGE_TIMEOUT:
94974b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT;
9500305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_page_timeout, 0x6000);  // ca. 15 sec
95174b323a9SMatthias Ringwald             break;
95274b323a9SMatthias Ringwald         case HCI_INIT_WRITE_CLASS_OF_DEVICE:
95374b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE;
9540305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
95574b323a9SMatthias Ringwald             break;
95674b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LOCAL_NAME:
9570305bdeaSMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME;
95874b323a9SMatthias Ringwald             if (hci_stack->local_name){
95974b323a9SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
96074b323a9SMatthias Ringwald             } else {
96174b323a9SMatthias Ringwald                 char hostname[30];
96274b323a9SMatthias Ringwald #ifdef EMBEDDED
96374b323a9SMatthias Ringwald                 // BTstack-11:22:33:44:55:66
96474b323a9SMatthias Ringwald                 strcpy(hostname, "BTstack ");
96574b323a9SMatthias Ringwald                 strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
96674b323a9SMatthias Ringwald                 log_info("---> Name %s", hostname);
96774b323a9SMatthias Ringwald #else
96874b323a9SMatthias Ringwald                 // hostname for POSIX systems
96974b323a9SMatthias Ringwald                 gethostname(hostname, 30);
97074b323a9SMatthias Ringwald                 hostname[29] = '\0';
97174b323a9SMatthias Ringwald #endif
97274b323a9SMatthias Ringwald                 hci_send_cmd(&hci_write_local_name, hostname);
97374b323a9SMatthias Ringwald             }
97474b323a9SMatthias Ringwald             break;
97574b323a9SMatthias Ringwald         case HCI_INIT_WRITE_SCAN_ENABLE:
97674b323a9SMatthias Ringwald             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
97774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE;
97874b323a9SMatthias Ringwald             break;
97974b323a9SMatthias Ringwald #ifdef HAVE_BLE
98074b323a9SMatthias Ringwald         // LE INIT
98174b323a9SMatthias Ringwald         case HCI_INIT_LE_READ_BUFFER_SIZE:
98274b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE;
9830305bdeaSMatthias Ringwald             hci_send_cmd(&hci_le_read_buffer_size);
98474b323a9SMatthias Ringwald             break;
98574b323a9SMatthias Ringwald         case HCI_INIT_WRITE_LE_HOST_SUPPORTED:
98674b323a9SMatthias Ringwald             // LE Supported Host = 1, Simultaneous Host = 0
98774b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED;
9880305bdeaSMatthias Ringwald             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
98974b323a9SMatthias Ringwald             break;
99074b323a9SMatthias Ringwald         case HCI_INIT_LE_SET_SCAN_PARAMETERS:
99174b323a9SMatthias Ringwald             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
99274b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS;
9930305bdeaSMatthias Ringwald             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
99474b323a9SMatthias Ringwald             break;
99574b323a9SMatthias Ringwald #endif
99674b323a9SMatthias Ringwald         // DONE
99774b323a9SMatthias Ringwald         case HCI_INIT_DONE:
99874b323a9SMatthias Ringwald             // done.
99974b323a9SMatthias Ringwald             hci_stack->state = HCI_STATE_WORKING;
100074b323a9SMatthias Ringwald             hci_emit_state();
100174b323a9SMatthias Ringwald             return;
100274b323a9SMatthias Ringwald         default:
100374b323a9SMatthias Ringwald             return;
100474b323a9SMatthias Ringwald     }
100555975f88SMatthias Ringwald }
100655975f88SMatthias Ringwald 
10076155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
1008a2481739S[email protected]     uint8_t command_completed = 0;
1009c4633093SMatthias Ringwald 
10106155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
10116155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,3);
10126155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
1013a2481739S[email protected]             command_completed = 1;
101476fcb19bSMatthias Ringwald             log_info("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate);
10156155b3d3S[email protected]         } else {
10166155b3d3S[email protected]             log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
10176155b3d3S[email protected]         }
10186155b3d3S[email protected]     }
10196155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_STATUS){
10206155b3d3S[email protected]         uint8_t  status = packet[2];
10216155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,4);
10226155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
10236155b3d3S[email protected]             if (status){
1024a2481739S[email protected]                 command_completed = 1;
102576fcb19bSMatthias Ringwald                 log_error("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate);
10266155b3d3S[email protected]             } else {
10276155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
10286155b3d3S[email protected]             }
10296155b3d3S[email protected]         } else {
10306155b3d3S[email protected]             log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
10316155b3d3S[email protected]         }
10326155b3d3S[email protected]     }
1033e47e68c7SMatthias Ringwald     // Vendor == CSR
10340d3b00a2SMatthias Ringwald     if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && packet[0] == HCI_EVENT_VENDOR_SPECIFIC){
1035e47e68c7SMatthias Ringwald         // TODO: track actual command
1036e47e68c7SMatthias Ringwald         command_completed = 1;
1037e47e68c7SMatthias Ringwald     }
1038a2481739S[email protected] 
1039a2481739S[email protected]     if (!command_completed) return;
1040a2481739S[email protected] 
1041db8bc6ffSMatthias Ringwald     int need_baud_change = hci_stack->config
1042db8bc6ffSMatthias Ringwald                         && hci_stack->control
1043db8bc6ffSMatthias Ringwald                         && hci_stack->control->baudrate_cmd
1044db8bc6ffSMatthias Ringwald                         && hci_stack->hci_transport->set_baudrate
1045db8bc6ffSMatthias Ringwald                         && ((hci_uart_config_t *)hci_stack->config)->baudrate_main;
1046db8bc6ffSMatthias Ringwald 
1047db8bc6ffSMatthias Ringwald     int need_addr_change = hci_stack->custom_bd_addr_set
1048db8bc6ffSMatthias Ringwald                         && hci_stack->control
1049db8bc6ffSMatthias Ringwald                         && hci_stack->control->set_bd_addr_cmd;
1050a80162e9SMatthias Ringwald 
10515c363727SMatthias Ringwald     switch(hci_stack->substate){
105274b323a9SMatthias Ringwald         case HCI_INIT_W4_SEND_RESET:
10530305bdeaSMatthias Ringwald             run_loop_remove_timer(&hci_stack->timeout);
105476fcb19bSMatthias Ringwald             break;
105576fcb19bSMatthias Ringwald         case HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION:
10562f48d920SMatthias Ringwald             if (need_addr_change){
10572f48d920SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
10582f48d920SMatthias Ringwald                 return;
10592f48d920SMatthias Ringwald             }
10602f48d920SMatthias Ringwald             // skipping addr change
10612f48d920SMatthias Ringwald             if (need_baud_change){
10622f48d920SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
10632f48d920SMatthias Ringwald                 return;
10642f48d920SMatthias Ringwald             }
10652f48d920SMatthias Ringwald             // also skip baud change
10662f48d920SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
10672f48d920SMatthias Ringwald             return;
10682f48d920SMatthias Ringwald         case HCI_INIT_W4_SET_BD_ADDR:
10698d29070eSMatthias Ringwald             // for STLC2500D, bd addr change only gets active after sending reset command
10708d29070eSMatthias Ringwald             if (hci_stack->manufacturer == 0x0030){
10718d29070eSMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT;
10728d29070eSMatthias Ringwald                 return;
10738d29070eSMatthias Ringwald             }
10748d29070eSMatthias Ringwald             // skipping warm boot on STLC2500D
10758d29070eSMatthias Ringwald             if (need_baud_change){
10768d29070eSMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
10778d29070eSMatthias Ringwald                 return;
10788d29070eSMatthias Ringwald             }
10798d29070eSMatthias Ringwald             // skipping baud change
10808d29070eSMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
10818d29070eSMatthias Ringwald             return;
10828d29070eSMatthias Ringwald         case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT:
1083a80162e9SMatthias Ringwald             if (need_baud_change){
1084a80162e9SMatthias Ringwald                 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
1085a80162e9SMatthias Ringwald                 return;
1086a80162e9SMatthias Ringwald             }
1087a80162e9SMatthias Ringwald             // skipping baud change
108874b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
108974b323a9SMatthias Ringwald             return;
1090a80162e9SMatthias Ringwald         case HCI_INIT_W4_SEND_BAUD_CHANGE:
10914696bddbSMatthias Ringwald             // for STLC2500D, baud rate change already happened.
10924696bddbSMatthias Ringwald             // for CC256x, baud rate gets changed now
10934696bddbSMatthias Ringwald             if (hci_stack->manufacturer != 0x0030){
10944696bddbSMatthias Ringwald                 uint32_t new_baud = ((hci_uart_config_t *)hci_stack->config)->baudrate_main;
10954696bddbSMatthias Ringwald                 log_info("Local baud rate change to %u", new_baud);
10964696bddbSMatthias Ringwald                 hci_stack->hci_transport->set_baudrate(new_baud);
10974696bddbSMatthias Ringwald             }
109874b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
109974b323a9SMatthias Ringwald             return;
1100a80162e9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
1101a80162e9SMatthias Ringwald             run_loop_remove_timer(&hci_stack->timeout);
1102a80162e9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1103a80162e9SMatthias Ringwald             return;
110474b323a9SMatthias Ringwald         case HCI_INIT_W4_CUSTOM_INIT:
110574b323a9SMatthias Ringwald             // repeat custom init
110674b323a9SMatthias Ringwald             hci_stack->substate = HCI_INIT_CUSTOM_INIT;
110774b323a9SMatthias Ringwald             return;
1108*a828a756SMatthias Ringwald         case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS:
1109*a828a756SMatthias Ringwald             // skip read buffer size if not supported
1110*a828a756SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x01) break;
1111*a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATUES;
1112*a828a756SMatthias Ringwald             return;
111374b323a9SMatthias Ringwald         case HCI_INIT_W4_SET_EVENT_MASK:
11146155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
11156155b3d3S[email protected]             if (!hci_classic_supported()){
11166155b3d3S[email protected]                 if (hci_le_supported()){
111774b323a9SMatthias Ringwald                     hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command
111874b323a9SMatthias Ringwald                     return;
11196155b3d3S[email protected]                 } else {
11206155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
112174b323a9SMatthias Ringwald                     hci_stack->substate = HCI_INIT_DONE; // skip all
112274b323a9SMatthias Ringwald                     return;
11236155b3d3S[email protected]                 }
11246155b3d3S[email protected]             }
112574b323a9SMatthias Ringwald             if (!hci_ssp_supported()){
11265c363727SMatthias Ringwald                 hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT;
112774b323a9SMatthias Ringwald                 return;
11286155b3d3S[email protected]             }
11296155b3d3S[email protected]             break;
1130*a828a756SMatthias Ringwald         case HCI_INIT_W4_LE_READ_BUFFER_SIZE:
1131*a828a756SMatthias Ringwald             // skip write le host if not supported (e.g. on LE only EM9301)
1132*a828a756SMatthias Ringwald             if (hci_stack->local_supported_commands[0] & 0x02) break;
1133*a828a756SMatthias Ringwald             hci_stack->substate = HCI_INIT_LE_SET_SCAN_PARAMETERS;
1134*a828a756SMatthias Ringwald             return;
113574b323a9SMatthias Ringwald         case HCI_INIT_W4_WRITE_SCAN_ENABLE:
11366155b3d3S[email protected]             if (!hci_le_supported()){
11376155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
113874b323a9SMatthias Ringwald                 hci_stack->substate = HCI_INIT_DONE;
113974b323a9SMatthias Ringwald                 return;
11406155b3d3S[email protected]             }
11416155b3d3S[email protected]         default:
114274b323a9SMatthias Ringwald             break;
11436155b3d3S[email protected]     }
114455975f88SMatthias Ringwald     hci_initializing_next_state();
11456155b3d3S[email protected] }
11466155b3d3S[email protected] 
114774b323a9SMatthias Ringwald 
114874ec757aSmatthias.ringwald // avoid huge local variables
1149223aafc1Smatthias.ringwald #ifndef EMBEDDED
115074ec757aSmatthias.ringwald static device_name_t device_name;
1151223aafc1Smatthias.ringwald #endif
115216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1153e76a89eeS[email protected] 
1154e76a89eeS[email protected]     uint16_t event_length = packet[1];
1155e76a89eeS[email protected] 
1156e76a89eeS[email protected]     // assert packet is complete
1157e76a89eeS[email protected]     if (size != event_length + 2){
1158e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
1159e76a89eeS[email protected]         return;
1160e76a89eeS[email protected]     }
1161e76a89eeS[email protected] 
11621281a47eSmatthias.ringwald     bd_addr_t addr;
116396a45072S[email protected]     bd_addr_type_t addr_type;
11642d00edd4Smatthias.ringwald     uint8_t link_type;
1165fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
11661f7b95a1Smatthias.ringwald     hci_connection_t * conn;
116756cf178bSmatthias.ringwald     int i;
116822909952Smatthias.ringwald 
11699da54300S[email protected]     // log_info("HCI:EVENT:%02x", packet[0]);
11705909f7f2Smatthias.ringwald 
11716772a24cSmatthias.ringwald     switch (packet[0]) {
117222909952Smatthias.ringwald 
11736772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
11747ec5eeaaSmatthias.ringwald             // get num cmd packets
11759da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u", hci_stack->num_cmd_packets, packet[2]);
11763a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
11777ec5eeaaSmatthias.ringwald 
1178e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
1179e2edc0c3Smatthias.ringwald                 // from offset 5
1180e2edc0c3Smatthias.ringwald                 // status
11811d279b20Smatthias.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"
11823a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
1183a8b12447S[email protected]                 hci_stack->sco_data_packet_length = packet[8];
11841a06f663S[email protected]                 hci_stack->acl_packets_total_num  = READ_BT_16(packet, 9);
11851a06f663S[email protected]                 hci_stack->sco_packets_total_num  = READ_BT_16(packet, 11);
1186a8b12447S[email protected] 
11873a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
1188a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
11893a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
11903a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
11918f8108aaSmatthias.ringwald                     }
11921a06f663S[email protected]                     log_info("hci_read_buffer_size: acl used size %u, count %u / sco size %u, count %u",
11931a06f663S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num,
11941a06f663S[email protected]                              hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num);
1195e2edc0c3Smatthias.ringwald                 }
119656cf178bSmatthias.ringwald             }
119765a46ef3S[email protected] #ifdef HAVE_BLE
119865a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
1199219eea5fS[email protected]                 hci_stack->le_data_packets_length = READ_BT_16(packet, 6);
1200ee303eddS[email protected]                 hci_stack->le_acl_packets_total_num  = packet[8];
12016c26b087S[email protected]                     // determine usable ACL payload size
12026c26b087S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){
12036c26b087S[email protected]                         hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE;
12046c26b087S[email protected]                     }
12059da54300S[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);
120665a46ef3S[email protected]             }
120765a46ef3S[email protected] #endif
1208188981d2Smatthias.ringwald             // Dump local address
1209188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
12103a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
12119da54300S[email protected]                 log_info("Local Address, Status: 0x%02x: Addr: %s",
12123a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
1213188981d2Smatthias.ringwald             }
1214381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
12153a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
1216381fbed8Smatthias.ringwald             }
121765389bfcS[email protected]             // Note: HCI init checks
1218559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
12193a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
122065389bfcS[email protected] 
1221a5a23fc2S[email protected]                 // determine usable ACL packet types based on host buffer size and supported features
1222a5a23fc2S[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]);
12233a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
1224f5d8d141S[email protected] 
1225f5d8d141S[email protected]                 // Classic/LE
1226f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
1227559e517eS[email protected]             }
12284696bddbSMatthias Ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_version_information)){
12294696bddbSMatthias Ringwald                 // hci_stack->hci_version    = READ_BT_16(packet, 4);
12304696bddbSMatthias Ringwald                 // hci_stack->hci_revision   = READ_BT_16(packet, 6);
12314696bddbSMatthias Ringwald                 // hci_stack->lmp_version    = READ_BT_16(packet, 8);
12324696bddbSMatthias Ringwald                 hci_stack->manufacturer   = READ_BT_16(packet, 10);
12334696bddbSMatthias Ringwald                 // hci_stack->lmp_subversion = READ_BT_16(packet, 12);
12344696bddbSMatthias Ringwald                 log_info("Manufacturer: 0x%04x", hci_stack->manufacturer);
12354696bddbSMatthias Ringwald             }
1236*a828a756SMatthias Ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_commands)){
1237*a828a756SMatthias Ringwald                 hci_stack->local_supported_commands[0] =
1238*a828a756SMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0X80) >> 7 |  // Octet 14, bit 7
1239*a828a756SMatthias Ringwald                     (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5;   // Octet 24, bit 6
1240*a828a756SMatthias Ringwald             }
124156cf178bSmatthias.ringwald             break;
124256cf178bSmatthias.ringwald 
12437ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
12447ec5eeaaSmatthias.ringwald             // get num cmd packets
12459da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]);
12463a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
12477ec5eeaaSmatthias.ringwald             break;
12487ec5eeaaSmatthias.ringwald 
12492e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
12502e440c8aS[email protected]             int offset = 3;
125156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
12522e440c8aS[email protected]                 handle = READ_BT_16(packet, offset);
12532e440c8aS[email protected]                 offset += 2;
12542e440c8aS[email protected]                 uint16_t num_packets = READ_BT_16(packet, offset);
12552e440c8aS[email protected]                 offset += 2;
12562e440c8aS[email protected] 
12575061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
125856cf178bSmatthias.ringwald                 if (!conn){
12599da54300S[email protected]                     log_error("hci_number_completed_packet lists unused con handle %u", handle);
126056cf178bSmatthias.ringwald                     continue;
126156cf178bSmatthias.ringwald                 }
126223bed257S[email protected] 
1263e35edcc1S[email protected]                 if (conn->address_type == BD_ADDR_TYPE_SCO){
1264e35edcc1S[email protected]                     if (conn->num_sco_packets_sent >= num_packets){
1265e35edcc1S[email protected]                         conn->num_sco_packets_sent -= num_packets;
1266e35edcc1S[email protected]                     } else {
1267e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more sco slots freed then sent.");
1268e35edcc1S[email protected]                         conn->num_sco_packets_sent = 0;
1269e35edcc1S[email protected]                     }
1270e35edcc1S[email protected] 
1271e35edcc1S[email protected]                 } else {
127223bed257S[email protected]                     if (conn->num_acl_packets_sent >= num_packets){
127356cf178bSmatthias.ringwald                         conn->num_acl_packets_sent -= num_packets;
127423bed257S[email protected]                     } else {
1275e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more acl slots freed then sent.");
127623bed257S[email protected]                         conn->num_acl_packets_sent = 0;
127723bed257S[email protected]                     }
1278e35edcc1S[email protected]                 }
12799da54300S[email protected]                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent);
128056cf178bSmatthias.ringwald             }
12816772a24cSmatthias.ringwald             break;
12822e440c8aS[email protected]         }
12831f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
128437eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
128537eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
12862d00edd4Smatthias.ringwald             link_type = packet[11];
12879da54300S[email protected]             log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type);
1288e35edcc1S[email protected]             addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO;
12892e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
12901f7b95a1Smatthias.ringwald             if (!conn) {
12915293c072S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, addr_type);
12921f7b95a1Smatthias.ringwald             }
1293ce4c8fabSmatthias.ringwald             if (!conn) {
1294ce4c8fabSmatthias.ringwald                 // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
12953a9fb326S[email protected]                 hci_stack->decline_reason = 0x0d;
12963a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
1297ce4c8fabSmatthias.ringwald                 break;
1298ce4c8fabSmatthias.ringwald             }
129932ab9390Smatthias.ringwald             conn->state = RECEIVED_CONNECTION_REQUEST;
130032ab9390Smatthias.ringwald             hci_run();
13011f7b95a1Smatthias.ringwald             break;
13021f7b95a1Smatthias.ringwald 
13036772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1304fe1ed1b8Smatthias.ringwald             // Connection management
1305fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
13069da54300S[email protected]             log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
130796a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
13082e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
1309fe1ed1b8Smatthias.ringwald             if (conn) {
1310b448a0e7Smatthias.ringwald                 if (!packet[2]){
1311c8e4258aSmatthias.ringwald                     conn->state = OPEN;
1312fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
1313ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
1314ee091cf1Smatthias.ringwald 
1315c785ef68Smatthias.ringwald                     // restart timer
1316c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1317ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
1318c785ef68Smatthias.ringwald 
13199da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
132043bfb1bdSmatthias.ringwald 
132143bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
1322b448a0e7Smatthias.ringwald                 } else {
13231bd5283dS[email protected]                     int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED;
13241bd5283dS[email protected]                     uint8_t status = packet[2];
13251bd5283dS[email protected]                     bd_addr_t bd_address;
13261bd5283dS[email protected]                     memcpy(&bd_address, conn->address, 6);
1327ad83dc6aS[email protected] 
1328b448a0e7Smatthias.ringwald                     // connection failed, remove entry
13293a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
1330a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
1331c12e46e7Smatthias.ringwald 
13321bd5283dS[email protected]                     // notify client if dedicated bonding
13331bd5283dS[email protected]                     if (notify_dedicated_bonding_failed){
13341bd5283dS[email protected]                         log_info("hci notify_dedicated_bonding_failed");
13351bd5283dS[email protected]                         hci_emit_dedicated_bonding_result(bd_address, status);
13361bd5283dS[email protected]                     }
13371bd5283dS[email protected] 
1338c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
1339c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
13402e77e513S[email protected]                         hci_drop_link_key_for_bd_addr(addr);
1341c12e46e7Smatthias.ringwald                     }
1342fe1ed1b8Smatthias.ringwald                 }
1343fe1ed1b8Smatthias.ringwald             }
13446772a24cSmatthias.ringwald             break;
1345fe1ed1b8Smatthias.ringwald 
134644d0e3d5S[email protected]         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:
134744d0e3d5S[email protected]             bt_flip_addr(addr, &packet[5]);
134844d0e3d5S[email protected]             log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
13491a06f663S[email protected]             if (packet[2]){
135044d0e3d5S[email protected]                 // connection failed
135144d0e3d5S[email protected]                 break;
135244d0e3d5S[email protected]             }
13532e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1354e35edcc1S[email protected]             if (!conn) {
1355e35edcc1S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1356e35edcc1S[email protected]             }
1357e35edcc1S[email protected]             if (!conn) {
1358e35edcc1S[email protected]                 break;
1359e35edcc1S[email protected]             }
13601a06f663S[email protected]             conn->state = OPEN;
13611a06f663S[email protected]             conn->con_handle = READ_BT_16(packet, 3);
136244d0e3d5S[email protected]             break;
136344d0e3d5S[email protected] 
1364afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1365afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
1366afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
1367afd4e962S[email protected]             if (!conn) break;
1368afd4e962S[email protected]             if (!packet[2]){
1369afd4e962S[email protected]                 uint8_t * features = &packet[5];
1370afd4e962S[email protected]                 if (features[6] & (1 << 3)){
1371afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
1372afd4e962S[email protected]                 }
1373afd4e962S[email protected]             }
1374afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
1375ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
1376ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1377ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1378ad83dc6aS[email protected]             }
1379afd4e962S[email protected]             break;
1380afd4e962S[email protected] 
13817fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
13829da54300S[email protected]             log_info("HCI_EVENT_LINK_KEY_REQUEST");
13837fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
138474d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
13853a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
138632ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
138764472d52Smatthias.ringwald             hci_run();
1388d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
138929d53098Smatthias.ringwald             return;
13907fde4af9Smatthias.ringwald 
13919ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
139229d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
13932e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
13949ab95c90S[email protected]             if (!conn) break;
13959ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
13967bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
13979ab95c90S[email protected]             // Change Connection Encryption keeps link key type
13989ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
13999ab95c90S[email protected]                 conn->link_key_type = link_key_type;
14009ab95c90S[email protected]             }
14013a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
14022e77e513S[email protected]             hci_stack->remote_device_db->put_link_key(addr, &packet[8], conn->link_key_type);
140329d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
14047fde4af9Smatthias.ringwald             break;
14059ab95c90S[email protected]         }
14067fde4af9Smatthias.ringwald 
14077fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
14086724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
14094c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
14103a9fb326S[email protected]             if (!hci_stack->bondable){
1411899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
1412f8fb5f6eS[email protected]                 hci_run();
1413f8fb5f6eS[email protected]                 return;
14144c57c146S[email protected]             }
1415d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
14163a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
1417d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
14182e77e513S[email protected]             hci_stack->remote_device_db->delete_link_key(addr);
14197fde4af9Smatthias.ringwald             break;
14207fde4af9Smatthias.ringwald 
14211d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
14221d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
1423dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
1424dbe1a790S[email protected]             break;
1425dbe1a790S[email protected] 
1426dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
14276724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
14283a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1429dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
1430dbe1a790S[email protected]             break;
1431dbe1a790S[email protected] 
1432dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
14336724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
14343a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1435dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
14361d6b20aeS[email protected]             break;
14371d6b20aeS[email protected] 
1438f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1439f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
1440f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1441f0944df2S[email protected]             if (!conn) break;
1442ad83dc6aS[email protected]             if (packet[2] == 0) {
1443f0944df2S[email protected]                 if (packet[5]){
1444f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1445f0944df2S[email protected]                 } else {
1446536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1447f0944df2S[email protected]                 }
1448ad83dc6aS[email protected]             }
1449a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
1450f0944df2S[email protected]             break;
1451f0944df2S[email protected] 
14521eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
14531eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
14541eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
14551eb2563eS[email protected]             if (!conn) break;
1456ad83dc6aS[email protected] 
1457ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1458ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1459ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1460ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
14611bd5283dS[email protected]                 conn->bonding_status = packet[2];
1462ad83dc6aS[email protected]                 break;
1463ad83dc6aS[email protected]             }
1464ad83dc6aS[email protected] 
1465b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
14661eb2563eS[email protected]                 // link key sufficient for requested security
14671eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1468ad83dc6aS[email protected]                 break;
1469ad83dc6aS[email protected]             }
14701eb2563eS[email protected]             // not enough
14711eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
14721eb2563eS[email protected]             break;
147334d2123cS[email protected] 
1474223aafc1Smatthias.ringwald #ifndef EMBEDDED
147574ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
14763a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
147774ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
147874ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
14795a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
14805a06394aSmatthias.ringwald             for (i=0; i<248;i++){
14815a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
14825a06394aSmatthias.ringwald                     packet[9+i] = 0;
14835a06394aSmatthias.ringwald                     break;
1484cdc9101dSmatthias.ringwald                 }
14855a06394aSmatthias.ringwald             }
1486d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
148774ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
14882e77e513S[email protected]             hci_stack->remote_device_db->put_name(addr, &device_name);
148974ec757aSmatthias.ringwald             break;
149074ec757aSmatthias.ringwald 
149174ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
1492206a9031S[email protected]         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
14933a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
149474ec757aSmatthias.ringwald             // first send inq result packet
14953a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
149674ec757aSmatthias.ringwald             // then send cached remote names
1497206a9031S[email protected]             int offset = 3;
149874ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
1499206a9031S[email protected]                 bt_flip_addr(addr, &packet[offset]);
1500206a9031S[email protected]                 offset += 14; // 6 + 1 + 1 + 1 + 3 + 2;
15012e77e513S[email protected]                 if (hci_stack->remote_device_db->get_name(addr, &device_name)){
15022e77e513S[email protected]                     hci_emit_remote_name_cached(addr, &device_name);
150374ec757aSmatthias.ringwald                 }
150474ec757aSmatthias.ringwald             }
150574ec757aSmatthias.ringwald             return;
1506206a9031S[email protected]         }
1507223aafc1Smatthias.ringwald #endif
150874ec757aSmatthias.ringwald 
150986805605S[email protected]         // HCI_EVENT_DISCONNECTION_COMPLETE
1510ccda6e14S[email protected]         // has been split, to first notify stack before shutting connection down
1511ccda6e14S[email protected]         // see end of function, too.
1512a4f30ec0S[email protected]         case HCI_EVENT_DISCONNECTION_COMPLETE:
1513a4f30ec0S[email protected]             if (packet[2]) break;   // status != 0
1514ccda6e14S[email protected]             handle = READ_BT_16(packet, 3);
15156da48142SSean Wilson             conn = hci_connection_for_handle(handle);
1516a4f30ec0S[email protected]             if (!conn) break;       // no conn struct anymore
1517ccda6e14S[email protected]             conn->state = RECEIVED_DISCONNECTION_COMPLETE;
1518ccda6e14S[email protected]             break;
15196772a24cSmatthias.ringwald 
1520c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
1521d23838ecSMatthias Ringwald             if (hci_stack->hardware_error_callback){
1522d23838ecSMatthias Ringwald                 (*hci_stack->hardware_error_callback)();
1523d23838ecSMatthias Ringwald             } else if(hci_stack->control && hci_stack->control->hw_error){
15243a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
15257586ee35S[email protected]             } else {
15267586ee35S[email protected]                 // if no special requests, just reboot stack
15277586ee35S[email protected]                 hci_power_control_off();
15287586ee35S[email protected]                 hci_power_control_on();
1529c68bdf90Smatthias.ringwald             }
1530c68bdf90Smatthias.ringwald             break;
1531c68bdf90Smatthias.ringwald 
15326b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
1533d051460cS[email protected]             // release packet buffer only for asynchronous transport and if there are not further fragements
15344fa24b5fS[email protected]             if (hci_transport_synchronous()) {
15354fa24b5fS[email protected]                 log_error("Synchronous HCI Transport shouldn't send DAEMON_EVENT_HCI_PACKET_SENT");
15364fa24b5fS[email protected]                 return; // instead of break: to avoid re-entering hci_run()
15374fa24b5fS[email protected]             }
1538d051460cS[email protected]             if (hci_stack->acl_fragmentation_total_size) break;
1539d051460cS[email protected]             hci_release_packet_buffer();
15406b4af23dS[email protected]             break;
15416b4af23dS[email protected] 
15425909f7f2Smatthias.ringwald #ifdef HAVE_BLE
15435909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
15445909f7f2Smatthias.ringwald             switch (packet[2]){
154557c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
15469da54300S[email protected]                     log_info("advertising report received");
15477bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
154857c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
15497bdc6798S[email protected]                     break;
15505909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
15515909f7f2Smatthias.ringwald                     // Connection management
15525909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
15537bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
15549da54300S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr));
15555909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
15562e77e513S[email protected]                     conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
15575909f7f2Smatthias.ringwald                     if (packet[3]){
15585909f7f2Smatthias.ringwald                         if (conn){
15595909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
15603a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
15615909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
15625909f7f2Smatthias.ringwald                         }
15635909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
15645909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
15652e77e513S[email protected]                             hci_drop_link_key_for_bd_addr(addr);
15665909f7f2Smatthias.ringwald                         }
15675909f7f2Smatthias.ringwald                         break;
15685909f7f2Smatthias.ringwald                     }
15695909f7f2Smatthias.ringwald                     if (!conn){
157096a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
15715909f7f2Smatthias.ringwald                     }
15725909f7f2Smatthias.ringwald                     if (!conn){
15735909f7f2Smatthias.ringwald                         // no memory
15745909f7f2Smatthias.ringwald                         break;
15755909f7f2Smatthias.ringwald                     }
15765909f7f2Smatthias.ringwald 
15775909f7f2Smatthias.ringwald                     conn->state = OPEN;
15785909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
15795909f7f2Smatthias.ringwald 
15805909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
15815909f7f2Smatthias.ringwald 
15825909f7f2Smatthias.ringwald                     // restart timer
15835909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
15845909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
15855909f7f2Smatthias.ringwald 
15869da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
15875909f7f2Smatthias.ringwald 
15885909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
15895909f7f2Smatthias.ringwald                     break;
15905909f7f2Smatthias.ringwald 
15919da54300S[email protected]             // log_info("LE buffer size: %u, count %u", READ_BT_16(packet,6), packet[8]);
159265a46ef3S[email protected] 
15935909f7f2Smatthias.ringwald                 default:
15945909f7f2Smatthias.ringwald                     break;
15955909f7f2Smatthias.ringwald             }
15965909f7f2Smatthias.ringwald             break;
15975909f7f2Smatthias.ringwald #endif
15986772a24cSmatthias.ringwald         default:
15996772a24cSmatthias.ringwald             break;
1600fe1ed1b8Smatthias.ringwald     }
1601fe1ed1b8Smatthias.ringwald 
16023429f56bSmatthias.ringwald     // handle BT initialization
16033a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
16046155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1605c9af4d3fS[email protected]     }
160622909952Smatthias.ringwald 
160789db417bSmatthias.ringwald     // help with BT sleep
16083a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
160974b323a9SMatthias Ringwald         && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE
161089db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
161155975f88SMatthias Ringwald         hci_initializing_next_state();
161289db417bSmatthias.ringwald     }
161389db417bSmatthias.ringwald 
161486805605S[email protected]     // notify upper stack
16153a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
161694ab26f8Smatthias.ringwald 
161786805605S[email protected]     // moved here to give upper stack a chance to close down everything with hci_connection_t intact
161886805605S[email protected]     if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){
161986805605S[email protected]         if (!packet[2]){
162086805605S[email protected]             handle = READ_BT_16(packet, 3);
162186805605S[email protected]             hci_connection_t * conn = hci_connection_for_handle(handle);
162286805605S[email protected]             if (conn) {
162386805605S[email protected]                 uint8_t status = conn->bonding_status;
1624bb820044S[email protected]                 uint16_t flags = conn->bonding_flags;
162586805605S[email protected]                 bd_addr_t bd_address;
162686805605S[email protected]                 memcpy(&bd_address, conn->address, 6);
162786805605S[email protected]                 hci_shutdown_connection(conn);
1628bb820044S[email protected]                 // connection struct is gone, don't access anymore
1629bb820044S[email protected]                 if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){
163086805605S[email protected]                     hci_emit_dedicated_bonding_result(bd_address, status);
163186805605S[email protected]                 }
163286805605S[email protected]             }
163386805605S[email protected]         }
163486805605S[email protected]     }
163586805605S[email protected] 
163694ab26f8Smatthias.ringwald 	// execute main loop
163794ab26f8Smatthias.ringwald 	hci_run();
163816833f0aSmatthias.ringwald }
163916833f0aSmatthias.ringwald 
1640c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){
1641c91d150bS[email protected]     // not handled yet
1642c91d150bS[email protected] }
1643c91d150bS[email protected] 
16440a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
16455bb5bc3eS[email protected]     hci_dump_packet(packet_type, 1, packet, size);
164610e830c9Smatthias.ringwald     switch (packet_type) {
164710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
164810e830c9Smatthias.ringwald             event_handler(packet, size);
164910e830c9Smatthias.ringwald             break;
165010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
165110e830c9Smatthias.ringwald             acl_handler(packet, size);
165210e830c9Smatthias.ringwald             break;
1653c91d150bS[email protected]         case HCI_SCO_DATA_PACKET:
1654c91d150bS[email protected]             sco_handler(packet, size);
165510e830c9Smatthias.ringwald         default:
165610e830c9Smatthias.ringwald             break;
165710e830c9Smatthias.ringwald     }
165810e830c9Smatthias.ringwald }
165910e830c9Smatthias.ringwald 
1660fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
16612718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
16623a9fb326S[email protected]     hci_stack->packet_handler = handler;
166316833f0aSmatthias.ringwald }
166416833f0aSmatthias.ringwald 
166571de195eSMatthias Ringwald static void hci_state_reset(void){
1666595bdbfbS[email protected]     // no connections yet
1667595bdbfbS[email protected]     hci_stack->connections = NULL;
166874308b23Smatthias.ringwald 
166974308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
167074308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
167174308b23Smatthias.ringwald     // hci_stack->connectable = 0;
167274308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1673595bdbfbS[email protected] 
167444935e40S[email protected]     // buffer is free
167544935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
167644935e40S[email protected] 
1677595bdbfbS[email protected]     // no pending cmds
1678595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1679595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1680595bdbfbS[email protected] 
1681595bdbfbS[email protected]     // LE
1682595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1683595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1684595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1685e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1686da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_min = 0x0006;
1687da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_max = 0x0C80;
1688da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_latency_min = 0x0000;
1689da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_latency_max = 0x03E8;
1690da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 0x000A;
1691da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 0x0C80;
1692595bdbfbS[email protected] }
1693595bdbfbS[email protected] 
16944f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1695475c8125Smatthias.ringwald 
16963a9fb326S[email protected] #ifdef HAVE_MALLOC
16973a9fb326S[email protected]     if (!hci_stack) {
16983a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
16993a9fb326S[email protected]     }
17003a9fb326S[email protected] #else
17013a9fb326S[email protected]     hci_stack = &hci_stack_static;
17023a9fb326S[email protected] #endif
170366fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
17043a9fb326S[email protected] 
1705475c8125Smatthias.ringwald     // reference to use transport layer implementation
17063a9fb326S[email protected]     hci_stack->hci_transport = transport;
1707475c8125Smatthias.ringwald 
170811e23e5fSmatthias.ringwald     // references to used control implementation
17093a9fb326S[email protected]     hci_stack->control = control;
171011e23e5fSmatthias.ringwald 
171111e23e5fSmatthias.ringwald     // reference to used config
17123a9fb326S[email protected]     hci_stack->config = config;
171311e23e5fSmatthias.ringwald 
171416833f0aSmatthias.ringwald     // higher level handler
17153a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
171616833f0aSmatthias.ringwald 
1717404843c1Smatthias.ringwald     // store and open remote device db
17183a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
17193a9fb326S[email protected]     if (hci_stack->remote_device_db) {
17203a9fb326S[email protected]         hci_stack->remote_device_db->open();
1721404843c1Smatthias.ringwald     }
172229d53098Smatthias.ringwald 
17238fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
17243a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
17258fcba05dSmatthias.ringwald 
172616833f0aSmatthias.ringwald     // register packet handlers with transport
172710e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1728f5454fc6Smatthias.ringwald 
17293a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1730e2386ba1S[email protected] 
1731e2386ba1S[email protected]     // class of device
17323a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1733a45d6b9fS[email protected] 
1734f20168b8Smatthias.ringwald     // bondable by default
1735f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1736f20168b8Smatthias.ringwald 
173763048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
17383a9fb326S[email protected]     hci_stack->ssp_enable = 1;
17393a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
17403a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
17413a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
174269a97523S[email protected] 
1743595bdbfbS[email protected]     hci_state_reset();
1744475c8125Smatthias.ringwald }
1745475c8125Smatthias.ringwald 
174671de195eSMatthias Ringwald void hci_close(void){
1747404843c1Smatthias.ringwald     // close remote device db
17483a9fb326S[email protected]     if (hci_stack->remote_device_db) {
17493a9fb326S[email protected]         hci_stack->remote_device_db->close();
1750404843c1Smatthias.ringwald     }
17513a9fb326S[email protected]     while (hci_stack->connections) {
1752bc68afe2S[email protected]         // cancel all l2cap connections
1753bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
17543a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1755f5454fc6Smatthias.ringwald     }
1756f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
17573a9fb326S[email protected] 
17583a9fb326S[email protected] #ifdef HAVE_MALLOC
17593a9fb326S[email protected]     free(hci_stack);
17603a9fb326S[email protected] #endif
17613a9fb326S[email protected]     hci_stack = NULL;
1762404843c1Smatthias.ringwald }
1763404843c1Smatthias.ringwald 
17649e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
17659e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
17669e61646fS[email protected] }
17679e61646fS[email protected] 
1768f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h
1769f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){
1770f456b2d0S[email protected]     memcpy(hci_stack->custom_bd_addr, addr, 6);
1771f456b2d0S[email protected]     hci_stack->custom_bd_addr_set = 1;
1772f456b2d0S[email protected] }
1773f456b2d0S[email protected] 
177471de195eSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){
177566fb9560S[email protected]     disable_l2cap_timeouts = 1;
177666fb9560S[email protected] }
17778d213e1aSmatthias.ringwald // State-Module-Driver overview
17788d213e1aSmatthias.ringwald // state                    module  low-level
17798d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
17808d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
17818d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
17828d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1783d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1784d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1785c7e0c5f6Smatthias.ringwald 
178640d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
17877301ad89Smatthias.ringwald 
1788038bc64cSmatthias.ringwald     // power on
1789f9a30166Smatthias.ringwald     int err = 0;
17903a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
17913a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1792f9a30166Smatthias.ringwald     }
1793038bc64cSmatthias.ringwald     if (err){
17949da54300S[email protected]         log_error( "POWER_ON failed");
1795038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1796038bc64cSmatthias.ringwald         return err;
1797038bc64cSmatthias.ringwald     }
1798038bc64cSmatthias.ringwald 
1799038bc64cSmatthias.ringwald     // open low-level device
18003a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1801038bc64cSmatthias.ringwald     if (err){
18029da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
18033a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
18043a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1805f9a30166Smatthias.ringwald         }
1806038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1807038bc64cSmatthias.ringwald         return err;
1808038bc64cSmatthias.ringwald     }
18098d213e1aSmatthias.ringwald     return 0;
18108d213e1aSmatthias.ringwald }
1811038bc64cSmatthias.ringwald 
181240d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
18138d213e1aSmatthias.ringwald 
18149da54300S[email protected]     log_info("hci_power_control_off");
18159418f9c9Smatthias.ringwald 
18168d213e1aSmatthias.ringwald     // close low-level device
18173a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
18188d213e1aSmatthias.ringwald 
18199da54300S[email protected]     log_info("hci_power_control_off - hci_transport closed");
18209418f9c9Smatthias.ringwald 
18218d213e1aSmatthias.ringwald     // power off
18223a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
18233a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
18248d213e1aSmatthias.ringwald     }
18259418f9c9Smatthias.ringwald 
18269da54300S[email protected]     log_info("hci_power_control_off - control closed");
18279418f9c9Smatthias.ringwald 
18283a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
182972ea5239Smatthias.ringwald }
183072ea5239Smatthias.ringwald 
183140d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
183272ea5239Smatthias.ringwald 
18339da54300S[email protected]     log_info("hci_power_control_sleep");
18343144bce4Smatthias.ringwald 
1835b429b9b7Smatthias.ringwald #if 0
1836b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1837b429b9b7Smatthias.ringwald 
183872ea5239Smatthias.ringwald     // close low-level device
18393a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1840b429b9b7Smatthias.ringwald #endif
184172ea5239Smatthias.ringwald 
184272ea5239Smatthias.ringwald     // sleep mode
18433a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
18443a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
184572ea5239Smatthias.ringwald     }
1846b429b9b7Smatthias.ringwald 
18473a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
18488d213e1aSmatthias.ringwald }
18498d213e1aSmatthias.ringwald 
185040d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1851b429b9b7Smatthias.ringwald 
18529da54300S[email protected]     log_info("hci_power_control_wake");
1853b429b9b7Smatthias.ringwald 
1854b429b9b7Smatthias.ringwald     // wake on
18553a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
18563a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1857b429b9b7Smatthias.ringwald     }
1858b429b9b7Smatthias.ringwald 
1859b429b9b7Smatthias.ringwald #if 0
1860b429b9b7Smatthias.ringwald     // open low-level device
18613a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1862b429b9b7Smatthias.ringwald     if (err){
18639da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
18643a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
18653a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1866b429b9b7Smatthias.ringwald         }
1867b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1868b429b9b7Smatthias.ringwald         return err;
1869b429b9b7Smatthias.ringwald     }
1870b429b9b7Smatthias.ringwald #endif
1871b429b9b7Smatthias.ringwald 
1872b429b9b7Smatthias.ringwald     return 0;
1873b429b9b7Smatthias.ringwald }
1874b429b9b7Smatthias.ringwald 
187544935e40S[email protected] static void hci_power_transition_to_initializing(void){
187644935e40S[email protected]     // set up state machine
187744935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
187844935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
187944935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
18805c363727SMatthias Ringwald     hci_stack->substate = HCI_INIT_SEND_RESET;
188144935e40S[email protected] }
1882b429b9b7Smatthias.ringwald 
18838d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1884d661ed19Smatthias.ringwald 
18859da54300S[email protected]     log_info("hci_power_control: %u, current mode %u", power_mode, hci_stack->state);
1886d661ed19Smatthias.ringwald 
18878d213e1aSmatthias.ringwald     int err = 0;
18883a9fb326S[email protected]     switch (hci_stack->state){
18898d213e1aSmatthias.ringwald 
18908d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
18918d213e1aSmatthias.ringwald             switch (power_mode){
18928d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
18938d213e1aSmatthias.ringwald                     err = hci_power_control_on();
189497b61c7bS[email protected]                     if (err) {
189597b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
189697b61c7bS[email protected]                         return err;
189797b61c7bS[email protected]                     }
189844935e40S[email protected]                     hci_power_transition_to_initializing();
18998d213e1aSmatthias.ringwald                     break;
19008d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
19018d213e1aSmatthias.ringwald                     // do nothing
19028d213e1aSmatthias.ringwald                     break;
19038d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1904b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
19058d213e1aSmatthias.ringwald                     break;
19068d213e1aSmatthias.ringwald             }
19078d213e1aSmatthias.ringwald             break;
19087301ad89Smatthias.ringwald 
19098d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
19108d213e1aSmatthias.ringwald             switch (power_mode){
19118d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
19128d213e1aSmatthias.ringwald                     // do nothing
19138d213e1aSmatthias.ringwald                     break;
19148d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
19158d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
19168d213e1aSmatthias.ringwald                     hci_power_control_off();
19178d213e1aSmatthias.ringwald                     break;
19188d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1919b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
192072ea5239Smatthias.ringwald                     hci_power_control_sleep();
19218d213e1aSmatthias.ringwald                     break;
19228d213e1aSmatthias.ringwald             }
19238d213e1aSmatthias.ringwald             break;
19247301ad89Smatthias.ringwald 
19258d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
19268d213e1aSmatthias.ringwald             switch (power_mode){
19278d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
19288d213e1aSmatthias.ringwald                     // do nothing
19298d213e1aSmatthias.ringwald                     break;
19308d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1931c7e0c5f6Smatthias.ringwald                     // see hci_run
19323a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
19338d213e1aSmatthias.ringwald                     break;
19348d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1935b546ac54Smatthias.ringwald                     // see hci_run
19363a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
193774b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
19388d213e1aSmatthias.ringwald                     break;
19398d213e1aSmatthias.ringwald             }
19408d213e1aSmatthias.ringwald             break;
19417301ad89Smatthias.ringwald 
19428d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
19438d213e1aSmatthias.ringwald             switch (power_mode){
19448d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
194544935e40S[email protected]                     hci_power_transition_to_initializing();
19468d213e1aSmatthias.ringwald                     break;
19478d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
19488d213e1aSmatthias.ringwald                     // do nothing
19498d213e1aSmatthias.ringwald                     break;
19508d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1951b546ac54Smatthias.ringwald                     // see hci_run
19523a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
195374b323a9SMatthias Ringwald                     hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT;
19548d213e1aSmatthias.ringwald                     break;
19558d213e1aSmatthias.ringwald             }
19568d213e1aSmatthias.ringwald             break;
19578d213e1aSmatthias.ringwald 
19588d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
19598d213e1aSmatthias.ringwald             switch (power_mode){
19608d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
196128171530Smatthias.ringwald 
196228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
196328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
196428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
19653a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
196674b323a9SMatthias Ringwald                         hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE;   // init after sleep
196728171530Smatthias.ringwald                         break;
196828171530Smatthias.ringwald                     }
196928171530Smatthias.ringwald #endif
197044935e40S[email protected]                     hci_power_transition_to_initializing();
19718d213e1aSmatthias.ringwald                     break;
19728d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1973b546ac54Smatthias.ringwald                     // see hci_run
19743a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
19758d213e1aSmatthias.ringwald                     break;
19768d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1977b546ac54Smatthias.ringwald                     // do nothing
19788d213e1aSmatthias.ringwald                     break;
19798d213e1aSmatthias.ringwald             }
19808d213e1aSmatthias.ringwald             break;
19818d213e1aSmatthias.ringwald 
19828d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
19838d213e1aSmatthias.ringwald             switch (power_mode){
19848d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
198528171530Smatthias.ringwald 
198628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
198728171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
198828171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
19893a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
19905c363727SMatthias Ringwald                         hci_stack->substate = HCI_INIT_AFTER_SLEEP;
1991758b46ceSmatthias.ringwald                         hci_update_scan_enable();
199228171530Smatthias.ringwald                         break;
199328171530Smatthias.ringwald                     }
199428171530Smatthias.ringwald #endif
19953144bce4Smatthias.ringwald                     err = hci_power_control_wake();
19963144bce4Smatthias.ringwald                     if (err) return err;
199744935e40S[email protected]                     hci_power_transition_to_initializing();
19988d213e1aSmatthias.ringwald                     break;
19998d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
20003a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
20018d213e1aSmatthias.ringwald                     break;
20028d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
2003b546ac54Smatthias.ringwald                     // do nothing
20048d213e1aSmatthias.ringwald                     break;
20058d213e1aSmatthias.ringwald             }
20068d213e1aSmatthias.ringwald             break;
200711e23e5fSmatthias.ringwald     }
200868d92d03Smatthias.ringwald 
2009038bc64cSmatthias.ringwald     // create internal event
2010ee8bf225Smatthias.ringwald 	hci_emit_state();
2011ee8bf225Smatthias.ringwald 
201268d92d03Smatthias.ringwald 	// trigger next/first action
201368d92d03Smatthias.ringwald 	hci_run();
201468d92d03Smatthias.ringwald 
2015475c8125Smatthias.ringwald     return 0;
2016475c8125Smatthias.ringwald }
2017475c8125Smatthias.ringwald 
2018758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
2019758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
20203a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
2021758b46ceSmatthias.ringwald     hci_run();
2022758b46ceSmatthias.ringwald }
2023758b46ceSmatthias.ringwald 
2024381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
2025381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
2026381fbed8Smatthias.ringwald 
20273a9fb326S[email protected]     if (hci_stack->discoverable == enable){
20283a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
2029381fbed8Smatthias.ringwald         return;
2030381fbed8Smatthias.ringwald     }
2031381fbed8Smatthias.ringwald 
20323a9fb326S[email protected]     hci_stack->discoverable = enable;
2033758b46ceSmatthias.ringwald     hci_update_scan_enable();
2034758b46ceSmatthias.ringwald }
2035b031bebbSmatthias.ringwald 
2036758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
2037758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
2038758b46ceSmatthias.ringwald 
2039758b46ceSmatthias.ringwald     // don't emit event
20403a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
2041758b46ceSmatthias.ringwald 
20423a9fb326S[email protected]     hci_stack->connectable = enable;
2043758b46ceSmatthias.ringwald     hci_update_scan_enable();
2044381fbed8Smatthias.ringwald }
2045381fbed8Smatthias.ringwald 
2046690bd0baS[email protected] void hci_local_bd_addr(bd_addr_t address_buffer){
2047690bd0baS[email protected]     memcpy(address_buffer, hci_stack->local_bd_addr, 6);
20485061f3afS[email protected] }
20495061f3afS[email protected] 
205071de195eSMatthias Ringwald void hci_run(void){
20518a485f27Smatthias.ringwald 
2052db8bc6ffSMatthias Ringwald     // log_info("hci_run: entered");
205332ab9390Smatthias.ringwald     hci_connection_t * connection;
205432ab9390Smatthias.ringwald     linked_item_t * it;
205532ab9390Smatthias.ringwald 
2056b5d8b22bS[email protected]     // send continuation fragments first, as they block the prepared packet buffer
2057b5d8b22bS[email protected]     if (hci_stack->acl_fragmentation_total_size > 0) {
2058b5d8b22bS[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer);
2059b5d8b22bS[email protected]         if (hci_can_send_prepared_acl_packet_now(con_handle)){
2060b5d8b22bS[email protected]             hci_connection_t *connection = hci_connection_for_handle(con_handle);
2061b5d8b22bS[email protected]             if (connection) {
2062b5d8b22bS[email protected]                 hci_send_acl_packet_fragments(connection);
2063b5d8b22bS[email protected]                 return;
2064b5d8b22bS[email protected]             }
2065b5d8b22bS[email protected]             // connection gone -> discard further fragments
2066b5d8b22bS[email protected]             hci_stack->acl_fragmentation_total_size = 0;
2067b5d8b22bS[email protected]             hci_stack->acl_fragmentation_pos = 0;
2068b5d8b22bS[email protected]         }
2069b5d8b22bS[email protected]     }
2070b5d8b22bS[email protected] 
2071d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()) return;
2072ce4c8fabSmatthias.ringwald 
2073b031bebbSmatthias.ringwald     // global/non-connection oriented commands
2074b031bebbSmatthias.ringwald 
2075b031bebbSmatthias.ringwald     // decline incoming connections
20763a9fb326S[email protected]     if (hci_stack->decline_reason){
20773a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
20783a9fb326S[email protected]         hci_stack->decline_reason = 0;
20793a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
2080dbe1a790S[email protected]         return;
2081ce4c8fabSmatthias.ringwald     }
2082ce4c8fabSmatthias.ringwald 
2083b031bebbSmatthias.ringwald     // send scan enable
20843a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
20853a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
20863a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
2087dbe1a790S[email protected]         return;
2088b031bebbSmatthias.ringwald     }
2089b031bebbSmatthias.ringwald 
2090b2f949feS[email protected] #ifdef HAVE_BLE
20917bdc6798S[email protected]     // handle le scan
20927bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
20937bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
20947bdc6798S[email protected]             case LE_START_SCAN:
20957bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
20967bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
20977bdc6798S[email protected]                 return;
20987bdc6798S[email protected] 
20997bdc6798S[email protected]             case LE_STOP_SCAN:
21007bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
21017bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
21027bdc6798S[email protected]                 return;
21037bdc6798S[email protected]             default:
21047bdc6798S[email protected]                 break;
21057bdc6798S[email protected]         }
2106e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
2107e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
2108e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
2109e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
2110e2602ea2Smatthias.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);
2111e2602ea2Smatthias.ringwald             return;
2112e2602ea2Smatthias.ringwald         }
21137bdc6798S[email protected]     }
2114b2f949feS[email protected] #endif
21157bdc6798S[email protected] 
211632ab9390Smatthias.ringwald     // send pending HCI commands
21173a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
2118b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
211932ab9390Smatthias.ringwald 
21200bf6344aS[email protected]         switch(connection->state){
21210bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
21224f3229d8S[email protected]                 switch(connection->address_type){
21234f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
21249da54300S[email protected]                         log_info("sending hci_create_connection");
2125ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
21264f3229d8S[email protected]                         break;
21274f3229d8S[email protected]                     default:
2128b2f949feS[email protected] #ifdef HAVE_BLE
21299da54300S[email protected]                         log_info("sending hci_le_create_connection");
21304f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
2131b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
2132b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
21334f3229d8S[email protected]                                      0,         // don't use whitelist
21344f3229d8S[email protected]                                      connection->address_type, // peer address type
21354f3229d8S[email protected]                                      connection->address,      // peer bd addr
2136b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
2137b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
2138b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
21394f3229d8S[email protected]                                      0,         // conn latency
2140b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
2141b2571179Smatthias.ringwald                                      0x0001,    // min ce length
2142b2571179Smatthias.ringwald                                      0x0001     // max ce length
21434f3229d8S[email protected]                                      );
21444f3229d8S[email protected] 
21454f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
2146b2f949feS[email protected] #endif
21474f3229d8S[email protected]                         break;
21484f3229d8S[email protected]                 }
2149ad83dc6aS[email protected]                 return;
2150ad83dc6aS[email protected] 
21510bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
21529da54300S[email protected]                 log_info("sending hci_accept_connection_request");
215332ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
2154e35edcc1S[email protected]                 if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
215534d2123cS[email protected]                     hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
2156e35edcc1S[email protected]                 } else {
2157e35edcc1S[email protected]                     // TODO: allows to customize synchronous connection parameters
2158e35edcc1S[email protected]                     hci_send_cmd(&hci_accept_synchronous_connection_command, connection->address, 8000, 8000, 0xFFFF, 0x0060, 0xFF, 0x003F);
2159e35edcc1S[email protected]                 }
2160dbe1a790S[email protected]                 return;
21610bf6344aS[email protected] 
2162a6725849S[email protected] #ifdef HAVE_BLE
21630bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
21640bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
21650bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
21660bf6344aS[email protected]                 return;
2167a6725849S[email protected] #endif
21680bf6344aS[email protected]             case SEND_DISCONNECT:
21690bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
21707851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
21710bf6344aS[email protected]                 return;
21720bf6344aS[email protected] 
21730bf6344aS[email protected]             default:
21740bf6344aS[email protected]                 break;
2175c7e0c5f6Smatthias.ringwald         }
2176c7e0c5f6Smatthias.ringwald 
217732ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
21789da54300S[email protected]             log_info("responding to link key request");
217934d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
218032ab9390Smatthias.ringwald             link_key_t link_key;
2181c77e8838S[email protected]             link_key_type_t link_key_type;
21823a9fb326S[email protected]             if ( hci_stack->remote_device_db
21832e77e513S[email protected]               && hci_stack->remote_device_db->get_link_key(connection->address, link_key, &link_key_type)
218434d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
21859ab95c90S[email protected]                connection->link_key_type = link_key_type;
218632ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
218732ab9390Smatthias.ringwald             } else {
218832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
218932ab9390Smatthias.ringwald             }
2190dbe1a790S[email protected]             return;
219132ab9390Smatthias.ringwald         }
21921d6b20aeS[email protected] 
2193899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
21949da54300S[email protected]             log_info("denying to pin request");
2195899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
219634d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
21974c57c146S[email protected]             return;
21984c57c146S[email protected]         }
21994c57c146S[email protected] 
2200dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
220134d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
220282d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
220382d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
2204106d6d11S[email protected]                 // tweak authentication requirements
22053a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
2206106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
22079faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
22089faad3abS[email protected]                 }
22099faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
22109faad3abS[email protected]                     authreq |= 1;
2211106d6d11S[email protected]                 }
22123a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
2213f8fb5f6eS[email protected]             } else {
2214f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
2215f8fb5f6eS[email protected]             }
2216dbe1a790S[email protected]             return;
221732ab9390Smatthias.ringwald         }
221832ab9390Smatthias.ringwald 
2219dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
2220dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
222134d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
2222dbe1a790S[email protected]             return;
2223dbe1a790S[email protected]         }
2224dbe1a790S[email protected] 
2225dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
2226dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
222734d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
2228dbe1a790S[email protected]             return;
2229dbe1a790S[email protected]         }
2230afd4e962S[email protected] 
2231afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
2232afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
223334d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
22342bd8b7e7S[email protected]             return;
22352bd8b7e7S[email protected]         }
22362bd8b7e7S[email protected] 
22372bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
22382bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
223934d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
224034d2123cS[email protected]             return;
224134d2123cS[email protected]         }
2242ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
2243ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
22441bd5283dS[email protected]             connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT;
224552d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
2246ad83dc6aS[email protected]             return;
2247ad83dc6aS[email protected]         }
224834d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
224934d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
225034d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
22512bd8b7e7S[email protected]             return;
2252afd4e962S[email protected]         }
2253dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
2254dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
2255dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
2256dce78009S[email protected]             return;
2257dce78009S[email protected]         }
2258da886c03S[email protected] 
2259c37a3166S[email protected] #ifdef HAVE_BLE
2260da886c03S[email protected]         if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){
2261da886c03S[email protected]             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
2262da886c03S[email protected] 
2263c37a3166S[email protected]             uint16_t connection_interval_min = connection->le_conn_interval_min;
2264c37a3166S[email protected]             connection->le_conn_interval_min = 0;
2265c37a3166S[email protected]             hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min,
2266c37a3166S[email protected]                 connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout,
2267c37a3166S[email protected]                 0x0000, 0xffff);
2268c37a3166S[email protected]         }
2269c37a3166S[email protected] #endif
2270dbe1a790S[email protected]     }
2271c7e0c5f6Smatthias.ringwald 
22723a9fb326S[email protected]     switch (hci_stack->state){
22733429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
227474b323a9SMatthias Ringwald             hci_initializing_run();
22753429f56bSmatthias.ringwald             break;
2276c7e0c5f6Smatthias.ringwald 
2277c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
2278c7e0c5f6Smatthias.ringwald 
22799da54300S[email protected]             log_info("HCI_STATE_HALTING");
2280c7e0c5f6Smatthias.ringwald             // close all open connections
22813a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
2282c7e0c5f6Smatthias.ringwald             if (connection){
228332ab9390Smatthias.ringwald 
2284c7e0c5f6Smatthias.ringwald                 // send disconnect
2285d94d3cafS[email protected]                 if (!hci_can_send_command_packet_now()) return;
228632ab9390Smatthias.ringwald 
22879da54300S[email protected]                 log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
22886ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
2289c7e0c5f6Smatthias.ringwald 
2290c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
2291c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
2292c7e0c5f6Smatthias.ringwald                 return;
2293c7e0c5f6Smatthias.ringwald             }
22949da54300S[email protected]             log_info("HCI_STATE_HALTING, calling off");
2295c7e0c5f6Smatthias.ringwald 
229672ea5239Smatthias.ringwald             // switch mode
2297c7e0c5f6Smatthias.ringwald             hci_power_control_off();
22989418f9c9Smatthias.ringwald 
22999da54300S[email protected]             log_info("HCI_STATE_HALTING, emitting state");
230072ea5239Smatthias.ringwald             hci_emit_state();
23019da54300S[email protected]             log_info("HCI_STATE_HALTING, done");
230272ea5239Smatthias.ringwald             break;
2303c7e0c5f6Smatthias.ringwald 
230472ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
23053a9fb326S[email protected]             switch(hci_stack->substate) {
230674b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_DISCONNECT:
23079da54300S[email protected]                     log_info("HCI_STATE_FALLING_ASLEEP");
230872ea5239Smatthias.ringwald                     // close all open connections
23093a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
231066da7044Smatthias.ringwald 
231128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
231266da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
231366da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
231466da7044Smatthias.ringwald                         connection = NULL;
231566da7044Smatthias.ringwald                     }
231666da7044Smatthias.ringwald #endif
231772ea5239Smatthias.ringwald                     if (connection){
231832ab9390Smatthias.ringwald 
231972ea5239Smatthias.ringwald                         // send disconnect
2320d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
232132ab9390Smatthias.ringwald 
23229da54300S[email protected]                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
23236ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
232472ea5239Smatthias.ringwald 
232572ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
232672ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
232772ea5239Smatthias.ringwald                         return;
232872ea5239Smatthias.ringwald                     }
232972ea5239Smatthias.ringwald 
233092368cd3S[email protected]                     if (hci_classic_supported()){
233189db417bSmatthias.ringwald                         // disable page and inquiry scan
2332d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
233332ab9390Smatthias.ringwald 
23349da54300S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans");
23353a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
233689db417bSmatthias.ringwald 
233789db417bSmatthias.ringwald                         // continue in next sub state
233874b323a9SMatthias Ringwald                         hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE;
233989db417bSmatthias.ringwald                         break;
234092368cd3S[email protected]                     }
234192368cd3S[email protected]                     // fall through for ble-only chips
234292368cd3S[email protected] 
234374b323a9SMatthias Ringwald                 case HCI_FALLING_ASLEEP_COMPLETE:
23449da54300S[email protected]                     log_info("HCI_STATE_HALTING, calling sleep");
234528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
234628171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
234728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
234828171530Smatthias.ringwald                         // SLEEP MODE reached
23493a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
235028171530Smatthias.ringwald                         hci_emit_state();
235128171530Smatthias.ringwald                         break;
235228171530Smatthias.ringwald                     }
235328171530Smatthias.ringwald #endif
235472ea5239Smatthias.ringwald                     // switch mode
23553a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
2356c7e0c5f6Smatthias.ringwald                     hci_emit_state();
235728171530Smatthias.ringwald                     break;
235828171530Smatthias.ringwald 
235989db417bSmatthias.ringwald                 default:
236089db417bSmatthias.ringwald                     break;
236189db417bSmatthias.ringwald             }
2362c7e0c5f6Smatthias.ringwald             break;
2363c7e0c5f6Smatthias.ringwald 
23643429f56bSmatthias.ringwald         default:
23653429f56bSmatthias.ringwald             break;
23661f504dbdSmatthias.ringwald     }
23673429f56bSmatthias.ringwald }
236816833f0aSmatthias.ringwald 
236931452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
2370c8e4258aSmatthias.ringwald     bd_addr_t addr;
2371c8e4258aSmatthias.ringwald     hci_connection_t * conn;
2372c8e4258aSmatthias.ringwald     // house-keeping
2373c8e4258aSmatthias.ringwald 
2374c8e4258aSmatthias.ringwald     // create_connection?
2375c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
2376c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
23779da54300S[email protected]         log_info("Create_connection to %s", bd_addr_to_str(addr));
2378c8e4258aSmatthias.ringwald 
23792e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2380ad83dc6aS[email protected]         if (!conn){
238196a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
238217f1ba2aSmatthias.ringwald             if (!conn){
238317f1ba2aSmatthias.ringwald                 // notify client that alloc failed
238417f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
238517f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
238617f1ba2aSmatthias.ringwald             }
2387ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
2388ad83dc6aS[email protected]         }
2389ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
2390ad83dc6aS[email protected]         switch (conn->state){
2391ad83dc6aS[email protected]             // if connection active exists
2392ad83dc6aS[email protected]             case OPEN:
239362bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
2394ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
239562bda3fbS[email protected]                 return 0;
2396ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
2397ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
2398ad83dc6aS[email protected]                 break;
2399ad83dc6aS[email protected]             default:
2400ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
2401ad83dc6aS[email protected]                 return 0;
2402ad83dc6aS[email protected]         }
2403c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
2404c8e4258aSmatthias.ringwald     }
24057fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
24067fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
24077fde4af9Smatthias.ringwald     }
24087fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
24097fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
24107fde4af9Smatthias.ringwald     }
24117fde4af9Smatthias.ringwald 
24128ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
24133a9fb326S[email protected]         if (hci_stack->remote_device_db){
24148ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
24152e77e513S[email protected]             hci_stack->remote_device_db->delete_link_key(addr);
24168ef73945Smatthias.ringwald         }
24178ef73945Smatthias.ringwald     }
2418c8e4258aSmatthias.ringwald 
24196724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
24206724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
24216724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
24222e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
24236724cd9eS[email protected]         if (conn){
24246724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
24256724cd9eS[email protected]         }
24266724cd9eS[email protected]     }
24276724cd9eS[email protected] 
24286724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
24296724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
24306724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
24316724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
24326724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
24332e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
24346724cd9eS[email protected]         if (conn){
24356724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
24366724cd9eS[email protected]         }
24376724cd9eS[email protected]     }
24386724cd9eS[email protected] 
243969a97523S[email protected] #ifdef HAVE_BLE
244069a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
24413a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
244269a97523S[email protected]     }
244369a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
24443a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
244569a97523S[email protected]     }
244669a97523S[email protected] #endif
244769a97523S[email protected] 
24483a9fb326S[email protected]     hci_stack->num_cmd_packets--;
24495bb5bc3eS[email protected] 
24505bb5bc3eS[email protected]     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size);
24516b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
24526b4af23dS[email protected] 
2453d051460cS[email protected]     // release packet buffer for synchronous transport implementations
2454c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
24556b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
24566b4af23dS[email protected]     }
24576b4af23dS[email protected] 
24586b4af23dS[email protected]     return err;
245931452debSmatthias.ringwald }
24608adf0ddaSmatthias.ringwald 
24612bd8b7e7S[email protected] // disconnect because of security block
24622bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
24632bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
24642bd8b7e7S[email protected]     if (!connection) return;
24652bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
24662bd8b7e7S[email protected] }
24672bd8b7e7S[email protected] 
24682bd8b7e7S[email protected] 
2469dbe1a790S[email protected] // Configure Secure Simple Pairing
2470dbe1a790S[email protected] 
2471dbe1a790S[email protected] // enable will enable SSP during init
2472dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
24733a9fb326S[email protected]     hci_stack->ssp_enable = enable;
2474dbe1a790S[email protected] }
2475dbe1a790S[email protected] 
247671de195eSMatthias Ringwald int hci_local_ssp_activated(void){
24773a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
24782bd8b7e7S[email protected] }
24792bd8b7e7S[email protected] 
2480dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
2481dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
24823a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
2483dbe1a790S[email protected] }
2484dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
24853a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
2486dbe1a790S[email protected] }
2487dbe1a790S[email protected] 
2488dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
2489dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
24903a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
2491dbe1a790S[email protected] }
2492dbe1a790S[email protected] 
24931cd208adSmatthias.ringwald /**
24941cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
24951cd208adSmatthias.ringwald  */
2496fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
24975127cc62S[email protected] 
2498d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()){
24999d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
25009d14b626S[email protected]         return 0;
25019d14b626S[email protected]     }
25029d14b626S[email protected] 
25035127cc62S[email protected]     // for HCI INITIALIZATION
25049da54300S[email protected]     // log_info("hci_send_cmd: opcode %04x", cmd->opcode);
25055127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
25065127cc62S[email protected] 
25079d14b626S[email protected]     hci_reserve_packet_buffer();
25089d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
25099d14b626S[email protected] 
25101cd208adSmatthias.ringwald     va_list argptr;
25111cd208adSmatthias.ringwald     va_start(argptr, cmd);
25129d14b626S[email protected]     uint16_t size = hci_create_cmd_internal(packet, cmd, argptr);
25131cd208adSmatthias.ringwald     va_end(argptr);
25149d14b626S[email protected] 
25159d14b626S[email protected]     return hci_send_cmd_packet(packet, size);
251693b8dc03Smatthias.ringwald }
2517c8e4258aSmatthias.ringwald 
2518ee091cf1Smatthias.ringwald // Create various non-HCI events.
2519ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
2520ee091cf1Smatthias.ringwald 
252171de195eSMatthias Ringwald void hci_emit_state(void){
25223a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
2523425d1371Smatthias.ringwald     uint8_t event[3];
252480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
2525425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
25263a9fb326S[email protected]     event[2] = hci_stack->state;
2527425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
25283a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2529c8e4258aSmatthias.ringwald }
2530c8e4258aSmatthias.ringwald 
253117f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
2532425d1371Smatthias.ringwald     uint8_t event[13];
2533c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
2534425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
253517f1ba2aSmatthias.ringwald     event[2] = status;
2536c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
2537c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
2538c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
2539c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
2540425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
25413a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2542c8e4258aSmatthias.ringwald }
2543c8e4258aSmatthias.ringwald 
25442e77e513S[email protected] void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t address, uint16_t conn_handle, uint8_t status){
25454f3229d8S[email protected]     uint8_t event[21];
25464f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
25474f3229d8S[email protected]     event[1] = sizeof(event) - 2;
25484f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
25494f3229d8S[email protected]     event[3] = status;
25506e2e9a6bS[email protected]     bt_store_16(event, 4, conn_handle);
25514f3229d8S[email protected]     event[6] = 0; // TODO: role
25526e2e9a6bS[email protected]     event[7] = address_type;
25532e77e513S[email protected]     bt_flip_addr(&event[8], address);
25544f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
25554f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
25564f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
25574f3229d8S[email protected]     event[20] = 0; // master clock accuracy
25584f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
25594f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
25604f3229d8S[email protected] }
25614f3229d8S[email protected] 
25623c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
2563425d1371Smatthias.ringwald     uint8_t event[6];
25643c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
2565e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
25663c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
25673c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
25683c4d4b90Smatthias.ringwald     event[5] = reason;
2569425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
25703a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
25713c4d4b90Smatthias.ringwald }
25723c4d4b90Smatthias.ringwald 
2573ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
257466fb9560S[email protected]     if (disable_l2cap_timeouts) return;
2575e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2576425d1371Smatthias.ringwald     uint8_t event[4];
257780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2578425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2579ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2580425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
25813a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2582ee091cf1Smatthias.ringwald }
258343bfb1bdSmatthias.ringwald 
258471de195eSMatthias Ringwald void hci_emit_nr_connections_changed(void){
2585e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2586425d1371Smatthias.ringwald     uint8_t event[3];
258780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2588425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
258943bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2590425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
25913a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
259243bfb1bdSmatthias.ringwald }
2593038bc64cSmatthias.ringwald 
259471de195eSMatthias Ringwald void hci_emit_hci_open_failed(void){
2595e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2596425d1371Smatthias.ringwald     uint8_t event[2];
259780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2598425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2599425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
26003a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2601038bc64cSmatthias.ringwald }
26021b0e3922Smatthias.ringwald 
260309ba8edeSmatthias.ringwald #ifndef EMBEDDED
260471de195eSMatthias Ringwald void hci_emit_btstack_version(void){
2605e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2606425d1371Smatthias.ringwald     uint8_t event[6];
26071b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2608425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2609425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2610425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
26111ebf5f9cSMatthias Ringwald     bt_store_16(event, 4, 3257);    // last SVN commit on Google Code + 1
2612425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
26133a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
26141b0e3922Smatthias.ringwald }
261509ba8edeSmatthias.ringwald #endif
26161b0e3922Smatthias.ringwald 
26172ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2618e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2619425d1371Smatthias.ringwald     uint8_t event[3];
26202ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2621425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
26222ed6235cSmatthias.ringwald     event[2] = enabled;
2623425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
26243a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
26252ed6235cSmatthias.ringwald }
2626627c2f45Smatthias.ringwald 
26272e77e513S[email protected] void hci_emit_remote_name_cached(bd_addr_t addr, device_name_t *name){
2628e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2629627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2630e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2631f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
26322e77e513S[email protected]     bt_flip_addr(&event[3], addr);
2633f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2634e0abb8e7S[email protected] 
2635e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
26362e77e513S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(addr), &event[9]);
2637e0abb8e7S[email protected] 
2638e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
26393a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2640627c2f45Smatthias.ringwald }
2641381fbed8Smatthias.ringwald 
2642381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2643e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2644425d1371Smatthias.ringwald     uint8_t event[3];
2645381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2646425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2647381fbed8Smatthias.ringwald     event[2] = enabled;
2648425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
26493a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2650381fbed8Smatthias.ringwald }
2651458bf4e8S[email protected] 
2652a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2653df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2654a00031e2S[email protected]     uint8_t event[5];
2655e00caf9cS[email protected]     int pos = 0;
2656a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2657e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2658a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2659e00caf9cS[email protected]     pos += 2;
2660e00caf9cS[email protected]     event[pos++] = level;
2661e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
26623a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2663e00caf9cS[email protected] }
2664e00caf9cS[email protected] 
26651bd5283dS[email protected] void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){
2666ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2667ad83dc6aS[email protected]     uint8_t event[9];
2668ad83dc6aS[email protected]     int pos = 0;
2669ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2670ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2671ad83dc6aS[email protected]     event[pos++] = status;
26722e77e513S[email protected]     bt_flip_addr( &event[pos], address);
2673ad83dc6aS[email protected]     pos += 6;
2674ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
26753a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2676ad83dc6aS[email protected] }
2677ad83dc6aS[email protected] 
26782bd8b7e7S[email protected] // query if remote side supports SSP
26792bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
26802bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
26812bd8b7e7S[email protected]     if (!connection) return 0;
26822bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
26832bd8b7e7S[email protected] }
26842bd8b7e7S[email protected] 
2685df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2686df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2687df3354fcS[email protected] }
2688df3354fcS[email protected] 
2689458bf4e8S[email protected] // GAP API
2690458bf4e8S[email protected] /**
2691458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2692458bf4e8S[email protected]  * @praram enabled
2693458bf4e8S[email protected]  */
26944c57c146S[email protected] void gap_set_bondable_mode(int enable){
26953a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2696458bf4e8S[email protected] }
2697cb230b9dS[email protected] 
2698cb230b9dS[email protected] /**
269934d2123cS[email protected]  * @brief map link keys to security levels
2700cb230b9dS[email protected]  */
270134d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
270234d2123cS[email protected]     switch (link_key_type){
27033c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
27043c68dfa9S[email protected]             return LEVEL_4;
27053c68dfa9S[email protected]         case COMBINATION_KEY:
27063c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
27073c68dfa9S[email protected]             return LEVEL_3;
27083c68dfa9S[email protected]         default:
27093c68dfa9S[email protected]             return LEVEL_2;
27103c68dfa9S[email protected]     }
2711cb230b9dS[email protected] }
2712cb230b9dS[email protected] 
2713a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
271434d2123cS[email protected]     if (!connection) return LEVEL_0;
271534d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
271634d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
271734d2123cS[email protected] }
271834d2123cS[email protected] 
271934d2123cS[email protected] 
2720106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
27215127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
2722106d6d11S[email protected]     return level > LEVEL_2;
2723106d6d11S[email protected] }
2724106d6d11S[email protected] 
272534d2123cS[email protected] /**
272634d2123cS[email protected]  * @brief get current security level
272734d2123cS[email protected]  */
272834d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
272934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
273034d2123cS[email protected]     if (!connection) return LEVEL_0;
273134d2123cS[email protected]     return gap_security_level_for_connection(connection);
273234d2123cS[email protected] }
273334d2123cS[email protected] 
2734cb230b9dS[email protected] /**
2735cb230b9dS[email protected]  * @brief request connection to device to
2736cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2737cb230b9dS[email protected]  */
273834d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
273934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
274034d2123cS[email protected]     if (!connection){
2741a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
274234d2123cS[email protected]         return;
274334d2123cS[email protected]     }
274434d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
274534d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
274634d2123cS[email protected]     if (current_level >= requested_level){
2747a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
274834d2123cS[email protected]         return;
274934d2123cS[email protected]     }
2750a00031e2S[email protected] 
275134d2123cS[email protected]     connection->requested_security_level = requested_level;
2752a00031e2S[email protected] 
275325bf5872S[email protected] #if 0
275425bf5872S[email protected]     // sending encryption request without a link key results in an error.
275525bf5872S[email protected]     // TODO: figure out how to use it properly
275625bf5872S[email protected] 
2757fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
27583a9fb326S[email protected]     if (hci_stack->remote_device_db){
2759a00031e2S[email protected]         link_key_type_t link_key_type;
2760a00031e2S[email protected]         link_key_t      link_key;
27613a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2762a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2763a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2764a00031e2S[email protected]                 return;
2765a00031e2S[email protected]             }
2766a00031e2S[email protected]         }
2767a00031e2S[email protected]     }
276825bf5872S[email protected] #endif
2769a00031e2S[email protected] 
27701eb2563eS[email protected]     // try to authenticate connection
27711eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2772e80b2cf9S[email protected]     hci_run();
2773e00caf9cS[email protected] }
2774ad83dc6aS[email protected] 
2775ad83dc6aS[email protected] /**
2776ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2777ad83dc6aS[email protected]  * @param device
2778ad83dc6aS[email protected]  * @param request MITM protection
2779ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2780ad83dc6aS[email protected]  */
2781ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2782ad83dc6aS[email protected] 
2783ad83dc6aS[email protected]     // create connection state machine
278496a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2785ad83dc6aS[email protected] 
2786ad83dc6aS[email protected]     if (!connection){
2787ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2788ad83dc6aS[email protected]     }
2789ad83dc6aS[email protected] 
2790ad83dc6aS[email protected]     // delete linkn key
27912e77e513S[email protected]     hci_drop_link_key_for_bd_addr(device);
2792ad83dc6aS[email protected] 
2793ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2794ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2795ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
27965127cc62S[email protected]     log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level);
2797ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2798ad83dc6aS[email protected] 
2799ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2800ad83dc6aS[email protected] 
2801ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2802ad83dc6aS[email protected]     // handle: authentication failure
2803ad83dc6aS[email protected]     // handle: disconnect on done
2804ad83dc6aS[email protected] 
2805ad83dc6aS[email protected]     hci_run();
2806ad83dc6aS[email protected] 
2807ad83dc6aS[email protected]     return 0;
2808ad83dc6aS[email protected] }
28098e618f72S[email protected] 
28108e618f72S[email protected] void gap_set_local_name(const char * local_name){
28118e618f72S[email protected]     hci_stack->local_name = local_name;
28128e618f72S[email protected] }
28138e618f72S[email protected] 
281471de195eSMatthias Ringwald le_command_status_t le_central_start_scan(void){
281536af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
28167bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
28177bdc6798S[email protected]     hci_run();
28187bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
28197bdc6798S[email protected] }
28208e618f72S[email protected] 
282171de195eSMatthias Ringwald le_command_status_t le_central_stop_scan(void){
282236af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
28237bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
28247bdc6798S[email protected]     hci_run();
28257bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
28267bdc6798S[email protected] }
28274f3229d8S[email protected] 
2828ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2829ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2830ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2831ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2832ef11999fSmatthias.ringwald     hci_run();
2833ef11999fSmatthias.ringwald }
28344f3229d8S[email protected] 
28352e77e513S[email protected] le_command_status_t le_central_connect(bd_addr_t  addr, bd_addr_type_t addr_type){
28364f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
28374f3229d8S[email protected]     if (!conn){
28381f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
28392e77e513S[email protected]         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
28404f3229d8S[email protected]         if (!conn){
28414f3229d8S[email protected]             // notify client that alloc failed
28426e2e9a6bS[email protected]             hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
28436e2e9a6bS[email protected]             log_info("le_central_connect: failed to alloc hci_connection_t");
28444f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
28454f3229d8S[email protected]         }
28464f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
28471f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2848564fca32S[email protected]         hci_run();
28494f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
28504f3229d8S[email protected]     }
28510bf6344aS[email protected] 
28520bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
28530bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
28540bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
28552e77e513S[email protected]         hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED);
28561f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
28570bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
28580bf6344aS[email protected]     }
28590bf6344aS[email protected] 
28601f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
28612e77e513S[email protected]     hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0);
28624f3229d8S[email protected]     hci_run();
28634f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
28644f3229d8S[email protected] }
28654f3229d8S[email protected] 
28667851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
286771de195eSMatthias Ringwald static hci_connection_t * le_central_get_outgoing_connection(void){
28680bf6344aS[email protected]     linked_item_t *it;
28690bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
28700bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
28710bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
28720bf6344aS[email protected]         switch (conn->state){
2873a6725849S[email protected]             case SEND_CREATE_CONNECTION:
28747851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
28757851196eSmatthias.ringwald                 return conn;
28767851196eSmatthias.ringwald             default:
28777851196eSmatthias.ringwald                 break;
28787851196eSmatthias.ringwald         };
28797851196eSmatthias.ringwald     }
28807851196eSmatthias.ringwald     return NULL;
28817851196eSmatthias.ringwald }
28827851196eSmatthias.ringwald 
288371de195eSMatthias Ringwald le_command_status_t le_central_connect_cancel(void){
28847851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
28857851196eSmatthias.ringwald     switch (conn->state){
28867851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
28877851196eSmatthias.ringwald             // skip sending create connection and emit event instead
28882e77e513S[email protected]             hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
28897851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
28907851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
28910bf6344aS[email protected]             break;
2892a6725849S[email protected]         case SENT_CREATE_CONNECTION:
28937851196eSmatthias.ringwald             // request to send cancel connection
28940bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
28950bf6344aS[email protected]             hci_run();
28960bf6344aS[email protected]             break;
28970bf6344aS[email protected]         default:
28980bf6344aS[email protected]             break;
28990bf6344aS[email protected]     }
2900e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2901e31f89a7S[email protected] }
29024f3229d8S[email protected] 
2903c37a3166S[email protected] /**
2904c37a3166S[email protected]  * @brief Updates the connection parameters for a given LE connection
2905c37a3166S[email protected]  * @param handle
2906c37a3166S[email protected]  * @param conn_interval_min (unit: 1.25ms)
2907c37a3166S[email protected]  * @param conn_interval_max (unit: 1.25ms)
2908c37a3166S[email protected]  * @param conn_latency
2909c37a3166S[email protected]  * @param supervision_timeout (unit: 10ms)
2910c37a3166S[email protected]  * @returns 0 if ok
2911c37a3166S[email protected]  */
2912c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
2913c37a3166S[email protected]     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
2914c37a3166S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
2915c37a3166S[email protected]     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2916c37a3166S[email protected]     connection->le_conn_interval_min = conn_interval_min;
2917c37a3166S[email protected]     connection->le_conn_interval_max = conn_interval_max;
2918c37a3166S[email protected]     connection->le_conn_latency = conn_latency;
2919c37a3166S[email protected]     connection->le_supervision_timeout = supervision_timeout;
2920c37a3166S[email protected]     return 0;
2921c37a3166S[email protected] }
2922c37a3166S[email protected] 
29235917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
29245917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
29255917a5c5S[email protected]     if (!conn){
29267851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
29275917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
29285917a5c5S[email protected]     }
29295917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
29305917a5c5S[email protected]     hci_run();
29314f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
29324f3229d8S[email protected] }
293304a6ef8cSmatthias.ringwald 
2934d23838ecSMatthias Ringwald /**
2935d23838ecSMatthias Ringwald  * @brief Set callback for Bluetooth Hardware Error
2936d23838ecSMatthias Ringwald  */
2937d23838ecSMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(void)){
2938d23838ecSMatthias Ringwald     hci_stack->hardware_error_callback = fn;
2939d23838ecSMatthias Ringwald }
2940d23838ecSMatthias Ringwald 
2941d23838ecSMatthias Ringwald 
294271de195eSMatthias Ringwald void hci_disconnect_all(void){
294304a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
294404a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
294504a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
294604a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
294704a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
294804a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
294904a6ef8cSmatthias.ringwald     }
2950d31fba26S[email protected]     hci_run();
295104a6ef8cSmatthias.ringwald }
2952