xref: /btstack/src/hci.c (revision 88789c61880a964f3ff681281fe6f3d90c3b1923)
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){
2916b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2926b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
2936b4af23dS[email protected]     return 1;
2946b4af23dS[email protected] }
2956b4af23dS[email protected] 
29668a0fcf7S[email protected] void hci_release_packet_buffer(void){
29768a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
29868a0fcf7S[email protected] }
29968a0fcf7S[email protected] 
3006b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
3016b4af23dS[email protected] int hci_transport_synchronous(void){
3026b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
3036b4af23dS[email protected] }
3046b4af23dS[email protected] 
305ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
3067856c818Smatthias.ringwald 
3076218e6f1Smatthias.ringwald     // check for free places on BT module
30897b61c7bS[email protected]     if (!hci_number_free_acl_slots()) {
30997b61c7bS[email protected]         hci_release_packet_buffer();
31097b61c7bS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
31197b61c7bS[email protected]     }
3126218e6f1Smatthias.ringwald 
3137856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3145061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
31597b61c7bS[email protected]     if (!connection) {
31697b61c7bS[email protected]         hci_release_packet_buffer();
31797b61c7bS[email protected]         return 0;
31897b61c7bS[email protected]     }
31956cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
32056cf178bSmatthias.ringwald 
32156cf178bSmatthias.ringwald     // count packet
32256cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
3237b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
3247856c818Smatthias.ringwald 
32500d8e42eSmatthias.ringwald     // send packet
3263a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
3276218e6f1Smatthias.ringwald 
3286b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
329c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
33097b61c7bS[email protected]         hci_release_packet_buffer();
3316b4af23dS[email protected]     }
3326b4af23dS[email protected] 
33300d8e42eSmatthias.ringwald     return err;
334ee091cf1Smatthias.ringwald }
335ee091cf1Smatthias.ringwald 
33616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
3377856c818Smatthias.ringwald 
338e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
339e76a89eeS[email protected] 
3407856c818Smatthias.ringwald     // get info
3417856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3425061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
3437856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
3447856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
3457856c818Smatthias.ringwald 
3467856c818Smatthias.ringwald     // ignore non-registered handle
3477856c818Smatthias.ringwald     if (!conn){
3487d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3497856c818Smatthias.ringwald         return;
3507856c818Smatthias.ringwald     }
3517856c818Smatthias.ringwald 
352e76a89eeS[email protected]     // assert packet is complete
3539ecc3e17S[email protected]     if (acl_length + 4 != size){
354e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
355e76a89eeS[email protected]         return;
356e76a89eeS[email protected]     }
357e76a89eeS[email protected] 
3587856c818Smatthias.ringwald     // update idle timestamp
3597856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3607856c818Smatthias.ringwald 
3617856c818Smatthias.ringwald     // handle different packet types
3627856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3637856c818Smatthias.ringwald 
3647856c818Smatthias.ringwald         case 0x01: // continuation fragment
3657856c818Smatthias.ringwald 
3667856c818Smatthias.ringwald             // sanity check
3677856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3687d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3697856c818Smatthias.ringwald                 return;
3707856c818Smatthias.ringwald             }
3717856c818Smatthias.ringwald 
3727856c818Smatthias.ringwald             // append fragment payload (header already stored)
3737856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3747856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3757856c818Smatthias.ringwald 
3767d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
377decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3787856c818Smatthias.ringwald 
3797856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3807856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3817856c818Smatthias.ringwald 
3823a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3837856c818Smatthias.ringwald                 // reset recombination buffer
3847856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3857856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3867856c818Smatthias.ringwald             }
3877856c818Smatthias.ringwald             break;
3887856c818Smatthias.ringwald 
3897856c818Smatthias.ringwald         case 0x02: { // first fragment
3907856c818Smatthias.ringwald 
3917856c818Smatthias.ringwald             // sanity check
3927856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3937d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3947856c818Smatthias.ringwald                 return;
3957856c818Smatthias.ringwald             }
3967856c818Smatthias.ringwald 
3977856c818Smatthias.ringwald             // peek into L2CAP packet!
3987856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3997856c818Smatthias.ringwald 
400e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
401decc01a8Smatthias.ringwald 
4027856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
4037856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
4047856c818Smatthias.ringwald 
4057856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
4063a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
4077856c818Smatthias.ringwald 
4087856c818Smatthias.ringwald             } else {
4097856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
4107856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
4117856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
4127856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
413decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
4147856c818Smatthias.ringwald             }
4157856c818Smatthias.ringwald             break;
4167856c818Smatthias.ringwald 
4177856c818Smatthias.ringwald         }
4187856c818Smatthias.ringwald         default:
4197d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
4207856c818Smatthias.ringwald             return;
4217856c818Smatthias.ringwald     }
42294ab26f8Smatthias.ringwald 
42394ab26f8Smatthias.ringwald     // execute main loop
42494ab26f8Smatthias.ringwald     hci_run();
42516833f0aSmatthias.ringwald }
42622909952Smatthias.ringwald 
42767a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
4281f479f8cS[email protected]     log_info("Connection closed: handle 0x%x, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
4293c4d4b90Smatthias.ringwald 
430c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
431c785ef68Smatthias.ringwald 
4323a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
433a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
4343c4d4b90Smatthias.ringwald 
4353c4d4b90Smatthias.ringwald     // now it's gone
436c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
437c7e0c5f6Smatthias.ringwald }
438c7e0c5f6Smatthias.ringwald 
4390c042179S[email protected] static const uint16_t packet_type_sizes[] = {
4408f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
4418f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
4428f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4438f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4448f8108aaSmatthias.ringwald };
44565389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
44665389bfcS[email protected]      0, // 3 slot packets
44765389bfcS[email protected]      1, // 5 slot packets
44865389bfcS[email protected]     25, // EDR 2 mpbs
44965389bfcS[email protected]     26, // EDR 3 mbps
45065389bfcS[email protected]     39, // 3 slot EDR packts
45165389bfcS[email protected]     40, // 5 slot EDR packet
45265389bfcS[email protected] };
45365389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
45465389bfcS[email protected]     0x0f00, // 3 slot packets
45565389bfcS[email protected]     0xf000, // 5 slot packets
45665389bfcS[email protected]     0x1102, // EDR 2 mpbs
45765389bfcS[email protected]     0x2204, // EDR 3 mbps
45865389bfcS[email protected]     0x0300, // 3 slot EDR packts
45965389bfcS[email protected]     0x3000, // 5 slot EDR packet
46065389bfcS[email protected] };
4618f8108aaSmatthias.ringwald 
46265389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
46365389bfcS[email protected]     // enable packet types based on size
4648f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4658f8108aaSmatthias.ringwald     int i;
4668f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4678f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4688f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4698f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4708f8108aaSmatthias.ringwald         }
4718f8108aaSmatthias.ringwald     }
47265389bfcS[email protected]     // disable packet types due to missing local supported features
47365389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
47465389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
47565389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
47665389bfcS[email protected]         if (feature_set) continue;
47765389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
47865389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
47965389bfcS[email protected]     }
4808f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4818f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4828f8108aaSmatthias.ringwald     return packet_types;
4838f8108aaSmatthias.ringwald }
4848f8108aaSmatthias.ringwald 
4858f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4863a9fb326S[email protected]     return hci_stack->packet_types;
4878f8108aaSmatthias.ringwald }
4888f8108aaSmatthias.ringwald 
489facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
4907dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4913a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
4927dc17943Smatthias.ringwald }
4937dc17943Smatthias.ringwald 
494f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4953a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
4967dc17943Smatthias.ringwald }
4977dc17943Smatthias.ringwald 
4986ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){
4996ac9a97eS[email protected]     // No. 54, byte 6, bit 6
5006ac9a97eS[email protected]     return (hci_stack->local_supported_features[6] & (1 << 6)) != 0;
5016ac9a97eS[email protected] }
5026ac9a97eS[email protected] 
503f5d8d141S[email protected] int hci_ssp_supported(void){
5046ac9a97eS[email protected]     // No. 51, byte 6, bit 3
5053a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
506f5d8d141S[email protected] }
507f5d8d141S[email protected] 
508f5d8d141S[email protected] int hci_classic_supported(void){
5096ac9a97eS[email protected]     // No. 37, byte 4, bit 5, = No BR/EDR Support
5103a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
511f5d8d141S[email protected] }
512f5d8d141S[email protected] 
513f5d8d141S[email protected] int hci_le_supported(void){
514f5d8d141S[email protected] #ifdef HAVE_BLE
5156ac9a97eS[email protected]     // No. 37, byte 4, bit 6 = LE Supported (Controller)
5163a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
517f5d8d141S[email protected] #else
518f5d8d141S[email protected]     return 0;
519f5d8d141S[email protected] #endif
520f5d8d141S[email protected] }
521f5d8d141S[email protected] 
52269a97523S[email protected] // get addr type and address used in advertisement packets
52318904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
5243a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
5253a9fb326S[email protected]     if (hci_stack->adv_addr_type){
5263a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
52769a97523S[email protected]     } else {
5283a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
52969a97523S[email protected]     }
53069a97523S[email protected] }
53169a97523S[email protected] 
532b2f949feS[email protected] #ifdef HAVE_BLE
533e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){
53457c9da5bS[email protected]     int num_reports = packet[3];
53557c9da5bS[email protected]     int i;
53657c9da5bS[email protected]     int total_data_length = 0;
53757c9da5bS[email protected]     int data_offset = 0;
53857c9da5bS[email protected] 
53957c9da5bS[email protected]     for (i=0; i<num_reports;i++){
54057c9da5bS[email protected]         total_data_length += packet[4+num_reports*8+i];
54157c9da5bS[email protected]     }
54257c9da5bS[email protected] 
54357c9da5bS[email protected]     for (i=0; i<num_reports;i++){
54457c9da5bS[email protected]         int pos = 0;
54557c9da5bS[email protected]         uint8_t data_length = packet[4+num_reports*8+i];
5462b552b23S[email protected]         uint8_t event_size = 10 + data_length;
5472b552b23S[email protected]         uint8_t event[2 + event_size ];
5482b552b23S[email protected]         event[pos++] = GAP_LE_ADVERTISING_REPORT;
54957c9da5bS[email protected]         event[pos++] = event_size;
55057c9da5bS[email protected]         event[pos++] = packet[4+i]; // event_type;
55157c9da5bS[email protected]         event[pos++] = packet[4+num_reports+i]; // address_type;
552564fca32S[email protected]         memcpy(&event[pos], &packet[4+num_reports*2+i*6], 6); // bt address
55357c9da5bS[email protected]         pos += 6;
5542b552b23S[email protected]         event[pos++] = packet[4+num_reports*9+total_data_length + i];
55557c9da5bS[email protected]         event[pos++] = data_length;
55688ed79cfS[email protected]         memcpy(&event[pos], &packet[4+num_reports*9+data_offset], data_length);
55757c9da5bS[email protected]         data_offset += data_length;
55857c9da5bS[email protected]         pos += data_length;
55957c9da5bS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
56057c9da5bS[email protected]     }
56157c9da5bS[email protected] }
562b2f949feS[email protected] #endif
56357c9da5bS[email protected] 
5646155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
565a2481739S[email protected]     uint8_t command_completed = 0;
566a2481739S[email protected]     if ((hci_stack->substate % 2) == 0) return;
5676155b3d3S[email protected]     // odd: waiting for event
5686155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5696155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,3);
5706155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
571a2481739S[email protected]             command_completed = 1;
5726155b3d3S[email protected]             log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate);
5736155b3d3S[email protected]         } else {
5746155b3d3S[email protected]             log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
5756155b3d3S[email protected]         }
5766155b3d3S[email protected]     }
5776155b3d3S[email protected]     if (packet[0] == HCI_EVENT_COMMAND_STATUS){
5786155b3d3S[email protected]         uint8_t  status = packet[2];
5796155b3d3S[email protected]         uint16_t opcode = READ_BT_16(packet,4);
5806155b3d3S[email protected]         if (opcode == hci_stack->last_cmd_opcode){
5816155b3d3S[email protected]             if (status){
582a2481739S[email protected]                 command_completed = 1;
5836155b3d3S[email protected]                 log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate);
5846155b3d3S[email protected]             } else {
5856155b3d3S[email protected]                 log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
5866155b3d3S[email protected]             }
5876155b3d3S[email protected]         } else {
5886155b3d3S[email protected]             log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
5896155b3d3S[email protected]         }
5906155b3d3S[email protected]     }
591a2481739S[email protected] 
592a2481739S[email protected]     if (!command_completed) return;
593a2481739S[email protected] 
594a2481739S[email protected]     switch(hci_stack->substate >> 1){
595a2481739S[email protected]         default:
596a2481739S[email protected]             hci_stack->substate++;
597a2481739S[email protected]             break;
5986155b3d3S[email protected]     }
5996155b3d3S[email protected] }
6006155b3d3S[email protected] 
6016155b3d3S[email protected] static void hci_initializing_state_machine(){
6026155b3d3S[email protected]         // log_info("hci_init: substate %u\n", hci_stack->substate);
6036155b3d3S[email protected]     if (hci_stack->substate % 2) {
6046155b3d3S[email protected]         // odd: waiting for command completion
6056155b3d3S[email protected]         return;
6066155b3d3S[email protected]     }
6076155b3d3S[email protected]     switch (hci_stack->substate >> 1){
6086155b3d3S[email protected]         case 0: // RESET
6096155b3d3S[email protected]             hci_state_reset();
6106155b3d3S[email protected] 
6116155b3d3S[email protected]             hci_send_cmd(&hci_reset);
6126155b3d3S[email protected]             if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
6136155b3d3S[email protected]                 // skip baud change
6146155b3d3S[email protected]                 hci_stack->substate = 4; // >> 1 = 2
6156155b3d3S[email protected]             }
6166155b3d3S[email protected]             break;
6176155b3d3S[email protected]         case 1: // SEND BAUD CHANGE
6186155b3d3S[email protected]             hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
619*88789c61Smatthias.ringwald             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
6206155b3d3S[email protected]             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
6216155b3d3S[email protected]             break;
6226155b3d3S[email protected]         case 2: // LOCAL BAUD CHANGE
6236155b3d3S[email protected]             log_info("Local baud rate change");
6246155b3d3S[email protected]             hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
6256155b3d3S[email protected]             hci_stack->substate += 2;
6266155b3d3S[email protected]             // break missing here for fall through
6276155b3d3S[email protected] 
6286155b3d3S[email protected]         case 3:
6296155b3d3S[email protected]             log_info("Custom init");
6306155b3d3S[email protected]             // Custom initialization
6316155b3d3S[email protected]             if (hci_stack->control && hci_stack->control->next_cmd){
6326155b3d3S[email protected]                 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
6336155b3d3S[email protected]                 if (valid_cmd){
6346155b3d3S[email protected]                     int size = 3 + hci_stack->hci_packet_buffer[2];
6356155b3d3S[email protected]                     hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
6366155b3d3S[email protected]                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
6376155b3d3S[email protected]                     hci_stack->substate = 4; // more init commands
6386155b3d3S[email protected]                     break;
6396155b3d3S[email protected]                 }
6406155b3d3S[email protected]                 log_info("hci_run: init script done\n\r");
6416155b3d3S[email protected]             }
6426155b3d3S[email protected]             // otherwise continue
6436155b3d3S[email protected]             hci_send_cmd(&hci_read_bd_addr);
6446155b3d3S[email protected]             break;
6456155b3d3S[email protected]         case 4:
6466155b3d3S[email protected]             hci_send_cmd(&hci_read_buffer_size);
6476155b3d3S[email protected]             break;
6486155b3d3S[email protected]         case 5:
6496155b3d3S[email protected]             hci_send_cmd(&hci_read_local_supported_features);
6506155b3d3S[email protected]             break;
6516155b3d3S[email protected]         case 6:
6526155b3d3S[email protected]             if (hci_le_supported()){
6536155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
6546155b3d3S[email protected]             } else {
6556155b3d3S[email protected]                 // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
6566155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
6576155b3d3S[email protected]             }
6586155b3d3S[email protected] 
6596155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
6606155b3d3S[email protected]             if (!hci_classic_supported()){
6616155b3d3S[email protected]                 if (hci_le_supported()){
6626155b3d3S[email protected]                     hci_stack->substate = 11 << 1;    // skip all classic command
6636155b3d3S[email protected]                 } else {
6646155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
6656155b3d3S[email protected]                     hci_stack->substate = 14 << 1;    // skip all
6666155b3d3S[email protected]                 }
6676155b3d3S[email protected]             }
6686155b3d3S[email protected]             break;
6696155b3d3S[email protected]         case 7:
6706155b3d3S[email protected]             if (hci_ssp_supported()){
6716155b3d3S[email protected]                 hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
6726155b3d3S[email protected]                 break;
6736155b3d3S[email protected]             }
6746155b3d3S[email protected]             hci_stack->substate += 2;
6756155b3d3S[email protected]             // break missing here for fall through
6766155b3d3S[email protected] 
6776155b3d3S[email protected]         case 8:
6786155b3d3S[email protected]             // ca. 15 sec
6796155b3d3S[email protected]             hci_send_cmd(&hci_write_page_timeout, 0x6000);
6806155b3d3S[email protected]             break;
6816155b3d3S[email protected]         case 9:
6826155b3d3S[email protected]             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
6836155b3d3S[email protected]             break;
6846155b3d3S[email protected]         case 10:
6856155b3d3S[email protected]             if (hci_stack->local_name){
6866155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
6876155b3d3S[email protected]             } else {
6886155b3d3S[email protected]                 char hostname[30];
6896155b3d3S[email protected] #ifdef EMBEDDED
6906155b3d3S[email protected]                 // BTstack-11:22:33:44:55:66
6916155b3d3S[email protected]                 strcpy(hostname, "BTstack ");
6926155b3d3S[email protected]                 strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
6936155b3d3S[email protected]                 log_info("---> Name %s", hostname);
6946155b3d3S[email protected] #else
6956155b3d3S[email protected]                 // hostname for POSIX systems
6966155b3d3S[email protected]                 gethostname(hostname, 30);
6976155b3d3S[email protected]                 hostname[29] = '\0';
6986155b3d3S[email protected] #endif
6996155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hostname);
7006155b3d3S[email protected]             }
7016155b3d3S[email protected]             break;
7026155b3d3S[email protected]         case 11:
7036155b3d3S[email protected]             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
7046155b3d3S[email protected]             if (!hci_le_supported()){
7056155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
7066155b3d3S[email protected]                 hci_stack->substate = 14 << 1;
7076155b3d3S[email protected]             }
7086155b3d3S[email protected]             break;
7096155b3d3S[email protected] 
7106155b3d3S[email protected] #ifdef HAVE_BLE
7116155b3d3S[email protected]         // LE INIT
7126155b3d3S[email protected]         case 12:
7136155b3d3S[email protected]             hci_send_cmd(&hci_le_read_buffer_size);
7146155b3d3S[email protected]             break;
7156155b3d3S[email protected]         case 13:
7166155b3d3S[email protected]             // LE Supported Host = 1, Simultaneous Host = 0
7176155b3d3S[email protected]             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
7186155b3d3S[email protected]             break;
7196155b3d3S[email protected]         case 14:
7206155b3d3S[email protected]             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
7216155b3d3S[email protected]             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
7226155b3d3S[email protected]             break;
7236155b3d3S[email protected] #endif
7246155b3d3S[email protected] 
7256155b3d3S[email protected]         // DONE
7266155b3d3S[email protected]         case 15:
7276155b3d3S[email protected]             // done.
7286155b3d3S[email protected]             hci_stack->state = HCI_STATE_WORKING;
7296155b3d3S[email protected]             hci_emit_state();
7306155b3d3S[email protected]             break;
7316155b3d3S[email protected]         default:
7326155b3d3S[email protected]             break;
7336155b3d3S[email protected]     }
7346155b3d3S[email protected]     hci_stack->substate++;
7356155b3d3S[email protected] }
7366155b3d3S[email protected] 
73774ec757aSmatthias.ringwald // avoid huge local variables
738223aafc1Smatthias.ringwald #ifndef EMBEDDED
73974ec757aSmatthias.ringwald static device_name_t device_name;
740223aafc1Smatthias.ringwald #endif
74116833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
742e76a89eeS[email protected] 
743e76a89eeS[email protected]     uint16_t event_length = packet[1];
744e76a89eeS[email protected] 
745e76a89eeS[email protected]     // assert packet is complete
746e76a89eeS[email protected]     if (size != event_length + 2){
747e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
748e76a89eeS[email protected]         return;
749e76a89eeS[email protected]     }
750e76a89eeS[email protected] 
7511281a47eSmatthias.ringwald     bd_addr_t addr;
75296a45072S[email protected]     bd_addr_type_t addr_type;
7532d00edd4Smatthias.ringwald     uint8_t link_type;
754fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
7551f7b95a1Smatthias.ringwald     hci_connection_t * conn;
75656cf178bSmatthias.ringwald     int i;
75722909952Smatthias.ringwald 
7585909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
7595909f7f2Smatthias.ringwald 
7606772a24cSmatthias.ringwald     switch (packet[0]) {
76122909952Smatthias.ringwald 
7626772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
7637ec5eeaaSmatthias.ringwald             // get num cmd packets
7643a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
7653a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
7667ec5eeaaSmatthias.ringwald 
767e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
768e2edc0c3Smatthias.ringwald                 // from offset 5
769e2edc0c3Smatthias.ringwald                 // status
7701d279b20Smatthias.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"
7713a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
77256cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
7733a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
77456cf178bSmatthias.ringwald                 // ignore: total num SCO packets
7753a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
776a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
7773a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
7783a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7798f8108aaSmatthias.ringwald                     }
78065389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
7813a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
782e2edc0c3Smatthias.ringwald                 }
78356cf178bSmatthias.ringwald             }
78465a46ef3S[email protected] #ifdef HAVE_BLE
78565a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
7863a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
7873a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
7883a9fb326S[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);
78901f1657cS[email protected] 
79001f1657cS[email protected]                 // use LE buffers if no clasic buffers have been reported
79101f1657cS[email protected]                 if (hci_stack->total_num_acl_packets == 0){
79201f1657cS[email protected]                     log_info("use le buffers instead of classic ones");
79301f1657cS[email protected]                     hci_stack->total_num_acl_packets  = hci_stack->total_num_le_packets;
79401f1657cS[email protected]                     hci_stack->acl_data_packet_length = hci_stack->le_data_packet_length;
79501f1657cS[email protected]                     // determine usable ACL payload size
79601f1657cS[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
79701f1657cS[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
79801f1657cS[email protected]                     }
79901f1657cS[email protected]                 }
80065a46ef3S[email protected]             }
80165a46ef3S[email protected] #endif
802188981d2Smatthias.ringwald             // Dump local address
803188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
8043a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
805188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
8063a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
807188981d2Smatthias.ringwald             }
808381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
8093a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
810381fbed8Smatthias.ringwald             }
81165389bfcS[email protected]             // Note: HCI init checks
812559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
8133a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
814559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
8153a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
8163a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
8173a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
8183a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
81965389bfcS[email protected] 
82065389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
8213a9fb326S[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]);
8223a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
823f5d8d141S[email protected] 
824f5d8d141S[email protected]                 // Classic/LE
825f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
826559e517eS[email protected]             }
82756cf178bSmatthias.ringwald             break;
82856cf178bSmatthias.ringwald 
8297ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
8307ec5eeaaSmatthias.ringwald             // get num cmd packets
8313a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
8323a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
8337ec5eeaaSmatthias.ringwald             break;
8347ec5eeaaSmatthias.ringwald 
83556cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
83656cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
83756cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
83856cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
8395061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
84056cf178bSmatthias.ringwald                 if (!conn){
8417d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
84256cf178bSmatthias.ringwald                     continue;
84356cf178bSmatthias.ringwald                 }
84456cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
8457b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
84656cf178bSmatthias.ringwald             }
8476772a24cSmatthias.ringwald             break;
8486772a24cSmatthias.ringwald 
8491f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
85037eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
85137eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
8522d00edd4Smatthias.ringwald             link_type = packet[11];
853339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
85437eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
85596a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
8561f7b95a1Smatthias.ringwald                 if (!conn) {
85796a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
8581f7b95a1Smatthias.ringwald                 }
859ce4c8fabSmatthias.ringwald                 if (!conn) {
860ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
8613a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
8623a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
863ce4c8fabSmatthias.ringwald                     break;
864ce4c8fabSmatthias.ringwald                 }
86532ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
86632ab9390Smatthias.ringwald                 hci_run();
86737eaa4cfSmatthias.ringwald             } else {
868ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
8693a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
8703a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
87137eaa4cfSmatthias.ringwald             }
8721f7b95a1Smatthias.ringwald             break;
8731f7b95a1Smatthias.ringwald 
8746772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
875fe1ed1b8Smatthias.ringwald             // Connection management
876fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
877339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
87896a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
87996a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
880fe1ed1b8Smatthias.ringwald             if (conn) {
881b448a0e7Smatthias.ringwald                 if (!packet[2]){
882c8e4258aSmatthias.ringwald                     conn->state = OPEN;
883fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
884ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
885ee091cf1Smatthias.ringwald 
886c785ef68Smatthias.ringwald                     // restart timer
887c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
888ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
889c785ef68Smatthias.ringwald 
890339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
89143bfb1bdSmatthias.ringwald 
89243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
893b448a0e7Smatthias.ringwald                 } else {
894ad83dc6aS[email protected]                     // notify client if dedicated bonding
895ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
896ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
897ad83dc6aS[email protected]                     }
898ad83dc6aS[email protected] 
899b448a0e7Smatthias.ringwald                     // connection failed, remove entry
9003a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
901a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
902c12e46e7Smatthias.ringwald 
903c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
904c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
905c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
906c12e46e7Smatthias.ringwald                     }
907fe1ed1b8Smatthias.ringwald                 }
908fe1ed1b8Smatthias.ringwald             }
9096772a24cSmatthias.ringwald             break;
910fe1ed1b8Smatthias.ringwald 
911afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
912afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
913afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
914afd4e962S[email protected]             if (!conn) break;
915afd4e962S[email protected]             if (!packet[2]){
916afd4e962S[email protected]                 uint8_t * features = &packet[5];
917afd4e962S[email protected]                 if (features[6] & (1 << 3)){
918afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
919afd4e962S[email protected]                 }
920afd4e962S[email protected]             }
921afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
922ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
923ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
924ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
925ad83dc6aS[email protected]             }
926afd4e962S[email protected]             break;
927afd4e962S[email protected] 
9287fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
9297b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
9307fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
93174d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
9323a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
93332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
93464472d52Smatthias.ringwald             hci_run();
935d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
93629d53098Smatthias.ringwald             return;
9377fde4af9Smatthias.ringwald 
9389ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
93929d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
94096a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
9419ab95c90S[email protected]             if (!conn) break;
9429ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
9437bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
9449ab95c90S[email protected]             // Change Connection Encryption keeps link key type
9459ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
9469ab95c90S[email protected]                 conn->link_key_type = link_key_type;
9479ab95c90S[email protected]             }
9483a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
9493a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
95029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
9517fde4af9Smatthias.ringwald             break;
9529ab95c90S[email protected]         }
9537fde4af9Smatthias.ringwald 
9547fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
9556724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
9564c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
9573a9fb326S[email protected]             if (!hci_stack->bondable){
958899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
959f8fb5f6eS[email protected]                 hci_run();
960f8fb5f6eS[email protected]                 return;
9614c57c146S[email protected]             }
962d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
9633a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
964d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
9653a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
9667fde4af9Smatthias.ringwald             break;
9677fde4af9Smatthias.ringwald 
9681d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
9691d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
970dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
971dbe1a790S[email protected]             break;
972dbe1a790S[email protected] 
973dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
9746724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
9753a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
976dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
977dbe1a790S[email protected]             break;
978dbe1a790S[email protected] 
979dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
9806724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
9813a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
982dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
9831d6b20aeS[email protected]             break;
9841d6b20aeS[email protected] 
985f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
986f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
987f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
988f0944df2S[email protected]             if (!conn) break;
989ad83dc6aS[email protected]             if (packet[2] == 0) {
990f0944df2S[email protected]                 if (packet[5]){
991f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
992f0944df2S[email protected]                 } else {
993536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
994f0944df2S[email protected]                 }
995ad83dc6aS[email protected]             }
996a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
997f0944df2S[email protected]             break;
998f0944df2S[email protected] 
9991eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
10001eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
10011eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
10021eb2563eS[email protected]             if (!conn) break;
1003ad83dc6aS[email protected] 
1004ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
1005ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
1006ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
1007ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
1008ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
1009ad83dc6aS[email protected]                 break;
1010ad83dc6aS[email protected]             }
1011ad83dc6aS[email protected] 
1012b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
10131eb2563eS[email protected]                 // link key sufficient for requested security
10141eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1015ad83dc6aS[email protected]                 break;
1016ad83dc6aS[email protected]             }
10171eb2563eS[email protected]             // not enough
10181eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
10191eb2563eS[email protected]             break;
102034d2123cS[email protected] 
1021223aafc1Smatthias.ringwald #ifndef EMBEDDED
102274ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
10233a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
102474ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
102574ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10265a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
10275a06394aSmatthias.ringwald             for (i=0; i<248;i++){
10285a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
10295a06394aSmatthias.ringwald                     packet[9+i] = 0;
10305a06394aSmatthias.ringwald                     break;
1031cdc9101dSmatthias.ringwald                 }
10325a06394aSmatthias.ringwald             }
1033d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
103474ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
10353a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
103674ec757aSmatthias.ringwald             break;
103774ec757aSmatthias.ringwald 
103874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
103974ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
10403a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
104174ec757aSmatthias.ringwald             // first send inq result packet
10423a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
104374ec757aSmatthias.ringwald             // then send cached remote names
104474ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
104574ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
10463a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
104774ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
104874ec757aSmatthias.ringwald                 }
104974ec757aSmatthias.ringwald             }
105074ec757aSmatthias.ringwald             return;
1051223aafc1Smatthias.ringwald #endif
105274ec757aSmatthias.ringwald 
10536772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1054fe1ed1b8Smatthias.ringwald             if (!packet[2]){
1055fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
10565061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
1057fe1ed1b8Smatthias.ringwald                 if (conn) {
1058c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
1059fe1ed1b8Smatthias.ringwald                 }
1060fe1ed1b8Smatthias.ringwald             }
10616772a24cSmatthias.ringwald             break;
10626772a24cSmatthias.ringwald 
1063c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
10643a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
10653a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
10667586ee35S[email protected]             } else {
10677586ee35S[email protected]                 // if no special requests, just reboot stack
10687586ee35S[email protected]                 hci_power_control_off();
10697586ee35S[email protected]                 hci_power_control_on();
1070c68bdf90Smatthias.ringwald             }
1071c68bdf90Smatthias.ringwald             break;
1072c68bdf90Smatthias.ringwald 
10736b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
10746b4af23dS[email protected]             // free packet buffer for asynchronous transport
10756b4af23dS[email protected]             if (hci_transport_synchronous()) break;
10766b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
10776b4af23dS[email protected]             break;
10786b4af23dS[email protected] 
10795909f7f2Smatthias.ringwald #ifdef HAVE_BLE
10805909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
10815909f7f2Smatthias.ringwald             switch (packet[2]){
108257c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
10837bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
108457c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
10857bdc6798S[email protected]                     break;
10865909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
10875909f7f2Smatthias.ringwald                     // Connection management
10885909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
10897bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
109096a45072S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr));
10915909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
109296a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
10935909f7f2Smatthias.ringwald                     if (packet[3]){
10945909f7f2Smatthias.ringwald                         if (conn){
10955909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
10963a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
10975909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
10985909f7f2Smatthias.ringwald                         }
10995909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
11005909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
11015909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
11025909f7f2Smatthias.ringwald                         }
11035909f7f2Smatthias.ringwald                         break;
11045909f7f2Smatthias.ringwald                     }
11055909f7f2Smatthias.ringwald                     if (!conn){
110696a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
11075909f7f2Smatthias.ringwald                     }
11085909f7f2Smatthias.ringwald                     if (!conn){
11095909f7f2Smatthias.ringwald                         // no memory
11105909f7f2Smatthias.ringwald                         break;
11115909f7f2Smatthias.ringwald                     }
11125909f7f2Smatthias.ringwald 
11135909f7f2Smatthias.ringwald                     conn->state = OPEN;
11145909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
11155909f7f2Smatthias.ringwald 
11165909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
11175909f7f2Smatthias.ringwald 
11185909f7f2Smatthias.ringwald                     // restart timer
11195909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
11205909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
11215909f7f2Smatthias.ringwald 
11225909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
11235909f7f2Smatthias.ringwald 
11245909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
11255909f7f2Smatthias.ringwald                     break;
11265909f7f2Smatthias.ringwald 
112765a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
112865a46ef3S[email protected] 
11295909f7f2Smatthias.ringwald                 default:
11305909f7f2Smatthias.ringwald                     break;
11315909f7f2Smatthias.ringwald             }
11325909f7f2Smatthias.ringwald             break;
11335909f7f2Smatthias.ringwald #endif
11346772a24cSmatthias.ringwald         default:
11356772a24cSmatthias.ringwald             break;
1136fe1ed1b8Smatthias.ringwald     }
1137fe1ed1b8Smatthias.ringwald 
11383429f56bSmatthias.ringwald     // handle BT initialization
11393a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
11406155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1141c9af4d3fS[email protected]     }
114222909952Smatthias.ringwald 
114389db417bSmatthias.ringwald     // help with BT sleep
11443a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
11453a9fb326S[email protected]         && hci_stack->substate == 1
114689db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
11473a9fb326S[email protected]         hci_stack->substate++;
114889db417bSmatthias.ringwald     }
114989db417bSmatthias.ringwald 
11503a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
115194ab26f8Smatthias.ringwald 
115294ab26f8Smatthias.ringwald 	// execute main loop
115394ab26f8Smatthias.ringwald 	hci_run();
115416833f0aSmatthias.ringwald }
115516833f0aSmatthias.ringwald 
11560a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
115710e830c9Smatthias.ringwald     switch (packet_type) {
115810e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
115910e830c9Smatthias.ringwald             event_handler(packet, size);
116010e830c9Smatthias.ringwald             break;
116110e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
116210e830c9Smatthias.ringwald             acl_handler(packet, size);
116310e830c9Smatthias.ringwald             break;
116410e830c9Smatthias.ringwald         default:
116510e830c9Smatthias.ringwald             break;
116610e830c9Smatthias.ringwald     }
116710e830c9Smatthias.ringwald }
116810e830c9Smatthias.ringwald 
1169fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
11702718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
11713a9fb326S[email protected]     hci_stack->packet_handler = handler;
117216833f0aSmatthias.ringwald }
117316833f0aSmatthias.ringwald 
11746155b3d3S[email protected] static void hci_state_reset(){
1175595bdbfbS[email protected]     // no connections yet
1176595bdbfbS[email protected]     hci_stack->connections = NULL;
117774308b23Smatthias.ringwald 
117874308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
117974308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
118074308b23Smatthias.ringwald     // hci_stack->connectable = 0;
118174308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1182595bdbfbS[email protected] 
118344935e40S[email protected]     // buffer is free
118444935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
118544935e40S[email protected] 
1186595bdbfbS[email protected]     // no pending cmds
1187595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1188595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1189595bdbfbS[email protected] 
1190595bdbfbS[email protected]     // LE
1191595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1192595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1193595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1194e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1195595bdbfbS[email protected] }
1196595bdbfbS[email protected] 
11974f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1198475c8125Smatthias.ringwald 
11993a9fb326S[email protected] #ifdef HAVE_MALLOC
12003a9fb326S[email protected]     if (!hci_stack) {
12013a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
12023a9fb326S[email protected]     }
12033a9fb326S[email protected] #else
12043a9fb326S[email protected]     hci_stack = &hci_stack_static;
12053a9fb326S[email protected] #endif
120666fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
12073a9fb326S[email protected] 
1208475c8125Smatthias.ringwald     // reference to use transport layer implementation
12093a9fb326S[email protected]     hci_stack->hci_transport = transport;
1210475c8125Smatthias.ringwald 
121111e23e5fSmatthias.ringwald     // references to used control implementation
12123a9fb326S[email protected]     hci_stack->control = control;
121311e23e5fSmatthias.ringwald 
121411e23e5fSmatthias.ringwald     // reference to used config
12153a9fb326S[email protected]     hci_stack->config = config;
121611e23e5fSmatthias.ringwald 
121716833f0aSmatthias.ringwald     // higher level handler
12183a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
121916833f0aSmatthias.ringwald 
1220404843c1Smatthias.ringwald     // store and open remote device db
12213a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
12223a9fb326S[email protected]     if (hci_stack->remote_device_db) {
12233a9fb326S[email protected]         hci_stack->remote_device_db->open();
1224404843c1Smatthias.ringwald     }
122529d53098Smatthias.ringwald 
12268fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
12273a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
12288fcba05dSmatthias.ringwald 
122916833f0aSmatthias.ringwald     // register packet handlers with transport
123010e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1231f5454fc6Smatthias.ringwald 
12323a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1233e2386ba1S[email protected] 
1234e2386ba1S[email protected]     // class of device
12353a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1236a45d6b9fS[email protected] 
1237f20168b8Smatthias.ringwald     // bondable by default
1238f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1239f20168b8Smatthias.ringwald 
124063048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
12413a9fb326S[email protected]     hci_stack->ssp_enable = 1;
12423a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
12433a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
12443a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
124569a97523S[email protected] 
1246595bdbfbS[email protected]     hci_state_reset();
1247475c8125Smatthias.ringwald }
1248475c8125Smatthias.ringwald 
1249404843c1Smatthias.ringwald void hci_close(){
1250404843c1Smatthias.ringwald     // close remote device db
12513a9fb326S[email protected]     if (hci_stack->remote_device_db) {
12523a9fb326S[email protected]         hci_stack->remote_device_db->close();
1253404843c1Smatthias.ringwald     }
12543a9fb326S[email protected]     while (hci_stack->connections) {
1255bc68afe2S[email protected]         // cancel all l2cap connections
1256bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
12573a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1258f5454fc6Smatthias.ringwald     }
1259f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
12603a9fb326S[email protected] 
12613a9fb326S[email protected] #ifdef HAVE_MALLOC
12623a9fb326S[email protected]     free(hci_stack);
12633a9fb326S[email protected] #endif
12643a9fb326S[email protected]     hci_stack = NULL;
1265404843c1Smatthias.ringwald }
1266404843c1Smatthias.ringwald 
12679e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
12689e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
12699e61646fS[email protected] }
12709e61646fS[email protected] 
127166fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
127266fb9560S[email protected]     disable_l2cap_timeouts = 1;
127366fb9560S[email protected] }
12748d213e1aSmatthias.ringwald // State-Module-Driver overview
12758d213e1aSmatthias.ringwald // state                    module  low-level
12768d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
12778d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
12788d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
12798d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1280d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1281d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1282c7e0c5f6Smatthias.ringwald 
128340d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
12847301ad89Smatthias.ringwald 
1285038bc64cSmatthias.ringwald     // power on
1286f9a30166Smatthias.ringwald     int err = 0;
12873a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
12883a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1289f9a30166Smatthias.ringwald     }
1290038bc64cSmatthias.ringwald     if (err){
12917d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1292038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1293038bc64cSmatthias.ringwald         return err;
1294038bc64cSmatthias.ringwald     }
1295038bc64cSmatthias.ringwald 
1296038bc64cSmatthias.ringwald     // open low-level device
12973a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1298038bc64cSmatthias.ringwald     if (err){
12997d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
13003a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
13013a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1302f9a30166Smatthias.ringwald         }
1303038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1304038bc64cSmatthias.ringwald         return err;
1305038bc64cSmatthias.ringwald     }
13068d213e1aSmatthias.ringwald     return 0;
13078d213e1aSmatthias.ringwald }
1308038bc64cSmatthias.ringwald 
130940d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
13108d213e1aSmatthias.ringwald 
13117b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
13129418f9c9Smatthias.ringwald 
13138d213e1aSmatthias.ringwald     // close low-level device
13143a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
13158d213e1aSmatthias.ringwald 
13167b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
13179418f9c9Smatthias.ringwald 
13188d213e1aSmatthias.ringwald     // power off
13193a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
13203a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
13218d213e1aSmatthias.ringwald     }
13229418f9c9Smatthias.ringwald 
13237b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
13249418f9c9Smatthias.ringwald 
13253a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
132672ea5239Smatthias.ringwald }
132772ea5239Smatthias.ringwald 
132840d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
132972ea5239Smatthias.ringwald 
13307b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
13313144bce4Smatthias.ringwald 
1332b429b9b7Smatthias.ringwald #if 0
1333b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1334b429b9b7Smatthias.ringwald 
133572ea5239Smatthias.ringwald     // close low-level device
13363a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1337b429b9b7Smatthias.ringwald #endif
133872ea5239Smatthias.ringwald 
133972ea5239Smatthias.ringwald     // sleep mode
13403a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
13413a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
134272ea5239Smatthias.ringwald     }
1343b429b9b7Smatthias.ringwald 
13443a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
13458d213e1aSmatthias.ringwald }
13468d213e1aSmatthias.ringwald 
134740d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1348b429b9b7Smatthias.ringwald 
13497b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1350b429b9b7Smatthias.ringwald 
1351b429b9b7Smatthias.ringwald     // wake on
13523a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
13533a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1354b429b9b7Smatthias.ringwald     }
1355b429b9b7Smatthias.ringwald 
1356b429b9b7Smatthias.ringwald #if 0
1357b429b9b7Smatthias.ringwald     // open low-level device
13583a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1359b429b9b7Smatthias.ringwald     if (err){
13607d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
13613a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
13623a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1363b429b9b7Smatthias.ringwald         }
1364b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1365b429b9b7Smatthias.ringwald         return err;
1366b429b9b7Smatthias.ringwald     }
1367b429b9b7Smatthias.ringwald #endif
1368b429b9b7Smatthias.ringwald 
1369b429b9b7Smatthias.ringwald     return 0;
1370b429b9b7Smatthias.ringwald }
1371b429b9b7Smatthias.ringwald 
137244935e40S[email protected] static void hci_power_transition_to_initializing(void){
137344935e40S[email protected]     // set up state machine
137444935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
137544935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
137644935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
137744935e40S[email protected]     hci_stack->substate = 0;
137844935e40S[email protected] }
1379b429b9b7Smatthias.ringwald 
13808d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1381d661ed19Smatthias.ringwald 
13823a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1383d661ed19Smatthias.ringwald 
13848d213e1aSmatthias.ringwald     int err = 0;
13853a9fb326S[email protected]     switch (hci_stack->state){
13868d213e1aSmatthias.ringwald 
13878d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
13888d213e1aSmatthias.ringwald             switch (power_mode){
13898d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
13908d213e1aSmatthias.ringwald                     err = hci_power_control_on();
139197b61c7bS[email protected]                     if (err) {
139297b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
139397b61c7bS[email protected]                         return err;
139497b61c7bS[email protected]                     }
139544935e40S[email protected]                     hci_power_transition_to_initializing();
13968d213e1aSmatthias.ringwald                     break;
13978d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
13988d213e1aSmatthias.ringwald                     // do nothing
13998d213e1aSmatthias.ringwald                     break;
14008d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1401b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
14028d213e1aSmatthias.ringwald                     break;
14038d213e1aSmatthias.ringwald             }
14048d213e1aSmatthias.ringwald             break;
14057301ad89Smatthias.ringwald 
14068d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
14078d213e1aSmatthias.ringwald             switch (power_mode){
14088d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14098d213e1aSmatthias.ringwald                     // do nothing
14108d213e1aSmatthias.ringwald                     break;
14118d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14128d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
14138d213e1aSmatthias.ringwald                     hci_power_control_off();
14148d213e1aSmatthias.ringwald                     break;
14158d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1416b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
141772ea5239Smatthias.ringwald                     hci_power_control_sleep();
14188d213e1aSmatthias.ringwald                     break;
14198d213e1aSmatthias.ringwald             }
14208d213e1aSmatthias.ringwald             break;
14217301ad89Smatthias.ringwald 
14228d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
14238d213e1aSmatthias.ringwald             switch (power_mode){
14248d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14258d213e1aSmatthias.ringwald                     // do nothing
14268d213e1aSmatthias.ringwald                     break;
14278d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1428c7e0c5f6Smatthias.ringwald                     // see hci_run
14293a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14308d213e1aSmatthias.ringwald                     break;
14318d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1432b546ac54Smatthias.ringwald                     // see hci_run
14333a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
14343a9fb326S[email protected]                     hci_stack->substate = 0;
14358d213e1aSmatthias.ringwald                     break;
14368d213e1aSmatthias.ringwald             }
14378d213e1aSmatthias.ringwald             break;
14387301ad89Smatthias.ringwald 
14398d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
14408d213e1aSmatthias.ringwald             switch (power_mode){
14418d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
144244935e40S[email protected]                     hci_power_transition_to_initializing();
14438d213e1aSmatthias.ringwald                     break;
14448d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14458d213e1aSmatthias.ringwald                     // do nothing
14468d213e1aSmatthias.ringwald                     break;
14478d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1448b546ac54Smatthias.ringwald                     // see hci_run
14493a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
14503a9fb326S[email protected]                     hci_stack->substate = 0;
14518d213e1aSmatthias.ringwald                     break;
14528d213e1aSmatthias.ringwald             }
14538d213e1aSmatthias.ringwald             break;
14548d213e1aSmatthias.ringwald 
14558d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
14568d213e1aSmatthias.ringwald             switch (power_mode){
14578d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
145828171530Smatthias.ringwald 
145928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
146028171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
146128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
14623a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
14633a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
146428171530Smatthias.ringwald                         break;
146528171530Smatthias.ringwald                     }
146628171530Smatthias.ringwald #endif
146744935e40S[email protected]                     hci_power_transition_to_initializing();
14688d213e1aSmatthias.ringwald                     break;
14698d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1470b546ac54Smatthias.ringwald                     // see hci_run
14713a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14728d213e1aSmatthias.ringwald                     break;
14738d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1474b546ac54Smatthias.ringwald                     // do nothing
14758d213e1aSmatthias.ringwald                     break;
14768d213e1aSmatthias.ringwald             }
14778d213e1aSmatthias.ringwald             break;
14788d213e1aSmatthias.ringwald 
14798d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
14808d213e1aSmatthias.ringwald             switch (power_mode){
14818d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
148228171530Smatthias.ringwald 
148328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
148428171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
148528171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
14863a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
14873a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1488758b46ceSmatthias.ringwald                         hci_update_scan_enable();
148928171530Smatthias.ringwald                         break;
149028171530Smatthias.ringwald                     }
149128171530Smatthias.ringwald #endif
14923144bce4Smatthias.ringwald                     err = hci_power_control_wake();
14933144bce4Smatthias.ringwald                     if (err) return err;
149444935e40S[email protected]                     hci_power_transition_to_initializing();
14958d213e1aSmatthias.ringwald                     break;
14968d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14973a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14988d213e1aSmatthias.ringwald                     break;
14998d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1500b546ac54Smatthias.ringwald                     // do nothing
15018d213e1aSmatthias.ringwald                     break;
15028d213e1aSmatthias.ringwald             }
15038d213e1aSmatthias.ringwald             break;
150411e23e5fSmatthias.ringwald     }
150568d92d03Smatthias.ringwald 
1506038bc64cSmatthias.ringwald     // create internal event
1507ee8bf225Smatthias.ringwald 	hci_emit_state();
1508ee8bf225Smatthias.ringwald 
150968d92d03Smatthias.ringwald 	// trigger next/first action
151068d92d03Smatthias.ringwald 	hci_run();
151168d92d03Smatthias.ringwald 
1512475c8125Smatthias.ringwald     return 0;
1513475c8125Smatthias.ringwald }
1514475c8125Smatthias.ringwald 
1515758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1516758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
15173a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1518758b46ceSmatthias.ringwald     hci_run();
1519758b46ceSmatthias.ringwald }
1520758b46ceSmatthias.ringwald 
1521381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1522381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1523381fbed8Smatthias.ringwald 
15243a9fb326S[email protected]     if (hci_stack->discoverable == enable){
15253a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1526381fbed8Smatthias.ringwald         return;
1527381fbed8Smatthias.ringwald     }
1528381fbed8Smatthias.ringwald 
15293a9fb326S[email protected]     hci_stack->discoverable = enable;
1530758b46ceSmatthias.ringwald     hci_update_scan_enable();
1531758b46ceSmatthias.ringwald }
1532b031bebbSmatthias.ringwald 
1533758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1534758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1535758b46ceSmatthias.ringwald 
1536758b46ceSmatthias.ringwald     // don't emit event
15373a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1538758b46ceSmatthias.ringwald 
15393a9fb326S[email protected]     hci_stack->connectable = enable;
1540758b46ceSmatthias.ringwald     hci_update_scan_enable();
1541381fbed8Smatthias.ringwald }
1542381fbed8Smatthias.ringwald 
15435061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
15443a9fb326S[email protected]     return &hci_stack->local_bd_addr;
15455061f3afS[email protected] }
15465061f3afS[email protected] 
154706b35ec0Smatthias.ringwald void hci_run(){
15488a485f27Smatthias.ringwald 
154932ab9390Smatthias.ringwald     hci_connection_t * connection;
155032ab9390Smatthias.ringwald     linked_item_t * it;
155132ab9390Smatthias.ringwald 
155264f0b431S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
1553ce4c8fabSmatthias.ringwald 
1554b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1555b031bebbSmatthias.ringwald 
1556b031bebbSmatthias.ringwald     // decline incoming connections
15573a9fb326S[email protected]     if (hci_stack->decline_reason){
15583a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
15593a9fb326S[email protected]         hci_stack->decline_reason = 0;
15603a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1561dbe1a790S[email protected]         return;
1562ce4c8fabSmatthias.ringwald     }
1563ce4c8fabSmatthias.ringwald 
1564b031bebbSmatthias.ringwald     // send scan enable
15653a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
15663a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
15673a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1568dbe1a790S[email protected]         return;
1569b031bebbSmatthias.ringwald     }
1570b031bebbSmatthias.ringwald 
1571b2f949feS[email protected] #ifdef HAVE_BLE
15727bdc6798S[email protected]     // handle le scan
15737bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
15747bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
15757bdc6798S[email protected]             case LE_START_SCAN:
15767bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
15777bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
15787bdc6798S[email protected]                 return;
15797bdc6798S[email protected] 
15807bdc6798S[email protected]             case LE_STOP_SCAN:
15817bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
15827bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
15837bdc6798S[email protected]                 return;
15847bdc6798S[email protected]             default:
15857bdc6798S[email protected]                 break;
15867bdc6798S[email protected]         }
1587e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
1588e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
1589e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
1590e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
1591e2602ea2Smatthias.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);
1592e2602ea2Smatthias.ringwald             return;
1593e2602ea2Smatthias.ringwald         }
15947bdc6798S[email protected]     }
1595b2f949feS[email protected] #endif
15967bdc6798S[email protected] 
159732ab9390Smatthias.ringwald     // send pending HCI commands
15983a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
1599b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
160032ab9390Smatthias.ringwald 
16010bf6344aS[email protected]         switch(connection->state){
16020bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
16034f3229d8S[email protected]                 switch(connection->address_type){
16044f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
1605564fca32S[email protected]                         log_info("sending hci_create_connection\n");
1606ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
16074f3229d8S[email protected]                         break;
16084f3229d8S[email protected]                     default:
1609b2f949feS[email protected] #ifdef HAVE_BLE
1610564fca32S[email protected]                         log_info("sending hci_le_create_connection\n");
16114f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
1612b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
1613b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
16144f3229d8S[email protected]                                      0,         // don't use whitelist
16154f3229d8S[email protected]                                      connection->address_type, // peer address type
16164f3229d8S[email protected]                                      connection->address,      // peer bd addr
1617b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
1618b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
1619b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
16204f3229d8S[email protected]                                      0,         // conn latency
1621b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
1622b2571179Smatthias.ringwald                                      0x0001,    // min ce length
1623b2571179Smatthias.ringwald                                      0x0001     // max ce length
16244f3229d8S[email protected]                                      );
16254f3229d8S[email protected] 
16264f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
1627b2f949feS[email protected] #endif
16284f3229d8S[email protected]                         break;
16294f3229d8S[email protected]                 }
1630ad83dc6aS[email protected]                 return;
1631ad83dc6aS[email protected] 
16320bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
16337b5fbe1fSmatthias.ringwald                 log_info("sending hci_accept_connection_request\n");
163432ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
163534d2123cS[email protected]                 hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1636dbe1a790S[email protected]                 return;
16370bf6344aS[email protected] 
1638a6725849S[email protected] #ifdef HAVE_BLE
16390bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
16400bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
16410bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
16420bf6344aS[email protected]                 return;
1643a6725849S[email protected] #endif
16440bf6344aS[email protected]             case SEND_DISCONNECT:
16450bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
16467851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
16470bf6344aS[email protected]                 return;
16480bf6344aS[email protected] 
16490bf6344aS[email protected]             default:
16500bf6344aS[email protected]                 break;
1651c7e0c5f6Smatthias.ringwald         }
1652c7e0c5f6Smatthias.ringwald 
165332ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
165434d2123cS[email protected]             log_info("responding to link key request\n");
165534d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
165632ab9390Smatthias.ringwald             link_key_t link_key;
1657c77e8838S[email protected]             link_key_type_t link_key_type;
16583a9fb326S[email protected]             if ( hci_stack->remote_device_db
16593a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
166034d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
16619ab95c90S[email protected]                connection->link_key_type = link_key_type;
166232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
166332ab9390Smatthias.ringwald             } else {
166432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
166532ab9390Smatthias.ringwald             }
1666dbe1a790S[email protected]             return;
166732ab9390Smatthias.ringwald         }
16681d6b20aeS[email protected] 
1669899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
16704c57c146S[email protected]             log_info("denying to pin request\n");
1671899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
167234d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
16734c57c146S[email protected]             return;
16744c57c146S[email protected]         }
16754c57c146S[email protected] 
1676dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
167734d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
167882d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
167982d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1680106d6d11S[email protected]                 // tweak authentication requirements
16813a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1682106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
16839faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
16849faad3abS[email protected]                 }
16859faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
16869faad3abS[email protected]                     authreq |= 1;
1687106d6d11S[email protected]                 }
16883a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1689f8fb5f6eS[email protected]             } else {
1690f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1691f8fb5f6eS[email protected]             }
1692dbe1a790S[email protected]             return;
169332ab9390Smatthias.ringwald         }
169432ab9390Smatthias.ringwald 
1695dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1696dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
169734d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1698dbe1a790S[email protected]             return;
1699dbe1a790S[email protected]         }
1700dbe1a790S[email protected] 
1701dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1702dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
170334d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1704dbe1a790S[email protected]             return;
1705dbe1a790S[email protected]         }
1706afd4e962S[email protected] 
1707afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1708afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
170934d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
17102bd8b7e7S[email protected]             return;
17112bd8b7e7S[email protected]         }
17122bd8b7e7S[email protected] 
17132bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
17142bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
171534d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
171634d2123cS[email protected]             return;
171734d2123cS[email protected]         }
1718ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1719ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
172052d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1721ad83dc6aS[email protected]             return;
1722ad83dc6aS[email protected]         }
172334d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
172434d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
172534d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
17262bd8b7e7S[email protected]             return;
1727afd4e962S[email protected]         }
1728dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1729dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1730dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1731dce78009S[email protected]             return;
1732dce78009S[email protected]         }
1733dbe1a790S[email protected]     }
1734c7e0c5f6Smatthias.ringwald 
17353a9fb326S[email protected]     switch (hci_stack->state){
17363429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
1737bd88fde9S[email protected]             hci_initializing_state_machine();
17383429f56bSmatthias.ringwald             break;
1739c7e0c5f6Smatthias.ringwald 
1740c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1741c7e0c5f6Smatthias.ringwald 
17427b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1743c7e0c5f6Smatthias.ringwald             // close all open connections
17443a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1745c7e0c5f6Smatthias.ringwald             if (connection){
174632ab9390Smatthias.ringwald 
1747c7e0c5f6Smatthias.ringwald                 // send disconnect
174864f0b431S[email protected]                 if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
174932ab9390Smatthias.ringwald 
175079bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
17516ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1752c7e0c5f6Smatthias.ringwald 
1753c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1754c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1755c7e0c5f6Smatthias.ringwald                 return;
1756c7e0c5f6Smatthias.ringwald             }
17577b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1758c7e0c5f6Smatthias.ringwald 
175972ea5239Smatthias.ringwald             // switch mode
1760c7e0c5f6Smatthias.ringwald             hci_power_control_off();
17619418f9c9Smatthias.ringwald 
17627b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
176372ea5239Smatthias.ringwald             hci_emit_state();
17647b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
176572ea5239Smatthias.ringwald             break;
1766c7e0c5f6Smatthias.ringwald 
176772ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
17683a9fb326S[email protected]             switch(hci_stack->substate) {
176989db417bSmatthias.ringwald                 case 0:
17707b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
177172ea5239Smatthias.ringwald                     // close all open connections
17723a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
177366da7044Smatthias.ringwald 
177428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
177566da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
177666da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
177766da7044Smatthias.ringwald                         connection = NULL;
177866da7044Smatthias.ringwald                     }
177966da7044Smatthias.ringwald #endif
178072ea5239Smatthias.ringwald                     if (connection){
178132ab9390Smatthias.ringwald 
178272ea5239Smatthias.ringwald                         // send disconnect
178332ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
178432ab9390Smatthias.ringwald 
178579bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
17866ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
178772ea5239Smatthias.ringwald 
178872ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
178972ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
179072ea5239Smatthias.ringwald                         return;
179172ea5239Smatthias.ringwald                     }
179272ea5239Smatthias.ringwald 
179392368cd3S[email protected]                     if (hci_classic_supported()){
179489db417bSmatthias.ringwald                         // disable page and inquiry scan
179564f0b431S[email protected]                         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
179632ab9390Smatthias.ringwald 
179792368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
17983a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
179989db417bSmatthias.ringwald 
180089db417bSmatthias.ringwald                         // continue in next sub state
18013a9fb326S[email protected]                         hci_stack->substate++;
180289db417bSmatthias.ringwald                         break;
180392368cd3S[email protected]                     }
180492368cd3S[email protected]                     // fall through for ble-only chips
180592368cd3S[email protected] 
180689db417bSmatthias.ringwald                 case 2:
18077b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
180828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
180928171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
181028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
181128171530Smatthias.ringwald                         // SLEEP MODE reached
18123a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
181328171530Smatthias.ringwald                         hci_emit_state();
181428171530Smatthias.ringwald                         break;
181528171530Smatthias.ringwald                     }
181628171530Smatthias.ringwald #endif
181772ea5239Smatthias.ringwald                     // switch mode
18183a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1819c7e0c5f6Smatthias.ringwald                     hci_emit_state();
182028171530Smatthias.ringwald                     break;
182128171530Smatthias.ringwald 
182289db417bSmatthias.ringwald                 default:
182389db417bSmatthias.ringwald                     break;
182489db417bSmatthias.ringwald             }
1825c7e0c5f6Smatthias.ringwald             break;
1826c7e0c5f6Smatthias.ringwald 
18273429f56bSmatthias.ringwald         default:
18283429f56bSmatthias.ringwald             break;
18291f504dbdSmatthias.ringwald     }
18303429f56bSmatthias.ringwald }
183116833f0aSmatthias.ringwald 
183231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1833c8e4258aSmatthias.ringwald     bd_addr_t addr;
1834c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1835c8e4258aSmatthias.ringwald     // house-keeping
1836c8e4258aSmatthias.ringwald 
1837c8e4258aSmatthias.ringwald     // create_connection?
1838c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1839c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1840339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1841c8e4258aSmatthias.ringwald 
184296a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1843ad83dc6aS[email protected]         if (!conn){
184496a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
184517f1ba2aSmatthias.ringwald             if (!conn){
184617f1ba2aSmatthias.ringwald                 // notify client that alloc failed
184717f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
184817f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
184917f1ba2aSmatthias.ringwald             }
1850ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1851ad83dc6aS[email protected]         }
1852ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1853ad83dc6aS[email protected]         switch (conn->state){
1854ad83dc6aS[email protected]             // if connection active exists
1855ad83dc6aS[email protected]             case OPEN:
185662bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1857ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
185862bda3fbS[email protected]                 return 0;
1859ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1860ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1861ad83dc6aS[email protected]                 break;
1862ad83dc6aS[email protected]             default:
1863ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1864ad83dc6aS[email protected]                 return 0;
1865ad83dc6aS[email protected]         }
1866c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1867c8e4258aSmatthias.ringwald     }
1868c8e4258aSmatthias.ringwald 
18697fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
18707fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
18717fde4af9Smatthias.ringwald     }
18727fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
18737fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
18747fde4af9Smatthias.ringwald     }
18757fde4af9Smatthias.ringwald 
18768ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
18773a9fb326S[email protected]         if (hci_stack->remote_device_db){
18788ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
18793a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
18808ef73945Smatthias.ringwald         }
18818ef73945Smatthias.ringwald     }
1882c8e4258aSmatthias.ringwald 
18836724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
18846724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
18856724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
188696a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
18876724cd9eS[email protected]         if (conn){
18886724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
18896724cd9eS[email protected]         }
18906724cd9eS[email protected]     }
18916724cd9eS[email protected] 
18926724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
18936724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
18946724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
18956724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
18966724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
189796a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
18986724cd9eS[email protected]         if (conn){
18996724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
19006724cd9eS[email protected]         }
19016724cd9eS[email protected]     }
19026724cd9eS[email protected] 
190369a97523S[email protected] #ifdef HAVE_BLE
190469a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
19053a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
190669a97523S[email protected]     }
190769a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
19083a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
190969a97523S[email protected]     }
191069a97523S[email protected] #endif
191169a97523S[email protected] 
19123a9fb326S[email protected]     hci_stack->num_cmd_packets--;
19136b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
19146b4af23dS[email protected] 
19156b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
1916c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
19176b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
19186b4af23dS[email protected]     }
19196b4af23dS[email protected] 
19206b4af23dS[email protected]     return err;
192131452debSmatthias.ringwald }
19228adf0ddaSmatthias.ringwald 
19232bd8b7e7S[email protected] // disconnect because of security block
19242bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
19252bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
19262bd8b7e7S[email protected]     if (!connection) return;
19272bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
19282bd8b7e7S[email protected] }
19292bd8b7e7S[email protected] 
19302bd8b7e7S[email protected] 
1931dbe1a790S[email protected] // Configure Secure Simple Pairing
1932dbe1a790S[email protected] 
1933dbe1a790S[email protected] // enable will enable SSP during init
1934dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
19353a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1936dbe1a790S[email protected] }
1937dbe1a790S[email protected] 
19382bd8b7e7S[email protected] int hci_local_ssp_activated(){
19393a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
19402bd8b7e7S[email protected] }
19412bd8b7e7S[email protected] 
1942dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1943dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
19443a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1945dbe1a790S[email protected] }
1946dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
19473a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1948dbe1a790S[email protected] }
1949dbe1a790S[email protected] 
1950dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1951dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
19523a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1953dbe1a790S[email protected] }
1954dbe1a790S[email protected] 
19551cd208adSmatthias.ringwald /**
19561cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
19571cd208adSmatthias.ringwald  */
1958fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
19595127cc62S[email protected] 
19605127cc62S[email protected]     // for HCI INITIALIZATION
19615127cc62S[email protected]     // printf("hci_send_cmd: opcode %04x\n", cmd->opcode);
19625127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
19635127cc62S[email protected] 
19641cd208adSmatthias.ringwald     va_list argptr;
19651cd208adSmatthias.ringwald     va_start(argptr, cmd);
19663a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
19671cd208adSmatthias.ringwald     va_end(argptr);
19683a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
196993b8dc03Smatthias.ringwald }
1970c8e4258aSmatthias.ringwald 
1971ee091cf1Smatthias.ringwald // Create various non-HCI events.
1972ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1973ee091cf1Smatthias.ringwald 
1974c8e4258aSmatthias.ringwald void hci_emit_state(){
19753a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1976425d1371Smatthias.ringwald     uint8_t event[3];
197780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1978425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
19793a9fb326S[email protected]     event[2] = hci_stack->state;
1980425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19813a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1982c8e4258aSmatthias.ringwald }
1983c8e4258aSmatthias.ringwald 
198417f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1985425d1371Smatthias.ringwald     uint8_t event[13];
1986c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1987425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
198817f1ba2aSmatthias.ringwald     event[2] = status;
1989c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1990c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1991c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1992c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1993425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
19943a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1995c8e4258aSmatthias.ringwald }
1996c8e4258aSmatthias.ringwald 
19974f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){
19984f3229d8S[email protected]     uint8_t event[21];
19994f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
20004f3229d8S[email protected]     event[1] = sizeof(event) - 2;
20014f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
20024f3229d8S[email protected]     event[3] = status;
20034f3229d8S[email protected]     bt_store_16(event, 4, conn->con_handle);
20044f3229d8S[email protected]     event[6] = 0; // TODO: role
20054f3229d8S[email protected]     event[7] = conn->address_type;
20064f3229d8S[email protected]     bt_flip_addr(&event[8], conn->address);
20074f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
20084f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
20094f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
20104f3229d8S[email protected]     event[20] = 0; // master clock accuracy
20114f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20124f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20134f3229d8S[email protected] }
20144f3229d8S[email protected] 
20153c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
2016425d1371Smatthias.ringwald     uint8_t event[6];
20173c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
2018e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
20193c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
20203c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
20213c4d4b90Smatthias.ringwald     event[5] = reason;
2022425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20233a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20243c4d4b90Smatthias.ringwald }
20253c4d4b90Smatthias.ringwald 
2026ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
202766fb9560S[email protected]     if (disable_l2cap_timeouts) return;
2028e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2029425d1371Smatthias.ringwald     uint8_t event[4];
203080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2031425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2032ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2033425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20343a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2035ee091cf1Smatthias.ringwald }
203643bfb1bdSmatthias.ringwald 
203743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
2038e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2039425d1371Smatthias.ringwald     uint8_t event[3];
204080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2041425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
204243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2043425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20443a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
204543bfb1bdSmatthias.ringwald }
2046038bc64cSmatthias.ringwald 
2047038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
2048e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2049425d1371Smatthias.ringwald     uint8_t event[2];
205080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2051425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2052425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20533a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2054038bc64cSmatthias.ringwald }
20551b0e3922Smatthias.ringwald 
205609ba8edeSmatthias.ringwald #ifndef EMBEDDED
20571b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
2058e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2059425d1371Smatthias.ringwald     uint8_t event[6];
20601b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2061425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2062425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2063425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
2064425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
2065425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20663a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20671b0e3922Smatthias.ringwald }
206809ba8edeSmatthias.ringwald #endif
20691b0e3922Smatthias.ringwald 
20702ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2071e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2072425d1371Smatthias.ringwald     uint8_t event[3];
20732ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2074425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
20752ed6235cSmatthias.ringwald     event[2] = enabled;
2076425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20773a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20782ed6235cSmatthias.ringwald }
2079627c2f45Smatthias.ringwald 
2080627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
2081e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2082627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2083e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2084f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
20852f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
2086f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2087e0abb8e7S[email protected] 
2088e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
2089e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
2090e0abb8e7S[email protected] 
2091e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
20923a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2093627c2f45Smatthias.ringwald }
2094381fbed8Smatthias.ringwald 
2095381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2096e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2097425d1371Smatthias.ringwald     uint8_t event[3];
2098381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2099425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2100381fbed8Smatthias.ringwald     event[2] = enabled;
2101425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21023a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2103381fbed8Smatthias.ringwald }
2104458bf4e8S[email protected] 
2105a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2106df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2107a00031e2S[email protected]     uint8_t event[5];
2108e00caf9cS[email protected]     int pos = 0;
2109a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2110e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2111a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2112e00caf9cS[email protected]     pos += 2;
2113e00caf9cS[email protected]     event[pos++] = level;
2114e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21153a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2116e00caf9cS[email protected] }
2117e00caf9cS[email protected] 
2118ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
2119ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2120ad83dc6aS[email protected]     uint8_t event[9];
2121ad83dc6aS[email protected]     int pos = 0;
2122ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2123ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2124ad83dc6aS[email protected]     event[pos++] = status;
2125ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
2126ad83dc6aS[email protected]     pos += 6;
2127ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21283a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2129ad83dc6aS[email protected] }
2130ad83dc6aS[email protected] 
21312bd8b7e7S[email protected] // query if remote side supports SSP
21322bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
21332bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
21342bd8b7e7S[email protected]     if (!connection) return 0;
21352bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
21362bd8b7e7S[email protected] }
21372bd8b7e7S[email protected] 
2138df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2139df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2140df3354fcS[email protected] }
2141df3354fcS[email protected] 
2142458bf4e8S[email protected] // GAP API
2143458bf4e8S[email protected] /**
2144458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2145458bf4e8S[email protected]  * @praram enabled
2146458bf4e8S[email protected]  */
21474c57c146S[email protected] void gap_set_bondable_mode(int enable){
21483a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2149458bf4e8S[email protected] }
2150cb230b9dS[email protected] 
2151cb230b9dS[email protected] /**
215234d2123cS[email protected]  * @brief map link keys to security levels
2153cb230b9dS[email protected]  */
215434d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
215534d2123cS[email protected]     switch (link_key_type){
21563c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
21573c68dfa9S[email protected]             return LEVEL_4;
21583c68dfa9S[email protected]         case COMBINATION_KEY:
21593c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
21603c68dfa9S[email protected]             return LEVEL_3;
21613c68dfa9S[email protected]         default:
21623c68dfa9S[email protected]             return LEVEL_2;
21633c68dfa9S[email protected]     }
2164cb230b9dS[email protected] }
2165cb230b9dS[email protected] 
2166a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
216734d2123cS[email protected]     if (!connection) return LEVEL_0;
216834d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
216934d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
217034d2123cS[email protected] }
217134d2123cS[email protected] 
217234d2123cS[email protected] 
2173106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
21745127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
2175106d6d11S[email protected]     return level > LEVEL_2;
2176106d6d11S[email protected] }
2177106d6d11S[email protected] 
217834d2123cS[email protected] /**
217934d2123cS[email protected]  * @brief get current security level
218034d2123cS[email protected]  */
218134d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
218234d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
218334d2123cS[email protected]     if (!connection) return LEVEL_0;
218434d2123cS[email protected]     return gap_security_level_for_connection(connection);
218534d2123cS[email protected] }
218634d2123cS[email protected] 
2187cb230b9dS[email protected] /**
2188cb230b9dS[email protected]  * @brief request connection to device to
2189cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2190cb230b9dS[email protected]  */
219134d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
219234d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
219334d2123cS[email protected]     if (!connection){
2194a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
219534d2123cS[email protected]         return;
219634d2123cS[email protected]     }
219734d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
219834d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
219934d2123cS[email protected]     if (current_level >= requested_level){
2200a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
220134d2123cS[email protected]         return;
220234d2123cS[email protected]     }
2203a00031e2S[email protected] 
220434d2123cS[email protected]     connection->requested_security_level = requested_level;
2205a00031e2S[email protected] 
2206fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
22073a9fb326S[email protected]     if (hci_stack->remote_device_db){
2208a00031e2S[email protected]         link_key_type_t link_key_type;
2209a00031e2S[email protected]         link_key_t      link_key;
22103a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2211a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2212a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2213a00031e2S[email protected]                 return;
2214a00031e2S[email protected]             }
2215a00031e2S[email protected]         }
2216a00031e2S[email protected]     }
2217a00031e2S[email protected] 
22181eb2563eS[email protected]     // try to authenticate connection
22191eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2220e80b2cf9S[email protected]     hci_run();
2221e00caf9cS[email protected] }
2222ad83dc6aS[email protected] 
2223ad83dc6aS[email protected] /**
2224ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2225ad83dc6aS[email protected]  * @param device
2226ad83dc6aS[email protected]  * @param request MITM protection
2227ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2228ad83dc6aS[email protected]  */
2229ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2230ad83dc6aS[email protected] 
2231ad83dc6aS[email protected]     // create connection state machine
223296a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2233ad83dc6aS[email protected] 
2234ad83dc6aS[email protected]     if (!connection){
2235ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2236ad83dc6aS[email protected]     }
2237ad83dc6aS[email protected] 
2238ad83dc6aS[email protected]     // delete linkn key
2239ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2240ad83dc6aS[email protected] 
2241ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2242ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2243ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
22445127cc62S[email protected]     log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level);
2245ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2246ad83dc6aS[email protected] 
2247ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2248ad83dc6aS[email protected] 
2249ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2250ad83dc6aS[email protected]     // handle: authentication failure
2251ad83dc6aS[email protected]     // handle: disconnect on done
2252ad83dc6aS[email protected] 
2253ad83dc6aS[email protected]     hci_run();
2254ad83dc6aS[email protected] 
2255ad83dc6aS[email protected]     return 0;
2256ad83dc6aS[email protected] }
22578e618f72S[email protected] 
22588e618f72S[email protected] void gap_set_local_name(const char * local_name){
22598e618f72S[email protected]     hci_stack->local_name = local_name;
22608e618f72S[email protected] }
22618e618f72S[email protected] 
22627bdc6798S[email protected] le_command_status_t le_central_start_scan(){
226336af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
22647bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
22657bdc6798S[email protected]     hci_run();
22667bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
22677bdc6798S[email protected] }
22688e618f72S[email protected] 
22697bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
227036af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
22717bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
22727bdc6798S[email protected]     hci_run();
22737bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
22747bdc6798S[email protected] }
22754f3229d8S[email protected] 
2276ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2277ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2278ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2279ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2280ef11999fSmatthias.ringwald     hci_run();
2281ef11999fSmatthias.ringwald }
22824f3229d8S[email protected] 
22834f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){
22844f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
22854f3229d8S[email protected]     if (!conn){
22861f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
22874f3229d8S[email protected]         conn = create_connection_for_bd_addr_and_type(*addr, addr_type);
22884f3229d8S[email protected]         if (!conn){
22894f3229d8S[email protected]             // notify client that alloc failed
22904f3229d8S[email protected]             hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
22911f479f8cS[email protected]             log_info("le_central_connect: failed to alloc context");
22924f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
22934f3229d8S[email protected]         }
22944f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
22951f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2296564fca32S[email protected]         hci_run();
22974f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
22984f3229d8S[email protected]     }
22990bf6344aS[email protected] 
23000bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
23010bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
23020bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
23030bf6344aS[email protected]         hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED);
23041f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
23050bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
23060bf6344aS[email protected]     }
23070bf6344aS[email protected] 
23081f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
23094f3229d8S[email protected]     hci_emit_le_connection_complete(conn, 0);
23104f3229d8S[email protected]     hci_run();
23114f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
23124f3229d8S[email protected] }
23134f3229d8S[email protected] 
23147851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
23157851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){
23160bf6344aS[email protected]     linked_item_t *it;
23170bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
23180bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
23190bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
23200bf6344aS[email protected]         switch (conn->state){
2321a6725849S[email protected]             case SEND_CREATE_CONNECTION:
23227851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
23237851196eSmatthias.ringwald                 return conn;
23247851196eSmatthias.ringwald             default:
23257851196eSmatthias.ringwald                 break;
23267851196eSmatthias.ringwald         };
23277851196eSmatthias.ringwald     }
23287851196eSmatthias.ringwald     return NULL;
23297851196eSmatthias.ringwald }
23307851196eSmatthias.ringwald 
23317851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){
23327851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
23337851196eSmatthias.ringwald     switch (conn->state){
23347851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
23357851196eSmatthias.ringwald             // skip sending create connection and emit event instead
23360bf6344aS[email protected]             hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
23377851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
23387851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
23390bf6344aS[email protected]             break;
2340a6725849S[email protected]         case SENT_CREATE_CONNECTION:
23417851196eSmatthias.ringwald             // request to send cancel connection
23420bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
23430bf6344aS[email protected]             hci_run();
23440bf6344aS[email protected]             break;
23450bf6344aS[email protected]         default:
23460bf6344aS[email protected]             break;
23470bf6344aS[email protected]     }
2348e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2349e31f89a7S[email protected] }
23504f3229d8S[email protected] 
23515917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
23525917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
23535917a5c5S[email protected]     if (!conn){
23547851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
23555917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
23565917a5c5S[email protected]     }
23575917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
23585917a5c5S[email protected]     hci_run();
23594f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
23604f3229d8S[email protected] }
236104a6ef8cSmatthias.ringwald 
236204a6ef8cSmatthias.ringwald void hci_disconnect_all(){
236304a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
236404a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
236504a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
236604a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
236704a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
236804a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
236904a6ef8cSmatthias.ringwald     }
2370d31fba26S[email protected]     hci_run();
237104a6ef8cSmatthias.ringwald }
2372