xref: /btstack/src/hci.c (revision 57c9da5b077e8ebd281120cad75cfb799f4c85b6)
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);
76758b46ceSmatthias.ringwald 
7706b35ec0Smatthias.ringwald // the STACK is here
783a9fb326S[email protected] #ifndef HAVE_MALLOC
793a9fb326S[email protected] static hci_stack_t   hci_stack_static;
803a9fb326S[email protected] #endif
813a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8216833f0aSmatthias.ringwald 
83*57c9da5bS[email protected] // static void (*le_central_callback)(le_event_t * event);
847bdc6798S[email protected] 
85*57c9da5bS[email protected] //static void dummy_notify(le_event_t* event){}
86*57c9da5bS[email protected] //void le_central_register_handler(void (*le_callback)(le_event_t* event)){
87*57c9da5bS[email protected] //    if (le_callback == NULL){
88*57c9da5bS[email protected] //        le_callback = dummy_notify;
89*57c9da5bS[email protected] //    }
90*57c9da5bS[email protected] //    le_central_callback = le_callback;
91*57c9da5bS[email protected] //}
927bdc6798S[email protected] 
9366fb9560S[email protected] // test helper
9466fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
9566fb9560S[email protected] 
9696a45072S[email protected] /**
9796a45072S[email protected]  * create connection for given address
9896a45072S[email protected]  *
9996a45072S[email protected]  * @return connection OR NULL, if no memory left
10096a45072S[email protected]  */
10196a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
10296a45072S[email protected] 
10396a45072S[email protected]     printf("create_connection_for_addr %s\n", bd_addr_to_str(addr));
10496a45072S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
10596a45072S[email protected]     if (!conn) return NULL;
10696a45072S[email protected]     BD_ADDR_COPY(conn->address, addr);
10796a45072S[email protected]     conn->address_type = addr_type;
10896a45072S[email protected]     conn->con_handle = 0xffff;
10996a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
11096a45072S[email protected]     conn->bonding_flags = 0;
11196a45072S[email protected]     conn->requested_security_level = LEVEL_0;
11296a45072S[email protected]     linked_item_set_user(&conn->timeout.item, conn);
11396a45072S[email protected]     conn->timeout.process = hci_connection_timeout_handler;
11496a45072S[email protected]     hci_connection_timestamp(conn);
11596a45072S[email protected]     conn->acl_recombination_length = 0;
11696a45072S[email protected]     conn->acl_recombination_pos = 0;
11796a45072S[email protected]     conn->num_acl_packets_sent = 0;
11896a45072S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
11996a45072S[email protected]     return conn;
12096a45072S[email protected] }
12166fb9560S[email protected] 
12297addcc5Smatthias.ringwald /**
123ee091cf1Smatthias.ringwald  * get connection for a given handle
124ee091cf1Smatthias.ringwald  *
125ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
126ee091cf1Smatthias.ringwald  */
1275061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
128ee091cf1Smatthias.ringwald     linked_item_t *it;
1293a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
130ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
131ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
132ee091cf1Smatthias.ringwald         }
133ee091cf1Smatthias.ringwald     }
134ee091cf1Smatthias.ringwald     return NULL;
135ee091cf1Smatthias.ringwald }
136ee091cf1Smatthias.ringwald 
13796a45072S[email protected] /**
13896a45072S[email protected]  * get connection for given address
13996a45072S[email protected]  *
14096a45072S[email protected]  * @return connection OR NULL, if not found
14196a45072S[email protected]  */
14296a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){
14362bda3fbS[email protected]     linked_item_t *it;
14462bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
14562bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
14696a45072S[email protected]         if (connection->address_type != addr_type)  continue;
14796a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
14862bda3fbS[email protected]         return connection;
14962bda3fbS[email protected]     }
15062bda3fbS[email protected]     return NULL;
15162bda3fbS[email protected] }
15262bda3fbS[email protected] 
1532b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
15428ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
155c785ef68Smatthias.ringwald #ifdef HAVE_TIME
156ee091cf1Smatthias.ringwald     struct timeval tv;
157ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
158c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
159ee091cf1Smatthias.ringwald         // connections might be timed out
160ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
161ee091cf1Smatthias.ringwald     }
1622b12a0b9Smatthias.ringwald #endif
163e5780900Smatthias.ringwald #ifdef HAVE_TICK
164c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
165c785ef68Smatthias.ringwald         // connections might be timed out
166c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
167c785ef68Smatthias.ringwald     }
168c785ef68Smatthias.ringwald #endif
169c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
170c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
171c785ef68Smatthias.ringwald }
172ee091cf1Smatthias.ringwald 
173ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
174c7492964Smatthias.ringwald #ifdef HAVE_TIME
175ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
176c7492964Smatthias.ringwald #endif
177e5780900Smatthias.ringwald #ifdef HAVE_TICK
178c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
179c785ef68Smatthias.ringwald #endif
180ee091cf1Smatthias.ringwald }
181ee091cf1Smatthias.ringwald 
18206b35ec0Smatthias.ringwald 
18328ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18428ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
18528ca2b46S[email protected] }
18628ca2b46S[email protected] 
18728ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18828ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
18928ca2b46S[email protected] }
19028ca2b46S[email protected] 
19128ca2b46S[email protected] 
19243bfb1bdSmatthias.ringwald /**
19380ca58a0Smatthias.ringwald  * add authentication flags and reset timer
19496a45072S[email protected]  * @note: assumes classic connection
1957fde4af9Smatthias.ringwald  */
1967fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1977fde4af9Smatthias.ringwald     bd_addr_t addr;
1987fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
19996a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
2007fde4af9Smatthias.ringwald     if (conn) {
20128ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
20280ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
2037fde4af9Smatthias.ringwald     }
2047fde4af9Smatthias.ringwald }
2057fde4af9Smatthias.ringwald 
20680ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2075061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
20880ca58a0Smatthias.ringwald     if (!conn) return 0;
2096724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2106724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2116724cd9eS[email protected]     return 0;
21280ca58a0Smatthias.ringwald }
21380ca58a0Smatthias.ringwald 
214c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2153a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2163a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
217c12e46e7Smatthias.ringwald     }
218c12e46e7Smatthias.ringwald }
219c12e46e7Smatthias.ringwald 
2207fde4af9Smatthias.ringwald 
2217fde4af9Smatthias.ringwald /**
22243bfb1bdSmatthias.ringwald  * count connections
22343bfb1bdSmatthias.ringwald  */
22440d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
22556c253c9Smatthias.ringwald     int count = 0;
22643bfb1bdSmatthias.ringwald     linked_item_t *it;
2273a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
22843bfb1bdSmatthias.ringwald     return count;
22943bfb1bdSmatthias.ringwald }
230c8e4258aSmatthias.ringwald 
23197addcc5Smatthias.ringwald /**
232ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
23316833f0aSmatthias.ringwald  */
2342718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
23516833f0aSmatthias.ringwald }
23616833f0aSmatthias.ringwald 
237998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2385061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
239998906cdSmatthias.ringwald     if (!connection) {
2407d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
241998906cdSmatthias.ringwald         return 0;
242998906cdSmatthias.ringwald     }
243998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
244998906cdSmatthias.ringwald }
245998906cdSmatthias.ringwald 
246998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
2473a9fb326S[email protected]     uint8_t free_slots = hci_stack->total_num_acl_packets;
248998906cdSmatthias.ringwald     linked_item_t *it;
2493a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
250998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
251998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2527d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
253998906cdSmatthias.ringwald             return 0;
254998906cdSmatthias.ringwald         }
255998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
256998906cdSmatthias.ringwald     }
257998906cdSmatthias.ringwald     return free_slots;
258998906cdSmatthias.ringwald }
259998906cdSmatthias.ringwald 
260c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2612b12a0b9Smatthias.ringwald 
2622b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2633a9fb326S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
2643a9fb326S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){
2652b12a0b9Smatthias.ringwald             return 0;
2662b12a0b9Smatthias.ringwald         }
2672b12a0b9Smatthias.ringwald     }
2682b12a0b9Smatthias.ringwald 
2692b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
270c24735b1Smatthias.ringwald     switch (packet_type) {
271c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
272c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
273c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
2743a9fb326S[email protected]             return hci_stack->num_cmd_packets;
275c24735b1Smatthias.ringwald         default:
276c24735b1Smatthias.ringwald             return 0;
277c24735b1Smatthias.ringwald     }
278c24735b1Smatthias.ringwald }
279c24735b1Smatthias.ringwald 
2806b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use
2816b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
2826b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2836b4af23dS[email protected]     return hci_can_send_packet_now(packet_type);
2846b4af23dS[email protected] }
2856b4af23dS[email protected] 
286c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
287c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
288c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
289c8b9416aS[email protected] }
290c8b9416aS[email protected] 
2916b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
2926b4af23dS[email protected] int hci_reserve_packet_buffer(void){
2936b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2946b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
2956b4af23dS[email protected]     return 1;
2966b4af23dS[email protected] }
2976b4af23dS[email protected] 
29868a0fcf7S[email protected] void hci_release_packet_buffer(void){
29968a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
30068a0fcf7S[email protected] }
30168a0fcf7S[email protected] 
3026b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
3036b4af23dS[email protected] int hci_transport_synchronous(void){
3046b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
3056b4af23dS[email protected] }
3066b4af23dS[email protected] 
307ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
3087856c818Smatthias.ringwald 
3096218e6f1Smatthias.ringwald     // check for free places on BT module
3105932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
3116218e6f1Smatthias.ringwald 
3127856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3135061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
31456cf178bSmatthias.ringwald     if (!connection) return 0;
31556cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
31656cf178bSmatthias.ringwald 
31756cf178bSmatthias.ringwald     // count packet
31856cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
3197b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
3207856c818Smatthias.ringwald 
32100d8e42eSmatthias.ringwald     // send packet
3223a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
3236218e6f1Smatthias.ringwald 
3246b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
325c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
3266b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
3276b4af23dS[email protected]     }
3286b4af23dS[email protected] 
32900d8e42eSmatthias.ringwald     return err;
330ee091cf1Smatthias.ringwald }
331ee091cf1Smatthias.ringwald 
33216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
3337856c818Smatthias.ringwald 
334e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
335e76a89eeS[email protected] 
3367856c818Smatthias.ringwald     // get info
3377856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3385061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
3397856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
3407856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
3417856c818Smatthias.ringwald 
3427856c818Smatthias.ringwald     // ignore non-registered handle
3437856c818Smatthias.ringwald     if (!conn){
3447d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3457856c818Smatthias.ringwald         return;
3467856c818Smatthias.ringwald     }
3477856c818Smatthias.ringwald 
348e76a89eeS[email protected]     // assert packet is complete
3499ecc3e17S[email protected]     if (acl_length + 4 != size){
350e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
351e76a89eeS[email protected]         return;
352e76a89eeS[email protected]     }
353e76a89eeS[email protected] 
3547856c818Smatthias.ringwald     // update idle timestamp
3557856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3567856c818Smatthias.ringwald 
3577856c818Smatthias.ringwald     // handle different packet types
3587856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3597856c818Smatthias.ringwald 
3607856c818Smatthias.ringwald         case 0x01: // continuation fragment
3617856c818Smatthias.ringwald 
3627856c818Smatthias.ringwald             // sanity check
3637856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3647d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3657856c818Smatthias.ringwald                 return;
3667856c818Smatthias.ringwald             }
3677856c818Smatthias.ringwald 
3687856c818Smatthias.ringwald             // append fragment payload (header already stored)
3697856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3707856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3717856c818Smatthias.ringwald 
3727d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
373decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3747856c818Smatthias.ringwald 
3757856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3767856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3777856c818Smatthias.ringwald 
3783a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3797856c818Smatthias.ringwald                 // reset recombination buffer
3807856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3817856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3827856c818Smatthias.ringwald             }
3837856c818Smatthias.ringwald             break;
3847856c818Smatthias.ringwald 
3857856c818Smatthias.ringwald         case 0x02: { // first fragment
3867856c818Smatthias.ringwald 
3877856c818Smatthias.ringwald             // sanity check
3887856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3897d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3907856c818Smatthias.ringwald                 return;
3917856c818Smatthias.ringwald             }
3927856c818Smatthias.ringwald 
3937856c818Smatthias.ringwald             // peek into L2CAP packet!
3947856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3957856c818Smatthias.ringwald 
396e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
397decc01a8Smatthias.ringwald 
3987856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3997856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
4007856c818Smatthias.ringwald 
4017856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
4023a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
4037856c818Smatthias.ringwald 
4047856c818Smatthias.ringwald             } else {
4057856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
4067856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
4077856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
4087856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
409decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
4107856c818Smatthias.ringwald             }
4117856c818Smatthias.ringwald             break;
4127856c818Smatthias.ringwald 
4137856c818Smatthias.ringwald         }
4147856c818Smatthias.ringwald         default:
4157d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
4167856c818Smatthias.ringwald             return;
4177856c818Smatthias.ringwald     }
41894ab26f8Smatthias.ringwald 
41994ab26f8Smatthias.ringwald     // execute main loop
42094ab26f8Smatthias.ringwald     hci_run();
42116833f0aSmatthias.ringwald }
42222909952Smatthias.ringwald 
42367a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
424339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
4253c4d4b90Smatthias.ringwald 
4263c4d4b90Smatthias.ringwald     // cancel all l2cap connections
4273c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
4283c4d4b90Smatthias.ringwald 
429c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
430c785ef68Smatthias.ringwald 
4313a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
432a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
4333c4d4b90Smatthias.ringwald 
4343c4d4b90Smatthias.ringwald     // now it's gone
435c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
436c7e0c5f6Smatthias.ringwald }
437c7e0c5f6Smatthias.ringwald 
4380c042179S[email protected] static const uint16_t packet_type_sizes[] = {
4398f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
4408f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
4418f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4428f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4438f8108aaSmatthias.ringwald };
44465389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
44565389bfcS[email protected]      0, // 3 slot packets
44665389bfcS[email protected]      1, // 5 slot packets
44765389bfcS[email protected]     25, // EDR 2 mpbs
44865389bfcS[email protected]     26, // EDR 3 mbps
44965389bfcS[email protected]     39, // 3 slot EDR packts
45065389bfcS[email protected]     40, // 5 slot EDR packet
45165389bfcS[email protected] };
45265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
45365389bfcS[email protected]     0x0f00, // 3 slot packets
45465389bfcS[email protected]     0xf000, // 5 slot packets
45565389bfcS[email protected]     0x1102, // EDR 2 mpbs
45665389bfcS[email protected]     0x2204, // EDR 3 mbps
45765389bfcS[email protected]     0x0300, // 3 slot EDR packts
45865389bfcS[email protected]     0x3000, // 5 slot EDR packet
45965389bfcS[email protected] };
4608f8108aaSmatthias.ringwald 
46165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
46265389bfcS[email protected]     // enable packet types based on size
4638f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4648f8108aaSmatthias.ringwald     int i;
4658f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4668f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4678f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4688f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4698f8108aaSmatthias.ringwald         }
4708f8108aaSmatthias.ringwald     }
47165389bfcS[email protected]     // disable packet types due to missing local supported features
47265389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
47365389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
47465389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
47565389bfcS[email protected]         if (feature_set) continue;
47665389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
47765389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
47865389bfcS[email protected]     }
4798f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4808f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4818f8108aaSmatthias.ringwald     return packet_types;
4828f8108aaSmatthias.ringwald }
4838f8108aaSmatthias.ringwald 
4848f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4853a9fb326S[email protected]     return hci_stack->packet_types;
4868f8108aaSmatthias.ringwald }
4878f8108aaSmatthias.ringwald 
488facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
4897dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4903a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
4917dc17943Smatthias.ringwald }
4927dc17943Smatthias.ringwald 
493f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4943a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
4957dc17943Smatthias.ringwald }
4967dc17943Smatthias.ringwald 
497f5d8d141S[email protected] int hci_ssp_supported(void){
498f5d8d141S[email protected]     // No 51, byte 6, bit 3
4993a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
500f5d8d141S[email protected] }
501f5d8d141S[email protected] 
502f5d8d141S[email protected] int hci_classic_supported(void){
503f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
5043a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
505f5d8d141S[email protected] }
506f5d8d141S[email protected] 
507f5d8d141S[email protected] int hci_le_supported(void){
508f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
509f5d8d141S[email protected] #ifdef HAVE_BLE
5103a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
511f5d8d141S[email protected] #else
512f5d8d141S[email protected]     return 0;
513f5d8d141S[email protected] #endif
514f5d8d141S[email protected] }
515f5d8d141S[email protected] 
51669a97523S[email protected] // get addr type and address used in advertisement packets
51718904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
5183a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
5193a9fb326S[email protected]     if (hci_stack->adv_addr_type){
5203a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
52169a97523S[email protected]     } else {
5223a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
52369a97523S[email protected]     }
52469a97523S[email protected] }
52569a97523S[email protected] 
526*57c9da5bS[email protected] static void le_handle_advertisement_report(uint8_t *packet, int size){
527*57c9da5bS[email protected]     int num_reports = packet[3];
528*57c9da5bS[email protected]     int i;
529*57c9da5bS[email protected]     int total_data_length = 0;
530*57c9da5bS[email protected]     int data_offset = 0;
531*57c9da5bS[email protected] 
532*57c9da5bS[email protected]     for (i=0; i<num_reports;i++){
533*57c9da5bS[email protected]         total_data_length += packet[4+num_reports*8+i];
534*57c9da5bS[email protected]     }
535*57c9da5bS[email protected] 
536*57c9da5bS[email protected]     for (i=0; i<num_reports;i++){
537*57c9da5bS[email protected]         int pos = 0;
538*57c9da5bS[email protected]         uint8_t data_length = packet[4+num_reports*8+i];
539*57c9da5bS[email protected]         uint8_t event_size = 3 + 9 + data_length + 1;
540*57c9da5bS[email protected]         uint8_t event[event_size];
541*57c9da5bS[email protected]         event[pos++] = GATT_ADVERTISEMENT;
542*57c9da5bS[email protected]         event[pos++] = event_size;
543*57c9da5bS[email protected]         event[pos++] = 0;
544*57c9da5bS[email protected]         event[pos++] = packet[4+i]; // event_type;
545*57c9da5bS[email protected]         event[pos++] = packet[4+num_reports+i]; // address_type;
546*57c9da5bS[email protected]         bt_flip_addr(&event[pos], &packet[4+num_reports*2+i*6]); // bt address
547*57c9da5bS[email protected]         pos += 6;
548*57c9da5bS[email protected]         event[pos++] = data_length;
549*57c9da5bS[email protected]         memcpy(&packet[4+num_reports*9+data_offset], &event[pos], data_length);
550*57c9da5bS[email protected]         data_offset += data_length;
551*57c9da5bS[email protected]         pos += data_length;
552*57c9da5bS[email protected]         event[pos] = packet[4+num_reports*9+total_data_length + i];
553*57c9da5bS[email protected] 
554*57c9da5bS[email protected]         hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
555*57c9da5bS[email protected]     }
556*57c9da5bS[email protected] }
557*57c9da5bS[email protected] 
55874ec757aSmatthias.ringwald // avoid huge local variables
559223aafc1Smatthias.ringwald #ifndef EMBEDDED
56074ec757aSmatthias.ringwald static device_name_t device_name;
561223aafc1Smatthias.ringwald #endif
56216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
563e76a89eeS[email protected] 
564e76a89eeS[email protected]     uint16_t event_length = packet[1];
565e76a89eeS[email protected] 
566e76a89eeS[email protected]     // assert packet is complete
567e76a89eeS[email protected]     if (size != event_length + 2){
568e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
569e76a89eeS[email protected]         return;
570e76a89eeS[email protected]     }
571e76a89eeS[email protected] 
5721281a47eSmatthias.ringwald     bd_addr_t addr;
57396a45072S[email protected]     bd_addr_type_t addr_type;
5742d00edd4Smatthias.ringwald     uint8_t link_type;
575fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
5761f7b95a1Smatthias.ringwald     hci_connection_t * conn;
57756cf178bSmatthias.ringwald     int i;
57822909952Smatthias.ringwald 
5795909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
5805909f7f2Smatthias.ringwald 
5816772a24cSmatthias.ringwald     switch (packet[0]) {
58222909952Smatthias.ringwald 
5836772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
5847ec5eeaaSmatthias.ringwald             // get num cmd packets
5853a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
5863a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
5877ec5eeaaSmatthias.ringwald 
588e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
589e2edc0c3Smatthias.ringwald                 // from offset 5
590e2edc0c3Smatthias.ringwald                 // status
5911d279b20Smatthias.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"
5923a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
59356cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
5943a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
59556cf178bSmatthias.ringwald                 // ignore: total num SCO packets
5963a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
597a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5983a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
5993a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
6008f8108aaSmatthias.ringwald                     }
60165389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
6023a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
603e2edc0c3Smatthias.ringwald                 }
60456cf178bSmatthias.ringwald             }
60565a46ef3S[email protected] #ifdef HAVE_BLE
60665a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
6073a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
6083a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
6093a9fb326S[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);
61065a46ef3S[email protected]             }
61165a46ef3S[email protected] #endif
612188981d2Smatthias.ringwald             // Dump local address
613188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
6143a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
615188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
6163a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
617188981d2Smatthias.ringwald             }
618381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
6193a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
620381fbed8Smatthias.ringwald             }
62165389bfcS[email protected]             // Note: HCI init checks
622559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
6233a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
624559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
6253a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
6263a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
6273a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
6283a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
62965389bfcS[email protected] 
63065389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
6313a9fb326S[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]);
6323a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
633f5d8d141S[email protected] 
634f5d8d141S[email protected]                 // Classic/LE
635f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
636559e517eS[email protected]             }
63756cf178bSmatthias.ringwald             break;
63856cf178bSmatthias.ringwald 
6397ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
6407ec5eeaaSmatthias.ringwald             // get num cmd packets
6413a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
6423a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
6437ec5eeaaSmatthias.ringwald             break;
6447ec5eeaaSmatthias.ringwald 
64556cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
64656cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
64756cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
64856cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
6495061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
65056cf178bSmatthias.ringwald                 if (!conn){
6517d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
65256cf178bSmatthias.ringwald                     continue;
65356cf178bSmatthias.ringwald                 }
65456cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
6557b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
65656cf178bSmatthias.ringwald             }
6576772a24cSmatthias.ringwald             break;
6586772a24cSmatthias.ringwald 
6591f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
66037eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
66137eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
6622d00edd4Smatthias.ringwald             link_type = packet[11];
663339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
66437eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
66596a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
6661f7b95a1Smatthias.ringwald                 if (!conn) {
66796a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
6681f7b95a1Smatthias.ringwald                 }
669ce4c8fabSmatthias.ringwald                 if (!conn) {
670ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
6713a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
6723a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
673ce4c8fabSmatthias.ringwald                     break;
674ce4c8fabSmatthias.ringwald                 }
67532ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
67632ab9390Smatthias.ringwald                 hci_run();
67737eaa4cfSmatthias.ringwald             } else {
678ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
6793a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
6803a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
68137eaa4cfSmatthias.ringwald             }
6821f7b95a1Smatthias.ringwald             break;
6831f7b95a1Smatthias.ringwald 
6846772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
685fe1ed1b8Smatthias.ringwald             // Connection management
686fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
687339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
68896a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
68996a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
690fe1ed1b8Smatthias.ringwald             if (conn) {
691b448a0e7Smatthias.ringwald                 if (!packet[2]){
692c8e4258aSmatthias.ringwald                     conn->state = OPEN;
693fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
694ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
695ee091cf1Smatthias.ringwald 
696c785ef68Smatthias.ringwald                     // restart timer
697c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
698ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
699c785ef68Smatthias.ringwald 
700339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
70143bfb1bdSmatthias.ringwald 
70243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
703b448a0e7Smatthias.ringwald                 } else {
704ad83dc6aS[email protected]                     // notify client if dedicated bonding
705ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
706ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
707ad83dc6aS[email protected]                     }
708ad83dc6aS[email protected] 
709b448a0e7Smatthias.ringwald                     // connection failed, remove entry
7103a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
711a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
712c12e46e7Smatthias.ringwald 
713c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
714c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
715c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
716c12e46e7Smatthias.ringwald                     }
717fe1ed1b8Smatthias.ringwald                 }
718fe1ed1b8Smatthias.ringwald             }
7196772a24cSmatthias.ringwald             break;
720fe1ed1b8Smatthias.ringwald 
721afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
722afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
723afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
724afd4e962S[email protected]             if (!conn) break;
725afd4e962S[email protected]             if (!packet[2]){
726afd4e962S[email protected]                 uint8_t * features = &packet[5];
727afd4e962S[email protected]                 if (features[6] & (1 << 3)){
728afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
729afd4e962S[email protected]                 }
730afd4e962S[email protected]             }
731afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
732ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
733ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
734ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
735ad83dc6aS[email protected]             }
736afd4e962S[email protected]             break;
737afd4e962S[email protected] 
7387fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
7397b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
7407fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
74174d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
7423a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
74332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
74464472d52Smatthias.ringwald             hci_run();
745d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
74629d53098Smatthias.ringwald             return;
7477fde4af9Smatthias.ringwald 
7489ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
74929d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
75096a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
7519ab95c90S[email protected]             if (!conn) break;
7529ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
7537bdc6798S[email protected]             link_key_type_t link_key_type = (link_key_type_t)packet[24];
7549ab95c90S[email protected]             // Change Connection Encryption keeps link key type
7559ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
7569ab95c90S[email protected]                 conn->link_key_type = link_key_type;
7579ab95c90S[email protected]             }
7583a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
7593a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
76029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
7617fde4af9Smatthias.ringwald             break;
7629ab95c90S[email protected]         }
7637fde4af9Smatthias.ringwald 
7647fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
7656724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
7664c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
7673a9fb326S[email protected]             if (!hci_stack->bondable){
768899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
769f8fb5f6eS[email protected]                 hci_run();
770f8fb5f6eS[email protected]                 return;
7714c57c146S[email protected]             }
772d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
7733a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
774d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
7753a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
7767fde4af9Smatthias.ringwald             break;
7777fde4af9Smatthias.ringwald 
7781d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
7791d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
780dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
781dbe1a790S[email protected]             break;
782dbe1a790S[email protected] 
783dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
7846724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7853a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
786dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
787dbe1a790S[email protected]             break;
788dbe1a790S[email protected] 
789dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
7906724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7913a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
792dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
7931d6b20aeS[email protected]             break;
7941d6b20aeS[email protected] 
795f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
796f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
797f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
798f0944df2S[email protected]             if (!conn) break;
799ad83dc6aS[email protected]             if (packet[2] == 0) {
800f0944df2S[email protected]                 if (packet[5]){
801f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
802f0944df2S[email protected]                 } else {
803536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
804f0944df2S[email protected]                 }
805ad83dc6aS[email protected]             }
806a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
807f0944df2S[email protected]             break;
808f0944df2S[email protected] 
8091eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
8101eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
8111eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
8121eb2563eS[email protected]             if (!conn) break;
813ad83dc6aS[email protected] 
814ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
815ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
816ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
817ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
818ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
819ad83dc6aS[email protected]                 break;
820ad83dc6aS[email protected]             }
821ad83dc6aS[email protected] 
822b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
8231eb2563eS[email protected]                 // link key sufficient for requested security
8241eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
825ad83dc6aS[email protected]                 break;
826ad83dc6aS[email protected]             }
8271eb2563eS[email protected]             // not enough
8281eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
8291eb2563eS[email protected]             break;
83034d2123cS[email protected] 
831223aafc1Smatthias.ringwald #ifndef EMBEDDED
83274ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
8333a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
83474ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
83574ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
8365a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
8375a06394aSmatthias.ringwald             for (i=0; i<248;i++){
8385a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
8395a06394aSmatthias.ringwald                     packet[9+i] = 0;
8405a06394aSmatthias.ringwald                     break;
841cdc9101dSmatthias.ringwald                 }
8425a06394aSmatthias.ringwald             }
843d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
84474ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
8453a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
84674ec757aSmatthias.ringwald             break;
84774ec757aSmatthias.ringwald 
84874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
84974ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
8503a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
85174ec757aSmatthias.ringwald             // first send inq result packet
8523a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
85374ec757aSmatthias.ringwald             // then send cached remote names
85474ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
85574ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
8563a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
85774ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
85874ec757aSmatthias.ringwald                 }
85974ec757aSmatthias.ringwald             }
86074ec757aSmatthias.ringwald             return;
861223aafc1Smatthias.ringwald #endif
86274ec757aSmatthias.ringwald 
8636772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
864fe1ed1b8Smatthias.ringwald             if (!packet[2]){
865fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
8665061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
867fe1ed1b8Smatthias.ringwald                 if (conn) {
868c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
869fe1ed1b8Smatthias.ringwald                 }
870fe1ed1b8Smatthias.ringwald             }
8716772a24cSmatthias.ringwald             break;
8726772a24cSmatthias.ringwald 
873c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
8743a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
8753a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
876c68bdf90Smatthias.ringwald             }
877c68bdf90Smatthias.ringwald             break;
878c68bdf90Smatthias.ringwald 
8796b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
8806b4af23dS[email protected]             // free packet buffer for asynchronous transport
8816b4af23dS[email protected]             if (hci_transport_synchronous()) break;
8826b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
8836b4af23dS[email protected]             break;
8846b4af23dS[email protected] 
8855909f7f2Smatthias.ringwald #ifdef HAVE_BLE
8865909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
8875909f7f2Smatthias.ringwald             switch (packet[2]){
888*57c9da5bS[email protected]                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
8897bdc6798S[email protected]                     if (hci_stack->le_scanning_state != LE_SCANNING) break;
890*57c9da5bS[email protected]                     le_handle_advertisement_report(packet, size);
8917bdc6798S[email protected]                     break;
8925909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
8935909f7f2Smatthias.ringwald                     // Connection management
8945909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
8957bdc6798S[email protected]                     addr_type = (bd_addr_type_t)packet[7];
89696a45072S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr));
8975909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
89896a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
8995909f7f2Smatthias.ringwald                     if (packet[3]){
9005909f7f2Smatthias.ringwald                         if (conn){
9015909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
9023a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
9035909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
9045909f7f2Smatthias.ringwald 
9055909f7f2Smatthias.ringwald                         }
9065909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
9075909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
9085909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
9095909f7f2Smatthias.ringwald                         }
9105909f7f2Smatthias.ringwald                         break;
9115909f7f2Smatthias.ringwald                     }
9125909f7f2Smatthias.ringwald                     if (!conn){
91396a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
9145909f7f2Smatthias.ringwald                     }
9155909f7f2Smatthias.ringwald                     if (!conn){
9165909f7f2Smatthias.ringwald                         // no memory
9175909f7f2Smatthias.ringwald                         break;
9185909f7f2Smatthias.ringwald                     }
9195909f7f2Smatthias.ringwald 
9205909f7f2Smatthias.ringwald                     conn->state = OPEN;
9215909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
9225909f7f2Smatthias.ringwald 
9235909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
9245909f7f2Smatthias.ringwald 
9255909f7f2Smatthias.ringwald                     // restart timer
9265909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
9275909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
9285909f7f2Smatthias.ringwald 
9295909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
9305909f7f2Smatthias.ringwald 
9315909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
9325909f7f2Smatthias.ringwald                     break;
9335909f7f2Smatthias.ringwald 
93465a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
93565a46ef3S[email protected] 
9365909f7f2Smatthias.ringwald                 default:
9375909f7f2Smatthias.ringwald                     break;
9385909f7f2Smatthias.ringwald             }
9395909f7f2Smatthias.ringwald             break;
9405909f7f2Smatthias.ringwald #endif
9415909f7f2Smatthias.ringwald 
9426772a24cSmatthias.ringwald         default:
9436772a24cSmatthias.ringwald             break;
944fe1ed1b8Smatthias.ringwald     }
945fe1ed1b8Smatthias.ringwald 
9463429f56bSmatthias.ringwald     // handle BT initialization
9473a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
9483a9fb326S[email protected]         if (hci_stack->substate % 2){
9493429f56bSmatthias.ringwald             // odd: waiting for event
950f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
951c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
9523a9fb326S[email protected]                 if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
9533a9fb326S[email protected]                     hci_stack->substate++;
9543429f56bSmatthias.ringwald                 }
9553429f56bSmatthias.ringwald             }
95622909952Smatthias.ringwald         }
957c9af4d3fS[email protected]     }
95822909952Smatthias.ringwald 
95989db417bSmatthias.ringwald     // help with BT sleep
9603a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
9613a9fb326S[email protected]         && hci_stack->substate == 1
96289db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
9633a9fb326S[email protected]         hci_stack->substate++;
96489db417bSmatthias.ringwald     }
96589db417bSmatthias.ringwald 
9663a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
96794ab26f8Smatthias.ringwald 
96894ab26f8Smatthias.ringwald 	// execute main loop
96994ab26f8Smatthias.ringwald 	hci_run();
97016833f0aSmatthias.ringwald }
97116833f0aSmatthias.ringwald 
9720a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
97310e830c9Smatthias.ringwald     switch (packet_type) {
97410e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
97510e830c9Smatthias.ringwald             event_handler(packet, size);
97610e830c9Smatthias.ringwald             break;
97710e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
97810e830c9Smatthias.ringwald             acl_handler(packet, size);
97910e830c9Smatthias.ringwald             break;
98010e830c9Smatthias.ringwald         default:
98110e830c9Smatthias.ringwald             break;
98210e830c9Smatthias.ringwald     }
98310e830c9Smatthias.ringwald }
98410e830c9Smatthias.ringwald 
985fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
9862718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
9873a9fb326S[email protected]     hci_stack->packet_handler = handler;
98816833f0aSmatthias.ringwald }
98916833f0aSmatthias.ringwald 
9904f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
991475c8125Smatthias.ringwald 
9923a9fb326S[email protected] #ifdef HAVE_MALLOC
9933a9fb326S[email protected]     if (!hci_stack) {
9943a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
9953a9fb326S[email protected]     }
9963a9fb326S[email protected] #else
9973a9fb326S[email protected]     hci_stack = &hci_stack_static;
9983a9fb326S[email protected] #endif
99966fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
10003a9fb326S[email protected] 
1001475c8125Smatthias.ringwald     // reference to use transport layer implementation
10023a9fb326S[email protected]     hci_stack->hci_transport = transport;
1003475c8125Smatthias.ringwald 
100411e23e5fSmatthias.ringwald     // references to used control implementation
10053a9fb326S[email protected]     hci_stack->control = control;
100611e23e5fSmatthias.ringwald 
100711e23e5fSmatthias.ringwald     // reference to used config
10083a9fb326S[email protected]     hci_stack->config = config;
100911e23e5fSmatthias.ringwald 
1010fe1ed1b8Smatthias.ringwald     // no connections yet
10113a9fb326S[email protected]     hci_stack->connections = NULL;
10123a9fb326S[email protected]     hci_stack->discoverable = 0;
10133a9fb326S[email protected]     hci_stack->connectable = 0;
10143a9fb326S[email protected]     hci_stack->bondable = 1;
1015758b46ceSmatthias.ringwald 
1016b031bebbSmatthias.ringwald     // no pending cmds
10173a9fb326S[email protected]     hci_stack->decline_reason = 0;
10183a9fb326S[email protected]     hci_stack->new_scan_enable_value = 0xff;
1019b031bebbSmatthias.ringwald 
102016833f0aSmatthias.ringwald     // higher level handler
10213a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
102216833f0aSmatthias.ringwald 
1023404843c1Smatthias.ringwald     // store and open remote device db
10243a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
10253a9fb326S[email protected]     if (hci_stack->remote_device_db) {
10263a9fb326S[email protected]         hci_stack->remote_device_db->open();
1027404843c1Smatthias.ringwald     }
102829d53098Smatthias.ringwald 
10298fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
10303a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
10318fcba05dSmatthias.ringwald 
103216833f0aSmatthias.ringwald     // register packet handlers with transport
103310e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
1034f5454fc6Smatthias.ringwald 
10353a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
1036e2386ba1S[email protected] 
1037e2386ba1S[email protected]     // class of device
10383a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
1039a45d6b9fS[email protected] 
104063048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
10413a9fb326S[email protected]     hci_stack->ssp_enable = 1;
10423a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
10433a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
10443a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
104569a97523S[email protected] 
104669a97523S[email protected]     // LE
10473a9fb326S[email protected]     hci_stack->adv_addr_type = 0;
10483a9fb326S[email protected]     memset(hci_stack->adv_address, 0, 6);
10497bdc6798S[email protected]     hci_stack->le_scanning_state = LE_SCAN_IDLE;
1050475c8125Smatthias.ringwald }
1051475c8125Smatthias.ringwald 
1052404843c1Smatthias.ringwald void hci_close(){
1053404843c1Smatthias.ringwald     // close remote device db
10543a9fb326S[email protected]     if (hci_stack->remote_device_db) {
10553a9fb326S[email protected]         hci_stack->remote_device_db->close();
1056404843c1Smatthias.ringwald     }
10573a9fb326S[email protected]     while (hci_stack->connections) {
10583a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1059f5454fc6Smatthias.ringwald     }
1060f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
10613a9fb326S[email protected] 
10623a9fb326S[email protected] #ifdef HAVE_MALLOC
10633a9fb326S[email protected]     free(hci_stack);
10643a9fb326S[email protected] #endif
10653a9fb326S[email protected]     hci_stack = NULL;
1066404843c1Smatthias.ringwald }
1067404843c1Smatthias.ringwald 
10689e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
10699e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
10709e61646fS[email protected] }
10719e61646fS[email protected] 
107266fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
107366fb9560S[email protected]     disable_l2cap_timeouts = 1;
107466fb9560S[email protected] }
10758d213e1aSmatthias.ringwald // State-Module-Driver overview
10768d213e1aSmatthias.ringwald // state                    module  low-level
10778d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
10788d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
10798d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
10808d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1081d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1082d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1083c7e0c5f6Smatthias.ringwald 
108440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
10857301ad89Smatthias.ringwald 
1086038bc64cSmatthias.ringwald     // power on
1087f9a30166Smatthias.ringwald     int err = 0;
10883a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
10893a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1090f9a30166Smatthias.ringwald     }
1091038bc64cSmatthias.ringwald     if (err){
10927d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1093038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1094038bc64cSmatthias.ringwald         return err;
1095038bc64cSmatthias.ringwald     }
1096038bc64cSmatthias.ringwald 
1097038bc64cSmatthias.ringwald     // open low-level device
10983a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1099038bc64cSmatthias.ringwald     if (err){
11007d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
11013a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
11023a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1103f9a30166Smatthias.ringwald         }
1104038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1105038bc64cSmatthias.ringwald         return err;
1106038bc64cSmatthias.ringwald     }
11078d213e1aSmatthias.ringwald     return 0;
11088d213e1aSmatthias.ringwald }
1109038bc64cSmatthias.ringwald 
111040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
11118d213e1aSmatthias.ringwald 
11127b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
11139418f9c9Smatthias.ringwald 
11148d213e1aSmatthias.ringwald     // close low-level device
11153a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
11168d213e1aSmatthias.ringwald 
11177b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
11189418f9c9Smatthias.ringwald 
11198d213e1aSmatthias.ringwald     // power off
11203a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
11213a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
11228d213e1aSmatthias.ringwald     }
11239418f9c9Smatthias.ringwald 
11247b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
11259418f9c9Smatthias.ringwald 
11263a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
112772ea5239Smatthias.ringwald }
112872ea5239Smatthias.ringwald 
112940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
113072ea5239Smatthias.ringwald 
11317b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
11323144bce4Smatthias.ringwald 
1133b429b9b7Smatthias.ringwald #if 0
1134b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1135b429b9b7Smatthias.ringwald 
113672ea5239Smatthias.ringwald     // close low-level device
11373a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1138b429b9b7Smatthias.ringwald #endif
113972ea5239Smatthias.ringwald 
114072ea5239Smatthias.ringwald     // sleep mode
11413a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
11423a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
114372ea5239Smatthias.ringwald     }
1144b429b9b7Smatthias.ringwald 
11453a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
11468d213e1aSmatthias.ringwald }
11478d213e1aSmatthias.ringwald 
114840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1149b429b9b7Smatthias.ringwald 
11507b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1151b429b9b7Smatthias.ringwald 
1152b429b9b7Smatthias.ringwald     // wake on
11533a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
11543a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1155b429b9b7Smatthias.ringwald     }
1156b429b9b7Smatthias.ringwald 
1157b429b9b7Smatthias.ringwald #if 0
1158b429b9b7Smatthias.ringwald     // open low-level device
11593a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1160b429b9b7Smatthias.ringwald     if (err){
11617d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
11623a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
11633a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1164b429b9b7Smatthias.ringwald         }
1165b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1166b429b9b7Smatthias.ringwald         return err;
1167b429b9b7Smatthias.ringwald     }
1168b429b9b7Smatthias.ringwald #endif
1169b429b9b7Smatthias.ringwald 
1170b429b9b7Smatthias.ringwald     return 0;
1171b429b9b7Smatthias.ringwald }
1172b429b9b7Smatthias.ringwald 
1173b429b9b7Smatthias.ringwald 
11748d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1175d661ed19Smatthias.ringwald 
11763a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1177d661ed19Smatthias.ringwald 
11788d213e1aSmatthias.ringwald     int err = 0;
11793a9fb326S[email protected]     switch (hci_stack->state){
11808d213e1aSmatthias.ringwald 
11818d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
11828d213e1aSmatthias.ringwald             switch (power_mode){
11838d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11848d213e1aSmatthias.ringwald                     err = hci_power_control_on();
11858d213e1aSmatthias.ringwald                     if (err) return err;
11867301ad89Smatthias.ringwald                     // set up state machine
11873a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
11883a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11893a9fb326S[email protected]                     hci_stack->substate = 0;
11908d213e1aSmatthias.ringwald                     break;
11918d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11928d213e1aSmatthias.ringwald                     // do nothing
11938d213e1aSmatthias.ringwald                     break;
11948d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1195b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
11968d213e1aSmatthias.ringwald                     break;
11978d213e1aSmatthias.ringwald             }
11988d213e1aSmatthias.ringwald             break;
11997301ad89Smatthias.ringwald 
12008d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
12018d213e1aSmatthias.ringwald             switch (power_mode){
12028d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
12038d213e1aSmatthias.ringwald                     // do nothing
12048d213e1aSmatthias.ringwald                     break;
12058d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12068d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
12078d213e1aSmatthias.ringwald                     hci_power_control_off();
12088d213e1aSmatthias.ringwald                     break;
12098d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1210b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
121172ea5239Smatthias.ringwald                     hci_power_control_sleep();
12128d213e1aSmatthias.ringwald                     break;
12138d213e1aSmatthias.ringwald             }
12148d213e1aSmatthias.ringwald             break;
12157301ad89Smatthias.ringwald 
12168d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
12178d213e1aSmatthias.ringwald             switch (power_mode){
12188d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
12198d213e1aSmatthias.ringwald                     // do nothing
12208d213e1aSmatthias.ringwald                     break;
12218d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1222c7e0c5f6Smatthias.ringwald                     // see hci_run
12233a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12248d213e1aSmatthias.ringwald                     break;
12258d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1226b546ac54Smatthias.ringwald                     // see hci_run
12273a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
12283a9fb326S[email protected]                     hci_stack->substate = 0;
12298d213e1aSmatthias.ringwald                     break;
12308d213e1aSmatthias.ringwald             }
12318d213e1aSmatthias.ringwald             break;
12327301ad89Smatthias.ringwald 
12338d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
12348d213e1aSmatthias.ringwald             switch (power_mode){
12358d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
12368d213e1aSmatthias.ringwald                     // set up state machine
12373a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12383a9fb326S[email protected]                     hci_stack->substate = 0;
12398d213e1aSmatthias.ringwald                     break;
12408d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12418d213e1aSmatthias.ringwald                     // do nothing
12428d213e1aSmatthias.ringwald                     break;
12438d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1244b546ac54Smatthias.ringwald                     // see hci_run
12453a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
12463a9fb326S[email protected]                     hci_stack->substate = 0;
12478d213e1aSmatthias.ringwald                     break;
12488d213e1aSmatthias.ringwald             }
12498d213e1aSmatthias.ringwald             break;
12508d213e1aSmatthias.ringwald 
12518d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
12528d213e1aSmatthias.ringwald             switch (power_mode){
12538d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
125428171530Smatthias.ringwald 
125528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
125628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
125728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
12583a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
12593a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
126028171530Smatthias.ringwald                         break;
126128171530Smatthias.ringwald                     }
126228171530Smatthias.ringwald #endif
1263b546ac54Smatthias.ringwald                     // set up state machine
12643a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12653a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12663a9fb326S[email protected]                     hci_stack->substate = 0;
12678d213e1aSmatthias.ringwald                     break;
12688d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1269b546ac54Smatthias.ringwald                     // see hci_run
12703a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12718d213e1aSmatthias.ringwald                     break;
12728d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1273b546ac54Smatthias.ringwald                     // do nothing
12748d213e1aSmatthias.ringwald                     break;
12758d213e1aSmatthias.ringwald             }
12768d213e1aSmatthias.ringwald             break;
12778d213e1aSmatthias.ringwald 
12788d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
12798d213e1aSmatthias.ringwald             switch (power_mode){
12808d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
128128171530Smatthias.ringwald 
128228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
128328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
128428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
12853a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
12863a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1287758b46ceSmatthias.ringwald                         hci_update_scan_enable();
128828171530Smatthias.ringwald                         break;
128928171530Smatthias.ringwald                     }
129028171530Smatthias.ringwald #endif
12913144bce4Smatthias.ringwald                     err = hci_power_control_wake();
12923144bce4Smatthias.ringwald                     if (err) return err;
1293b546ac54Smatthias.ringwald                     // set up state machine
12943a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12953a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12963a9fb326S[email protected]                     hci_stack->substate = 0;
12978d213e1aSmatthias.ringwald                     break;
12988d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12993a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
13008d213e1aSmatthias.ringwald                     break;
13018d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1302b546ac54Smatthias.ringwald                     // do nothing
13038d213e1aSmatthias.ringwald                     break;
13048d213e1aSmatthias.ringwald             }
13058d213e1aSmatthias.ringwald             break;
130611e23e5fSmatthias.ringwald     }
130768d92d03Smatthias.ringwald 
1308038bc64cSmatthias.ringwald     // create internal event
1309ee8bf225Smatthias.ringwald 	hci_emit_state();
1310ee8bf225Smatthias.ringwald 
131168d92d03Smatthias.ringwald 	// trigger next/first action
131268d92d03Smatthias.ringwald 	hci_run();
131368d92d03Smatthias.ringwald 
1314475c8125Smatthias.ringwald     return 0;
1315475c8125Smatthias.ringwald }
1316475c8125Smatthias.ringwald 
1317758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1318758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
13193a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1320758b46ceSmatthias.ringwald     hci_run();
1321758b46ceSmatthias.ringwald }
1322758b46ceSmatthias.ringwald 
1323381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1324381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1325381fbed8Smatthias.ringwald 
13263a9fb326S[email protected]     if (hci_stack->discoverable == enable){
13273a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1328381fbed8Smatthias.ringwald         return;
1329381fbed8Smatthias.ringwald     }
1330381fbed8Smatthias.ringwald 
13313a9fb326S[email protected]     hci_stack->discoverable = enable;
1332758b46ceSmatthias.ringwald     hci_update_scan_enable();
1333758b46ceSmatthias.ringwald }
1334b031bebbSmatthias.ringwald 
1335758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1336758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1337758b46ceSmatthias.ringwald 
1338758b46ceSmatthias.ringwald     // don't emit event
13393a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1340758b46ceSmatthias.ringwald 
13413a9fb326S[email protected]     hci_stack->connectable = enable;
1342758b46ceSmatthias.ringwald     hci_update_scan_enable();
1343381fbed8Smatthias.ringwald }
1344381fbed8Smatthias.ringwald 
13455061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
13463a9fb326S[email protected]     return &hci_stack->local_bd_addr;
13475061f3afS[email protected] }
13485061f3afS[email protected] 
134906b35ec0Smatthias.ringwald void hci_run(){
13508a485f27Smatthias.ringwald 
135132ab9390Smatthias.ringwald     hci_connection_t * connection;
135232ab9390Smatthias.ringwald     linked_item_t * it;
135332ab9390Smatthias.ringwald 
135464f0b431S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
1355ce4c8fabSmatthias.ringwald 
1356b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1357b031bebbSmatthias.ringwald 
1358b031bebbSmatthias.ringwald     // decline incoming connections
13593a9fb326S[email protected]     if (hci_stack->decline_reason){
13603a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
13613a9fb326S[email protected]         hci_stack->decline_reason = 0;
13623a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1363dbe1a790S[email protected]         return;
1364ce4c8fabSmatthias.ringwald     }
1365ce4c8fabSmatthias.ringwald 
1366b031bebbSmatthias.ringwald     // send scan enable
13673a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
13683a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
13693a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1370dbe1a790S[email protected]         return;
1371b031bebbSmatthias.ringwald     }
1372b031bebbSmatthias.ringwald 
13737bdc6798S[email protected]     // handle le scan
13747bdc6798S[email protected]     if (hci_stack->state == HCI_STATE_WORKING){
13757bdc6798S[email protected]         switch(hci_stack->le_scanning_state){
13767bdc6798S[email protected]             case LE_START_SCAN:
13777bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCANNING;
13787bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 1, 0);
13797bdc6798S[email protected]                 return;
13807bdc6798S[email protected] 
13817bdc6798S[email protected]             case LE_STOP_SCAN:
13827bdc6798S[email protected]                 hci_stack->le_scanning_state = LE_SCAN_IDLE;
13837bdc6798S[email protected]                 hci_send_cmd(&hci_le_set_scan_enable, 0, 0);
13847bdc6798S[email protected]                 return;
13857bdc6798S[email protected]             default:
13867bdc6798S[email protected]                 break;
13877bdc6798S[email protected]         }
13887bdc6798S[email protected]     }
13897bdc6798S[email protected] 
139032ab9390Smatthias.ringwald     // send pending HCI commands
13913a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
139232ab9390Smatthias.ringwald 
1393b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
139432ab9390Smatthias.ringwald 
1395ad83dc6aS[email protected]         if (connection->state == SEND_CREATE_CONNECTION){
1396ad83dc6aS[email protected]             log_info("sending hci_create_connection\n");
1397ad83dc6aS[email protected]             hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
1398ad83dc6aS[email protected]             return;
1399ad83dc6aS[email protected]         }
1400ad83dc6aS[email protected] 
140132ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
14027b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
140332ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
140434d2123cS[email protected]             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1405dbe1a790S[email protected]             return;
1406c7e0c5f6Smatthias.ringwald         }
1407c7e0c5f6Smatthias.ringwald 
140832ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
140934d2123cS[email protected]             log_info("responding to link key request\n");
141034d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
141132ab9390Smatthias.ringwald             link_key_t link_key;
1412c77e8838S[email protected]             link_key_type_t link_key_type;
14133a9fb326S[email protected]             if ( hci_stack->remote_device_db
14143a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
141534d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
14169ab95c90S[email protected]                connection->link_key_type = link_key_type;
141732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
141832ab9390Smatthias.ringwald             } else {
141932ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
142032ab9390Smatthias.ringwald             }
1421dbe1a790S[email protected]             return;
142232ab9390Smatthias.ringwald         }
14231d6b20aeS[email protected] 
1424899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
14254c57c146S[email protected]             log_info("denying to pin request\n");
1426899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
142734d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
14284c57c146S[email protected]             return;
14294c57c146S[email protected]         }
14304c57c146S[email protected] 
1431dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
143234d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
143382d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
143482d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1435106d6d11S[email protected]                 // tweak authentication requirements
14363a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1437106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
14389faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
14399faad3abS[email protected]                 }
14409faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
14419faad3abS[email protected]                     authreq |= 1;
1442106d6d11S[email protected]                 }
14433a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1444f8fb5f6eS[email protected]             } else {
1445f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1446f8fb5f6eS[email protected]             }
1447dbe1a790S[email protected]             return;
144832ab9390Smatthias.ringwald         }
144932ab9390Smatthias.ringwald 
1450dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1451dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
145234d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1453dbe1a790S[email protected]             return;
1454dbe1a790S[email protected]         }
1455dbe1a790S[email protected] 
1456dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1457dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
145834d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1459dbe1a790S[email protected]             return;
1460dbe1a790S[email protected]         }
1461afd4e962S[email protected] 
1462afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1463afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
146434d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
14652bd8b7e7S[email protected]             return;
14662bd8b7e7S[email protected]         }
14672bd8b7e7S[email protected] 
14682bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
14692bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
147034d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
147134d2123cS[email protected]             return;
147234d2123cS[email protected]         }
1473ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1474ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
147552d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1476ad83dc6aS[email protected]             return;
1477ad83dc6aS[email protected]         }
147834d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
147934d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
148034d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
14812bd8b7e7S[email protected]             return;
1482afd4e962S[email protected]         }
1483dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1484dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1485dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1486dce78009S[email protected]             return;
1487dce78009S[email protected]         }
1488dbe1a790S[email protected]     }
1489c7e0c5f6Smatthias.ringwald 
14903a9fb326S[email protected]     switch (hci_stack->state){
14913429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
14923a9fb326S[email protected]             // log_info("hci_init: substate %u\n", hci_stack->substate);
14933a9fb326S[email protected]             if (hci_stack->substate % 2) {
14943429f56bSmatthias.ringwald                 // odd: waiting for command completion
149506b35ec0Smatthias.ringwald                 return;
14963429f56bSmatthias.ringwald             }
14973a9fb326S[email protected]             switch (hci_stack->substate >> 1){
1498c7492964Smatthias.ringwald                 case 0: // RESET
149922909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
150024052c2aS[email protected] 
15013a9fb326S[email protected]                     if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
1502c7492964Smatthias.ringwald                         // skip baud change
15033a9fb326S[email protected]                         hci_stack->substate = 4; // >> 1 = 2
1504c7492964Smatthias.ringwald                     }
15053429f56bSmatthias.ringwald                     break;
1506c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
15073a9fb326S[email protected]                     hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
15083a9fb326S[email protected]                     hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1509c7492964Smatthias.ringwald                     break;
1510c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
15113a9fb326S[email protected]                     hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
15123a9fb326S[email protected]                     hci_stack->substate += 2;
1513c7492964Smatthias.ringwald                     // break missing here for fall through
1514c7492964Smatthias.ringwald 
1515c7492964Smatthias.ringwald                 case 3:
151624052c2aS[email protected]                     // Custom initialization
15173a9fb326S[email protected]                     if (hci_stack->control && hci_stack->control->next_cmd){
15183a9fb326S[email protected]                         int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
1519d62aca9fSmatthias.ringwald                         if (valid_cmd){
15203a9fb326S[email protected]                             int size = 3 + hci_stack->hci_packet_buffer[2];
15213a9fb326S[email protected]                             hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
15223a9fb326S[email protected]                             hci_stack->substate = 4; // more init commands
152390919203Smatthias.ringwald                             break;
152490919203Smatthias.ringwald                         }
1525d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
152690919203Smatthias.ringwald                     }
15272f6c30e1Smatthias.ringwald                     // otherwise continue
1528f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1529f432a6ddSmatthias.ringwald 					break;
1530c7492964Smatthias.ringwald 				case 4:
1531e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1532e2edc0c3Smatthias.ringwald 					break;
1533c7492964Smatthias.ringwald                 case 5:
153465389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
15353429f56bSmatthias.ringwald                     break;
1536c7492964Smatthias.ringwald                 case 6:
1537d7e0e3a8S[email protected]                     if (hci_le_supported()){
1538d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1539d7e0e3a8S[email protected]                     } else {
1540d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1541d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1542d7e0e3a8S[email protected]                     }
1543f5d8d141S[email protected] 
1544f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
154524052c2aS[email protected]                     if (!hci_classic_supported()){
154624052c2aS[email protected]                         if (hci_le_supported()){
15473a9fb326S[email protected]                             hci_stack->substate = 11 << 1;    // skip all classic command
154824052c2aS[email protected]                         } else {
154924052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
15503a9fb326S[email protected]                             hci_stack->substate = 13 << 1;    // skip all
155124052c2aS[email protected]                         }
1552f5d8d141S[email protected]                     }
1553f5d8d141S[email protected]                     break;
1554f5d8d141S[email protected]                 case 7:
155524052c2aS[email protected]                     if (hci_ssp_supported()){
15563a9fb326S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
1557f5d8d141S[email protected]                         break;
155824052c2aS[email protected]                     }
15593a9fb326S[email protected]                     hci_stack->substate += 2;
156024052c2aS[email protected]                     // break missing here for fall through
156124052c2aS[email protected] 
1562f5d8d141S[email protected]                 case 8:
156365389bfcS[email protected]                     // ca. 15 sec
156465389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1565559e517eS[email protected]                     break;
1566f5d8d141S[email protected]                 case 9:
15673a9fb326S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
1568e2386ba1S[email protected]                     break;
1569f5d8d141S[email protected]                 case 10:
15703a9fb326S[email protected]                     if (hci_stack->local_name){
15713a9fb326S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
1572e2386ba1S[email protected]                     } else {
15738a485f27Smatthias.ringwald                         char hostname[30];
1574e2386ba1S[email protected] #ifdef EMBEDDED
1575e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1576e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
15773a9fb326S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
1578e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1579e2386ba1S[email protected] #else
1580e2386ba1S[email protected]                         // hostname for POSIX systems
15818a485f27Smatthias.ringwald                         gethostname(hostname, 30);
15828a485f27Smatthias.ringwald                         hostname[29] = '\0';
1583e2386ba1S[email protected] #endif
15848a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
15858a485f27Smatthias.ringwald                     }
15863c4d4b90Smatthias.ringwald                     break;
1587e0dbca83S[email protected]                 case 11:
15883a9fb326S[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
158924052c2aS[email protected]                     if (!hci_le_supported()){
159024052c2aS[email protected]                         // SKIP LE init for Classic only configuration
15913a9fb326S[email protected]                         hci_stack->substate = 13 << 1;
159224052c2aS[email protected]                     }
1593a45d6b9fS[email protected] 					break;
159424052c2aS[email protected] 
159543aa7007S[email protected] #ifdef HAVE_BLE
159624052c2aS[email protected]                 // LE INIT
1597a45d6b9fS[email protected]                 case 12:
159824052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
159924052c2aS[email protected]                     break;
160024052c2aS[email protected]                 case 13:
160124052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
160224052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
160324052c2aS[email protected]                     break;
160443aa7007S[email protected] #endif
160524052c2aS[email protected] 
160624052c2aS[email protected]                 // DONE
160724052c2aS[email protected]                 case 14:
16083429f56bSmatthias.ringwald                     // done.
16093a9fb326S[email protected]                     hci_stack->state = HCI_STATE_WORKING;
1610b360b6adSmatthias.ringwald                     hci_emit_state();
16113429f56bSmatthias.ringwald                     break;
16123429f56bSmatthias.ringwald                 default:
16133429f56bSmatthias.ringwald                     break;
1614475c8125Smatthias.ringwald             }
16153a9fb326S[email protected]             hci_stack->substate++;
16163429f56bSmatthias.ringwald             break;
1617c7e0c5f6Smatthias.ringwald 
1618c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1619c7e0c5f6Smatthias.ringwald 
16207b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1621c7e0c5f6Smatthias.ringwald             // close all open connections
16223a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1623c7e0c5f6Smatthias.ringwald             if (connection){
162432ab9390Smatthias.ringwald 
1625c7e0c5f6Smatthias.ringwald                 // send disconnect
162664f0b431S[email protected]                 if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
162732ab9390Smatthias.ringwald 
162879bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
16296ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1630c7e0c5f6Smatthias.ringwald 
1631c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1632c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1633c7e0c5f6Smatthias.ringwald                 return;
1634c7e0c5f6Smatthias.ringwald             }
16357b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1636c7e0c5f6Smatthias.ringwald 
163772ea5239Smatthias.ringwald             // switch mode
1638c7e0c5f6Smatthias.ringwald             hci_power_control_off();
16399418f9c9Smatthias.ringwald 
16407b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
164172ea5239Smatthias.ringwald             hci_emit_state();
16427b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
164372ea5239Smatthias.ringwald             break;
1644c7e0c5f6Smatthias.ringwald 
164572ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
16463a9fb326S[email protected]             switch(hci_stack->substate) {
164789db417bSmatthias.ringwald                 case 0:
16487b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
164972ea5239Smatthias.ringwald                     // close all open connections
16503a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
165166da7044Smatthias.ringwald 
165228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
165366da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
165466da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
165566da7044Smatthias.ringwald                         connection = NULL;
165666da7044Smatthias.ringwald                     }
165766da7044Smatthias.ringwald #endif
165872ea5239Smatthias.ringwald                     if (connection){
165932ab9390Smatthias.ringwald 
166072ea5239Smatthias.ringwald                         // send disconnect
166132ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
166232ab9390Smatthias.ringwald 
166379bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
16646ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
166572ea5239Smatthias.ringwald 
166672ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
166772ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
166872ea5239Smatthias.ringwald                         return;
166972ea5239Smatthias.ringwald                     }
167072ea5239Smatthias.ringwald 
167192368cd3S[email protected]                     if (hci_classic_supported()){
167289db417bSmatthias.ringwald                         // disable page and inquiry scan
167364f0b431S[email protected]                         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
167432ab9390Smatthias.ringwald 
167592368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
16763a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
167789db417bSmatthias.ringwald 
167889db417bSmatthias.ringwald                         // continue in next sub state
16793a9fb326S[email protected]                         hci_stack->substate++;
168089db417bSmatthias.ringwald                         break;
168192368cd3S[email protected]                     }
168292368cd3S[email protected]                     // fall through for ble-only chips
168392368cd3S[email protected] 
168489db417bSmatthias.ringwald                 case 2:
16857b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
168628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
168728171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
168828171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
168928171530Smatthias.ringwald                         // SLEEP MODE reached
16903a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
169128171530Smatthias.ringwald                         hci_emit_state();
169228171530Smatthias.ringwald                         break;
169328171530Smatthias.ringwald                     }
169428171530Smatthias.ringwald #endif
169572ea5239Smatthias.ringwald                     // switch mode
16963a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1697c7e0c5f6Smatthias.ringwald                     hci_emit_state();
169828171530Smatthias.ringwald                     break;
169928171530Smatthias.ringwald 
170089db417bSmatthias.ringwald                 default:
170189db417bSmatthias.ringwald                     break;
170289db417bSmatthias.ringwald             }
1703c7e0c5f6Smatthias.ringwald             break;
1704c7e0c5f6Smatthias.ringwald 
17053429f56bSmatthias.ringwald         default:
17063429f56bSmatthias.ringwald             break;
17071f504dbdSmatthias.ringwald     }
17083429f56bSmatthias.ringwald }
170916833f0aSmatthias.ringwald 
171031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1711c8e4258aSmatthias.ringwald     bd_addr_t addr;
1712c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1713c8e4258aSmatthias.ringwald     // house-keeping
1714c8e4258aSmatthias.ringwald 
1715c8e4258aSmatthias.ringwald     // create_connection?
1716c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1717c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1718339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1719c8e4258aSmatthias.ringwald 
172096a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1721ad83dc6aS[email protected]         if (!conn){
172296a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
172317f1ba2aSmatthias.ringwald             if (!conn){
172417f1ba2aSmatthias.ringwald                 // notify client that alloc failed
172517f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
172617f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
172717f1ba2aSmatthias.ringwald             }
1728ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1729ad83dc6aS[email protected]         }
1730ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1731ad83dc6aS[email protected]         switch (conn->state){
1732ad83dc6aS[email protected]             // if connection active exists
1733ad83dc6aS[email protected]             case OPEN:
173462bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1735ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
173662bda3fbS[email protected]                 return 0;
1737ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1738ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1739ad83dc6aS[email protected]                 break;
1740ad83dc6aS[email protected]             default:
1741ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1742ad83dc6aS[email protected]                 return 0;
1743ad83dc6aS[email protected]         }
1744c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1745c8e4258aSmatthias.ringwald     }
1746c8e4258aSmatthias.ringwald 
17477fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
17487fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
17497fde4af9Smatthias.ringwald     }
17507fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
17517fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
17527fde4af9Smatthias.ringwald     }
17537fde4af9Smatthias.ringwald 
17548ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
17553a9fb326S[email protected]         if (hci_stack->remote_device_db){
17568ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
17573a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
17588ef73945Smatthias.ringwald         }
17598ef73945Smatthias.ringwald     }
1760c8e4258aSmatthias.ringwald 
17616724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
17626724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
17636724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
176496a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
17656724cd9eS[email protected]         if (conn){
17666724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
17676724cd9eS[email protected]         }
17686724cd9eS[email protected]     }
17696724cd9eS[email protected] 
17706724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
17716724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
17726724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
17736724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
17746724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
177596a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
17766724cd9eS[email protected]         if (conn){
17776724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
17786724cd9eS[email protected]         }
17796724cd9eS[email protected]     }
17806724cd9eS[email protected] 
178169a97523S[email protected] #ifdef HAVE_BLE
178269a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
17833a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
178469a97523S[email protected]     }
178569a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
17863a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
178769a97523S[email protected]     }
178869a97523S[email protected] #endif
178969a97523S[email protected] 
17903a9fb326S[email protected]     hci_stack->num_cmd_packets--;
17916b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
17926b4af23dS[email protected] 
17936b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
1794c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
17956b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
17966b4af23dS[email protected]     }
17976b4af23dS[email protected] 
17986b4af23dS[email protected]     return err;
179931452debSmatthias.ringwald }
18008adf0ddaSmatthias.ringwald 
18012bd8b7e7S[email protected] // disconnect because of security block
18022bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
18032bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
18042bd8b7e7S[email protected]     if (!connection) return;
18052bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
18062bd8b7e7S[email protected] }
18072bd8b7e7S[email protected] 
18082bd8b7e7S[email protected] 
1809dbe1a790S[email protected] // Configure Secure Simple Pairing
1810dbe1a790S[email protected] 
1811dbe1a790S[email protected] // enable will enable SSP during init
1812dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
18133a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1814dbe1a790S[email protected] }
1815dbe1a790S[email protected] 
18162bd8b7e7S[email protected] int hci_local_ssp_activated(){
18173a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
18182bd8b7e7S[email protected] }
18192bd8b7e7S[email protected] 
1820dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1821dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
18223a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1823dbe1a790S[email protected] }
1824dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
18253a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1826dbe1a790S[email protected] }
1827dbe1a790S[email protected] 
1828dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1829dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
18303a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1831dbe1a790S[email protected] }
1832dbe1a790S[email protected] 
18331cd208adSmatthias.ringwald /**
18341cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
18351cd208adSmatthias.ringwald  */
1836fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
18371cd208adSmatthias.ringwald     va_list argptr;
18381cd208adSmatthias.ringwald     va_start(argptr, cmd);
18393a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
18401cd208adSmatthias.ringwald     va_end(argptr);
18413a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
184293b8dc03Smatthias.ringwald }
1843c8e4258aSmatthias.ringwald 
1844ee091cf1Smatthias.ringwald // Create various non-HCI events.
1845ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1846ee091cf1Smatthias.ringwald 
1847c8e4258aSmatthias.ringwald void hci_emit_state(){
18483a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1849425d1371Smatthias.ringwald     uint8_t event[3];
185080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1851425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
18523a9fb326S[email protected]     event[2] = hci_stack->state;
1853425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18543a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1855c8e4258aSmatthias.ringwald }
1856c8e4258aSmatthias.ringwald 
185717f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1858425d1371Smatthias.ringwald     uint8_t event[13];
1859c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1860425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
186117f1ba2aSmatthias.ringwald     event[2] = status;
1862c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1863c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1864c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1865c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1866425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18673a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1868c8e4258aSmatthias.ringwald }
1869c8e4258aSmatthias.ringwald 
18703c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1871425d1371Smatthias.ringwald     uint8_t event[6];
18723c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1873e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
18743c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
18753c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
18763c4d4b90Smatthias.ringwald     event[5] = reason;
1877425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18783a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18793c4d4b90Smatthias.ringwald }
18803c4d4b90Smatthias.ringwald 
1881ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
188266fb9560S[email protected]     if (disable_l2cap_timeouts) return;
1883e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1884425d1371Smatthias.ringwald     uint8_t event[4];
188580d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1886425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1887ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1888425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18893a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1890ee091cf1Smatthias.ringwald }
189143bfb1bdSmatthias.ringwald 
189243bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1893e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1894425d1371Smatthias.ringwald     uint8_t event[3];
189580d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1896425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
189743bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1898425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18993a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
190043bfb1bdSmatthias.ringwald }
1901038bc64cSmatthias.ringwald 
1902038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1903e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1904425d1371Smatthias.ringwald     uint8_t event[2];
190580d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1906425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1907425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19083a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1909038bc64cSmatthias.ringwald }
19101b0e3922Smatthias.ringwald 
191109ba8edeSmatthias.ringwald #ifndef EMBEDDED
19121b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1913e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1914425d1371Smatthias.ringwald     uint8_t event[6];
19151b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1916425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1917425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1918425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1919425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1920425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19213a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
19221b0e3922Smatthias.ringwald }
192309ba8edeSmatthias.ringwald #endif
19241b0e3922Smatthias.ringwald 
19252ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1926e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1927425d1371Smatthias.ringwald     uint8_t event[3];
19282ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1929425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
19302ed6235cSmatthias.ringwald     event[2] = enabled;
1931425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19323a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
19332ed6235cSmatthias.ringwald }
1934627c2f45Smatthias.ringwald 
1935627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1936e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1937627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1938e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1939f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
19402f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1941f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1942e0abb8e7S[email protected] 
1943e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1944e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1945e0abb8e7S[email protected] 
1946e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
19473a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1948627c2f45Smatthias.ringwald }
1949381fbed8Smatthias.ringwald 
1950381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1951e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1952425d1371Smatthias.ringwald     uint8_t event[3];
1953381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1954425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1955381fbed8Smatthias.ringwald     event[2] = enabled;
1956425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19573a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1958381fbed8Smatthias.ringwald }
1959458bf4e8S[email protected] 
1960a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1961df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
1962a00031e2S[email protected]     uint8_t event[5];
1963e00caf9cS[email protected]     int pos = 0;
1964a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
1965e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
1966a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
1967e00caf9cS[email protected]     pos += 2;
1968e00caf9cS[email protected]     event[pos++] = level;
1969e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19703a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1971e00caf9cS[email protected] }
1972e00caf9cS[email protected] 
1973ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
1974ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
1975ad83dc6aS[email protected]     uint8_t event[9];
1976ad83dc6aS[email protected]     int pos = 0;
1977ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
1978ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
1979ad83dc6aS[email protected]     event[pos++] = status;
1980ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
1981ad83dc6aS[email protected]     pos += 6;
1982ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19833a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1984ad83dc6aS[email protected] }
1985ad83dc6aS[email protected] 
19862bd8b7e7S[email protected] // query if remote side supports SSP
19872bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
19882bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
19892bd8b7e7S[email protected]     if (!connection) return 0;
19902bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
19912bd8b7e7S[email protected] }
19922bd8b7e7S[email protected] 
1993df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
1994df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
1995df3354fcS[email protected] }
1996df3354fcS[email protected] 
1997458bf4e8S[email protected] // GAP API
1998458bf4e8S[email protected] /**
1999458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
2000458bf4e8S[email protected]  * @praram enabled
2001458bf4e8S[email protected]  */
20024c57c146S[email protected] void gap_set_bondable_mode(int enable){
20033a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
2004458bf4e8S[email protected] }
2005cb230b9dS[email protected] 
2006cb230b9dS[email protected] /**
200734d2123cS[email protected]  * @brief map link keys to security levels
2008cb230b9dS[email protected]  */
200934d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
201034d2123cS[email protected]     switch (link_key_type){
20113c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
20123c68dfa9S[email protected]             return LEVEL_4;
20133c68dfa9S[email protected]         case COMBINATION_KEY:
20143c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
20153c68dfa9S[email protected]             return LEVEL_3;
20163c68dfa9S[email protected]         default:
20173c68dfa9S[email protected]             return LEVEL_2;
20183c68dfa9S[email protected]     }
2019cb230b9dS[email protected] }
2020cb230b9dS[email protected] 
2021a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
202234d2123cS[email protected]     if (!connection) return LEVEL_0;
202334d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
202434d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
202534d2123cS[email protected] }
202634d2123cS[email protected] 
202734d2123cS[email protected] 
2028106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
202982d8f825S[email protected]     printf("gap_mitm_protection_required_for_security_level %u\n", level);
2030106d6d11S[email protected]     return level > LEVEL_2;
2031106d6d11S[email protected] }
2032106d6d11S[email protected] 
203334d2123cS[email protected] /**
203434d2123cS[email protected]  * @brief get current security level
203534d2123cS[email protected]  */
203634d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
203734d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
203834d2123cS[email protected]     if (!connection) return LEVEL_0;
203934d2123cS[email protected]     return gap_security_level_for_connection(connection);
204034d2123cS[email protected] }
204134d2123cS[email protected] 
2042cb230b9dS[email protected] /**
2043cb230b9dS[email protected]  * @brief request connection to device to
2044cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
2045cb230b9dS[email protected]  */
204634d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
204734d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
204834d2123cS[email protected]     if (!connection){
2049a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
205034d2123cS[email protected]         return;
205134d2123cS[email protected]     }
205234d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
205334d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
205434d2123cS[email protected]     if (current_level >= requested_level){
2055a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
205634d2123cS[email protected]         return;
205734d2123cS[email protected]     }
2058a00031e2S[email protected] 
205934d2123cS[email protected]     connection->requested_security_level = requested_level;
2060a00031e2S[email protected] 
2061fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
20623a9fb326S[email protected]     if (hci_stack->remote_device_db){
2063a00031e2S[email protected]         link_key_type_t link_key_type;
2064a00031e2S[email protected]         link_key_t      link_key;
20653a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2066a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2067a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2068a00031e2S[email protected]                 return;
2069a00031e2S[email protected]             }
2070a00031e2S[email protected]         }
2071a00031e2S[email protected]     }
2072a00031e2S[email protected] 
20731eb2563eS[email protected]     // try to authenticate connection
20741eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2075e80b2cf9S[email protected]     hci_run();
2076e00caf9cS[email protected] }
2077ad83dc6aS[email protected] 
2078ad83dc6aS[email protected] /**
2079ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2080ad83dc6aS[email protected]  * @param device
2081ad83dc6aS[email protected]  * @param request MITM protection
2082ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2083ad83dc6aS[email protected]  */
2084ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2085ad83dc6aS[email protected] 
2086ad83dc6aS[email protected]     // create connection state machine
208796a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2088ad83dc6aS[email protected] 
2089ad83dc6aS[email protected]     if (!connection){
2090ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2091ad83dc6aS[email protected]     }
2092ad83dc6aS[email protected] 
2093ad83dc6aS[email protected]     // delete linkn key
2094ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2095ad83dc6aS[email protected] 
2096ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2097ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2098ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
209982d8f825S[email protected]     printf("gap_dedicated_bonding, mitm %u -> level %u\n", mitm_protection_required, connection->requested_security_level);
2100ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2101ad83dc6aS[email protected] 
2102ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2103ad83dc6aS[email protected] 
2104ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2105ad83dc6aS[email protected]     // handle: authentication failure
2106ad83dc6aS[email protected]     // handle: disconnect on done
2107ad83dc6aS[email protected] 
2108ad83dc6aS[email protected]     hci_run();
2109ad83dc6aS[email protected] 
2110ad83dc6aS[email protected]     return 0;
2111ad83dc6aS[email protected] }
21128e618f72S[email protected] 
21138e618f72S[email protected] void gap_set_local_name(const char * local_name){
21148e618f72S[email protected]     hci_stack->local_name = local_name;
21158e618f72S[email protected] }
21168e618f72S[email protected] 
21177bdc6798S[email protected] le_command_status_t le_central_start_scan(){
21187bdc6798S[email protected]     printf("hci_init: scanning state START_SCAN\n");
21197bdc6798S[email protected]     hci_stack->le_scanning_state = LE_START_SCAN;
21207bdc6798S[email protected]     hci_run();
21217bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
21227bdc6798S[email protected] }
21238e618f72S[email protected] 
21247bdc6798S[email protected] le_command_status_t le_central_stop_scan(){
21257bdc6798S[email protected]     hci_stack->le_scanning_state = LE_STOP_SCAN;
21267bdc6798S[email protected]     hci_run();
21277bdc6798S[email protected]     return BLE_PERIPHERAL_OK;
21287bdc6798S[email protected] }
2129