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*7bdc6798S[email protected] static void (*le_central_callback)(le_event_t * event); 84*7bdc6798S[email protected] 85*7bdc6798S[email protected] static void dummy_notify(le_event_t* event){} 86*7bdc6798S[email protected] void le_central_register_handler(void (*le_callback)(le_event_t* event)){ 87*7bdc6798S[email protected] if (le_callback == NULL){ 88*7bdc6798S[email protected] le_callback = dummy_notify; 89*7bdc6798S[email protected] } 90*7bdc6798S[email protected] le_central_callback = le_callback; 91*7bdc6798S[email protected] } 92*7bdc6798S[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] 52674ec757aSmatthias.ringwald // avoid huge local variables 527223aafc1Smatthias.ringwald #ifndef EMBEDDED 52874ec757aSmatthias.ringwald static device_name_t device_name; 529223aafc1Smatthias.ringwald #endif 53016833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 531e76a89eeS[email protected] 532e76a89eeS[email protected] uint16_t event_length = packet[1]; 533e76a89eeS[email protected] 534e76a89eeS[email protected] // assert packet is complete 535e76a89eeS[email protected] if (size != event_length + 2){ 536e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 537e76a89eeS[email protected] return; 538e76a89eeS[email protected] } 539e76a89eeS[email protected] 5401281a47eSmatthias.ringwald bd_addr_t addr; 54196a45072S[email protected] bd_addr_type_t addr_type; 5422d00edd4Smatthias.ringwald uint8_t link_type; 543fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 5441f7b95a1Smatthias.ringwald hci_connection_t * conn; 54556cf178bSmatthias.ringwald int i; 54622909952Smatthias.ringwald 5475909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 5485909f7f2Smatthias.ringwald 5496772a24cSmatthias.ringwald switch (packet[0]) { 55022909952Smatthias.ringwald 5516772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 5527ec5eeaaSmatthias.ringwald // get num cmd packets 5533a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]); 5543a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 5557ec5eeaaSmatthias.ringwald 556e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 557e2edc0c3Smatthias.ringwald // from offset 5 558e2edc0c3Smatthias.ringwald // status 5591d279b20Smatthias.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" 5603a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 56156cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 5623a9fb326S[email protected] hci_stack->total_num_acl_packets = packet[9]; 56356cf178bSmatthias.ringwald // ignore: total num SCO packets 5643a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 565a7a04bd9Smatthias.ringwald // determine usable ACL payload size 5663a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 5673a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 5688f8108aaSmatthias.ringwald } 56965389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 5703a9fb326S[email protected] hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets); 571e2edc0c3Smatthias.ringwald } 57256cf178bSmatthias.ringwald } 57365a46ef3S[email protected] #ifdef HAVE_BLE 57465a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 5753a9fb326S[email protected] hci_stack->le_data_packet_length = READ_BT_16(packet, 6); 5763a9fb326S[email protected] hci_stack->total_num_le_packets = packet[8]; 5773a9fb326S[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); 57865a46ef3S[email protected] } 57965a46ef3S[email protected] #endif 580188981d2Smatthias.ringwald // Dump local address 581188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 5823a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 583188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 5843a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 585188981d2Smatthias.ringwald } 586381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 5873a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 588381fbed8Smatthias.ringwald } 58965389bfcS[email protected] // Note: HCI init checks 590559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 5913a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 592559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 5933a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 5943a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 5953a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 5963a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 59765389bfcS[email protected] 59865389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 5993a9fb326S[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]); 6003a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 601f5d8d141S[email protected] 602f5d8d141S[email protected] // Classic/LE 603f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 604559e517eS[email protected] } 60556cf178bSmatthias.ringwald break; 60656cf178bSmatthias.ringwald 6077ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 6087ec5eeaaSmatthias.ringwald // get num cmd packets 6093a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]); 6103a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 6117ec5eeaaSmatthias.ringwald break; 6127ec5eeaaSmatthias.ringwald 61356cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 61456cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 61556cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 61656cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 6175061f3afS[email protected] conn = hci_connection_for_handle(handle); 61856cf178bSmatthias.ringwald if (!conn){ 6197d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 62056cf178bSmatthias.ringwald continue; 62156cf178bSmatthias.ringwald } 62256cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 6237b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 62456cf178bSmatthias.ringwald } 6256772a24cSmatthias.ringwald break; 6266772a24cSmatthias.ringwald 6271f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 62837eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 62937eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 6302d00edd4Smatthias.ringwald link_type = packet[11]; 631339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 63237eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 63396a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 6341f7b95a1Smatthias.ringwald if (!conn) { 63596a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 6361f7b95a1Smatthias.ringwald } 637ce4c8fabSmatthias.ringwald if (!conn) { 638ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 6393a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 6403a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 641ce4c8fabSmatthias.ringwald break; 642ce4c8fabSmatthias.ringwald } 64332ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 64432ab9390Smatthias.ringwald hci_run(); 64537eaa4cfSmatthias.ringwald } else { 646ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 6473a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 6483a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 64937eaa4cfSmatthias.ringwald } 6501f7b95a1Smatthias.ringwald break; 6511f7b95a1Smatthias.ringwald 6526772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 653fe1ed1b8Smatthias.ringwald // Connection management 654fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 655339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 65696a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 65796a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 658fe1ed1b8Smatthias.ringwald if (conn) { 659b448a0e7Smatthias.ringwald if (!packet[2]){ 660c8e4258aSmatthias.ringwald conn->state = OPEN; 661fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 662ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 663ee091cf1Smatthias.ringwald 664c785ef68Smatthias.ringwald // restart timer 665c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 666ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 667c785ef68Smatthias.ringwald 668339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 66943bfb1bdSmatthias.ringwald 67043bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 671b448a0e7Smatthias.ringwald } else { 672ad83dc6aS[email protected] // notify client if dedicated bonding 673ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 674ad83dc6aS[email protected] hci_emit_dedicated_bonding_result(conn, packet[2]); 675ad83dc6aS[email protected] } 676ad83dc6aS[email protected] 677b448a0e7Smatthias.ringwald // connection failed, remove entry 6783a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 679a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 680c12e46e7Smatthias.ringwald 681c12e46e7Smatthias.ringwald // if authentication error, also delete link key 682c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 683c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 684c12e46e7Smatthias.ringwald } 685fe1ed1b8Smatthias.ringwald } 686fe1ed1b8Smatthias.ringwald } 6876772a24cSmatthias.ringwald break; 688fe1ed1b8Smatthias.ringwald 689afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 690afd4e962S[email protected] handle = READ_BT_16(packet, 3); 691afd4e962S[email protected] conn = hci_connection_for_handle(handle); 692afd4e962S[email protected] if (!conn) break; 693afd4e962S[email protected] if (!packet[2]){ 694afd4e962S[email protected] uint8_t * features = &packet[5]; 695afd4e962S[email protected] if (features[6] & (1 << 3)){ 696afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 697afd4e962S[email protected] } 698afd4e962S[email protected] } 699afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 700ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 701ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 702ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 703ad83dc6aS[email protected] } 704afd4e962S[email protected] break; 705afd4e962S[email protected] 7067fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 7077b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 7087fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 70974d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 7103a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 71132ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 71264472d52Smatthias.ringwald hci_run(); 713d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 71429d53098Smatthias.ringwald return; 7157fde4af9Smatthias.ringwald 7169ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 71729d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 71896a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 7199ab95c90S[email protected] if (!conn) break; 7209ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 721*7bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 7229ab95c90S[email protected] // Change Connection Encryption keeps link key type 7239ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 7249ab95c90S[email protected] conn->link_key_type = link_key_type; 7259ab95c90S[email protected] } 7263a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 7273a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 72829d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 7297fde4af9Smatthias.ringwald break; 7309ab95c90S[email protected] } 7317fde4af9Smatthias.ringwald 7327fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 7336724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 7344c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 7353a9fb326S[email protected] if (!hci_stack->bondable){ 736899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 737f8fb5f6eS[email protected] hci_run(); 738f8fb5f6eS[email protected] return; 7394c57c146S[email protected] } 740d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 7413a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 742d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 7433a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 7447fde4af9Smatthias.ringwald break; 7457fde4af9Smatthias.ringwald 7461d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 7471d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 748dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 749dbe1a790S[email protected] break; 750dbe1a790S[email protected] 751dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 7526724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 7533a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 754dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 755dbe1a790S[email protected] break; 756dbe1a790S[email protected] 757dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 7586724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 7593a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 760dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 7611d6b20aeS[email protected] break; 7621d6b20aeS[email protected] 763f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 764f0944df2S[email protected] handle = READ_BT_16(packet, 3); 765f0944df2S[email protected] conn = hci_connection_for_handle(handle); 766f0944df2S[email protected] if (!conn) break; 767ad83dc6aS[email protected] if (packet[2] == 0) { 768f0944df2S[email protected] if (packet[5]){ 769f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 770f0944df2S[email protected] } else { 771536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 772f0944df2S[email protected] } 773ad83dc6aS[email protected] } 774a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 775f0944df2S[email protected] break; 776f0944df2S[email protected] 7771eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 7781eb2563eS[email protected] handle = READ_BT_16(packet, 3); 7791eb2563eS[email protected] conn = hci_connection_for_handle(handle); 7801eb2563eS[email protected] if (!conn) break; 781ad83dc6aS[email protected] 782ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 783ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 784ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 785ad83dc6aS[email protected] hci_emit_dedicated_bonding_result( conn, packet[2]); 786ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 787ad83dc6aS[email protected] break; 788ad83dc6aS[email protected] } 789ad83dc6aS[email protected] 790b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 7911eb2563eS[email protected] // link key sufficient for requested security 7921eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 793ad83dc6aS[email protected] break; 794ad83dc6aS[email protected] } 7951eb2563eS[email protected] // not enough 7961eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 7971eb2563eS[email protected] break; 79834d2123cS[email protected] 799223aafc1Smatthias.ringwald #ifndef EMBEDDED 80074ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 8013a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 80274ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 80374ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 8045a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 8055a06394aSmatthias.ringwald for (i=0; i<248;i++){ 8065a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 8075a06394aSmatthias.ringwald packet[9+i] = 0; 8085a06394aSmatthias.ringwald break; 809cdc9101dSmatthias.ringwald } 8105a06394aSmatthias.ringwald } 811d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 81274ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 8133a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 81474ec757aSmatthias.ringwald break; 81574ec757aSmatthias.ringwald 81674ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 81774ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 8183a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 81974ec757aSmatthias.ringwald // first send inq result packet 8203a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 82174ec757aSmatthias.ringwald // then send cached remote names 82274ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 82374ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 8243a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 82574ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 82674ec757aSmatthias.ringwald } 82774ec757aSmatthias.ringwald } 82874ec757aSmatthias.ringwald return; 829223aafc1Smatthias.ringwald #endif 83074ec757aSmatthias.ringwald 8316772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 832fe1ed1b8Smatthias.ringwald if (!packet[2]){ 833fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 8345061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 835fe1ed1b8Smatthias.ringwald if (conn) { 836c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 837fe1ed1b8Smatthias.ringwald } 838fe1ed1b8Smatthias.ringwald } 8396772a24cSmatthias.ringwald break; 8406772a24cSmatthias.ringwald 841c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 8423a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 8433a9fb326S[email protected] (*hci_stack->control->hw_error)(); 844c68bdf90Smatthias.ringwald } 845c68bdf90Smatthias.ringwald break; 846c68bdf90Smatthias.ringwald 8476b4af23dS[email protected] case DAEMON_EVENT_HCI_PACKET_SENT: 8486b4af23dS[email protected] // free packet buffer for asynchronous transport 8496b4af23dS[email protected] if (hci_transport_synchronous()) break; 8506b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 8516b4af23dS[email protected] break; 8526b4af23dS[email protected] 8535909f7f2Smatthias.ringwald #ifdef HAVE_BLE 8545909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 8555909f7f2Smatthias.ringwald switch (packet[2]){ 856*7bdc6798S[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT:{ 857*7bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 858*7bdc6798S[email protected] int num_reports = packet[3]; 859*7bdc6798S[email protected] int i; 860*7bdc6798S[email protected] int total_data_length = 0; 861*7bdc6798S[email protected] int data_offset = 0; 862*7bdc6798S[email protected] 863*7bdc6798S[email protected] for (i=0; i<num_reports;i++){ 864*7bdc6798S[email protected] total_data_length += packet[4+num_reports*8+i]; 865*7bdc6798S[email protected] } 866*7bdc6798S[email protected] 867*7bdc6798S[email protected] ad_event_t advertisement_event; 868*7bdc6798S[email protected] advertisement_event.type = GATT_ADVERTISEMENT; 869*7bdc6798S[email protected] for (i=0; i<num_reports;i++){ 870*7bdc6798S[email protected] advertisement_event.event_type = packet[4+i]; 871*7bdc6798S[email protected] 872*7bdc6798S[email protected] advertisement_event.address_type = packet[4+num_reports+i]; 873*7bdc6798S[email protected] bt_flip_addr(advertisement_event.address, &packet[4+num_reports*2+i*6]); 874*7bdc6798S[email protected] advertisement_event.length = packet[4+num_reports*8+i]; 875*7bdc6798S[email protected] advertisement_event.data = &packet[4+num_reports*9+data_offset]; 876*7bdc6798S[email protected] data_offset += advertisement_event.length; 877*7bdc6798S[email protected] 878*7bdc6798S[email protected] advertisement_event.rssi = packet[4+num_reports*9+total_data_length + i]; 879*7bdc6798S[email protected] 880*7bdc6798S[email protected] (*le_central_callback)((le_event_t*)&advertisement_event); 881*7bdc6798S[email protected] } 882*7bdc6798S[email protected] break; 883*7bdc6798S[email protected] } 8845909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 8855909f7f2Smatthias.ringwald // Connection management 8865909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 887*7bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 88896a45072S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr)); 8895909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 89096a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 8915909f7f2Smatthias.ringwald if (packet[3]){ 8925909f7f2Smatthias.ringwald if (conn){ 8935909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 8943a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 8955909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8965909f7f2Smatthias.ringwald 8975909f7f2Smatthias.ringwald } 8985909f7f2Smatthias.ringwald // if authentication error, also delete link key 8995909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 9005909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 9015909f7f2Smatthias.ringwald } 9025909f7f2Smatthias.ringwald break; 9035909f7f2Smatthias.ringwald } 9045909f7f2Smatthias.ringwald if (!conn){ 90596a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 9065909f7f2Smatthias.ringwald } 9075909f7f2Smatthias.ringwald if (!conn){ 9085909f7f2Smatthias.ringwald // no memory 9095909f7f2Smatthias.ringwald break; 9105909f7f2Smatthias.ringwald } 9115909f7f2Smatthias.ringwald 9125909f7f2Smatthias.ringwald conn->state = OPEN; 9135909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 9145909f7f2Smatthias.ringwald 9155909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 9165909f7f2Smatthias.ringwald 9175909f7f2Smatthias.ringwald // restart timer 9185909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 9195909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 9205909f7f2Smatthias.ringwald 9215909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 9225909f7f2Smatthias.ringwald 9235909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 9245909f7f2Smatthias.ringwald break; 9255909f7f2Smatthias.ringwald 92665a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 92765a46ef3S[email protected] 9285909f7f2Smatthias.ringwald default: 9295909f7f2Smatthias.ringwald break; 9305909f7f2Smatthias.ringwald } 9315909f7f2Smatthias.ringwald break; 9325909f7f2Smatthias.ringwald #endif 9335909f7f2Smatthias.ringwald 9346772a24cSmatthias.ringwald default: 9356772a24cSmatthias.ringwald break; 936fe1ed1b8Smatthias.ringwald } 937fe1ed1b8Smatthias.ringwald 9383429f56bSmatthias.ringwald // handle BT initialization 9393a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 9403a9fb326S[email protected] if (hci_stack->substate % 2){ 9413429f56bSmatthias.ringwald // odd: waiting for event 942f155fca3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 943c9af4d3fS[email protected] // wait for explicit COMMAND COMPLETE on RESET 9443a9fb326S[email protected] if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) { 9453a9fb326S[email protected] hci_stack->substate++; 9463429f56bSmatthias.ringwald } 9473429f56bSmatthias.ringwald } 94822909952Smatthias.ringwald } 949c9af4d3fS[email protected] } 95022909952Smatthias.ringwald 95189db417bSmatthias.ringwald // help with BT sleep 9523a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 9533a9fb326S[email protected] && hci_stack->substate == 1 95489db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 9553a9fb326S[email protected] hci_stack->substate++; 95689db417bSmatthias.ringwald } 95789db417bSmatthias.ringwald 9583a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 95994ab26f8Smatthias.ringwald 96094ab26f8Smatthias.ringwald // execute main loop 96194ab26f8Smatthias.ringwald hci_run(); 96216833f0aSmatthias.ringwald } 96316833f0aSmatthias.ringwald 9640a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 96510e830c9Smatthias.ringwald switch (packet_type) { 96610e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 96710e830c9Smatthias.ringwald event_handler(packet, size); 96810e830c9Smatthias.ringwald break; 96910e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 97010e830c9Smatthias.ringwald acl_handler(packet, size); 97110e830c9Smatthias.ringwald break; 97210e830c9Smatthias.ringwald default: 97310e830c9Smatthias.ringwald break; 97410e830c9Smatthias.ringwald } 97510e830c9Smatthias.ringwald } 97610e830c9Smatthias.ringwald 977fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 9782718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 9793a9fb326S[email protected] hci_stack->packet_handler = handler; 98016833f0aSmatthias.ringwald } 98116833f0aSmatthias.ringwald 9824f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 983475c8125Smatthias.ringwald 9843a9fb326S[email protected] #ifdef HAVE_MALLOC 9853a9fb326S[email protected] if (!hci_stack) { 9863a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 9873a9fb326S[email protected] } 9883a9fb326S[email protected] #else 9893a9fb326S[email protected] hci_stack = &hci_stack_static; 9903a9fb326S[email protected] #endif 99166fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 9923a9fb326S[email protected] 993475c8125Smatthias.ringwald // reference to use transport layer implementation 9943a9fb326S[email protected] hci_stack->hci_transport = transport; 995475c8125Smatthias.ringwald 99611e23e5fSmatthias.ringwald // references to used control implementation 9973a9fb326S[email protected] hci_stack->control = control; 99811e23e5fSmatthias.ringwald 99911e23e5fSmatthias.ringwald // reference to used config 10003a9fb326S[email protected] hci_stack->config = config; 100111e23e5fSmatthias.ringwald 1002fe1ed1b8Smatthias.ringwald // no connections yet 10033a9fb326S[email protected] hci_stack->connections = NULL; 10043a9fb326S[email protected] hci_stack->discoverable = 0; 10053a9fb326S[email protected] hci_stack->connectable = 0; 10063a9fb326S[email protected] hci_stack->bondable = 1; 1007758b46ceSmatthias.ringwald 1008b031bebbSmatthias.ringwald // no pending cmds 10093a9fb326S[email protected] hci_stack->decline_reason = 0; 10103a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1011b031bebbSmatthias.ringwald 101216833f0aSmatthias.ringwald // higher level handler 10133a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 101416833f0aSmatthias.ringwald 1015404843c1Smatthias.ringwald // store and open remote device db 10163a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 10173a9fb326S[email protected] if (hci_stack->remote_device_db) { 10183a9fb326S[email protected] hci_stack->remote_device_db->open(); 1019404843c1Smatthias.ringwald } 102029d53098Smatthias.ringwald 10218fcba05dSmatthias.ringwald // max acl payload size defined in config.h 10223a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 10238fcba05dSmatthias.ringwald 102416833f0aSmatthias.ringwald // register packet handlers with transport 102510e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 1026f5454fc6Smatthias.ringwald 10273a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 1028e2386ba1S[email protected] 1029e2386ba1S[email protected] // class of device 10303a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 1031a45d6b9fS[email protected] 103263048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 10333a9fb326S[email protected] hci_stack->ssp_enable = 1; 10343a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 10353a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 10363a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 103769a97523S[email protected] 103869a97523S[email protected] // LE 10393a9fb326S[email protected] hci_stack->adv_addr_type = 0; 10403a9fb326S[email protected] memset(hci_stack->adv_address, 0, 6); 1041*7bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 1042475c8125Smatthias.ringwald } 1043475c8125Smatthias.ringwald 1044404843c1Smatthias.ringwald void hci_close(){ 1045404843c1Smatthias.ringwald // close remote device db 10463a9fb326S[email protected] if (hci_stack->remote_device_db) { 10473a9fb326S[email protected] hci_stack->remote_device_db->close(); 1048404843c1Smatthias.ringwald } 10493a9fb326S[email protected] while (hci_stack->connections) { 10503a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 1051f5454fc6Smatthias.ringwald } 1052f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 10533a9fb326S[email protected] 10543a9fb326S[email protected] #ifdef HAVE_MALLOC 10553a9fb326S[email protected] free(hci_stack); 10563a9fb326S[email protected] #endif 10573a9fb326S[email protected] hci_stack = NULL; 1058404843c1Smatthias.ringwald } 1059404843c1Smatthias.ringwald 10609e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){ 10619e61646fS[email protected] hci_stack->class_of_device = class_of_device; 10629e61646fS[email protected] } 10639e61646fS[email protected] 106466fb9560S[email protected] void hci_disable_l2cap_timeout_check(){ 106566fb9560S[email protected] disable_l2cap_timeouts = 1; 106666fb9560S[email protected] } 10678d213e1aSmatthias.ringwald // State-Module-Driver overview 10688d213e1aSmatthias.ringwald // state module low-level 10698d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 10708d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 10718d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 10728d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 1073d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 1074d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 1075c7e0c5f6Smatthias.ringwald 107640d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 10777301ad89Smatthias.ringwald 1078038bc64cSmatthias.ringwald // power on 1079f9a30166Smatthias.ringwald int err = 0; 10803a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 10813a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 1082f9a30166Smatthias.ringwald } 1083038bc64cSmatthias.ringwald if (err){ 10847d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 1085038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1086038bc64cSmatthias.ringwald return err; 1087038bc64cSmatthias.ringwald } 1088038bc64cSmatthias.ringwald 1089038bc64cSmatthias.ringwald // open low-level device 10903a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 1091038bc64cSmatthias.ringwald if (err){ 10927d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 10933a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 10943a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1095f9a30166Smatthias.ringwald } 1096038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1097038bc64cSmatthias.ringwald return err; 1098038bc64cSmatthias.ringwald } 10998d213e1aSmatthias.ringwald return 0; 11008d213e1aSmatthias.ringwald } 1101038bc64cSmatthias.ringwald 110240d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 11038d213e1aSmatthias.ringwald 11047b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 11059418f9c9Smatthias.ringwald 11068d213e1aSmatthias.ringwald // close low-level device 11073a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 11088d213e1aSmatthias.ringwald 11097b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 11109418f9c9Smatthias.ringwald 11118d213e1aSmatthias.ringwald // power off 11123a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 11133a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 11148d213e1aSmatthias.ringwald } 11159418f9c9Smatthias.ringwald 11167b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 11179418f9c9Smatthias.ringwald 11183a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 111972ea5239Smatthias.ringwald } 112072ea5239Smatthias.ringwald 112140d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 112272ea5239Smatthias.ringwald 11237b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 11243144bce4Smatthias.ringwald 1125b429b9b7Smatthias.ringwald #if 0 1126b429b9b7Smatthias.ringwald // don't close serial port during sleep 1127b429b9b7Smatthias.ringwald 112872ea5239Smatthias.ringwald // close low-level device 11293a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1130b429b9b7Smatthias.ringwald #endif 113172ea5239Smatthias.ringwald 113272ea5239Smatthias.ringwald // sleep mode 11333a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 11343a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 113572ea5239Smatthias.ringwald } 1136b429b9b7Smatthias.ringwald 11373a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 11388d213e1aSmatthias.ringwald } 11398d213e1aSmatthias.ringwald 114040d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1141b429b9b7Smatthias.ringwald 11427b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 1143b429b9b7Smatthias.ringwald 1144b429b9b7Smatthias.ringwald // wake on 11453a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 11463a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1147b429b9b7Smatthias.ringwald } 1148b429b9b7Smatthias.ringwald 1149b429b9b7Smatthias.ringwald #if 0 1150b429b9b7Smatthias.ringwald // open low-level device 11513a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1152b429b9b7Smatthias.ringwald if (err){ 11537d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 11543a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 11553a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1156b429b9b7Smatthias.ringwald } 1157b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1158b429b9b7Smatthias.ringwald return err; 1159b429b9b7Smatthias.ringwald } 1160b429b9b7Smatthias.ringwald #endif 1161b429b9b7Smatthias.ringwald 1162b429b9b7Smatthias.ringwald return 0; 1163b429b9b7Smatthias.ringwald } 1164b429b9b7Smatthias.ringwald 1165b429b9b7Smatthias.ringwald 11668d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1167d661ed19Smatthias.ringwald 11683a9fb326S[email protected] log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state); 1169d661ed19Smatthias.ringwald 11708d213e1aSmatthias.ringwald int err = 0; 11713a9fb326S[email protected] switch (hci_stack->state){ 11728d213e1aSmatthias.ringwald 11738d213e1aSmatthias.ringwald case HCI_STATE_OFF: 11748d213e1aSmatthias.ringwald switch (power_mode){ 11758d213e1aSmatthias.ringwald case HCI_POWER_ON: 11768d213e1aSmatthias.ringwald err = hci_power_control_on(); 11778d213e1aSmatthias.ringwald if (err) return err; 11787301ad89Smatthias.ringwald // set up state machine 11793a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 11803a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 11813a9fb326S[email protected] hci_stack->substate = 0; 11828d213e1aSmatthias.ringwald break; 11838d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11848d213e1aSmatthias.ringwald // do nothing 11858d213e1aSmatthias.ringwald break; 11868d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1187b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 11888d213e1aSmatthias.ringwald break; 11898d213e1aSmatthias.ringwald } 11908d213e1aSmatthias.ringwald break; 11917301ad89Smatthias.ringwald 11928d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 11938d213e1aSmatthias.ringwald switch (power_mode){ 11948d213e1aSmatthias.ringwald case HCI_POWER_ON: 11958d213e1aSmatthias.ringwald // do nothing 11968d213e1aSmatthias.ringwald break; 11978d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11988d213e1aSmatthias.ringwald // no connections yet, just turn it off 11998d213e1aSmatthias.ringwald hci_power_control_off(); 12008d213e1aSmatthias.ringwald break; 12018d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1202b546ac54Smatthias.ringwald // no connections yet, just turn it off 120372ea5239Smatthias.ringwald hci_power_control_sleep(); 12048d213e1aSmatthias.ringwald break; 12058d213e1aSmatthias.ringwald } 12068d213e1aSmatthias.ringwald break; 12077301ad89Smatthias.ringwald 12088d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 12098d213e1aSmatthias.ringwald switch (power_mode){ 12108d213e1aSmatthias.ringwald case HCI_POWER_ON: 12118d213e1aSmatthias.ringwald // do nothing 12128d213e1aSmatthias.ringwald break; 12138d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1214c7e0c5f6Smatthias.ringwald // see hci_run 12153a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 12168d213e1aSmatthias.ringwald break; 12178d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1218b546ac54Smatthias.ringwald // see hci_run 12193a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 12203a9fb326S[email protected] hci_stack->substate = 0; 12218d213e1aSmatthias.ringwald break; 12228d213e1aSmatthias.ringwald } 12238d213e1aSmatthias.ringwald break; 12247301ad89Smatthias.ringwald 12258d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 12268d213e1aSmatthias.ringwald switch (power_mode){ 12278d213e1aSmatthias.ringwald case HCI_POWER_ON: 12288d213e1aSmatthias.ringwald // set up state machine 12293a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12303a9fb326S[email protected] hci_stack->substate = 0; 12318d213e1aSmatthias.ringwald break; 12328d213e1aSmatthias.ringwald case HCI_POWER_OFF: 12338d213e1aSmatthias.ringwald // do nothing 12348d213e1aSmatthias.ringwald break; 12358d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1236b546ac54Smatthias.ringwald // see hci_run 12373a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 12383a9fb326S[email protected] hci_stack->substate = 0; 12398d213e1aSmatthias.ringwald break; 12408d213e1aSmatthias.ringwald } 12418d213e1aSmatthias.ringwald break; 12428d213e1aSmatthias.ringwald 12438d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 12448d213e1aSmatthias.ringwald switch (power_mode){ 12458d213e1aSmatthias.ringwald case HCI_POWER_ON: 124628171530Smatthias.ringwald 124728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 124828171530Smatthias.ringwald // nothing to do, if H4 supports power management 124928171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 12503a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12513a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 125228171530Smatthias.ringwald break; 125328171530Smatthias.ringwald } 125428171530Smatthias.ringwald #endif 1255b546ac54Smatthias.ringwald // set up state machine 12563a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 12573a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12583a9fb326S[email protected] hci_stack->substate = 0; 12598d213e1aSmatthias.ringwald break; 12608d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1261b546ac54Smatthias.ringwald // see hci_run 12623a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 12638d213e1aSmatthias.ringwald break; 12648d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1265b546ac54Smatthias.ringwald // do nothing 12668d213e1aSmatthias.ringwald break; 12678d213e1aSmatthias.ringwald } 12688d213e1aSmatthias.ringwald break; 12698d213e1aSmatthias.ringwald 12708d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 12718d213e1aSmatthias.ringwald switch (power_mode){ 12728d213e1aSmatthias.ringwald case HCI_POWER_ON: 127328171530Smatthias.ringwald 127428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 127528171530Smatthias.ringwald // nothing to do, if H4 supports power management 127628171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 12773a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12783a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1279758b46ceSmatthias.ringwald hci_update_scan_enable(); 128028171530Smatthias.ringwald break; 128128171530Smatthias.ringwald } 128228171530Smatthias.ringwald #endif 12833144bce4Smatthias.ringwald err = hci_power_control_wake(); 12843144bce4Smatthias.ringwald if (err) return err; 1285b546ac54Smatthias.ringwald // set up state machine 12863a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 12873a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12883a9fb326S[email protected] hci_stack->substate = 0; 12898d213e1aSmatthias.ringwald break; 12908d213e1aSmatthias.ringwald case HCI_POWER_OFF: 12913a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 12928d213e1aSmatthias.ringwald break; 12938d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1294b546ac54Smatthias.ringwald // do nothing 12958d213e1aSmatthias.ringwald break; 12968d213e1aSmatthias.ringwald } 12978d213e1aSmatthias.ringwald break; 129811e23e5fSmatthias.ringwald } 129968d92d03Smatthias.ringwald 1300038bc64cSmatthias.ringwald // create internal event 1301ee8bf225Smatthias.ringwald hci_emit_state(); 1302ee8bf225Smatthias.ringwald 130368d92d03Smatthias.ringwald // trigger next/first action 130468d92d03Smatthias.ringwald hci_run(); 130568d92d03Smatthias.ringwald 1306475c8125Smatthias.ringwald return 0; 1307475c8125Smatthias.ringwald } 1308475c8125Smatthias.ringwald 1309758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1310758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 13113a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1312758b46ceSmatthias.ringwald hci_run(); 1313758b46ceSmatthias.ringwald } 1314758b46ceSmatthias.ringwald 1315381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1316381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1317381fbed8Smatthias.ringwald 13183a9fb326S[email protected] if (hci_stack->discoverable == enable){ 13193a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1320381fbed8Smatthias.ringwald return; 1321381fbed8Smatthias.ringwald } 1322381fbed8Smatthias.ringwald 13233a9fb326S[email protected] hci_stack->discoverable = enable; 1324758b46ceSmatthias.ringwald hci_update_scan_enable(); 1325758b46ceSmatthias.ringwald } 1326b031bebbSmatthias.ringwald 1327758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1328758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1329758b46ceSmatthias.ringwald 1330758b46ceSmatthias.ringwald // don't emit event 13313a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1332758b46ceSmatthias.ringwald 13333a9fb326S[email protected] hci_stack->connectable = enable; 1334758b46ceSmatthias.ringwald hci_update_scan_enable(); 1335381fbed8Smatthias.ringwald } 1336381fbed8Smatthias.ringwald 13375061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 13383a9fb326S[email protected] return &hci_stack->local_bd_addr; 13395061f3afS[email protected] } 13405061f3afS[email protected] 134106b35ec0Smatthias.ringwald void hci_run(){ 13428a485f27Smatthias.ringwald 134332ab9390Smatthias.ringwald hci_connection_t * connection; 134432ab9390Smatthias.ringwald linked_item_t * it; 134532ab9390Smatthias.ringwald 134664f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 1347ce4c8fabSmatthias.ringwald 1348b031bebbSmatthias.ringwald // global/non-connection oriented commands 1349b031bebbSmatthias.ringwald 1350b031bebbSmatthias.ringwald // decline incoming connections 13513a9fb326S[email protected] if (hci_stack->decline_reason){ 13523a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 13533a9fb326S[email protected] hci_stack->decline_reason = 0; 13543a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1355dbe1a790S[email protected] return; 1356ce4c8fabSmatthias.ringwald } 1357ce4c8fabSmatthias.ringwald 1358b031bebbSmatthias.ringwald // send scan enable 13593a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 13603a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 13613a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1362dbe1a790S[email protected] return; 1363b031bebbSmatthias.ringwald } 1364b031bebbSmatthias.ringwald 1365*7bdc6798S[email protected] // handle le scan 1366*7bdc6798S[email protected] if (hci_stack->state == HCI_STATE_WORKING){ 1367*7bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 1368*7bdc6798S[email protected] case LE_START_SCAN: 1369*7bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 1370*7bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 1371*7bdc6798S[email protected] return; 1372*7bdc6798S[email protected] 1373*7bdc6798S[email protected] case LE_STOP_SCAN: 1374*7bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 1375*7bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 1376*7bdc6798S[email protected] return; 1377*7bdc6798S[email protected] default: 1378*7bdc6798S[email protected] break; 1379*7bdc6798S[email protected] } 1380*7bdc6798S[email protected] } 1381*7bdc6798S[email protected] 138232ab9390Smatthias.ringwald // send pending HCI commands 13833a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 138432ab9390Smatthias.ringwald 1385b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 138632ab9390Smatthias.ringwald 1387ad83dc6aS[email protected] if (connection->state == SEND_CREATE_CONNECTION){ 1388ad83dc6aS[email protected] log_info("sending hci_create_connection\n"); 1389ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 1390ad83dc6aS[email protected] return; 1391ad83dc6aS[email protected] } 1392ad83dc6aS[email protected] 139332ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 13947b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 139532ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 139634d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1397dbe1a790S[email protected] return; 1398c7e0c5f6Smatthias.ringwald } 1399c7e0c5f6Smatthias.ringwald 140032ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 140134d2123cS[email protected] log_info("responding to link key request\n"); 140234d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 140332ab9390Smatthias.ringwald link_key_t link_key; 1404c77e8838S[email protected] link_key_type_t link_key_type; 14053a9fb326S[email protected] if ( hci_stack->remote_device_db 14063a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 140734d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 14089ab95c90S[email protected] connection->link_key_type = link_key_type; 140932ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 141032ab9390Smatthias.ringwald } else { 141132ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 141232ab9390Smatthias.ringwald } 1413dbe1a790S[email protected] return; 141432ab9390Smatthias.ringwald } 14151d6b20aeS[email protected] 1416899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 14174c57c146S[email protected] log_info("denying to pin request\n"); 1418899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 141934d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 14204c57c146S[email protected] return; 14214c57c146S[email protected] } 14224c57c146S[email protected] 1423dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 142434d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 142582d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 142682d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 1427106d6d11S[email protected] // tweak authentication requirements 14283a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1429106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 14309faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 14319faad3abS[email protected] } 14329faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 14339faad3abS[email protected] authreq |= 1; 1434106d6d11S[email protected] } 14353a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1436f8fb5f6eS[email protected] } else { 1437f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1438f8fb5f6eS[email protected] } 1439dbe1a790S[email protected] return; 144032ab9390Smatthias.ringwald } 144132ab9390Smatthias.ringwald 1442dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1443dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 144434d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1445dbe1a790S[email protected] return; 1446dbe1a790S[email protected] } 1447dbe1a790S[email protected] 1448dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1449dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 145034d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1451dbe1a790S[email protected] return; 1452dbe1a790S[email protected] } 1453afd4e962S[email protected] 1454afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1455afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 145634d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 14572bd8b7e7S[email protected] return; 14582bd8b7e7S[email protected] } 14592bd8b7e7S[email protected] 14602bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 14612bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 146234d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 146334d2123cS[email protected] return; 146434d2123cS[email protected] } 1465ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1466ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 146752d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 1468ad83dc6aS[email protected] return; 1469ad83dc6aS[email protected] } 147034d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 147134d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 147234d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 14732bd8b7e7S[email protected] return; 1474afd4e962S[email protected] } 1475dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 1476dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 1477dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 1478dce78009S[email protected] return; 1479dce78009S[email protected] } 1480dbe1a790S[email protected] } 1481c7e0c5f6Smatthias.ringwald 14823a9fb326S[email protected] switch (hci_stack->state){ 14833429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 14843a9fb326S[email protected] // log_info("hci_init: substate %u\n", hci_stack->substate); 14853a9fb326S[email protected] if (hci_stack->substate % 2) { 14863429f56bSmatthias.ringwald // odd: waiting for command completion 148706b35ec0Smatthias.ringwald return; 14883429f56bSmatthias.ringwald } 14893a9fb326S[email protected] switch (hci_stack->substate >> 1){ 1490c7492964Smatthias.ringwald case 0: // RESET 149122909952Smatthias.ringwald hci_send_cmd(&hci_reset); 149224052c2aS[email protected] 14933a9fb326S[email protected] if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 1494c7492964Smatthias.ringwald // skip baud change 14953a9fb326S[email protected] hci_stack->substate = 4; // >> 1 = 2 1496c7492964Smatthias.ringwald } 14973429f56bSmatthias.ringwald break; 1498c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 14993a9fb326S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 15003a9fb326S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1501c7492964Smatthias.ringwald break; 1502c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 15033a9fb326S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 15043a9fb326S[email protected] hci_stack->substate += 2; 1505c7492964Smatthias.ringwald // break missing here for fall through 1506c7492964Smatthias.ringwald 1507c7492964Smatthias.ringwald case 3: 150824052c2aS[email protected] // Custom initialization 15093a9fb326S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 15103a9fb326S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 1511d62aca9fSmatthias.ringwald if (valid_cmd){ 15123a9fb326S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 15133a9fb326S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 15143a9fb326S[email protected] hci_stack->substate = 4; // more init commands 151590919203Smatthias.ringwald break; 151690919203Smatthias.ringwald } 1517d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 151890919203Smatthias.ringwald } 15192f6c30e1Smatthias.ringwald // otherwise continue 1520f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1521f432a6ddSmatthias.ringwald break; 1522c7492964Smatthias.ringwald case 4: 1523e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1524e2edc0c3Smatthias.ringwald break; 1525c7492964Smatthias.ringwald case 5: 152665389bfcS[email protected] hci_send_cmd(&hci_read_local_supported_features); 15273429f56bSmatthias.ringwald break; 1528c7492964Smatthias.ringwald case 6: 1529d7e0e3a8S[email protected] if (hci_le_supported()){ 1530d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 1531d7e0e3a8S[email protected] } else { 1532d7e0e3a8S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 1533d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 1534d7e0e3a8S[email protected] } 1535f5d8d141S[email protected] 1536f5d8d141S[email protected] // skip Classic init commands for LE only chipsets 153724052c2aS[email protected] if (!hci_classic_supported()){ 153824052c2aS[email protected] if (hci_le_supported()){ 15393a9fb326S[email protected] hci_stack->substate = 11 << 1; // skip all classic command 154024052c2aS[email protected] } else { 154124052c2aS[email protected] log_error("Neither BR/EDR nor LE supported"); 15423a9fb326S[email protected] hci_stack->substate = 13 << 1; // skip all 154324052c2aS[email protected] } 1544f5d8d141S[email protected] } 1545f5d8d141S[email protected] break; 1546f5d8d141S[email protected] case 7: 154724052c2aS[email protected] if (hci_ssp_supported()){ 15483a9fb326S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 1549f5d8d141S[email protected] break; 155024052c2aS[email protected] } 15513a9fb326S[email protected] hci_stack->substate += 2; 155224052c2aS[email protected] // break missing here for fall through 155324052c2aS[email protected] 1554f5d8d141S[email protected] case 8: 155565389bfcS[email protected] // ca. 15 sec 155665389bfcS[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 1557559e517eS[email protected] break; 1558f5d8d141S[email protected] case 9: 15593a9fb326S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1560e2386ba1S[email protected] break; 1561f5d8d141S[email protected] case 10: 15623a9fb326S[email protected] if (hci_stack->local_name){ 15633a9fb326S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 1564e2386ba1S[email protected] } else { 15658a485f27Smatthias.ringwald char hostname[30]; 1566e2386ba1S[email protected] #ifdef EMBEDDED 1567e2386ba1S[email protected] // BTstack-11:22:33:44:55:66 1568e2386ba1S[email protected] strcpy(hostname, "BTstack "); 15693a9fb326S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 1570e2386ba1S[email protected] printf("---> Name %s\n", hostname); 1571e2386ba1S[email protected] #else 1572e2386ba1S[email protected] // hostname for POSIX systems 15738a485f27Smatthias.ringwald gethostname(hostname, 30); 15748a485f27Smatthias.ringwald hostname[29] = '\0'; 1575e2386ba1S[email protected] #endif 15768a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 15778a485f27Smatthias.ringwald } 15783c4d4b90Smatthias.ringwald break; 1579e0dbca83S[email protected] case 11: 15803a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 158124052c2aS[email protected] if (!hci_le_supported()){ 158224052c2aS[email protected] // SKIP LE init for Classic only configuration 15833a9fb326S[email protected] hci_stack->substate = 13 << 1; 158424052c2aS[email protected] } 1585a45d6b9fS[email protected] break; 158624052c2aS[email protected] 158743aa7007S[email protected] #ifdef HAVE_BLE 158824052c2aS[email protected] // LE INIT 1589a45d6b9fS[email protected] case 12: 159024052c2aS[email protected] hci_send_cmd(&hci_le_read_buffer_size); 159124052c2aS[email protected] break; 159224052c2aS[email protected] case 13: 159324052c2aS[email protected] // LE Supported Host = 1, Simultaneous Host = 0 159424052c2aS[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 159524052c2aS[email protected] break; 159643aa7007S[email protected] #endif 159724052c2aS[email protected] 159824052c2aS[email protected] // DONE 159924052c2aS[email protected] case 14: 16003429f56bSmatthias.ringwald // done. 16013a9fb326S[email protected] hci_stack->state = HCI_STATE_WORKING; 1602b360b6adSmatthias.ringwald hci_emit_state(); 16033429f56bSmatthias.ringwald break; 16043429f56bSmatthias.ringwald default: 16053429f56bSmatthias.ringwald break; 1606475c8125Smatthias.ringwald } 16073a9fb326S[email protected] hci_stack->substate++; 16083429f56bSmatthias.ringwald break; 1609c7e0c5f6Smatthias.ringwald 1610c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1611c7e0c5f6Smatthias.ringwald 16127b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1613c7e0c5f6Smatthias.ringwald // close all open connections 16143a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 1615c7e0c5f6Smatthias.ringwald if (connection){ 161632ab9390Smatthias.ringwald 1617c7e0c5f6Smatthias.ringwald // send disconnect 161864f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 161932ab9390Smatthias.ringwald 162079bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 16216ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1622c7e0c5f6Smatthias.ringwald 1623c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1624c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1625c7e0c5f6Smatthias.ringwald return; 1626c7e0c5f6Smatthias.ringwald } 16277b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1628c7e0c5f6Smatthias.ringwald 162972ea5239Smatthias.ringwald // switch mode 1630c7e0c5f6Smatthias.ringwald hci_power_control_off(); 16319418f9c9Smatthias.ringwald 16327b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 163372ea5239Smatthias.ringwald hci_emit_state(); 16347b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 163572ea5239Smatthias.ringwald break; 1636c7e0c5f6Smatthias.ringwald 163772ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 16383a9fb326S[email protected] switch(hci_stack->substate) { 163989db417bSmatthias.ringwald case 0: 16407b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 164172ea5239Smatthias.ringwald // close all open connections 16423a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 164366da7044Smatthias.ringwald 164428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 164566da7044Smatthias.ringwald // don't close connections, if H4 supports power management 164666da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 164766da7044Smatthias.ringwald connection = NULL; 164866da7044Smatthias.ringwald } 164966da7044Smatthias.ringwald #endif 165072ea5239Smatthias.ringwald if (connection){ 165132ab9390Smatthias.ringwald 165272ea5239Smatthias.ringwald // send disconnect 165332ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 165432ab9390Smatthias.ringwald 165579bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 16566ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 165772ea5239Smatthias.ringwald 165872ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 165972ea5239Smatthias.ringwald hci_shutdown_connection(connection); 166072ea5239Smatthias.ringwald return; 166172ea5239Smatthias.ringwald } 166272ea5239Smatthias.ringwald 166392368cd3S[email protected] if (hci_classic_supported()){ 166489db417bSmatthias.ringwald // disable page and inquiry scan 166564f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 166632ab9390Smatthias.ringwald 166792368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 16683a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 166989db417bSmatthias.ringwald 167089db417bSmatthias.ringwald // continue in next sub state 16713a9fb326S[email protected] hci_stack->substate++; 167289db417bSmatthias.ringwald break; 167392368cd3S[email protected] } 167492368cd3S[email protected] // fall through for ble-only chips 167592368cd3S[email protected] 167689db417bSmatthias.ringwald case 2: 16777b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 167828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 167928171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 168028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 168128171530Smatthias.ringwald // SLEEP MODE reached 16823a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 168328171530Smatthias.ringwald hci_emit_state(); 168428171530Smatthias.ringwald break; 168528171530Smatthias.ringwald } 168628171530Smatthias.ringwald #endif 168772ea5239Smatthias.ringwald // switch mode 16883a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 1689c7e0c5f6Smatthias.ringwald hci_emit_state(); 169028171530Smatthias.ringwald break; 169128171530Smatthias.ringwald 169289db417bSmatthias.ringwald default: 169389db417bSmatthias.ringwald break; 169489db417bSmatthias.ringwald } 1695c7e0c5f6Smatthias.ringwald break; 1696c7e0c5f6Smatthias.ringwald 16973429f56bSmatthias.ringwald default: 16983429f56bSmatthias.ringwald break; 16991f504dbdSmatthias.ringwald } 17003429f56bSmatthias.ringwald } 170116833f0aSmatthias.ringwald 170231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1703c8e4258aSmatthias.ringwald bd_addr_t addr; 1704c8e4258aSmatthias.ringwald hci_connection_t * conn; 1705c8e4258aSmatthias.ringwald // house-keeping 1706c8e4258aSmatthias.ringwald 1707c8e4258aSmatthias.ringwald // create_connection? 1708c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1709c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1710339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1711c8e4258aSmatthias.ringwald 171296a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 1713ad83dc6aS[email protected] if (!conn){ 171496a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 171517f1ba2aSmatthias.ringwald if (!conn){ 171617f1ba2aSmatthias.ringwald // notify client that alloc failed 171717f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 171817f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 171917f1ba2aSmatthias.ringwald } 1720ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 1721ad83dc6aS[email protected] } 1722ad83dc6aS[email protected] log_info("conn state %u", conn->state); 1723ad83dc6aS[email protected] switch (conn->state){ 1724ad83dc6aS[email protected] // if connection active exists 1725ad83dc6aS[email protected] case OPEN: 172662bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 1727ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 172862bda3fbS[email protected] return 0; 1729ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 1730ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 1731ad83dc6aS[email protected] break; 1732ad83dc6aS[email protected] default: 1733ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 1734ad83dc6aS[email protected] return 0; 1735ad83dc6aS[email protected] } 1736c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1737c8e4258aSmatthias.ringwald } 1738c8e4258aSmatthias.ringwald 17397fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 17407fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 17417fde4af9Smatthias.ringwald } 17427fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 17437fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 17447fde4af9Smatthias.ringwald } 17457fde4af9Smatthias.ringwald 17468ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 17473a9fb326S[email protected] if (hci_stack->remote_device_db){ 17488ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 17493a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 17508ef73945Smatthias.ringwald } 17518ef73945Smatthias.ringwald } 1752c8e4258aSmatthias.ringwald 17536724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 17546724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 17556724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 175696a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 17576724cd9eS[email protected] if (conn){ 17586724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 17596724cd9eS[email protected] } 17606724cd9eS[email protected] } 17616724cd9eS[email protected] 17626724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 17636724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 17646724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 17656724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 17666724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 176796a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 17686724cd9eS[email protected] if (conn){ 17696724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 17706724cd9eS[email protected] } 17716724cd9eS[email protected] } 17726724cd9eS[email protected] 177369a97523S[email protected] #ifdef HAVE_BLE 177469a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 17753a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 177669a97523S[email protected] } 177769a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 17783a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 177969a97523S[email protected] } 178069a97523S[email protected] #endif 178169a97523S[email protected] 17823a9fb326S[email protected] hci_stack->num_cmd_packets--; 17836b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 17846b4af23dS[email protected] 17856b4af23dS[email protected] // free packet buffer for synchronous transport implementations 1786c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 17876b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 17886b4af23dS[email protected] } 17896b4af23dS[email protected] 17906b4af23dS[email protected] return err; 179131452debSmatthias.ringwald } 17928adf0ddaSmatthias.ringwald 17932bd8b7e7S[email protected] // disconnect because of security block 17942bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 17952bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 17962bd8b7e7S[email protected] if (!connection) return; 17972bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 17982bd8b7e7S[email protected] } 17992bd8b7e7S[email protected] 18002bd8b7e7S[email protected] 1801dbe1a790S[email protected] // Configure Secure Simple Pairing 1802dbe1a790S[email protected] 1803dbe1a790S[email protected] // enable will enable SSP during init 1804dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 18053a9fb326S[email protected] hci_stack->ssp_enable = enable; 1806dbe1a790S[email protected] } 1807dbe1a790S[email protected] 18082bd8b7e7S[email protected] int hci_local_ssp_activated(){ 18093a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 18102bd8b7e7S[email protected] } 18112bd8b7e7S[email protected] 1812dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1813dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 18143a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 1815dbe1a790S[email protected] } 1816dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 18173a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 1818dbe1a790S[email protected] } 1819dbe1a790S[email protected] 1820dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 1821dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 18223a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 1823dbe1a790S[email protected] } 1824dbe1a790S[email protected] 18251cd208adSmatthias.ringwald /** 18261cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 18271cd208adSmatthias.ringwald */ 1828fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 18291cd208adSmatthias.ringwald va_list argptr; 18301cd208adSmatthias.ringwald va_start(argptr, cmd); 18313a9fb326S[email protected] uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr); 18321cd208adSmatthias.ringwald va_end(argptr); 18333a9fb326S[email protected] return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size); 183493b8dc03Smatthias.ringwald } 1835c8e4258aSmatthias.ringwald 1836ee091cf1Smatthias.ringwald // Create various non-HCI events. 1837ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1838ee091cf1Smatthias.ringwald 1839c8e4258aSmatthias.ringwald void hci_emit_state(){ 18403a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 1841425d1371Smatthias.ringwald uint8_t event[3]; 184280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1843425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 18443a9fb326S[email protected] event[2] = hci_stack->state; 1845425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18463a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1847c8e4258aSmatthias.ringwald } 1848c8e4258aSmatthias.ringwald 184917f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1850425d1371Smatthias.ringwald uint8_t event[13]; 1851c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1852425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 185317f1ba2aSmatthias.ringwald event[2] = status; 1854c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1855c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1856c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1857c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1858425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18593a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1860c8e4258aSmatthias.ringwald } 1861c8e4258aSmatthias.ringwald 18623c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1863425d1371Smatthias.ringwald uint8_t event[6]; 18643c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1865e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 18663c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 18673c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 18683c4d4b90Smatthias.ringwald event[5] = reason; 1869425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18703a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 18713c4d4b90Smatthias.ringwald } 18723c4d4b90Smatthias.ringwald 1873ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 187466fb9560S[email protected] if (disable_l2cap_timeouts) return; 1875e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1876425d1371Smatthias.ringwald uint8_t event[4]; 187780d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1878425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1879ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1880425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18813a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1882ee091cf1Smatthias.ringwald } 188343bfb1bdSmatthias.ringwald 188443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1885e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1886425d1371Smatthias.ringwald uint8_t event[3]; 188780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1888425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 188943bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1890425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18913a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 189243bfb1bdSmatthias.ringwald } 1893038bc64cSmatthias.ringwald 1894038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1895e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1896425d1371Smatthias.ringwald uint8_t event[2]; 189780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1898425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1899425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19003a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1901038bc64cSmatthias.ringwald } 19021b0e3922Smatthias.ringwald 190309ba8edeSmatthias.ringwald #ifndef EMBEDDED 19041b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1905e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1906425d1371Smatthias.ringwald uint8_t event[6]; 19071b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1908425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1909425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1910425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1911425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1912425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19133a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 19141b0e3922Smatthias.ringwald } 191509ba8edeSmatthias.ringwald #endif 19161b0e3922Smatthias.ringwald 19172ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1918e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1919425d1371Smatthias.ringwald uint8_t event[3]; 19202ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1921425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 19222ed6235cSmatthias.ringwald event[2] = enabled; 1923425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19243a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 19252ed6235cSmatthias.ringwald } 1926627c2f45Smatthias.ringwald 1927627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1928e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1929627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1930e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1931f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 19322f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1933f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1934e0abb8e7S[email protected] 1935e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1936e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1937e0abb8e7S[email protected] 1938e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 19393a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1940627c2f45Smatthias.ringwald } 1941381fbed8Smatthias.ringwald 1942381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1943e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1944425d1371Smatthias.ringwald uint8_t event[3]; 1945381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1946425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1947381fbed8Smatthias.ringwald event[2] = enabled; 1948425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19493a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1950381fbed8Smatthias.ringwald } 1951458bf4e8S[email protected] 1952a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 1953df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 1954a00031e2S[email protected] uint8_t event[5]; 1955e00caf9cS[email protected] int pos = 0; 1956a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 1957e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 1958a00031e2S[email protected] bt_store_16(event, 2, con_handle); 1959e00caf9cS[email protected] pos += 2; 1960e00caf9cS[email protected] event[pos++] = level; 1961e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19623a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1963e00caf9cS[email protected] } 1964e00caf9cS[email protected] 1965ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){ 1966ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 1967ad83dc6aS[email protected] uint8_t event[9]; 1968ad83dc6aS[email protected] int pos = 0; 1969ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 1970ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 1971ad83dc6aS[email protected] event[pos++] = status; 1972ad83dc6aS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address); 1973ad83dc6aS[email protected] pos += 6; 1974ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19753a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1976ad83dc6aS[email protected] } 1977ad83dc6aS[email protected] 19782bd8b7e7S[email protected] // query if remote side supports SSP 19792bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 19802bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 19812bd8b7e7S[email protected] if (!connection) return 0; 19822bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 19832bd8b7e7S[email protected] } 19842bd8b7e7S[email protected] 1985df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 1986df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 1987df3354fcS[email protected] } 1988df3354fcS[email protected] 1989458bf4e8S[email protected] // GAP API 1990458bf4e8S[email protected] /** 1991458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 1992458bf4e8S[email protected] * @praram enabled 1993458bf4e8S[email protected] */ 19944c57c146S[email protected] void gap_set_bondable_mode(int enable){ 19953a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 1996458bf4e8S[email protected] } 1997cb230b9dS[email protected] 1998cb230b9dS[email protected] /** 199934d2123cS[email protected] * @brief map link keys to security levels 2000cb230b9dS[email protected] */ 200134d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 200234d2123cS[email protected] switch (link_key_type){ 20033c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 20043c68dfa9S[email protected] return LEVEL_4; 20053c68dfa9S[email protected] case COMBINATION_KEY: 20063c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 20073c68dfa9S[email protected] return LEVEL_3; 20083c68dfa9S[email protected] default: 20093c68dfa9S[email protected] return LEVEL_2; 20103c68dfa9S[email protected] } 2011cb230b9dS[email protected] } 2012cb230b9dS[email protected] 2013a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 201434d2123cS[email protected] if (!connection) return LEVEL_0; 201534d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 201634d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 201734d2123cS[email protected] } 201834d2123cS[email protected] 201934d2123cS[email protected] 2020106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 202182d8f825S[email protected] printf("gap_mitm_protection_required_for_security_level %u\n", level); 2022106d6d11S[email protected] return level > LEVEL_2; 2023106d6d11S[email protected] } 2024106d6d11S[email protected] 202534d2123cS[email protected] /** 202634d2123cS[email protected] * @brief get current security level 202734d2123cS[email protected] */ 202834d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 202934d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 203034d2123cS[email protected] if (!connection) return LEVEL_0; 203134d2123cS[email protected] return gap_security_level_for_connection(connection); 203234d2123cS[email protected] } 203334d2123cS[email protected] 2034cb230b9dS[email protected] /** 2035cb230b9dS[email protected] * @brief request connection to device to 2036cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 2037cb230b9dS[email protected] */ 203834d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 203934d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 204034d2123cS[email protected] if (!connection){ 2041a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 204234d2123cS[email protected] return; 204334d2123cS[email protected] } 204434d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 204534d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 204634d2123cS[email protected] if (current_level >= requested_level){ 2047a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 204834d2123cS[email protected] return; 204934d2123cS[email protected] } 2050a00031e2S[email protected] 205134d2123cS[email protected] connection->requested_security_level = requested_level; 2052a00031e2S[email protected] 2053fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 20543a9fb326S[email protected] if (hci_stack->remote_device_db){ 2055a00031e2S[email protected] link_key_type_t link_key_type; 2056a00031e2S[email protected] link_key_t link_key; 20573a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 2058a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 2059a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2060a00031e2S[email protected] return; 2061a00031e2S[email protected] } 2062a00031e2S[email protected] } 2063a00031e2S[email protected] } 2064a00031e2S[email protected] 20651eb2563eS[email protected] // try to authenticate connection 20661eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2067e80b2cf9S[email protected] hci_run(); 2068e00caf9cS[email protected] } 2069ad83dc6aS[email protected] 2070ad83dc6aS[email protected] /** 2071ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 2072ad83dc6aS[email protected] * @param device 2073ad83dc6aS[email protected] * @param request MITM protection 2074ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 2075ad83dc6aS[email protected] */ 2076ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 2077ad83dc6aS[email protected] 2078ad83dc6aS[email protected] // create connection state machine 207996a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 2080ad83dc6aS[email protected] 2081ad83dc6aS[email protected] if (!connection){ 2082ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 2083ad83dc6aS[email protected] } 2084ad83dc6aS[email protected] 2085ad83dc6aS[email protected] // delete linkn key 2086ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 2087ad83dc6aS[email protected] 2088ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 2089ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 2090ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 209182d8f825S[email protected] printf("gap_dedicated_bonding, mitm %u -> level %u\n", mitm_protection_required, connection->requested_security_level); 2092ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 2093ad83dc6aS[email protected] 2094ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 2095ad83dc6aS[email protected] 2096ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 2097ad83dc6aS[email protected] // handle: authentication failure 2098ad83dc6aS[email protected] // handle: disconnect on done 2099ad83dc6aS[email protected] 2100ad83dc6aS[email protected] hci_run(); 2101ad83dc6aS[email protected] 2102ad83dc6aS[email protected] return 0; 2103ad83dc6aS[email protected] } 21048e618f72S[email protected] 21058e618f72S[email protected] void gap_set_local_name(const char * local_name){ 21068e618f72S[email protected] hci_stack->local_name = local_name; 21078e618f72S[email protected] } 21088e618f72S[email protected] 2109*7bdc6798S[email protected] le_command_status_t le_central_start_scan(){ 2110*7bdc6798S[email protected] printf("hci_init: scanning state START_SCAN\n"); 2111*7bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 2112*7bdc6798S[email protected] hci_run(); 2113*7bdc6798S[email protected] return BLE_PERIPHERAL_OK; 2114*7bdc6798S[email protected] } 21158e618f72S[email protected] 2116*7bdc6798S[email protected] le_command_status_t le_central_stop_scan(){ 2117*7bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 2118*7bdc6798S[email protected] hci_run(); 2119*7bdc6798S[email protected] return BLE_PERIPHERAL_OK; 2120*7bdc6798S[email protected] } 2121