xref: /btstack/src/hci.c (revision 206a90311e870d317fd990b95f336b4f65a08036)
11f504dbdSmatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
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  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
381f504dbdSmatthias.ringwald  *  hci.c
391f504dbdSmatthias.ringwald  *
401f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
411f504dbdSmatthias.ringwald  *
421f504dbdSmatthias.ringwald  */
431f504dbdSmatthias.ringwald 
44bde315ceS[email protected] #include "btstack-config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
474c57c146S[email protected] #include "gap.h"
487f2435e6Smatthias.ringwald 
4993b8dc03Smatthias.ringwald #include <stdarg.h>
5093b8dc03Smatthias.ringwald #include <string.h>
5156fe0872Smatthias.ringwald #include <stdio.h>
527f2435e6Smatthias.ringwald 
53549e6ebeSmatthias.ringwald #ifndef EMBEDDED
54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5509ba8edeSmatthias.ringwald #include <btstack/version.h>
56549e6ebeSmatthias.ringwald #endif
57549e6ebeSmatthias.ringwald 
58a3b02b71Smatthias.ringwald #include "btstack_memory.h"
597f2435e6Smatthias.ringwald #include "debug.h"
60d8905019Smatthias.ringwald #include "hci_dump.h"
6193b8dc03Smatthias.ringwald 
62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
631b0e3922Smatthias.ringwald 
64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
65ee091cf1Smatthias.ringwald 
66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
67da5275c5S[email protected] 
6828171530Smatthias.ringwald #ifdef USE_BLUETOOL
6928171530Smatthias.ringwald #include "bt_control_iphone.h"
7028171530Smatthias.ringwald #endif
7128171530Smatthias.ringwald 
72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
7496a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer);
7596a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
767586ee35S[email protected] static int  hci_power_control_on(void);
777586ee35S[email protected] static void hci_power_control_off(void);
786155b3d3S[email protected] static void hci_state_reset();
79758b46ceSmatthias.ringwald 
8006b35ec0Smatthias.ringwald // the STACK is here
813a9fb326S[email protected] #ifndef HAVE_MALLOC
823a9fb326S[email protected] static hci_stack_t   hci_stack_static;
833a9fb326S[email protected] #endif
843a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8516833f0aSmatthias.ringwald 
8666fb9560S[email protected] // test helper
8766fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
8866fb9560S[email protected] 
8996a45072S[email protected] /**
9096a45072S[email protected]  * create connection for given address
9196a45072S[email protected]  *
9296a45072S[email protected]  * @return connection OR NULL, if no memory left
9396a45072S[email protected]  */
9496a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
9596a45072S[email protected] 
965127cc62S[email protected]     log_info("create_connection_for_addr %s", bd_addr_to_str(addr));
9796a45072S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
9896a45072S[email protected]     if (!conn) return NULL;
9996a45072S[email protected]     BD_ADDR_COPY(conn->address, addr);
10096a45072S[email protected]     conn->address_type = addr_type;
10196a45072S[email protected]     conn->con_handle = 0xffff;
10296a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
10396a45072S[email protected]     conn->bonding_flags = 0;
10496a45072S[email protected]     conn->requested_security_level = LEVEL_0;
10596a45072S[email protected]     linked_item_set_user(&conn->timeout.item, conn);
10696a45072S[email protected]     conn->timeout.process = hci_connection_timeout_handler;
10796a45072S[email protected]     hci_connection_timestamp(conn);
10896a45072S[email protected]     conn->acl_recombination_length = 0;
10996a45072S[email protected]     conn->acl_recombination_pos = 0;
11096a45072S[email protected]     conn->num_acl_packets_sent = 0;
11196a45072S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
11296a45072S[email protected]     return conn;
11396a45072S[email protected] }
11466fb9560S[email protected] 
11597addcc5Smatthias.ringwald /**
116ee091cf1Smatthias.ringwald  * get connection for a given handle
117ee091cf1Smatthias.ringwald  *
118ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
119ee091cf1Smatthias.ringwald  */
1205061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
121ee091cf1Smatthias.ringwald     linked_item_t *it;
1223a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
123ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
124ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
125ee091cf1Smatthias.ringwald         }
126ee091cf1Smatthias.ringwald     }
127ee091cf1Smatthias.ringwald     return NULL;
128ee091cf1Smatthias.ringwald }
129ee091cf1Smatthias.ringwald 
13096a45072S[email protected] /**
13196a45072S[email protected]  * get connection for given address
13296a45072S[email protected]  *
13396a45072S[email protected]  * @return connection OR NULL, if not found
13496a45072S[email protected]  */
13596a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){
13662bda3fbS[email protected]     linked_item_t *it;
13762bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
13862bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
13996a45072S[email protected]         if (connection->address_type != addr_type)  continue;
14096a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
14162bda3fbS[email protected]         return connection;
14262bda3fbS[email protected]     }
14362bda3fbS[email protected]     return NULL;
14462bda3fbS[email protected] }
14562bda3fbS[email protected] 
1462b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
14728ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
148c785ef68Smatthias.ringwald #ifdef HAVE_TIME
149ee091cf1Smatthias.ringwald     struct timeval tv;
150ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
151c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
152ee091cf1Smatthias.ringwald         // connections might be timed out
153ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
154ee091cf1Smatthias.ringwald     }
1552b12a0b9Smatthias.ringwald #endif
156e5780900Smatthias.ringwald #ifdef HAVE_TICK
157c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
158c785ef68Smatthias.ringwald         // connections might be timed out
159c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
160c785ef68Smatthias.ringwald     }
161c785ef68Smatthias.ringwald #endif
162c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
163c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
164c785ef68Smatthias.ringwald }
165ee091cf1Smatthias.ringwald 
166ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
167c7492964Smatthias.ringwald #ifdef HAVE_TIME
168ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
169c7492964Smatthias.ringwald #endif
170e5780900Smatthias.ringwald #ifdef HAVE_TICK
171c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
172c785ef68Smatthias.ringwald #endif
173ee091cf1Smatthias.ringwald }
174ee091cf1Smatthias.ringwald 
17506b35ec0Smatthias.ringwald 
17628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
17728ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
17828ca2b46S[email protected] }
17928ca2b46S[email protected] 
18028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
18228ca2b46S[email protected] }
18328ca2b46S[email protected] 
18428ca2b46S[email protected] 
18543bfb1bdSmatthias.ringwald /**
18680ca58a0Smatthias.ringwald  * add authentication flags and reset timer
18796a45072S[email protected]  * @note: assumes classic connection
1887fde4af9Smatthias.ringwald  */
1897fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1907fde4af9Smatthias.ringwald     bd_addr_t addr;
1917fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
19296a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1937fde4af9Smatthias.ringwald     if (conn) {
19428ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
19580ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1967fde4af9Smatthias.ringwald     }
1977fde4af9Smatthias.ringwald }
1987fde4af9Smatthias.ringwald 
19980ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2005061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
20180ca58a0Smatthias.ringwald     if (!conn) return 0;
2026724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2036724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2046724cd9eS[email protected]     return 0;
20580ca58a0Smatthias.ringwald }
20680ca58a0Smatthias.ringwald 
207c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2083a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2093a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
210c12e46e7Smatthias.ringwald     }
211c12e46e7Smatthias.ringwald }
212c12e46e7Smatthias.ringwald 
2130bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){
2140bf6344aS[email protected]     return  connection->address_type == BD_ADDR_TYPE_LE_PUBLIC ||
2150bf6344aS[email protected]     connection->address_type == BD_ADDR_TYPE_LE_RANDOM;
2160bf6344aS[email protected] }
2170bf6344aS[email protected] 
2187fde4af9Smatthias.ringwald 
2197fde4af9Smatthias.ringwald /**
22043bfb1bdSmatthias.ringwald  * count connections
22143bfb1bdSmatthias.ringwald  */
22240d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
22356c253c9Smatthias.ringwald     int count = 0;
22443bfb1bdSmatthias.ringwald     linked_item_t *it;
2253a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
22643bfb1bdSmatthias.ringwald     return count;
22743bfb1bdSmatthias.ringwald }
228c8e4258aSmatthias.ringwald 
22997addcc5Smatthias.ringwald /**
230ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
23116833f0aSmatthias.ringwald  */
2322718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
23316833f0aSmatthias.ringwald }
23416833f0aSmatthias.ringwald 
235998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2365061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
237998906cdSmatthias.ringwald     if (!connection) {
2387d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
239998906cdSmatthias.ringwald         return 0;
240998906cdSmatthias.ringwald     }
241998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
242998906cdSmatthias.ringwald }
243998906cdSmatthias.ringwald 
244998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
2453a9fb326S[email protected]     uint8_t free_slots = hci_stack->total_num_acl_packets;
246998906cdSmatthias.ringwald     linked_item_t *it;
2473a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
248998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
249998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2507d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
251998906cdSmatthias.ringwald             return 0;
252998906cdSmatthias.ringwald         }
253998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
254998906cdSmatthias.ringwald     }
255998906cdSmatthias.ringwald     return free_slots;
256998906cdSmatthias.ringwald }
257998906cdSmatthias.ringwald 
258c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2592b12a0b9Smatthias.ringwald 
2602b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2613a9fb326S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
2623a9fb326S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){
2632b12a0b9Smatthias.ringwald             return 0;
2642b12a0b9Smatthias.ringwald         }
2652b12a0b9Smatthias.ringwald     }
2662b12a0b9Smatthias.ringwald 
2672b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
268c24735b1Smatthias.ringwald     switch (packet_type) {
269c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
270c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
271c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
2723a9fb326S[email protected]             return hci_stack->num_cmd_packets;
273c24735b1Smatthias.ringwald         default:
274c24735b1Smatthias.ringwald             return 0;
275c24735b1Smatthias.ringwald     }
276c24735b1Smatthias.ringwald }
277c24735b1Smatthias.ringwald 
2786b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use
2796b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
2806b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2816b4af23dS[email protected]     return hci_can_send_packet_now(packet_type);
2826b4af23dS[email protected] }
2836b4af23dS[email protected] 
284c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
285c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
286c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
287c8b9416aS[email protected] }
288c8b9416aS[email protected] 
2896b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
2906b4af23dS[email protected] int hci_reserve_packet_buffer(void){
2919d14b626S[email protected]     if (hci_stack->hci_packet_buffer_reserved) {
2929d14b626S[email protected]         log_error("hci_reserve_packet_buffer called but buffer already reserved");
2939d14b626S[email protected]         return 0;
2949d14b626S[email protected]     }
2956b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
2966b4af23dS[email protected]     return 1;
2976b4af23dS[email protected] }
2986b4af23dS[email protected] 
29968a0fcf7S[email protected] void hci_release_packet_buffer(void){
30068a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
30168a0fcf7S[email protected] }
30268a0fcf7S[email protected] 
3036b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
3046b4af23dS[email protected] int hci_transport_synchronous(void){
3056b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
3066b4af23dS[email protected] }
3076b4af23dS[email protected] 
308826f7347S[email protected] // pre: caller has reserved the packet buffer
309826f7347S[email protected] int hci_send_acl_packet_buffer(int size){
3107856c818Smatthias.ringwald 
311826f7347S[email protected]     if (!hci_stack->hci_packet_buffer_reserved) {
312826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called without reserving packet buffer");
313826f7347S[email protected]         return 0;
314826f7347S[email protected]     }
315826f7347S[email protected] 
316826f7347S[email protected]     // check for free places on Bluetooth module
31797b61c7bS[email protected]     if (!hci_number_free_acl_slots()) {
318826f7347S[email protected]         log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller");
31997b61c7bS[email protected]         hci_release_packet_buffer();
32097b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
32197b61c7bS[email protected]     }
3226218e6f1Smatthias.ringwald 
323826f7347S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
3247856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3255061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
32697b61c7bS[email protected]     if (!connection) {
3275fa0b7cfS[email protected]         log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle);
32897b61c7bS[email protected]         hci_release_packet_buffer();
32997b61c7bS[email protected]         return 0;
33097b61c7bS[email protected]     }
33156cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
33256cf178bSmatthias.ringwald 
33356cf178bSmatthias.ringwald     // count packet
33456cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
3357b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
3367856c818Smatthias.ringwald 
33700d8e42eSmatthias.ringwald     // send packet
3383a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
3396218e6f1Smatthias.ringwald 
3406b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
341826f7347S[email protected]     if (hci_transport_synchronous()){
34297b61c7bS[email protected]         hci_release_packet_buffer();
3436b4af23dS[email protected]     }
3446b4af23dS[email protected] 
34500d8e42eSmatthias.ringwald     return err;
346ee091cf1Smatthias.ringwald }
347ee091cf1Smatthias.ringwald 
34816833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
3497856c818Smatthias.ringwald 
350e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
351e76a89eeS[email protected] 
3527856c818Smatthias.ringwald     // get info
3537856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3545061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
3557856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
3567856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
3577856c818Smatthias.ringwald 
3587856c818Smatthias.ringwald     // ignore non-registered handle
3597856c818Smatthias.ringwald     if (!conn){
3607d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3617856c818Smatthias.ringwald         return;
3627856c818Smatthias.ringwald     }
3637856c818Smatthias.ringwald 
364e76a89eeS[email protected]     // assert packet is complete
3659ecc3e17S[email protected]     if (acl_length + 4 != size){
366e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
367e76a89eeS[email protected]         return;
368e76a89eeS[email protected]     }
369e76a89eeS[email protected] 
3707856c818Smatthias.ringwald     // update idle timestamp
3717856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3727856c818Smatthias.ringwald 
3737856c818Smatthias.ringwald     // handle different packet types
3747856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3757856c818Smatthias.ringwald 
3767856c818Smatthias.ringwald         case 0x01: // continuation fragment
3777856c818Smatthias.ringwald 
3787856c818Smatthias.ringwald             // sanity check
3797856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3807d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3817856c818Smatthias.ringwald                 return;
3827856c818Smatthias.ringwald             }
3837856c818Smatthias.ringwald 
3847856c818Smatthias.ringwald             // append fragment payload (header already stored)
3857856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3867856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3877856c818Smatthias.ringwald 
3887d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
389decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3907856c818Smatthias.ringwald 
3917856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3927856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3937856c818Smatthias.ringwald 
3943a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3957856c818Smatthias.ringwald                 // reset recombination buffer
3967856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3977856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3987856c818Smatthias.ringwald             }
3997856c818Smatthias.ringwald             break;
4007856c818Smatthias.ringwald 
4017856c818Smatthias.ringwald         case 0x02: { // first fragment
4027856c818Smatthias.ringwald 
4037856c818Smatthias.ringwald             // sanity check
4047856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
4057d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
4067856c818Smatthias.ringwald                 return;
4077856c818Smatthias.ringwald             }
4087856c818Smatthias.ringwald 
4097856c818Smatthias.ringwald             // peek into L2CAP packet!
4107856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
4117856c818Smatthias.ringwald 
412e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
413decc01a8Smatthias.ringwald 
4147856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
4157856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
4167856c818Smatthias.ringwald 
4177856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
4183a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
4197856c818Smatthias.ringwald 
4207856c818Smatthias.ringwald             } else {
4217856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
4227856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
4237856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
4247856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
425decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
4267856c818Smatthias.ringwald             }
4277856c818Smatthias.ringwald             break;
4287856c818Smatthias.ringwald 
4297856c818Smatthias.ringwald         }
4307856c818Smatthias.ringwald         default:
4317d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
4327856c818Smatthias.ringwald             return;
4337856c818Smatthias.ringwald     }
43494ab26f8Smatthias.ringwald 
43594ab26f8Smatthias.ringwald     // execute main loop
43694ab26f8Smatthias.ringwald     hci_run();
43716833f0aSmatthias.ringwald }
43822909952Smatthias.ringwald 
43967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
4401f479f8cS[email protected]     log_info("Connection closed: handle 0x%x, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
4413c4d4b90Smatthias.ringwald 
442c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
443c785ef68Smatthias.ringwald 
4443a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
445a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
4463c4d4b90Smatthias.ringwald 
4473c4d4b90Smatthias.ringwald     // now it's gone
448c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
449c7e0c5f6Smatthias.ringwald }
450c7e0c5f6Smatthias.ringwald 
4510c042179S[email protected] static const uint16_t packet_type_sizes[] = {
4528f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
4538f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
4548f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4558f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4568f8108aaSmatthias.ringwald };
45765389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
45865389bfcS[email protected]      0, // 3 slot packets
45965389bfcS[email protected]      1, // 5 slot packets
46065389bfcS[email protected]     25, // EDR 2 mpbs
46165389bfcS[email protected]     26, // EDR 3 mbps
46265389bfcS[email protected]     39, // 3 slot EDR packts
46365389bfcS[email protected]     40, // 5 slot EDR packet
46465389bfcS[email protected] };
46565389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
46665389bfcS[email protected]     0x0f00, // 3 slot packets
46765389bfcS[email protected]     0xf000, // 5 slot packets
46865389bfcS[email protected]     0x1102, // EDR 2 mpbs
46965389bfcS[email protected]     0x2204, // EDR 3 mbps
47065389bfcS[email protected]     0x0300, // 3 slot EDR packts
47165389bfcS[email protected]     0x3000, // 5 slot EDR packet
47265389bfcS[email protected] };
4738f8108aaSmatthias.ringwald 
47465389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
47565389bfcS[email protected]     // enable packet types based on size
4768f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4778f8108aaSmatthias.ringwald     int i;
4788f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4798f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4808f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4818f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4828f8108aaSmatthias.ringwald         }
4838f8108aaSmatthias.ringwald     }
48465389bfcS[email protected]     // disable packet types due to missing local supported features
48565389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
48665389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
48765389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
48865389bfcS[email protected]         if (feature_set) continue;
48965389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
49065389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
49165389bfcS[email protected]     }
4928f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4938f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4948f8108aaSmatthias.ringwald     return packet_types;
4958f8108aaSmatthias.ringwald }
4968f8108aaSmatthias.ringwald 
4978f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4983a9fb326S[email protected]     return hci_stack->packet_types;
4998f8108aaSmatthias.ringwald }
5008f8108aaSmatthias.ringwald 
501facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
5027dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
5033a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
5047dc17943Smatthias.ringwald }
5057dc17943Smatthias.ringwald 
506f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
5073a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
5087dc17943Smatthias.ringwald }
5097dc17943Smatthias.ringwald 
5106ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
5116ac9a97eS[email protected]     // No. 54, byte 6, bit 6
5126ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
5136ac9a97eS[email protected] }
5146ac9a97eS[email protected] 
515f5d8d141S[email protected] int hci_ssp_supported(void){
5166ac9a97eS[email protected]     // No. 51, byte 6, bit 3
5173a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
518f5d8d141S[email protected] }
519f5d8d141S[email protected] 
520f5d8d141S[email protected] int hci_classic_supported(void){
5216ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
5223a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
523f5d8d141S[email protected] }
524f5d8d141S[email protected] 
525f5d8d141S[email protected] int hci_le_supported(void){
526f5d8d141S[email protected] #ifdef HAVE_BLE
5276ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
5283a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
529f5d8d141S[email protected] #else
530f5d8d141S[email protected]     return 0;
531f5d8d141S[email protected] #endif
532f5d8d141S[email protected] }
533f5d8d141S[email protected] 
53469a97523S[email protected] // get addr type and address used in advertisement packets
53518904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
5363a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
5373a9fb326S[email protected]     if (hci_stack->adv_addr_type){
5383a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
53969a97523S[email protected]     } else {
5403a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
54169a97523S[email protected]     }
54269a97523S[email protected] }
54369a97523S[email protected] 
544b2f949feS[email protected] #ifdef HAVE_BLE
545e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){
54657c9da5bS[email protected]     int num_reports = packet[3];
54757c9da5bS[email protected]     int i;
54857c9da5bS[email protected]     int total_data_length = 0;
54957c9da5bS[email protected]     int data_offset = 0;
55057c9da5bS[email protected] 
55157c9da5bS[email protected]     for (i=0; i<num_reports;i++){
55257c9da5bS[email protected]         total_data_length += packet[4+num_reports*8+i];
55357c9da5bS[email protected]     }
55457c9da5bS[email protected] 
5552e440c8aS[email protected]     log_info("num reports: %d, ", num_reports);
55657c9da5bS[email protected]     for (i=0; i<num_reports;i++){
55757c9da5bS[email protected]         int pos = 0;
55857c9da5bS[email protected]         uint8_t data_length = packet[4+num_reports*8+i];
5592b552b23S[email protected]         uint8_t event_size = 10 + data_length;
5602b552b23S[email protected]         uint8_t event[2 + event_size ];
5612b552b23S[email protected]         event[pos++] = GAP_LE_ADVERTISING_REPORT;
56257c9da5bS[email protected]         event[pos++] = event_size;
56357c9da5bS[email protected]         event[pos++] = packet[4+i]; // event_type;
56457c9da5bS[email protected]         event[pos++] = packet[4+num_reports+i]; // address_type;
565564fca32S[email protected]         memcpy(&event[pos], &packet[4+num_reports*2+i*6], 6); // bt address
56657c9da5bS[email protected]         pos += 6;
5672b552b23S[email protected]         event[pos++] = packet[4+num_reports*9+total_data_length + i];
5682e440c8aS[email protected]         log_info("rssi: %d, ", event[pos-1]);
56957c9da5bS[email protected]         event[pos++] = data_length;
57088ed79cfS[email protected]         memcpy(&event[pos], &packet[4+num_reports*9+data_offset], data_length);
57157c9da5bS[email protected]         data_offset += data_length;
57257c9da5bS[email protected]         pos += data_length;
57357c9da5bS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
57457c9da5bS[email protected]     }
57557c9da5bS[email protected] }
576b2f949feS[email protected] #endif
57757c9da5bS[email protected] 
5786155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
579a2481739S[email protected]     uint8_t command_completed = 0;
580a2481739S[email protected]     if ((hci_stack->substate % 2) == 0) return;
5816155b3d3S[email protected]     // odd: waiting for event
5826155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5836155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,3);
5846155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
585a2481739S[email protected]             command_completed = 1;
5866155b3d3S[email protected]             log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate);
5876155b3d3S[email protected]         } else {
5886155b3d3S[email protected]             log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
5896155b3d3S[email protected]         }
5906155b3d3S[email protected]     }
5916155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_STATUS){
5926155b3d3S[email protected]         uint8_t  status = packet[2];
5936155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,4);
5946155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
5956155b3d3S[email protected]             if (status){
596a2481739S[email protected]                 command_completed = 1;
5976155b3d3S[email protected]                 log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate);
5986155b3d3S[email protected]             } else {
5996155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
6006155b3d3S[email protected]             }
6016155b3d3S[email protected]         } else {
6026155b3d3S[email protected]             log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
6036155b3d3S[email protected]         }
6046155b3d3S[email protected]     }
605a2481739S[email protected] 
606a2481739S[email protected]     if (!command_completed) return;
607a2481739S[email protected] 
608a2481739S[email protected]     switch(hci_stack->substate >> 1){
609a2481739S[email protected]         default:
610a2481739S[email protected]             hci_stack->substate++;
611a2481739S[email protected]             break;
6126155b3d3S[email protected]     }
6136155b3d3S[email protected] }
6146155b3d3S[email protected] 
6156155b3d3S[email protected] static void hci_initializing_state_machine(){
6166155b3d3S[email protected]         // log_info("hci_init: substate %u\n", hci_stack->substate);
6176155b3d3S[email protected]     if (hci_stack->substate % 2) {
6186155b3d3S[email protected]         // odd: waiting for command completion
6196155b3d3S[email protected]         return;
6206155b3d3S[email protected]     }
6216155b3d3S[email protected]     switch (hci_stack->substate >> 1){
6226155b3d3S[email protected]         case 0: // RESET
6236155b3d3S[email protected]             hci_state_reset();
6246155b3d3S[email protected] 
6256155b3d3S[email protected]             hci_send_cmd(&hci_reset);
6266155b3d3S[email protected]             if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
6276155b3d3S[email protected]                 // skip baud change
6286155b3d3S[email protected]                 hci_stack->substate = 4; // >> 1 = 2
6296155b3d3S[email protected]             }
6306155b3d3S[email protected]             break;
6316155b3d3S[email protected]         case 1: // SEND BAUD CHANGE
6326155b3d3S[email protected]             hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
63388789c61Smatthias.ringwald             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
6346155b3d3S[email protected]             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
6356155b3d3S[email protected]             break;
6366155b3d3S[email protected]         case 2: // LOCAL BAUD CHANGE
6376155b3d3S[email protected]             log_info("Local baud rate change");
6386155b3d3S[email protected]             hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
6396155b3d3S[email protected]             hci_stack->substate += 2;
6406155b3d3S[email protected]             // break missing here for fall through
6416155b3d3S[email protected] 
6426155b3d3S[email protected]         case 3:
6436155b3d3S[email protected]             log_info("Custom init");
6446155b3d3S[email protected]             // Custom initialization
6456155b3d3S[email protected]             if (hci_stack->control && hci_stack->control->next_cmd){
6466155b3d3S[email protected]                 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
6476155b3d3S[email protected]                 if (valid_cmd){
6486155b3d3S[email protected]                     int size = 3 + hci_stack->hci_packet_buffer[2];
6496155b3d3S[email protected]                     hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
6506155b3d3S[email protected]                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
6516155b3d3S[email protected]                     hci_stack->substate = 4; // more init commands
6526155b3d3S[email protected]                     break;
6536155b3d3S[email protected]                 }
6546155b3d3S[email protected]                 log_info("hci_run: init script done\n\r");
6556155b3d3S[email protected]             }
6566155b3d3S[email protected]             // otherwise continue
6576155b3d3S[email protected]             hci_send_cmd(&hci_read_bd_addr);
6586155b3d3S[email protected]             break;
6596155b3d3S[email protected]         case 4:
6606155b3d3S[email protected]             hci_send_cmd(&hci_read_buffer_size);
6616155b3d3S[email protected]             break;
6626155b3d3S[email protected]         case 5:
6636155b3d3S[email protected]             hci_send_cmd(&hci_read_local_supported_features);
6646155b3d3S[email protected]             break;
6656155b3d3S[email protected]         case 6:
6666155b3d3S[email protected]             if (hci_le_supported()){
6676155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
6686155b3d3S[email protected]             } else {
6696155b3d3S[email protected]                 // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
6706155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
6716155b3d3S[email protected]             }
6726155b3d3S[email protected] 
6736155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
6746155b3d3S[email protected]             if (!hci_classic_supported()){
6756155b3d3S[email protected]                 if (hci_le_supported()){
6766155b3d3S[email protected]                     hci_stack->substate = 11 << 1;    // skip all classic command
6776155b3d3S[email protected]                 } else {
6786155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
6796155b3d3S[email protected]                     hci_stack->substate = 14 << 1;    // skip all
6806155b3d3S[email protected]                 }
6816155b3d3S[email protected]             }
6826155b3d3S[email protected]             break;
6836155b3d3S[email protected]         case 7:
6846155b3d3S[email protected]             if (hci_ssp_supported()){
6856155b3d3S[email protected]                 hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
6866155b3d3S[email protected]                 break;
6876155b3d3S[email protected]             }
6886155b3d3S[email protected]             hci_stack->substate += 2;
6896155b3d3S[email protected]             // break missing here for fall through
6906155b3d3S[email protected] 
6916155b3d3S[email protected]         case 8:
6926155b3d3S[email protected]             // ca. 15 sec
6936155b3d3S[email protected]             hci_send_cmd(&hci_write_page_timeout, 0x6000);
6946155b3d3S[email protected]             break;
6956155b3d3S[email protected]         case 9:
6966155b3d3S[email protected]             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
6976155b3d3S[email protected]             break;
6986155b3d3S[email protected]         case 10:
6996155b3d3S[email protected]             if (hci_stack->local_name){
7006155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
7016155b3d3S[email protected]             } else {
7026155b3d3S[email protected]                 char hostname[30];
7036155b3d3S[email protected] #ifdef EMBEDDED
7046155b3d3S[email protected]                 // BTstack-11:22:33:44:55:66
7056155b3d3S[email protected]                 strcpy(hostname, "BTstack ");
7066155b3d3S[email protected]                 strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
7076155b3d3S[email protected]                 log_info("---> Name %s", hostname);
7086155b3d3S[email protected] #else
7096155b3d3S[email protected]                 // hostname for POSIX systems
7106155b3d3S[email protected]                 gethostname(hostname, 30);
7116155b3d3S[email protected]                 hostname[29] = '\0';
7126155b3d3S[email protected] #endif
7136155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hostname);
7146155b3d3S[email protected]             }
7156155b3d3S[email protected]             break;
7166155b3d3S[email protected]         case 11:
7176155b3d3S[email protected]             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
7186155b3d3S[email protected]             if (!hci_le_supported()){
7196155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
7206155b3d3S[email protected]                 hci_stack->substate = 14 << 1;
7216155b3d3S[email protected]             }
7226155b3d3S[email protected]             break;
7236155b3d3S[email protected] 
7246155b3d3S[email protected] #ifdef HAVE_BLE
7256155b3d3S[email protected]         // LE INIT
7266155b3d3S[email protected]         case 12:
7276155b3d3S[email protected]             hci_send_cmd(&hci_le_read_buffer_size);
7286155b3d3S[email protected]             break;
7296155b3d3S[email protected]         case 13:
7306155b3d3S[email protected]             // LE Supported Host = 1, Simultaneous Host = 0
7316155b3d3S[email protected]             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
7326155b3d3S[email protected]             break;
7336155b3d3S[email protected]         case 14:
7346155b3d3S[email protected]             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
7356155b3d3S[email protected]             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
7366155b3d3S[email protected]             break;
7376155b3d3S[email protected] #endif
7386155b3d3S[email protected] 
7396155b3d3S[email protected]         // DONE
7406155b3d3S[email protected]         case 15:
7416155b3d3S[email protected]             // done.
7426155b3d3S[email protected]             hci_stack->state = HCI_STATE_WORKING;
7436155b3d3S[email protected]             hci_emit_state();
7446155b3d3S[email protected]             break;
7456155b3d3S[email protected]         default:
7466155b3d3S[email protected]             break;
7476155b3d3S[email protected]     }
7486155b3d3S[email protected]     hci_stack->substate++;
7496155b3d3S[email protected] }
7506155b3d3S[email protected] 
75174ec757aSmatthias.ringwald // avoid huge local variables
752223aafc1Smatthias.ringwald #ifndef EMBEDDED
75374ec757aSmatthias.ringwald static device_name_t device_name;
754223aafc1Smatthias.ringwald #endif
75516833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
756e76a89eeS[email protected] 
757e76a89eeS[email protected]     uint16_t event_length = packet[1];
758e76a89eeS[email protected] 
759e76a89eeS[email protected]     // assert packet is complete
760e76a89eeS[email protected]     if (size != event_length + 2){
761e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
762e76a89eeS[email protected]         return;
763e76a89eeS[email protected]     }
764e76a89eeS[email protected] 
7651281a47eSmatthias.ringwald     bd_addr_t addr;
76696a45072S[email protected]     bd_addr_type_t addr_type;
7672d00edd4Smatthias.ringwald     uint8_t link_type;
768fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
7691f7b95a1Smatthias.ringwald     hci_connection_t * conn;
77056cf178bSmatthias.ringwald     int i;
77122909952Smatthias.ringwald 
7725909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
7735909f7f2Smatthias.ringwald 
7746772a24cSmatthias.ringwald     switch (packet[0]) {
77522909952Smatthias.ringwald 
7766772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
7777ec5eeaaSmatthias.ringwald             // get num cmd packets
7783a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
7793a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
7807ec5eeaaSmatthias.ringwald 
781e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
782e2edc0c3Smatthias.ringwald                 // from offset 5
783e2edc0c3Smatthias.ringwald                 // status
7841d279b20Smatthias.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"
7853a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
78656cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
7873a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
78856cf178bSmatthias.ringwald                 // ignore: total num SCO packets
7893a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
790a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
7913a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
7923a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7938f8108aaSmatthias.ringwald                     }
79465389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
7953a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
796e2edc0c3Smatthias.ringwald                 }
79756cf178bSmatthias.ringwald             }
79865a46ef3S[email protected] #ifdef HAVE_BLE
79965a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
8003a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
8013a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
8023a9fb326S[email protected]                 log_info("hci_le_read_buffer_size: size %u, count %u\n", hci_stack->le_data_packet_length, hci_stack->total_num_le_packets);
80301f1657cS[email protected] 
80401f1657cS[email protected]                 // use LE buffers if no clasic buffers have been reported
80501f1657cS[email protected]                 if (hci_stack->total_num_acl_packets == 0){
80601f1657cS[email protected]                     log_info("use le buffers instead of classic ones");
80701f1657cS[email protected]                     hci_stack->total_num_acl_packets  = hci_stack->total_num_le_packets;
80801f1657cS[email protected]                     hci_stack->acl_data_packet_length = hci_stack->le_data_packet_length;
80901f1657cS[email protected]                     // determine usable ACL payload size
81001f1657cS[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
81101f1657cS[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
81201f1657cS[email protected]                     }
81301f1657cS[email protected]                 }
81465a46ef3S[email protected]             }
81565a46ef3S[email protected] #endif
816188981d2Smatthias.ringwald             // Dump local address
817188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
8183a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
819188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
8203a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
821188981d2Smatthias.ringwald             }
822381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
8233a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
824381fbed8Smatthias.ringwald             }
82565389bfcS[email protected]             // Note: HCI init checks
826559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
8273a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
828559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
8293a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
8303a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
8313a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
8323a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
83365389bfcS[email protected] 
83465389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
8353a9fb326S[email protected]                 hci_stack->packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(hci_stack->acl_data_packet_length, &hci_stack->local_supported_features[0]);
8363a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
837f5d8d141S[email protected] 
838f5d8d141S[email protected]                 // Classic/LE
839f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
840559e517eS[email protected]             }
84156cf178bSmatthias.ringwald             break;
84256cf178bSmatthias.ringwald 
8437ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
8447ec5eeaaSmatthias.ringwald             // get num cmd packets
8453a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
8463a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
8477ec5eeaaSmatthias.ringwald             break;
8487ec5eeaaSmatthias.ringwald 
8492e440c8aS[email protected]         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{
8502e440c8aS[email protected]             int offset = 3;
85156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
8522e440c8aS[email protected]                 handle = READ_BT_16(packet, offset);
8532e440c8aS[email protected]                 offset += 2;
8542e440c8aS[email protected]                 uint16_t num_packets = READ_BT_16(packet, offset);
8552e440c8aS[email protected]                 offset += 2;
8562e440c8aS[email protected] 
8575061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
85856cf178bSmatthias.ringwald                 if (!conn){
8597d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
86056cf178bSmatthias.ringwald                     continue;
86156cf178bSmatthias.ringwald                 }
86256cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
8637b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
86456cf178bSmatthias.ringwald             }
8656772a24cSmatthias.ringwald             break;
8662e440c8aS[email protected]         }
8671f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
86837eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
86937eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
8702d00edd4Smatthias.ringwald             link_type = packet[11];
871339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
87237eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
87396a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
8741f7b95a1Smatthias.ringwald                 if (!conn) {
87596a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
8761f7b95a1Smatthias.ringwald                 }
877ce4c8fabSmatthias.ringwald                 if (!conn) {
878ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
8793a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
8803a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
881ce4c8fabSmatthias.ringwald                     break;
882ce4c8fabSmatthias.ringwald                 }
88332ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
88432ab9390Smatthias.ringwald                 hci_run();
88537eaa4cfSmatthias.ringwald             } else {
886ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
8873a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
8883a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
88937eaa4cfSmatthias.ringwald             }
8901f7b95a1Smatthias.ringwald             break;
8911f7b95a1Smatthias.ringwald 
8926772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
893fe1ed1b8Smatthias.ringwald             // Connection management
894fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
895339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
89696a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
89796a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
898fe1ed1b8Smatthias.ringwald             if (conn) {
899b448a0e7Smatthias.ringwald                 if (!packet[2]){
900c8e4258aSmatthias.ringwald                     conn->state = OPEN;
901fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
902ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
903ee091cf1Smatthias.ringwald 
904c785ef68Smatthias.ringwald                     // restart timer
905c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
906ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
907c785ef68Smatthias.ringwald 
908339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
90943bfb1bdSmatthias.ringwald 
91043bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
911b448a0e7Smatthias.ringwald                 } else {
912ad83dc6aS[email protected]                     // notify client if dedicated bonding
913ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
914ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
915ad83dc6aS[email protected]                     }
916ad83dc6aS[email protected] 
917b448a0e7Smatthias.ringwald                     // connection failed, remove entry
9183a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
919a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
920c12e46e7Smatthias.ringwald 
921c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
922c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
923c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
924c12e46e7Smatthias.ringwald                     }
925fe1ed1b8Smatthias.ringwald                 }
926fe1ed1b8Smatthias.ringwald             }
9276772a24cSmatthias.ringwald             break;
928fe1ed1b8Smatthias.ringwald 
929afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
930afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
931afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
932afd4e962S[email protected]             if (!conn) break;
933afd4e962S[email protected]             if (!packet[2]){
934afd4e962S[email protected]                 uint8_t * features = &packet[5];
935afd4e962S[email protected]                 if (features[6] & (1 << 3)){
936afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
937afd4e962S[email protected]                 }
938afd4e962S[email protected]             }
939afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
940ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
941ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
942ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
943ad83dc6aS[email protected]             }
944afd4e962S[email protected]             break;
945afd4e962S[email protected] 
9467fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
9477b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
9487fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
94974d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
9503a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
95132ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
95264472d52Smatthias.ringwald             hci_run();
953d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
95429d53098Smatthias.ringwald             return;
9557fde4af9Smatthias.ringwald 
9569ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
95729d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
95896a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
9599ab95c90S[email protected]             if (!conn) break;
9609ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
9617bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
9629ab95c90S[email protected]             // Change Connection Encryption keeps link key type
9639ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
9649ab95c90S[email protected]                 conn->link_key_type = link_key_type;
9659ab95c90S[email protected]             }
9663a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
9673a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
96829d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
9697fde4af9Smatthias.ringwald             break;
9709ab95c90S[email protected]         }
9717fde4af9Smatthias.ringwald 
9727fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
9736724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
9744c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
9753a9fb326S[email protected]             if (!hci_stack->bondable){
976899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
977f8fb5f6eS[email protected]                 hci_run();
978f8fb5f6eS[email protected]                 return;
9794c57c146S[email protected]             }
980d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
9813a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
982d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
9833a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
9847fde4af9Smatthias.ringwald             break;
9857fde4af9Smatthias.ringwald 
9861d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
9871d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
988dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
989dbe1a790S[email protected]             break;
990dbe1a790S[email protected] 
991dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
9926724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
9933a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
994dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
995dbe1a790S[email protected]             break;
996dbe1a790S[email protected] 
997dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
9986724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
9993a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
1000dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
10011d6b20aeS[email protected]             break;
10021d6b20aeS[email protected] 
1003f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
1004f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
1005f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
1006f0944df2S[email protected]             if (!conn) break;
1007ad83dc6aS[email protected]             if (packet[2] == 0) {
1008f0944df2S[email protected]                 if (packet[5]){
1009f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
1010f0944df2S[email protected]                 } else {
1011536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
1012f0944df2S[email protected]                 }
1013ad83dc6aS[email protected]             }
1014a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
1015f0944df2S[email protected]             break;
1016f0944df2S[email protected] 
10171eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
10181eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
10191eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
10201eb2563eS[email protected]             if (!conn) break;
1021ad83dc6aS[email protected] 
1022ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1023ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1024ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1025ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
1026ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
1027ad83dc6aS[email protected]                 break;
1028ad83dc6aS[email protected]             }
1029ad83dc6aS[email protected] 
1030b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
10311eb2563eS[email protected]                 // link key sufficient for requested security
10321eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1033ad83dc6aS[email protected]                 break;
1034ad83dc6aS[email protected]             }
10351eb2563eS[email protected]             // not enough
10361eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
10371eb2563eS[email protected]             break;
103834d2123cS[email protected] 
1039223aafc1Smatthias.ringwald #ifndef EMBEDDED
104074ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
10413a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
104274ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
104374ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10445a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
10455a06394aSmatthias.ringwald             for (i=0; i<248;i++){
10465a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
10475a06394aSmatthias.ringwald                     packet[9+i] = 0;
10485a06394aSmatthias.ringwald                     break;
1049cdc9101dSmatthias.ringwald                 }
10505a06394aSmatthias.ringwald             }
1051d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
105274ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
10533a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
105474ec757aSmatthias.ringwald             break;
105574ec757aSmatthias.ringwald 
105674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
1057*206a9031S[email protected]         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
10583a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
105974ec757aSmatthias.ringwald             // first send inq result packet
10603a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
106174ec757aSmatthias.ringwald             // then send cached remote names
1062*206a9031S[email protected]             int offset = 3;
106374ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
1064*206a9031S[email protected]                 bt_flip_addr(addr, &packet[offset]);
1065*206a9031S[email protected]                 offset += 14; // 6 + 1 + 1 + 1 + 3 + 2;
10663a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
106774ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
106874ec757aSmatthias.ringwald                 }
106974ec757aSmatthias.ringwald             }
107074ec757aSmatthias.ringwald             return;
1071*206a9031S[email protected]         }
1072223aafc1Smatthias.ringwald #endif
107374ec757aSmatthias.ringwald 
10746772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1075fe1ed1b8Smatthias.ringwald             if (!packet[2]){
1076fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
10775061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
1078fe1ed1b8Smatthias.ringwald                 if (conn) {
1079c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
1080fe1ed1b8Smatthias.ringwald                 }
1081fe1ed1b8Smatthias.ringwald             }
10826772a24cSmatthias.ringwald             break;
10836772a24cSmatthias.ringwald 
1084c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
10853a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
10863a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
10877586ee35S[email protected]             } else {
10887586ee35S[email protected]                 // if no special requests, just reboot stack
10897586ee35S[email protected]                 hci_power_control_off();
10907586ee35S[email protected]                 hci_power_control_on();
1091c68bdf90Smatthias.ringwald             }
1092c68bdf90Smatthias.ringwald             break;
1093c68bdf90Smatthias.ringwald 
10946b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
10956b4af23dS[email protected]             // free packet buffer for asynchronous transport
10966b4af23dS[email protected]             if (hci_transport_synchronous()) break;
10976b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
10986b4af23dS[email protected]             break;
10996b4af23dS[email protected] 
11005909f7f2Smatthias.ringwald #ifdef HAVE_BLE
11015909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
11025909f7f2Smatthias.ringwald             switch (packet[2]){
110357c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
11042e440c8aS[email protected]                     log_info("advertising report received\n");
11057bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
110657c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
11077bdc6798S[email protected]                     break;
11085909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
11095909f7f2Smatthias.ringwald                     // Connection management
11105909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
11117bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
111296a45072S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr));
11135909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
111496a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
11155909f7f2Smatthias.ringwald                     if (packet[3]){
11165909f7f2Smatthias.ringwald                         if (conn){
11175909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
11183a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
11195909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
11205909f7f2Smatthias.ringwald                         }
11215909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
11225909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
11235909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
11245909f7f2Smatthias.ringwald                         }
11255909f7f2Smatthias.ringwald                         break;
11265909f7f2Smatthias.ringwald                     }
11275909f7f2Smatthias.ringwald                     if (!conn){
112896a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
11295909f7f2Smatthias.ringwald                     }
11305909f7f2Smatthias.ringwald                     if (!conn){
11315909f7f2Smatthias.ringwald                         // no memory
11325909f7f2Smatthias.ringwald                         break;
11335909f7f2Smatthias.ringwald                     }
11345909f7f2Smatthias.ringwald 
11355909f7f2Smatthias.ringwald                     conn->state = OPEN;
11365909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
11375909f7f2Smatthias.ringwald 
11385909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
11395909f7f2Smatthias.ringwald 
11405909f7f2Smatthias.ringwald                     // restart timer
11415909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
11425909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
11435909f7f2Smatthias.ringwald 
11445909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
11455909f7f2Smatthias.ringwald 
11465909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
11475909f7f2Smatthias.ringwald                     break;
11485909f7f2Smatthias.ringwald 
114965a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
115065a46ef3S[email protected] 
11515909f7f2Smatthias.ringwald                 default:
11525909f7f2Smatthias.ringwald                     break;
11535909f7f2Smatthias.ringwald             }
11545909f7f2Smatthias.ringwald             break;
11555909f7f2Smatthias.ringwald #endif
11566772a24cSmatthias.ringwald         default:
11576772a24cSmatthias.ringwald             break;
1158fe1ed1b8Smatthias.ringwald     }
1159fe1ed1b8Smatthias.ringwald 
11603429f56bSmatthias.ringwald     // handle BT initialization
11613a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
11626155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1163c9af4d3fS[email protected]     }
116422909952Smatthias.ringwald 
116589db417bSmatthias.ringwald     // help with BT sleep
11663a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
11673a9fb326S[email protected]         && hci_stack->substate == 1
116889db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
11693a9fb326S[email protected]         hci_stack->substate++;
117089db417bSmatthias.ringwald     }
117189db417bSmatthias.ringwald 
11723a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
117394ab26f8Smatthias.ringwald 
117494ab26f8Smatthias.ringwald 	// execute main loop
117594ab26f8Smatthias.ringwald 	hci_run();
117616833f0aSmatthias.ringwald }
117716833f0aSmatthias.ringwald 
11780a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
117910e830c9Smatthias.ringwald     switch (packet_type) {
118010e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
118110e830c9Smatthias.ringwald             event_handler(packet, size);
118210e830c9Smatthias.ringwald             break;
118310e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
118410e830c9Smatthias.ringwald             acl_handler(packet, size);
118510e830c9Smatthias.ringwald             break;
118610e830c9Smatthias.ringwald         default:
118710e830c9Smatthias.ringwald             break;
118810e830c9Smatthias.ringwald     }
118910e830c9Smatthias.ringwald }
119010e830c9Smatthias.ringwald 
1191fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
11922718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
11933a9fb326S[email protected]     hci_stack->packet_handler = handler;
119416833f0aSmatthias.ringwald }
119516833f0aSmatthias.ringwald 
11966155b3d3S[email protected] static void hci_state_reset(){
1197595bdbfbS[email protected]     // no connections yet
1198595bdbfbS[email protected]     hci_stack->connections = NULL;
119974308b23Smatthias.ringwald 
120074308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
120174308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
120274308b23Smatthias.ringwald     // hci_stack->connectable = 0;
120374308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1204595bdbfbS[email protected] 
120544935e40S[email protected]     // buffer is free
120644935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
120744935e40S[email protected] 
1208595bdbfbS[email protected]     // no pending cmds
1209595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1210595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1211595bdbfbS[email protected] 
1212595bdbfbS[email protected]     // LE
1213595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1214595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1215595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1216e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1217595bdbfbS[email protected] }
1218595bdbfbS[email protected] 
12194f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1220475c8125Smatthias.ringwald 
12213a9fb326S[email protected] #ifdef HAVE_MALLOC
12223a9fb326S[email protected]     if (!hci_stack) {
12233a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
12243a9fb326S[email protected]     }
12253a9fb326S[email protected] #else
12263a9fb326S[email protected]     hci_stack = &hci_stack_static;
12273a9fb326S[email protected] #endif
122866fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
12293a9fb326S[email protected] 
1230475c8125Smatthias.ringwald     // reference to use transport layer implementation
12313a9fb326S[email protected]     hci_stack->hci_transport = transport;
1232475c8125Smatthias.ringwald 
123311e23e5fSmatthias.ringwald     // references to used control implementation
12343a9fb326S[email protected]     hci_stack->control = control;
123511e23e5fSmatthias.ringwald 
123611e23e5fSmatthias.ringwald     // reference to used config
12373a9fb326S[email protected]     hci_stack->config = config;
123811e23e5fSmatthias.ringwald 
123916833f0aSmatthias.ringwald     // higher level handler
12403a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
124116833f0aSmatthias.ringwald 
1242404843c1Smatthias.ringwald     // store and open remote device db
12433a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
12443a9fb326S[email protected]     if (hci_stack->remote_device_db) {
12453a9fb326S[email protected]         hci_stack->remote_device_db->open();
1246404843c1Smatthias.ringwald     }
124729d53098Smatthias.ringwald 
12488fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
12493a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
12508fcba05dSmatthias.ringwald 
125116833f0aSmatthias.ringwald     // register packet handlers with transport
125210e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1253f5454fc6Smatthias.ringwald 
12543a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1255e2386ba1S[email protected] 
1256e2386ba1S[email protected]     // class of device
12573a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1258a45d6b9fS[email protected] 
1259f20168b8Smatthias.ringwald     // bondable by default
1260f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1261f20168b8Smatthias.ringwald 
126263048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
12633a9fb326S[email protected]     hci_stack->ssp_enable = 1;
12643a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
12653a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
12663a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
126769a97523S[email protected] 
1268595bdbfbS[email protected]     hci_state_reset();
1269475c8125Smatthias.ringwald }
1270475c8125Smatthias.ringwald 
1271404843c1Smatthias.ringwald void hci_close(){
1272404843c1Smatthias.ringwald     // close remote device db
12733a9fb326S[email protected]     if (hci_stack->remote_device_db) {
12743a9fb326S[email protected]         hci_stack->remote_device_db->close();
1275404843c1Smatthias.ringwald     }
12763a9fb326S[email protected]     while (hci_stack->connections) {
1277bc68afe2S[email protected]         // cancel all l2cap connections
1278bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
12793a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1280f5454fc6Smatthias.ringwald     }
1281f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
12823a9fb326S[email protected] 
12833a9fb326S[email protected] #ifdef HAVE_MALLOC
12843a9fb326S[email protected]     free(hci_stack);
12853a9fb326S[email protected] #endif
12863a9fb326S[email protected]     hci_stack = NULL;
1287404843c1Smatthias.ringwald }
1288404843c1Smatthias.ringwald 
12899e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
12909e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
12919e61646fS[email protected] }
12929e61646fS[email protected] 
129366fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
129466fb9560S[email protected]     disable_l2cap_timeouts = 1;
129566fb9560S[email protected] }
12968d213e1aSmatthias.ringwald // State-Module-Driver overview
12978d213e1aSmatthias.ringwald // state                    module  low-level
12988d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
12998d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
13008d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
13018d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1302d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1303d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1304c7e0c5f6Smatthias.ringwald 
130540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
13067301ad89Smatthias.ringwald 
1307038bc64cSmatthias.ringwald     // power on
1308f9a30166Smatthias.ringwald     int err = 0;
13093a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
13103a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1311f9a30166Smatthias.ringwald     }
1312038bc64cSmatthias.ringwald     if (err){
13137d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1314038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1315038bc64cSmatthias.ringwald         return err;
1316038bc64cSmatthias.ringwald     }
1317038bc64cSmatthias.ringwald 
1318038bc64cSmatthias.ringwald     // open low-level device
13193a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1320038bc64cSmatthias.ringwald     if (err){
13217d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
13223a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
13233a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1324f9a30166Smatthias.ringwald         }
1325038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1326038bc64cSmatthias.ringwald         return err;
1327038bc64cSmatthias.ringwald     }
13288d213e1aSmatthias.ringwald     return 0;
13298d213e1aSmatthias.ringwald }
1330038bc64cSmatthias.ringwald 
133140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
13328d213e1aSmatthias.ringwald 
13337b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
13349418f9c9Smatthias.ringwald 
13358d213e1aSmatthias.ringwald     // close low-level device
13363a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
13378d213e1aSmatthias.ringwald 
13387b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
13399418f9c9Smatthias.ringwald 
13408d213e1aSmatthias.ringwald     // power off
13413a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
13423a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
13438d213e1aSmatthias.ringwald     }
13449418f9c9Smatthias.ringwald 
13457b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
13469418f9c9Smatthias.ringwald 
13473a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
134872ea5239Smatthias.ringwald }
134972ea5239Smatthias.ringwald 
135040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
135172ea5239Smatthias.ringwald 
13527b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
13533144bce4Smatthias.ringwald 
1354b429b9b7Smatthias.ringwald #if 0
1355b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1356b429b9b7Smatthias.ringwald 
135772ea5239Smatthias.ringwald     // close low-level device
13583a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1359b429b9b7Smatthias.ringwald #endif
136072ea5239Smatthias.ringwald 
136172ea5239Smatthias.ringwald     // sleep mode
13623a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
13633a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
136472ea5239Smatthias.ringwald     }
1365b429b9b7Smatthias.ringwald 
13663a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
13678d213e1aSmatthias.ringwald }
13688d213e1aSmatthias.ringwald 
136940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1370b429b9b7Smatthias.ringwald 
13717b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1372b429b9b7Smatthias.ringwald 
1373b429b9b7Smatthias.ringwald     // wake on
13743a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
13753a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1376b429b9b7Smatthias.ringwald     }
1377b429b9b7Smatthias.ringwald 
1378b429b9b7Smatthias.ringwald #if 0
1379b429b9b7Smatthias.ringwald     // open low-level device
13803a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1381b429b9b7Smatthias.ringwald     if (err){
13827d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
13833a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
13843a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1385b429b9b7Smatthias.ringwald         }
1386b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1387b429b9b7Smatthias.ringwald         return err;
1388b429b9b7Smatthias.ringwald     }
1389b429b9b7Smatthias.ringwald #endif
1390b429b9b7Smatthias.ringwald 
1391b429b9b7Smatthias.ringwald     return 0;
1392b429b9b7Smatthias.ringwald }
1393b429b9b7Smatthias.ringwald 
139444935e40S[email protected] static void hci_power_transition_to_initializing(void){
139544935e40S[email protected]     // set up state machine
139644935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
139744935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
139844935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
139944935e40S[email protected]     hci_stack->substate = 0;
140044935e40S[email protected] }
1401b429b9b7Smatthias.ringwald 
14028d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1403d661ed19Smatthias.ringwald 
14043a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1405d661ed19Smatthias.ringwald 
14068d213e1aSmatthias.ringwald     int err = 0;
14073a9fb326S[email protected]     switch (hci_stack->state){
14088d213e1aSmatthias.ringwald 
14098d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
14108d213e1aSmatthias.ringwald             switch (power_mode){
14118d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14128d213e1aSmatthias.ringwald                     err = hci_power_control_on();
141397b61c7bS[email protected]                     if (err) {
141497b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
141597b61c7bS[email protected]                         return err;
141697b61c7bS[email protected]                     }
141744935e40S[email protected]                     hci_power_transition_to_initializing();
14188d213e1aSmatthias.ringwald                     break;
14198d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14208d213e1aSmatthias.ringwald                     // do nothing
14218d213e1aSmatthias.ringwald                     break;
14228d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1423b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
14248d213e1aSmatthias.ringwald                     break;
14258d213e1aSmatthias.ringwald             }
14268d213e1aSmatthias.ringwald             break;
14277301ad89Smatthias.ringwald 
14288d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
14298d213e1aSmatthias.ringwald             switch (power_mode){
14308d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14318d213e1aSmatthias.ringwald                     // do nothing
14328d213e1aSmatthias.ringwald                     break;
14338d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14348d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
14358d213e1aSmatthias.ringwald                     hci_power_control_off();
14368d213e1aSmatthias.ringwald                     break;
14378d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1438b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
143972ea5239Smatthias.ringwald                     hci_power_control_sleep();
14408d213e1aSmatthias.ringwald                     break;
14418d213e1aSmatthias.ringwald             }
14428d213e1aSmatthias.ringwald             break;
14437301ad89Smatthias.ringwald 
14448d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
14458d213e1aSmatthias.ringwald             switch (power_mode){
14468d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14478d213e1aSmatthias.ringwald                     // do nothing
14488d213e1aSmatthias.ringwald                     break;
14498d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1450c7e0c5f6Smatthias.ringwald                     // see hci_run
14513a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14528d213e1aSmatthias.ringwald                     break;
14538d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1454b546ac54Smatthias.ringwald                     // see hci_run
14553a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
14563a9fb326S[email protected]                     hci_stack->substate = 0;
14578d213e1aSmatthias.ringwald                     break;
14588d213e1aSmatthias.ringwald             }
14598d213e1aSmatthias.ringwald             break;
14607301ad89Smatthias.ringwald 
14618d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
14628d213e1aSmatthias.ringwald             switch (power_mode){
14638d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
146444935e40S[email protected]                     hci_power_transition_to_initializing();
14658d213e1aSmatthias.ringwald                     break;
14668d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14678d213e1aSmatthias.ringwald                     // do nothing
14688d213e1aSmatthias.ringwald                     break;
14698d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1470b546ac54Smatthias.ringwald                     // see hci_run
14713a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
14723a9fb326S[email protected]                     hci_stack->substate = 0;
14738d213e1aSmatthias.ringwald                     break;
14748d213e1aSmatthias.ringwald             }
14758d213e1aSmatthias.ringwald             break;
14768d213e1aSmatthias.ringwald 
14778d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
14788d213e1aSmatthias.ringwald             switch (power_mode){
14798d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
148028171530Smatthias.ringwald 
148128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
148228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
148328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
14843a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
14853a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
148628171530Smatthias.ringwald                         break;
148728171530Smatthias.ringwald                     }
148828171530Smatthias.ringwald #endif
148944935e40S[email protected]                     hci_power_transition_to_initializing();
14908d213e1aSmatthias.ringwald                     break;
14918d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1492b546ac54Smatthias.ringwald                     // see hci_run
14933a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14948d213e1aSmatthias.ringwald                     break;
14958d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1496b546ac54Smatthias.ringwald                     // do nothing
14978d213e1aSmatthias.ringwald                     break;
14988d213e1aSmatthias.ringwald             }
14998d213e1aSmatthias.ringwald             break;
15008d213e1aSmatthias.ringwald 
15018d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
15028d213e1aSmatthias.ringwald             switch (power_mode){
15038d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
150428171530Smatthias.ringwald 
150528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
150628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
150728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
15083a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
15093a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1510758b46ceSmatthias.ringwald                         hci_update_scan_enable();
151128171530Smatthias.ringwald                         break;
151228171530Smatthias.ringwald                     }
151328171530Smatthias.ringwald #endif
15143144bce4Smatthias.ringwald                     err = hci_power_control_wake();
15153144bce4Smatthias.ringwald                     if (err) return err;
151644935e40S[email protected]                     hci_power_transition_to_initializing();
15178d213e1aSmatthias.ringwald                     break;
15188d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
15193a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
15208d213e1aSmatthias.ringwald                     break;
15218d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1522b546ac54Smatthias.ringwald                     // do nothing
15238d213e1aSmatthias.ringwald                     break;
15248d213e1aSmatthias.ringwald             }
15258d213e1aSmatthias.ringwald             break;
152611e23e5fSmatthias.ringwald     }
152768d92d03Smatthias.ringwald 
1528038bc64cSmatthias.ringwald     // create internal event
1529ee8bf225Smatthias.ringwald 	hci_emit_state();
1530ee8bf225Smatthias.ringwald 
153168d92d03Smatthias.ringwald 	// trigger next/first action
153268d92d03Smatthias.ringwald 	hci_run();
153368d92d03Smatthias.ringwald 
1534475c8125Smatthias.ringwald     return 0;
1535475c8125Smatthias.ringwald }
1536475c8125Smatthias.ringwald 
1537758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1538758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
15393a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1540758b46ceSmatthias.ringwald     hci_run();
1541758b46ceSmatthias.ringwald }
1542758b46ceSmatthias.ringwald 
1543381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1544381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1545381fbed8Smatthias.ringwald 
15463a9fb326S[email protected]     if (hci_stack->discoverable == enable){
15473a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1548381fbed8Smatthias.ringwald         return;
1549381fbed8Smatthias.ringwald     }
1550381fbed8Smatthias.ringwald 
15513a9fb326S[email protected]     hci_stack->discoverable = enable;
1552758b46ceSmatthias.ringwald     hci_update_scan_enable();
1553758b46ceSmatthias.ringwald }
1554b031bebbSmatthias.ringwald 
1555758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1556758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1557758b46ceSmatthias.ringwald 
1558758b46ceSmatthias.ringwald     // don't emit event
15593a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1560758b46ceSmatthias.ringwald 
15613a9fb326S[email protected]     hci_stack->connectable = enable;
1562758b46ceSmatthias.ringwald     hci_update_scan_enable();
1563381fbed8Smatthias.ringwald }
1564381fbed8Smatthias.ringwald 
15655061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
15663a9fb326S[email protected]     return &hci_stack->local_bd_addr;
15675061f3afS[email protected] }
15685061f3afS[email protected] 
156906b35ec0Smatthias.ringwald void hci_run(){
15708a485f27Smatthias.ringwald 
157132ab9390Smatthias.ringwald     hci_connection_t * connection;
157232ab9390Smatthias.ringwald     linked_item_t * it;
157332ab9390Smatthias.ringwald 
157464f0b431S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
1575ce4c8fabSmatthias.ringwald 
1576b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1577b031bebbSmatthias.ringwald 
1578b031bebbSmatthias.ringwald     // decline incoming connections
15793a9fb326S[email protected]     if (hci_stack->decline_reason){
15803a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
15813a9fb326S[email protected]         hci_stack->decline_reason = 0;
15823a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1583dbe1a790S[email protected]         return;
1584ce4c8fabSmatthias.ringwald     }
1585ce4c8fabSmatthias.ringwald 
1586b031bebbSmatthias.ringwald     // send scan enable
15873a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
15883a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
15893a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1590dbe1a790S[email protected]         return;
1591b031bebbSmatthias.ringwald     }
1592b031bebbSmatthias.ringwald 
1593b2f949feS[email protected] #ifdef HAVE_BLE
15947bdc6798S[email protected]     // handle le scan
15957bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
15967bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
15977bdc6798S[email protected]             case LE_START_SCAN:
15987bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
15997bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
16007bdc6798S[email protected]                 return;
16017bdc6798S[email protected] 
16027bdc6798S[email protected]             case LE_STOP_SCAN:
16037bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
16047bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
16057bdc6798S[email protected]                 return;
16067bdc6798S[email protected]             default:
16077bdc6798S[email protected]                 break;
16087bdc6798S[email protected]         }
1609e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
1610e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
1611e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
1612e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
1613e2602ea2Smatthias.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);
1614e2602ea2Smatthias.ringwald             return;
1615e2602ea2Smatthias.ringwald         }
16167bdc6798S[email protected]     }
1617b2f949feS[email protected] #endif
16187bdc6798S[email protected] 
161932ab9390Smatthias.ringwald     // send pending HCI commands
16203a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
1621b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
162232ab9390Smatthias.ringwald 
16230bf6344aS[email protected]         switch(connection->state){
16240bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
16254f3229d8S[email protected]                 switch(connection->address_type){
16264f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
1627564fca32S[email protected]                         log_info("sending hci_create_connection\n");
1628ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
16294f3229d8S[email protected]                         break;
16304f3229d8S[email protected]                     default:
1631b2f949feS[email protected] #ifdef HAVE_BLE
1632564fca32S[email protected]                         log_info("sending hci_le_create_connection\n");
16334f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
1634b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
1635b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
16364f3229d8S[email protected]                                      0,         // don't use whitelist
16374f3229d8S[email protected]                                      connection->address_type, // peer address type
16384f3229d8S[email protected]                                      connection->address,      // peer bd addr
1639b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
1640b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
1641b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
16424f3229d8S[email protected]                                      0,         // conn latency
1643b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
1644b2571179Smatthias.ringwald                                      0x0001,    // min ce length
1645b2571179Smatthias.ringwald                                      0x0001     // max ce length
16464f3229d8S[email protected]                                      );
16474f3229d8S[email protected] 
16484f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
1649b2f949feS[email protected] #endif
16504f3229d8S[email protected]                         break;
16514f3229d8S[email protected]                 }
1652ad83dc6aS[email protected]                 return;
1653ad83dc6aS[email protected] 
16540bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
16557b5fbe1fSmatthias.ringwald                 log_info("sending hci_accept_connection_request\n");
165632ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
165734d2123cS[email protected]                 hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1658dbe1a790S[email protected]                 return;
16590bf6344aS[email protected] 
1660a6725849S[email protected] #ifdef HAVE_BLE
16610bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
16620bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
16630bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
16640bf6344aS[email protected]                 return;
1665a6725849S[email protected] #endif
16660bf6344aS[email protected]             case SEND_DISCONNECT:
16670bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
16687851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
16690bf6344aS[email protected]                 return;
16700bf6344aS[email protected] 
16710bf6344aS[email protected]             default:
16720bf6344aS[email protected]                 break;
1673c7e0c5f6Smatthias.ringwald         }
1674c7e0c5f6Smatthias.ringwald 
167532ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
167634d2123cS[email protected]             log_info("responding to link key request\n");
167734d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
167832ab9390Smatthias.ringwald             link_key_t link_key;
1679c77e8838S[email protected]             link_key_type_t link_key_type;
16803a9fb326S[email protected]             if ( hci_stack->remote_device_db
16813a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
168234d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
16839ab95c90S[email protected]                connection->link_key_type = link_key_type;
168432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
168532ab9390Smatthias.ringwald             } else {
168632ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
168732ab9390Smatthias.ringwald             }
1688dbe1a790S[email protected]             return;
168932ab9390Smatthias.ringwald         }
16901d6b20aeS[email protected] 
1691899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
16924c57c146S[email protected]             log_info("denying to pin request\n");
1693899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
169434d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
16954c57c146S[email protected]             return;
16964c57c146S[email protected]         }
16974c57c146S[email protected] 
1698dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
169934d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
170082d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
170182d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1702106d6d11S[email protected]                 // tweak authentication requirements
17033a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1704106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
17059faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
17069faad3abS[email protected]                 }
17079faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
17089faad3abS[email protected]                     authreq |= 1;
1709106d6d11S[email protected]                 }
17103a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1711f8fb5f6eS[email protected]             } else {
1712f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1713f8fb5f6eS[email protected]             }
1714dbe1a790S[email protected]             return;
171532ab9390Smatthias.ringwald         }
171632ab9390Smatthias.ringwald 
1717dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1718dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
171934d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1720dbe1a790S[email protected]             return;
1721dbe1a790S[email protected]         }
1722dbe1a790S[email protected] 
1723dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1724dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
172534d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1726dbe1a790S[email protected]             return;
1727dbe1a790S[email protected]         }
1728afd4e962S[email protected] 
1729afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1730afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
173134d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
17322bd8b7e7S[email protected]             return;
17332bd8b7e7S[email protected]         }
17342bd8b7e7S[email protected] 
17352bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
17362bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
173734d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
173834d2123cS[email protected]             return;
173934d2123cS[email protected]         }
1740ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1741ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
174252d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1743ad83dc6aS[email protected]             return;
1744ad83dc6aS[email protected]         }
174534d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
174634d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
174734d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
17482bd8b7e7S[email protected]             return;
1749afd4e962S[email protected]         }
1750dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1751dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1752dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1753dce78009S[email protected]             return;
1754dce78009S[email protected]         }
1755dbe1a790S[email protected]     }
1756c7e0c5f6Smatthias.ringwald 
17573a9fb326S[email protected]     switch (hci_stack->state){
17583429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
1759bd88fde9S[email protected]             hci_initializing_state_machine();
17603429f56bSmatthias.ringwald             break;
1761c7e0c5f6Smatthias.ringwald 
1762c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1763c7e0c5f6Smatthias.ringwald 
17647b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1765c7e0c5f6Smatthias.ringwald             // close all open connections
17663a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1767c7e0c5f6Smatthias.ringwald             if (connection){
176832ab9390Smatthias.ringwald 
1769c7e0c5f6Smatthias.ringwald                 // send disconnect
177064f0b431S[email protected]                 if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
177132ab9390Smatthias.ringwald 
177279bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
17736ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1774c7e0c5f6Smatthias.ringwald 
1775c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1776c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1777c7e0c5f6Smatthias.ringwald                 return;
1778c7e0c5f6Smatthias.ringwald             }
17797b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1780c7e0c5f6Smatthias.ringwald 
178172ea5239Smatthias.ringwald             // switch mode
1782c7e0c5f6Smatthias.ringwald             hci_power_control_off();
17839418f9c9Smatthias.ringwald 
17847b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
178572ea5239Smatthias.ringwald             hci_emit_state();
17867b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
178772ea5239Smatthias.ringwald             break;
1788c7e0c5f6Smatthias.ringwald 
178972ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
17903a9fb326S[email protected]             switch(hci_stack->substate) {
179189db417bSmatthias.ringwald                 case 0:
17927b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
179372ea5239Smatthias.ringwald                     // close all open connections
17943a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
179566da7044Smatthias.ringwald 
179628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
179766da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
179866da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
179966da7044Smatthias.ringwald                         connection = NULL;
180066da7044Smatthias.ringwald                     }
180166da7044Smatthias.ringwald #endif
180272ea5239Smatthias.ringwald                     if (connection){
180332ab9390Smatthias.ringwald 
180472ea5239Smatthias.ringwald                         // send disconnect
180532ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
180632ab9390Smatthias.ringwald 
180779bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
18086ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
180972ea5239Smatthias.ringwald 
181072ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
181172ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
181272ea5239Smatthias.ringwald                         return;
181372ea5239Smatthias.ringwald                     }
181472ea5239Smatthias.ringwald 
181592368cd3S[email protected]                     if (hci_classic_supported()){
181689db417bSmatthias.ringwald                         // disable page and inquiry scan
181764f0b431S[email protected]                         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
181832ab9390Smatthias.ringwald 
181992368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
18203a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
182189db417bSmatthias.ringwald 
182289db417bSmatthias.ringwald                         // continue in next sub state
18233a9fb326S[email protected]                         hci_stack->substate++;
182489db417bSmatthias.ringwald                         break;
182592368cd3S[email protected]                     }
182692368cd3S[email protected]                     // fall through for ble-only chips
182792368cd3S[email protected] 
182889db417bSmatthias.ringwald                 case 2:
18297b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
183028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
183128171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
183228171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
183328171530Smatthias.ringwald                         // SLEEP MODE reached
18343a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
183528171530Smatthias.ringwald                         hci_emit_state();
183628171530Smatthias.ringwald                         break;
183728171530Smatthias.ringwald                     }
183828171530Smatthias.ringwald #endif
183972ea5239Smatthias.ringwald                     // switch mode
18403a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1841c7e0c5f6Smatthias.ringwald                     hci_emit_state();
184228171530Smatthias.ringwald                     break;
184328171530Smatthias.ringwald 
184489db417bSmatthias.ringwald                 default:
184589db417bSmatthias.ringwald                     break;
184689db417bSmatthias.ringwald             }
1847c7e0c5f6Smatthias.ringwald             break;
1848c7e0c5f6Smatthias.ringwald 
18493429f56bSmatthias.ringwald         default:
18503429f56bSmatthias.ringwald             break;
18511f504dbdSmatthias.ringwald     }
18523429f56bSmatthias.ringwald }
185316833f0aSmatthias.ringwald 
185431452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1855c8e4258aSmatthias.ringwald     bd_addr_t addr;
1856c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1857c8e4258aSmatthias.ringwald     // house-keeping
1858c8e4258aSmatthias.ringwald 
1859c8e4258aSmatthias.ringwald     // create_connection?
1860c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1861c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1862339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1863c8e4258aSmatthias.ringwald 
186496a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1865ad83dc6aS[email protected]         if (!conn){
186696a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
186717f1ba2aSmatthias.ringwald             if (!conn){
186817f1ba2aSmatthias.ringwald                 // notify client that alloc failed
186917f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
187017f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
187117f1ba2aSmatthias.ringwald             }
1872ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1873ad83dc6aS[email protected]         }
1874ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1875ad83dc6aS[email protected]         switch (conn->state){
1876ad83dc6aS[email protected]             // if connection active exists
1877ad83dc6aS[email protected]             case OPEN:
187862bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1879ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
188062bda3fbS[email protected]                 return 0;
1881ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1882ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1883ad83dc6aS[email protected]                 break;
1884ad83dc6aS[email protected]             default:
1885ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1886ad83dc6aS[email protected]                 return 0;
1887ad83dc6aS[email protected]         }
1888c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1889c8e4258aSmatthias.ringwald     }
1890c8e4258aSmatthias.ringwald 
18917fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
18927fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
18937fde4af9Smatthias.ringwald     }
18947fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
18957fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
18967fde4af9Smatthias.ringwald     }
18977fde4af9Smatthias.ringwald 
18988ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
18993a9fb326S[email protected]         if (hci_stack->remote_device_db){
19008ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
19013a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
19028ef73945Smatthias.ringwald         }
19038ef73945Smatthias.ringwald     }
1904c8e4258aSmatthias.ringwald 
19056724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
19066724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
19076724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
190896a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
19096724cd9eS[email protected]         if (conn){
19106724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
19116724cd9eS[email protected]         }
19126724cd9eS[email protected]     }
19136724cd9eS[email protected] 
19146724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
19156724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
19166724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
19176724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
19186724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
191996a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
19206724cd9eS[email protected]         if (conn){
19216724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
19226724cd9eS[email protected]         }
19236724cd9eS[email protected]     }
19246724cd9eS[email protected] 
192569a97523S[email protected] #ifdef HAVE_BLE
192669a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
19273a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
192869a97523S[email protected]     }
192969a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
19303a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
193169a97523S[email protected]     }
193269a97523S[email protected] #endif
193369a97523S[email protected] 
19343a9fb326S[email protected]     hci_stack->num_cmd_packets--;
19356b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
19366b4af23dS[email protected] 
19376b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
1938c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
19396b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
19406b4af23dS[email protected]     }
19416b4af23dS[email protected] 
19426b4af23dS[email protected]     return err;
194331452debSmatthias.ringwald }
19448adf0ddaSmatthias.ringwald 
19452bd8b7e7S[email protected] // disconnect because of security block
19462bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
19472bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
19482bd8b7e7S[email protected]     if (!connection) return;
19492bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
19502bd8b7e7S[email protected] }
19512bd8b7e7S[email protected] 
19522bd8b7e7S[email protected] 
1953dbe1a790S[email protected] // Configure Secure Simple Pairing
1954dbe1a790S[email protected] 
1955dbe1a790S[email protected] // enable will enable SSP during init
1956dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
19573a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1958dbe1a790S[email protected] }
1959dbe1a790S[email protected] 
19602bd8b7e7S[email protected] int hci_local_ssp_activated(){
19613a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
19622bd8b7e7S[email protected] }
19632bd8b7e7S[email protected] 
1964dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1965dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
19663a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1967dbe1a790S[email protected] }
1968dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
19693a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1970dbe1a790S[email protected] }
1971dbe1a790S[email protected] 
1972dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1973dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
19743a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1975dbe1a790S[email protected] }
1976dbe1a790S[email protected] 
19771cd208adSmatthias.ringwald /**
19781cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
19791cd208adSmatthias.ringwald  */
1980fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
19815127cc62S[email protected] 
19829d14b626S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)){
19839d14b626S[email protected]         log_error("hci_send_cmd called but cannot send packet now");
19849d14b626S[email protected]         return 0;
19859d14b626S[email protected]     }
19869d14b626S[email protected] 
19875127cc62S[email protected]     // for HCI INITIALIZATION
19885127cc62S[email protected]     // printf("hci_send_cmd: opcode %04x\n", cmd->opcode);
19895127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
19905127cc62S[email protected] 
19919d14b626S[email protected]     hci_reserve_packet_buffer();
19929d14b626S[email protected]     uint8_t * packet = hci_stack->hci_packet_buffer;
19939d14b626S[email protected] 
19941cd208adSmatthias.ringwald     va_list argptr;
19951cd208adSmatthias.ringwald     va_start(argptr, cmd);
19969d14b626S[email protected]     uint16_t size = hci_create_cmd_internal(packet, cmd, argptr);
19971cd208adSmatthias.ringwald     va_end(argptr);
19989d14b626S[email protected] 
19999d14b626S[email protected]     return hci_send_cmd_packet(packet, size);
200093b8dc03Smatthias.ringwald }
2001c8e4258aSmatthias.ringwald 
2002ee091cf1Smatthias.ringwald // Create various non-HCI events.
2003ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
2004ee091cf1Smatthias.ringwald 
2005c8e4258aSmatthias.ringwald void hci_emit_state(){
20063a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
2007425d1371Smatthias.ringwald     uint8_t event[3];
200880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
2009425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
20103a9fb326S[email protected]     event[2] = hci_stack->state;
2011425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20123a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2013c8e4258aSmatthias.ringwald }
2014c8e4258aSmatthias.ringwald 
201517f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
2016425d1371Smatthias.ringwald     uint8_t event[13];
2017c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
2018425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
201917f1ba2aSmatthias.ringwald     event[2] = status;
2020c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
2021c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
2022c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
2023c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
2024425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20253a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2026c8e4258aSmatthias.ringwald }
2027c8e4258aSmatthias.ringwald 
20284f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){
20294f3229d8S[email protected]     uint8_t event[21];
20304f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
20314f3229d8S[email protected]     event[1] = sizeof(event) - 2;
20324f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
20334f3229d8S[email protected]     event[3] = status;
20344f3229d8S[email protected]     bt_store_16(event, 4, conn->con_handle);
20354f3229d8S[email protected]     event[6] = 0; // TODO: role
20364f3229d8S[email protected]     event[7] = conn->address_type;
20374f3229d8S[email protected]     bt_flip_addr(&event[8], conn->address);
20384f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
20394f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
20404f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
20414f3229d8S[email protected]     event[20] = 0; // master clock accuracy
20424f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20434f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20444f3229d8S[email protected] }
20454f3229d8S[email protected] 
20463c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
2047425d1371Smatthias.ringwald     uint8_t event[6];
20483c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
2049e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
20503c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
20513c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
20523c4d4b90Smatthias.ringwald     event[5] = reason;
2053425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20543a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20553c4d4b90Smatthias.ringwald }
20563c4d4b90Smatthias.ringwald 
2057ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
205866fb9560S[email protected]     if (disable_l2cap_timeouts) return;
2059e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2060425d1371Smatthias.ringwald     uint8_t event[4];
206180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2062425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2063ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2064425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20653a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2066ee091cf1Smatthias.ringwald }
206743bfb1bdSmatthias.ringwald 
206843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
2069e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2070425d1371Smatthias.ringwald     uint8_t event[3];
207180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2072425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
207343bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2074425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20753a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
207643bfb1bdSmatthias.ringwald }
2077038bc64cSmatthias.ringwald 
2078038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
2079e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2080425d1371Smatthias.ringwald     uint8_t event[2];
208180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2082425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2083425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20843a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2085038bc64cSmatthias.ringwald }
20861b0e3922Smatthias.ringwald 
208709ba8edeSmatthias.ringwald #ifndef EMBEDDED
20881b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
2089e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2090425d1371Smatthias.ringwald     uint8_t event[6];
20911b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2092425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2093425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2094425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
2095425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
2096425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20973a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20981b0e3922Smatthias.ringwald }
209909ba8edeSmatthias.ringwald #endif
21001b0e3922Smatthias.ringwald 
21012ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2102e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2103425d1371Smatthias.ringwald     uint8_t event[3];
21042ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2105425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
21062ed6235cSmatthias.ringwald     event[2] = enabled;
2107425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21083a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
21092ed6235cSmatthias.ringwald }
2110627c2f45Smatthias.ringwald 
2111627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
2112e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2113627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2114e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2115f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
21162f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
2117f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2118e0abb8e7S[email protected] 
2119e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
2120e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
2121e0abb8e7S[email protected] 
2122e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
21233a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2124627c2f45Smatthias.ringwald }
2125381fbed8Smatthias.ringwald 
2126381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2127e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2128425d1371Smatthias.ringwald     uint8_t event[3];
2129381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2130425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2131381fbed8Smatthias.ringwald     event[2] = enabled;
2132425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21333a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2134381fbed8Smatthias.ringwald }
2135458bf4e8S[email protected] 
2136a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2137df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2138a00031e2S[email protected]     uint8_t event[5];
2139e00caf9cS[email protected]     int pos = 0;
2140a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2141e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2142a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2143e00caf9cS[email protected]     pos += 2;
2144e00caf9cS[email protected]     event[pos++] = level;
2145e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21463a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2147e00caf9cS[email protected] }
2148e00caf9cS[email protected] 
2149ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
2150ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2151ad83dc6aS[email protected]     uint8_t event[9];
2152ad83dc6aS[email protected]     int pos = 0;
2153ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2154ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2155ad83dc6aS[email protected]     event[pos++] = status;
2156ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
2157ad83dc6aS[email protected]     pos += 6;
2158ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21593a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2160ad83dc6aS[email protected] }
2161ad83dc6aS[email protected] 
21622bd8b7e7S[email protected] // query if remote side supports SSP
21632bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
21642bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
21652bd8b7e7S[email protected]     if (!connection) return 0;
21662bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
21672bd8b7e7S[email protected] }
21682bd8b7e7S[email protected] 
2169df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2170df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2171df3354fcS[email protected] }
2172df3354fcS[email protected] 
2173458bf4e8S[email protected] // GAP API
2174458bf4e8S[email protected] /**
2175458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2176458bf4e8S[email protected]  * @praram enabled
2177458bf4e8S[email protected]  */
21784c57c146S[email protected] void gap_set_bondable_mode(int enable){
21793a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2180458bf4e8S[email protected] }
2181cb230b9dS[email protected] 
2182cb230b9dS[email protected] /**
218334d2123cS[email protected]  * @brief map link keys to security levels
2184cb230b9dS[email protected]  */
218534d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
218634d2123cS[email protected]     switch (link_key_type){
21873c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
21883c68dfa9S[email protected]             return LEVEL_4;
21893c68dfa9S[email protected]         case COMBINATION_KEY:
21903c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
21913c68dfa9S[email protected]             return LEVEL_3;
21923c68dfa9S[email protected]         default:
21933c68dfa9S[email protected]             return LEVEL_2;
21943c68dfa9S[email protected]     }
2195cb230b9dS[email protected] }
2196cb230b9dS[email protected] 
2197a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
219834d2123cS[email protected]     if (!connection) return LEVEL_0;
219934d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
220034d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
220134d2123cS[email protected] }
220234d2123cS[email protected] 
220334d2123cS[email protected] 
2204106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
22055127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
2206106d6d11S[email protected]     return level > LEVEL_2;
2207106d6d11S[email protected] }
2208106d6d11S[email protected] 
220934d2123cS[email protected] /**
221034d2123cS[email protected]  * @brief get current security level
221134d2123cS[email protected]  */
221234d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
221334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
221434d2123cS[email protected]     if (!connection) return LEVEL_0;
221534d2123cS[email protected]     return gap_security_level_for_connection(connection);
221634d2123cS[email protected] }
221734d2123cS[email protected] 
2218cb230b9dS[email protected] /**
2219cb230b9dS[email protected]  * @brief request connection to device to
2220cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2221cb230b9dS[email protected]  */
222234d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
222334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
222434d2123cS[email protected]     if (!connection){
2225a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
222634d2123cS[email protected]         return;
222734d2123cS[email protected]     }
222834d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
222934d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
223034d2123cS[email protected]     if (current_level >= requested_level){
2231a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
223234d2123cS[email protected]         return;
223334d2123cS[email protected]     }
2234a00031e2S[email protected] 
223534d2123cS[email protected]     connection->requested_security_level = requested_level;
2236a00031e2S[email protected] 
2237fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
22383a9fb326S[email protected]     if (hci_stack->remote_device_db){
2239a00031e2S[email protected]         link_key_type_t link_key_type;
2240a00031e2S[email protected]         link_key_t      link_key;
22413a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2242a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2243a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2244a00031e2S[email protected]                 return;
2245a00031e2S[email protected]             }
2246a00031e2S[email protected]         }
2247a00031e2S[email protected]     }
2248a00031e2S[email protected] 
22491eb2563eS[email protected]     // try to authenticate connection
22501eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2251e80b2cf9S[email protected]     hci_run();
2252e00caf9cS[email protected] }
2253ad83dc6aS[email protected] 
2254ad83dc6aS[email protected] /**
2255ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2256ad83dc6aS[email protected]  * @param device
2257ad83dc6aS[email protected]  * @param request MITM protection
2258ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2259ad83dc6aS[email protected]  */
2260ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2261ad83dc6aS[email protected] 
2262ad83dc6aS[email protected]     // create connection state machine
226396a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2264ad83dc6aS[email protected] 
2265ad83dc6aS[email protected]     if (!connection){
2266ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2267ad83dc6aS[email protected]     }
2268ad83dc6aS[email protected] 
2269ad83dc6aS[email protected]     // delete linkn key
2270ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2271ad83dc6aS[email protected] 
2272ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2273ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2274ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
22755127cc62S[email protected]     log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level);
2276ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2277ad83dc6aS[email protected] 
2278ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2279ad83dc6aS[email protected] 
2280ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2281ad83dc6aS[email protected]     // handle: authentication failure
2282ad83dc6aS[email protected]     // handle: disconnect on done
2283ad83dc6aS[email protected] 
2284ad83dc6aS[email protected]     hci_run();
2285ad83dc6aS[email protected] 
2286ad83dc6aS[email protected]     return 0;
2287ad83dc6aS[email protected] }
22888e618f72S[email protected] 
22898e618f72S[email protected] void gap_set_local_name(const char * local_name){
22908e618f72S[email protected]     hci_stack->local_name = local_name;
22918e618f72S[email protected] }
22928e618f72S[email protected] 
22937bdc6798S[email protected] le_command_status_t le_central_start_scan(){
229436af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
22957bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
22967bdc6798S[email protected]     hci_run();
22977bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
22987bdc6798S[email protected] }
22998e618f72S[email protected] 
23007bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
230136af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
23027bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
23037bdc6798S[email protected]     hci_run();
23047bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
23057bdc6798S[email protected] }
23064f3229d8S[email protected] 
2307ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2308ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2309ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2310ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2311ef11999fSmatthias.ringwald     hci_run();
2312ef11999fSmatthias.ringwald }
23134f3229d8S[email protected] 
23144f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){
23154f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
23164f3229d8S[email protected]     if (!conn){
23171f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
23184f3229d8S[email protected]         conn = create_connection_for_bd_addr_and_type(*addr, addr_type);
23194f3229d8S[email protected]         if (!conn){
23204f3229d8S[email protected]             // notify client that alloc failed
23214f3229d8S[email protected]             hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
23221f479f8cS[email protected]             log_info("le_central_connect: failed to alloc context");
23234f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
23244f3229d8S[email protected]         }
23254f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
23261f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2327564fca32S[email protected]         hci_run();
23284f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
23294f3229d8S[email protected]     }
23300bf6344aS[email protected] 
23310bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
23320bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
23330bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
23340bf6344aS[email protected]         hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED);
23351f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
23360bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
23370bf6344aS[email protected]     }
23380bf6344aS[email protected] 
23391f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
23404f3229d8S[email protected]     hci_emit_le_connection_complete(conn, 0);
23414f3229d8S[email protected]     hci_run();
23424f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
23434f3229d8S[email protected] }
23444f3229d8S[email protected] 
23457851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
23467851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){
23470bf6344aS[email protected]     linked_item_t *it;
23480bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
23490bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
23500bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
23510bf6344aS[email protected]         switch (conn->state){
2352a6725849S[email protected]             case SEND_CREATE_CONNECTION:
23537851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
23547851196eSmatthias.ringwald                 return conn;
23557851196eSmatthias.ringwald             default:
23567851196eSmatthias.ringwald                 break;
23577851196eSmatthias.ringwald         };
23587851196eSmatthias.ringwald     }
23597851196eSmatthias.ringwald     return NULL;
23607851196eSmatthias.ringwald }
23617851196eSmatthias.ringwald 
23627851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){
23637851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
23647851196eSmatthias.ringwald     switch (conn->state){
23657851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
23667851196eSmatthias.ringwald             // skip sending create connection and emit event instead
23670bf6344aS[email protected]             hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
23687851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
23697851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
23700bf6344aS[email protected]             break;
2371a6725849S[email protected]         case SENT_CREATE_CONNECTION:
23727851196eSmatthias.ringwald             // request to send cancel connection
23730bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
23740bf6344aS[email protected]             hci_run();
23750bf6344aS[email protected]             break;
23760bf6344aS[email protected]         default:
23770bf6344aS[email protected]             break;
23780bf6344aS[email protected]     }
2379e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2380e31f89a7S[email protected] }
23814f3229d8S[email protected] 
23825917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
23835917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
23845917a5c5S[email protected]     if (!conn){
23857851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
23865917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
23875917a5c5S[email protected]     }
23885917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
23895917a5c5S[email protected]     hci_run();
23904f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
23914f3229d8S[email protected] }
239204a6ef8cSmatthias.ringwald 
239304a6ef8cSmatthias.ringwald void hci_disconnect_all(){
239404a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
239504a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
239604a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
239704a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
239804a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
239904a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
240004a6ef8cSmatthias.ringwald     }
2401d31fba26S[email protected]     hci_run();
240204a6ef8cSmatthias.ringwald }
2403