xref: /btstack/src/hci.c (revision d7e0e3a80cf918cf9b23dbf491279da0862040a2)
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 
44c8901d41Smatthias.ringwald #include "config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
477f2435e6Smatthias.ringwald 
4893b8dc03Smatthias.ringwald #include <stdarg.h>
4993b8dc03Smatthias.ringwald #include <string.h>
5056fe0872Smatthias.ringwald #include <stdio.h>
517f2435e6Smatthias.ringwald 
52549e6ebeSmatthias.ringwald #ifndef EMBEDDED
53549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5409ba8edeSmatthias.ringwald #include <btstack/version.h>
55549e6ebeSmatthias.ringwald #endif
56549e6ebeSmatthias.ringwald 
57a3b02b71Smatthias.ringwald #include "btstack_memory.h"
587f2435e6Smatthias.ringwald #include "debug.h"
59d8905019Smatthias.ringwald #include "hci_dump.h"
6093b8dc03Smatthias.ringwald 
61ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
621b0e3922Smatthias.ringwald 
63169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
64ee091cf1Smatthias.ringwald 
65a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
66da5275c5S[email protected] 
6728171530Smatthias.ringwald #ifdef USE_BLUETOOL
6828171530Smatthias.ringwald #include "bt_control_iphone.h"
6928171530Smatthias.ringwald #endif
7028171530Smatthias.ringwald 
71758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
72758b46ceSmatthias.ringwald 
7306b35ec0Smatthias.ringwald // the STACK is here
7416833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
7516833f0aSmatthias.ringwald 
7697addcc5Smatthias.ringwald /**
77ee091cf1Smatthias.ringwald  * get connection for a given handle
78ee091cf1Smatthias.ringwald  *
79ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
80ee091cf1Smatthias.ringwald  */
81645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
82ee091cf1Smatthias.ringwald     linked_item_t *it;
83ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
84ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
85ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
86ee091cf1Smatthias.ringwald         }
87ee091cf1Smatthias.ringwald     }
88ee091cf1Smatthias.ringwald     return NULL;
89ee091cf1Smatthias.ringwald }
90ee091cf1Smatthias.ringwald 
912b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
9228ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
93c785ef68Smatthias.ringwald #ifdef HAVE_TIME
94ee091cf1Smatthias.ringwald     struct timeval tv;
95ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
96c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
97ee091cf1Smatthias.ringwald         // connections might be timed out
98ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
99ee091cf1Smatthias.ringwald     }
1002b12a0b9Smatthias.ringwald #endif
101e5780900Smatthias.ringwald #ifdef HAVE_TICK
102c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
103c785ef68Smatthias.ringwald         // connections might be timed out
104c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
105c785ef68Smatthias.ringwald     }
106c785ef68Smatthias.ringwald #endif
107c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
108c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
109c785ef68Smatthias.ringwald }
110ee091cf1Smatthias.ringwald 
111ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
112c7492964Smatthias.ringwald #ifdef HAVE_TIME
113ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
114c7492964Smatthias.ringwald #endif
115e5780900Smatthias.ringwald #ifdef HAVE_TICK
116c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
117c785ef68Smatthias.ringwald #endif
118ee091cf1Smatthias.ringwald }
119ee091cf1Smatthias.ringwald 
120ee091cf1Smatthias.ringwald /**
121c8e4258aSmatthias.ringwald  * create connection for given address
122c8e4258aSmatthias.ringwald  *
12317f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
124c8e4258aSmatthias.ringwald  */
125c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
12628ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
127c8e4258aSmatthias.ringwald     if (!conn) return NULL;
128c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
129c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1307d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
131ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
132ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
133ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
134d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1357856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
13656cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
137c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
138c8e4258aSmatthias.ringwald     return conn;
139c8e4258aSmatthias.ringwald }
140c8e4258aSmatthias.ringwald 
141c8e4258aSmatthias.ringwald /**
14206b35ec0Smatthias.ringwald  * get connection for given address
14397addcc5Smatthias.ringwald  *
14497addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
14597addcc5Smatthias.ringwald  */
146fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
14706b35ec0Smatthias.ringwald     linked_item_t *it;
14806b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
14906b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
15006b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
15106b35ec0Smatthias.ringwald         }
15206b35ec0Smatthias.ringwald     }
15306b35ec0Smatthias.ringwald     return NULL;
15406b35ec0Smatthias.ringwald }
15506b35ec0Smatthias.ringwald 
15628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
15728ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
15828ca2b46S[email protected] }
15928ca2b46S[email protected] 
16028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
16128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
16228ca2b46S[email protected] }
16328ca2b46S[email protected] 
16428ca2b46S[email protected] 
16543bfb1bdSmatthias.ringwald /**
16680ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1677fde4af9Smatthias.ringwald  */
1687fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1697fde4af9Smatthias.ringwald     bd_addr_t addr;
1707fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1717fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1727fde4af9Smatthias.ringwald     if (conn) {
17328ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
17480ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1757fde4af9Smatthias.ringwald     }
1767fde4af9Smatthias.ringwald }
1777fde4af9Smatthias.ringwald 
17880ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
17980ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
18080ca58a0Smatthias.ringwald     if (!conn) return 0;
18180ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
18280ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
18380ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
18480ca58a0Smatthias.ringwald     return 1;
18580ca58a0Smatthias.ringwald }
18680ca58a0Smatthias.ringwald 
187c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
188c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
189c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
190c12e46e7Smatthias.ringwald     }
191c12e46e7Smatthias.ringwald }
192c12e46e7Smatthias.ringwald 
1937fde4af9Smatthias.ringwald 
1947fde4af9Smatthias.ringwald /**
19543bfb1bdSmatthias.ringwald  * count connections
19643bfb1bdSmatthias.ringwald  */
19740d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
19856c253c9Smatthias.ringwald     int count = 0;
19943bfb1bdSmatthias.ringwald     linked_item_t *it;
20043bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
20143bfb1bdSmatthias.ringwald     return count;
20243bfb1bdSmatthias.ringwald }
203c8e4258aSmatthias.ringwald 
20497addcc5Smatthias.ringwald /**
205ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
20616833f0aSmatthias.ringwald  */
2072718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
20816833f0aSmatthias.ringwald }
20916833f0aSmatthias.ringwald 
210998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
211998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
212998906cdSmatthias.ringwald     if (!connection) {
2137d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
214998906cdSmatthias.ringwald         return 0;
215998906cdSmatthias.ringwald     }
216998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
217998906cdSmatthias.ringwald }
218998906cdSmatthias.ringwald 
219998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
220998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
221998906cdSmatthias.ringwald     linked_item_t *it;
222998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
223998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
224998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2257d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
226998906cdSmatthias.ringwald             return 0;
227998906cdSmatthias.ringwald         }
228998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
229998906cdSmatthias.ringwald     }
230998906cdSmatthias.ringwald     return free_slots;
231998906cdSmatthias.ringwald }
232998906cdSmatthias.ringwald 
233c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2342b12a0b9Smatthias.ringwald 
2352b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2362b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2372b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2382b12a0b9Smatthias.ringwald             return 0;
2392b12a0b9Smatthias.ringwald         }
2402b12a0b9Smatthias.ringwald     }
2412b12a0b9Smatthias.ringwald 
2422b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
243c24735b1Smatthias.ringwald     switch (packet_type) {
244c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
245c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
246c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
247de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
248c24735b1Smatthias.ringwald         default:
249c24735b1Smatthias.ringwald             return 0;
250c24735b1Smatthias.ringwald     }
251c24735b1Smatthias.ringwald }
252c24735b1Smatthias.ringwald 
253ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2547856c818Smatthias.ringwald 
2556218e6f1Smatthias.ringwald     // check for free places on BT module
2565932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2576218e6f1Smatthias.ringwald 
2587856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2597856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
26056cf178bSmatthias.ringwald     if (!connection) return 0;
26156cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
26256cf178bSmatthias.ringwald 
26356cf178bSmatthias.ringwald     // count packet
26456cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2657b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2667856c818Smatthias.ringwald 
26700d8e42eSmatthias.ringwald     // send packet
26800d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2696218e6f1Smatthias.ringwald 
27000d8e42eSmatthias.ringwald     return err;
271ee091cf1Smatthias.ringwald }
272ee091cf1Smatthias.ringwald 
27316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2747856c818Smatthias.ringwald 
2757856c818Smatthias.ringwald     // get info
2767856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2777856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2787856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2797856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2807856c818Smatthias.ringwald 
2817856c818Smatthias.ringwald     // ignore non-registered handle
2827856c818Smatthias.ringwald     if (!conn){
2837d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2847856c818Smatthias.ringwald         return;
2857856c818Smatthias.ringwald     }
2867856c818Smatthias.ringwald 
2877856c818Smatthias.ringwald     // update idle timestamp
2887856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2897856c818Smatthias.ringwald 
2907856c818Smatthias.ringwald     // handle different packet types
2917856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2927856c818Smatthias.ringwald 
2937856c818Smatthias.ringwald         case 0x01: // continuation fragment
2947856c818Smatthias.ringwald 
2957856c818Smatthias.ringwald             // sanity check
2967856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2977d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2987856c818Smatthias.ringwald                 return;
2997856c818Smatthias.ringwald             }
3007856c818Smatthias.ringwald 
3017856c818Smatthias.ringwald             // append fragment payload (header already stored)
3027856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3037856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3047856c818Smatthias.ringwald 
3057d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
306decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3077856c818Smatthias.ringwald 
3087856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3097856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3107856c818Smatthias.ringwald 
3112718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3127856c818Smatthias.ringwald                 // reset recombination buffer
3137856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3147856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3157856c818Smatthias.ringwald             }
3167856c818Smatthias.ringwald             break;
3177856c818Smatthias.ringwald 
3187856c818Smatthias.ringwald         case 0x02: { // first fragment
3197856c818Smatthias.ringwald 
3207856c818Smatthias.ringwald             // sanity check
3217856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3227d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3237856c818Smatthias.ringwald                 return;
3247856c818Smatthias.ringwald             }
3257856c818Smatthias.ringwald 
3267856c818Smatthias.ringwald             // peek into L2CAP packet!
3277856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3287856c818Smatthias.ringwald 
3297d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
330decc01a8Smatthias.ringwald 
3317856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3327856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3337856c818Smatthias.ringwald 
3347856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3352718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3367856c818Smatthias.ringwald 
3377856c818Smatthias.ringwald             } else {
3387856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3397856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3407856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3417856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
342decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3437856c818Smatthias.ringwald             }
3447856c818Smatthias.ringwald             break;
3457856c818Smatthias.ringwald 
3467856c818Smatthias.ringwald         }
3477856c818Smatthias.ringwald         default:
3487d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3497856c818Smatthias.ringwald             return;
3507856c818Smatthias.ringwald     }
35194ab26f8Smatthias.ringwald 
35294ab26f8Smatthias.ringwald     // execute main loop
35394ab26f8Smatthias.ringwald     hci_run();
35416833f0aSmatthias.ringwald }
35522909952Smatthias.ringwald 
35667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
357339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3583c4d4b90Smatthias.ringwald 
3593c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3603c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3613c4d4b90Smatthias.ringwald 
362c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
363c785ef68Smatthias.ringwald 
364c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
365a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3663c4d4b90Smatthias.ringwald 
3673c4d4b90Smatthias.ringwald     // now it's gone
368c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
369c7e0c5f6Smatthias.ringwald }
370c7e0c5f6Smatthias.ringwald 
3710c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3728f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3738f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3748f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3758f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3768f8108aaSmatthias.ringwald };
37765389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
37865389bfcS[email protected]      0, // 3 slot packets
37965389bfcS[email protected]      1, // 5 slot packets
38065389bfcS[email protected]     25, // EDR 2 mpbs
38165389bfcS[email protected]     26, // EDR 3 mbps
38265389bfcS[email protected]     39, // 3 slot EDR packts
38365389bfcS[email protected]     40, // 5 slot EDR packet
38465389bfcS[email protected] };
38565389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
38665389bfcS[email protected]     0x0f00, // 3 slot packets
38765389bfcS[email protected]     0xf000, // 5 slot packets
38865389bfcS[email protected]     0x1102, // EDR 2 mpbs
38965389bfcS[email protected]     0x2204, // EDR 3 mbps
39065389bfcS[email protected]     0x0300, // 3 slot EDR packts
39165389bfcS[email protected]     0x3000, // 5 slot EDR packet
39265389bfcS[email protected] };
3938f8108aaSmatthias.ringwald 
39465389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
39565389bfcS[email protected]     // enable packet types based on size
3968f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
3978f8108aaSmatthias.ringwald     int i;
3988f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
3998f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4008f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4018f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4028f8108aaSmatthias.ringwald         }
4038f8108aaSmatthias.ringwald     }
40465389bfcS[email protected]     // disable packet types due to missing local supported features
40565389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
40665389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
40765389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
40865389bfcS[email protected]         if (feature_set) continue;
40965389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
41065389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
41165389bfcS[email protected]     }
4128f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4138f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4148f8108aaSmatthias.ringwald     return packet_types;
4158f8108aaSmatthias.ringwald }
4168f8108aaSmatthias.ringwald 
4178f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4188f8108aaSmatthias.ringwald     return hci_stack.packet_types;
4198f8108aaSmatthias.ringwald }
4208f8108aaSmatthias.ringwald 
4217dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4227dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4237dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
4247dc17943Smatthias.ringwald }
4257dc17943Smatthias.ringwald 
426f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4277dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
4287dc17943Smatthias.ringwald }
4297dc17943Smatthias.ringwald 
430f5d8d141S[email protected] int hci_ssp_supported(void){
431f5d8d141S[email protected]     // No 51, byte 6, bit 3
432f5d8d141S[email protected]     return (hci_stack.local_supported_features[6] & (1 << 3)) != 0;
433f5d8d141S[email protected] }
434f5d8d141S[email protected] 
435f5d8d141S[email protected] int hci_classic_supported(void){
436f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
437f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 5)) == 0;
438f5d8d141S[email protected] }
439f5d8d141S[email protected] 
440f5d8d141S[email protected] int hci_le_supported(void){
441f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
442f5d8d141S[email protected] #ifdef HAVE_BLE
443f5d8d141S[email protected]     return (hci_stack.local_supported_features[4] & (1 << 6)) != 0;
444f5d8d141S[email protected] #else
445f5d8d141S[email protected]     return 0;
446f5d8d141S[email protected] #endif
447f5d8d141S[email protected] }
448f5d8d141S[email protected] 
44974ec757aSmatthias.ringwald // avoid huge local variables
450223aafc1Smatthias.ringwald #ifndef EMBEDDED
45174ec757aSmatthias.ringwald static device_name_t device_name;
452223aafc1Smatthias.ringwald #endif
45316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
4541281a47eSmatthias.ringwald     bd_addr_t addr;
4552d00edd4Smatthias.ringwald     uint8_t link_type;
456fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
4571f7b95a1Smatthias.ringwald     hci_connection_t * conn;
45856cf178bSmatthias.ringwald     int i;
45922909952Smatthias.ringwald 
4605909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4615909f7f2Smatthias.ringwald 
4626772a24cSmatthias.ringwald     switch (packet[0]) {
46322909952Smatthias.ringwald 
4646772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4657ec5eeaaSmatthias.ringwald             // get num cmd packets
4667b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4677ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4687ec5eeaaSmatthias.ringwald 
469e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
470e2edc0c3Smatthias.ringwald                 // from offset 5
471e2edc0c3Smatthias.ringwald                 // status
4721d279b20Smatthias.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"
473e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
47456cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
475e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
47656cf178bSmatthias.ringwald                 // ignore: total num SCO packets
47756cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
478a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
4798fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
4808fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
4818f8108aaSmatthias.ringwald                     }
48265389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
48365389bfcS[email protected]                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
484e2edc0c3Smatthias.ringwald                 }
48556cf178bSmatthias.ringwald             }
48665a46ef3S[email protected] #ifdef HAVE_BLE
48765a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
48865a46ef3S[email protected]                 hci_stack.le_data_packet_length = READ_BT_16(packet, 6);
48965a46ef3S[email protected]                 hci_stack.total_num_le_packets  = packet[8];
49065a46ef3S[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);
49165a46ef3S[email protected]             }
49265a46ef3S[email protected] #endif
493188981d2Smatthias.ringwald             // Dump local address
494188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
495e2386ba1S[email protected]                 bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
496188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
497e2386ba1S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr));
498188981d2Smatthias.ringwald             }
499381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
500381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
501381fbed8Smatthias.ringwald             }
50265389bfcS[email protected]             // Note: HCI init checks
503559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
50465389bfcS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
505559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
506559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
507559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
508559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
509559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
51065389bfcS[email protected] 
51165389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
51265389bfcS[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]);
513f5d8d141S[email protected]                 log_info("packet types %04x", hci_stack.packet_types);
514f5d8d141S[email protected] 
515f5d8d141S[email protected]                 // Classic/LE
516f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
517559e517eS[email protected]             }
51856cf178bSmatthias.ringwald             break;
51956cf178bSmatthias.ringwald 
5207ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5217ec5eeaaSmatthias.ringwald             // get num cmd packets
5227b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
5237ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
5247ec5eeaaSmatthias.ringwald             break;
5257ec5eeaaSmatthias.ringwald 
52656cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
52756cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
52856cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
52956cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
53056cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
53156cf178bSmatthias.ringwald                 if (!conn){
5327d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
53356cf178bSmatthias.ringwald                     continue;
53456cf178bSmatthias.ringwald                 }
53556cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5367b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
53756cf178bSmatthias.ringwald             }
5386772a24cSmatthias.ringwald             break;
5396772a24cSmatthias.ringwald 
5401f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
54137eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
54237eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5432d00edd4Smatthias.ringwald             link_type = packet[11];
544339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
54537eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5461f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5471f7b95a1Smatthias.ringwald                 if (!conn) {
5481f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5491f7b95a1Smatthias.ringwald                 }
550ce4c8fabSmatthias.ringwald                 if (!conn) {
551ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
552ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
553ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
554ce4c8fabSmatthias.ringwald                     break;
555ce4c8fabSmatthias.ringwald                 }
55632ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
55732ab9390Smatthias.ringwald                 hci_run();
55837eaa4cfSmatthias.ringwald             } else {
559ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
560ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
561ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
56237eaa4cfSmatthias.ringwald             }
5631f7b95a1Smatthias.ringwald             break;
5641f7b95a1Smatthias.ringwald 
5656772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
566fe1ed1b8Smatthias.ringwald             // Connection management
567fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
568339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
5691f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
570fe1ed1b8Smatthias.ringwald             if (conn) {
571b448a0e7Smatthias.ringwald                 if (!packet[2]){
572c8e4258aSmatthias.ringwald                     conn->state = OPEN;
573fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
574ee091cf1Smatthias.ringwald 
575c785ef68Smatthias.ringwald                     // restart timer
576c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
577ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
578c785ef68Smatthias.ringwald 
579339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
58043bfb1bdSmatthias.ringwald 
58143bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
582b448a0e7Smatthias.ringwald                 } else {
583b448a0e7Smatthias.ringwald                     // connection failed, remove entry
584b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
585a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
586c12e46e7Smatthias.ringwald 
587c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
588c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
589c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
590c12e46e7Smatthias.ringwald                     }
591fe1ed1b8Smatthias.ringwald                 }
592fe1ed1b8Smatthias.ringwald             }
5936772a24cSmatthias.ringwald             break;
594fe1ed1b8Smatthias.ringwald 
5957fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5967b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5977fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
59874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
59932ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
60064472d52Smatthias.ringwald             hci_run();
601d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
60229d53098Smatthias.ringwald             return;
6037fde4af9Smatthias.ringwald 
6047fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
6057fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
60674ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
60729d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
608287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
60929d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
6107fde4af9Smatthias.ringwald             break;
6117fde4af9Smatthias.ringwald 
6127fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
6137fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
614d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
615d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
616d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
617d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
6187fde4af9Smatthias.ringwald             break;
6197fde4af9Smatthias.ringwald 
6201d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
6211d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
6221d6b20aeS[email protected]             if (hci_stack.ssp_io_capability == SSP_IO_CAPABILITY_UNKNOWN) break;
623dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
624dbe1a790S[email protected]             break;
625dbe1a790S[email protected] 
626dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
627dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_CONFIRM_REQUEST);
628dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
629dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
630dbe1a790S[email protected]             break;
631dbe1a790S[email protected] 
632dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
633dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_PASSKEY_REQUEST);
634dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
635dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
6361d6b20aeS[email protected]             break;
6371d6b20aeS[email protected] 
638223aafc1Smatthias.ringwald #ifndef EMBEDDED
63974ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
64074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
64174ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
64274ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
6435a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
6445a06394aSmatthias.ringwald             for (i=0; i<248;i++){
6455a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
6465a06394aSmatthias.ringwald                     packet[9+i] = 0;
6475a06394aSmatthias.ringwald                     break;
648cdc9101dSmatthias.ringwald                 }
6495a06394aSmatthias.ringwald             }
650d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
65174ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
65274ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
65374ec757aSmatthias.ringwald             break;
65474ec757aSmatthias.ringwald 
65574ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
65674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
65774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
65874ec757aSmatthias.ringwald             // first send inq result packet
65974ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
66074ec757aSmatthias.ringwald             // then send cached remote names
66174ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
66274ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
66374ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
66474ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
66574ec757aSmatthias.ringwald                 }
66674ec757aSmatthias.ringwald             }
66774ec757aSmatthias.ringwald             return;
668223aafc1Smatthias.ringwald #endif
66974ec757aSmatthias.ringwald 
6706772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
671fe1ed1b8Smatthias.ringwald             if (!packet[2]){
672fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
673fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
674fe1ed1b8Smatthias.ringwald                 if (conn) {
675c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
676fe1ed1b8Smatthias.ringwald                 }
677fe1ed1b8Smatthias.ringwald             }
6786772a24cSmatthias.ringwald             break;
6796772a24cSmatthias.ringwald 
680c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
681c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
682c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
683c68bdf90Smatthias.ringwald             }
684c68bdf90Smatthias.ringwald             break;
685c68bdf90Smatthias.ringwald 
6865909f7f2Smatthias.ringwald #ifdef HAVE_BLE
6875909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
6885909f7f2Smatthias.ringwald             switch (packet[2]) {
6895909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
6905909f7f2Smatthias.ringwald                     // Connection management
6915909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
6925909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
6935909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
6945909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
6955909f7f2Smatthias.ringwald                     if (packet[3]){
6965909f7f2Smatthias.ringwald                         if (conn){
6975909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
6985909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
6995909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
7005909f7f2Smatthias.ringwald 
7015909f7f2Smatthias.ringwald                         }
7025909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
7035909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
7045909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
7055909f7f2Smatthias.ringwald                         }
7065909f7f2Smatthias.ringwald                         break;
7075909f7f2Smatthias.ringwald                     }
7085909f7f2Smatthias.ringwald                     if (!conn){
7095909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
7105909f7f2Smatthias.ringwald                     }
7115909f7f2Smatthias.ringwald                     if (!conn){
7125909f7f2Smatthias.ringwald                         // no memory
7135909f7f2Smatthias.ringwald                         break;
7145909f7f2Smatthias.ringwald                     }
7155909f7f2Smatthias.ringwald 
7165909f7f2Smatthias.ringwald                     conn->state = OPEN;
7175909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
7185909f7f2Smatthias.ringwald 
7195909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
7205909f7f2Smatthias.ringwald 
7215909f7f2Smatthias.ringwald                     // restart timer
7225909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
7235909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
7245909f7f2Smatthias.ringwald 
7255909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
7265909f7f2Smatthias.ringwald 
7275909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
7285909f7f2Smatthias.ringwald                     break;
7295909f7f2Smatthias.ringwald 
73065a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
73165a46ef3S[email protected] 
7325909f7f2Smatthias.ringwald                 default:
7335909f7f2Smatthias.ringwald                     break;
7345909f7f2Smatthias.ringwald             }
7355909f7f2Smatthias.ringwald             break;
7365909f7f2Smatthias.ringwald #endif
7375909f7f2Smatthias.ringwald 
7386772a24cSmatthias.ringwald         default:
7396772a24cSmatthias.ringwald             break;
740fe1ed1b8Smatthias.ringwald     }
741fe1ed1b8Smatthias.ringwald 
7423429f56bSmatthias.ringwald     // handle BT initialization
7433429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
7447301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
7457301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
7467301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
7477301ad89Smatthias.ringwald         // }
7487301ad89Smatthias.ringwald         // handle normal init sequence
7493429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
7503429f56bSmatthias.ringwald             // odd: waiting for event
751f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
7523429f56bSmatthias.ringwald                 hci_stack.substate++;
7533429f56bSmatthias.ringwald             }
7543429f56bSmatthias.ringwald         }
75522909952Smatthias.ringwald     }
75622909952Smatthias.ringwald 
75789db417bSmatthias.ringwald     // help with BT sleep
75889db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
75989db417bSmatthias.ringwald         && hci_stack.substate == 1
76089db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
76189db417bSmatthias.ringwald         hci_stack.substate++;
76289db417bSmatthias.ringwald     }
76389db417bSmatthias.ringwald 
7642718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
76594ab26f8Smatthias.ringwald 
76694ab26f8Smatthias.ringwald 	// execute main loop
76794ab26f8Smatthias.ringwald 	hci_run();
76816833f0aSmatthias.ringwald }
76916833f0aSmatthias.ringwald 
7700a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
77110e830c9Smatthias.ringwald     switch (packet_type) {
77210e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
77310e830c9Smatthias.ringwald             event_handler(packet, size);
77410e830c9Smatthias.ringwald             break;
77510e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
77610e830c9Smatthias.ringwald             acl_handler(packet, size);
77710e830c9Smatthias.ringwald             break;
77810e830c9Smatthias.ringwald         default:
77910e830c9Smatthias.ringwald             break;
78010e830c9Smatthias.ringwald     }
78110e830c9Smatthias.ringwald }
78210e830c9Smatthias.ringwald 
783fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
7842718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
7852718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
78616833f0aSmatthias.ringwald }
78716833f0aSmatthias.ringwald 
7884f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
789475c8125Smatthias.ringwald 
790475c8125Smatthias.ringwald     // reference to use transport layer implementation
79116833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
792475c8125Smatthias.ringwald 
79311e23e5fSmatthias.ringwald     // references to used control implementation
79411e23e5fSmatthias.ringwald     hci_stack.control = control;
79511e23e5fSmatthias.ringwald 
79611e23e5fSmatthias.ringwald     // reference to used config
79711e23e5fSmatthias.ringwald     hci_stack.config = config;
79811e23e5fSmatthias.ringwald 
799fe1ed1b8Smatthias.ringwald     // no connections yet
800fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
8010a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
802c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
803758b46ceSmatthias.ringwald 
804b031bebbSmatthias.ringwald     // no pending cmds
805b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
806b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
807b031bebbSmatthias.ringwald 
80816833f0aSmatthias.ringwald     // higher level handler
8092718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
81016833f0aSmatthias.ringwald 
811404843c1Smatthias.ringwald     // store and open remote device db
812404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
813404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
814404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
815404843c1Smatthias.ringwald     }
81629d53098Smatthias.ringwald 
8178fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
8188fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
8198fcba05dSmatthias.ringwald 
82016833f0aSmatthias.ringwald     // register packet handlers with transport
82110e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
822f5454fc6Smatthias.ringwald 
823f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
824e2386ba1S[email protected] 
825e2386ba1S[email protected]     // class of device
826e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
827a45d6b9fS[email protected] 
828a45d6b9fS[email protected]     // Secure Simple Pairing
829a45d6b9fS[email protected]     hci_stack.ssp_enable = 0;
830a45d6b9fS[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_UNKNOWN;
831a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
832475c8125Smatthias.ringwald }
833475c8125Smatthias.ringwald 
834404843c1Smatthias.ringwald void hci_close(){
835404843c1Smatthias.ringwald     // close remote device db
836404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
837404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
838404843c1Smatthias.ringwald     }
839f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
840f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
841f5454fc6Smatthias.ringwald }
842f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
843404843c1Smatthias.ringwald }
844404843c1Smatthias.ringwald 
8458d213e1aSmatthias.ringwald // State-Module-Driver overview
8468d213e1aSmatthias.ringwald // state                    module  low-level
8478d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
8488d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
8498d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
8508d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
851d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
852d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
853c7e0c5f6Smatthias.ringwald 
85440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
8557301ad89Smatthias.ringwald 
856038bc64cSmatthias.ringwald     // power on
857f9a30166Smatthias.ringwald     int err = 0;
858f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
859f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
860f9a30166Smatthias.ringwald     }
861038bc64cSmatthias.ringwald     if (err){
8627d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
863038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
864038bc64cSmatthias.ringwald         return err;
865038bc64cSmatthias.ringwald     }
866038bc64cSmatthias.ringwald 
867038bc64cSmatthias.ringwald     // open low-level device
868038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
869038bc64cSmatthias.ringwald     if (err){
8707d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
871f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
872f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
873f9a30166Smatthias.ringwald         }
874038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
875038bc64cSmatthias.ringwald         return err;
876038bc64cSmatthias.ringwald     }
8778d213e1aSmatthias.ringwald     return 0;
8788d213e1aSmatthias.ringwald }
879038bc64cSmatthias.ringwald 
88040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
8818d213e1aSmatthias.ringwald 
8827b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
8839418f9c9Smatthias.ringwald 
8848d213e1aSmatthias.ringwald     // close low-level device
8858d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
8868d213e1aSmatthias.ringwald 
8877b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
8889418f9c9Smatthias.ringwald 
8898d213e1aSmatthias.ringwald     // power off
8908d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
8918d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
8928d213e1aSmatthias.ringwald     }
8939418f9c9Smatthias.ringwald 
8947b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
8959418f9c9Smatthias.ringwald 
89672ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
89772ea5239Smatthias.ringwald }
89872ea5239Smatthias.ringwald 
89940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
90072ea5239Smatthias.ringwald 
9017b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
9023144bce4Smatthias.ringwald 
903b429b9b7Smatthias.ringwald #if 0
904b429b9b7Smatthias.ringwald     // don't close serial port during sleep
905b429b9b7Smatthias.ringwald 
90672ea5239Smatthias.ringwald     // close low-level device
90772ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
908b429b9b7Smatthias.ringwald #endif
90972ea5239Smatthias.ringwald 
91072ea5239Smatthias.ringwald     // sleep mode
9113144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
91272ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
91372ea5239Smatthias.ringwald     }
914b429b9b7Smatthias.ringwald 
91572ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
9168d213e1aSmatthias.ringwald }
9178d213e1aSmatthias.ringwald 
91840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
919b429b9b7Smatthias.ringwald 
9207b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
921b429b9b7Smatthias.ringwald 
922b429b9b7Smatthias.ringwald     // wake on
923b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
924b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
925b429b9b7Smatthias.ringwald     }
926b429b9b7Smatthias.ringwald 
927b429b9b7Smatthias.ringwald #if 0
928b429b9b7Smatthias.ringwald     // open low-level device
929b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
930b429b9b7Smatthias.ringwald     if (err){
9317d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
932b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
933b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
934b429b9b7Smatthias.ringwald         }
935b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
936b429b9b7Smatthias.ringwald         return err;
937b429b9b7Smatthias.ringwald     }
938b429b9b7Smatthias.ringwald #endif
939b429b9b7Smatthias.ringwald 
940b429b9b7Smatthias.ringwald     return 0;
941b429b9b7Smatthias.ringwald }
942b429b9b7Smatthias.ringwald 
943b429b9b7Smatthias.ringwald 
9448d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
945d661ed19Smatthias.ringwald 
9467b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
947d661ed19Smatthias.ringwald 
9488d213e1aSmatthias.ringwald     int err = 0;
9498d213e1aSmatthias.ringwald     switch (hci_stack.state){
9508d213e1aSmatthias.ringwald 
9518d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
9528d213e1aSmatthias.ringwald             switch (power_mode){
9538d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9548d213e1aSmatthias.ringwald                     err = hci_power_control_on();
9558d213e1aSmatthias.ringwald                     if (err) return err;
9567301ad89Smatthias.ringwald                     // set up state machine
9577301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
9587301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9597301ad89Smatthias.ringwald                     hci_stack.substate = 0;
9608d213e1aSmatthias.ringwald                     break;
9618d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9628d213e1aSmatthias.ringwald                     // do nothing
9638d213e1aSmatthias.ringwald                     break;
9648d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
965b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
9668d213e1aSmatthias.ringwald                     break;
9678d213e1aSmatthias.ringwald             }
9688d213e1aSmatthias.ringwald             break;
9697301ad89Smatthias.ringwald 
9708d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9718d213e1aSmatthias.ringwald             switch (power_mode){
9728d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9738d213e1aSmatthias.ringwald                     // do nothing
9748d213e1aSmatthias.ringwald                     break;
9758d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9768d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
9778d213e1aSmatthias.ringwald                     hci_power_control_off();
9788d213e1aSmatthias.ringwald                     break;
9798d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
980b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
98172ea5239Smatthias.ringwald                     hci_power_control_sleep();
9828d213e1aSmatthias.ringwald                     break;
9838d213e1aSmatthias.ringwald             }
9848d213e1aSmatthias.ringwald             break;
9857301ad89Smatthias.ringwald 
9868d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
9878d213e1aSmatthias.ringwald             switch (power_mode){
9888d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9898d213e1aSmatthias.ringwald                     // do nothing
9908d213e1aSmatthias.ringwald                     break;
9918d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
992c7e0c5f6Smatthias.ringwald                     // see hci_run
993c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9948d213e1aSmatthias.ringwald                     break;
9958d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
996b546ac54Smatthias.ringwald                     // see hci_run
997b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
99889db417bSmatthias.ringwald                     hci_stack.substate = 0;
9998d213e1aSmatthias.ringwald                     break;
10008d213e1aSmatthias.ringwald             }
10018d213e1aSmatthias.ringwald             break;
10027301ad89Smatthias.ringwald 
10038d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
10048d213e1aSmatthias.ringwald             switch (power_mode){
10058d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10068d213e1aSmatthias.ringwald                     // set up state machine
10078d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10088d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
10098d213e1aSmatthias.ringwald                     break;
10108d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10118d213e1aSmatthias.ringwald                     // do nothing
10128d213e1aSmatthias.ringwald                     break;
10138d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1014b546ac54Smatthias.ringwald                     // see hci_run
1015b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
101689db417bSmatthias.ringwald                     hci_stack.substate = 0;
10178d213e1aSmatthias.ringwald                     break;
10188d213e1aSmatthias.ringwald             }
10198d213e1aSmatthias.ringwald             break;
10208d213e1aSmatthias.ringwald 
10218d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
10228d213e1aSmatthias.ringwald             switch (power_mode){
10238d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
102428171530Smatthias.ringwald 
102528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
102628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
102728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10288747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1029da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
103028171530Smatthias.ringwald                         break;
103128171530Smatthias.ringwald                     }
103228171530Smatthias.ringwald #endif
1033b546ac54Smatthias.ringwald                     // set up state machine
1034b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1035b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1036b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10378d213e1aSmatthias.ringwald                     break;
10388d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1039b546ac54Smatthias.ringwald                     // see hci_run
1040b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10418d213e1aSmatthias.ringwald                     break;
10428d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1043b546ac54Smatthias.ringwald                     // do nothing
10448d213e1aSmatthias.ringwald                     break;
10458d213e1aSmatthias.ringwald             }
10468d213e1aSmatthias.ringwald             break;
10478d213e1aSmatthias.ringwald 
10488d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
10498d213e1aSmatthias.ringwald             switch (power_mode){
10508d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
105128171530Smatthias.ringwald 
105228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
105328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
105428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10558747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1056da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1057758b46ceSmatthias.ringwald                         hci_update_scan_enable();
105828171530Smatthias.ringwald                         break;
105928171530Smatthias.ringwald                     }
106028171530Smatthias.ringwald #endif
10613144bce4Smatthias.ringwald                     err = hci_power_control_wake();
10623144bce4Smatthias.ringwald                     if (err) return err;
1063b546ac54Smatthias.ringwald                     // set up state machine
1064b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1065b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1066b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10678d213e1aSmatthias.ringwald                     break;
10688d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10694ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10708d213e1aSmatthias.ringwald                     break;
10718d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1072b546ac54Smatthias.ringwald                     // do nothing
10738d213e1aSmatthias.ringwald                     break;
10748d213e1aSmatthias.ringwald             }
10758d213e1aSmatthias.ringwald             break;
107611e23e5fSmatthias.ringwald     }
107768d92d03Smatthias.ringwald 
1078038bc64cSmatthias.ringwald     // create internal event
1079ee8bf225Smatthias.ringwald 	hci_emit_state();
1080ee8bf225Smatthias.ringwald 
108168d92d03Smatthias.ringwald 	// trigger next/first action
108268d92d03Smatthias.ringwald 	hci_run();
108368d92d03Smatthias.ringwald 
1084475c8125Smatthias.ringwald     return 0;
1085475c8125Smatthias.ringwald }
1086475c8125Smatthias.ringwald 
1087758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1088758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1089758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1090758b46ceSmatthias.ringwald     hci_run();
1091758b46ceSmatthias.ringwald }
1092758b46ceSmatthias.ringwald 
1093381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1094381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1095381fbed8Smatthias.ringwald 
1096381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1097381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1098381fbed8Smatthias.ringwald         return;
1099381fbed8Smatthias.ringwald     }
1100381fbed8Smatthias.ringwald 
1101381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1102758b46ceSmatthias.ringwald     hci_update_scan_enable();
1103758b46ceSmatthias.ringwald }
1104b031bebbSmatthias.ringwald 
1105758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1106758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1107758b46ceSmatthias.ringwald 
1108758b46ceSmatthias.ringwald     // don't emit event
1109758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1110758b46ceSmatthias.ringwald 
1111758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1112758b46ceSmatthias.ringwald     hci_update_scan_enable();
1113381fbed8Smatthias.ringwald }
1114381fbed8Smatthias.ringwald 
111506b35ec0Smatthias.ringwald void hci_run(){
11168a485f27Smatthias.ringwald 
111732ab9390Smatthias.ringwald     hci_connection_t * connection;
111832ab9390Smatthias.ringwald     linked_item_t * it;
111932ab9390Smatthias.ringwald 
1120ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1121ce4c8fabSmatthias.ringwald 
1122b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1123b031bebbSmatthias.ringwald 
1124b031bebbSmatthias.ringwald     // decline incoming connections
1125ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1126ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1127ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1128ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1129dbe1a790S[email protected]         return;
1130ce4c8fabSmatthias.ringwald     }
1131ce4c8fabSmatthias.ringwald 
1132b031bebbSmatthias.ringwald     // send scan enable
113392368cd3S[email protected]     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){
1134b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1135b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1136dbe1a790S[email protected]         return;
1137b031bebbSmatthias.ringwald     }
1138b031bebbSmatthias.ringwald 
113932ab9390Smatthias.ringwald     // send pending HCI commands
114032ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
114132ab9390Smatthias.ringwald 
1142b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
114332ab9390Smatthias.ringwald 
114432ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
11457b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
114632ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
114732ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1148dbe1a790S[email protected]             return;
1149c7e0c5f6Smatthias.ringwald         }
1150c7e0c5f6Smatthias.ringwald 
115132ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
115232ab9390Smatthias.ringwald             link_key_t link_key;
11537b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
115432ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
115532ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
115632ab9390Smatthias.ringwald             } else {
115732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
115832ab9390Smatthias.ringwald             }
115928ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
1160dbe1a790S[email protected]             return;
116132ab9390Smatthias.ringwald         }
11621d6b20aeS[email protected] 
1163dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
1164dbe1a790S[email protected]             hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement);
1165dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
1166dbe1a790S[email protected]             return;
116732ab9390Smatthias.ringwald         }
116832ab9390Smatthias.ringwald 
1169dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1170dbe1a790S[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1171dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
1172dbe1a790S[email protected]             return;
1173dbe1a790S[email protected]         }
1174dbe1a790S[email protected] 
1175dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1176dbe1a790S[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1177dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
1178dbe1a790S[email protected]             return;
1179dbe1a790S[email protected]         }
1180dbe1a790S[email protected]     }
1181c7e0c5f6Smatthias.ringwald 
11823429f56bSmatthias.ringwald     switch (hci_stack.state){
11833429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11847b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
11853429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
11863429f56bSmatthias.ringwald                 // odd: waiting for command completion
118706b35ec0Smatthias.ringwald                 return;
11883429f56bSmatthias.ringwald             }
118990919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1190c7492964Smatthias.ringwald                 case 0: // RESET
119122909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
119224052c2aS[email protected] 
1193c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1194c7492964Smatthias.ringwald                         // skip baud change
1195c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1196c7492964Smatthias.ringwald                     }
11973429f56bSmatthias.ringwald                     break;
1198c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
11997dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
12007dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1201c7492964Smatthias.ringwald                     break;
1202c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1203c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1204c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1205c7492964Smatthias.ringwald                     // break missing here for fall through
1206c7492964Smatthias.ringwald 
1207c7492964Smatthias.ringwald                 case 3:
120824052c2aS[email protected]                     // Custom initialization
1209db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
12107dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1211d62aca9fSmatthias.ringwald                         if (valid_cmd){
12127dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
12137dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1214c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
121590919203Smatthias.ringwald                             break;
121690919203Smatthias.ringwald                         }
1217d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
121890919203Smatthias.ringwald                     }
12192f6c30e1Smatthias.ringwald                     // otherwise continue
1220f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1221f432a6ddSmatthias.ringwald 					break;
1222c7492964Smatthias.ringwald 				case 4:
1223e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1224e2edc0c3Smatthias.ringwald 					break;
1225c7492964Smatthias.ringwald                 case 5:
122665389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
12273429f56bSmatthias.ringwald                     break;
1228c7492964Smatthias.ringwald                 case 6:
1229*d7e0e3a8S[email protected]                     if (hci_le_supported()){
1230*d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1231*d7e0e3a8S[email protected]                     } else {
1232*d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1233*d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1234*d7e0e3a8S[email protected]                     }
1235f5d8d141S[email protected] 
1236f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
123724052c2aS[email protected]                     if (!hci_classic_supported()){
123824052c2aS[email protected]                         if (hci_le_supported()){
1239f5d8d141S[email protected]                             hci_stack.substate = 11 << 1;    // skip all classic command
124024052c2aS[email protected]                         } else {
124124052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
1242*d7e0e3a8S[email protected]                             hci_stack.substate = 13 << 1;    // skip all
124324052c2aS[email protected]                         }
1244f5d8d141S[email protected]                     }
1245f5d8d141S[email protected]                     break;
1246f5d8d141S[email protected]                 case 7:
124724052c2aS[email protected]                     if (hci_ssp_supported()){
1248f5d8d141S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1249f5d8d141S[email protected]                         break;
125024052c2aS[email protected]                     }
125124052c2aS[email protected]                     hci_stack.substate += 2;
125224052c2aS[email protected]                     // break missing here for fall through
125324052c2aS[email protected] 
1254f5d8d141S[email protected]                 case 8:
125565389bfcS[email protected]                     // ca. 15 sec
125665389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1257559e517eS[email protected]                     break;
1258f5d8d141S[email protected]                 case 9:
1259e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1260e2386ba1S[email protected]                     break;
1261f5d8d141S[email protected]                 case 10:
1262e2386ba1S[email protected]                     if (hci_stack.local_name){
1263e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1264e2386ba1S[email protected]                     } else {
12658a485f27Smatthias.ringwald                         char hostname[30];
1266e2386ba1S[email protected] #ifdef EMBEDDED
1267e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1268e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1269e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1270e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1271e2386ba1S[email protected] #else
1272e2386ba1S[email protected]                         // hostname for POSIX systems
12738a485f27Smatthias.ringwald                         gethostname(hostname, 30);
12748a485f27Smatthias.ringwald                         hostname[29] = '\0';
1275e2386ba1S[email protected] #endif
12768a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
12778a485f27Smatthias.ringwald                     }
12783c4d4b90Smatthias.ringwald                     break;
1279e0dbca83S[email protected]                 case 11:
1280a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
128124052c2aS[email protected]                     if (!hci_le_supported()){
128224052c2aS[email protected]                         // SKIP LE init for Classic only configuration
128324052c2aS[email protected]                         hci_stack.substate = 13 << 1;
128424052c2aS[email protected]                     }
1285a45d6b9fS[email protected] 					break;
128624052c2aS[email protected] 
128743aa7007S[email protected] #ifdef HAVE_BLE
128824052c2aS[email protected]                 // LE INIT
1289a45d6b9fS[email protected]                 case 12:
129024052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
129124052c2aS[email protected]                     break;
129224052c2aS[email protected]                 case 13:
129324052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
129424052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
129524052c2aS[email protected]                     break;
129643aa7007S[email protected] #endif
129724052c2aS[email protected] 
129824052c2aS[email protected]                 // DONE
129924052c2aS[email protected]                 case 14:
13003429f56bSmatthias.ringwald                     // done.
13013429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1302b360b6adSmatthias.ringwald                     hci_emit_state();
13033429f56bSmatthias.ringwald                     break;
13043429f56bSmatthias.ringwald                 default:
13053429f56bSmatthias.ringwald                     break;
1306475c8125Smatthias.ringwald             }
13073429f56bSmatthias.ringwald             hci_stack.substate++;
13083429f56bSmatthias.ringwald             break;
1309c7e0c5f6Smatthias.ringwald 
1310c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1311c7e0c5f6Smatthias.ringwald 
13127b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1313c7e0c5f6Smatthias.ringwald             // close all open connections
1314c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1315c7e0c5f6Smatthias.ringwald             if (connection){
131632ab9390Smatthias.ringwald 
1317c7e0c5f6Smatthias.ringwald                 // send disconnect
131832ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
131932ab9390Smatthias.ringwald 
132079bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
13216ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1322c7e0c5f6Smatthias.ringwald 
1323c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1324c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1325c7e0c5f6Smatthias.ringwald                 return;
1326c7e0c5f6Smatthias.ringwald             }
13277b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1328c7e0c5f6Smatthias.ringwald 
132972ea5239Smatthias.ringwald             // switch mode
1330c7e0c5f6Smatthias.ringwald             hci_power_control_off();
13319418f9c9Smatthias.ringwald 
13327b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
133372ea5239Smatthias.ringwald             hci_emit_state();
13347b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
133572ea5239Smatthias.ringwald             break;
1336c7e0c5f6Smatthias.ringwald 
133772ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
133889db417bSmatthias.ringwald             switch(hci_stack.substate) {
133989db417bSmatthias.ringwald                 case 0:
13407b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
134172ea5239Smatthias.ringwald                     // close all open connections
134272ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
134366da7044Smatthias.ringwald 
134428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
134566da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
134666da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
134766da7044Smatthias.ringwald                         connection = NULL;
134866da7044Smatthias.ringwald                     }
134966da7044Smatthias.ringwald #endif
135072ea5239Smatthias.ringwald                     if (connection){
135132ab9390Smatthias.ringwald 
135272ea5239Smatthias.ringwald                         // send disconnect
135332ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
135432ab9390Smatthias.ringwald 
135579bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
13566ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
135772ea5239Smatthias.ringwald 
135872ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
135972ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
136072ea5239Smatthias.ringwald                         return;
136172ea5239Smatthias.ringwald                     }
136272ea5239Smatthias.ringwald 
136392368cd3S[email protected]                     if (hci_classic_supported()){
136489db417bSmatthias.ringwald                         // disable page and inquiry scan
136532ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
136632ab9390Smatthias.ringwald 
136792368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
1368758b46ceSmatthias.ringwald                         hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
136989db417bSmatthias.ringwald 
137089db417bSmatthias.ringwald                         // continue in next sub state
137189db417bSmatthias.ringwald                         hci_stack.substate++;
137289db417bSmatthias.ringwald                         break;
137392368cd3S[email protected]                     }
137492368cd3S[email protected]                     // fall through for ble-only chips
137592368cd3S[email protected] 
137689db417bSmatthias.ringwald                 case 2:
13777b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
137828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
137928171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
138028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
138128171530Smatthias.ringwald                         // SLEEP MODE reached
138228171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
138328171530Smatthias.ringwald                         hci_emit_state();
138428171530Smatthias.ringwald                         break;
138528171530Smatthias.ringwald                     }
138628171530Smatthias.ringwald #endif
138772ea5239Smatthias.ringwald                     // switch mode
138889db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1389c7e0c5f6Smatthias.ringwald                     hci_emit_state();
139028171530Smatthias.ringwald                     break;
139128171530Smatthias.ringwald 
139289db417bSmatthias.ringwald                 default:
139389db417bSmatthias.ringwald                     break;
139489db417bSmatthias.ringwald             }
1395c7e0c5f6Smatthias.ringwald             break;
1396c7e0c5f6Smatthias.ringwald 
13973429f56bSmatthias.ringwald         default:
13983429f56bSmatthias.ringwald             break;
13991f504dbdSmatthias.ringwald     }
14003429f56bSmatthias.ringwald }
140116833f0aSmatthias.ringwald 
140231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1403c8e4258aSmatthias.ringwald     bd_addr_t addr;
1404c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1405c8e4258aSmatthias.ringwald     // house-keeping
1406c8e4258aSmatthias.ringwald 
1407c8e4258aSmatthias.ringwald     // create_connection?
1408c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1409c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1410339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1411c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1412c8e4258aSmatthias.ringwald         if (conn) {
1413c8e4258aSmatthias.ringwald             // if connection exists
1414c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
141517f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
141617f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1417c8e4258aSmatthias.ringwald             }
141817f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1419c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1420c8e4258aSmatthias.ringwald 
142117f1ba2aSmatthias.ringwald         }
1422c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
142317f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
142417f1ba2aSmatthias.ringwald         if (!conn){
142517f1ba2aSmatthias.ringwald             // notify client that alloc failed
142617f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
142717f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
142817f1ba2aSmatthias.ringwald         }
1429c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1430c8e4258aSmatthias.ringwald     }
1431c8e4258aSmatthias.ringwald 
14327fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
14337fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
14347fde4af9Smatthias.ringwald     }
14357fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
14367fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
14377fde4af9Smatthias.ringwald     }
14387fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
14397fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
14407fde4af9Smatthias.ringwald     }
14417fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
14427fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
14437fde4af9Smatthias.ringwald     }
14447fde4af9Smatthias.ringwald 
14458ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
14468ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
14478ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
14488ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
14498ef73945Smatthias.ringwald         }
14508ef73945Smatthias.ringwald     }
1451c8e4258aSmatthias.ringwald 
145231452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1453622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
145431452debSmatthias.ringwald }
14558adf0ddaSmatthias.ringwald 
1456dbe1a790S[email protected] // Configure Secure Simple Pairing
1457dbe1a790S[email protected] 
1458dbe1a790S[email protected] // enable will enable SSP during init
1459dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
1460dbe1a790S[email protected]     hci_stack.ssp_enable = enable;
1461dbe1a790S[email protected] }
1462dbe1a790S[email protected] 
1463dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1464dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
1465dbe1a790S[email protected]     hci_stack.ssp_io_capability = io_capability;
1466dbe1a790S[email protected] }
1467dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
1468dbe1a790S[email protected]     hci_stack.ssp_authentication_requirement = authentication_requirement;
1469dbe1a790S[email protected] }
1470dbe1a790S[email protected] 
1471dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1472dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
1473dbe1a790S[email protected]     hci_stack.ssp_auto_accept = auto_accept;
1474dbe1a790S[email protected] }
1475dbe1a790S[email protected] 
14761cd208adSmatthias.ringwald /**
14771cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
14781cd208adSmatthias.ringwald  */
1479fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
14801cd208adSmatthias.ringwald     va_list argptr;
14811cd208adSmatthias.ringwald     va_start(argptr, cmd);
14827dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
14831cd208adSmatthias.ringwald     va_end(argptr);
14847dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
148593b8dc03Smatthias.ringwald }
1486c8e4258aSmatthias.ringwald 
1487ee091cf1Smatthias.ringwald // Create various non-HCI events.
1488ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1489ee091cf1Smatthias.ringwald 
1490c8e4258aSmatthias.ringwald void hci_emit_state(){
1491e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1492425d1371Smatthias.ringwald     uint8_t event[3];
149380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1494425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1495c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1496425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1497425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1498c8e4258aSmatthias.ringwald }
1499c8e4258aSmatthias.ringwald 
150017f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1501425d1371Smatthias.ringwald     uint8_t event[13];
1502c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1503425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
150417f1ba2aSmatthias.ringwald     event[2] = status;
1505c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1506c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1507c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1508c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1509425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1510425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1511c8e4258aSmatthias.ringwald }
1512c8e4258aSmatthias.ringwald 
15133c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1514425d1371Smatthias.ringwald     uint8_t event[6];
15153c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1516e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
15173c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
15183c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
15193c4d4b90Smatthias.ringwald     event[5] = reason;
1520425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1521425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15223c4d4b90Smatthias.ringwald }
15233c4d4b90Smatthias.ringwald 
1524ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1525e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1526425d1371Smatthias.ringwald     uint8_t event[4];
152780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1528425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1529ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1530425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1531425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1532ee091cf1Smatthias.ringwald }
153343bfb1bdSmatthias.ringwald 
153443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1535e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1536425d1371Smatthias.ringwald     uint8_t event[3];
153780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1538425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
153943bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1540425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1541425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
154243bfb1bdSmatthias.ringwald }
1543038bc64cSmatthias.ringwald 
1544038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1545e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1546425d1371Smatthias.ringwald     uint8_t event[2];
154780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1548425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1549425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1550425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1551038bc64cSmatthias.ringwald }
15521b0e3922Smatthias.ringwald 
155309ba8edeSmatthias.ringwald #ifndef EMBEDDED
15541b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1555e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1556425d1371Smatthias.ringwald     uint8_t event[6];
15571b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1558425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1559425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1560425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1561425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1562425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1563425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15641b0e3922Smatthias.ringwald }
156509ba8edeSmatthias.ringwald #endif
15661b0e3922Smatthias.ringwald 
15672ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1568e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1569425d1371Smatthias.ringwald     uint8_t event[3];
15702ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1571425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
15722ed6235cSmatthias.ringwald     event[2] = enabled;
1573425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1574e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15752ed6235cSmatthias.ringwald }
1576627c2f45Smatthias.ringwald 
1577627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1578e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1579627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1580e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1581f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
15822f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1583f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1584e0abb8e7S[email protected] 
1585e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1586e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1587e0abb8e7S[email protected] 
1588e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1589e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1590627c2f45Smatthias.ringwald }
1591381fbed8Smatthias.ringwald 
1592381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1593e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1594425d1371Smatthias.ringwald     uint8_t event[3];
1595381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1596425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1597381fbed8Smatthias.ringwald     event[2] = enabled;
1598425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1599425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1600381fbed8Smatthias.ringwald }
1601