xref: /btstack/src/hci.c (revision 1d6b20ae69e08539c0afd1c6ac325f9db54ad1ea)
11f504dbdSmatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
381f504dbdSmatthias.ringwald  *  hci.c
391f504dbdSmatthias.ringwald  *
401f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
411f504dbdSmatthias.ringwald  *
421f504dbdSmatthias.ringwald  */
431f504dbdSmatthias.ringwald 
44c8901d41Smatthias.ringwald #include "config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
477f2435e6Smatthias.ringwald 
4893b8dc03Smatthias.ringwald #include <stdarg.h>
4993b8dc03Smatthias.ringwald #include <string.h>
5056fe0872Smatthias.ringwald #include <stdio.h>
517f2435e6Smatthias.ringwald 
52549e6ebeSmatthias.ringwald #ifndef EMBEDDED
53549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5409ba8edeSmatthias.ringwald #include <btstack/version.h>
55549e6ebeSmatthias.ringwald #endif
56549e6ebeSmatthias.ringwald 
57a3b02b71Smatthias.ringwald #include "btstack_memory.h"
587f2435e6Smatthias.ringwald #include "debug.h"
59d8905019Smatthias.ringwald #include "hci_dump.h"
6093b8dc03Smatthias.ringwald 
61ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
621b0e3922Smatthias.ringwald 
63169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
64ee091cf1Smatthias.ringwald 
65a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
66da5275c5S[email protected] 
6728171530Smatthias.ringwald #ifdef USE_BLUETOOL
6828171530Smatthias.ringwald #include "bt_control_iphone.h"
6928171530Smatthias.ringwald #endif
7028171530Smatthias.ringwald 
71758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
72758b46ceSmatthias.ringwald 
7306b35ec0Smatthias.ringwald // the STACK is here
7416833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
7516833f0aSmatthias.ringwald 
7697addcc5Smatthias.ringwald /**
77ee091cf1Smatthias.ringwald  * get connection for a given handle
78ee091cf1Smatthias.ringwald  *
79ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
80ee091cf1Smatthias.ringwald  */
81645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
82ee091cf1Smatthias.ringwald     linked_item_t *it;
83ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
84ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
85ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
86ee091cf1Smatthias.ringwald         }
87ee091cf1Smatthias.ringwald     }
88ee091cf1Smatthias.ringwald     return NULL;
89ee091cf1Smatthias.ringwald }
90ee091cf1Smatthias.ringwald 
912b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
9228ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
93c785ef68Smatthias.ringwald #ifdef HAVE_TIME
94ee091cf1Smatthias.ringwald     struct timeval tv;
95ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
96c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
97ee091cf1Smatthias.ringwald         // connections might be timed out
98ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
99ee091cf1Smatthias.ringwald     }
1002b12a0b9Smatthias.ringwald #endif
101e5780900Smatthias.ringwald #ifdef HAVE_TICK
102c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
103c785ef68Smatthias.ringwald         // connections might be timed out
104c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
105c785ef68Smatthias.ringwald     }
106c785ef68Smatthias.ringwald #endif
107c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
108c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
109c785ef68Smatthias.ringwald }
110ee091cf1Smatthias.ringwald 
111ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
112c7492964Smatthias.ringwald #ifdef HAVE_TIME
113ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
114c7492964Smatthias.ringwald #endif
115e5780900Smatthias.ringwald #ifdef HAVE_TICK
116c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
117c785ef68Smatthias.ringwald #endif
118ee091cf1Smatthias.ringwald }
119ee091cf1Smatthias.ringwald 
120ee091cf1Smatthias.ringwald /**
121c8e4258aSmatthias.ringwald  * create connection for given address
122c8e4258aSmatthias.ringwald  *
12317f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
124c8e4258aSmatthias.ringwald  */
125c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
12628ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
127c8e4258aSmatthias.ringwald     if (!conn) return NULL;
128c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
129c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1307d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
131ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
132ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
133ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
134d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1357856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
13656cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
137c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
138c8e4258aSmatthias.ringwald     return conn;
139c8e4258aSmatthias.ringwald }
140c8e4258aSmatthias.ringwald 
141c8e4258aSmatthias.ringwald /**
14206b35ec0Smatthias.ringwald  * get connection for given address
14397addcc5Smatthias.ringwald  *
14497addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
14597addcc5Smatthias.ringwald  */
146fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
14706b35ec0Smatthias.ringwald     linked_item_t *it;
14806b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
14906b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
15006b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
15106b35ec0Smatthias.ringwald         }
15206b35ec0Smatthias.ringwald     }
15306b35ec0Smatthias.ringwald     return NULL;
15406b35ec0Smatthias.ringwald }
15506b35ec0Smatthias.ringwald 
15628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
15728ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
15828ca2b46S[email protected] }
15928ca2b46S[email protected] 
16028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
16128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
16228ca2b46S[email protected] }
16328ca2b46S[email protected] 
16428ca2b46S[email protected] 
16543bfb1bdSmatthias.ringwald /**
16680ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1677fde4af9Smatthias.ringwald  */
1687fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1697fde4af9Smatthias.ringwald     bd_addr_t addr;
1707fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1717fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1727fde4af9Smatthias.ringwald     if (conn) {
17328ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
17480ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1757fde4af9Smatthias.ringwald     }
1767fde4af9Smatthias.ringwald }
1777fde4af9Smatthias.ringwald 
17880ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
17980ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
18080ca58a0Smatthias.ringwald     if (!conn) return 0;
18180ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
18280ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
18380ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
18480ca58a0Smatthias.ringwald     return 1;
18580ca58a0Smatthias.ringwald }
18680ca58a0Smatthias.ringwald 
187c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
188c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
189c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
190c12e46e7Smatthias.ringwald     }
191c12e46e7Smatthias.ringwald }
192c12e46e7Smatthias.ringwald 
1937fde4af9Smatthias.ringwald 
1947fde4af9Smatthias.ringwald /**
19543bfb1bdSmatthias.ringwald  * count connections
19643bfb1bdSmatthias.ringwald  */
19740d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
19856c253c9Smatthias.ringwald     int count = 0;
19943bfb1bdSmatthias.ringwald     linked_item_t *it;
20043bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
20143bfb1bdSmatthias.ringwald     return count;
20243bfb1bdSmatthias.ringwald }
203c8e4258aSmatthias.ringwald 
20497addcc5Smatthias.ringwald /**
205ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
20616833f0aSmatthias.ringwald  */
2072718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
20816833f0aSmatthias.ringwald }
20916833f0aSmatthias.ringwald 
210998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
211998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
212998906cdSmatthias.ringwald     if (!connection) {
2137d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
214998906cdSmatthias.ringwald         return 0;
215998906cdSmatthias.ringwald     }
216998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
217998906cdSmatthias.ringwald }
218998906cdSmatthias.ringwald 
219998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
220998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
221998906cdSmatthias.ringwald     linked_item_t *it;
222998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
223998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
224998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2257d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
226998906cdSmatthias.ringwald             return 0;
227998906cdSmatthias.ringwald         }
228998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
229998906cdSmatthias.ringwald     }
230998906cdSmatthias.ringwald     return free_slots;
231998906cdSmatthias.ringwald }
232998906cdSmatthias.ringwald 
233c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2342b12a0b9Smatthias.ringwald 
2352b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2362b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2372b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2382b12a0b9Smatthias.ringwald             return 0;
2392b12a0b9Smatthias.ringwald         }
2402b12a0b9Smatthias.ringwald     }
2412b12a0b9Smatthias.ringwald 
2422b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
243c24735b1Smatthias.ringwald     switch (packet_type) {
244c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
245c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
246c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
247de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
248c24735b1Smatthias.ringwald         default:
249c24735b1Smatthias.ringwald             return 0;
250c24735b1Smatthias.ringwald     }
251c24735b1Smatthias.ringwald }
252c24735b1Smatthias.ringwald 
253ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2547856c818Smatthias.ringwald 
2556218e6f1Smatthias.ringwald     // check for free places on BT module
2565932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2576218e6f1Smatthias.ringwald 
2587856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2597856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
26056cf178bSmatthias.ringwald     if (!connection) return 0;
26156cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
26256cf178bSmatthias.ringwald 
26356cf178bSmatthias.ringwald     // count packet
26456cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2657b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2667856c818Smatthias.ringwald 
26700d8e42eSmatthias.ringwald     // send packet
26800d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2696218e6f1Smatthias.ringwald 
27000d8e42eSmatthias.ringwald     return err;
271ee091cf1Smatthias.ringwald }
272ee091cf1Smatthias.ringwald 
27316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2747856c818Smatthias.ringwald 
2757856c818Smatthias.ringwald     // get info
2767856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2777856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2787856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2797856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2807856c818Smatthias.ringwald 
2817856c818Smatthias.ringwald     // ignore non-registered handle
2827856c818Smatthias.ringwald     if (!conn){
2837d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2847856c818Smatthias.ringwald         return;
2857856c818Smatthias.ringwald     }
2867856c818Smatthias.ringwald 
2877856c818Smatthias.ringwald     // update idle timestamp
2887856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2897856c818Smatthias.ringwald 
2907856c818Smatthias.ringwald     // handle different packet types
2917856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2927856c818Smatthias.ringwald 
2937856c818Smatthias.ringwald         case 0x01: // continuation fragment
2947856c818Smatthias.ringwald 
2957856c818Smatthias.ringwald             // sanity check
2967856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2977d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2987856c818Smatthias.ringwald                 return;
2997856c818Smatthias.ringwald             }
3007856c818Smatthias.ringwald 
3017856c818Smatthias.ringwald             // append fragment payload (header already stored)
3027856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3037856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3047856c818Smatthias.ringwald 
3057d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
306decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3077856c818Smatthias.ringwald 
3087856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3097856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3107856c818Smatthias.ringwald 
3112718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3127856c818Smatthias.ringwald                 // reset recombination buffer
3137856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3147856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3157856c818Smatthias.ringwald             }
3167856c818Smatthias.ringwald             break;
3177856c818Smatthias.ringwald 
3187856c818Smatthias.ringwald         case 0x02: { // first fragment
3197856c818Smatthias.ringwald 
3207856c818Smatthias.ringwald             // sanity check
3217856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3227d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3237856c818Smatthias.ringwald                 return;
3247856c818Smatthias.ringwald             }
3257856c818Smatthias.ringwald 
3267856c818Smatthias.ringwald             // peek into L2CAP packet!
3277856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3287856c818Smatthias.ringwald 
3297d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
330decc01a8Smatthias.ringwald 
3317856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3327856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3337856c818Smatthias.ringwald 
3347856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3352718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3367856c818Smatthias.ringwald 
3377856c818Smatthias.ringwald             } else {
3387856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3397856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3407856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3417856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
342decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3437856c818Smatthias.ringwald             }
3447856c818Smatthias.ringwald             break;
3457856c818Smatthias.ringwald 
3467856c818Smatthias.ringwald         }
3477856c818Smatthias.ringwald         default:
3487d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3497856c818Smatthias.ringwald             return;
3507856c818Smatthias.ringwald     }
35194ab26f8Smatthias.ringwald 
35294ab26f8Smatthias.ringwald     // execute main loop
35394ab26f8Smatthias.ringwald     hci_run();
35416833f0aSmatthias.ringwald }
35522909952Smatthias.ringwald 
35667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
357339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3583c4d4b90Smatthias.ringwald 
3593c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3603c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3613c4d4b90Smatthias.ringwald 
362c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
363c785ef68Smatthias.ringwald 
364c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
365a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3663c4d4b90Smatthias.ringwald 
3673c4d4b90Smatthias.ringwald     // now it's gone
368c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
369c7e0c5f6Smatthias.ringwald }
370c7e0c5f6Smatthias.ringwald 
3710c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3728f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3738f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3748f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3758f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3768f8108aaSmatthias.ringwald };
3778f8108aaSmatthias.ringwald 
3788f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){
3798f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
3808f8108aaSmatthias.ringwald     int i;
3818f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
3828f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
3838f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
3848f8108aaSmatthias.ringwald             packet_types |= 1 << i;
3858f8108aaSmatthias.ringwald         }
3868f8108aaSmatthias.ringwald     }
3878f8108aaSmatthias.ringwald     // flip bits for "may not be used"
3888f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
3898f8108aaSmatthias.ringwald     return packet_types;
3908f8108aaSmatthias.ringwald }
3918f8108aaSmatthias.ringwald 
3928f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
3938f8108aaSmatthias.ringwald     return hci_stack.packet_types;
3948f8108aaSmatthias.ringwald }
3958f8108aaSmatthias.ringwald 
3967dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
3977dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
3987dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
3997dc17943Smatthias.ringwald }
4007dc17943Smatthias.ringwald 
4017dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
4027dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
4037dc17943Smatthias.ringwald }
4047dc17943Smatthias.ringwald 
40574ec757aSmatthias.ringwald // avoid huge local variables
406223aafc1Smatthias.ringwald #ifndef EMBEDDED
40774ec757aSmatthias.ringwald static device_name_t device_name;
408223aafc1Smatthias.ringwald #endif
40916833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
4101281a47eSmatthias.ringwald     bd_addr_t addr;
4112d00edd4Smatthias.ringwald     uint8_t link_type;
412fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
4131f7b95a1Smatthias.ringwald     hci_connection_t * conn;
41456cf178bSmatthias.ringwald     int i;
41522909952Smatthias.ringwald 
4165909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4175909f7f2Smatthias.ringwald 
4186772a24cSmatthias.ringwald     switch (packet[0]) {
41922909952Smatthias.ringwald 
4206772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4217ec5eeaaSmatthias.ringwald             // get num cmd packets
4227b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4237ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4247ec5eeaaSmatthias.ringwald 
425e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
426e2edc0c3Smatthias.ringwald                 // from offset 5
427e2edc0c3Smatthias.ringwald                 // status
4281d279b20Smatthias.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"
429e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
43056cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
431e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
43256cf178bSmatthias.ringwald                 // ignore: total num SCO packets
43356cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
434a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
4358fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
4368fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
4378f8108aaSmatthias.ringwald                     }
438a7a04bd9Smatthias.ringwald                     // determine usable ACL packet types
43928c93ceeSmatthias.ringwald                     hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length);
4408f8108aaSmatthias.ringwald 
44197fdcd42Smatthias.ringwald                     log_info("hci_read_buffer_size: used size %u, count %u, packet types %04x\n",
4428f8108aaSmatthias.ringwald                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types);
443e2edc0c3Smatthias.ringwald                 }
44456cf178bSmatthias.ringwald             }
445188981d2Smatthias.ringwald             // Dump local address
446188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
447e2386ba1S[email protected]                 bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
448188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
449e2386ba1S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr));
450188981d2Smatthias.ringwald             }
451381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
452381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
453381fbed8Smatthias.ringwald             }
454559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
455559e517eS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], 8);
456559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
457559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
458559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
459559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
460559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
461559e517eS[email protected]             }
46256cf178bSmatthias.ringwald             break;
46356cf178bSmatthias.ringwald 
4647ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4657ec5eeaaSmatthias.ringwald             // get num cmd packets
4667b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4677ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4687ec5eeaaSmatthias.ringwald             break;
4697ec5eeaaSmatthias.ringwald 
47056cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
47156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
47256cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
47356cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
47456cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
47556cf178bSmatthias.ringwald                 if (!conn){
4767d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
47756cf178bSmatthias.ringwald                     continue;
47856cf178bSmatthias.ringwald                 }
47956cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4807b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
48156cf178bSmatthias.ringwald             }
4826772a24cSmatthias.ringwald             break;
4836772a24cSmatthias.ringwald 
4841f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
48537eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
48637eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4872d00edd4Smatthias.ringwald             link_type = packet[11];
488339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
48937eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4901f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4911f7b95a1Smatthias.ringwald                 if (!conn) {
4921f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4931f7b95a1Smatthias.ringwald                 }
494ce4c8fabSmatthias.ringwald                 if (!conn) {
495ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
496ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
497ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
498ce4c8fabSmatthias.ringwald                     break;
499ce4c8fabSmatthias.ringwald                 }
50032ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
50132ab9390Smatthias.ringwald                 hci_run();
50237eaa4cfSmatthias.ringwald             } else {
503ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
504ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
505ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
50637eaa4cfSmatthias.ringwald             }
5071f7b95a1Smatthias.ringwald             break;
5081f7b95a1Smatthias.ringwald 
5096772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
510fe1ed1b8Smatthias.ringwald             // Connection management
511fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
512339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
5131f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
514fe1ed1b8Smatthias.ringwald             if (conn) {
515b448a0e7Smatthias.ringwald                 if (!packet[2]){
516c8e4258aSmatthias.ringwald                     conn->state = OPEN;
517fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
518ee091cf1Smatthias.ringwald 
519c785ef68Smatthias.ringwald                     // restart timer
520c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
521ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
522c785ef68Smatthias.ringwald 
523339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
52443bfb1bdSmatthias.ringwald 
52543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
526b448a0e7Smatthias.ringwald                 } else {
527b448a0e7Smatthias.ringwald                     // connection failed, remove entry
528b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
529a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
530c12e46e7Smatthias.ringwald 
531c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
532c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
533c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
534c12e46e7Smatthias.ringwald                     }
535fe1ed1b8Smatthias.ringwald                 }
536fe1ed1b8Smatthias.ringwald             }
5376772a24cSmatthias.ringwald             break;
538fe1ed1b8Smatthias.ringwald 
5397fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5407b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5417fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
54274ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
54332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
54464472d52Smatthias.ringwald             hci_run();
545d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
54629d53098Smatthias.ringwald             return;
5477fde4af9Smatthias.ringwald 
5487fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5497fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
55074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
55129d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
552287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
55329d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5547fde4af9Smatthias.ringwald             break;
5557fde4af9Smatthias.ringwald 
5567fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5577fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
558d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
559d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
560d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
561d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
5627fde4af9Smatthias.ringwald             break;
5637fde4af9Smatthias.ringwald 
564*1d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
565*1d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
566*1d6b20aeS[email protected]             if (hci_stack.ssp_io_capability == SSP_IO_CAPABILITY_UNKNOWN) break;
567*1d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SENT_IO_CAPABILITIES_REPLY);
568*1d6b20aeS[email protected]             break;
569*1d6b20aeS[email protected] 
570223aafc1Smatthias.ringwald #ifndef EMBEDDED
57174ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
57274ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
57374ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
57474ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5755a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5765a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5775a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5785a06394aSmatthias.ringwald                     packet[9+i] = 0;
5795a06394aSmatthias.ringwald                     break;
580cdc9101dSmatthias.ringwald                 }
5815a06394aSmatthias.ringwald             }
582d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
58374ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
58474ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
58574ec757aSmatthias.ringwald             break;
58674ec757aSmatthias.ringwald 
58774ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
58874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
58974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
59074ec757aSmatthias.ringwald             // first send inq result packet
59174ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
59274ec757aSmatthias.ringwald             // then send cached remote names
59374ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
59474ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
59574ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
59674ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
59774ec757aSmatthias.ringwald                 }
59874ec757aSmatthias.ringwald             }
59974ec757aSmatthias.ringwald             return;
600223aafc1Smatthias.ringwald #endif
60174ec757aSmatthias.ringwald 
6026772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
603fe1ed1b8Smatthias.ringwald             if (!packet[2]){
604fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
605fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
606fe1ed1b8Smatthias.ringwald                 if (conn) {
607c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
608fe1ed1b8Smatthias.ringwald                 }
609fe1ed1b8Smatthias.ringwald             }
6106772a24cSmatthias.ringwald             break;
6116772a24cSmatthias.ringwald 
612c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
613c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
614c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
615c68bdf90Smatthias.ringwald             }
616c68bdf90Smatthias.ringwald             break;
617c68bdf90Smatthias.ringwald 
6185909f7f2Smatthias.ringwald #ifdef HAVE_BLE
6195909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
6205909f7f2Smatthias.ringwald             switch (packet[2]) {
6215909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
6225909f7f2Smatthias.ringwald                     // Connection management
6235909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
6245909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
6255909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
6265909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
6275909f7f2Smatthias.ringwald                     if (packet[3]){
6285909f7f2Smatthias.ringwald                         if (conn){
6295909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
6305909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
6315909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
6325909f7f2Smatthias.ringwald 
6335909f7f2Smatthias.ringwald                         }
6345909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
6355909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
6365909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
6375909f7f2Smatthias.ringwald                         }
6385909f7f2Smatthias.ringwald                         break;
6395909f7f2Smatthias.ringwald                     }
6405909f7f2Smatthias.ringwald                     if (!conn){
6415909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
6425909f7f2Smatthias.ringwald                     }
6435909f7f2Smatthias.ringwald                     if (!conn){
6445909f7f2Smatthias.ringwald                         // no memory
6455909f7f2Smatthias.ringwald                         break;
6465909f7f2Smatthias.ringwald                     }
6475909f7f2Smatthias.ringwald 
6485909f7f2Smatthias.ringwald                     conn->state = OPEN;
6495909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
6505909f7f2Smatthias.ringwald 
6515909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
6525909f7f2Smatthias.ringwald 
6535909f7f2Smatthias.ringwald                     // restart timer
6545909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
6555909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
6565909f7f2Smatthias.ringwald 
6575909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
6585909f7f2Smatthias.ringwald 
6595909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
6605909f7f2Smatthias.ringwald                     break;
6615909f7f2Smatthias.ringwald 
6625909f7f2Smatthias.ringwald                 default:
6635909f7f2Smatthias.ringwald                     break;
6645909f7f2Smatthias.ringwald             }
6655909f7f2Smatthias.ringwald             break;
6665909f7f2Smatthias.ringwald #endif
6675909f7f2Smatthias.ringwald 
6686772a24cSmatthias.ringwald         default:
6696772a24cSmatthias.ringwald             break;
670fe1ed1b8Smatthias.ringwald     }
671fe1ed1b8Smatthias.ringwald 
6723429f56bSmatthias.ringwald     // handle BT initialization
6733429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
6747301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
6757301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
6767301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
6777301ad89Smatthias.ringwald         // }
6787301ad89Smatthias.ringwald         // handle normal init sequence
6793429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
6803429f56bSmatthias.ringwald             // odd: waiting for event
681f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
6823429f56bSmatthias.ringwald                 hci_stack.substate++;
6833429f56bSmatthias.ringwald             }
6843429f56bSmatthias.ringwald         }
68522909952Smatthias.ringwald     }
68622909952Smatthias.ringwald 
68789db417bSmatthias.ringwald     // help with BT sleep
68889db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
68989db417bSmatthias.ringwald         && hci_stack.substate == 1
69089db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
69189db417bSmatthias.ringwald         hci_stack.substate++;
69289db417bSmatthias.ringwald     }
69389db417bSmatthias.ringwald 
6942718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
69594ab26f8Smatthias.ringwald 
69694ab26f8Smatthias.ringwald 	// execute main loop
69794ab26f8Smatthias.ringwald 	hci_run();
69816833f0aSmatthias.ringwald }
69916833f0aSmatthias.ringwald 
7000a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
70110e830c9Smatthias.ringwald     switch (packet_type) {
70210e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
70310e830c9Smatthias.ringwald             event_handler(packet, size);
70410e830c9Smatthias.ringwald             break;
70510e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
70610e830c9Smatthias.ringwald             acl_handler(packet, size);
70710e830c9Smatthias.ringwald             break;
70810e830c9Smatthias.ringwald         default:
70910e830c9Smatthias.ringwald             break;
71010e830c9Smatthias.ringwald     }
71110e830c9Smatthias.ringwald }
71210e830c9Smatthias.ringwald 
713fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
7142718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
7152718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
71616833f0aSmatthias.ringwald }
71716833f0aSmatthias.ringwald 
7184f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
719475c8125Smatthias.ringwald 
720475c8125Smatthias.ringwald     // reference to use transport layer implementation
72116833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
722475c8125Smatthias.ringwald 
72311e23e5fSmatthias.ringwald     // references to used control implementation
72411e23e5fSmatthias.ringwald     hci_stack.control = control;
72511e23e5fSmatthias.ringwald 
72611e23e5fSmatthias.ringwald     // reference to used config
72711e23e5fSmatthias.ringwald     hci_stack.config = config;
72811e23e5fSmatthias.ringwald 
729fe1ed1b8Smatthias.ringwald     // no connections yet
730fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
7310a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
732c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
733758b46ceSmatthias.ringwald 
734b031bebbSmatthias.ringwald     // no pending cmds
735b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
736b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
737b031bebbSmatthias.ringwald 
73816833f0aSmatthias.ringwald     // higher level handler
7392718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
74016833f0aSmatthias.ringwald 
741404843c1Smatthias.ringwald     // store and open remote device db
742404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
743404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
744404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
745404843c1Smatthias.ringwald     }
74629d53098Smatthias.ringwald 
7478fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
7488fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7498fcba05dSmatthias.ringwald 
75016833f0aSmatthias.ringwald     // register packet handlers with transport
75110e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
752f5454fc6Smatthias.ringwald 
753f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
754e2386ba1S[email protected] 
755e2386ba1S[email protected]     // class of device
756e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
757a45d6b9fS[email protected] 
758a45d6b9fS[email protected]     // Secure Simple Pairing
759a45d6b9fS[email protected]     hci_stack.ssp_enable = 0;
760a45d6b9fS[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_UNKNOWN;
761a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
762475c8125Smatthias.ringwald }
763475c8125Smatthias.ringwald 
764404843c1Smatthias.ringwald void hci_close(){
765404843c1Smatthias.ringwald     // close remote device db
766404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
767404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
768404843c1Smatthias.ringwald     }
769f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
770f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
771f5454fc6Smatthias.ringwald }
772f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
773404843c1Smatthias.ringwald }
774404843c1Smatthias.ringwald 
7758d213e1aSmatthias.ringwald // State-Module-Driver overview
7768d213e1aSmatthias.ringwald // state                    module  low-level
7778d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
7788d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
7798d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
7808d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
781d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
782d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
783c7e0c5f6Smatthias.ringwald 
78440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
7857301ad89Smatthias.ringwald 
786038bc64cSmatthias.ringwald     // power on
787f9a30166Smatthias.ringwald     int err = 0;
788f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
789f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
790f9a30166Smatthias.ringwald     }
791038bc64cSmatthias.ringwald     if (err){
7927d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
793038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
794038bc64cSmatthias.ringwald         return err;
795038bc64cSmatthias.ringwald     }
796038bc64cSmatthias.ringwald 
797038bc64cSmatthias.ringwald     // open low-level device
798038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
799038bc64cSmatthias.ringwald     if (err){
8007d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
801f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
802f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
803f9a30166Smatthias.ringwald         }
804038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
805038bc64cSmatthias.ringwald         return err;
806038bc64cSmatthias.ringwald     }
8078d213e1aSmatthias.ringwald     return 0;
8088d213e1aSmatthias.ringwald }
809038bc64cSmatthias.ringwald 
81040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
8118d213e1aSmatthias.ringwald 
8127b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
8139418f9c9Smatthias.ringwald 
8148d213e1aSmatthias.ringwald     // close low-level device
8158d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
8168d213e1aSmatthias.ringwald 
8177b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
8189418f9c9Smatthias.ringwald 
8198d213e1aSmatthias.ringwald     // power off
8208d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
8218d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
8228d213e1aSmatthias.ringwald     }
8239418f9c9Smatthias.ringwald 
8247b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
8259418f9c9Smatthias.ringwald 
82672ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
82772ea5239Smatthias.ringwald }
82872ea5239Smatthias.ringwald 
82940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
83072ea5239Smatthias.ringwald 
8317b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
8323144bce4Smatthias.ringwald 
833b429b9b7Smatthias.ringwald #if 0
834b429b9b7Smatthias.ringwald     // don't close serial port during sleep
835b429b9b7Smatthias.ringwald 
83672ea5239Smatthias.ringwald     // close low-level device
83772ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
838b429b9b7Smatthias.ringwald #endif
83972ea5239Smatthias.ringwald 
84072ea5239Smatthias.ringwald     // sleep mode
8413144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
84272ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
84372ea5239Smatthias.ringwald     }
844b429b9b7Smatthias.ringwald 
84572ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
8468d213e1aSmatthias.ringwald }
8478d213e1aSmatthias.ringwald 
84840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
849b429b9b7Smatthias.ringwald 
8507b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
851b429b9b7Smatthias.ringwald 
852b429b9b7Smatthias.ringwald     // wake on
853b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
854b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
855b429b9b7Smatthias.ringwald     }
856b429b9b7Smatthias.ringwald 
857b429b9b7Smatthias.ringwald #if 0
858b429b9b7Smatthias.ringwald     // open low-level device
859b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
860b429b9b7Smatthias.ringwald     if (err){
8617d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
862b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
863b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
864b429b9b7Smatthias.ringwald         }
865b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
866b429b9b7Smatthias.ringwald         return err;
867b429b9b7Smatthias.ringwald     }
868b429b9b7Smatthias.ringwald #endif
869b429b9b7Smatthias.ringwald 
870b429b9b7Smatthias.ringwald     return 0;
871b429b9b7Smatthias.ringwald }
872b429b9b7Smatthias.ringwald 
873b429b9b7Smatthias.ringwald 
8748d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
875d661ed19Smatthias.ringwald 
8767b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
877d661ed19Smatthias.ringwald 
8788d213e1aSmatthias.ringwald     int err = 0;
8798d213e1aSmatthias.ringwald     switch (hci_stack.state){
8808d213e1aSmatthias.ringwald 
8818d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
8828d213e1aSmatthias.ringwald             switch (power_mode){
8838d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8848d213e1aSmatthias.ringwald                     err = hci_power_control_on();
8858d213e1aSmatthias.ringwald                     if (err) return err;
8867301ad89Smatthias.ringwald                     // set up state machine
8877301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
8887301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8897301ad89Smatthias.ringwald                     hci_stack.substate = 0;
8908d213e1aSmatthias.ringwald                     break;
8918d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8928d213e1aSmatthias.ringwald                     // do nothing
8938d213e1aSmatthias.ringwald                     break;
8948d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
895b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
8968d213e1aSmatthias.ringwald                     break;
8978d213e1aSmatthias.ringwald             }
8988d213e1aSmatthias.ringwald             break;
8997301ad89Smatthias.ringwald 
9008d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9018d213e1aSmatthias.ringwald             switch (power_mode){
9028d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9038d213e1aSmatthias.ringwald                     // do nothing
9048d213e1aSmatthias.ringwald                     break;
9058d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9068d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
9078d213e1aSmatthias.ringwald                     hci_power_control_off();
9088d213e1aSmatthias.ringwald                     break;
9098d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
910b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
91172ea5239Smatthias.ringwald                     hci_power_control_sleep();
9128d213e1aSmatthias.ringwald                     break;
9138d213e1aSmatthias.ringwald             }
9148d213e1aSmatthias.ringwald             break;
9157301ad89Smatthias.ringwald 
9168d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
9178d213e1aSmatthias.ringwald             switch (power_mode){
9188d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9198d213e1aSmatthias.ringwald                     // do nothing
9208d213e1aSmatthias.ringwald                     break;
9218d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
922c7e0c5f6Smatthias.ringwald                     // see hci_run
923c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9248d213e1aSmatthias.ringwald                     break;
9258d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
926b546ac54Smatthias.ringwald                     // see hci_run
927b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
92889db417bSmatthias.ringwald                     hci_stack.substate = 0;
9298d213e1aSmatthias.ringwald                     break;
9308d213e1aSmatthias.ringwald             }
9318d213e1aSmatthias.ringwald             break;
9327301ad89Smatthias.ringwald 
9338d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
9348d213e1aSmatthias.ringwald             switch (power_mode){
9358d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9368d213e1aSmatthias.ringwald                     // set up state machine
9378d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9388d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
9398d213e1aSmatthias.ringwald                     break;
9408d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9418d213e1aSmatthias.ringwald                     // do nothing
9428d213e1aSmatthias.ringwald                     break;
9438d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
944b546ac54Smatthias.ringwald                     // see hci_run
945b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
94689db417bSmatthias.ringwald                     hci_stack.substate = 0;
9478d213e1aSmatthias.ringwald                     break;
9488d213e1aSmatthias.ringwald             }
9498d213e1aSmatthias.ringwald             break;
9508d213e1aSmatthias.ringwald 
9518d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
9528d213e1aSmatthias.ringwald             switch (power_mode){
9538d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
95428171530Smatthias.ringwald 
95528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
95628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
95728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9588747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
959da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
96028171530Smatthias.ringwald                         break;
96128171530Smatthias.ringwald                     }
96228171530Smatthias.ringwald #endif
963b546ac54Smatthias.ringwald                     // set up state machine
964b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
965b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
966b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9678d213e1aSmatthias.ringwald                     break;
9688d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
969b546ac54Smatthias.ringwald                     // see hci_run
970b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9718d213e1aSmatthias.ringwald                     break;
9728d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
973b546ac54Smatthias.ringwald                     // do nothing
9748d213e1aSmatthias.ringwald                     break;
9758d213e1aSmatthias.ringwald             }
9768d213e1aSmatthias.ringwald             break;
9778d213e1aSmatthias.ringwald 
9788d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
9798d213e1aSmatthias.ringwald             switch (power_mode){
9808d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
98128171530Smatthias.ringwald 
98228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
98328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
98428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9858747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
986da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
987758b46ceSmatthias.ringwald                         hci_update_scan_enable();
98828171530Smatthias.ringwald                         break;
98928171530Smatthias.ringwald                     }
99028171530Smatthias.ringwald #endif
9913144bce4Smatthias.ringwald                     err = hci_power_control_wake();
9923144bce4Smatthias.ringwald                     if (err) return err;
993b546ac54Smatthias.ringwald                     // set up state machine
994b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
995b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
996b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9978d213e1aSmatthias.ringwald                     break;
9988d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9994ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10008d213e1aSmatthias.ringwald                     break;
10018d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1002b546ac54Smatthias.ringwald                     // do nothing
10038d213e1aSmatthias.ringwald                     break;
10048d213e1aSmatthias.ringwald             }
10058d213e1aSmatthias.ringwald             break;
100611e23e5fSmatthias.ringwald     }
100768d92d03Smatthias.ringwald 
1008038bc64cSmatthias.ringwald     // create internal event
1009ee8bf225Smatthias.ringwald 	hci_emit_state();
1010ee8bf225Smatthias.ringwald 
101168d92d03Smatthias.ringwald 	// trigger next/first action
101268d92d03Smatthias.ringwald 	hci_run();
101368d92d03Smatthias.ringwald 
1014475c8125Smatthias.ringwald     return 0;
1015475c8125Smatthias.ringwald }
1016475c8125Smatthias.ringwald 
1017758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1018758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1019758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1020758b46ceSmatthias.ringwald     hci_run();
1021758b46ceSmatthias.ringwald }
1022758b46ceSmatthias.ringwald 
1023381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1024381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1025381fbed8Smatthias.ringwald 
1026381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1027381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1028381fbed8Smatthias.ringwald         return;
1029381fbed8Smatthias.ringwald     }
1030381fbed8Smatthias.ringwald 
1031381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1032758b46ceSmatthias.ringwald     hci_update_scan_enable();
1033758b46ceSmatthias.ringwald }
1034b031bebbSmatthias.ringwald 
1035758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1036758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1037758b46ceSmatthias.ringwald 
1038758b46ceSmatthias.ringwald     // don't emit event
1039758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1040758b46ceSmatthias.ringwald 
1041758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1042758b46ceSmatthias.ringwald     hci_update_scan_enable();
1043381fbed8Smatthias.ringwald }
1044381fbed8Smatthias.ringwald 
104506b35ec0Smatthias.ringwald void hci_run(){
10468a485f27Smatthias.ringwald 
104732ab9390Smatthias.ringwald     hci_connection_t * connection;
104832ab9390Smatthias.ringwald     linked_item_t * it;
104932ab9390Smatthias.ringwald 
1050ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1051ce4c8fabSmatthias.ringwald 
1052b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1053b031bebbSmatthias.ringwald 
1054b031bebbSmatthias.ringwald     // decline incoming connections
1055ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1056ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1057ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1058ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1059ce4c8fabSmatthias.ringwald     }
1060ce4c8fabSmatthias.ringwald 
1061b031bebbSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1062b031bebbSmatthias.ringwald 
1063b031bebbSmatthias.ringwald     // send scan enable
106496774428Smatthias.ringwald     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff){
1065b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1066b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1067b031bebbSmatthias.ringwald     }
1068b031bebbSmatthias.ringwald 
106932ab9390Smatthias.ringwald     // send pending HCI commands
107032ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
107132ab9390Smatthias.ringwald 
1072b031bebbSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
107332ab9390Smatthias.ringwald 
1074b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
107532ab9390Smatthias.ringwald 
107632ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
10777b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
107832ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
107932ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1080c7e0c5f6Smatthias.ringwald         }
1081c7e0c5f6Smatthias.ringwald 
1082de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
108332ab9390Smatthias.ringwald 
108432ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
108532ab9390Smatthias.ringwald             link_key_t link_key;
10867b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
108732ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
108832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
108932ab9390Smatthias.ringwald             } else {
109032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
109132ab9390Smatthias.ringwald             }
109228ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
109332ab9390Smatthias.ringwald         }
1094*1d6b20aeS[email protected] 
1095*1d6b20aeS[email protected]         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1096*1d6b20aeS[email protected] 
1097*1d6b20aeS[email protected]         if (connection->authentication_flags & SENT_IO_CAPABILITIES_REPLY){
1098*1d6b20aeS[email protected]             hci_send_cmd(&hci_io_capability_request_reply, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement);
1099*1d6b20aeS[email protected]             connectionClearAuthenticationFlags(connection, SENT_IO_CAPABILITIES_REPLY);
1100*1d6b20aeS[email protected]         }
110132ab9390Smatthias.ringwald     }
110232ab9390Smatthias.ringwald 
1103de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1104c7e0c5f6Smatthias.ringwald 
11053429f56bSmatthias.ringwald     switch (hci_stack.state){
11063429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11077b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
11083429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
11093429f56bSmatthias.ringwald                 // odd: waiting for command completion
111006b35ec0Smatthias.ringwald                 return;
11113429f56bSmatthias.ringwald             }
111290919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1113c7492964Smatthias.ringwald                 case 0: // RESET
111422909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
1115c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1116c7492964Smatthias.ringwald                         // skip baud change
1117c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1118c7492964Smatthias.ringwald                     }
11193429f56bSmatthias.ringwald                     break;
1120c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
11217dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
11227dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1123c7492964Smatthias.ringwald                     break;
1124c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1125c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1126c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1127c7492964Smatthias.ringwald                     // break missing here for fall through
1128c7492964Smatthias.ringwald 
1129c7492964Smatthias.ringwald                 case 3:
113090919203Smatthias.ringwald                     // custom initialization
1131db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
11327dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1133d62aca9fSmatthias.ringwald                         if (valid_cmd){
11347dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
11357dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1136c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
113790919203Smatthias.ringwald                             break;
113890919203Smatthias.ringwald                         }
1139d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
114090919203Smatthias.ringwald                     }
11412f6c30e1Smatthias.ringwald                     // otherwise continue
1142f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1143f432a6ddSmatthias.ringwald 					break;
1144c7492964Smatthias.ringwald 				case 4:
1145e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1146e2edc0c3Smatthias.ringwald 					break;
1147c7492964Smatthias.ringwald                 case 5:
11483429f56bSmatthias.ringwald                     // ca. 15 sec
11493429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
11503429f56bSmatthias.ringwald                     break;
1151c7492964Smatthias.ringwald                 case 6:
1152559e517eS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
1153559e517eS[email protected]                     break;
1154e2386ba1S[email protected]                 case 7:
1155e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1156e2386ba1S[email protected]                     break;
1157559e517eS[email protected]                 case 8:
1158e2386ba1S[email protected]                     if (hci_stack.local_name){
1159e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1160e2386ba1S[email protected]                     } else {
11618a485f27Smatthias.ringwald                         char hostname[30];
1162e2386ba1S[email protected] #ifdef EMBEDDED
1163e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1164e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1165e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1166e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1167e2386ba1S[email protected] #else
1168e2386ba1S[email protected]                         // hostname for POSIX systems
11698a485f27Smatthias.ringwald                         gethostname(hostname, 30);
11708a485f27Smatthias.ringwald                         hostname[29] = '\0';
1171e2386ba1S[email protected] #endif
11728a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
11738a485f27Smatthias.ringwald                     }
11743c4d4b90Smatthias.ringwald                     break;
1175e2386ba1S[email protected]                 case 9:
1176e0dbca83S[email protected]                     hci_send_cmd(&hci_set_event_mask,0xffffffff, 0xFFFFFFFF); ///0x1DFFFFFF
1177e2386ba1S[email protected]                     break;
1178559e517eS[email protected]                 case 10:
1179a45d6b9fS[email protected]                     hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1180e0dbca83S[email protected]                     break;
1181e0dbca83S[email protected]                 case 11:
1182a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
1183a45d6b9fS[email protected] 					break;
1184a45d6b9fS[email protected]                 case 12:
11853429f56bSmatthias.ringwald                     // done.
11863429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1187b360b6adSmatthias.ringwald                     hci_emit_state();
11883429f56bSmatthias.ringwald                     break;
11893429f56bSmatthias.ringwald                 default:
11903429f56bSmatthias.ringwald                     break;
1191475c8125Smatthias.ringwald             }
11923429f56bSmatthias.ringwald             hci_stack.substate++;
11933429f56bSmatthias.ringwald             break;
1194c7e0c5f6Smatthias.ringwald 
1195c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1196c7e0c5f6Smatthias.ringwald 
11977b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1198c7e0c5f6Smatthias.ringwald             // close all open connections
1199c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1200c7e0c5f6Smatthias.ringwald             if (connection){
120132ab9390Smatthias.ringwald 
1202c7e0c5f6Smatthias.ringwald                 // send disconnect
120332ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
120432ab9390Smatthias.ringwald 
120579bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
12066ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1207c7e0c5f6Smatthias.ringwald 
1208c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1209c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1210c7e0c5f6Smatthias.ringwald                 return;
1211c7e0c5f6Smatthias.ringwald             }
12127b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1213c7e0c5f6Smatthias.ringwald 
121472ea5239Smatthias.ringwald             // switch mode
1215c7e0c5f6Smatthias.ringwald             hci_power_control_off();
12169418f9c9Smatthias.ringwald 
12177b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
121872ea5239Smatthias.ringwald             hci_emit_state();
12197b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
122072ea5239Smatthias.ringwald             break;
1221c7e0c5f6Smatthias.ringwald 
122272ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
122389db417bSmatthias.ringwald             switch(hci_stack.substate) {
122489db417bSmatthias.ringwald                 case 0:
12257b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
122672ea5239Smatthias.ringwald                     // close all open connections
122772ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
122866da7044Smatthias.ringwald 
122928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
123066da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
123166da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
123266da7044Smatthias.ringwald                         connection = NULL;
123366da7044Smatthias.ringwald                     }
123466da7044Smatthias.ringwald #endif
123572ea5239Smatthias.ringwald                     if (connection){
123632ab9390Smatthias.ringwald 
123772ea5239Smatthias.ringwald                         // send disconnect
123832ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
123932ab9390Smatthias.ringwald 
124079bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
12416ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
124272ea5239Smatthias.ringwald 
124372ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
124472ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
124572ea5239Smatthias.ringwald                         return;
124672ea5239Smatthias.ringwald                     }
124772ea5239Smatthias.ringwald 
124889db417bSmatthias.ringwald                     // disable page and inquiry scan
124932ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
125032ab9390Smatthias.ringwald 
1251758b46ceSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq cans\n");
1252758b46ceSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
125389db417bSmatthias.ringwald 
125489db417bSmatthias.ringwald                     // continue in next sub state
125589db417bSmatthias.ringwald                     hci_stack.substate++;
125689db417bSmatthias.ringwald                     break;
125789db417bSmatthias.ringwald                 case 1:
125889db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
125989db417bSmatthias.ringwald                     break;
126089db417bSmatthias.ringwald                 case 2:
12617b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
126228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
126328171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
126428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
126528171530Smatthias.ringwald                         // SLEEP MODE reached
126628171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
126728171530Smatthias.ringwald                         hci_emit_state();
126828171530Smatthias.ringwald                         break;
126928171530Smatthias.ringwald                     }
127028171530Smatthias.ringwald #endif
127172ea5239Smatthias.ringwald                     // switch mode
127289db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1273c7e0c5f6Smatthias.ringwald                     hci_emit_state();
127428171530Smatthias.ringwald                     break;
127528171530Smatthias.ringwald 
127689db417bSmatthias.ringwald                 default:
127789db417bSmatthias.ringwald                     break;
127889db417bSmatthias.ringwald             }
1279c7e0c5f6Smatthias.ringwald             break;
1280c7e0c5f6Smatthias.ringwald 
12813429f56bSmatthias.ringwald         default:
12823429f56bSmatthias.ringwald             break;
12831f504dbdSmatthias.ringwald     }
12843429f56bSmatthias.ringwald }
128516833f0aSmatthias.ringwald 
128631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1287c8e4258aSmatthias.ringwald     bd_addr_t addr;
1288c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1289c8e4258aSmatthias.ringwald     // house-keeping
1290c8e4258aSmatthias.ringwald 
1291c8e4258aSmatthias.ringwald     // create_connection?
1292c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1293c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1294339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1295c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1296c8e4258aSmatthias.ringwald         if (conn) {
1297c8e4258aSmatthias.ringwald             // if connection exists
1298c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
129917f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
130017f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1301c8e4258aSmatthias.ringwald             }
130217f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1303c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1304c8e4258aSmatthias.ringwald 
130517f1ba2aSmatthias.ringwald         }
1306c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
130717f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
130817f1ba2aSmatthias.ringwald         if (!conn){
130917f1ba2aSmatthias.ringwald             // notify client that alloc failed
131017f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
131117f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
131217f1ba2aSmatthias.ringwald         }
1313c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1314c8e4258aSmatthias.ringwald     }
1315c8e4258aSmatthias.ringwald 
13167fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
13177fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
13187fde4af9Smatthias.ringwald     }
13197fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
13207fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
13217fde4af9Smatthias.ringwald     }
13227fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
13237fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
13247fde4af9Smatthias.ringwald     }
13257fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
13267fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
13277fde4af9Smatthias.ringwald     }
13287fde4af9Smatthias.ringwald 
13298ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
13308ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
13318ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
13328ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
13338ef73945Smatthias.ringwald         }
13348ef73945Smatthias.ringwald     }
1335c8e4258aSmatthias.ringwald 
133631452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1337622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
133831452debSmatthias.ringwald }
13398adf0ddaSmatthias.ringwald 
13401cd208adSmatthias.ringwald /**
13411cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
13421cd208adSmatthias.ringwald  */
1343fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
13441cd208adSmatthias.ringwald     va_list argptr;
13451cd208adSmatthias.ringwald     va_start(argptr, cmd);
13467dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
13471cd208adSmatthias.ringwald     va_end(argptr);
13487dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
134993b8dc03Smatthias.ringwald }
1350c8e4258aSmatthias.ringwald 
1351ee091cf1Smatthias.ringwald // Create various non-HCI events.
1352ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1353ee091cf1Smatthias.ringwald 
1354c8e4258aSmatthias.ringwald void hci_emit_state(){
1355e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1356425d1371Smatthias.ringwald     uint8_t event[3];
135780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1358425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1359c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1360425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1361425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1362c8e4258aSmatthias.ringwald }
1363c8e4258aSmatthias.ringwald 
136417f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1365425d1371Smatthias.ringwald     uint8_t event[13];
1366c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1367425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
136817f1ba2aSmatthias.ringwald     event[2] = status;
1369c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1370c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1371c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1372c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1373425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1374425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1375c8e4258aSmatthias.ringwald }
1376c8e4258aSmatthias.ringwald 
13773c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1378425d1371Smatthias.ringwald     uint8_t event[6];
13793c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1380e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
13813c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
13823c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
13833c4d4b90Smatthias.ringwald     event[5] = reason;
1384425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1385425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13863c4d4b90Smatthias.ringwald }
13873c4d4b90Smatthias.ringwald 
1388ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1389e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1390425d1371Smatthias.ringwald     uint8_t event[4];
139180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1392425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1393ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1394425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1395425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1396ee091cf1Smatthias.ringwald }
139743bfb1bdSmatthias.ringwald 
139843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1399e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1400425d1371Smatthias.ringwald     uint8_t event[3];
140180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1402425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
140343bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1404425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1405425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
140643bfb1bdSmatthias.ringwald }
1407038bc64cSmatthias.ringwald 
1408038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1409e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1410425d1371Smatthias.ringwald     uint8_t event[2];
141180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1412425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1413425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1414425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1415038bc64cSmatthias.ringwald }
14161b0e3922Smatthias.ringwald 
141709ba8edeSmatthias.ringwald #ifndef EMBEDDED
14181b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1419e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1420425d1371Smatthias.ringwald     uint8_t event[6];
14211b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1422425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1423425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1424425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1425425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1426425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1427425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
14281b0e3922Smatthias.ringwald }
142909ba8edeSmatthias.ringwald #endif
14301b0e3922Smatthias.ringwald 
14312ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1432e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1433425d1371Smatthias.ringwald     uint8_t event[3];
14342ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1435425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
14362ed6235cSmatthias.ringwald     event[2] = enabled;
1437425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1438e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
14392ed6235cSmatthias.ringwald }
1440627c2f45Smatthias.ringwald 
1441627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1442e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1443627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1444e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1445f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
14462f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1447f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1448e0abb8e7S[email protected] 
1449e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1450e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1451e0abb8e7S[email protected] 
1452e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1453e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1454627c2f45Smatthias.ringwald }
1455381fbed8Smatthias.ringwald 
1456381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1457e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1458425d1371Smatthias.ringwald     uint8_t event[3];
1459381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1460425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1461381fbed8Smatthias.ringwald     event[2] = enabled;
1462425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1463425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1464381fbed8Smatthias.ringwald }
1465