xref: /btstack/src/hci.c (revision 690bd0bab814922b1c953d2691dac79068c2fb89)
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 
72a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
73da5275c5S[email protected] 
7428171530Smatthias.ringwald #ifdef USE_BLUETOOL
753d7a3f67S[email protected] #include "../platforms/ios/src/bt_control_iphone.h"
7628171530Smatthias.ringwald #endif
7728171530Smatthias.ringwald 
78758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
79a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
8096a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer);
8196a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
827586ee35S[email protected] static int  hci_power_control_on(void);
837586ee35S[email protected] static void hci_power_control_off(void);
846155b3d3S[email protected] static void hci_state_reset();
85758b46ceSmatthias.ringwald 
8606b35ec0Smatthias.ringwald // the STACK is here
873a9fb326S[email protected] #ifndef HAVE_MALLOC
883a9fb326S[email protected] static hci_stack_t   hci_stack_static;
893a9fb326S[email protected] #endif
903a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
9116833f0aSmatthias.ringwald 
9266fb9560S[email protected] // test helper
9366fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
9466fb9560S[email protected] 
9596a45072S[email protected] /**
9696a45072S[email protected]  * create connection for given address
9796a45072S[email protected]  *
9896a45072S[email protected]  * @return connection OR NULL, if no memory left
9996a45072S[email protected]  */
10096a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
1011a06f663S[email protected]     log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type);
102bb69aaaeS[email protected]     hci_connection_t * conn = btstack_memory_hci_connection_get();
10396a45072S[email protected]     if (!conn) return NULL;
104c91d150bS[email protected]     memset(conn, 0, sizeof(hci_connection_t));
10596a45072S[email protected]     BD_ADDR_COPY(conn->address, addr);
10696a45072S[email protected]     conn->address_type = addr_type;
10796a45072S[email protected]     conn->con_handle = 0xffff;
10896a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
10996a45072S[email protected]     conn->bonding_flags = 0;
11096a45072S[email protected]     conn->requested_security_level = LEVEL_0;
11196a45072S[email protected]     linked_item_set_user(&conn->timeout.item, conn);
11296a45072S[email protected]     conn->timeout.process = hci_connection_timeout_handler;
11396a45072S[email protected]     hci_connection_timestamp(conn);
11496a45072S[email protected]     conn->acl_recombination_length = 0;
11596a45072S[email protected]     conn->acl_recombination_pos = 0;
11696a45072S[email protected]     conn->num_acl_packets_sent = 0;
1171a06f663S[email protected]     conn->num_sco_packets_sent = 0;
118da886c03S[email protected]     conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
11996a45072S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
12096a45072S[email protected]     return conn;
12196a45072S[email protected] }
12266fb9560S[email protected] 
123da886c03S[email protected] 
124da886c03S[email protected] /**
125da886c03S[email protected]  * get le connection parameter range
126da886c03S[email protected] *
127da886c03S[email protected]  * @return le connection parameter range struct
128da886c03S[email protected]  */
129da886c03S[email protected] le_connection_parameter_range_t gap_le_get_connection_parameter_range(){
130da886c03S[email protected]     return hci_stack->le_connection_parameter_range;
131da886c03S[email protected] }
132da886c03S[email protected] 
133da886c03S[email protected] /**
134da886c03S[email protected]  * set le connection parameter range
135da886c03S[email protected]  *
136da886c03S[email protected]  */
137da886c03S[email protected] 
138da886c03S[email protected] void gap_le_set_connection_parameter_range(le_connection_parameter_range_t range){
139da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_interval_min;
140da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_interval_max;
141da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_latency_min;
142da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_latency_max;
143da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_min = range.le_supervision_timeout_min;
144da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_max = range.le_supervision_timeout_max;
145da886c03S[email protected] }
146da886c03S[email protected] 
147da886c03S[email protected] /**
148da886c03S[email protected]  * get hci connections iterator
149da886c03S[email protected]  *
150da886c03S[email protected]  * @return hci connections iterator
151da886c03S[email protected]  */
152da886c03S[email protected] 
153da886c03S[email protected] void hci_connections_get_iterator(linked_list_iterator_t *it){
154da886c03S[email protected]     linked_list_iterator_init(it, &hci_stack->connections);
155da886c03S[email protected] }
156da886c03S[email protected] 
15797addcc5Smatthias.ringwald /**
158ee091cf1Smatthias.ringwald  * get connection for a given handle
159ee091cf1Smatthias.ringwald  *
160ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
161ee091cf1Smatthias.ringwald  */
1625061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
163da886c03S[email protected]     linked_list_iterator_t it;
164da886c03S[email protected]     linked_list_iterator_init(&it, &hci_stack->connections);
165da886c03S[email protected]     while (linked_list_iterator_has_next(&it)){
166da886c03S[email protected]         hci_connection_t * item = (hci_connection_t *) linked_list_iterator_next(&it);
1673ac2fe56S[email protected]         if ( item->con_handle == con_handle ) {
168da886c03S[email protected]             return item;
169ee091cf1Smatthias.ringwald         }
170ee091cf1Smatthias.ringwald     }
171ee091cf1Smatthias.ringwald     return NULL;
172ee091cf1Smatthias.ringwald }
173ee091cf1Smatthias.ringwald 
17496a45072S[email protected] /**
17596a45072S[email protected]  * get connection for given address
17696a45072S[email protected]  *
17796a45072S[email protected]  * @return connection OR NULL, if not found
17896a45072S[email protected]  */
1792e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t  addr, bd_addr_type_t addr_type){
180da886c03S[email protected]     linked_list_iterator_t it;
181da886c03S[email protected]     linked_list_iterator_init(&it, &hci_stack->connections);
182da886c03S[email protected]     while (linked_list_iterator_has_next(&it)){
183da886c03S[email protected]         hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it);
18496a45072S[email protected]         if (connection->address_type != addr_type)  continue;
18596a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
18662bda3fbS[email protected]         return connection;
18762bda3fbS[email protected]     }
18862bda3fbS[email protected]     return NULL;
18962bda3fbS[email protected] }
19062bda3fbS[email protected] 
1912b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
19228ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
193c785ef68Smatthias.ringwald #ifdef HAVE_TIME
194ee091cf1Smatthias.ringwald     struct timeval tv;
195ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
196c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
197ee091cf1Smatthias.ringwald         // connections might be timed out
198ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
199ee091cf1Smatthias.ringwald     }
2002b12a0b9Smatthias.ringwald #endif
201e5780900Smatthias.ringwald #ifdef HAVE_TICK
202c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
203c785ef68Smatthias.ringwald         // connections might be timed out
204c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
205c785ef68Smatthias.ringwald     }
206c785ef68Smatthias.ringwald #endif
207c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
208c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
209c785ef68Smatthias.ringwald }
210ee091cf1Smatthias.ringwald 
211ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
212c7492964Smatthias.ringwald #ifdef HAVE_TIME
213ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
214c7492964Smatthias.ringwald #endif
215e5780900Smatthias.ringwald #ifdef HAVE_TICK
216c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
217c785ef68Smatthias.ringwald #endif
218ee091cf1Smatthias.ringwald }
219ee091cf1Smatthias.ringwald 
22006b35ec0Smatthias.ringwald 
22128ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
22228ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
22328ca2b46S[email protected] }
22428ca2b46S[email protected] 
22528ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
22628ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
22728ca2b46S[email protected] }
22828ca2b46S[email protected] 
22928ca2b46S[email protected] 
23043bfb1bdSmatthias.ringwald /**
23180ca58a0Smatthias.ringwald  * add authentication flags and reset timer
23296a45072S[email protected]  * @note: assumes classic connection
2332e77e513S[email protected]  * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets
2347fde4af9Smatthias.ringwald  */
2357fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
2367fde4af9Smatthias.ringwald     bd_addr_t addr;
2372e77e513S[email protected]     bt_flip_addr(addr, bd_addr);
2382e77e513S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2397fde4af9Smatthias.ringwald     if (conn) {
24028ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
24180ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
2427fde4af9Smatthias.ringwald     }
2437fde4af9Smatthias.ringwald }
2447fde4af9Smatthias.ringwald 
24580ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2465061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
24780ca58a0Smatthias.ringwald     if (!conn) return 0;
2486724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2496724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2506724cd9eS[email protected]     return 0;
25180ca58a0Smatthias.ringwald }
25280ca58a0Smatthias.ringwald 
2532e77e513S[email protected] void hci_drop_link_key_for_bd_addr(bd_addr_t addr){
2543a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2553a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
256c12e46e7Smatthias.ringwald     }
257c12e46e7Smatthias.ringwald }
258c12e46e7Smatthias.ringwald 
2590bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){
2600bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2610bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2620bf6344aS[email protected] }
2630bf6344aS[email protected] 
2647fde4af9Smatthias.ringwald 
2657fde4af9Smatthias.ringwald /**
26643bfb1bdSmatthias.ringwald  * count connections
26743bfb1bdSmatthias.ringwald  */
26840d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
26956c253c9Smatthias.ringwald     int count = 0;
27043bfb1bdSmatthias.ringwald     linked_item_t *it;
2713a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
27243bfb1bdSmatthias.ringwald     return count;
27343bfb1bdSmatthias.ringwald }
274c8e4258aSmatthias.ringwald 
27597addcc5Smatthias.ringwald /**
276ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
27716833f0aSmatthias.ringwald  */
2782718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
27916833f0aSmatthias.ringwald }
28016833f0aSmatthias.ringwald 
281998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2825061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
283998906cdSmatthias.ringwald     if (!connection) {
2849da54300S[email protected]         log_error("hci_number_outgoing_packets: connection for handle %u does not exist!", handle);
285998906cdSmatthias.ringwald         return 0;
286998906cdSmatthias.ringwald     }
287998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
288998906cdSmatthias.ringwald }
289998906cdSmatthias.ringwald 
290e79abdd6S[email protected] uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){
291ee303eddS[email protected] 
292ee303eddS[email protected]     int num_packets_sent_classic = 0;
293ee303eddS[email protected]     int num_packets_sent_le = 0;
294ee303eddS[email protected] 
295ee303eddS[email protected]     bd_addr_type_t address_type = BD_ADDR_TYPE_UNKNOWN;
296ee303eddS[email protected] 
297998906cdSmatthias.ringwald     linked_item_t *it;
2983a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
299998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
300ee303eddS[email protected]         if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
301ee303eddS[email protected]             num_packets_sent_classic += connection->num_acl_packets_sent;
302ee303eddS[email protected]         } else {
303ee303eddS[email protected]             num_packets_sent_le += connection->num_acl_packets_sent;
304ee303eddS[email protected]         }
305ccda6e14S[email protected]         // ignore connections that are not open, e.g., in state RECEIVED_DISCONNECTION_COMPLETE
306ccda6e14S[email protected]         if (connection->con_handle == con_handle && connection->state == OPEN){
307ee303eddS[email protected]             address_type = connection->address_type;
308ee303eddS[email protected]         }
309ee303eddS[email protected]     }
310ee303eddS[email protected] 
311ee303eddS[email protected]     int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic;
312ee303eddS[email protected]     int free_slots_le = 0;
313ee303eddS[email protected] 
314ee303eddS[email protected]     if (free_slots_classic < 0){
3159da54300S[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);
316998906cdSmatthias.ringwald         return 0;
317998906cdSmatthias.ringwald     }
318ee303eddS[email protected] 
319ee303eddS[email protected]     if (hci_stack->le_acl_packets_total_num){
320ee303eddS[email protected]         // if we have LE slots, they are used
321ee303eddS[email protected]         free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le;
322ee303eddS[email protected]         if (free_slots_le < 0){
3239da54300S[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);
324ee303eddS[email protected]             return 0;
325998906cdSmatthias.ringwald         }
326ee303eddS[email protected]     } else {
327ee303eddS[email protected]         // otherwise, classic slots are used for LE, too
328ee303eddS[email protected]         free_slots_classic -= num_packets_sent_le;
329ee303eddS[email protected]         if (free_slots_classic < 0){
3309da54300S[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);
331ee303eddS[email protected]             return 0;
332ee303eddS[email protected]         }
333ee303eddS[email protected]     }
334ee303eddS[email protected] 
335ee303eddS[email protected]     switch (address_type){
336ee303eddS[email protected]         case BD_ADDR_TYPE_UNKNOWN:
3379da54300S[email protected]             log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle);
338ee303eddS[email protected]             return 0;
339ee303eddS[email protected] 
340ee303eddS[email protected]         case BD_ADDR_TYPE_CLASSIC:
341ee303eddS[email protected]             return free_slots_classic;
342ee303eddS[email protected] 
343ee303eddS[email protected]         default:
344cb00d3aaS[email protected]            if (hci_stack->le_acl_packets_total_num){
345ee303eddS[email protected]                return free_slots_le;
346ee303eddS[email protected]            }
347cb00d3aaS[email protected]            return free_slots_classic;
348cb00d3aaS[email protected]     }
349998906cdSmatthias.ringwald }
350998906cdSmatthias.ringwald 
35144d0e3d5S[email protected] int hci_number_free_sco_slots_for_handle(hci_con_handle_t handle){
352e35edcc1S[email protected]     int num_sco_packets_sent = 0;
353e35edcc1S[email protected]     linked_item_t *it;
354e35edcc1S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
355e35edcc1S[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
356e35edcc1S[email protected]         num_sco_packets_sent += connection->num_sco_packets_sent;
357e35edcc1S[email protected]     }
358e35edcc1S[email protected]     if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
359e35edcc1S[email protected]         log_info("hci_number_free_sco_slots_for_handle: outgoing packets (%u) > total packets ()", num_sco_packets_sent, hci_stack->sco_packets_total_num);
36044d0e3d5S[email protected]         return 0;
36144d0e3d5S[email protected]     }
362e35edcc1S[email protected]     return hci_stack->sco_packets_total_num - num_sco_packets_sent;
363e35edcc1S[email protected] }
36444d0e3d5S[email protected] 
365ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
366ac928cc2S[email protected] int hci_can_send_command_packet_now(void){
367ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
368ac928cc2S[email protected] 
369ac928cc2S[email protected]     // check for async hci transport implementations
370ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
371ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
372ac928cc2S[email protected]             return 0;
373ac928cc2S[email protected]         }
374ac928cc2S[email protected]     }
375ac928cc2S[email protected] 
376ac928cc2S[email protected]     return hci_stack->num_cmd_packets > 0;
377ac928cc2S[email protected] }
378ac928cc2S[email protected] 
379ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
380ac928cc2S[email protected]     // check for async hci transport implementations
381ac928cc2S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
382ac928cc2S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){
383ac928cc2S[email protected]             return 0;
384ac928cc2S[email protected]         }
385ac928cc2S[email protected]     }
386e79abdd6S[email protected]     return hci_number_free_acl_slots_for_handle(con_handle) > 0;
387ac928cc2S[email protected] }
388ac928cc2S[email protected] 
389ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
390ac928cc2S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
391ac928cc2S[email protected]     return hci_can_send_prepared_acl_packet_now(con_handle);
3926b4af23dS[email protected] }
3936b4af23dS[email protected] 
39444d0e3d5S[email protected] int hci_can_send_prepared_sco_packet_now(hci_con_handle_t con_handle){
39544d0e3d5S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
39644d0e3d5S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(HCI_SCO_DATA_PACKET)){
39744d0e3d5S[email protected]             return 0;
39844d0e3d5S[email protected]         }
39944d0e3d5S[email protected]     }
40044d0e3d5S[email protected]     return hci_number_free_sco_slots_for_handle(con_handle) > 0;
40144d0e3d5S[email protected] }
40244d0e3d5S[email protected] 
40344d0e3d5S[email protected] int hci_can_send_sco_packet_now(hci_con_handle_t con_handle){
40444d0e3d5S[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
40544d0e3d5S[email protected]     return hci_can_send_prepared_sco_packet_now(con_handle);
40644d0e3d5S[email protected] }
40744d0e3d5S[email protected] 
408c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
409c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
410c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
411c8b9416aS[email protected] }
412c8b9416aS[email protected] 
4136b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
4146b4af23dS[email protected] int hci_reserve_packet_buffer(void){
4159d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
4169d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
4179d14b626S[email protected]         return 0;
4189d14b626S[email protected]     }
4196b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
4206b4af23dS[email protected]     return 1;
4216b4af23dS[email protected] }
4226b4af23dS[email protected] 
42368a0fcf7S[email protected] void hci_release_packet_buffer(void){
42468a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
42568a0fcf7S[email protected] }
42668a0fcf7S[email protected] 
4276b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
4286b4af23dS[email protected] int hci_transport_synchronous(void){
4296b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
4306b4af23dS[email protected] }
4316b4af23dS[email protected] 
4326c26b087S[email protected] uint16_t hci_max_acl_le_data_packet_length(void){
4336c26b087S[email protected]     return hci_stack->le_data_packets_length > 0 ? hci_stack->le_data_packets_length : hci_stack->acl_data_packet_length;
4346c26b087S[email protected] }
4356c26b087S[email protected] 
436452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){
437452cf3bbS[email protected] 
438452cf3bbS[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);
439452cf3bbS[email protected] 
440452cf3bbS[email protected]     // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers
441452cf3bbS[email protected]     uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length;
442452cf3bbS[email protected]     if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){
443452cf3bbS[email protected]         max_acl_data_packet_length = hci_stack->le_data_packets_length;
444452cf3bbS[email protected]     }
445452cf3bbS[email protected] 
446452cf3bbS[email protected]     // testing: reduce buffer to minimum
447452cf3bbS[email protected]     // max_acl_data_packet_length = 52;
448452cf3bbS[email protected] 
449452cf3bbS[email protected]     int err;
450452cf3bbS[email protected]     // multiple packets could be send on a synchronous HCI transport
451452cf3bbS[email protected]     while (1){
452452cf3bbS[email protected] 
453452cf3bbS[email protected]         // get current data
454452cf3bbS[email protected]         const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4;
455452cf3bbS[email protected]         int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
456452cf3bbS[email protected]         int more_fragments = 0;
457452cf3bbS[email protected] 
458452cf3bbS[email protected]         // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
459452cf3bbS[email protected]         if (current_acl_data_packet_length > max_acl_data_packet_length){
460452cf3bbS[email protected]             more_fragments = 1;
461452cf3bbS[email protected]             current_acl_data_packet_length = max_acl_data_packet_length;
462452cf3bbS[email protected]         }
463452cf3bbS[email protected] 
464452cf3bbS[email protected]         // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent)
465452cf3bbS[email protected]         if (acl_header_pos > 0){
466452cf3bbS[email protected]             uint16_t handle_and_flags = READ_BT_16(hci_stack->hci_packet_buffer, 0);
467452cf3bbS[email protected]             handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12);
468452cf3bbS[email protected]             bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
469452cf3bbS[email protected]         }
470452cf3bbS[email protected] 
471452cf3bbS[email protected]         // update header len
472452cf3bbS[email protected]         bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length);
473452cf3bbS[email protected] 
474452cf3bbS[email protected]         // count packet
475452cf3bbS[email protected]         connection->num_acl_packets_sent++;
476452cf3bbS[email protected] 
477452cf3bbS[email protected]         // send packet
478452cf3bbS[email protected]         uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
479452cf3bbS[email protected]         const int size = current_acl_data_packet_length + 4;
4805bb5bc3eS[email protected]         hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
481452cf3bbS[email protected]         err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
482452cf3bbS[email protected] 
483452cf3bbS[email protected]         // done yet?
484452cf3bbS[email protected]         if (!more_fragments) break;
485452cf3bbS[email protected] 
486452cf3bbS[email protected]         // update start of next fragment to send
487452cf3bbS[email protected]         hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
488452cf3bbS[email protected] 
489452cf3bbS[email protected]         // can send more?
490452cf3bbS[email protected]         if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err;
491452cf3bbS[email protected]     }
492452cf3bbS[email protected] 
493452cf3bbS[email protected]     // done
494452cf3bbS[email protected]     hci_stack->acl_fragmentation_pos = 0;
495452cf3bbS[email protected]     hci_stack->acl_fragmentation_total_size = 0;
496452cf3bbS[email protected] 
497d051460cS[email protected]     // release buffer now for synchronous transport
498203bace6S[email protected]     if (hci_transport_synchronous()){
499452cf3bbS[email protected]         hci_release_packet_buffer();
500*690bd0baS[email protected]         // notify upper stack that iit might be possible to send again
501*690bd0baS[email protected]         uint8_t event[] = { DAEMON_EVENT_HCI_PACKET_SENT, 0};
502*690bd0baS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
503452cf3bbS[email protected]     }
504452cf3bbS[email protected] 
505452cf3bbS[email protected]     return err;
506452cf3bbS[email protected] }
507452cf3bbS[email protected] 
508826f7347S[email protected] // pre: caller has reserved the packet buffer
509826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
5107856c818Smatthias.ringwald 
511452cf3bbS[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
512452cf3bbS[email protected] 
513826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
514826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
515826f7347S[email protected]         return 0;
516826f7347S[email protected]     }
517826f7347S[email protected] 
518d713a683S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
519d713a683S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
520d713a683S[email protected] 
521826f7347S[email protected]     // check for free places on Bluetooth module
522d713a683S[email protected]     if (!hci_can_send_prepared_acl_packet_now(con_handle)) {
523826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
52497b61c7bS[email protected]         hci_release_packet_buffer();
52597b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
52697b61c7bS[email protected]     }
5276218e6f1Smatthias.ringwald 
5285061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
52997b61c7bS[email protected]     if (!connection) {
5305fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
53197b61c7bS[email protected]         hci_release_packet_buffer();
53297b61c7bS[email protected]         return 0;
53397b61c7bS[email protected]     }
53456cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
53556cf178bSmatthias.ringwald 
536452cf3bbS[email protected]     // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size);
5377856c818Smatthias.ringwald 
538452cf3bbS[email protected]     // setup data
539452cf3bbS[email protected]     hci_stack->acl_fragmentation_total_size = size;
540452cf3bbS[email protected]     hci_stack->acl_fragmentation_pos = 4;   // start of L2CAP packet
5416218e6f1Smatthias.ringwald 
542452cf3bbS[email protected]     return hci_send_acl_packet_fragments(connection);
543ee091cf1Smatthias.ringwald }
544ee091cf1Smatthias.ringwald 
54544d0e3d5S[email protected] // pre: caller has reserved the packet buffer
54644d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){
54744d0e3d5S[email protected] 
54844d0e3d5S[email protected]     // log_info("hci_send_acl_packet_buffer size %u", size);
54944d0e3d5S[email protected] 
55044d0e3d5S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
55144d0e3d5S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
55244d0e3d5S[email protected]         return 0;
55344d0e3d5S[email protected]     }
55444d0e3d5S[email protected] 
55544d0e3d5S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
55644d0e3d5S[email protected]     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);   // same for ACL and SCO
55744d0e3d5S[email protected] 
55844d0e3d5S[email protected]     // check for free places on Bluetooth module
55944d0e3d5S[email protected]     if (!hci_can_send_prepared_sco_packet_now(con_handle)) {
56044d0e3d5S[email protected]         log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller");
56144d0e3d5S[email protected]         hci_release_packet_buffer();
56244d0e3d5S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
56344d0e3d5S[email protected]     }
56444d0e3d5S[email protected] 
565e35edcc1S[email protected]     // track send packet in connection struct
566e35edcc1S[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
567e35edcc1S[email protected]     if (!connection) {
568e35edcc1S[email protected]         log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle);
569e35edcc1S[email protected]         hci_release_packet_buffer();
570e35edcc1S[email protected]         return 0;
571e35edcc1S[email protected]     }
572e35edcc1S[email protected]     connection->num_sco_packets_sent++;
57344d0e3d5S[email protected] 
57444d0e3d5S[email protected]     hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size);
57544d0e3d5S[email protected]     return hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size);
57644d0e3d5S[email protected] }
57744d0e3d5S[email protected] 
57816833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
5797856c818Smatthias.ringwald 
580e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
581e76a89eeS[email protected] 
5827856c818Smatthias.ringwald     // get info
5837856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
5845061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
5857856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
5867856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
5877856c818Smatthias.ringwald 
5887856c818Smatthias.ringwald     // ignore non-registered handle
5897856c818Smatthias.ringwald     if (!conn){
5909da54300S[email protected]         log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle);
5917856c818Smatthias.ringwald         return;
5927856c818Smatthias.ringwald     }
5937856c818Smatthias.ringwald 
594e76a89eeS[email protected]     // assert packet is complete
5959ecc3e17S[email protected]     if (acl_length + 4 != size){
596e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
597e76a89eeS[email protected]         return;
598e76a89eeS[email protected]     }
599e76a89eeS[email protected] 
6007856c818Smatthias.ringwald     // update idle timestamp
6017856c818Smatthias.ringwald     hci_connection_timestamp(conn);
6027856c818Smatthias.ringwald 
6037856c818Smatthias.ringwald     // handle different packet types
6047856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
6057856c818Smatthias.ringwald 
6067856c818Smatthias.ringwald         case 0x01: // continuation fragment
6077856c818Smatthias.ringwald 
6080ca847afS[email protected]             // sanity checks
6097856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
6109da54300S[email protected]                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle);
6117856c818Smatthias.ringwald                 return;
6127856c818Smatthias.ringwald             }
6130ca847afS[email protected]             if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){
6140ca847afS[email protected]                 log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x",
6150ca847afS[email protected]                     conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
6160ca847afS[email protected]                 conn->acl_recombination_pos = 0;
6170ca847afS[email protected]                 return;
6180ca847afS[email protected]             }
6197856c818Smatthias.ringwald 
6207856c818Smatthias.ringwald             // append fragment payload (header already stored)
621ec6321eeS[email protected]             memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length );
6227856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
6237856c818Smatthias.ringwald 
6249da54300S[email protected]             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length,
625decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
6267856c818Smatthias.ringwald 
6277856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
6287856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
6297856c818Smatthias.ringwald 
630ec6321eeS[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, &conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos);
6317856c818Smatthias.ringwald                 // reset recombination buffer
6327856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
6337856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
6347856c818Smatthias.ringwald             }
6357856c818Smatthias.ringwald             break;
6367856c818Smatthias.ringwald 
6377856c818Smatthias.ringwald         case 0x02: { // first fragment
6387856c818Smatthias.ringwald 
63923a77e1aS[email protected]             // sanity check
64023a77e1aS[email protected]             if (conn->acl_recombination_pos) {
64123a77e1aS[email protected]                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle);
64223a77e1aS[email protected]                 conn->acl_recombination_pos = 0;
64323a77e1aS[email protected]             }
64423a77e1aS[email protected] 
6457856c818Smatthias.ringwald             // peek into L2CAP packet!
6467856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
6477856c818Smatthias.ringwald 
6489da54300S[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length);
649decc01a8Smatthias.ringwald 
6507856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
6517856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
6527856c818Smatthias.ringwald 
6537856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
6543a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
6557856c818Smatthias.ringwald 
6567856c818Smatthias.ringwald             } else {
6570ca847afS[email protected] 
6580ca847afS[email protected]                 if (acl_length > HCI_ACL_BUFFER_SIZE){
6590ca847afS[email protected]                     log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x",
6600ca847afS[email protected]                         4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle);
6610ca847afS[email protected]                     return;
6620ca847afS[email protected]                 }
6630ca847afS[email protected] 
6647856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
665ec6321eeS[email protected]                 memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4);
6667856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
6677856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
668ec6321eeS[email protected]                 bt_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4);
6697856c818Smatthias.ringwald             }
6707856c818Smatthias.ringwald             break;
6717856c818Smatthias.ringwald 
6727856c818Smatthias.ringwald         }
6737856c818Smatthias.ringwald         default:
6749da54300S[email protected]             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03);
6757856c818Smatthias.ringwald             return;
6767856c818Smatthias.ringwald     }
67794ab26f8Smatthias.ringwald 
67894ab26f8Smatthias.ringwald     // execute main loop
67994ab26f8Smatthias.ringwald     hci_run();
68016833f0aSmatthias.ringwald }
68122909952Smatthias.ringwald 
68267a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
6839da54300S[email protected]     log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address));
6843c4d4b90Smatthias.ringwald 
685c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
686c785ef68Smatthias.ringwald 
6873a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
688a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
6893c4d4b90Smatthias.ringwald 
6903c4d4b90Smatthias.ringwald     // now it's gone
691c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
692c7e0c5f6Smatthias.ringwald }
693c7e0c5f6Smatthias.ringwald 
6940c042179S[email protected] static const uint16_t packet_type_sizes[] = {
6958f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
6968f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
6978f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
6988f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
6998f8108aaSmatthias.ringwald };
70065389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
70165389bfcS[email protected]      0, // 3 slot packets
70265389bfcS[email protected]      1, // 5 slot packets
70365389bfcS[email protected]     25, // EDR 2 mpbs
70465389bfcS[email protected]     26, // EDR 3 mbps
70565389bfcS[email protected]     39, // 3 slot EDR packts
70665389bfcS[email protected]     40, // 5 slot EDR packet
70765389bfcS[email protected] };
70865389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
70965389bfcS[email protected]     0x0f00, // 3 slot packets
71065389bfcS[email protected]     0xf000, // 5 slot packets
71165389bfcS[email protected]     0x1102, // EDR 2 mpbs
71265389bfcS[email protected]     0x2204, // EDR 3 mbps
71365389bfcS[email protected]     0x0300, // 3 slot EDR packts
71465389bfcS[email protected]     0x3000, // 5 slot EDR packet
71565389bfcS[email protected] };
7168f8108aaSmatthias.ringwald 
71765389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
71865389bfcS[email protected]     // enable packet types based on size
7198f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
720f16a69bbS[email protected]     unsigned int i;
7218f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
7228f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
7238f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
7248f8108aaSmatthias.ringwald             packet_types |= 1 << i;
7258f8108aaSmatthias.ringwald         }
7268f8108aaSmatthias.ringwald     }
72765389bfcS[email protected]     // disable packet types due to missing local supported features
72865389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
72965389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
73065389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
73165389bfcS[email protected]         if (feature_set) continue;
73265389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
73365389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
73465389bfcS[email protected]     }
7358f8108aaSmatthias.ringwald     // flip bits for "may not be used"
7368f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
7378f8108aaSmatthias.ringwald     return packet_types;
7388f8108aaSmatthias.ringwald }
7398f8108aaSmatthias.ringwald 
7408f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
7413a9fb326S[email protected]     return hci_stack->packet_types;
7428f8108aaSmatthias.ringwald }
7438f8108aaSmatthias.ringwald 
744facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
7457dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
7463a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
7477dc17943Smatthias.ringwald }
7487dc17943Smatthias.ringwald 
749f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
7503a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
7517dc17943Smatthias.ringwald }
7527dc17943Smatthias.ringwald 
7536ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
7546ac9a97eS[email protected]     // No. 54, byte 6, bit 6
7556ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
7566ac9a97eS[email protected] }
7576ac9a97eS[email protected] 
758f5d8d141S[email protected] int hci_ssp_supported(void){
7596ac9a97eS[email protected]     // No. 51, byte 6, bit 3
7603a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
761f5d8d141S[email protected] }
762f5d8d141S[email protected] 
763f5d8d141S[email protected] int hci_classic_supported(void){
7646ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
7653a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
766f5d8d141S[email protected] }
767f5d8d141S[email protected] 
768f5d8d141S[email protected] int hci_le_supported(void){
769f5d8d141S[email protected] #ifdef HAVE_BLE
7706ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
7713a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
772f5d8d141S[email protected] #else
773f5d8d141S[email protected]     return 0;
774f5d8d141S[email protected] #endif
775f5d8d141S[email protected] }
776f5d8d141S[email protected] 
77769a97523S[email protected] // get addr type and address used in advertisement packets
7782e77e513S[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t  addr){
7793a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
7803a9fb326S[email protected]     if (hci_stack->adv_addr_type){
7813a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
78269a97523S[email protected]     } else {
7833a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
78469a97523S[email protected]     }
78569a97523S[email protected] }
78669a97523S[email protected] 
787b2f949feS[email protected] #ifdef HAVE_BLE
788e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){
789d1dc057bS[email protected]     int offset = 3;
790d1dc057bS[email protected]     int num_reports = packet[offset];
791d1dc057bS[email protected]     offset += 1;
792d1dc057bS[email protected] 
79357c9da5bS[email protected]     int i;
7944f4b43f3S[email protected]     log_info("HCI: handle adv report with num reports: %d", num_reports);
79503fbe9c6S[email protected]     uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
79657c9da5bS[email protected]     for (i=0; i<num_reports;i++){
797210c6774S[email protected]         uint8_t data_length = packet[offset + 8];
798a0aac9c4S[email protected]         uint8_t event_size = 10 + data_length;
799d1dc057bS[email protected]         int pos = 0;
8002b552b23S[email protected]         event[pos++] = GAP_LE_ADVERTISING_REPORT;
80157c9da5bS[email protected]         event[pos++] = event_size;
802210c6774S[email protected]         memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address
803d1dc057bS[email protected]         offset += 8;
804d1dc057bS[email protected]         pos += 8;
805d1dc057bS[email protected]         event[pos++] = packet[offset + 1 + data_length]; // rssi
806d1dc057bS[email protected]         event[pos++] = packet[offset++]; //data_length;
807d1dc057bS[email protected]         memcpy(&event[pos], &packet[offset], data_length);
80857c9da5bS[email protected]         pos += data_length;
809d1dc057bS[email protected]         offset += data_length + 1; // rssi
81003fbe9c6S[email protected]         hci_dump_packet( HCI_EVENT_PACKET, 0, event, pos);
81103fbe9c6S[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, pos);
81257c9da5bS[email protected]     }
81357c9da5bS[email protected] }
814b2f949feS[email protected] #endif
81557c9da5bS[email protected] 
8166155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
817a2481739S[email protected]     uint8_t command_completed = 0;
818a2481739S[email protected]     if ((hci_stack->substate % 2) == 0) return;
8196155b3d3S[email protected]     // odd: waiting for event
8206155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
8216155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,3);
8226155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
823a2481739S[email protected]             command_completed = 1;
824f456b2d0S[email protected]             log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate >> 1);
8256155b3d3S[email protected]         } else {
8266155b3d3S[email protected]             log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
8276155b3d3S[email protected]         }
8286155b3d3S[email protected]     }
8296155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_STATUS){
8306155b3d3S[email protected]         uint8_t  status = packet[2];
8316155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,4);
8326155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
8336155b3d3S[email protected]             if (status){
834a2481739S[email protected]                 command_completed = 1;
835f456b2d0S[email protected]                 log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate >> 1);
8366155b3d3S[email protected]             } else {
8376155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
8386155b3d3S[email protected]             }
8396155b3d3S[email protected]         } else {
8406155b3d3S[email protected]             log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
8416155b3d3S[email protected]         }
8426155b3d3S[email protected]     }
843a2481739S[email protected] 
844a2481739S[email protected]     if (!command_completed) return;
845a2481739S[email protected] 
846a2481739S[email protected]     switch(hci_stack->substate >> 1){
847a2481739S[email protected]         default:
848a2481739S[email protected]             hci_stack->substate++;
849a2481739S[email protected]             break;
8506155b3d3S[email protected]     }
8516155b3d3S[email protected] }
8526155b3d3S[email protected] 
8536155b3d3S[email protected] static void hci_initializing_state_machine(){
8546155b3d3S[email protected]     if (hci_stack->substate % 2) {
8556155b3d3S[email protected]         // odd: waiting for command completion
8566155b3d3S[email protected]         return;
8576155b3d3S[email protected]     }
858f456b2d0S[email protected]     // log_info("hci_init: substate %u", hci_stack->substate >> 1);
8596155b3d3S[email protected]     switch (hci_stack->substate >> 1){
8606155b3d3S[email protected]         case 0: // RESET
8616155b3d3S[email protected]             hci_state_reset();
8626155b3d3S[email protected] 
8636155b3d3S[email protected]             hci_send_cmd(&hci_reset);
8646155b3d3S[email protected]             if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
8656155b3d3S[email protected]                 // skip baud change
866f456b2d0S[email protected]                 hci_stack->substate = 2 << 1;
8676155b3d3S[email protected]             }
8686155b3d3S[email protected]             break;
8696155b3d3S[email protected]         case 1: // SEND BAUD CHANGE
8706155b3d3S[email protected]             hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
87188789c61Smatthias.ringwald             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
8726155b3d3S[email protected]             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
8736155b3d3S[email protected]             break;
8746155b3d3S[email protected]         case 2: // LOCAL BAUD CHANGE
8756155b3d3S[email protected]             log_info("Local baud rate change");
8766155b3d3S[email protected]             hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
8776155b3d3S[email protected]             hci_stack->substate += 2;
8786155b3d3S[email protected]             // break missing here for fall through
8796155b3d3S[email protected] 
880f456b2d0S[email protected]         case 3: // SET BD ADDR
881f456b2d0S[email protected]             if ( hci_stack->custom_bd_addr_set && hci_stack->control && hci_stack->control->set_bd_addr_cmd){
882f456b2d0S[email protected]                 log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr));
883f456b2d0S[email protected]                 hci_stack->control->set_bd_addr_cmd(hci_stack->config, hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer);
884bc01039dS[email protected]                 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
885f456b2d0S[email protected]                 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
886f456b2d0S[email protected]                 break;
887f456b2d0S[email protected]             }
888f456b2d0S[email protected]             hci_stack->substate += 2;
889f456b2d0S[email protected]             // break missing here for fall through
890f456b2d0S[email protected] 
891f456b2d0S[email protected]         case 4:
8926155b3d3S[email protected]             log_info("Custom init");
8936155b3d3S[email protected]             // Custom initialization
8946155b3d3S[email protected]             if (hci_stack->control && hci_stack->control->next_cmd){
8956155b3d3S[email protected]                 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
8966155b3d3S[email protected]                 if (valid_cmd){
8976155b3d3S[email protected]                     int size = 3 + hci_stack->hci_packet_buffer[2];
8986155b3d3S[email protected]                     hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
8995bb5bc3eS[email protected]                     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size);
9006155b3d3S[email protected]                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
901f456b2d0S[email protected]                     hci_stack->substate = 3 << 1; // more init commands
9026155b3d3S[email protected]                     break;
9036155b3d3S[email protected]                 }
9049da54300S[email protected]                 log_info("hci_run: init script done");
9056155b3d3S[email protected]             }
9066155b3d3S[email protected]             // otherwise continue
9076155b3d3S[email protected]             hci_send_cmd(&hci_read_bd_addr);
9086155b3d3S[email protected]             break;
909f456b2d0S[email protected]         case 5:
9106155b3d3S[email protected]             hci_send_cmd(&hci_read_buffer_size);
9116155b3d3S[email protected]             break;
912f456b2d0S[email protected]         case 6:
9136155b3d3S[email protected]             hci_send_cmd(&hci_read_local_supported_features);
9146155b3d3S[email protected]             break;
915f456b2d0S[email protected]         case 7:
9166155b3d3S[email protected]             if (hci_le_supported()){
9176155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
9186155b3d3S[email protected]             } else {
919f456b2d0S[email protected]                 // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
9206155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
9216155b3d3S[email protected]             }
9226155b3d3S[email protected] 
9236155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
9246155b3d3S[email protected]             if (!hci_classic_supported()){
9256155b3d3S[email protected]                 if (hci_le_supported()){
926f456b2d0S[email protected]                     hci_stack->substate = 12 << 1;    // skip all classic command
9276155b3d3S[email protected]                 } else {
9286155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
929f456b2d0S[email protected]                     hci_stack->substate = 15 << 1;    // skip all
9306155b3d3S[email protected]                 }
9316155b3d3S[email protected]             }
9326155b3d3S[email protected]             break;
933f456b2d0S[email protected]         case 8:
9346155b3d3S[email protected]             if (hci_ssp_supported()){
9356155b3d3S[email protected]                 hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
9366155b3d3S[email protected]                 break;
9376155b3d3S[email protected]             }
9386155b3d3S[email protected]             hci_stack->substate += 2;
9396155b3d3S[email protected]             // break missing here for fall through
9406155b3d3S[email protected] 
941f456b2d0S[email protected]         case 9:
9426155b3d3S[email protected]             // ca. 15 sec
9436155b3d3S[email protected]             hci_send_cmd(&hci_write_page_timeout, 0x6000);
9446155b3d3S[email protected]             break;
945f456b2d0S[email protected]         case 10:
9466155b3d3S[email protected]             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
9476155b3d3S[email protected]             break;
948f456b2d0S[email protected]         case 11:
9496155b3d3S[email protected]             if (hci_stack->local_name){
9506155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
9516155b3d3S[email protected]             } else {
9526155b3d3S[email protected]                 char hostname[30];
9536155b3d3S[email protected] #ifdef EMBEDDED
9546155b3d3S[email protected]                 // BTstack-11:22:33:44:55:66
9556155b3d3S[email protected]                 strcpy(hostname, "BTstack ");
9566155b3d3S[email protected]                 strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
9576155b3d3S[email protected]                 log_info("---> Name %s", hostname);
9586155b3d3S[email protected] #else
9596155b3d3S[email protected]                 // hostname for POSIX systems
9606155b3d3S[email protected]                 gethostname(hostname, 30);
9616155b3d3S[email protected]                 hostname[29] = '\0';
9626155b3d3S[email protected] #endif
9636155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hostname);
9646155b3d3S[email protected]             }
9656155b3d3S[email protected]             break;
966f456b2d0S[email protected]         case 12:
9676155b3d3S[email protected]             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
9686155b3d3S[email protected]             if (!hci_le_supported()){
9696155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
970f456b2d0S[email protected]                 hci_stack->substate = 15 << 1;
9716155b3d3S[email protected]             }
9726155b3d3S[email protected]             break;
9736155b3d3S[email protected] 
9746155b3d3S[email protected] #ifdef HAVE_BLE
9756155b3d3S[email protected]         // LE INIT
976f456b2d0S[email protected]         case 13:
9776155b3d3S[email protected]             hci_send_cmd(&hci_le_read_buffer_size);
9786155b3d3S[email protected]             break;
979f456b2d0S[email protected]         case 14:
9806155b3d3S[email protected]             // LE Supported Host = 1, Simultaneous Host = 0
9816155b3d3S[email protected]             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
9826155b3d3S[email protected]             break;
983f456b2d0S[email protected]         case 15:
9846155b3d3S[email protected]             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
9856155b3d3S[email protected]             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
9866155b3d3S[email protected]             break;
9876155b3d3S[email protected] #endif
9886155b3d3S[email protected] 
9896155b3d3S[email protected]         // DONE
990f456b2d0S[email protected]         case 16:
9916155b3d3S[email protected]             // done.
9926155b3d3S[email protected]             hci_stack->state = HCI_STATE_WORKING;
9936155b3d3S[email protected]             hci_emit_state();
9946155b3d3S[email protected]             break;
9956155b3d3S[email protected]         default:
9966155b3d3S[email protected]             break;
9976155b3d3S[email protected]     }
9986155b3d3S[email protected]     hci_stack->substate++;
9996155b3d3S[email protected] }
10006155b3d3S[email protected] 
100174ec757aSmatthias.ringwald // avoid huge local variables
1002223aafc1Smatthias.ringwald #ifndef EMBEDDED
100374ec757aSmatthias.ringwald static device_name_t device_name;
1004223aafc1Smatthias.ringwald #endif
100516833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1006e76a89eeS[email protected] 
1007e76a89eeS[email protected]     uint16_t event_length = packet[1];
1008e76a89eeS[email protected] 
1009e76a89eeS[email protected]     // assert packet is complete
1010e76a89eeS[email protected]     if (size != event_length + 2){
1011e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
1012e76a89eeS[email protected]         return;
1013e76a89eeS[email protected]     }
1014e76a89eeS[email protected] 
10151281a47eSmatthias.ringwald     bd_addr_t addr;
101696a45072S[email protected]     bd_addr_type_t addr_type;
10172d00edd4Smatthias.ringwald     uint8_t link_type;
1018fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
10191f7b95a1Smatthias.ringwald     hci_connection_t * conn;
102056cf178bSmatthias.ringwald     int i;
102122909952Smatthias.ringwald 
10229da54300S[email protected]     // log_info("HCI:EVENT:%02x", packet[0]);
10235909f7f2Smatthias.ringwald 
10246772a24cSmatthias.ringwald     switch (packet[0]) {
102522909952Smatthias.ringwald 
10266772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
10277ec5eeaaSmatthias.ringwald             // get num cmd packets
10289da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u", hci_stack->num_cmd_packets, packet[2]);
10293a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
10307ec5eeaaSmatthias.ringwald 
1031e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
1032e2edc0c3Smatthias.ringwald                 // from offset 5
1033e2edc0c3Smatthias.ringwald                 // status
10341d279b20Smatthias.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"
10353a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
1036a8b12447S[email protected]                 hci_stack->sco_data_packet_length = packet[8];
10371a06f663S[email protected]                 hci_stack->acl_packets_total_num  = READ_BT_16(packet, 9);
10381a06f663S[email protected]                 hci_stack->sco_packets_total_num  = READ_BT_16(packet, 11);
1039a8b12447S[email protected] 
10403a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
1041a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
10423a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
10433a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
10448f8108aaSmatthias.ringwald                     }
10451a06f663S[email protected]                     log_info("hci_read_buffer_size: acl used size %u, count %u / sco size %u, count %u",
10461a06f663S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num,
10471a06f663S[email protected]                              hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num);
1048e2edc0c3Smatthias.ringwald                 }
104956cf178bSmatthias.ringwald             }
105065a46ef3S[email protected] #ifdef HAVE_BLE
105165a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
1052219eea5fS[email protected]                 hci_stack->le_data_packets_length = READ_BT_16(packet, 6);
1053ee303eddS[email protected]                 hci_stack->le_acl_packets_total_num  = packet[8];
10546c26b087S[email protected]                     // determine usable ACL payload size
10556c26b087S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){
10566c26b087S[email protected]                         hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE;
10576c26b087S[email protected]                     }
10589da54300S[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);
105965a46ef3S[email protected]             }
106065a46ef3S[email protected] #endif
1061188981d2Smatthias.ringwald             // Dump local address
1062188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
10633a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
10649da54300S[email protected]                 log_info("Local Address, Status: 0x%02x: Addr: %s",
10653a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
1066188981d2Smatthias.ringwald             }
1067381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
10683a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
1069381fbed8Smatthias.ringwald             }
107065389bfcS[email protected]             // Note: HCI init checks
1071559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
10723a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
1073559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
10743a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
10753a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
10763a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
10773a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
107865389bfcS[email protected] 
1079a5a23fc2S[email protected]                 // determine usable ACL packet types based on host buffer size and supported features
1080a5a23fc2S[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]);
10813a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
1082f5d8d141S[email protected] 
1083f5d8d141S[email protected]                 // Classic/LE
1084f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
1085559e517eS[email protected]             }
108656cf178bSmatthias.ringwald             break;
108756cf178bSmatthias.ringwald 
10887ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
10897ec5eeaaSmatthias.ringwald             // get num cmd packets
10909da54300S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]);
10913a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
10927ec5eeaaSmatthias.ringwald             break;
10937ec5eeaaSmatthias.ringwald 
10942e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
10952e440c8aS[email protected]             int offset = 3;
109656cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
10972e440c8aS[email protected]                 handle = READ_BT_16(packet, offset);
10982e440c8aS[email protected]                 offset += 2;
10992e440c8aS[email protected]                 uint16_t num_packets = READ_BT_16(packet, offset);
11002e440c8aS[email protected]                 offset += 2;
11012e440c8aS[email protected] 
11025061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
110356cf178bSmatthias.ringwald                 if (!conn){
11049da54300S[email protected]                     log_error("hci_number_completed_packet lists unused con handle %u", handle);
110556cf178bSmatthias.ringwald                     continue;
110656cf178bSmatthias.ringwald                 }
110723bed257S[email protected] 
1108e35edcc1S[email protected]                 if (conn->address_type == BD_ADDR_TYPE_SCO){
1109e35edcc1S[email protected]                     if (conn->num_sco_packets_sent >= num_packets){
1110e35edcc1S[email protected]                         conn->num_sco_packets_sent -= num_packets;
1111e35edcc1S[email protected]                     } else {
1112e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more sco slots freed then sent.");
1113e35edcc1S[email protected]                         conn->num_sco_packets_sent = 0;
1114e35edcc1S[email protected]                     }
1115e35edcc1S[email protected] 
1116e35edcc1S[email protected]                 } else {
111723bed257S[email protected]                     if (conn->num_acl_packets_sent >= num_packets){
111856cf178bSmatthias.ringwald                         conn->num_acl_packets_sent -= num_packets;
111923bed257S[email protected]                     } else {
1120e35edcc1S[email protected]                         log_error("hci_number_completed_packets, more acl slots freed then sent.");
112123bed257S[email protected]                         conn->num_acl_packets_sent = 0;
112223bed257S[email protected]                     }
1123e35edcc1S[email protected]                 }
11249da54300S[email protected]                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent);
112556cf178bSmatthias.ringwald             }
11266772a24cSmatthias.ringwald             break;
11272e440c8aS[email protected]         }
11281f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
112937eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
113037eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
11312d00edd4Smatthias.ringwald             link_type = packet[11];
11329da54300S[email protected]             log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type);
1133e35edcc1S[email protected]             addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO;
11342e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
11351f7b95a1Smatthias.ringwald             if (!conn) {
11365293c072S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, addr_type);
11371f7b95a1Smatthias.ringwald             }
1138ce4c8fabSmatthias.ringwald             if (!conn) {
1139ce4c8fabSmatthias.ringwald                 // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
11403a9fb326S[email protected]                 hci_stack->decline_reason = 0x0d;
11413a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
1142ce4c8fabSmatthias.ringwald                 break;
1143ce4c8fabSmatthias.ringwald             }
114432ab9390Smatthias.ringwald             conn->state = RECEIVED_CONNECTION_REQUEST;
114532ab9390Smatthias.ringwald             hci_run();
11461f7b95a1Smatthias.ringwald             break;
11471f7b95a1Smatthias.ringwald 
11486772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1149fe1ed1b8Smatthias.ringwald             // Connection management
1150fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
11519da54300S[email protected]             log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
115296a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
11532e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
1154fe1ed1b8Smatthias.ringwald             if (conn) {
1155b448a0e7Smatthias.ringwald                 if (!packet[2]){
1156c8e4258aSmatthias.ringwald                     conn->state = OPEN;
1157fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
1158ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
1159ee091cf1Smatthias.ringwald 
1160c785ef68Smatthias.ringwald                     // restart timer
1161c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
1162ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
1163c785ef68Smatthias.ringwald 
11649da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
116543bfb1bdSmatthias.ringwald 
116643bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
1167b448a0e7Smatthias.ringwald                 } else {
11681bd5283dS[email protected]                     int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED;
11691bd5283dS[email protected]                     uint8_t status = packet[2];
11701bd5283dS[email protected]                     bd_addr_t bd_address;
11711bd5283dS[email protected]                     memcpy(&bd_address, conn->address, 6);
1172ad83dc6aS[email protected] 
1173b448a0e7Smatthias.ringwald                     // connection failed, remove entry
11743a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
1175a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
1176c12e46e7Smatthias.ringwald 
11771bd5283dS[email protected]                     // notify client if dedicated bonding
11781bd5283dS[email protected]                     if (notify_dedicated_bonding_failed){
11791bd5283dS[email protected]                         log_info("hci notify_dedicated_bonding_failed");
11801bd5283dS[email protected]                         hci_emit_dedicated_bonding_result(bd_address, status);
11811bd5283dS[email protected]                     }
11821bd5283dS[email protected] 
1183c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
1184c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
11852e77e513S[email protected]                         hci_drop_link_key_for_bd_addr(addr);
1186c12e46e7Smatthias.ringwald                     }
1187fe1ed1b8Smatthias.ringwald                 }
1188fe1ed1b8Smatthias.ringwald             }
11896772a24cSmatthias.ringwald             break;
1190fe1ed1b8Smatthias.ringwald 
119144d0e3d5S[email protected]         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:
119244d0e3d5S[email protected]             bt_flip_addr(addr, &packet[5]);
119344d0e3d5S[email protected]             log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr));
11941a06f663S[email protected]             if (packet[2]){
119544d0e3d5S[email protected]                 // connection failed
119644d0e3d5S[email protected]                 break;
119744d0e3d5S[email protected]             }
11982e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1199e35edcc1S[email protected]             if (!conn) {
1200e35edcc1S[email protected]                 conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO);
1201e35edcc1S[email protected]             }
1202e35edcc1S[email protected]             if (!conn) {
1203e35edcc1S[email protected]                 break;
1204e35edcc1S[email protected]             }
12051a06f663S[email protected]             conn->state = OPEN;
12061a06f663S[email protected]             conn->con_handle = READ_BT_16(packet, 3);
120744d0e3d5S[email protected]             break;
120844d0e3d5S[email protected] 
1209afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1210afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
1211afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
1212afd4e962S[email protected]             if (!conn) break;
1213afd4e962S[email protected]             if (!packet[2]){
1214afd4e962S[email protected]                 uint8_t * features = &packet[5];
1215afd4e962S[email protected]                 if (features[6] & (1 << 3)){
1216afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
1217afd4e962S[email protected]                 }
1218afd4e962S[email protected]             }
1219afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
1220ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
1221ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1222ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1223ad83dc6aS[email protected]             }
1224afd4e962S[email protected]             break;
1225afd4e962S[email protected] 
12267fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
12279da54300S[email protected]             log_info("HCI_EVENT_LINK_KEY_REQUEST");
12287fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
122974d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
12303a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
123132ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
123264472d52Smatthias.ringwald             hci_run();
1233d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
123429d53098Smatthias.ringwald             return;
12357fde4af9Smatthias.ringwald 
12369ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
123729d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
12382e77e513S[email protected]             conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
12399ab95c90S[email protected]             if (!conn) break;
12409ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
12417bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
12429ab95c90S[email protected]             // Change Connection Encryption keeps link key type
12439ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
12449ab95c90S[email protected]                 conn->link_key_type = link_key_type;
12459ab95c90S[email protected]             }
12463a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
12472e77e513S[email protected]             hci_stack->remote_device_db->put_link_key(addr, &packet[8], conn->link_key_type);
124829d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
12497fde4af9Smatthias.ringwald             break;
12509ab95c90S[email protected]         }
12517fde4af9Smatthias.ringwald 
12527fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
12536724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
12544c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
12553a9fb326S[email protected]             if (!hci_stack->bondable){
1256899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
1257f8fb5f6eS[email protected]                 hci_run();
1258f8fb5f6eS[email protected]                 return;
12594c57c146S[email protected]             }
1260d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
12613a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
1262d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
12632e77e513S[email protected]             hci_stack->remote_device_db->delete_link_key(addr);
12647fde4af9Smatthias.ringwald             break;
12657fde4af9Smatthias.ringwald 
12661d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
12671d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
1268dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
1269dbe1a790S[email protected]             break;
1270dbe1a790S[email protected] 
1271dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
12726724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
12733a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1274dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
1275dbe1a790S[email protected]             break;
1276dbe1a790S[email protected] 
1277dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
12786724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
12793a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1280dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
12811d6b20aeS[email protected]             break;
12821d6b20aeS[email protected] 
1283f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1284f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
1285f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1286f0944df2S[email protected]             if (!conn) break;
1287ad83dc6aS[email protected]             if (packet[2] == 0) {
1288f0944df2S[email protected]                 if (packet[5]){
1289f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1290f0944df2S[email protected]                 } else {
1291536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1292f0944df2S[email protected]                 }
1293ad83dc6aS[email protected]             }
1294a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
1295f0944df2S[email protected]             break;
1296f0944df2S[email protected] 
12971eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
12981eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
12991eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
13001eb2563eS[email protected]             if (!conn) break;
1301ad83dc6aS[email protected] 
1302ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1303ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1304ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1305ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
13061bd5283dS[email protected]                 conn->bonding_status = packet[2];
1307ad83dc6aS[email protected]                 break;
1308ad83dc6aS[email protected]             }
1309ad83dc6aS[email protected] 
1310b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
13111eb2563eS[email protected]                 // link key sufficient for requested security
13121eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1313ad83dc6aS[email protected]                 break;
1314ad83dc6aS[email protected]             }
13151eb2563eS[email protected]             // not enough
13161eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
13171eb2563eS[email protected]             break;
131834d2123cS[email protected] 
1319223aafc1Smatthias.ringwald #ifndef EMBEDDED
132074ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
13213a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
132274ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
132374ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
13245a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
13255a06394aSmatthias.ringwald             for (i=0; i<248;i++){
13265a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
13275a06394aSmatthias.ringwald                     packet[9+i] = 0;
13285a06394aSmatthias.ringwald                     break;
1329cdc9101dSmatthias.ringwald                 }
13305a06394aSmatthias.ringwald             }
1331d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
133274ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
13332e77e513S[email protected]             hci_stack->remote_device_db->put_name(addr, &device_name);
133474ec757aSmatthias.ringwald             break;
133574ec757aSmatthias.ringwald 
133674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
1337206a9031S[email protected]         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
13383a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
133974ec757aSmatthias.ringwald             // first send inq result packet
13403a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
134174ec757aSmatthias.ringwald             // then send cached remote names
1342206a9031S[email protected]             int offset = 3;
134374ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
1344206a9031S[email protected]                 bt_flip_addr(addr, &packet[offset]);
1345206a9031S[email protected]                 offset += 14; // 6 + 1 + 1 + 1 + 3 + 2;
13462e77e513S[email protected]                 if (hci_stack->remote_device_db->get_name(addr, &device_name)){
13472e77e513S[email protected]                     hci_emit_remote_name_cached(addr, &device_name);
134874ec757aSmatthias.ringwald                 }
134974ec757aSmatthias.ringwald             }
135074ec757aSmatthias.ringwald             return;
1351206a9031S[email protected]         }
1352223aafc1Smatthias.ringwald #endif
135374ec757aSmatthias.ringwald 
135486805605S[email protected]         // HCI_EVENT_DISCONNECTION_COMPLETE
1355ccda6e14S[email protected]         // has been split, to first notify stack before shutting connection down
1356ccda6e14S[email protected]         // see end of function, too.
1357a4f30ec0S[email protected]         case HCI_EVENT_DISCONNECTION_COMPLETE:
1358a4f30ec0S[email protected]             if (packet[2]) break;   // status != 0
1359ccda6e14S[email protected]             handle = READ_BT_16(packet, 3);
1360ccda6e14S[email protected]             hci_connection_t * conn = hci_connection_for_handle(handle);
1361a4f30ec0S[email protected]             if (!conn) break;       // no conn struct anymore
1362ccda6e14S[email protected]             conn->state = RECEIVED_DISCONNECTION_COMPLETE;
1363ccda6e14S[email protected]             break;
13646772a24cSmatthias.ringwald 
1365c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
13663a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
13673a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
13687586ee35S[email protected]             } else {
13697586ee35S[email protected]                 // if no special requests, just reboot stack
13707586ee35S[email protected]                 hci_power_control_off();
13717586ee35S[email protected]                 hci_power_control_on();
1372c68bdf90Smatthias.ringwald             }
1373c68bdf90Smatthias.ringwald             break;
1374c68bdf90Smatthias.ringwald 
13756b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
1376d051460cS[email protected]             // release packet buffer only for asynchronous transport and if there are not further fragements
13774fa24b5fS[email protected]             if (hci_transport_synchronous()) {
13784fa24b5fS[email protected]                 log_error("Synchronous HCI Transport shouldn't send DAEMON_EVENT_HCI_PACKET_SENT");
13794fa24b5fS[email protected]                 return; // instead of break: to avoid re-entering hci_run()
13804fa24b5fS[email protected]             }
1381d051460cS[email protected]             if (hci_stack->acl_fragmentation_total_size) break;
1382d051460cS[email protected]             hci_release_packet_buffer();
13836b4af23dS[email protected]             break;
13846b4af23dS[email protected] 
13855909f7f2Smatthias.ringwald #ifdef HAVE_BLE
13865909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
13875909f7f2Smatthias.ringwald             switch (packet[2]){
138857c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
13899da54300S[email protected]                     log_info("advertising report received");
13907bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
139157c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
13927bdc6798S[email protected]                     break;
13935909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
13945909f7f2Smatthias.ringwald                     // Connection management
13955909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
13967bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
13979da54300S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr));
13985909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
13992e77e513S[email protected]                     conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
14005909f7f2Smatthias.ringwald                     if (packet[3]){
14015909f7f2Smatthias.ringwald                         if (conn){
14025909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
14033a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
14045909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
14055909f7f2Smatthias.ringwald                         }
14065909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
14075909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
14082e77e513S[email protected]                             hci_drop_link_key_for_bd_addr(addr);
14095909f7f2Smatthias.ringwald                         }
14105909f7f2Smatthias.ringwald                         break;
14115909f7f2Smatthias.ringwald                     }
14125909f7f2Smatthias.ringwald                     if (!conn){
141396a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
14145909f7f2Smatthias.ringwald                     }
14155909f7f2Smatthias.ringwald                     if (!conn){
14165909f7f2Smatthias.ringwald                         // no memory
14175909f7f2Smatthias.ringwald                         break;
14185909f7f2Smatthias.ringwald                     }
14195909f7f2Smatthias.ringwald 
14205909f7f2Smatthias.ringwald                     conn->state = OPEN;
14215909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
14225909f7f2Smatthias.ringwald 
14235909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
14245909f7f2Smatthias.ringwald 
14255909f7f2Smatthias.ringwald                     // restart timer
14265909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
14275909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
14285909f7f2Smatthias.ringwald 
14299da54300S[email protected]                     log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address));
14305909f7f2Smatthias.ringwald 
14315909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
14325909f7f2Smatthias.ringwald                     break;
14335909f7f2Smatthias.ringwald 
14349da54300S[email protected]             // log_info("LE buffer size: %u, count %u", READ_BT_16(packet,6), packet[8]);
143565a46ef3S[email protected] 
14365909f7f2Smatthias.ringwald                 default:
14375909f7f2Smatthias.ringwald                     break;
14385909f7f2Smatthias.ringwald             }
14395909f7f2Smatthias.ringwald             break;
14405909f7f2Smatthias.ringwald #endif
14416772a24cSmatthias.ringwald         default:
14426772a24cSmatthias.ringwald             break;
1443fe1ed1b8Smatthias.ringwald     }
1444fe1ed1b8Smatthias.ringwald 
14453429f56bSmatthias.ringwald     // handle BT initialization
14463a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
14476155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1448c9af4d3fS[email protected]     }
144922909952Smatthias.ringwald 
145089db417bSmatthias.ringwald     // help with BT sleep
14513a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
14523a9fb326S[email protected]         && hci_stack->substate == 1
145389db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
14543a9fb326S[email protected]         hci_stack->substate++;
145589db417bSmatthias.ringwald     }
145689db417bSmatthias.ringwald 
145786805605S[email protected]     // notify upper stack
14583a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
145994ab26f8Smatthias.ringwald 
146086805605S[email protected]     // moved here to give upper stack a chance to close down everything with hci_connection_t intact
146186805605S[email protected]     if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){
146286805605S[email protected]         if (!packet[2]){
146386805605S[email protected]             handle = READ_BT_16(packet, 3);
146486805605S[email protected]             hci_connection_t * conn = hci_connection_for_handle(handle);
146586805605S[email protected]             if (conn) {
146686805605S[email protected]                 uint8_t status = conn->bonding_status;
1467bb820044S[email protected]                 uint16_t flags = conn->bonding_flags;
146886805605S[email protected]                 bd_addr_t bd_address;
146986805605S[email protected]                 memcpy(&bd_address, conn->address, 6);
147086805605S[email protected]                 hci_shutdown_connection(conn);
1471bb820044S[email protected]                 // connection struct is gone, don't access anymore
1472bb820044S[email protected]                 if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){
147386805605S[email protected]                     hci_emit_dedicated_bonding_result(bd_address, status);
147486805605S[email protected]                 }
147586805605S[email protected]             }
147686805605S[email protected]         }
147786805605S[email protected]     }
147886805605S[email protected] 
147994ab26f8Smatthias.ringwald 	// execute main loop
148094ab26f8Smatthias.ringwald 	hci_run();
148116833f0aSmatthias.ringwald }
148216833f0aSmatthias.ringwald 
1483c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){
1484c91d150bS[email protected]     // not handled yet
1485c91d150bS[email protected] }
1486c91d150bS[email protected] 
14870a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
14885bb5bc3eS[email protected]     hci_dump_packet(packet_type, 1, packet, size);
148910e830c9Smatthias.ringwald     switch (packet_type) {
149010e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
149110e830c9Smatthias.ringwald             event_handler(packet, size);
149210e830c9Smatthias.ringwald             break;
149310e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
149410e830c9Smatthias.ringwald             acl_handler(packet, size);
149510e830c9Smatthias.ringwald             break;
1496c91d150bS[email protected]         case HCI_SCO_DATA_PACKET:
1497c91d150bS[email protected]             sco_handler(packet, size);
149810e830c9Smatthias.ringwald         default:
149910e830c9Smatthias.ringwald             break;
150010e830c9Smatthias.ringwald     }
150110e830c9Smatthias.ringwald }
150210e830c9Smatthias.ringwald 
1503fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
15042718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
15053a9fb326S[email protected]     hci_stack->packet_handler = handler;
150616833f0aSmatthias.ringwald }
150716833f0aSmatthias.ringwald 
15086155b3d3S[email protected] static void hci_state_reset(){
1509595bdbfbS[email protected]     // no connections yet
1510595bdbfbS[email protected]     hci_stack->connections = NULL;
151174308b23Smatthias.ringwald 
151274308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
151374308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
151474308b23Smatthias.ringwald     // hci_stack->connectable = 0;
151574308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1516595bdbfbS[email protected] 
151744935e40S[email protected]     // buffer is free
151844935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
151944935e40S[email protected] 
1520595bdbfbS[email protected]     // no pending cmds
1521595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1522595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1523595bdbfbS[email protected] 
1524595bdbfbS[email protected]     // LE
1525595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1526595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1527595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1528e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1529da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_min = 0x0006;
1530da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_interval_max = 0x0C80;
1531da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_latency_min = 0x0000;
1532da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_conn_latency_max = 0x03E8;
1533da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 0x000A;
1534da886c03S[email protected]     hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 0x0C80;
1535595bdbfbS[email protected] }
1536595bdbfbS[email protected] 
15374f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1538475c8125Smatthias.ringwald 
15393a9fb326S[email protected] #ifdef HAVE_MALLOC
15403a9fb326S[email protected]     if (!hci_stack) {
15413a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
15423a9fb326S[email protected]     }
15433a9fb326S[email protected] #else
15443a9fb326S[email protected]     hci_stack = &hci_stack_static;
15453a9fb326S[email protected] #endif
154666fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
15473a9fb326S[email protected] 
1548475c8125Smatthias.ringwald     // reference to use transport layer implementation
15493a9fb326S[email protected]     hci_stack->hci_transport = transport;
1550475c8125Smatthias.ringwald 
155111e23e5fSmatthias.ringwald     // references to used control implementation
15523a9fb326S[email protected]     hci_stack->control = control;
155311e23e5fSmatthias.ringwald 
155411e23e5fSmatthias.ringwald     // reference to used config
15553a9fb326S[email protected]     hci_stack->config = config;
155611e23e5fSmatthias.ringwald 
155716833f0aSmatthias.ringwald     // higher level handler
15583a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
155916833f0aSmatthias.ringwald 
1560404843c1Smatthias.ringwald     // store and open remote device db
15613a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
15623a9fb326S[email protected]     if (hci_stack->remote_device_db) {
15633a9fb326S[email protected]         hci_stack->remote_device_db->open();
1564404843c1Smatthias.ringwald     }
156529d53098Smatthias.ringwald 
15668fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
15673a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
15688fcba05dSmatthias.ringwald 
156916833f0aSmatthias.ringwald     // register packet handlers with transport
157010e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1571f5454fc6Smatthias.ringwald 
15723a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1573e2386ba1S[email protected] 
1574e2386ba1S[email protected]     // class of device
15753a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1576a45d6b9fS[email protected] 
1577f20168b8Smatthias.ringwald     // bondable by default
1578f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1579f20168b8Smatthias.ringwald 
158063048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
15813a9fb326S[email protected]     hci_stack->ssp_enable = 1;
15823a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
15833a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
15843a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
158569a97523S[email protected] 
1586595bdbfbS[email protected]     hci_state_reset();
1587475c8125Smatthias.ringwald }
1588475c8125Smatthias.ringwald 
1589404843c1Smatthias.ringwald void hci_close(){
1590404843c1Smatthias.ringwald     // close remote device db
15913a9fb326S[email protected]     if (hci_stack->remote_device_db) {
15923a9fb326S[email protected]         hci_stack->remote_device_db->close();
1593404843c1Smatthias.ringwald     }
15943a9fb326S[email protected]     while (hci_stack->connections) {
1595bc68afe2S[email protected]         // cancel all l2cap connections
1596bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
15973a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1598f5454fc6Smatthias.ringwald     }
1599f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
16003a9fb326S[email protected] 
16013a9fb326S[email protected] #ifdef HAVE_MALLOC
16023a9fb326S[email protected]     free(hci_stack);
16033a9fb326S[email protected] #endif
16043a9fb326S[email protected]     hci_stack = NULL;
1605404843c1Smatthias.ringwald }
1606404843c1Smatthias.ringwald 
16079e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
16089e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
16099e61646fS[email protected] }
16109e61646fS[email protected] 
1611f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h
1612f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){
1613f456b2d0S[email protected]     memcpy(hci_stack->custom_bd_addr, addr, 6);
1614f456b2d0S[email protected]     hci_stack->custom_bd_addr_set = 1;
1615f456b2d0S[email protected] }
1616f456b2d0S[email protected] 
161766fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
161866fb9560S[email protected]     disable_l2cap_timeouts = 1;
161966fb9560S[email protected] }
16208d213e1aSmatthias.ringwald // State-Module-Driver overview
16218d213e1aSmatthias.ringwald // state                    module  low-level
16228d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
16238d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
16248d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
16258d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1626d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1627d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1628c7e0c5f6Smatthias.ringwald 
162940d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
16307301ad89Smatthias.ringwald 
1631038bc64cSmatthias.ringwald     // power on
1632f9a30166Smatthias.ringwald     int err = 0;
16333a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
16343a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1635f9a30166Smatthias.ringwald     }
1636038bc64cSmatthias.ringwald     if (err){
16379da54300S[email protected]         log_error( "POWER_ON failed");
1638038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1639038bc64cSmatthias.ringwald         return err;
1640038bc64cSmatthias.ringwald     }
1641038bc64cSmatthias.ringwald 
1642038bc64cSmatthias.ringwald     // open low-level device
16433a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1644038bc64cSmatthias.ringwald     if (err){
16459da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
16463a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
16473a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1648f9a30166Smatthias.ringwald         }
1649038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1650038bc64cSmatthias.ringwald         return err;
1651038bc64cSmatthias.ringwald     }
16528d213e1aSmatthias.ringwald     return 0;
16538d213e1aSmatthias.ringwald }
1654038bc64cSmatthias.ringwald 
165540d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
16568d213e1aSmatthias.ringwald 
16579da54300S[email protected]     log_info("hci_power_control_off");
16589418f9c9Smatthias.ringwald 
16598d213e1aSmatthias.ringwald     // close low-level device
16603a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
16618d213e1aSmatthias.ringwald 
16629da54300S[email protected]     log_info("hci_power_control_off - hci_transport closed");
16639418f9c9Smatthias.ringwald 
16648d213e1aSmatthias.ringwald     // power off
16653a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
16663a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
16678d213e1aSmatthias.ringwald     }
16689418f9c9Smatthias.ringwald 
16699da54300S[email protected]     log_info("hci_power_control_off - control closed");
16709418f9c9Smatthias.ringwald 
16713a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
167272ea5239Smatthias.ringwald }
167372ea5239Smatthias.ringwald 
167440d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
167572ea5239Smatthias.ringwald 
16769da54300S[email protected]     log_info("hci_power_control_sleep");
16773144bce4Smatthias.ringwald 
1678b429b9b7Smatthias.ringwald #if 0
1679b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1680b429b9b7Smatthias.ringwald 
168172ea5239Smatthias.ringwald     // close low-level device
16823a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1683b429b9b7Smatthias.ringwald #endif
168472ea5239Smatthias.ringwald 
168572ea5239Smatthias.ringwald     // sleep mode
16863a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
16873a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
168872ea5239Smatthias.ringwald     }
1689b429b9b7Smatthias.ringwald 
16903a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
16918d213e1aSmatthias.ringwald }
16928d213e1aSmatthias.ringwald 
169340d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1694b429b9b7Smatthias.ringwald 
16959da54300S[email protected]     log_info("hci_power_control_wake");
1696b429b9b7Smatthias.ringwald 
1697b429b9b7Smatthias.ringwald     // wake on
16983a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
16993a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1700b429b9b7Smatthias.ringwald     }
1701b429b9b7Smatthias.ringwald 
1702b429b9b7Smatthias.ringwald #if 0
1703b429b9b7Smatthias.ringwald     // open low-level device
17043a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1705b429b9b7Smatthias.ringwald     if (err){
17069da54300S[email protected]         log_error( "HCI_INIT failed, turning Bluetooth off again");
17073a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
17083a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1709b429b9b7Smatthias.ringwald         }
1710b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1711b429b9b7Smatthias.ringwald         return err;
1712b429b9b7Smatthias.ringwald     }
1713b429b9b7Smatthias.ringwald #endif
1714b429b9b7Smatthias.ringwald 
1715b429b9b7Smatthias.ringwald     return 0;
1716b429b9b7Smatthias.ringwald }
1717b429b9b7Smatthias.ringwald 
171844935e40S[email protected] static void hci_power_transition_to_initializing(void){
171944935e40S[email protected]     // set up state machine
172044935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
172144935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
172244935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
172344935e40S[email protected]     hci_stack->substate = 0;
172444935e40S[email protected] }
1725b429b9b7Smatthias.ringwald 
17268d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1727d661ed19Smatthias.ringwald 
17289da54300S[email protected]     log_info("hci_power_control: %u, current mode %u", power_mode, hci_stack->state);
1729d661ed19Smatthias.ringwald 
17308d213e1aSmatthias.ringwald     int err = 0;
17313a9fb326S[email protected]     switch (hci_stack->state){
17328d213e1aSmatthias.ringwald 
17338d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
17348d213e1aSmatthias.ringwald             switch (power_mode){
17358d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
17368d213e1aSmatthias.ringwald                     err = hci_power_control_on();
173797b61c7bS[email protected]                     if (err) {
173897b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
173997b61c7bS[email protected]                         return err;
174097b61c7bS[email protected]                     }
174144935e40S[email protected]                     hci_power_transition_to_initializing();
17428d213e1aSmatthias.ringwald                     break;
17438d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
17448d213e1aSmatthias.ringwald                     // do nothing
17458d213e1aSmatthias.ringwald                     break;
17468d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1747b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
17488d213e1aSmatthias.ringwald                     break;
17498d213e1aSmatthias.ringwald             }
17508d213e1aSmatthias.ringwald             break;
17517301ad89Smatthias.ringwald 
17528d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
17538d213e1aSmatthias.ringwald             switch (power_mode){
17548d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
17558d213e1aSmatthias.ringwald                     // do nothing
17568d213e1aSmatthias.ringwald                     break;
17578d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
17588d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
17598d213e1aSmatthias.ringwald                     hci_power_control_off();
17608d213e1aSmatthias.ringwald                     break;
17618d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1762b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
176372ea5239Smatthias.ringwald                     hci_power_control_sleep();
17648d213e1aSmatthias.ringwald                     break;
17658d213e1aSmatthias.ringwald             }
17668d213e1aSmatthias.ringwald             break;
17677301ad89Smatthias.ringwald 
17688d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
17698d213e1aSmatthias.ringwald             switch (power_mode){
17708d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
17718d213e1aSmatthias.ringwald                     // do nothing
17728d213e1aSmatthias.ringwald                     break;
17738d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1774c7e0c5f6Smatthias.ringwald                     // see hci_run
17753a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
17768d213e1aSmatthias.ringwald                     break;
17778d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1778b546ac54Smatthias.ringwald                     // see hci_run
17793a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
17803a9fb326S[email protected]                     hci_stack->substate = 0;
17818d213e1aSmatthias.ringwald                     break;
17828d213e1aSmatthias.ringwald             }
17838d213e1aSmatthias.ringwald             break;
17847301ad89Smatthias.ringwald 
17858d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
17868d213e1aSmatthias.ringwald             switch (power_mode){
17878d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
178844935e40S[email protected]                     hci_power_transition_to_initializing();
17898d213e1aSmatthias.ringwald                     break;
17908d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
17918d213e1aSmatthias.ringwald                     // do nothing
17928d213e1aSmatthias.ringwald                     break;
17938d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1794b546ac54Smatthias.ringwald                     // see hci_run
17953a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
17963a9fb326S[email protected]                     hci_stack->substate = 0;
17978d213e1aSmatthias.ringwald                     break;
17988d213e1aSmatthias.ringwald             }
17998d213e1aSmatthias.ringwald             break;
18008d213e1aSmatthias.ringwald 
18018d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
18028d213e1aSmatthias.ringwald             switch (power_mode){
18038d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
180428171530Smatthias.ringwald 
180528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
180628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
180728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
18083a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
18093a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
181028171530Smatthias.ringwald                         break;
181128171530Smatthias.ringwald                     }
181228171530Smatthias.ringwald #endif
181344935e40S[email protected]                     hci_power_transition_to_initializing();
18148d213e1aSmatthias.ringwald                     break;
18158d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1816b546ac54Smatthias.ringwald                     // see hci_run
18173a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
18188d213e1aSmatthias.ringwald                     break;
18198d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1820b546ac54Smatthias.ringwald                     // do nothing
18218d213e1aSmatthias.ringwald                     break;
18228d213e1aSmatthias.ringwald             }
18238d213e1aSmatthias.ringwald             break;
18248d213e1aSmatthias.ringwald 
18258d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
18268d213e1aSmatthias.ringwald             switch (power_mode){
18278d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
182828171530Smatthias.ringwald 
182928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
183028171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
183128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
18323a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
18333a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1834758b46ceSmatthias.ringwald                         hci_update_scan_enable();
183528171530Smatthias.ringwald                         break;
183628171530Smatthias.ringwald                     }
183728171530Smatthias.ringwald #endif
18383144bce4Smatthias.ringwald                     err = hci_power_control_wake();
18393144bce4Smatthias.ringwald                     if (err) return err;
184044935e40S[email protected]                     hci_power_transition_to_initializing();
18418d213e1aSmatthias.ringwald                     break;
18428d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
18433a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
18448d213e1aSmatthias.ringwald                     break;
18458d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1846b546ac54Smatthias.ringwald                     // do nothing
18478d213e1aSmatthias.ringwald                     break;
18488d213e1aSmatthias.ringwald             }
18498d213e1aSmatthias.ringwald             break;
185011e23e5fSmatthias.ringwald     }
185168d92d03Smatthias.ringwald 
1852038bc64cSmatthias.ringwald     // create internal event
1853ee8bf225Smatthias.ringwald 	hci_emit_state();
1854ee8bf225Smatthias.ringwald 
185568d92d03Smatthias.ringwald 	// trigger next/first action
185668d92d03Smatthias.ringwald 	hci_run();
185768d92d03Smatthias.ringwald 
1858475c8125Smatthias.ringwald     return 0;
1859475c8125Smatthias.ringwald }
1860475c8125Smatthias.ringwald 
1861758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1862758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
18633a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1864758b46ceSmatthias.ringwald     hci_run();
1865758b46ceSmatthias.ringwald }
1866758b46ceSmatthias.ringwald 
1867381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1868381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1869381fbed8Smatthias.ringwald 
18703a9fb326S[email protected]     if (hci_stack->discoverable == enable){
18713a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1872381fbed8Smatthias.ringwald         return;
1873381fbed8Smatthias.ringwald     }
1874381fbed8Smatthias.ringwald 
18753a9fb326S[email protected]     hci_stack->discoverable = enable;
1876758b46ceSmatthias.ringwald     hci_update_scan_enable();
1877758b46ceSmatthias.ringwald }
1878b031bebbSmatthias.ringwald 
1879758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1880758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1881758b46ceSmatthias.ringwald 
1882758b46ceSmatthias.ringwald     // don't emit event
18833a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1884758b46ceSmatthias.ringwald 
18853a9fb326S[email protected]     hci_stack->connectable = enable;
1886758b46ceSmatthias.ringwald     hci_update_scan_enable();
1887381fbed8Smatthias.ringwald }
1888381fbed8Smatthias.ringwald 
1889*690bd0baS[email protected] void hci_local_bd_addr(bd_addr_t address_buffer){
1890*690bd0baS[email protected]     memcpy(address_buffer, hci_stack->local_bd_addr, 6);
18915061f3afS[email protected] }
18925061f3afS[email protected] 
189306b35ec0Smatthias.ringwald void hci_run(){
18948a485f27Smatthias.ringwald 
189532ab9390Smatthias.ringwald     hci_connection_t * connection;
189632ab9390Smatthias.ringwald     linked_item_t * it;
189732ab9390Smatthias.ringwald 
1898b5d8b22bS[email protected]     // send continuation fragments first, as they block the prepared packet buffer
1899b5d8b22bS[email protected]     if (hci_stack->acl_fragmentation_total_size > 0) {
1900b5d8b22bS[email protected]         hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer);
1901b5d8b22bS[email protected]         if (hci_can_send_prepared_acl_packet_now(con_handle)){
1902b5d8b22bS[email protected]             hci_connection_t *connection = hci_connection_for_handle(con_handle);
1903b5d8b22bS[email protected]             if (connection) {
1904b5d8b22bS[email protected]                 hci_send_acl_packet_fragments(connection);
1905b5d8b22bS[email protected]                 return;
1906b5d8b22bS[email protected]             }
1907b5d8b22bS[email protected]             // connection gone -> discard further fragments
1908b5d8b22bS[email protected]             hci_stack->acl_fragmentation_total_size = 0;
1909b5d8b22bS[email protected]             hci_stack->acl_fragmentation_pos = 0;
1910b5d8b22bS[email protected]         }
1911b5d8b22bS[email protected]     }
1912b5d8b22bS[email protected] 
1913d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()) return;
1914ce4c8fabSmatthias.ringwald 
1915b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1916b031bebbSmatthias.ringwald 
1917b031bebbSmatthias.ringwald     // decline incoming connections
19183a9fb326S[email protected]     if (hci_stack->decline_reason){
19193a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
19203a9fb326S[email protected]         hci_stack->decline_reason = 0;
19213a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1922dbe1a790S[email protected]         return;
1923ce4c8fabSmatthias.ringwald     }
1924ce4c8fabSmatthias.ringwald 
1925b031bebbSmatthias.ringwald     // send scan enable
19263a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
19273a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
19283a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1929dbe1a790S[email protected]         return;
1930b031bebbSmatthias.ringwald     }
1931b031bebbSmatthias.ringwald 
1932b2f949feS[email protected] #ifdef HAVE_BLE
19337bdc6798S[email protected]     // handle le scan
19347bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
19357bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
19367bdc6798S[email protected]             case LE_START_SCAN:
19377bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
19387bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
19397bdc6798S[email protected]                 return;
19407bdc6798S[email protected] 
19417bdc6798S[email protected]             case LE_STOP_SCAN:
19427bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
19437bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
19447bdc6798S[email protected]                 return;
19457bdc6798S[email protected]             default:
19467bdc6798S[email protected]                 break;
19477bdc6798S[email protected]         }
1948e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
1949e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
1950e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
1951e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
1952e2602ea2Smatthias.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);
1953e2602ea2Smatthias.ringwald             return;
1954e2602ea2Smatthias.ringwald         }
19557bdc6798S[email protected]     }
1956b2f949feS[email protected] #endif
19577bdc6798S[email protected] 
195832ab9390Smatthias.ringwald     // send pending HCI commands
19593a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
1960b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
196132ab9390Smatthias.ringwald 
19620bf6344aS[email protected]         switch(connection->state){
19630bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
19644f3229d8S[email protected]                 switch(connection->address_type){
19654f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
19669da54300S[email protected]                         log_info("sending hci_create_connection");
1967ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
19684f3229d8S[email protected]                         break;
19694f3229d8S[email protected]                     default:
1970b2f949feS[email protected] #ifdef HAVE_BLE
19719da54300S[email protected]                         log_info("sending hci_le_create_connection");
19724f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
1973b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
1974b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
19754f3229d8S[email protected]                                      0,         // don't use whitelist
19764f3229d8S[email protected]                                      connection->address_type, // peer address type
19774f3229d8S[email protected]                                      connection->address,      // peer bd addr
1978b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
1979b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
1980b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
19814f3229d8S[email protected]                                      0,         // conn latency
1982b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
1983b2571179Smatthias.ringwald                                      0x0001,    // min ce length
1984b2571179Smatthias.ringwald                                      0x0001     // max ce length
19854f3229d8S[email protected]                                      );
19864f3229d8S[email protected] 
19874f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
1988b2f949feS[email protected] #endif
19894f3229d8S[email protected]                         break;
19904f3229d8S[email protected]                 }
1991ad83dc6aS[email protected]                 return;
1992ad83dc6aS[email protected] 
19930bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
19949da54300S[email protected]                 log_info("sending hci_accept_connection_request");
199532ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
1996e35edcc1S[email protected]                 if (connection->address_type == BD_ADDR_TYPE_CLASSIC){
199734d2123cS[email protected]                     hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1998e35edcc1S[email protected]                 } else {
1999e35edcc1S[email protected]                     // TODO: allows to customize synchronous connection parameters
2000e35edcc1S[email protected]                     hci_send_cmd(&hci_accept_synchronous_connection_command, connection->address, 8000, 8000, 0xFFFF, 0x0060, 0xFF, 0x003F);
2001e35edcc1S[email protected]                 }
2002dbe1a790S[email protected]                 return;
20030bf6344aS[email protected] 
2004a6725849S[email protected] #ifdef HAVE_BLE
20050bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
20060bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
20070bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
20080bf6344aS[email protected]                 return;
2009a6725849S[email protected] #endif
20100bf6344aS[email protected]             case SEND_DISCONNECT:
20110bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
20127851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
20130bf6344aS[email protected]                 return;
20140bf6344aS[email protected] 
20150bf6344aS[email protected]             default:
20160bf6344aS[email protected]                 break;
2017c7e0c5f6Smatthias.ringwald         }
2018c7e0c5f6Smatthias.ringwald 
201932ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
20209da54300S[email protected]             log_info("responding to link key request");
202134d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
202232ab9390Smatthias.ringwald             link_key_t link_key;
2023c77e8838S[email protected]             link_key_type_t link_key_type;
20243a9fb326S[email protected]             if ( hci_stack->remote_device_db
20252e77e513S[email protected]               && hci_stack->remote_device_db->get_link_key(connection->address, link_key, &link_key_type)
202634d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
20279ab95c90S[email protected]                connection->link_key_type = link_key_type;
202832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
202932ab9390Smatthias.ringwald             } else {
203032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
203132ab9390Smatthias.ringwald             }
2032dbe1a790S[email protected]             return;
203332ab9390Smatthias.ringwald         }
20341d6b20aeS[email protected] 
2035899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
20369da54300S[email protected]             log_info("denying to pin request");
2037899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
203834d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
20394c57c146S[email protected]             return;
20404c57c146S[email protected]         }
20414c57c146S[email protected] 
2042dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
204334d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
204482d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
204582d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
2046106d6d11S[email protected]                 // tweak authentication requirements
20473a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
2048106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
20499faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
20509faad3abS[email protected]                 }
20519faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
20529faad3abS[email protected]                     authreq |= 1;
2053106d6d11S[email protected]                 }
20543a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
2055f8fb5f6eS[email protected]             } else {
2056f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
2057f8fb5f6eS[email protected]             }
2058dbe1a790S[email protected]             return;
205932ab9390Smatthias.ringwald         }
206032ab9390Smatthias.ringwald 
2061dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
2062dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
206334d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
2064dbe1a790S[email protected]             return;
2065dbe1a790S[email protected]         }
2066dbe1a790S[email protected] 
2067dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
2068dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
206934d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
2070dbe1a790S[email protected]             return;
2071dbe1a790S[email protected]         }
2072afd4e962S[email protected] 
2073afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
2074afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
207534d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
20762bd8b7e7S[email protected]             return;
20772bd8b7e7S[email protected]         }
20782bd8b7e7S[email protected] 
20792bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
20802bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
208134d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
208234d2123cS[email protected]             return;
208334d2123cS[email protected]         }
2084ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
2085ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
20861bd5283dS[email protected]             connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT;
208752d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
2088ad83dc6aS[email protected]             return;
2089ad83dc6aS[email protected]         }
209034d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
209134d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
209234d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
20932bd8b7e7S[email protected]             return;
2094afd4e962S[email protected]         }
2095dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
2096dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
2097dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
2098dce78009S[email protected]             return;
2099dce78009S[email protected]         }
2100da886c03S[email protected] 
2101c37a3166S[email protected] #ifdef HAVE_BLE
2102da886c03S[email protected]         if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){
2103da886c03S[email protected]             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
2104da886c03S[email protected] 
2105c37a3166S[email protected]             uint16_t connection_interval_min = connection->le_conn_interval_min;
2106c37a3166S[email protected]             connection->le_conn_interval_min = 0;
2107c37a3166S[email protected]             hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min,
2108c37a3166S[email protected]                 connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout,
2109c37a3166S[email protected]                 0x0000, 0xffff);
2110c37a3166S[email protected]         }
2111c37a3166S[email protected] #endif
2112dbe1a790S[email protected]     }
2113c7e0c5f6Smatthias.ringwald 
21143a9fb326S[email protected]     switch (hci_stack->state){
21153429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
2116bd88fde9S[email protected]             hci_initializing_state_machine();
21173429f56bSmatthias.ringwald             break;
2118c7e0c5f6Smatthias.ringwald 
2119c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
2120c7e0c5f6Smatthias.ringwald 
21219da54300S[email protected]             log_info("HCI_STATE_HALTING");
2122c7e0c5f6Smatthias.ringwald             // close all open connections
21233a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
2124c7e0c5f6Smatthias.ringwald             if (connection){
212532ab9390Smatthias.ringwald 
2126c7e0c5f6Smatthias.ringwald                 // send disconnect
2127d94d3cafS[email protected]                 if (!hci_can_send_command_packet_now()) return;
212832ab9390Smatthias.ringwald 
21299da54300S[email protected]                 log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
21306ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
2131c7e0c5f6Smatthias.ringwald 
2132c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
2133c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
2134c7e0c5f6Smatthias.ringwald                 return;
2135c7e0c5f6Smatthias.ringwald             }
21369da54300S[email protected]             log_info("HCI_STATE_HALTING, calling off");
2137c7e0c5f6Smatthias.ringwald 
213872ea5239Smatthias.ringwald             // switch mode
2139c7e0c5f6Smatthias.ringwald             hci_power_control_off();
21409418f9c9Smatthias.ringwald 
21419da54300S[email protected]             log_info("HCI_STATE_HALTING, emitting state");
214272ea5239Smatthias.ringwald             hci_emit_state();
21439da54300S[email protected]             log_info("HCI_STATE_HALTING, done");
214472ea5239Smatthias.ringwald             break;
2145c7e0c5f6Smatthias.ringwald 
214672ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
21473a9fb326S[email protected]             switch(hci_stack->substate) {
214889db417bSmatthias.ringwald                 case 0:
21499da54300S[email protected]                     log_info("HCI_STATE_FALLING_ASLEEP");
215072ea5239Smatthias.ringwald                     // close all open connections
21513a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
215266da7044Smatthias.ringwald 
215328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
215466da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
215566da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
215666da7044Smatthias.ringwald                         connection = NULL;
215766da7044Smatthias.ringwald                     }
215866da7044Smatthias.ringwald #endif
215972ea5239Smatthias.ringwald                     if (connection){
216032ab9390Smatthias.ringwald 
216172ea5239Smatthias.ringwald                         // send disconnect
2162d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
216332ab9390Smatthias.ringwald 
21649da54300S[email protected]                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle);
21656ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
216672ea5239Smatthias.ringwald 
216772ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
216872ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
216972ea5239Smatthias.ringwald                         return;
217072ea5239Smatthias.ringwald                     }
217172ea5239Smatthias.ringwald 
217292368cd3S[email protected]                     if (hci_classic_supported()){
217389db417bSmatthias.ringwald                         // disable page and inquiry scan
2174d94d3cafS[email protected]                         if (!hci_can_send_command_packet_now()) return;
217532ab9390Smatthias.ringwald 
21769da54300S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans");
21773a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
217889db417bSmatthias.ringwald 
217989db417bSmatthias.ringwald                         // continue in next sub state
21803a9fb326S[email protected]                         hci_stack->substate++;
218189db417bSmatthias.ringwald                         break;
218292368cd3S[email protected]                     }
218392368cd3S[email protected]                     // fall through for ble-only chips
218492368cd3S[email protected] 
218589db417bSmatthias.ringwald                 case 2:
21869da54300S[email protected]                     log_info("HCI_STATE_HALTING, calling sleep");
218728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
218828171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
218928171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
219028171530Smatthias.ringwald                         // SLEEP MODE reached
21913a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
219228171530Smatthias.ringwald                         hci_emit_state();
219328171530Smatthias.ringwald                         break;
219428171530Smatthias.ringwald                     }
219528171530Smatthias.ringwald #endif
219672ea5239Smatthias.ringwald                     // switch mode
21973a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
2198c7e0c5f6Smatthias.ringwald                     hci_emit_state();
219928171530Smatthias.ringwald                     break;
220028171530Smatthias.ringwald 
220189db417bSmatthias.ringwald                 default:
220289db417bSmatthias.ringwald                     break;
220389db417bSmatthias.ringwald             }
2204c7e0c5f6Smatthias.ringwald             break;
2205c7e0c5f6Smatthias.ringwald 
22063429f56bSmatthias.ringwald         default:
22073429f56bSmatthias.ringwald             break;
22081f504dbdSmatthias.ringwald     }
22093429f56bSmatthias.ringwald }
221016833f0aSmatthias.ringwald 
221131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
2212c8e4258aSmatthias.ringwald     bd_addr_t addr;
2213c8e4258aSmatthias.ringwald     hci_connection_t * conn;
2214c8e4258aSmatthias.ringwald     // house-keeping
2215c8e4258aSmatthias.ringwald 
2216c8e4258aSmatthias.ringwald     // create_connection?
2217c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
2218c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
22199da54300S[email protected]         log_info("Create_connection to %s", bd_addr_to_str(addr));
2220c8e4258aSmatthias.ringwald 
22212e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
2222ad83dc6aS[email protected]         if (!conn){
222396a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
222417f1ba2aSmatthias.ringwald             if (!conn){
222517f1ba2aSmatthias.ringwald                 // notify client that alloc failed
222617f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
222717f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
222817f1ba2aSmatthias.ringwald             }
2229ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
2230ad83dc6aS[email protected]         }
2231ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
2232ad83dc6aS[email protected]         switch (conn->state){
2233ad83dc6aS[email protected]             // if connection active exists
2234ad83dc6aS[email protected]             case OPEN:
223562bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
2236ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
223762bda3fbS[email protected]                 return 0;
2238ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
2239ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
2240ad83dc6aS[email protected]                 break;
2241ad83dc6aS[email protected]             default:
2242ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
2243ad83dc6aS[email protected]                 return 0;
2244ad83dc6aS[email protected]         }
2245c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
2246c8e4258aSmatthias.ringwald     }
22477fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
22487fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
22497fde4af9Smatthias.ringwald     }
22507fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
22517fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
22527fde4af9Smatthias.ringwald     }
22537fde4af9Smatthias.ringwald 
22548ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
22553a9fb326S[email protected]         if (hci_stack->remote_device_db){
22568ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
22572e77e513S[email protected]             hci_stack->remote_device_db->delete_link_key(addr);
22588ef73945Smatthias.ringwald         }
22598ef73945Smatthias.ringwald     }
2260c8e4258aSmatthias.ringwald 
22616724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
22626724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
22636724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
22642e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
22656724cd9eS[email protected]         if (conn){
22666724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
22676724cd9eS[email protected]         }
22686724cd9eS[email protected]     }
22696724cd9eS[email protected] 
22706724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
22716724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
22726724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
22736724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
22746724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
22752e77e513S[email protected]         conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
22766724cd9eS[email protected]         if (conn){
22776724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
22786724cd9eS[email protected]         }
22796724cd9eS[email protected]     }
22806724cd9eS[email protected] 
228169a97523S[email protected] #ifdef HAVE_BLE
228269a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
22833a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
228469a97523S[email protected]     }
228569a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
22863a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
228769a97523S[email protected]     }
228869a97523S[email protected] #endif
228969a97523S[email protected] 
22903a9fb326S[email protected]     hci_stack->num_cmd_packets--;
22915bb5bc3eS[email protected] 
22925bb5bc3eS[email protected]     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size);
22936b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
22946b4af23dS[email protected] 
2295d051460cS[email protected]     // release packet buffer for synchronous transport implementations
2296c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
22976b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
22986b4af23dS[email protected]     }
22996b4af23dS[email protected] 
23006b4af23dS[email protected]     return err;
230131452debSmatthias.ringwald }
23028adf0ddaSmatthias.ringwald 
23032bd8b7e7S[email protected] // disconnect because of security block
23042bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
23052bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
23062bd8b7e7S[email protected]     if (!connection) return;
23072bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
23082bd8b7e7S[email protected] }
23092bd8b7e7S[email protected] 
23102bd8b7e7S[email protected] 
2311dbe1a790S[email protected] // Configure Secure Simple Pairing
2312dbe1a790S[email protected] 
2313dbe1a790S[email protected] // enable will enable SSP during init
2314dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
23153a9fb326S[email protected]     hci_stack->ssp_enable = enable;
2316dbe1a790S[email protected] }
2317dbe1a790S[email protected] 
23182bd8b7e7S[email protected] int hci_local_ssp_activated(){
23193a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
23202bd8b7e7S[email protected] }
23212bd8b7e7S[email protected] 
2322dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
2323dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
23243a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
2325dbe1a790S[email protected] }
2326dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
23273a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
2328dbe1a790S[email protected] }
2329dbe1a790S[email protected] 
2330dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
2331dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
23323a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
2333dbe1a790S[email protected] }
2334dbe1a790S[email protected] 
23351cd208adSmatthias.ringwald /**
23361cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
23371cd208adSmatthias.ringwald  */
2338fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
23395127cc62S[email protected] 
2340d94d3cafS[email protected]     if (!hci_can_send_command_packet_now()){
23419d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
23429d14b626S[email protected]         return 0;
23439d14b626S[email protected]     }
23449d14b626S[email protected] 
23455127cc62S[email protected]     // for HCI INITIALIZATION
23469da54300S[email protected]     // log_info("hci_send_cmd: opcode %04x", cmd->opcode);
23475127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
23485127cc62S[email protected] 
23499d14b626S[email protected]     hci_reserve_packet_buffer();
23509d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
23519d14b626S[email protected] 
23521cd208adSmatthias.ringwald     va_list argptr;
23531cd208adSmatthias.ringwald     va_start(argptr, cmd);
23549d14b626S[email protected]     uint16_t size = hci_create_cmd_internal(packet, cmd, argptr);
23551cd208adSmatthias.ringwald     va_end(argptr);
23569d14b626S[email protected] 
23579d14b626S[email protected]     return hci_send_cmd_packet(packet, size);
235893b8dc03Smatthias.ringwald }
2359c8e4258aSmatthias.ringwald 
2360ee091cf1Smatthias.ringwald // Create various non-HCI events.
2361ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
2362ee091cf1Smatthias.ringwald 
2363c8e4258aSmatthias.ringwald void hci_emit_state(){
23643a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
2365425d1371Smatthias.ringwald     uint8_t event[3];
236680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
2367425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
23683a9fb326S[email protected]     event[2] = hci_stack->state;
2369425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23703a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2371c8e4258aSmatthias.ringwald }
2372c8e4258aSmatthias.ringwald 
237317f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
2374425d1371Smatthias.ringwald     uint8_t event[13];
2375c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
2376425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
237717f1ba2aSmatthias.ringwald     event[2] = status;
2378c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
2379c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
2380c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
2381c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
2382425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
23833a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2384c8e4258aSmatthias.ringwald }
2385c8e4258aSmatthias.ringwald 
23862e77e513S[email protected] void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t address, uint16_t conn_handle, uint8_t status){
23874f3229d8S[email protected]     uint8_t event[21];
23884f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
23894f3229d8S[email protected]     event[1] = sizeof(event) - 2;
23904f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
23914f3229d8S[email protected]     event[3] = status;
23926e2e9a6bS[email protected]     bt_store_16(event, 4, conn_handle);
23934f3229d8S[email protected]     event[6] = 0; // TODO: role
23946e2e9a6bS[email protected]     event[7] = address_type;
23952e77e513S[email protected]     bt_flip_addr(&event[8], address);
23964f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
23974f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
23984f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
23994f3229d8S[email protected]     event[20] = 0; // master clock accuracy
24004f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
24014f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
24024f3229d8S[email protected] }
24034f3229d8S[email protected] 
24043c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
2405425d1371Smatthias.ringwald     uint8_t event[6];
24063c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
2407e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
24083c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
24093c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
24103c4d4b90Smatthias.ringwald     event[5] = reason;
2411425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
24123a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
24133c4d4b90Smatthias.ringwald }
24143c4d4b90Smatthias.ringwald 
2415ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
241666fb9560S[email protected]     if (disable_l2cap_timeouts) return;
2417e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2418425d1371Smatthias.ringwald     uint8_t event[4];
241980d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2420425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2421ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2422425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
24233a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2424ee091cf1Smatthias.ringwald }
242543bfb1bdSmatthias.ringwald 
242643bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
2427e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2428425d1371Smatthias.ringwald     uint8_t event[3];
242980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2430425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
243143bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2432425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24333a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
243443bfb1bdSmatthias.ringwald }
2435038bc64cSmatthias.ringwald 
2436038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
2437e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2438425d1371Smatthias.ringwald     uint8_t event[2];
243980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2440425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2441425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24423a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2443038bc64cSmatthias.ringwald }
24441b0e3922Smatthias.ringwald 
244509ba8edeSmatthias.ringwald #ifndef EMBEDDED
24461b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
2447e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2448425d1371Smatthias.ringwald     uint8_t event[6];
24491b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2450425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2451425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2452425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
2453425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
2454425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24553a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
24561b0e3922Smatthias.ringwald }
245709ba8edeSmatthias.ringwald #endif
24581b0e3922Smatthias.ringwald 
24592ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2460e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2461425d1371Smatthias.ringwald     uint8_t event[3];
24622ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2463425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
24642ed6235cSmatthias.ringwald     event[2] = enabled;
2465425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24663a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
24672ed6235cSmatthias.ringwald }
2468627c2f45Smatthias.ringwald 
24692e77e513S[email protected] void hci_emit_remote_name_cached(bd_addr_t addr, device_name_t *name){
2470e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2471627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2472e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2473f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
24742e77e513S[email protected]     bt_flip_addr(&event[3], addr);
2475f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2476e0abb8e7S[email protected] 
2477e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
24782e77e513S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(addr), &event[9]);
2479e0abb8e7S[email protected] 
2480e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
24813a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2482627c2f45Smatthias.ringwald }
2483381fbed8Smatthias.ringwald 
2484381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2485e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2486425d1371Smatthias.ringwald     uint8_t event[3];
2487381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2488425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2489381fbed8Smatthias.ringwald     event[2] = enabled;
2490425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24913a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2492381fbed8Smatthias.ringwald }
2493458bf4e8S[email protected] 
2494a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2495df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2496a00031e2S[email protected]     uint8_t event[5];
2497e00caf9cS[email protected]     int pos = 0;
2498a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2499e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2500a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2501e00caf9cS[email protected]     pos += 2;
2502e00caf9cS[email protected]     event[pos++] = level;
2503e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
25043a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2505e00caf9cS[email protected] }
2506e00caf9cS[email protected] 
25071bd5283dS[email protected] void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){
2508ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2509ad83dc6aS[email protected]     uint8_t event[9];
2510ad83dc6aS[email protected]     int pos = 0;
2511ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2512ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2513ad83dc6aS[email protected]     event[pos++] = status;
25142e77e513S[email protected]     bt_flip_addr( &event[pos], address);
2515ad83dc6aS[email protected]     pos += 6;
2516ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
25173a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2518ad83dc6aS[email protected] }
2519ad83dc6aS[email protected] 
25202bd8b7e7S[email protected] // query if remote side supports SSP
25212bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
25222bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
25232bd8b7e7S[email protected]     if (!connection) return 0;
25242bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
25252bd8b7e7S[email protected] }
25262bd8b7e7S[email protected] 
2527df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2528df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2529df3354fcS[email protected] }
2530df3354fcS[email protected] 
2531458bf4e8S[email protected] // GAP API
2532458bf4e8S[email protected] /**
2533458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2534458bf4e8S[email protected]  * @praram enabled
2535458bf4e8S[email protected]  */
25364c57c146S[email protected] void gap_set_bondable_mode(int enable){
25373a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2538458bf4e8S[email protected] }
2539cb230b9dS[email protected] 
2540cb230b9dS[email protected] /**
254134d2123cS[email protected]  * @brief map link keys to security levels
2542cb230b9dS[email protected]  */
254334d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
254434d2123cS[email protected]     switch (link_key_type){
25453c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
25463c68dfa9S[email protected]             return LEVEL_4;
25473c68dfa9S[email protected]         case COMBINATION_KEY:
25483c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
25493c68dfa9S[email protected]             return LEVEL_3;
25503c68dfa9S[email protected]         default:
25513c68dfa9S[email protected]             return LEVEL_2;
25523c68dfa9S[email protected]     }
2553cb230b9dS[email protected] }
2554cb230b9dS[email protected] 
2555a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
255634d2123cS[email protected]     if (!connection) return LEVEL_0;
255734d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
255834d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
255934d2123cS[email protected] }
256034d2123cS[email protected] 
256134d2123cS[email protected] 
2562106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
25635127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
2564106d6d11S[email protected]     return level > LEVEL_2;
2565106d6d11S[email protected] }
2566106d6d11S[email protected] 
256734d2123cS[email protected] /**
256834d2123cS[email protected]  * @brief get current security level
256934d2123cS[email protected]  */
257034d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
257134d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
257234d2123cS[email protected]     if (!connection) return LEVEL_0;
257334d2123cS[email protected]     return gap_security_level_for_connection(connection);
257434d2123cS[email protected] }
257534d2123cS[email protected] 
2576cb230b9dS[email protected] /**
2577cb230b9dS[email protected]  * @brief request connection to device to
2578cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2579cb230b9dS[email protected]  */
258034d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
258134d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
258234d2123cS[email protected]     if (!connection){
2583a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
258434d2123cS[email protected]         return;
258534d2123cS[email protected]     }
258634d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
258734d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
258834d2123cS[email protected]     if (current_level >= requested_level){
2589a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
259034d2123cS[email protected]         return;
259134d2123cS[email protected]     }
2592a00031e2S[email protected] 
259334d2123cS[email protected]     connection->requested_security_level = requested_level;
2594a00031e2S[email protected] 
259525bf5872S[email protected] #if 0
259625bf5872S[email protected]     // sending encryption request without a link key results in an error.
259725bf5872S[email protected]     // TODO: figure out how to use it properly
259825bf5872S[email protected] 
2599fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
26003a9fb326S[email protected]     if (hci_stack->remote_device_db){
2601a00031e2S[email protected]         link_key_type_t link_key_type;
2602a00031e2S[email protected]         link_key_t      link_key;
26033a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2604a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2605a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2606a00031e2S[email protected]                 return;
2607a00031e2S[email protected]             }
2608a00031e2S[email protected]         }
2609a00031e2S[email protected]     }
261025bf5872S[email protected] #endif
2611a00031e2S[email protected] 
26121eb2563eS[email protected]     // try to authenticate connection
26131eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2614e80b2cf9S[email protected]     hci_run();
2615e00caf9cS[email protected] }
2616ad83dc6aS[email protected] 
2617ad83dc6aS[email protected] /**
2618ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2619ad83dc6aS[email protected]  * @param device
2620ad83dc6aS[email protected]  * @param request MITM protection
2621ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2622ad83dc6aS[email protected]  */
2623ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2624ad83dc6aS[email protected] 
2625ad83dc6aS[email protected]     // create connection state machine
262696a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2627ad83dc6aS[email protected] 
2628ad83dc6aS[email protected]     if (!connection){
2629ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2630ad83dc6aS[email protected]     }
2631ad83dc6aS[email protected] 
2632ad83dc6aS[email protected]     // delete linkn key
26332e77e513S[email protected]     hci_drop_link_key_for_bd_addr(device);
2634ad83dc6aS[email protected] 
2635ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2636ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2637ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
26385127cc62S[email protected]     log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level);
2639ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2640ad83dc6aS[email protected] 
2641ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2642ad83dc6aS[email protected] 
2643ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2644ad83dc6aS[email protected]     // handle: authentication failure
2645ad83dc6aS[email protected]     // handle: disconnect on done
2646ad83dc6aS[email protected] 
2647ad83dc6aS[email protected]     hci_run();
2648ad83dc6aS[email protected] 
2649ad83dc6aS[email protected]     return 0;
2650ad83dc6aS[email protected] }
26518e618f72S[email protected] 
26528e618f72S[email protected] void gap_set_local_name(const char * local_name){
26538e618f72S[email protected]     hci_stack->local_name = local_name;
26548e618f72S[email protected] }
26558e618f72S[email protected] 
26567bdc6798S[email protected] le_command_status_t le_central_start_scan(){
265736af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
26587bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
26597bdc6798S[email protected]     hci_run();
26607bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
26617bdc6798S[email protected] }
26628e618f72S[email protected] 
26637bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
266436af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
26657bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
26667bdc6798S[email protected]     hci_run();
26677bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
26687bdc6798S[email protected] }
26694f3229d8S[email protected] 
2670ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2671ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2672ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2673ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2674ef11999fSmatthias.ringwald     hci_run();
2675ef11999fSmatthias.ringwald }
26764f3229d8S[email protected] 
26772e77e513S[email protected] le_command_status_t le_central_connect(bd_addr_t  addr, bd_addr_type_t addr_type){
26784f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
26794f3229d8S[email protected]     if (!conn){
26801f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
26812e77e513S[email protected]         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
26824f3229d8S[email protected]         if (!conn){
26834f3229d8S[email protected]             // notify client that alloc failed
26846e2e9a6bS[email protected]             hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
26856e2e9a6bS[email protected]             log_info("le_central_connect: failed to alloc hci_connection_t");
26864f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
26874f3229d8S[email protected]         }
26884f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
26891f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2690564fca32S[email protected]         hci_run();
26914f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
26924f3229d8S[email protected]     }
26930bf6344aS[email protected] 
26940bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
26950bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
26960bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
26972e77e513S[email protected]         hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED);
26981f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
26990bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
27000bf6344aS[email protected]     }
27010bf6344aS[email protected] 
27021f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
27032e77e513S[email protected]     hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0);
27044f3229d8S[email protected]     hci_run();
27054f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
27064f3229d8S[email protected] }
27074f3229d8S[email protected] 
27087851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
27097851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){
27100bf6344aS[email protected]     linked_item_t *it;
27110bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
27120bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
27130bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
27140bf6344aS[email protected]         switch (conn->state){
2715a6725849S[email protected]             case SEND_CREATE_CONNECTION:
27167851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
27177851196eSmatthias.ringwald                 return conn;
27187851196eSmatthias.ringwald             default:
27197851196eSmatthias.ringwald                 break;
27207851196eSmatthias.ringwald         };
27217851196eSmatthias.ringwald     }
27227851196eSmatthias.ringwald     return NULL;
27237851196eSmatthias.ringwald }
27247851196eSmatthias.ringwald 
27257851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){
27267851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
27277851196eSmatthias.ringwald     switch (conn->state){
27287851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
27297851196eSmatthias.ringwald             // skip sending create connection and emit event instead
27302e77e513S[email protected]             hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
27317851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
27327851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
27330bf6344aS[email protected]             break;
2734a6725849S[email protected]         case SENT_CREATE_CONNECTION:
27357851196eSmatthias.ringwald             // request to send cancel connection
27360bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
27370bf6344aS[email protected]             hci_run();
27380bf6344aS[email protected]             break;
27390bf6344aS[email protected]         default:
27400bf6344aS[email protected]             break;
27410bf6344aS[email protected]     }
2742e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2743e31f89a7S[email protected] }
27444f3229d8S[email protected] 
2745c37a3166S[email protected] /**
2746c37a3166S[email protected]  * @brief Updates the connection parameters for a given LE connection
2747c37a3166S[email protected]  * @param handle
2748c37a3166S[email protected]  * @param conn_interval_min (unit: 1.25ms)
2749c37a3166S[email protected]  * @param conn_interval_max (unit: 1.25ms)
2750c37a3166S[email protected]  * @param conn_latency
2751c37a3166S[email protected]  * @param supervision_timeout (unit: 10ms)
2752c37a3166S[email protected]  * @returns 0 if ok
2753c37a3166S[email protected]  */
2754c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
2755c37a3166S[email protected]     uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
2756c37a3166S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
2757c37a3166S[email protected]     if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2758c37a3166S[email protected]     connection->le_conn_interval_min = conn_interval_min;
2759c37a3166S[email protected]     connection->le_conn_interval_max = conn_interval_max;
2760c37a3166S[email protected]     connection->le_conn_latency = conn_latency;
2761c37a3166S[email protected]     connection->le_supervision_timeout = supervision_timeout;
2762c37a3166S[email protected]     return 0;
2763c37a3166S[email protected] }
2764c37a3166S[email protected] 
27655917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
27665917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
27675917a5c5S[email protected]     if (!conn){
27687851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
27695917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
27705917a5c5S[email protected]     }
27715917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
27725917a5c5S[email protected]     hci_run();
27734f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
27744f3229d8S[email protected] }
277504a6ef8cSmatthias.ringwald 
277604a6ef8cSmatthias.ringwald void hci_disconnect_all(){
277704a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
277804a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
277904a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
278004a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
278104a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
278204a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
278304a6ef8cSmatthias.ringwald     }
2784d31fba26S[email protected]     hci_run();
278504a6ef8cSmatthias.ringwald }
2786