xref: /btstack/src/hci.c (revision c9af4d3f19c8eb0849d0dbf67148b9c0bf6a3082)
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  */
815061f3afS[email protected] hci_connection_t * hci_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){
1795061f3afS[email protected]     hci_connection_t * conn = hci_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){
2115061f3afS[email protected]     hci_connection_t * connection = hci_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);
2595061f3afS[email protected]     hci_connection_t *connection = hci_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);
2775061f3afS[email protected]     hci_connection_t *conn      = hci_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);
5305061f3afS[email protected]                 conn = hci_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);
6735061f3afS[email protected]                 hci_connection_t * conn = hci_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){
7443429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
7453429f56bSmatthias.ringwald             // odd: waiting for event
746f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
747*c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
748*c9af4d3fS[email protected]                 if (hci_stack.substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
7493429f56bSmatthias.ringwald                     hci_stack.substate++;
7503429f56bSmatthias.ringwald                 }
7513429f56bSmatthias.ringwald             }
75222909952Smatthias.ringwald         }
753*c9af4d3fS[email protected]     }
75422909952Smatthias.ringwald 
75589db417bSmatthias.ringwald     // help with BT sleep
75689db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
75789db417bSmatthias.ringwald         && hci_stack.substate == 1
75889db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
75989db417bSmatthias.ringwald         hci_stack.substate++;
76089db417bSmatthias.ringwald     }
76189db417bSmatthias.ringwald 
7622718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
76394ab26f8Smatthias.ringwald 
76494ab26f8Smatthias.ringwald 	// execute main loop
76594ab26f8Smatthias.ringwald 	hci_run();
76616833f0aSmatthias.ringwald }
76716833f0aSmatthias.ringwald 
7680a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
76910e830c9Smatthias.ringwald     switch (packet_type) {
77010e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
77110e830c9Smatthias.ringwald             event_handler(packet, size);
77210e830c9Smatthias.ringwald             break;
77310e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
77410e830c9Smatthias.ringwald             acl_handler(packet, size);
77510e830c9Smatthias.ringwald             break;
77610e830c9Smatthias.ringwald         default:
77710e830c9Smatthias.ringwald             break;
77810e830c9Smatthias.ringwald     }
77910e830c9Smatthias.ringwald }
78010e830c9Smatthias.ringwald 
781fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
7822718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
7832718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
78416833f0aSmatthias.ringwald }
78516833f0aSmatthias.ringwald 
7864f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
787475c8125Smatthias.ringwald 
788475c8125Smatthias.ringwald     // reference to use transport layer implementation
78916833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
790475c8125Smatthias.ringwald 
79111e23e5fSmatthias.ringwald     // references to used control implementation
79211e23e5fSmatthias.ringwald     hci_stack.control = control;
79311e23e5fSmatthias.ringwald 
79411e23e5fSmatthias.ringwald     // reference to used config
79511e23e5fSmatthias.ringwald     hci_stack.config = config;
79611e23e5fSmatthias.ringwald 
797fe1ed1b8Smatthias.ringwald     // no connections yet
798fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
7990a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
800c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
801758b46ceSmatthias.ringwald 
802b031bebbSmatthias.ringwald     // no pending cmds
803b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
804b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
805b031bebbSmatthias.ringwald 
80616833f0aSmatthias.ringwald     // higher level handler
8072718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
80816833f0aSmatthias.ringwald 
809404843c1Smatthias.ringwald     // store and open remote device db
810404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
811404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
812404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
813404843c1Smatthias.ringwald     }
81429d53098Smatthias.ringwald 
8158fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
8168fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
8178fcba05dSmatthias.ringwald 
81816833f0aSmatthias.ringwald     // register packet handlers with transport
81910e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
820f5454fc6Smatthias.ringwald 
821f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
822e2386ba1S[email protected] 
823e2386ba1S[email protected]     // class of device
824e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
825a45d6b9fS[email protected] 
826ae5d8360S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, auto accept
827ae5d8360S[email protected]     hci_stack.ssp_enable = 1;
828ae5d8360S[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
829a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
830ae5d8360S[email protected]     hci_stack.ssp_auto_accept = 1;
831475c8125Smatthias.ringwald }
832475c8125Smatthias.ringwald 
833404843c1Smatthias.ringwald void hci_close(){
834404843c1Smatthias.ringwald     // close remote device db
835404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
836404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
837404843c1Smatthias.ringwald     }
838f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
839f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
840f5454fc6Smatthias.ringwald }
841f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
842404843c1Smatthias.ringwald }
843404843c1Smatthias.ringwald 
8448d213e1aSmatthias.ringwald // State-Module-Driver overview
8458d213e1aSmatthias.ringwald // state                    module  low-level
8468d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
8478d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
8488d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
8498d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
850d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
851d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
852c7e0c5f6Smatthias.ringwald 
85340d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
8547301ad89Smatthias.ringwald 
855038bc64cSmatthias.ringwald     // power on
856f9a30166Smatthias.ringwald     int err = 0;
857f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
858f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
859f9a30166Smatthias.ringwald     }
860038bc64cSmatthias.ringwald     if (err){
8617d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
862038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
863038bc64cSmatthias.ringwald         return err;
864038bc64cSmatthias.ringwald     }
865038bc64cSmatthias.ringwald 
866038bc64cSmatthias.ringwald     // open low-level device
867038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
868038bc64cSmatthias.ringwald     if (err){
8697d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
870f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
871f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
872f9a30166Smatthias.ringwald         }
873038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
874038bc64cSmatthias.ringwald         return err;
875038bc64cSmatthias.ringwald     }
8768d213e1aSmatthias.ringwald     return 0;
8778d213e1aSmatthias.ringwald }
878038bc64cSmatthias.ringwald 
87940d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
8808d213e1aSmatthias.ringwald 
8817b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
8829418f9c9Smatthias.ringwald 
8838d213e1aSmatthias.ringwald     // close low-level device
8848d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
8858d213e1aSmatthias.ringwald 
8867b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
8879418f9c9Smatthias.ringwald 
8888d213e1aSmatthias.ringwald     // power off
8898d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
8908d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
8918d213e1aSmatthias.ringwald     }
8929418f9c9Smatthias.ringwald 
8937b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
8949418f9c9Smatthias.ringwald 
89572ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
89672ea5239Smatthias.ringwald }
89772ea5239Smatthias.ringwald 
89840d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
89972ea5239Smatthias.ringwald 
9007b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
9013144bce4Smatthias.ringwald 
902b429b9b7Smatthias.ringwald #if 0
903b429b9b7Smatthias.ringwald     // don't close serial port during sleep
904b429b9b7Smatthias.ringwald 
90572ea5239Smatthias.ringwald     // close low-level device
90672ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
907b429b9b7Smatthias.ringwald #endif
90872ea5239Smatthias.ringwald 
90972ea5239Smatthias.ringwald     // sleep mode
9103144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
91172ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
91272ea5239Smatthias.ringwald     }
913b429b9b7Smatthias.ringwald 
91472ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
9158d213e1aSmatthias.ringwald }
9168d213e1aSmatthias.ringwald 
91740d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
918b429b9b7Smatthias.ringwald 
9197b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
920b429b9b7Smatthias.ringwald 
921b429b9b7Smatthias.ringwald     // wake on
922b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
923b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
924b429b9b7Smatthias.ringwald     }
925b429b9b7Smatthias.ringwald 
926b429b9b7Smatthias.ringwald #if 0
927b429b9b7Smatthias.ringwald     // open low-level device
928b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
929b429b9b7Smatthias.ringwald     if (err){
9307d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
931b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
932b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
933b429b9b7Smatthias.ringwald         }
934b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
935b429b9b7Smatthias.ringwald         return err;
936b429b9b7Smatthias.ringwald     }
937b429b9b7Smatthias.ringwald #endif
938b429b9b7Smatthias.ringwald 
939b429b9b7Smatthias.ringwald     return 0;
940b429b9b7Smatthias.ringwald }
941b429b9b7Smatthias.ringwald 
942b429b9b7Smatthias.ringwald 
9438d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
944d661ed19Smatthias.ringwald 
9457b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
946d661ed19Smatthias.ringwald 
9478d213e1aSmatthias.ringwald     int err = 0;
9488d213e1aSmatthias.ringwald     switch (hci_stack.state){
9498d213e1aSmatthias.ringwald 
9508d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
9518d213e1aSmatthias.ringwald             switch (power_mode){
9528d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9538d213e1aSmatthias.ringwald                     err = hci_power_control_on();
9548d213e1aSmatthias.ringwald                     if (err) return err;
9557301ad89Smatthias.ringwald                     // set up state machine
9567301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
9577301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9587301ad89Smatthias.ringwald                     hci_stack.substate = 0;
9598d213e1aSmatthias.ringwald                     break;
9608d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9618d213e1aSmatthias.ringwald                     // do nothing
9628d213e1aSmatthias.ringwald                     break;
9638d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
964b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
9658d213e1aSmatthias.ringwald                     break;
9668d213e1aSmatthias.ringwald             }
9678d213e1aSmatthias.ringwald             break;
9687301ad89Smatthias.ringwald 
9698d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9708d213e1aSmatthias.ringwald             switch (power_mode){
9718d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9728d213e1aSmatthias.ringwald                     // do nothing
9738d213e1aSmatthias.ringwald                     break;
9748d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9758d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
9768d213e1aSmatthias.ringwald                     hci_power_control_off();
9778d213e1aSmatthias.ringwald                     break;
9788d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
979b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
98072ea5239Smatthias.ringwald                     hci_power_control_sleep();
9818d213e1aSmatthias.ringwald                     break;
9828d213e1aSmatthias.ringwald             }
9838d213e1aSmatthias.ringwald             break;
9847301ad89Smatthias.ringwald 
9858d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
9868d213e1aSmatthias.ringwald             switch (power_mode){
9878d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9888d213e1aSmatthias.ringwald                     // do nothing
9898d213e1aSmatthias.ringwald                     break;
9908d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
991c7e0c5f6Smatthias.ringwald                     // see hci_run
992c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9938d213e1aSmatthias.ringwald                     break;
9948d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
995b546ac54Smatthias.ringwald                     // see hci_run
996b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
99789db417bSmatthias.ringwald                     hci_stack.substate = 0;
9988d213e1aSmatthias.ringwald                     break;
9998d213e1aSmatthias.ringwald             }
10008d213e1aSmatthias.ringwald             break;
10017301ad89Smatthias.ringwald 
10028d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
10038d213e1aSmatthias.ringwald             switch (power_mode){
10048d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10058d213e1aSmatthias.ringwald                     // set up state machine
10068d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
10078d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
10088d213e1aSmatthias.ringwald                     break;
10098d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10108d213e1aSmatthias.ringwald                     // do nothing
10118d213e1aSmatthias.ringwald                     break;
10128d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1013b546ac54Smatthias.ringwald                     // see hci_run
1014b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
101589db417bSmatthias.ringwald                     hci_stack.substate = 0;
10168d213e1aSmatthias.ringwald                     break;
10178d213e1aSmatthias.ringwald             }
10188d213e1aSmatthias.ringwald             break;
10198d213e1aSmatthias.ringwald 
10208d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
10218d213e1aSmatthias.ringwald             switch (power_mode){
10228d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
102328171530Smatthias.ringwald 
102428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
102528171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
102628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10278747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1028da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
102928171530Smatthias.ringwald                         break;
103028171530Smatthias.ringwald                     }
103128171530Smatthias.ringwald #endif
1032b546ac54Smatthias.ringwald                     // set up state machine
1033b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1034b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1035b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10368d213e1aSmatthias.ringwald                     break;
10378d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1038b546ac54Smatthias.ringwald                     // see hci_run
1039b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10408d213e1aSmatthias.ringwald                     break;
10418d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1042b546ac54Smatthias.ringwald                     // do nothing
10438d213e1aSmatthias.ringwald                     break;
10448d213e1aSmatthias.ringwald             }
10458d213e1aSmatthias.ringwald             break;
10468d213e1aSmatthias.ringwald 
10478d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
10488d213e1aSmatthias.ringwald             switch (power_mode){
10498d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
105028171530Smatthias.ringwald 
105128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
105228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
105328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10548747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1055da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1056758b46ceSmatthias.ringwald                         hci_update_scan_enable();
105728171530Smatthias.ringwald                         break;
105828171530Smatthias.ringwald                     }
105928171530Smatthias.ringwald #endif
10603144bce4Smatthias.ringwald                     err = hci_power_control_wake();
10613144bce4Smatthias.ringwald                     if (err) return err;
1062b546ac54Smatthias.ringwald                     // set up state machine
1063b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1064b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1065b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10668d213e1aSmatthias.ringwald                     break;
10678d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10684ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10698d213e1aSmatthias.ringwald                     break;
10708d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1071b546ac54Smatthias.ringwald                     // do nothing
10728d213e1aSmatthias.ringwald                     break;
10738d213e1aSmatthias.ringwald             }
10748d213e1aSmatthias.ringwald             break;
107511e23e5fSmatthias.ringwald     }
107668d92d03Smatthias.ringwald 
1077038bc64cSmatthias.ringwald     // create internal event
1078ee8bf225Smatthias.ringwald 	hci_emit_state();
1079ee8bf225Smatthias.ringwald 
108068d92d03Smatthias.ringwald 	// trigger next/first action
108168d92d03Smatthias.ringwald 	hci_run();
108268d92d03Smatthias.ringwald 
1083475c8125Smatthias.ringwald     return 0;
1084475c8125Smatthias.ringwald }
1085475c8125Smatthias.ringwald 
1086758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1087758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1088758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1089758b46ceSmatthias.ringwald     hci_run();
1090758b46ceSmatthias.ringwald }
1091758b46ceSmatthias.ringwald 
1092381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1093381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1094381fbed8Smatthias.ringwald 
1095381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1096381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1097381fbed8Smatthias.ringwald         return;
1098381fbed8Smatthias.ringwald     }
1099381fbed8Smatthias.ringwald 
1100381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1101758b46ceSmatthias.ringwald     hci_update_scan_enable();
1102758b46ceSmatthias.ringwald }
1103b031bebbSmatthias.ringwald 
1104758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1105758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1106758b46ceSmatthias.ringwald 
1107758b46ceSmatthias.ringwald     // don't emit event
1108758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1109758b46ceSmatthias.ringwald 
1110758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1111758b46ceSmatthias.ringwald     hci_update_scan_enable();
1112381fbed8Smatthias.ringwald }
1113381fbed8Smatthias.ringwald 
11145061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
11155061f3afS[email protected]     return &hci_stack.local_bd_addr;
11165061f3afS[email protected] }
11175061f3afS[email protected] 
111806b35ec0Smatthias.ringwald void hci_run(){
11198a485f27Smatthias.ringwald 
112032ab9390Smatthias.ringwald     hci_connection_t * connection;
112132ab9390Smatthias.ringwald     linked_item_t * it;
112232ab9390Smatthias.ringwald 
1123ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1124ce4c8fabSmatthias.ringwald 
1125b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1126b031bebbSmatthias.ringwald 
1127b031bebbSmatthias.ringwald     // decline incoming connections
1128ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1129ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1130ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1131ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1132dbe1a790S[email protected]         return;
1133ce4c8fabSmatthias.ringwald     }
1134ce4c8fabSmatthias.ringwald 
1135b031bebbSmatthias.ringwald     // send scan enable
113692368cd3S[email protected]     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){
1137b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1138b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1139dbe1a790S[email protected]         return;
1140b031bebbSmatthias.ringwald     }
1141b031bebbSmatthias.ringwald 
114232ab9390Smatthias.ringwald     // send pending HCI commands
114332ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
114432ab9390Smatthias.ringwald 
1145b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
114632ab9390Smatthias.ringwald 
114732ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
11487b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
114932ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
115032ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1151dbe1a790S[email protected]             return;
1152c7e0c5f6Smatthias.ringwald         }
1153c7e0c5f6Smatthias.ringwald 
115432ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
115532ab9390Smatthias.ringwald             link_key_t link_key;
11567b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
115732ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
115832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
115932ab9390Smatthias.ringwald             } else {
116032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
116132ab9390Smatthias.ringwald             }
116228ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
1163dbe1a790S[email protected]             return;
116432ab9390Smatthias.ringwald         }
11651d6b20aeS[email protected] 
1166dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
1167dbe1a790S[email protected]             hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement);
1168dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
1169dbe1a790S[email protected]             return;
117032ab9390Smatthias.ringwald         }
117132ab9390Smatthias.ringwald 
1172dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1173dbe1a790S[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1174dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
1175dbe1a790S[email protected]             return;
1176dbe1a790S[email protected]         }
1177dbe1a790S[email protected] 
1178dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1179dbe1a790S[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1180dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
1181dbe1a790S[email protected]             return;
1182dbe1a790S[email protected]         }
1183dbe1a790S[email protected]     }
1184c7e0c5f6Smatthias.ringwald 
11853429f56bSmatthias.ringwald     switch (hci_stack.state){
11863429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11877b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
11883429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
11893429f56bSmatthias.ringwald                 // odd: waiting for command completion
119006b35ec0Smatthias.ringwald                 return;
11913429f56bSmatthias.ringwald             }
119290919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1193c7492964Smatthias.ringwald                 case 0: // RESET
119422909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
119524052c2aS[email protected] 
1196c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1197c7492964Smatthias.ringwald                         // skip baud change
1198c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1199c7492964Smatthias.ringwald                     }
12003429f56bSmatthias.ringwald                     break;
1201c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
12027dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
12037dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1204c7492964Smatthias.ringwald                     break;
1205c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1206c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1207c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1208c7492964Smatthias.ringwald                     // break missing here for fall through
1209c7492964Smatthias.ringwald 
1210c7492964Smatthias.ringwald                 case 3:
121124052c2aS[email protected]                     // Custom initialization
1212db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
12137dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1214d62aca9fSmatthias.ringwald                         if (valid_cmd){
12157dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
12167dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1217c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
121890919203Smatthias.ringwald                             break;
121990919203Smatthias.ringwald                         }
1220d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
122190919203Smatthias.ringwald                     }
12222f6c30e1Smatthias.ringwald                     // otherwise continue
1223f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1224f432a6ddSmatthias.ringwald 					break;
1225c7492964Smatthias.ringwald 				case 4:
1226e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1227e2edc0c3Smatthias.ringwald 					break;
1228c7492964Smatthias.ringwald                 case 5:
122965389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
12303429f56bSmatthias.ringwald                     break;
1231c7492964Smatthias.ringwald                 case 6:
1232d7e0e3a8S[email protected]                     if (hci_le_supported()){
1233d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1234d7e0e3a8S[email protected]                     } else {
1235d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1236d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1237d7e0e3a8S[email protected]                     }
1238f5d8d141S[email protected] 
1239f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
124024052c2aS[email protected]                     if (!hci_classic_supported()){
124124052c2aS[email protected]                         if (hci_le_supported()){
1242f5d8d141S[email protected]                             hci_stack.substate = 11 << 1;    // skip all classic command
124324052c2aS[email protected]                         } else {
124424052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
1245d7e0e3a8S[email protected]                             hci_stack.substate = 13 << 1;    // skip all
124624052c2aS[email protected]                         }
1247f5d8d141S[email protected]                     }
1248f5d8d141S[email protected]                     break;
1249f5d8d141S[email protected]                 case 7:
125024052c2aS[email protected]                     if (hci_ssp_supported()){
1251f5d8d141S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1252f5d8d141S[email protected]                         break;
125324052c2aS[email protected]                     }
125424052c2aS[email protected]                     hci_stack.substate += 2;
125524052c2aS[email protected]                     // break missing here for fall through
125624052c2aS[email protected] 
1257f5d8d141S[email protected]                 case 8:
125865389bfcS[email protected]                     // ca. 15 sec
125965389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1260559e517eS[email protected]                     break;
1261f5d8d141S[email protected]                 case 9:
1262e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1263e2386ba1S[email protected]                     break;
1264f5d8d141S[email protected]                 case 10:
1265e2386ba1S[email protected]                     if (hci_stack.local_name){
1266e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1267e2386ba1S[email protected]                     } else {
12688a485f27Smatthias.ringwald                         char hostname[30];
1269e2386ba1S[email protected] #ifdef EMBEDDED
1270e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1271e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1272e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1273e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1274e2386ba1S[email protected] #else
1275e2386ba1S[email protected]                         // hostname for POSIX systems
12768a485f27Smatthias.ringwald                         gethostname(hostname, 30);
12778a485f27Smatthias.ringwald                         hostname[29] = '\0';
1278e2386ba1S[email protected] #endif
12798a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
12808a485f27Smatthias.ringwald                     }
12813c4d4b90Smatthias.ringwald                     break;
1282e0dbca83S[email protected]                 case 11:
1283a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
128424052c2aS[email protected]                     if (!hci_le_supported()){
128524052c2aS[email protected]                         // SKIP LE init for Classic only configuration
128624052c2aS[email protected]                         hci_stack.substate = 13 << 1;
128724052c2aS[email protected]                     }
1288a45d6b9fS[email protected] 					break;
128924052c2aS[email protected] 
129043aa7007S[email protected] #ifdef HAVE_BLE
129124052c2aS[email protected]                 // LE INIT
1292a45d6b9fS[email protected]                 case 12:
129324052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
129424052c2aS[email protected]                     break;
129524052c2aS[email protected]                 case 13:
129624052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
129724052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
129824052c2aS[email protected]                     break;
129943aa7007S[email protected] #endif
130024052c2aS[email protected] 
130124052c2aS[email protected]                 // DONE
130224052c2aS[email protected]                 case 14:
13033429f56bSmatthias.ringwald                     // done.
13043429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1305b360b6adSmatthias.ringwald                     hci_emit_state();
13063429f56bSmatthias.ringwald                     break;
13073429f56bSmatthias.ringwald                 default:
13083429f56bSmatthias.ringwald                     break;
1309475c8125Smatthias.ringwald             }
13103429f56bSmatthias.ringwald             hci_stack.substate++;
13113429f56bSmatthias.ringwald             break;
1312c7e0c5f6Smatthias.ringwald 
1313c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1314c7e0c5f6Smatthias.ringwald 
13157b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1316c7e0c5f6Smatthias.ringwald             // close all open connections
1317c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1318c7e0c5f6Smatthias.ringwald             if (connection){
131932ab9390Smatthias.ringwald 
1320c7e0c5f6Smatthias.ringwald                 // send disconnect
132132ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
132232ab9390Smatthias.ringwald 
132379bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
13246ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1325c7e0c5f6Smatthias.ringwald 
1326c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1327c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1328c7e0c5f6Smatthias.ringwald                 return;
1329c7e0c5f6Smatthias.ringwald             }
13307b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1331c7e0c5f6Smatthias.ringwald 
133272ea5239Smatthias.ringwald             // switch mode
1333c7e0c5f6Smatthias.ringwald             hci_power_control_off();
13349418f9c9Smatthias.ringwald 
13357b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
133672ea5239Smatthias.ringwald             hci_emit_state();
13377b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
133872ea5239Smatthias.ringwald             break;
1339c7e0c5f6Smatthias.ringwald 
134072ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
134189db417bSmatthias.ringwald             switch(hci_stack.substate) {
134289db417bSmatthias.ringwald                 case 0:
13437b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
134472ea5239Smatthias.ringwald                     // close all open connections
134572ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
134666da7044Smatthias.ringwald 
134728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
134866da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
134966da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
135066da7044Smatthias.ringwald                         connection = NULL;
135166da7044Smatthias.ringwald                     }
135266da7044Smatthias.ringwald #endif
135372ea5239Smatthias.ringwald                     if (connection){
135432ab9390Smatthias.ringwald 
135572ea5239Smatthias.ringwald                         // send disconnect
135632ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
135732ab9390Smatthias.ringwald 
135879bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
13596ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
136072ea5239Smatthias.ringwald 
136172ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
136272ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
136372ea5239Smatthias.ringwald                         return;
136472ea5239Smatthias.ringwald                     }
136572ea5239Smatthias.ringwald 
136692368cd3S[email protected]                     if (hci_classic_supported()){
136789db417bSmatthias.ringwald                         // disable page and inquiry scan
136832ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
136932ab9390Smatthias.ringwald 
137092368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
1371758b46ceSmatthias.ringwald                         hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
137289db417bSmatthias.ringwald 
137389db417bSmatthias.ringwald                         // continue in next sub state
137489db417bSmatthias.ringwald                         hci_stack.substate++;
137589db417bSmatthias.ringwald                         break;
137692368cd3S[email protected]                     }
137792368cd3S[email protected]                     // fall through for ble-only chips
137892368cd3S[email protected] 
137989db417bSmatthias.ringwald                 case 2:
13807b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
138128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
138228171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
138328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
138428171530Smatthias.ringwald                         // SLEEP MODE reached
138528171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
138628171530Smatthias.ringwald                         hci_emit_state();
138728171530Smatthias.ringwald                         break;
138828171530Smatthias.ringwald                     }
138928171530Smatthias.ringwald #endif
139072ea5239Smatthias.ringwald                     // switch mode
139189db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1392c7e0c5f6Smatthias.ringwald                     hci_emit_state();
139328171530Smatthias.ringwald                     break;
139428171530Smatthias.ringwald 
139589db417bSmatthias.ringwald                 default:
139689db417bSmatthias.ringwald                     break;
139789db417bSmatthias.ringwald             }
1398c7e0c5f6Smatthias.ringwald             break;
1399c7e0c5f6Smatthias.ringwald 
14003429f56bSmatthias.ringwald         default:
14013429f56bSmatthias.ringwald             break;
14021f504dbdSmatthias.ringwald     }
14033429f56bSmatthias.ringwald }
140416833f0aSmatthias.ringwald 
140531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1406c8e4258aSmatthias.ringwald     bd_addr_t addr;
1407c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1408c8e4258aSmatthias.ringwald     // house-keeping
1409c8e4258aSmatthias.ringwald 
1410c8e4258aSmatthias.ringwald     // create_connection?
1411c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1412c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1413339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1414c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1415c8e4258aSmatthias.ringwald         if (conn) {
1416c8e4258aSmatthias.ringwald             // if connection exists
1417c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
141817f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
141917f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1420c8e4258aSmatthias.ringwald             }
142117f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1422c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1423c8e4258aSmatthias.ringwald 
142417f1ba2aSmatthias.ringwald         }
1425c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
142617f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
142717f1ba2aSmatthias.ringwald         if (!conn){
142817f1ba2aSmatthias.ringwald             // notify client that alloc failed
142917f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
143017f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
143117f1ba2aSmatthias.ringwald         }
1432c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1433c8e4258aSmatthias.ringwald     }
1434c8e4258aSmatthias.ringwald 
14357fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
14367fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
14377fde4af9Smatthias.ringwald     }
14387fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
14397fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
14407fde4af9Smatthias.ringwald     }
14417fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
14427fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
14437fde4af9Smatthias.ringwald     }
14447fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
14457fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
14467fde4af9Smatthias.ringwald     }
14477fde4af9Smatthias.ringwald 
14488ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
14498ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
14508ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
14518ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
14528ef73945Smatthias.ringwald         }
14538ef73945Smatthias.ringwald     }
1454c8e4258aSmatthias.ringwald 
145531452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1456622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
145731452debSmatthias.ringwald }
14588adf0ddaSmatthias.ringwald 
1459dbe1a790S[email protected] // Configure Secure Simple Pairing
1460dbe1a790S[email protected] 
1461dbe1a790S[email protected] // enable will enable SSP during init
1462dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
1463dbe1a790S[email protected]     hci_stack.ssp_enable = enable;
1464dbe1a790S[email protected] }
1465dbe1a790S[email protected] 
1466dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1467dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
1468dbe1a790S[email protected]     hci_stack.ssp_io_capability = io_capability;
1469dbe1a790S[email protected] }
1470dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
1471dbe1a790S[email protected]     hci_stack.ssp_authentication_requirement = authentication_requirement;
1472dbe1a790S[email protected] }
1473dbe1a790S[email protected] 
1474dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1475dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
1476dbe1a790S[email protected]     hci_stack.ssp_auto_accept = auto_accept;
1477dbe1a790S[email protected] }
1478dbe1a790S[email protected] 
14791cd208adSmatthias.ringwald /**
14801cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
14811cd208adSmatthias.ringwald  */
1482fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
14831cd208adSmatthias.ringwald     va_list argptr;
14841cd208adSmatthias.ringwald     va_start(argptr, cmd);
14857dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
14861cd208adSmatthias.ringwald     va_end(argptr);
14877dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
148893b8dc03Smatthias.ringwald }
1489c8e4258aSmatthias.ringwald 
1490ee091cf1Smatthias.ringwald // Create various non-HCI events.
1491ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1492ee091cf1Smatthias.ringwald 
1493c8e4258aSmatthias.ringwald void hci_emit_state(){
1494e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1495425d1371Smatthias.ringwald     uint8_t event[3];
149680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1497425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1498c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1499425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1500425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1501c8e4258aSmatthias.ringwald }
1502c8e4258aSmatthias.ringwald 
150317f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1504425d1371Smatthias.ringwald     uint8_t event[13];
1505c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1506425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
150717f1ba2aSmatthias.ringwald     event[2] = status;
1508c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1509c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1510c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1511c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1512425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1513425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1514c8e4258aSmatthias.ringwald }
1515c8e4258aSmatthias.ringwald 
15163c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1517425d1371Smatthias.ringwald     uint8_t event[6];
15183c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1519e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
15203c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
15213c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
15223c4d4b90Smatthias.ringwald     event[5] = reason;
1523425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1524425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15253c4d4b90Smatthias.ringwald }
15263c4d4b90Smatthias.ringwald 
1527ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1528e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1529425d1371Smatthias.ringwald     uint8_t event[4];
153080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1531425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1532ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1533425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1534425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1535ee091cf1Smatthias.ringwald }
153643bfb1bdSmatthias.ringwald 
153743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1538e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1539425d1371Smatthias.ringwald     uint8_t event[3];
154080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1541425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
154243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1543425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1544425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
154543bfb1bdSmatthias.ringwald }
1546038bc64cSmatthias.ringwald 
1547038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1548e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1549425d1371Smatthias.ringwald     uint8_t event[2];
155080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1551425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1552425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1553425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1554038bc64cSmatthias.ringwald }
15551b0e3922Smatthias.ringwald 
155609ba8edeSmatthias.ringwald #ifndef EMBEDDED
15571b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1558e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1559425d1371Smatthias.ringwald     uint8_t event[6];
15601b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1561425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1562425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1563425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1564425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1565425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1566425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15671b0e3922Smatthias.ringwald }
156809ba8edeSmatthias.ringwald #endif
15691b0e3922Smatthias.ringwald 
15702ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1571e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1572425d1371Smatthias.ringwald     uint8_t event[3];
15732ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1574425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
15752ed6235cSmatthias.ringwald     event[2] = enabled;
1576425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1577e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15782ed6235cSmatthias.ringwald }
1579627c2f45Smatthias.ringwald 
1580627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1581e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1582627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1583e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1584f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
15852f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1586f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1587e0abb8e7S[email protected] 
1588e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1589e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1590e0abb8e7S[email protected] 
1591e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1592e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1593627c2f45Smatthias.ringwald }
1594381fbed8Smatthias.ringwald 
1595381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1596e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1597425d1371Smatthias.ringwald     uint8_t event[3];
1598381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1599425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1600381fbed8Smatthias.ringwald     event[2] = enabled;
1601425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1602425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1603381fbed8Smatthias.ringwald }
1604