xref: /btstack/src/hci.c (revision 6155b3d3f9769fb9b6898a2627629d0df76798d6)
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);
78*6155b3d3S[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] 
564*6155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){
565*6155b3d3S[email protected]     if (hci_stack->substate % 2){
566*6155b3d3S[email protected]         // odd: waiting for event
567*6155b3d3S[email protected]         if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
568*6155b3d3S[email protected]             uint16_t opcode = READ_BT_16(packet,3);
569*6155b3d3S[email protected]             if (opcode == hci_stack->last_cmd_opcode){
570*6155b3d3S[email protected]                 hci_stack->substate++;
571*6155b3d3S[email protected]                 log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate);
572*6155b3d3S[email protected]             } else {
573*6155b3d3S[email protected]                 log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
574*6155b3d3S[email protected]             }
575*6155b3d3S[email protected]         }
576*6155b3d3S[email protected]         if (packet[0] == HCI_EVENT_COMMAND_STATUS){
577*6155b3d3S[email protected]             uint8_t  status = packet[2];
578*6155b3d3S[email protected]             uint16_t opcode = READ_BT_16(packet,4);
579*6155b3d3S[email protected]             if (opcode == hci_stack->last_cmd_opcode){
580*6155b3d3S[email protected]                 if (status){
581*6155b3d3S[email protected]                     hci_stack->substate++;
582*6155b3d3S[email protected]                     log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate);
583*6155b3d3S[email protected]                 } else {
584*6155b3d3S[email protected]                     log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode);
585*6155b3d3S[email protected]                 }
586*6155b3d3S[email protected]             } else {
587*6155b3d3S[email protected]                 log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode);
588*6155b3d3S[email protected]             }
589*6155b3d3S[email protected]         }
590*6155b3d3S[email protected]     }
591*6155b3d3S[email protected] }
592*6155b3d3S[email protected] 
593*6155b3d3S[email protected] static void hci_initializing_state_machine(){
594*6155b3d3S[email protected]         // log_info("hci_init: substate %u\n", hci_stack->substate);
595*6155b3d3S[email protected]     if (hci_stack->substate % 2) {
596*6155b3d3S[email protected]         // odd: waiting for command completion
597*6155b3d3S[email protected]         return;
598*6155b3d3S[email protected]     }
599*6155b3d3S[email protected]     switch (hci_stack->substate >> 1){
600*6155b3d3S[email protected]         case 0: // RESET
601*6155b3d3S[email protected]             hci_state_reset();
602*6155b3d3S[email protected] 
603*6155b3d3S[email protected]             hci_send_cmd(&hci_reset);
604*6155b3d3S[email protected]             if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
605*6155b3d3S[email protected]                 // skip baud change
606*6155b3d3S[email protected]                 hci_stack->substate = 4; // >> 1 = 2
607*6155b3d3S[email protected]             }
608*6155b3d3S[email protected]             break;
609*6155b3d3S[email protected]         case 1: // SEND BAUD CHANGE
610*6155b3d3S[email protected]             hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
611*6155b3d3S[email protected]             hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
612*6155b3d3S[email protected]             break;
613*6155b3d3S[email protected]         case 2: // LOCAL BAUD CHANGE
614*6155b3d3S[email protected]             log_info("Local baud rate change");
615*6155b3d3S[email protected]             hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
616*6155b3d3S[email protected]             hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
617*6155b3d3S[email protected]             hci_stack->substate += 2;
618*6155b3d3S[email protected]             // break missing here for fall through
619*6155b3d3S[email protected] 
620*6155b3d3S[email protected]         case 3:
621*6155b3d3S[email protected]             log_info("Custom init");
622*6155b3d3S[email protected]             // Custom initialization
623*6155b3d3S[email protected]             if (hci_stack->control && hci_stack->control->next_cmd){
624*6155b3d3S[email protected]                 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
625*6155b3d3S[email protected]                 if (valid_cmd){
626*6155b3d3S[email protected]                     int size = 3 + hci_stack->hci_packet_buffer[2];
627*6155b3d3S[email protected]                     hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
628*6155b3d3S[email protected]                     hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
629*6155b3d3S[email protected]                     hci_stack->substate = 4; // more init commands
630*6155b3d3S[email protected]                     break;
631*6155b3d3S[email protected]                 }
632*6155b3d3S[email protected]                 log_info("hci_run: init script done\n\r");
633*6155b3d3S[email protected]             }
634*6155b3d3S[email protected]             // otherwise continue
635*6155b3d3S[email protected]             hci_send_cmd(&hci_read_bd_addr);
636*6155b3d3S[email protected]             break;
637*6155b3d3S[email protected]         case 4:
638*6155b3d3S[email protected]             hci_send_cmd(&hci_read_buffer_size);
639*6155b3d3S[email protected]             break;
640*6155b3d3S[email protected]         case 5:
641*6155b3d3S[email protected]             hci_send_cmd(&hci_read_local_supported_features);
642*6155b3d3S[email protected]             break;
643*6155b3d3S[email protected]         case 6:
644*6155b3d3S[email protected]             if (hci_le_supported()){
645*6155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
646*6155b3d3S[email protected]             } else {
647*6155b3d3S[email protected]                 // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
648*6155b3d3S[email protected]                 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
649*6155b3d3S[email protected]             }
650*6155b3d3S[email protected] 
651*6155b3d3S[email protected]             // skip Classic init commands for LE only chipsets
652*6155b3d3S[email protected]             if (!hci_classic_supported()){
653*6155b3d3S[email protected]                 if (hci_le_supported()){
654*6155b3d3S[email protected]                     hci_stack->substate = 11 << 1;    // skip all classic command
655*6155b3d3S[email protected]                 } else {
656*6155b3d3S[email protected]                     log_error("Neither BR/EDR nor LE supported");
657*6155b3d3S[email protected]                     hci_stack->substate = 14 << 1;    // skip all
658*6155b3d3S[email protected]                 }
659*6155b3d3S[email protected]             }
660*6155b3d3S[email protected]             break;
661*6155b3d3S[email protected]         case 7:
662*6155b3d3S[email protected]             if (hci_ssp_supported()){
663*6155b3d3S[email protected]                 hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
664*6155b3d3S[email protected]                 break;
665*6155b3d3S[email protected]             }
666*6155b3d3S[email protected]             hci_stack->substate += 2;
667*6155b3d3S[email protected]             // break missing here for fall through
668*6155b3d3S[email protected] 
669*6155b3d3S[email protected]         case 8:
670*6155b3d3S[email protected]             // ca. 15 sec
671*6155b3d3S[email protected]             hci_send_cmd(&hci_write_page_timeout, 0x6000);
672*6155b3d3S[email protected]             break;
673*6155b3d3S[email protected]         case 9:
674*6155b3d3S[email protected]             hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
675*6155b3d3S[email protected]             break;
676*6155b3d3S[email protected]         case 10:
677*6155b3d3S[email protected]             if (hci_stack->local_name){
678*6155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
679*6155b3d3S[email protected]             } else {
680*6155b3d3S[email protected]                 char hostname[30];
681*6155b3d3S[email protected] #ifdef EMBEDDED
682*6155b3d3S[email protected]                 // BTstack-11:22:33:44:55:66
683*6155b3d3S[email protected]                 strcpy(hostname, "BTstack ");
684*6155b3d3S[email protected]                 strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
685*6155b3d3S[email protected]                 log_info("---> Name %s", hostname);
686*6155b3d3S[email protected] #else
687*6155b3d3S[email protected]                 // hostname for POSIX systems
688*6155b3d3S[email protected]                 gethostname(hostname, 30);
689*6155b3d3S[email protected]                 hostname[29] = '\0';
690*6155b3d3S[email protected] #endif
691*6155b3d3S[email protected]                 hci_send_cmd(&hci_write_local_name, hostname);
692*6155b3d3S[email protected]             }
693*6155b3d3S[email protected]             break;
694*6155b3d3S[email protected]         case 11:
695*6155b3d3S[email protected]             hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
696*6155b3d3S[email protected]             if (!hci_le_supported()){
697*6155b3d3S[email protected]                 // SKIP LE init for Classic only configuration
698*6155b3d3S[email protected]                 hci_stack->substate = 14 << 1;
699*6155b3d3S[email protected]             }
700*6155b3d3S[email protected]             break;
701*6155b3d3S[email protected] 
702*6155b3d3S[email protected] #ifdef HAVE_BLE
703*6155b3d3S[email protected]         // LE INIT
704*6155b3d3S[email protected]         case 12:
705*6155b3d3S[email protected]             hci_send_cmd(&hci_le_read_buffer_size);
706*6155b3d3S[email protected]             break;
707*6155b3d3S[email protected]         case 13:
708*6155b3d3S[email protected]             // LE Supported Host = 1, Simultaneous Host = 0
709*6155b3d3S[email protected]             hci_send_cmd(&hci_write_le_host_supported, 1, 0);
710*6155b3d3S[email protected]             break;
711*6155b3d3S[email protected]         case 14:
712*6155b3d3S[email protected]             // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs
713*6155b3d3S[email protected]             hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0);
714*6155b3d3S[email protected]             break;
715*6155b3d3S[email protected] #endif
716*6155b3d3S[email protected] 
717*6155b3d3S[email protected]         // DONE
718*6155b3d3S[email protected]         case 15:
719*6155b3d3S[email protected]             // done.
720*6155b3d3S[email protected]             hci_stack->state = HCI_STATE_WORKING;
721*6155b3d3S[email protected]             hci_emit_state();
722*6155b3d3S[email protected]             break;
723*6155b3d3S[email protected]         default:
724*6155b3d3S[email protected]             break;
725*6155b3d3S[email protected]     }
726*6155b3d3S[email protected]     hci_stack->substate++;
727*6155b3d3S[email protected] }
728*6155b3d3S[email protected] 
72974ec757aSmatthias.ringwald // avoid huge local variables
730223aafc1Smatthias.ringwald #ifndef EMBEDDED
73174ec757aSmatthias.ringwald static device_name_t device_name;
732223aafc1Smatthias.ringwald #endif
73316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
734e76a89eeS[email protected] 
735e76a89eeS[email protected]     uint16_t event_length = packet[1];
736e76a89eeS[email protected] 
737e76a89eeS[email protected]     // assert packet is complete
738e76a89eeS[email protected]     if (size != event_length + 2){
739e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
740e76a89eeS[email protected]         return;
741e76a89eeS[email protected]     }
742e76a89eeS[email protected] 
7431281a47eSmatthias.ringwald     bd_addr_t addr;
74496a45072S[email protected]     bd_addr_type_t addr_type;
7452d00edd4Smatthias.ringwald     uint8_t link_type;
746fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
7471f7b95a1Smatthias.ringwald     hci_connection_t * conn;
74856cf178bSmatthias.ringwald     int i;
74922909952Smatthias.ringwald 
7505909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
7515909f7f2Smatthias.ringwald 
7526772a24cSmatthias.ringwald     switch (packet[0]) {
75322909952Smatthias.ringwald 
7546772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
7557ec5eeaaSmatthias.ringwald             // get num cmd packets
7563a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
7573a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
7587ec5eeaaSmatthias.ringwald 
759e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
760e2edc0c3Smatthias.ringwald                 // from offset 5
761e2edc0c3Smatthias.ringwald                 // status
7621d279b20Smatthias.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"
7633a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
76456cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
7653a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
76656cf178bSmatthias.ringwald                 // ignore: total num SCO packets
7673a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
768a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
7693a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
7703a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7718f8108aaSmatthias.ringwald                     }
77265389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
7733a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
774e2edc0c3Smatthias.ringwald                 }
77556cf178bSmatthias.ringwald             }
77665a46ef3S[email protected] #ifdef HAVE_BLE
77765a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
7783a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
7793a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
7803a9fb326S[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);
78101f1657cS[email protected] 
78201f1657cS[email protected]                 // use LE buffers if no clasic buffers have been reported
78301f1657cS[email protected]                 if (hci_stack->total_num_acl_packets == 0){
78401f1657cS[email protected]                     log_info("use le buffers instead of classic ones");
78501f1657cS[email protected]                     hci_stack->total_num_acl_packets  = hci_stack->total_num_le_packets;
78601f1657cS[email protected]                     hci_stack->acl_data_packet_length = hci_stack->le_data_packet_length;
78701f1657cS[email protected]                     // determine usable ACL payload size
78801f1657cS[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
78901f1657cS[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
79001f1657cS[email protected]                     }
79101f1657cS[email protected]                 }
79265a46ef3S[email protected]             }
79365a46ef3S[email protected] #endif
794188981d2Smatthias.ringwald             // Dump local address
795188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
7963a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
797188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
7983a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
799188981d2Smatthias.ringwald             }
800381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
8013a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
802381fbed8Smatthias.ringwald             }
80365389bfcS[email protected]             // Note: HCI init checks
804559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
8053a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
806559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
8073a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
8083a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
8093a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
8103a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
81165389bfcS[email protected] 
81265389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
8133a9fb326S[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]);
8143a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
815f5d8d141S[email protected] 
816f5d8d141S[email protected]                 // Classic/LE
817f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
818559e517eS[email protected]             }
81956cf178bSmatthias.ringwald             break;
82056cf178bSmatthias.ringwald 
8217ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
8227ec5eeaaSmatthias.ringwald             // get num cmd packets
8233a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
8243a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
8257ec5eeaaSmatthias.ringwald             break;
8267ec5eeaaSmatthias.ringwald 
82756cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
82856cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
82956cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
83056cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
8315061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
83256cf178bSmatthias.ringwald                 if (!conn){
8337d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
83456cf178bSmatthias.ringwald                     continue;
83556cf178bSmatthias.ringwald                 }
83656cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
8377b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
83856cf178bSmatthias.ringwald             }
8396772a24cSmatthias.ringwald             break;
8406772a24cSmatthias.ringwald 
8411f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
84237eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
84337eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
8442d00edd4Smatthias.ringwald             link_type = packet[11];
845339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
84637eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
84796a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
8481f7b95a1Smatthias.ringwald                 if (!conn) {
84996a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
8501f7b95a1Smatthias.ringwald                 }
851ce4c8fabSmatthias.ringwald                 if (!conn) {
852ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
8533a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
8543a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
855ce4c8fabSmatthias.ringwald                     break;
856ce4c8fabSmatthias.ringwald                 }
85732ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
85832ab9390Smatthias.ringwald                 hci_run();
85937eaa4cfSmatthias.ringwald             } else {
860ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
8613a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
8623a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
86337eaa4cfSmatthias.ringwald             }
8641f7b95a1Smatthias.ringwald             break;
8651f7b95a1Smatthias.ringwald 
8666772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
867fe1ed1b8Smatthias.ringwald             // Connection management
868fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
869339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
87096a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
87196a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
872fe1ed1b8Smatthias.ringwald             if (conn) {
873b448a0e7Smatthias.ringwald                 if (!packet[2]){
874c8e4258aSmatthias.ringwald                     conn->state = OPEN;
875fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
876ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
877ee091cf1Smatthias.ringwald 
878c785ef68Smatthias.ringwald                     // restart timer
879c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
880ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
881c785ef68Smatthias.ringwald 
882339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
88343bfb1bdSmatthias.ringwald 
88443bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
885b448a0e7Smatthias.ringwald                 } else {
886ad83dc6aS[email protected]                     // notify client if dedicated bonding
887ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
888ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
889ad83dc6aS[email protected]                     }
890ad83dc6aS[email protected] 
891b448a0e7Smatthias.ringwald                     // connection failed, remove entry
8923a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
893a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
894c12e46e7Smatthias.ringwald 
895c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
896c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
897c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
898c12e46e7Smatthias.ringwald                     }
899fe1ed1b8Smatthias.ringwald                 }
900fe1ed1b8Smatthias.ringwald             }
9016772a24cSmatthias.ringwald             break;
902fe1ed1b8Smatthias.ringwald 
903afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
904afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
905afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
906afd4e962S[email protected]             if (!conn) break;
907afd4e962S[email protected]             if (!packet[2]){
908afd4e962S[email protected]                 uint8_t * features = &packet[5];
909afd4e962S[email protected]                 if (features[6] & (1 << 3)){
910afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
911afd4e962S[email protected]                 }
912afd4e962S[email protected]             }
913afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
914ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
915ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
916ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
917ad83dc6aS[email protected]             }
918afd4e962S[email protected]             break;
919afd4e962S[email protected] 
9207fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
9217b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
9227fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
92374d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
9243a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
92532ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
92664472d52Smatthias.ringwald             hci_run();
927d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
92829d53098Smatthias.ringwald             return;
9297fde4af9Smatthias.ringwald 
9309ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
93129d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
93296a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
9339ab95c90S[email protected]             if (!conn) break;
9349ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
9357bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
9369ab95c90S[email protected]             // Change Connection Encryption keeps link key type
9379ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
9389ab95c90S[email protected]                 conn->link_key_type = link_key_type;
9399ab95c90S[email protected]             }
9403a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
9413a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
94229d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
9437fde4af9Smatthias.ringwald             break;
9449ab95c90S[email protected]         }
9457fde4af9Smatthias.ringwald 
9467fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
9476724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
9484c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
9493a9fb326S[email protected]             if (!hci_stack->bondable){
950899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
951f8fb5f6eS[email protected]                 hci_run();
952f8fb5f6eS[email protected]                 return;
9534c57c146S[email protected]             }
954d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
9553a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
956d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
9573a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
9587fde4af9Smatthias.ringwald             break;
9597fde4af9Smatthias.ringwald 
9601d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
9611d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
962dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
963dbe1a790S[email protected]             break;
964dbe1a790S[email protected] 
965dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
9666724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
9673a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
968dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
969dbe1a790S[email protected]             break;
970dbe1a790S[email protected] 
971dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
9726724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
9733a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
974dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
9751d6b20aeS[email protected]             break;
9761d6b20aeS[email protected] 
977f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
978f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
979f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
980f0944df2S[email protected]             if (!conn) break;
981ad83dc6aS[email protected]             if (packet[2] == 0) {
982f0944df2S[email protected]                 if (packet[5]){
983f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
984f0944df2S[email protected]                 } else {
985536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
986f0944df2S[email protected]                 }
987ad83dc6aS[email protected]             }
988a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
989f0944df2S[email protected]             break;
990f0944df2S[email protected] 
9911eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
9921eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
9931eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
9941eb2563eS[email protected]             if (!conn) break;
995ad83dc6aS[email protected] 
996ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
997ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
998ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
999ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
1000ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
1001ad83dc6aS[email protected]                 break;
1002ad83dc6aS[email protected]             }
1003ad83dc6aS[email protected] 
1004b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
10051eb2563eS[email protected]                 // link key sufficient for requested security
10061eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1007ad83dc6aS[email protected]                 break;
1008ad83dc6aS[email protected]             }
10091eb2563eS[email protected]             // not enough
10101eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
10111eb2563eS[email protected]             break;
101234d2123cS[email protected] 
1013223aafc1Smatthias.ringwald #ifndef EMBEDDED
101474ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
10153a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
101674ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
101774ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10185a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
10195a06394aSmatthias.ringwald             for (i=0; i<248;i++){
10205a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
10215a06394aSmatthias.ringwald                     packet[9+i] = 0;
10225a06394aSmatthias.ringwald                     break;
1023cdc9101dSmatthias.ringwald                 }
10245a06394aSmatthias.ringwald             }
1025d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
102674ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
10273a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
102874ec757aSmatthias.ringwald             break;
102974ec757aSmatthias.ringwald 
103074ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
103174ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
10323a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
103374ec757aSmatthias.ringwald             // first send inq result packet
10343a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
103574ec757aSmatthias.ringwald             // then send cached remote names
103674ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
103774ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
10383a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
103974ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
104074ec757aSmatthias.ringwald                 }
104174ec757aSmatthias.ringwald             }
104274ec757aSmatthias.ringwald             return;
1043223aafc1Smatthias.ringwald #endif
104474ec757aSmatthias.ringwald 
10456772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1046fe1ed1b8Smatthias.ringwald             if (!packet[2]){
1047fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
10485061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
1049fe1ed1b8Smatthias.ringwald                 if (conn) {
1050c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
1051fe1ed1b8Smatthias.ringwald                 }
1052fe1ed1b8Smatthias.ringwald             }
10536772a24cSmatthias.ringwald             break;
10546772a24cSmatthias.ringwald 
1055c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
10563a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
10573a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
10587586ee35S[email protected]             } else {
10597586ee35S[email protected]                 // if no special requests, just reboot stack
10607586ee35S[email protected]                 hci_power_control_off();
10617586ee35S[email protected]                 hci_power_control_on();
1062c68bdf90Smatthias.ringwald             }
1063c68bdf90Smatthias.ringwald             break;
1064c68bdf90Smatthias.ringwald 
10656b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
10666b4af23dS[email protected]             // free packet buffer for asynchronous transport
10676b4af23dS[email protected]             if (hci_transport_synchronous()) break;
10686b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
10696b4af23dS[email protected]             break;
10706b4af23dS[email protected] 
10715909f7f2Smatthias.ringwald #ifdef HAVE_BLE
10725909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
10735909f7f2Smatthias.ringwald             switch (packet[2]){
107457c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
10757bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
107657c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
10777bdc6798S[email protected]                     break;
10785909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
10795909f7f2Smatthias.ringwald                     // Connection management
10805909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
10817bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
108296a45072S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr));
10835909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
108496a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
10855909f7f2Smatthias.ringwald                     if (packet[3]){
10865909f7f2Smatthias.ringwald                         if (conn){
10875909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
10883a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
10895909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
10905909f7f2Smatthias.ringwald                         }
10915909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
10925909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
10935909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
10945909f7f2Smatthias.ringwald                         }
10955909f7f2Smatthias.ringwald                         break;
10965909f7f2Smatthias.ringwald                     }
10975909f7f2Smatthias.ringwald                     if (!conn){
109896a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
10995909f7f2Smatthias.ringwald                     }
11005909f7f2Smatthias.ringwald                     if (!conn){
11015909f7f2Smatthias.ringwald                         // no memory
11025909f7f2Smatthias.ringwald                         break;
11035909f7f2Smatthias.ringwald                     }
11045909f7f2Smatthias.ringwald 
11055909f7f2Smatthias.ringwald                     conn->state = OPEN;
11065909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
11075909f7f2Smatthias.ringwald 
11085909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
11095909f7f2Smatthias.ringwald 
11105909f7f2Smatthias.ringwald                     // restart timer
11115909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
11125909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
11135909f7f2Smatthias.ringwald 
11145909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
11155909f7f2Smatthias.ringwald 
11165909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
11175909f7f2Smatthias.ringwald                     break;
11185909f7f2Smatthias.ringwald 
111965a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
112065a46ef3S[email protected] 
11215909f7f2Smatthias.ringwald                 default:
11225909f7f2Smatthias.ringwald                     break;
11235909f7f2Smatthias.ringwald             }
11245909f7f2Smatthias.ringwald             break;
11255909f7f2Smatthias.ringwald #endif
11266772a24cSmatthias.ringwald         default:
11276772a24cSmatthias.ringwald             break;
1128fe1ed1b8Smatthias.ringwald     }
1129fe1ed1b8Smatthias.ringwald 
11303429f56bSmatthias.ringwald     // handle BT initialization
11313a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
1132*6155b3d3S[email protected]         hci_initializing_event_handler(packet, size);
1133c9af4d3fS[email protected]     }
113422909952Smatthias.ringwald 
113589db417bSmatthias.ringwald     // help with BT sleep
11363a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
11373a9fb326S[email protected]         && hci_stack->substate == 1
113889db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
11393a9fb326S[email protected]         hci_stack->substate++;
114089db417bSmatthias.ringwald     }
114189db417bSmatthias.ringwald 
11423a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
114394ab26f8Smatthias.ringwald 
114494ab26f8Smatthias.ringwald 	// execute main loop
114594ab26f8Smatthias.ringwald 	hci_run();
114616833f0aSmatthias.ringwald }
114716833f0aSmatthias.ringwald 
11480a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
114910e830c9Smatthias.ringwald     switch (packet_type) {
115010e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
115110e830c9Smatthias.ringwald             event_handler(packet, size);
115210e830c9Smatthias.ringwald             break;
115310e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
115410e830c9Smatthias.ringwald             acl_handler(packet, size);
115510e830c9Smatthias.ringwald             break;
115610e830c9Smatthias.ringwald         default:
115710e830c9Smatthias.ringwald             break;
115810e830c9Smatthias.ringwald     }
115910e830c9Smatthias.ringwald }
116010e830c9Smatthias.ringwald 
1161fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
11622718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
11633a9fb326S[email protected]     hci_stack->packet_handler = handler;
116416833f0aSmatthias.ringwald }
116516833f0aSmatthias.ringwald 
1166*6155b3d3S[email protected] static void hci_state_reset(){
1167595bdbfbS[email protected]     // no connections yet
1168595bdbfbS[email protected]     hci_stack->connections = NULL;
116974308b23Smatthias.ringwald 
117074308b23Smatthias.ringwald     // keep discoverable/connectable as this has been requested by the client(s)
117174308b23Smatthias.ringwald     // hci_stack->discoverable = 0;
117274308b23Smatthias.ringwald     // hci_stack->connectable = 0;
117374308b23Smatthias.ringwald     // hci_stack->bondable = 1;
1174595bdbfbS[email protected] 
117544935e40S[email protected]     // buffer is free
117644935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
117744935e40S[email protected] 
1178595bdbfbS[email protected]     // no pending cmds
1179595bdbfbS[email protected]     hci_stack->decline_reason = 0;
1180595bdbfbS[email protected]     hci_stack->new_scan_enable_value = 0xff;
1181595bdbfbS[email protected] 
1182595bdbfbS[email protected]     // LE
1183595bdbfbS[email protected]     hci_stack->adv_addr_type = 0;
1184595bdbfbS[email protected]     memset(hci_stack->adv_address, 0, 6);
1185595bdbfbS[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1186e2602ea2Smatthias.ringwald     hci_stack->le_scan_type = 0xff;
1187595bdbfbS[email protected] }
1188595bdbfbS[email protected] 
11894f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
1190475c8125Smatthias.ringwald 
11913a9fb326S[email protected] #ifdef HAVE_MALLOC
11923a9fb326S[email protected]     if (!hci_stack) {
11933a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
11943a9fb326S[email protected]     }
11953a9fb326S[email protected] #else
11963a9fb326S[email protected]     hci_stack = &hci_stack_static;
11973a9fb326S[email protected] #endif
119866fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
11993a9fb326S[email protected] 
1200475c8125Smatthias.ringwald     // reference to use transport layer implementation
12013a9fb326S[email protected]     hci_stack->hci_transport = transport;
1202475c8125Smatthias.ringwald 
120311e23e5fSmatthias.ringwald     // references to used control implementation
12043a9fb326S[email protected]     hci_stack->control = control;
120511e23e5fSmatthias.ringwald 
120611e23e5fSmatthias.ringwald     // reference to used config
12073a9fb326S[email protected]     hci_stack->config = config;
120811e23e5fSmatthias.ringwald 
120916833f0aSmatthias.ringwald     // higher level handler
12103a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
121116833f0aSmatthias.ringwald 
1212404843c1Smatthias.ringwald     // store and open remote device db
12133a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
12143a9fb326S[email protected]     if (hci_stack->remote_device_db) {
12153a9fb326S[email protected]         hci_stack->remote_device_db->open();
1216404843c1Smatthias.ringwald     }
121729d53098Smatthias.ringwald 
12188fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
12193a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
12208fcba05dSmatthias.ringwald 
122116833f0aSmatthias.ringwald     // register packet handlers with transport
122210e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1223f5454fc6Smatthias.ringwald 
12243a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1225e2386ba1S[email protected] 
1226e2386ba1S[email protected]     // class of device
12273a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1228a45d6b9fS[email protected] 
1229f20168b8Smatthias.ringwald     // bondable by default
1230f20168b8Smatthias.ringwald     hci_stack->bondable = 1;
1231f20168b8Smatthias.ringwald 
123263048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
12333a9fb326S[email protected]     hci_stack->ssp_enable = 1;
12343a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
12353a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
12363a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
123769a97523S[email protected] 
1238595bdbfbS[email protected]     hci_state_reset();
1239475c8125Smatthias.ringwald }
1240475c8125Smatthias.ringwald 
1241404843c1Smatthias.ringwald void hci_close(){
1242404843c1Smatthias.ringwald     // close remote device db
12433a9fb326S[email protected]     if (hci_stack->remote_device_db) {
12443a9fb326S[email protected]         hci_stack->remote_device_db->close();
1245404843c1Smatthias.ringwald     }
12463a9fb326S[email protected]     while (hci_stack->connections) {
1247bc68afe2S[email protected]         // cancel all l2cap connections
1248bc68afe2S[email protected]         hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host
12493a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1250f5454fc6Smatthias.ringwald     }
1251f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
12523a9fb326S[email protected] 
12533a9fb326S[email protected] #ifdef HAVE_MALLOC
12543a9fb326S[email protected]     free(hci_stack);
12553a9fb326S[email protected] #endif
12563a9fb326S[email protected]     hci_stack = NULL;
1257404843c1Smatthias.ringwald }
1258404843c1Smatthias.ringwald 
12599e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
12609e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
12619e61646fS[email protected] }
12629e61646fS[email protected] 
126366fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
126466fb9560S[email protected]     disable_l2cap_timeouts = 1;
126566fb9560S[email protected] }
12668d213e1aSmatthias.ringwald // State-Module-Driver overview
12678d213e1aSmatthias.ringwald // state                    module  low-level
12688d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
12698d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
12708d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
12718d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1272d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1273d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1274c7e0c5f6Smatthias.ringwald 
127540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
12767301ad89Smatthias.ringwald 
1277038bc64cSmatthias.ringwald     // power on
1278f9a30166Smatthias.ringwald     int err = 0;
12793a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
12803a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1281f9a30166Smatthias.ringwald     }
1282038bc64cSmatthias.ringwald     if (err){
12837d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1284038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1285038bc64cSmatthias.ringwald         return err;
1286038bc64cSmatthias.ringwald     }
1287038bc64cSmatthias.ringwald 
1288038bc64cSmatthias.ringwald     // open low-level device
12893a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1290038bc64cSmatthias.ringwald     if (err){
12917d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
12923a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
12933a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1294f9a30166Smatthias.ringwald         }
1295038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1296038bc64cSmatthias.ringwald         return err;
1297038bc64cSmatthias.ringwald     }
12988d213e1aSmatthias.ringwald     return 0;
12998d213e1aSmatthias.ringwald }
1300038bc64cSmatthias.ringwald 
130140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
13028d213e1aSmatthias.ringwald 
13037b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
13049418f9c9Smatthias.ringwald 
13058d213e1aSmatthias.ringwald     // close low-level device
13063a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
13078d213e1aSmatthias.ringwald 
13087b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
13099418f9c9Smatthias.ringwald 
13108d213e1aSmatthias.ringwald     // power off
13113a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
13123a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
13138d213e1aSmatthias.ringwald     }
13149418f9c9Smatthias.ringwald 
13157b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
13169418f9c9Smatthias.ringwald 
13173a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
131872ea5239Smatthias.ringwald }
131972ea5239Smatthias.ringwald 
132040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
132172ea5239Smatthias.ringwald 
13227b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
13233144bce4Smatthias.ringwald 
1324b429b9b7Smatthias.ringwald #if 0
1325b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1326b429b9b7Smatthias.ringwald 
132772ea5239Smatthias.ringwald     // close low-level device
13283a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1329b429b9b7Smatthias.ringwald #endif
133072ea5239Smatthias.ringwald 
133172ea5239Smatthias.ringwald     // sleep mode
13323a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
13333a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
133472ea5239Smatthias.ringwald     }
1335b429b9b7Smatthias.ringwald 
13363a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
13378d213e1aSmatthias.ringwald }
13388d213e1aSmatthias.ringwald 
133940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1340b429b9b7Smatthias.ringwald 
13417b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1342b429b9b7Smatthias.ringwald 
1343b429b9b7Smatthias.ringwald     // wake on
13443a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
13453a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1346b429b9b7Smatthias.ringwald     }
1347b429b9b7Smatthias.ringwald 
1348b429b9b7Smatthias.ringwald #if 0
1349b429b9b7Smatthias.ringwald     // open low-level device
13503a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1351b429b9b7Smatthias.ringwald     if (err){
13527d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
13533a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
13543a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1355b429b9b7Smatthias.ringwald         }
1356b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1357b429b9b7Smatthias.ringwald         return err;
1358b429b9b7Smatthias.ringwald     }
1359b429b9b7Smatthias.ringwald #endif
1360b429b9b7Smatthias.ringwald 
1361b429b9b7Smatthias.ringwald     return 0;
1362b429b9b7Smatthias.ringwald }
1363b429b9b7Smatthias.ringwald 
136444935e40S[email protected] static void hci_power_transition_to_initializing(void){
136544935e40S[email protected]     // set up state machine
136644935e40S[email protected]     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
136744935e40S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
136844935e40S[email protected]     hci_stack->state = HCI_STATE_INITIALIZING;
136944935e40S[email protected]     hci_stack->substate = 0;
137044935e40S[email protected] }
1371b429b9b7Smatthias.ringwald 
13728d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1373d661ed19Smatthias.ringwald 
13743a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1375d661ed19Smatthias.ringwald 
13768d213e1aSmatthias.ringwald     int err = 0;
13773a9fb326S[email protected]     switch (hci_stack->state){
13788d213e1aSmatthias.ringwald 
13798d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
13808d213e1aSmatthias.ringwald             switch (power_mode){
13818d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
13828d213e1aSmatthias.ringwald                     err = hci_power_control_on();
138397b61c7bS[email protected]                     if (err) {
138497b61c7bS[email protected]                         log_error("hci_power_control_on() error %u", err);
138597b61c7bS[email protected]                         return err;
138697b61c7bS[email protected]                     }
138744935e40S[email protected]                     hci_power_transition_to_initializing();
13888d213e1aSmatthias.ringwald                     break;
13898d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
13908d213e1aSmatthias.ringwald                     // do nothing
13918d213e1aSmatthias.ringwald                     break;
13928d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1393b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
13948d213e1aSmatthias.ringwald                     break;
13958d213e1aSmatthias.ringwald             }
13968d213e1aSmatthias.ringwald             break;
13977301ad89Smatthias.ringwald 
13988d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
13998d213e1aSmatthias.ringwald             switch (power_mode){
14008d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14018d213e1aSmatthias.ringwald                     // do nothing
14028d213e1aSmatthias.ringwald                     break;
14038d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14048d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
14058d213e1aSmatthias.ringwald                     hci_power_control_off();
14068d213e1aSmatthias.ringwald                     break;
14078d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1408b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
140972ea5239Smatthias.ringwald                     hci_power_control_sleep();
14108d213e1aSmatthias.ringwald                     break;
14118d213e1aSmatthias.ringwald             }
14128d213e1aSmatthias.ringwald             break;
14137301ad89Smatthias.ringwald 
14148d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
14158d213e1aSmatthias.ringwald             switch (power_mode){
14168d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
14178d213e1aSmatthias.ringwald                     // do nothing
14188d213e1aSmatthias.ringwald                     break;
14198d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1420c7e0c5f6Smatthias.ringwald                     // see hci_run
14213a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14228d213e1aSmatthias.ringwald                     break;
14238d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1424b546ac54Smatthias.ringwald                     // see hci_run
14253a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
14263a9fb326S[email protected]                     hci_stack->substate = 0;
14278d213e1aSmatthias.ringwald                     break;
14288d213e1aSmatthias.ringwald             }
14298d213e1aSmatthias.ringwald             break;
14307301ad89Smatthias.ringwald 
14318d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
14328d213e1aSmatthias.ringwald             switch (power_mode){
14338d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
143444935e40S[email protected]                     hci_power_transition_to_initializing();
14358d213e1aSmatthias.ringwald                     break;
14368d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14378d213e1aSmatthias.ringwald                     // do nothing
14388d213e1aSmatthias.ringwald                     break;
14398d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1440b546ac54Smatthias.ringwald                     // see hci_run
14413a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
14423a9fb326S[email protected]                     hci_stack->substate = 0;
14438d213e1aSmatthias.ringwald                     break;
14448d213e1aSmatthias.ringwald             }
14458d213e1aSmatthias.ringwald             break;
14468d213e1aSmatthias.ringwald 
14478d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
14488d213e1aSmatthias.ringwald             switch (power_mode){
14498d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
145028171530Smatthias.ringwald 
145128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
145228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
145328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
14543a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
14553a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
145628171530Smatthias.ringwald                         break;
145728171530Smatthias.ringwald                     }
145828171530Smatthias.ringwald #endif
145944935e40S[email protected]                     hci_power_transition_to_initializing();
14608d213e1aSmatthias.ringwald                     break;
14618d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1462b546ac54Smatthias.ringwald                     // see hci_run
14633a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14648d213e1aSmatthias.ringwald                     break;
14658d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1466b546ac54Smatthias.ringwald                     // do nothing
14678d213e1aSmatthias.ringwald                     break;
14688d213e1aSmatthias.ringwald             }
14698d213e1aSmatthias.ringwald             break;
14708d213e1aSmatthias.ringwald 
14718d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
14728d213e1aSmatthias.ringwald             switch (power_mode){
14738d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
147428171530Smatthias.ringwald 
147528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
147628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
147728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
14783a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
14793a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1480758b46ceSmatthias.ringwald                         hci_update_scan_enable();
148128171530Smatthias.ringwald                         break;
148228171530Smatthias.ringwald                     }
148328171530Smatthias.ringwald #endif
14843144bce4Smatthias.ringwald                     err = hci_power_control_wake();
14853144bce4Smatthias.ringwald                     if (err) return err;
148644935e40S[email protected]                     hci_power_transition_to_initializing();
14878d213e1aSmatthias.ringwald                     break;
14888d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
14893a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
14908d213e1aSmatthias.ringwald                     break;
14918d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1492b546ac54Smatthias.ringwald                     // do nothing
14938d213e1aSmatthias.ringwald                     break;
14948d213e1aSmatthias.ringwald             }
14958d213e1aSmatthias.ringwald             break;
149611e23e5fSmatthias.ringwald     }
149768d92d03Smatthias.ringwald 
1498038bc64cSmatthias.ringwald     // create internal event
1499ee8bf225Smatthias.ringwald 	hci_emit_state();
1500ee8bf225Smatthias.ringwald 
150168d92d03Smatthias.ringwald 	// trigger next/first action
150268d92d03Smatthias.ringwald 	hci_run();
150368d92d03Smatthias.ringwald 
1504475c8125Smatthias.ringwald     return 0;
1505475c8125Smatthias.ringwald }
1506475c8125Smatthias.ringwald 
1507758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1508758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
15093a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1510758b46ceSmatthias.ringwald     hci_run();
1511758b46ceSmatthias.ringwald }
1512758b46ceSmatthias.ringwald 
1513381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1514381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1515381fbed8Smatthias.ringwald 
15163a9fb326S[email protected]     if (hci_stack->discoverable == enable){
15173a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1518381fbed8Smatthias.ringwald         return;
1519381fbed8Smatthias.ringwald     }
1520381fbed8Smatthias.ringwald 
15213a9fb326S[email protected]     hci_stack->discoverable = enable;
1522758b46ceSmatthias.ringwald     hci_update_scan_enable();
1523758b46ceSmatthias.ringwald }
1524b031bebbSmatthias.ringwald 
1525758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1526758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1527758b46ceSmatthias.ringwald 
1528758b46ceSmatthias.ringwald     // don't emit event
15293a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1530758b46ceSmatthias.ringwald 
15313a9fb326S[email protected]     hci_stack->connectable = enable;
1532758b46ceSmatthias.ringwald     hci_update_scan_enable();
1533381fbed8Smatthias.ringwald }
1534381fbed8Smatthias.ringwald 
15355061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
15363a9fb326S[email protected]     return &hci_stack->local_bd_addr;
15375061f3afS[email protected] }
15385061f3afS[email protected] 
153906b35ec0Smatthias.ringwald void hci_run(){
15408a485f27Smatthias.ringwald 
154132ab9390Smatthias.ringwald     hci_connection_t * connection;
154232ab9390Smatthias.ringwald     linked_item_t * it;
154332ab9390Smatthias.ringwald 
154464f0b431S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
1545ce4c8fabSmatthias.ringwald 
1546b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1547b031bebbSmatthias.ringwald 
1548b031bebbSmatthias.ringwald     // decline incoming connections
15493a9fb326S[email protected]     if (hci_stack->decline_reason){
15503a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
15513a9fb326S[email protected]         hci_stack->decline_reason = 0;
15523a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1553dbe1a790S[email protected]         return;
1554ce4c8fabSmatthias.ringwald     }
1555ce4c8fabSmatthias.ringwald 
1556b031bebbSmatthias.ringwald     // send scan enable
15573a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
15583a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
15593a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1560dbe1a790S[email protected]         return;
1561b031bebbSmatthias.ringwald     }
1562b031bebbSmatthias.ringwald 
1563b2f949feS[email protected] #ifdef HAVE_BLE
15647bdc6798S[email protected]     // handle le scan
15657bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
15667bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
15677bdc6798S[email protected]             case LE_START_SCAN:
15687bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
15697bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
15707bdc6798S[email protected]                 return;
15717bdc6798S[email protected] 
15727bdc6798S[email protected]             case LE_STOP_SCAN:
15737bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
15747bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
15757bdc6798S[email protected]                 return;
15767bdc6798S[email protected]             default:
15777bdc6798S[email protected]                 break;
15787bdc6798S[email protected]         }
1579e2602ea2Smatthias.ringwald         if (hci_stack->le_scan_type != 0xff){
1580e2602ea2Smatthias.ringwald             // defaults: active scanning, accept all advertisement packets
1581e2602ea2Smatthias.ringwald             int scan_type = hci_stack->le_scan_type;
1582e2602ea2Smatthias.ringwald             hci_stack->le_scan_type = 0xff;
1583e2602ea2Smatthias.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);
1584e2602ea2Smatthias.ringwald             return;
1585e2602ea2Smatthias.ringwald         }
15867bdc6798S[email protected]     }
1587b2f949feS[email protected] #endif
15887bdc6798S[email protected] 
158932ab9390Smatthias.ringwald     // send pending HCI commands
15903a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
1591b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
159232ab9390Smatthias.ringwald 
15930bf6344aS[email protected]         switch(connection->state){
15940bf6344aS[email protected]             case SEND_CREATE_CONNECTION:
15954f3229d8S[email protected]                 switch(connection->address_type){
15964f3229d8S[email protected]                     case BD_ADDR_TYPE_CLASSIC:
1597564fca32S[email protected]                         log_info("sending hci_create_connection\n");
1598ad83dc6aS[email protected]                         hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
15994f3229d8S[email protected]                         break;
16004f3229d8S[email protected]                     default:
1601b2f949feS[email protected] #ifdef HAVE_BLE
1602564fca32S[email protected]                         log_info("sending hci_le_create_connection\n");
16034f3229d8S[email protected]                         hci_send_cmd(&hci_le_create_connection,
1604b2571179Smatthias.ringwald                                      0x0060,    // scan interval: 60 ms
1605b2571179Smatthias.ringwald                                      0x0030,    // scan interval: 30 ms
16064f3229d8S[email protected]                                      0,         // don't use whitelist
16074f3229d8S[email protected]                                      connection->address_type, // peer address type
16084f3229d8S[email protected]                                      connection->address,      // peer bd addr
1609b2571179Smatthias.ringwald                                      hci_stack->adv_addr_type, // our addr type:
1610b2571179Smatthias.ringwald                                      0x0008,    // conn interval min
1611b2571179Smatthias.ringwald                                      0x0018,    // conn interval max
16124f3229d8S[email protected]                                      0,         // conn latency
1613b2571179Smatthias.ringwald                                      0x0048,    // supervision timeout
1614b2571179Smatthias.ringwald                                      0x0001,    // min ce length
1615b2571179Smatthias.ringwald                                      0x0001     // max ce length
16164f3229d8S[email protected]                                      );
16174f3229d8S[email protected] 
16184f3229d8S[email protected]                         connection->state = SENT_CREATE_CONNECTION;
1619b2f949feS[email protected] #endif
16204f3229d8S[email protected]                         break;
16214f3229d8S[email protected]                 }
1622ad83dc6aS[email protected]                 return;
1623ad83dc6aS[email protected] 
16240bf6344aS[email protected]             case RECEIVED_CONNECTION_REQUEST:
16257b5fbe1fSmatthias.ringwald                 log_info("sending hci_accept_connection_request\n");
162632ab9390Smatthias.ringwald                 connection->state = ACCEPTED_CONNECTION_REQUEST;
162734d2123cS[email protected]                 hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1628dbe1a790S[email protected]                 return;
16290bf6344aS[email protected] 
1630a6725849S[email protected] #ifdef HAVE_BLE
16310bf6344aS[email protected]             case SEND_CANCEL_CONNECTION:
16320bf6344aS[email protected]                 connection->state = SENT_CANCEL_CONNECTION;
16330bf6344aS[email protected]                 hci_send_cmd(&hci_le_create_connection_cancel);
16340bf6344aS[email protected]                 return;
1635a6725849S[email protected] #endif
16360bf6344aS[email protected]             case SEND_DISCONNECT:
16370bf6344aS[email protected]                 connection->state = SENT_DISCONNECT;
16387851196eSmatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
16390bf6344aS[email protected]                 return;
16400bf6344aS[email protected] 
16410bf6344aS[email protected]             default:
16420bf6344aS[email protected]                 break;
1643c7e0c5f6Smatthias.ringwald         }
1644c7e0c5f6Smatthias.ringwald 
164532ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
164634d2123cS[email protected]             log_info("responding to link key request\n");
164734d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
164832ab9390Smatthias.ringwald             link_key_t link_key;
1649c77e8838S[email protected]             link_key_type_t link_key_type;
16503a9fb326S[email protected]             if ( hci_stack->remote_device_db
16513a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
165234d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
16539ab95c90S[email protected]                connection->link_key_type = link_key_type;
165432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
165532ab9390Smatthias.ringwald             } else {
165632ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
165732ab9390Smatthias.ringwald             }
1658dbe1a790S[email protected]             return;
165932ab9390Smatthias.ringwald         }
16601d6b20aeS[email protected] 
1661899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
16624c57c146S[email protected]             log_info("denying to pin request\n");
1663899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
166434d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
16654c57c146S[email protected]             return;
16664c57c146S[email protected]         }
16674c57c146S[email protected] 
1668dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
166934d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
167082d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
167182d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1672106d6d11S[email protected]                 // tweak authentication requirements
16733a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1674106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
16759faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
16769faad3abS[email protected]                 }
16779faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
16789faad3abS[email protected]                     authreq |= 1;
1679106d6d11S[email protected]                 }
16803a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1681f8fb5f6eS[email protected]             } else {
1682f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1683f8fb5f6eS[email protected]             }
1684dbe1a790S[email protected]             return;
168532ab9390Smatthias.ringwald         }
168632ab9390Smatthias.ringwald 
1687dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1688dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
168934d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1690dbe1a790S[email protected]             return;
1691dbe1a790S[email protected]         }
1692dbe1a790S[email protected] 
1693dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1694dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
169534d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1696dbe1a790S[email protected]             return;
1697dbe1a790S[email protected]         }
1698afd4e962S[email protected] 
1699afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1700afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
170134d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
17022bd8b7e7S[email protected]             return;
17032bd8b7e7S[email protected]         }
17042bd8b7e7S[email protected] 
17052bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
17062bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
170734d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
170834d2123cS[email protected]             return;
170934d2123cS[email protected]         }
1710ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1711ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
171252d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1713ad83dc6aS[email protected]             return;
1714ad83dc6aS[email protected]         }
171534d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
171634d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
171734d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
17182bd8b7e7S[email protected]             return;
1719afd4e962S[email protected]         }
1720dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1721dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1722dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1723dce78009S[email protected]             return;
1724dce78009S[email protected]         }
1725dbe1a790S[email protected]     }
1726c7e0c5f6Smatthias.ringwald 
17273a9fb326S[email protected]     switch (hci_stack->state){
17283429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
1729bd88fde9S[email protected]             hci_initializing_state_machine();
17303429f56bSmatthias.ringwald             break;
1731c7e0c5f6Smatthias.ringwald 
1732c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1733c7e0c5f6Smatthias.ringwald 
17347b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1735c7e0c5f6Smatthias.ringwald             // close all open connections
17363a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1737c7e0c5f6Smatthias.ringwald             if (connection){
173832ab9390Smatthias.ringwald 
1739c7e0c5f6Smatthias.ringwald                 // send disconnect
174064f0b431S[email protected]                 if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
174132ab9390Smatthias.ringwald 
174279bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
17436ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1744c7e0c5f6Smatthias.ringwald 
1745c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1746c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1747c7e0c5f6Smatthias.ringwald                 return;
1748c7e0c5f6Smatthias.ringwald             }
17497b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1750c7e0c5f6Smatthias.ringwald 
175172ea5239Smatthias.ringwald             // switch mode
1752c7e0c5f6Smatthias.ringwald             hci_power_control_off();
17539418f9c9Smatthias.ringwald 
17547b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
175572ea5239Smatthias.ringwald             hci_emit_state();
17567b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
175772ea5239Smatthias.ringwald             break;
1758c7e0c5f6Smatthias.ringwald 
175972ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
17603a9fb326S[email protected]             switch(hci_stack->substate) {
176189db417bSmatthias.ringwald                 case 0:
17627b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
176372ea5239Smatthias.ringwald                     // close all open connections
17643a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
176566da7044Smatthias.ringwald 
176628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
176766da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
176866da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
176966da7044Smatthias.ringwald                         connection = NULL;
177066da7044Smatthias.ringwald                     }
177166da7044Smatthias.ringwald #endif
177272ea5239Smatthias.ringwald                     if (connection){
177332ab9390Smatthias.ringwald 
177472ea5239Smatthias.ringwald                         // send disconnect
177532ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
177632ab9390Smatthias.ringwald 
177779bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
17786ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
177972ea5239Smatthias.ringwald 
178072ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
178172ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
178272ea5239Smatthias.ringwald                         return;
178372ea5239Smatthias.ringwald                     }
178472ea5239Smatthias.ringwald 
178592368cd3S[email protected]                     if (hci_classic_supported()){
178689db417bSmatthias.ringwald                         // disable page and inquiry scan
178764f0b431S[email protected]                         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
178832ab9390Smatthias.ringwald 
178992368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
17903a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
179189db417bSmatthias.ringwald 
179289db417bSmatthias.ringwald                         // continue in next sub state
17933a9fb326S[email protected]                         hci_stack->substate++;
179489db417bSmatthias.ringwald                         break;
179592368cd3S[email protected]                     }
179692368cd3S[email protected]                     // fall through for ble-only chips
179792368cd3S[email protected] 
179889db417bSmatthias.ringwald                 case 2:
17997b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
180028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
180128171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
180228171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
180328171530Smatthias.ringwald                         // SLEEP MODE reached
18043a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
180528171530Smatthias.ringwald                         hci_emit_state();
180628171530Smatthias.ringwald                         break;
180728171530Smatthias.ringwald                     }
180828171530Smatthias.ringwald #endif
180972ea5239Smatthias.ringwald                     // switch mode
18103a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1811c7e0c5f6Smatthias.ringwald                     hci_emit_state();
181228171530Smatthias.ringwald                     break;
181328171530Smatthias.ringwald 
181489db417bSmatthias.ringwald                 default:
181589db417bSmatthias.ringwald                     break;
181689db417bSmatthias.ringwald             }
1817c7e0c5f6Smatthias.ringwald             break;
1818c7e0c5f6Smatthias.ringwald 
18193429f56bSmatthias.ringwald         default:
18203429f56bSmatthias.ringwald             break;
18211f504dbdSmatthias.ringwald     }
18223429f56bSmatthias.ringwald }
182316833f0aSmatthias.ringwald 
182431452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1825c8e4258aSmatthias.ringwald     bd_addr_t addr;
1826c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1827c8e4258aSmatthias.ringwald     // house-keeping
1828c8e4258aSmatthias.ringwald 
1829c8e4258aSmatthias.ringwald     // create_connection?
1830c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1831c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1832339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1833c8e4258aSmatthias.ringwald 
183496a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1835ad83dc6aS[email protected]         if (!conn){
183696a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
183717f1ba2aSmatthias.ringwald             if (!conn){
183817f1ba2aSmatthias.ringwald                 // notify client that alloc failed
183917f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
184017f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
184117f1ba2aSmatthias.ringwald             }
1842ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1843ad83dc6aS[email protected]         }
1844ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1845ad83dc6aS[email protected]         switch (conn->state){
1846ad83dc6aS[email protected]             // if connection active exists
1847ad83dc6aS[email protected]             case OPEN:
184862bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1849ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
185062bda3fbS[email protected]                 return 0;
1851ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1852ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1853ad83dc6aS[email protected]                 break;
1854ad83dc6aS[email protected]             default:
1855ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1856ad83dc6aS[email protected]                 return 0;
1857ad83dc6aS[email protected]         }
1858c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1859c8e4258aSmatthias.ringwald     }
1860c8e4258aSmatthias.ringwald 
18617fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
18627fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
18637fde4af9Smatthias.ringwald     }
18647fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
18657fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
18667fde4af9Smatthias.ringwald     }
18677fde4af9Smatthias.ringwald 
18688ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
18693a9fb326S[email protected]         if (hci_stack->remote_device_db){
18708ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
18713a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
18728ef73945Smatthias.ringwald         }
18738ef73945Smatthias.ringwald     }
1874c8e4258aSmatthias.ringwald 
18756724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
18766724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
18776724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
187896a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
18796724cd9eS[email protected]         if (conn){
18806724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
18816724cd9eS[email protected]         }
18826724cd9eS[email protected]     }
18836724cd9eS[email protected] 
18846724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
18856724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
18866724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
18876724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
18886724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
188996a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
18906724cd9eS[email protected]         if (conn){
18916724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
18926724cd9eS[email protected]         }
18936724cd9eS[email protected]     }
18946724cd9eS[email protected] 
189569a97523S[email protected] #ifdef HAVE_BLE
189669a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
18973a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
189869a97523S[email protected]     }
189969a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
19003a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
190169a97523S[email protected]     }
190269a97523S[email protected] #endif
190369a97523S[email protected] 
19043a9fb326S[email protected]     hci_stack->num_cmd_packets--;
19056b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
19066b4af23dS[email protected] 
19076b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
1908c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
19096b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
19106b4af23dS[email protected]     }
19116b4af23dS[email protected] 
19126b4af23dS[email protected]     return err;
191331452debSmatthias.ringwald }
19148adf0ddaSmatthias.ringwald 
19152bd8b7e7S[email protected] // disconnect because of security block
19162bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
19172bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
19182bd8b7e7S[email protected]     if (!connection) return;
19192bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
19202bd8b7e7S[email protected] }
19212bd8b7e7S[email protected] 
19222bd8b7e7S[email protected] 
1923dbe1a790S[email protected] // Configure Secure Simple Pairing
1924dbe1a790S[email protected] 
1925dbe1a790S[email protected] // enable will enable SSP during init
1926dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
19273a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1928dbe1a790S[email protected] }
1929dbe1a790S[email protected] 
19302bd8b7e7S[email protected] int hci_local_ssp_activated(){
19313a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
19322bd8b7e7S[email protected] }
19332bd8b7e7S[email protected] 
1934dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1935dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
19363a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1937dbe1a790S[email protected] }
1938dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
19393a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1940dbe1a790S[email protected] }
1941dbe1a790S[email protected] 
1942dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1943dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
19443a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1945dbe1a790S[email protected] }
1946dbe1a790S[email protected] 
19471cd208adSmatthias.ringwald /**
19481cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
19491cd208adSmatthias.ringwald  */
1950fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
19515127cc62S[email protected] 
19525127cc62S[email protected]     // for HCI INITIALIZATION
19535127cc62S[email protected]     // printf("hci_send_cmd: opcode %04x\n", cmd->opcode);
19545127cc62S[email protected]     hci_stack->last_cmd_opcode = cmd->opcode;
19555127cc62S[email protected] 
19561cd208adSmatthias.ringwald     va_list argptr;
19571cd208adSmatthias.ringwald     va_start(argptr, cmd);
19583a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
19591cd208adSmatthias.ringwald     va_end(argptr);
19603a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
196193b8dc03Smatthias.ringwald }
1962c8e4258aSmatthias.ringwald 
1963ee091cf1Smatthias.ringwald // Create various non-HCI events.
1964ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1965ee091cf1Smatthias.ringwald 
1966c8e4258aSmatthias.ringwald void hci_emit_state(){
19673a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1968425d1371Smatthias.ringwald     uint8_t event[3];
196980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1970425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
19713a9fb326S[email protected]     event[2] = hci_stack->state;
1972425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19733a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1974c8e4258aSmatthias.ringwald }
1975c8e4258aSmatthias.ringwald 
197617f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1977425d1371Smatthias.ringwald     uint8_t event[13];
1978c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1979425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
198017f1ba2aSmatthias.ringwald     event[2] = status;
1981c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1982c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1983c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1984c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1985425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
19863a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1987c8e4258aSmatthias.ringwald }
1988c8e4258aSmatthias.ringwald 
19894f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){
19904f3229d8S[email protected]     uint8_t event[21];
19914f3229d8S[email protected]     event[0] = HCI_EVENT_LE_META;
19924f3229d8S[email protected]     event[1] = sizeof(event) - 2;
19934f3229d8S[email protected]     event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
19944f3229d8S[email protected]     event[3] = status;
19954f3229d8S[email protected]     bt_store_16(event, 4, conn->con_handle);
19964f3229d8S[email protected]     event[6] = 0; // TODO: role
19974f3229d8S[email protected]     event[7] = conn->address_type;
19984f3229d8S[email protected]     bt_flip_addr(&event[8], conn->address);
19994f3229d8S[email protected]     bt_store_16(event, 14, 0); // interval
20004f3229d8S[email protected]     bt_store_16(event, 16, 0); // latency
20014f3229d8S[email protected]     bt_store_16(event, 18, 0); // supervision timeout
20024f3229d8S[email protected]     event[20] = 0; // master clock accuracy
20034f3229d8S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20044f3229d8S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20054f3229d8S[email protected] }
20064f3229d8S[email protected] 
20073c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
2008425d1371Smatthias.ringwald     uint8_t event[6];
20093c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
2010e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
20113c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
20123c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
20133c4d4b90Smatthias.ringwald     event[5] = reason;
2014425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20153a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20163c4d4b90Smatthias.ringwald }
20173c4d4b90Smatthias.ringwald 
2018ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
201966fb9560S[email protected]     if (disable_l2cap_timeouts) return;
2020e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
2021425d1371Smatthias.ringwald     uint8_t event[4];
202280d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
2023425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2024ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
2025425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
20263a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2027ee091cf1Smatthias.ringwald }
202843bfb1bdSmatthias.ringwald 
202943bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
2030e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
2031425d1371Smatthias.ringwald     uint8_t event[3];
203280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
2033425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
203443bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
2035425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20363a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
203743bfb1bdSmatthias.ringwald }
2038038bc64cSmatthias.ringwald 
2039038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
2040e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
2041425d1371Smatthias.ringwald     uint8_t event[2];
204280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
2043425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2044425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20453a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2046038bc64cSmatthias.ringwald }
20471b0e3922Smatthias.ringwald 
204809ba8edeSmatthias.ringwald #ifndef EMBEDDED
20491b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
2050e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
2051425d1371Smatthias.ringwald     uint8_t event[6];
20521b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
2053425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2054425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
2055425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
2056425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
2057425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20583a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20591b0e3922Smatthias.ringwald }
206009ba8edeSmatthias.ringwald #endif
20611b0e3922Smatthias.ringwald 
20622ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
2063e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
2064425d1371Smatthias.ringwald     uint8_t event[3];
20652ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
2066425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
20672ed6235cSmatthias.ringwald     event[2] = enabled;
2068425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20693a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
20702ed6235cSmatthias.ringwald }
2071627c2f45Smatthias.ringwald 
2072627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
2073e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
2074627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
2075e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
2076f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
20772f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
2078f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
2079e0abb8e7S[email protected] 
2080e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
2081e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
2082e0abb8e7S[email protected] 
2083e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
20843a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
2085627c2f45Smatthias.ringwald }
2086381fbed8Smatthias.ringwald 
2087381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
2088e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
2089425d1371Smatthias.ringwald     uint8_t event[3];
2090381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
2091425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
2092381fbed8Smatthias.ringwald     event[2] = enabled;
2093425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20943a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2095381fbed8Smatthias.ringwald }
2096458bf4e8S[email protected] 
2097a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
2098df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
2099a00031e2S[email protected]     uint8_t event[5];
2100e00caf9cS[email protected]     int pos = 0;
2101a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
2102e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
2103a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
2104e00caf9cS[email protected]     pos += 2;
2105e00caf9cS[email protected]     event[pos++] = level;
2106e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21073a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2108e00caf9cS[email protected] }
2109e00caf9cS[email protected] 
2110ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
2111ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
2112ad83dc6aS[email protected]     uint8_t event[9];
2113ad83dc6aS[email protected]     int pos = 0;
2114ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
2115ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
2116ad83dc6aS[email protected]     event[pos++] = status;
2117ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
2118ad83dc6aS[email protected]     pos += 6;
2119ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21203a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
2121ad83dc6aS[email protected] }
2122ad83dc6aS[email protected] 
21232bd8b7e7S[email protected] // query if remote side supports SSP
21242bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
21252bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
21262bd8b7e7S[email protected]     if (!connection) return 0;
21272bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
21282bd8b7e7S[email protected] }
21292bd8b7e7S[email protected] 
2130df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
2131df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
2132df3354fcS[email protected] }
2133df3354fcS[email protected] 
2134458bf4e8S[email protected] // GAP API
2135458bf4e8S[email protected] /**
2136458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2137458bf4e8S[email protected]  * @praram enabled
2138458bf4e8S[email protected]  */
21394c57c146S[email protected] void gap_set_bondable_mode(int enable){
21403a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2141458bf4e8S[email protected] }
2142cb230b9dS[email protected] 
2143cb230b9dS[email protected] /**
214434d2123cS[email protected]  * @brief map link keys to security levels
2145cb230b9dS[email protected]  */
214634d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
214734d2123cS[email protected]     switch (link_key_type){
21483c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
21493c68dfa9S[email protected]             return LEVEL_4;
21503c68dfa9S[email protected]         case COMBINATION_KEY:
21513c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
21523c68dfa9S[email protected]             return LEVEL_3;
21533c68dfa9S[email protected]         default:
21543c68dfa9S[email protected]             return LEVEL_2;
21553c68dfa9S[email protected]     }
2156cb230b9dS[email protected] }
2157cb230b9dS[email protected] 
2158a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
215934d2123cS[email protected]     if (!connection) return LEVEL_0;
216034d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
216134d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
216234d2123cS[email protected] }
216334d2123cS[email protected] 
216434d2123cS[email protected] 
2165106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
21665127cc62S[email protected]     log_info("gap_mitm_protection_required_for_security_level %u", level);
2167106d6d11S[email protected]     return level > LEVEL_2;
2168106d6d11S[email protected] }
2169106d6d11S[email protected] 
217034d2123cS[email protected] /**
217134d2123cS[email protected]  * @brief get current security level
217234d2123cS[email protected]  */
217334d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
217434d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
217534d2123cS[email protected]     if (!connection) return LEVEL_0;
217634d2123cS[email protected]     return gap_security_level_for_connection(connection);
217734d2123cS[email protected] }
217834d2123cS[email protected] 
2179cb230b9dS[email protected] /**
2180cb230b9dS[email protected]  * @brief request connection to device to
2181cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2182cb230b9dS[email protected]  */
218334d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
218434d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
218534d2123cS[email protected]     if (!connection){
2186a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
218734d2123cS[email protected]         return;
218834d2123cS[email protected]     }
218934d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
219034d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
219134d2123cS[email protected]     if (current_level >= requested_level){
2192a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
219334d2123cS[email protected]         return;
219434d2123cS[email protected]     }
2195a00031e2S[email protected] 
219634d2123cS[email protected]     connection->requested_security_level = requested_level;
2197a00031e2S[email protected] 
2198fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
21993a9fb326S[email protected]     if (hci_stack->remote_device_db){
2200a00031e2S[email protected]         link_key_type_t link_key_type;
2201a00031e2S[email protected]         link_key_t      link_key;
22023a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2203a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2204a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2205a00031e2S[email protected]                 return;
2206a00031e2S[email protected]             }
2207a00031e2S[email protected]         }
2208a00031e2S[email protected]     }
2209a00031e2S[email protected] 
22101eb2563eS[email protected]     // try to authenticate connection
22111eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2212e80b2cf9S[email protected]     hci_run();
2213e00caf9cS[email protected] }
2214ad83dc6aS[email protected] 
2215ad83dc6aS[email protected] /**
2216ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2217ad83dc6aS[email protected]  * @param device
2218ad83dc6aS[email protected]  * @param request MITM protection
2219ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2220ad83dc6aS[email protected]  */
2221ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2222ad83dc6aS[email protected] 
2223ad83dc6aS[email protected]     // create connection state machine
222496a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2225ad83dc6aS[email protected] 
2226ad83dc6aS[email protected]     if (!connection){
2227ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2228ad83dc6aS[email protected]     }
2229ad83dc6aS[email protected] 
2230ad83dc6aS[email protected]     // delete linkn key
2231ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2232ad83dc6aS[email protected] 
2233ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2234ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2235ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
22365127cc62S[email protected]     log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level);
2237ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2238ad83dc6aS[email protected] 
2239ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2240ad83dc6aS[email protected] 
2241ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2242ad83dc6aS[email protected]     // handle: authentication failure
2243ad83dc6aS[email protected]     // handle: disconnect on done
2244ad83dc6aS[email protected] 
2245ad83dc6aS[email protected]     hci_run();
2246ad83dc6aS[email protected] 
2247ad83dc6aS[email protected]     return 0;
2248ad83dc6aS[email protected] }
22498e618f72S[email protected] 
22508e618f72S[email protected] void gap_set_local_name(const char * local_name){
22518e618f72S[email protected]     hci_stack->local_name = local_name;
22528e618f72S[email protected] }
22538e618f72S[email protected] 
22547bdc6798S[email protected] le_command_status_t le_central_start_scan(){
225536af3e18S[email protected]     if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
22567bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
22577bdc6798S[email protected]     hci_run();
22587bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
22597bdc6798S[email protected] }
22608e618f72S[email protected] 
22617bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
226236af3e18S[email protected]     if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
22637bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
22647bdc6798S[email protected]     hci_run();
22657bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
22667bdc6798S[email protected] }
22674f3229d8S[email protected] 
2268ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
2269ef11999fSmatthias.ringwald     hci_stack->le_scan_type     = scan_type;
2270ef11999fSmatthias.ringwald     hci_stack->le_scan_interval = scan_interval;
2271ef11999fSmatthias.ringwald     hci_stack->le_scan_window   = scan_window;
2272ef11999fSmatthias.ringwald     hci_run();
2273ef11999fSmatthias.ringwald }
22744f3229d8S[email protected] 
22754f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){
22764f3229d8S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
22774f3229d8S[email protected]     if (!conn){
22781f479f8cS[email protected]         log_info("le_central_connect: no connection exists yet, creating context");
22794f3229d8S[email protected]         conn = create_connection_for_bd_addr_and_type(*addr, addr_type);
22804f3229d8S[email protected]         if (!conn){
22814f3229d8S[email protected]             // notify client that alloc failed
22824f3229d8S[email protected]             hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
22831f479f8cS[email protected]             log_info("le_central_connect: failed to alloc context");
22844f3229d8S[email protected]             return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
22854f3229d8S[email protected]         }
22864f3229d8S[email protected]         conn->state = SEND_CREATE_CONNECTION;
22871f479f8cS[email protected]         log_info("le_central_connect: send create connection next");
2288564fca32S[email protected]         hci_run();
22894f3229d8S[email protected]         return BLE_PERIPHERAL_OK;
22904f3229d8S[email protected]     }
22910bf6344aS[email protected] 
22920bf6344aS[email protected]     if (!hci_is_le_connection(conn) ||
22930bf6344aS[email protected]         conn->state == SEND_CREATE_CONNECTION ||
22940bf6344aS[email protected]         conn->state == SENT_CREATE_CONNECTION) {
22950bf6344aS[email protected]         hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED);
22961f479f8cS[email protected]         log_error("le_central_connect: classic connection or connect is already being created");
22970bf6344aS[email protected]         return BLE_PERIPHERAL_IN_WRONG_STATE;
22980bf6344aS[email protected]     }
22990bf6344aS[email protected] 
23001f479f8cS[email protected]     log_info("le_central_connect: context exists with state %u", conn->state);
23014f3229d8S[email protected]     hci_emit_le_connection_complete(conn, 0);
23024f3229d8S[email protected]     hci_run();
23034f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
23044f3229d8S[email protected] }
23054f3229d8S[email protected] 
23067851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists
23077851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){
23080bf6344aS[email protected]     linked_item_t *it;
23090bf6344aS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
23100bf6344aS[email protected]         hci_connection_t * conn = (hci_connection_t *) it;
23110bf6344aS[email protected]         if (!hci_is_le_connection(conn)) continue;
23120bf6344aS[email protected]         switch (conn->state){
2313a6725849S[email protected]             case SEND_CREATE_CONNECTION:
23147851196eSmatthias.ringwald             case SENT_CREATE_CONNECTION:
23157851196eSmatthias.ringwald                 return conn;
23167851196eSmatthias.ringwald             default:
23177851196eSmatthias.ringwald                 break;
23187851196eSmatthias.ringwald         };
23197851196eSmatthias.ringwald     }
23207851196eSmatthias.ringwald     return NULL;
23217851196eSmatthias.ringwald }
23227851196eSmatthias.ringwald 
23237851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){
23247851196eSmatthias.ringwald     hci_connection_t * conn = le_central_get_outgoing_connection();
23257851196eSmatthias.ringwald     switch (conn->state){
23267851196eSmatthias.ringwald         case SEND_CREATE_CONNECTION:
23277851196eSmatthias.ringwald             // skip sending create connection and emit event instead
23280bf6344aS[email protected]             hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
23297851196eSmatthias.ringwald             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
23307851196eSmatthias.ringwald             btstack_memory_hci_connection_free( conn );
23310bf6344aS[email protected]             break;
2332a6725849S[email protected]         case SENT_CREATE_CONNECTION:
23337851196eSmatthias.ringwald             // request to send cancel connection
23340bf6344aS[email protected]             conn->state = SEND_CANCEL_CONNECTION;
23350bf6344aS[email protected]             hci_run();
23360bf6344aS[email protected]             break;
23370bf6344aS[email protected]         default:
23380bf6344aS[email protected]             break;
23390bf6344aS[email protected]     }
2340e31f89a7S[email protected]     return BLE_PERIPHERAL_OK;
2341e31f89a7S[email protected] }
23424f3229d8S[email protected] 
23435917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){
23445917a5c5S[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
23455917a5c5S[email protected]     if (!conn){
23467851196eSmatthias.ringwald         hci_emit_disconnection_complete(handle, 0);
23475917a5c5S[email protected]         return BLE_PERIPHERAL_OK;
23485917a5c5S[email protected]     }
23495917a5c5S[email protected]     conn->state = SEND_DISCONNECT;
23505917a5c5S[email protected]     hci_run();
23514f3229d8S[email protected]     return BLE_PERIPHERAL_OK;
23524f3229d8S[email protected] }
235304a6ef8cSmatthias.ringwald 
235404a6ef8cSmatthias.ringwald void hci_disconnect_all(){
235504a6ef8cSmatthias.ringwald     linked_list_iterator_t it;
235604a6ef8cSmatthias.ringwald     linked_list_iterator_init(&it, &hci_stack->connections);
235704a6ef8cSmatthias.ringwald     while (linked_list_iterator_has_next(&it)){
235804a6ef8cSmatthias.ringwald         hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it);
235904a6ef8cSmatthias.ringwald         if (con->state == SENT_DISCONNECT) continue;
236004a6ef8cSmatthias.ringwald         con->state = SEND_DISCONNECT;
236104a6ef8cSmatthias.ringwald     }
2362d31fba26S[email protected]     hci_run();
236304a6ef8cSmatthias.ringwald }
2364