xref: /btstack/src/hci.c (revision e0dbca836d24c7b5fd8ef62976cf5e397ef40224)
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 
65*e0dbca83S[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 10
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 
564223aafc1Smatthias.ringwald #ifndef EMBEDDED
56574ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
56674ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
56774ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
56874ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5695a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5705a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5715a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5725a06394aSmatthias.ringwald                     packet[9+i] = 0;
5735a06394aSmatthias.ringwald                     break;
574cdc9101dSmatthias.ringwald                 }
5755a06394aSmatthias.ringwald             }
576d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
57774ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
57874ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
57974ec757aSmatthias.ringwald             break;
58074ec757aSmatthias.ringwald 
58174ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
58274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
58374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
58474ec757aSmatthias.ringwald             // first send inq result packet
58574ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
58674ec757aSmatthias.ringwald             // then send cached remote names
58774ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
58874ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
58974ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
59074ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
59174ec757aSmatthias.ringwald                 }
59274ec757aSmatthias.ringwald             }
59374ec757aSmatthias.ringwald             return;
594223aafc1Smatthias.ringwald #endif
59574ec757aSmatthias.ringwald 
5966772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
597fe1ed1b8Smatthias.ringwald             if (!packet[2]){
598fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
599fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
600fe1ed1b8Smatthias.ringwald                 if (conn) {
601c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
602fe1ed1b8Smatthias.ringwald                 }
603fe1ed1b8Smatthias.ringwald             }
6046772a24cSmatthias.ringwald             break;
6056772a24cSmatthias.ringwald 
606c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
607c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
608c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
609c68bdf90Smatthias.ringwald             }
610c68bdf90Smatthias.ringwald             break;
611c68bdf90Smatthias.ringwald 
6125909f7f2Smatthias.ringwald #ifdef HAVE_BLE
6135909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
6145909f7f2Smatthias.ringwald             switch (packet[2]) {
6155909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
6165909f7f2Smatthias.ringwald                     // Connection management
6175909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
6185909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
6195909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
6205909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
6215909f7f2Smatthias.ringwald                     if (packet[3]){
6225909f7f2Smatthias.ringwald                         if (conn){
6235909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
6245909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
6255909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
6265909f7f2Smatthias.ringwald 
6275909f7f2Smatthias.ringwald                         }
6285909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
6295909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
6305909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
6315909f7f2Smatthias.ringwald                         }
6325909f7f2Smatthias.ringwald                         break;
6335909f7f2Smatthias.ringwald                     }
6345909f7f2Smatthias.ringwald                     if (!conn){
6355909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
6365909f7f2Smatthias.ringwald                     }
6375909f7f2Smatthias.ringwald                     if (!conn){
6385909f7f2Smatthias.ringwald                         // no memory
6395909f7f2Smatthias.ringwald                         break;
6405909f7f2Smatthias.ringwald                     }
6415909f7f2Smatthias.ringwald 
6425909f7f2Smatthias.ringwald                     conn->state = OPEN;
6435909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
6445909f7f2Smatthias.ringwald 
6455909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
6465909f7f2Smatthias.ringwald 
6475909f7f2Smatthias.ringwald                     // restart timer
6485909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
6495909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
6505909f7f2Smatthias.ringwald 
6515909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
6525909f7f2Smatthias.ringwald 
6535909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
6545909f7f2Smatthias.ringwald                     break;
6555909f7f2Smatthias.ringwald 
6565909f7f2Smatthias.ringwald                 default:
6575909f7f2Smatthias.ringwald                     break;
6585909f7f2Smatthias.ringwald             }
6595909f7f2Smatthias.ringwald             break;
6605909f7f2Smatthias.ringwald #endif
6615909f7f2Smatthias.ringwald 
6626772a24cSmatthias.ringwald         default:
6636772a24cSmatthias.ringwald             break;
664fe1ed1b8Smatthias.ringwald     }
665fe1ed1b8Smatthias.ringwald 
6663429f56bSmatthias.ringwald     // handle BT initialization
6673429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
6687301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
6697301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
6707301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
6717301ad89Smatthias.ringwald         // }
6727301ad89Smatthias.ringwald         // handle normal init sequence
6733429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
6743429f56bSmatthias.ringwald             // odd: waiting for event
675f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
6763429f56bSmatthias.ringwald                 hci_stack.substate++;
6773429f56bSmatthias.ringwald             }
6783429f56bSmatthias.ringwald         }
67922909952Smatthias.ringwald     }
68022909952Smatthias.ringwald 
68189db417bSmatthias.ringwald     // help with BT sleep
68289db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
68389db417bSmatthias.ringwald         && hci_stack.substate == 1
68489db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
68589db417bSmatthias.ringwald         hci_stack.substate++;
68689db417bSmatthias.ringwald     }
68789db417bSmatthias.ringwald 
6882718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
68994ab26f8Smatthias.ringwald 
69094ab26f8Smatthias.ringwald 	// execute main loop
69194ab26f8Smatthias.ringwald 	hci_run();
69216833f0aSmatthias.ringwald }
69316833f0aSmatthias.ringwald 
6940a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
69510e830c9Smatthias.ringwald     switch (packet_type) {
69610e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
69710e830c9Smatthias.ringwald             event_handler(packet, size);
69810e830c9Smatthias.ringwald             break;
69910e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
70010e830c9Smatthias.ringwald             acl_handler(packet, size);
70110e830c9Smatthias.ringwald             break;
70210e830c9Smatthias.ringwald         default:
70310e830c9Smatthias.ringwald             break;
70410e830c9Smatthias.ringwald     }
70510e830c9Smatthias.ringwald }
70610e830c9Smatthias.ringwald 
707fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
7082718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
7092718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
71016833f0aSmatthias.ringwald }
71116833f0aSmatthias.ringwald 
7124f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
713475c8125Smatthias.ringwald 
714475c8125Smatthias.ringwald     // reference to use transport layer implementation
71516833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
716475c8125Smatthias.ringwald 
71711e23e5fSmatthias.ringwald     // references to used control implementation
71811e23e5fSmatthias.ringwald     hci_stack.control = control;
71911e23e5fSmatthias.ringwald 
72011e23e5fSmatthias.ringwald     // reference to used config
72111e23e5fSmatthias.ringwald     hci_stack.config = config;
72211e23e5fSmatthias.ringwald 
723fe1ed1b8Smatthias.ringwald     // no connections yet
724fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
7250a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
726c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
727758b46ceSmatthias.ringwald 
728b031bebbSmatthias.ringwald     // no pending cmds
729b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
730b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
731b031bebbSmatthias.ringwald 
73216833f0aSmatthias.ringwald     // higher level handler
7332718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
73416833f0aSmatthias.ringwald 
735404843c1Smatthias.ringwald     // store and open remote device db
736404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
737404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
738404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
739404843c1Smatthias.ringwald     }
74029d53098Smatthias.ringwald 
7418fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
7428fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7438fcba05dSmatthias.ringwald 
74416833f0aSmatthias.ringwald     // register packet handlers with transport
74510e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
746f5454fc6Smatthias.ringwald 
747f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
748e2386ba1S[email protected] 
749e2386ba1S[email protected]     // class of device
750e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
751475c8125Smatthias.ringwald }
752475c8125Smatthias.ringwald 
753404843c1Smatthias.ringwald void hci_close(){
754404843c1Smatthias.ringwald     // close remote device db
755404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
756404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
757404843c1Smatthias.ringwald     }
758f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
759f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
760f5454fc6Smatthias.ringwald }
761f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
762404843c1Smatthias.ringwald }
763404843c1Smatthias.ringwald 
7648d213e1aSmatthias.ringwald // State-Module-Driver overview
7658d213e1aSmatthias.ringwald // state                    module  low-level
7668d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
7678d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
7688d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
7698d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
770d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
771d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
772c7e0c5f6Smatthias.ringwald 
77340d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
7747301ad89Smatthias.ringwald 
775038bc64cSmatthias.ringwald     // power on
776f9a30166Smatthias.ringwald     int err = 0;
777f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
778f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
779f9a30166Smatthias.ringwald     }
780038bc64cSmatthias.ringwald     if (err){
7817d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
782038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
783038bc64cSmatthias.ringwald         return err;
784038bc64cSmatthias.ringwald     }
785038bc64cSmatthias.ringwald 
786038bc64cSmatthias.ringwald     // open low-level device
787038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
788038bc64cSmatthias.ringwald     if (err){
7897d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
790f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
791f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
792f9a30166Smatthias.ringwald         }
793038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
794038bc64cSmatthias.ringwald         return err;
795038bc64cSmatthias.ringwald     }
7968d213e1aSmatthias.ringwald     return 0;
7978d213e1aSmatthias.ringwald }
798038bc64cSmatthias.ringwald 
79940d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
8008d213e1aSmatthias.ringwald 
8017b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
8029418f9c9Smatthias.ringwald 
8038d213e1aSmatthias.ringwald     // close low-level device
8048d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
8058d213e1aSmatthias.ringwald 
8067b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
8079418f9c9Smatthias.ringwald 
8088d213e1aSmatthias.ringwald     // power off
8098d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
8108d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
8118d213e1aSmatthias.ringwald     }
8129418f9c9Smatthias.ringwald 
8137b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
8149418f9c9Smatthias.ringwald 
81572ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
81672ea5239Smatthias.ringwald }
81772ea5239Smatthias.ringwald 
81840d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
81972ea5239Smatthias.ringwald 
8207b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
8213144bce4Smatthias.ringwald 
822b429b9b7Smatthias.ringwald #if 0
823b429b9b7Smatthias.ringwald     // don't close serial port during sleep
824b429b9b7Smatthias.ringwald 
82572ea5239Smatthias.ringwald     // close low-level device
82672ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
827b429b9b7Smatthias.ringwald #endif
82872ea5239Smatthias.ringwald 
82972ea5239Smatthias.ringwald     // sleep mode
8303144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
83172ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
83272ea5239Smatthias.ringwald     }
833b429b9b7Smatthias.ringwald 
83472ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
8358d213e1aSmatthias.ringwald }
8368d213e1aSmatthias.ringwald 
83740d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
838b429b9b7Smatthias.ringwald 
8397b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
840b429b9b7Smatthias.ringwald 
841b429b9b7Smatthias.ringwald     // wake on
842b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
843b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
844b429b9b7Smatthias.ringwald     }
845b429b9b7Smatthias.ringwald 
846b429b9b7Smatthias.ringwald #if 0
847b429b9b7Smatthias.ringwald     // open low-level device
848b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
849b429b9b7Smatthias.ringwald     if (err){
8507d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
851b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
852b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
853b429b9b7Smatthias.ringwald         }
854b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
855b429b9b7Smatthias.ringwald         return err;
856b429b9b7Smatthias.ringwald     }
857b429b9b7Smatthias.ringwald #endif
858b429b9b7Smatthias.ringwald 
859b429b9b7Smatthias.ringwald     return 0;
860b429b9b7Smatthias.ringwald }
861b429b9b7Smatthias.ringwald 
862b429b9b7Smatthias.ringwald 
8638d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
864d661ed19Smatthias.ringwald 
8657b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
866d661ed19Smatthias.ringwald 
8678d213e1aSmatthias.ringwald     int err = 0;
8688d213e1aSmatthias.ringwald     switch (hci_stack.state){
8698d213e1aSmatthias.ringwald 
8708d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
8718d213e1aSmatthias.ringwald             switch (power_mode){
8728d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8738d213e1aSmatthias.ringwald                     err = hci_power_control_on();
8748d213e1aSmatthias.ringwald                     if (err) return err;
8757301ad89Smatthias.ringwald                     // set up state machine
8767301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
8777301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8787301ad89Smatthias.ringwald                     hci_stack.substate = 0;
8798d213e1aSmatthias.ringwald                     break;
8808d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8818d213e1aSmatthias.ringwald                     // do nothing
8828d213e1aSmatthias.ringwald                     break;
8838d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
884b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
8858d213e1aSmatthias.ringwald                     break;
8868d213e1aSmatthias.ringwald             }
8878d213e1aSmatthias.ringwald             break;
8887301ad89Smatthias.ringwald 
8898d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8908d213e1aSmatthias.ringwald             switch (power_mode){
8918d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8928d213e1aSmatthias.ringwald                     // do nothing
8938d213e1aSmatthias.ringwald                     break;
8948d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8958d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
8968d213e1aSmatthias.ringwald                     hci_power_control_off();
8978d213e1aSmatthias.ringwald                     break;
8988d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
899b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
90072ea5239Smatthias.ringwald                     hci_power_control_sleep();
9018d213e1aSmatthias.ringwald                     break;
9028d213e1aSmatthias.ringwald             }
9038d213e1aSmatthias.ringwald             break;
9047301ad89Smatthias.ringwald 
9058d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
9068d213e1aSmatthias.ringwald             switch (power_mode){
9078d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9088d213e1aSmatthias.ringwald                     // do nothing
9098d213e1aSmatthias.ringwald                     break;
9108d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
911c7e0c5f6Smatthias.ringwald                     // see hci_run
912c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9138d213e1aSmatthias.ringwald                     break;
9148d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
915b546ac54Smatthias.ringwald                     // see hci_run
916b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
91789db417bSmatthias.ringwald                     hci_stack.substate = 0;
9188d213e1aSmatthias.ringwald                     break;
9198d213e1aSmatthias.ringwald             }
9208d213e1aSmatthias.ringwald             break;
9217301ad89Smatthias.ringwald 
9228d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
9238d213e1aSmatthias.ringwald             switch (power_mode){
9248d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9258d213e1aSmatthias.ringwald                     // set up state machine
9268d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9278d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
9288d213e1aSmatthias.ringwald                     break;
9298d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9308d213e1aSmatthias.ringwald                     // do nothing
9318d213e1aSmatthias.ringwald                     break;
9328d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
933b546ac54Smatthias.ringwald                     // see hci_run
934b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
93589db417bSmatthias.ringwald                     hci_stack.substate = 0;
9368d213e1aSmatthias.ringwald                     break;
9378d213e1aSmatthias.ringwald             }
9388d213e1aSmatthias.ringwald             break;
9398d213e1aSmatthias.ringwald 
9408d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
9418d213e1aSmatthias.ringwald             switch (power_mode){
9428d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
94328171530Smatthias.ringwald 
94428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
94528171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
94628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9478747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
948da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
94928171530Smatthias.ringwald                         break;
95028171530Smatthias.ringwald                     }
95128171530Smatthias.ringwald #endif
952b546ac54Smatthias.ringwald                     // set up state machine
953b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
954b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
955b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9568d213e1aSmatthias.ringwald                     break;
9578d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
958b546ac54Smatthias.ringwald                     // see hci_run
959b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9608d213e1aSmatthias.ringwald                     break;
9618d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
962b546ac54Smatthias.ringwald                     // do nothing
9638d213e1aSmatthias.ringwald                     break;
9648d213e1aSmatthias.ringwald             }
9658d213e1aSmatthias.ringwald             break;
9668d213e1aSmatthias.ringwald 
9678d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
9688d213e1aSmatthias.ringwald             switch (power_mode){
9698d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
97028171530Smatthias.ringwald 
97128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
97228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
97328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9748747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
975da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
976758b46ceSmatthias.ringwald                         hci_update_scan_enable();
97728171530Smatthias.ringwald                         break;
97828171530Smatthias.ringwald                     }
97928171530Smatthias.ringwald #endif
9803144bce4Smatthias.ringwald                     err = hci_power_control_wake();
9813144bce4Smatthias.ringwald                     if (err) return err;
982b546ac54Smatthias.ringwald                     // set up state machine
983b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
984b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
985b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9868d213e1aSmatthias.ringwald                     break;
9878d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9884ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9898d213e1aSmatthias.ringwald                     break;
9908d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
991b546ac54Smatthias.ringwald                     // do nothing
9928d213e1aSmatthias.ringwald                     break;
9938d213e1aSmatthias.ringwald             }
9948d213e1aSmatthias.ringwald             break;
99511e23e5fSmatthias.ringwald     }
99668d92d03Smatthias.ringwald 
997038bc64cSmatthias.ringwald     // create internal event
998ee8bf225Smatthias.ringwald 	hci_emit_state();
999ee8bf225Smatthias.ringwald 
100068d92d03Smatthias.ringwald 	// trigger next/first action
100168d92d03Smatthias.ringwald 	hci_run();
100268d92d03Smatthias.ringwald 
1003475c8125Smatthias.ringwald     return 0;
1004475c8125Smatthias.ringwald }
1005475c8125Smatthias.ringwald 
1006758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1007758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1008758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1009758b46ceSmatthias.ringwald     hci_run();
1010758b46ceSmatthias.ringwald }
1011758b46ceSmatthias.ringwald 
1012381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1013381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1014381fbed8Smatthias.ringwald 
1015381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1016381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1017381fbed8Smatthias.ringwald         return;
1018381fbed8Smatthias.ringwald     }
1019381fbed8Smatthias.ringwald 
1020381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1021758b46ceSmatthias.ringwald     hci_update_scan_enable();
1022758b46ceSmatthias.ringwald }
1023b031bebbSmatthias.ringwald 
1024758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1025758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1026758b46ceSmatthias.ringwald 
1027758b46ceSmatthias.ringwald     // don't emit event
1028758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1029758b46ceSmatthias.ringwald 
1030758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1031758b46ceSmatthias.ringwald     hci_update_scan_enable();
1032381fbed8Smatthias.ringwald }
1033381fbed8Smatthias.ringwald 
103406b35ec0Smatthias.ringwald void hci_run(){
10358a485f27Smatthias.ringwald 
103632ab9390Smatthias.ringwald     hci_connection_t * connection;
103732ab9390Smatthias.ringwald     linked_item_t * it;
103832ab9390Smatthias.ringwald 
1039ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1040ce4c8fabSmatthias.ringwald 
1041b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1042b031bebbSmatthias.ringwald 
1043b031bebbSmatthias.ringwald     // decline incoming connections
1044ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1045ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1046ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1047ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1048ce4c8fabSmatthias.ringwald     }
1049ce4c8fabSmatthias.ringwald 
1050b031bebbSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1051b031bebbSmatthias.ringwald 
1052b031bebbSmatthias.ringwald     // send scan enable
105396774428Smatthias.ringwald     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff){
1054b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1055b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1056b031bebbSmatthias.ringwald     }
1057b031bebbSmatthias.ringwald 
105832ab9390Smatthias.ringwald     // send pending HCI commands
105932ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
106032ab9390Smatthias.ringwald 
1061b031bebbSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
106232ab9390Smatthias.ringwald 
1063b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
106432ab9390Smatthias.ringwald 
106532ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
10667b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
106732ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
106832ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1069c7e0c5f6Smatthias.ringwald         }
1070c7e0c5f6Smatthias.ringwald 
1071de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
107232ab9390Smatthias.ringwald 
107332ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
107432ab9390Smatthias.ringwald             link_key_t link_key;
10757b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
107632ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
107732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
107832ab9390Smatthias.ringwald             } else {
107932ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
108032ab9390Smatthias.ringwald             }
108128ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
108232ab9390Smatthias.ringwald         }
108332ab9390Smatthias.ringwald     }
108432ab9390Smatthias.ringwald 
1085de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1086c7e0c5f6Smatthias.ringwald 
10873429f56bSmatthias.ringwald     switch (hci_stack.state){
10883429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10897b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
10903429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
10913429f56bSmatthias.ringwald                 // odd: waiting for command completion
109206b35ec0Smatthias.ringwald                 return;
10933429f56bSmatthias.ringwald             }
109490919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1095c7492964Smatthias.ringwald                 case 0: // RESET
109622909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
1097c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1098c7492964Smatthias.ringwald                         // skip baud change
1099c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1100c7492964Smatthias.ringwald                     }
11013429f56bSmatthias.ringwald                     break;
1102c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
11037dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
11047dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1105c7492964Smatthias.ringwald                     break;
1106c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1107c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1108c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1109c7492964Smatthias.ringwald                     // break missing here for fall through
1110c7492964Smatthias.ringwald 
1111c7492964Smatthias.ringwald                 case 3:
111290919203Smatthias.ringwald                     // custom initialization
1113db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
11147dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1115d62aca9fSmatthias.ringwald                         if (valid_cmd){
11167dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
11177dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1118c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
111990919203Smatthias.ringwald                             break;
112090919203Smatthias.ringwald                         }
1121d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
112290919203Smatthias.ringwald                     }
11232f6c30e1Smatthias.ringwald                     // otherwise continue
1124f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1125f432a6ddSmatthias.ringwald 					break;
1126c7492964Smatthias.ringwald 				case 4:
1127e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1128e2edc0c3Smatthias.ringwald 					break;
1129c7492964Smatthias.ringwald                 case 5:
11303429f56bSmatthias.ringwald                     // ca. 15 sec
11313429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
11323429f56bSmatthias.ringwald                     break;
1133c7492964Smatthias.ringwald                 case 6:
1134559e517eS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
1135559e517eS[email protected]                     break;
1136e2386ba1S[email protected]                 case 7:
1137e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1138e2386ba1S[email protected]                     break;
1139559e517eS[email protected]                 case 8:
1140e2386ba1S[email protected]                     if (hci_stack.local_name){
1141e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1142e2386ba1S[email protected]                     } else {
11438a485f27Smatthias.ringwald                         char hostname[30];
1144e2386ba1S[email protected] #ifdef EMBEDDED
1145e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1146e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1147e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1148e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1149e2386ba1S[email protected] #else
1150e2386ba1S[email protected]                         // hostname for POSIX systems
11518a485f27Smatthias.ringwald                         gethostname(hostname, 30);
11528a485f27Smatthias.ringwald                         hostname[29] = '\0';
1153e2386ba1S[email protected] #endif
11548a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
11558a485f27Smatthias.ringwald                     }
11563c4d4b90Smatthias.ringwald                     break;
1157e2386ba1S[email protected]                 case 9:
1158*e0dbca83S[email protected]                     hci_send_cmd(&hci_set_event_mask,0xffffffff, 0xFFFFFFFF); ///0x1DFFFFFF
1159e2386ba1S[email protected]                     break;
1160559e517eS[email protected]                 case 10:
1161*e0dbca83S[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
1162*e0dbca83S[email protected] 					break;
1163*e0dbca83S[email protected]                 case 11:
11643429f56bSmatthias.ringwald                     // done.
11653429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1166b360b6adSmatthias.ringwald                     hci_emit_state();
11673429f56bSmatthias.ringwald                     break;
11683429f56bSmatthias.ringwald                 default:
11693429f56bSmatthias.ringwald                     break;
1170475c8125Smatthias.ringwald             }
11713429f56bSmatthias.ringwald             hci_stack.substate++;
11723429f56bSmatthias.ringwald             break;
1173c7e0c5f6Smatthias.ringwald 
1174c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1175c7e0c5f6Smatthias.ringwald 
11767b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1177c7e0c5f6Smatthias.ringwald             // close all open connections
1178c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1179c7e0c5f6Smatthias.ringwald             if (connection){
118032ab9390Smatthias.ringwald 
1181c7e0c5f6Smatthias.ringwald                 // send disconnect
118232ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
118332ab9390Smatthias.ringwald 
118479bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11856ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1186c7e0c5f6Smatthias.ringwald 
1187c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1188c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1189c7e0c5f6Smatthias.ringwald                 return;
1190c7e0c5f6Smatthias.ringwald             }
11917b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1192c7e0c5f6Smatthias.ringwald 
119372ea5239Smatthias.ringwald             // switch mode
1194c7e0c5f6Smatthias.ringwald             hci_power_control_off();
11959418f9c9Smatthias.ringwald 
11967b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
119772ea5239Smatthias.ringwald             hci_emit_state();
11987b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
119972ea5239Smatthias.ringwald             break;
1200c7e0c5f6Smatthias.ringwald 
120172ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
120289db417bSmatthias.ringwald             switch(hci_stack.substate) {
120389db417bSmatthias.ringwald                 case 0:
12047b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
120572ea5239Smatthias.ringwald                     // close all open connections
120672ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
120766da7044Smatthias.ringwald 
120828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
120966da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
121066da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
121166da7044Smatthias.ringwald                         connection = NULL;
121266da7044Smatthias.ringwald                     }
121366da7044Smatthias.ringwald #endif
121472ea5239Smatthias.ringwald                     if (connection){
121532ab9390Smatthias.ringwald 
121672ea5239Smatthias.ringwald                         // send disconnect
121732ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
121832ab9390Smatthias.ringwald 
121979bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
12206ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
122172ea5239Smatthias.ringwald 
122272ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
122372ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
122472ea5239Smatthias.ringwald                         return;
122572ea5239Smatthias.ringwald                     }
122672ea5239Smatthias.ringwald 
122789db417bSmatthias.ringwald                     // disable page and inquiry scan
122832ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
122932ab9390Smatthias.ringwald 
1230758b46ceSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq cans\n");
1231758b46ceSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
123289db417bSmatthias.ringwald 
123389db417bSmatthias.ringwald                     // continue in next sub state
123489db417bSmatthias.ringwald                     hci_stack.substate++;
123589db417bSmatthias.ringwald                     break;
123689db417bSmatthias.ringwald                 case 1:
123789db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
123889db417bSmatthias.ringwald                     break;
123989db417bSmatthias.ringwald                 case 2:
12407b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
124128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
124228171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
124328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
124428171530Smatthias.ringwald                         // SLEEP MODE reached
124528171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
124628171530Smatthias.ringwald                         hci_emit_state();
124728171530Smatthias.ringwald                         break;
124828171530Smatthias.ringwald                     }
124928171530Smatthias.ringwald #endif
125072ea5239Smatthias.ringwald                     // switch mode
125189db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1252c7e0c5f6Smatthias.ringwald                     hci_emit_state();
125328171530Smatthias.ringwald                     break;
125428171530Smatthias.ringwald 
125589db417bSmatthias.ringwald                 default:
125689db417bSmatthias.ringwald                     break;
125789db417bSmatthias.ringwald             }
1258c7e0c5f6Smatthias.ringwald             break;
1259c7e0c5f6Smatthias.ringwald 
12603429f56bSmatthias.ringwald         default:
12613429f56bSmatthias.ringwald             break;
12621f504dbdSmatthias.ringwald     }
12633429f56bSmatthias.ringwald }
126416833f0aSmatthias.ringwald 
126531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1266c8e4258aSmatthias.ringwald     bd_addr_t addr;
1267c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1268c8e4258aSmatthias.ringwald     // house-keeping
1269c8e4258aSmatthias.ringwald 
1270c8e4258aSmatthias.ringwald     // create_connection?
1271c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1272c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1273339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1274c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1275c8e4258aSmatthias.ringwald         if (conn) {
1276c8e4258aSmatthias.ringwald             // if connection exists
1277c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
127817f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
127917f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1280c8e4258aSmatthias.ringwald             }
128117f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1282c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1283c8e4258aSmatthias.ringwald 
128417f1ba2aSmatthias.ringwald         }
1285c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
128617f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
128717f1ba2aSmatthias.ringwald         if (!conn){
128817f1ba2aSmatthias.ringwald             // notify client that alloc failed
128917f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
129017f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
129117f1ba2aSmatthias.ringwald         }
1292c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1293c8e4258aSmatthias.ringwald     }
1294c8e4258aSmatthias.ringwald 
12957fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
12967fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
12977fde4af9Smatthias.ringwald     }
12987fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
12997fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
13007fde4af9Smatthias.ringwald     }
13017fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
13027fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
13037fde4af9Smatthias.ringwald     }
13047fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
13057fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
13067fde4af9Smatthias.ringwald     }
13077fde4af9Smatthias.ringwald 
13088ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
13098ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
13108ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
13118ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
13128ef73945Smatthias.ringwald         }
13138ef73945Smatthias.ringwald     }
1314c8e4258aSmatthias.ringwald 
131531452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1316622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
131731452debSmatthias.ringwald }
13188adf0ddaSmatthias.ringwald 
13191cd208adSmatthias.ringwald /**
13201cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
13211cd208adSmatthias.ringwald  */
1322fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
13231cd208adSmatthias.ringwald     va_list argptr;
13241cd208adSmatthias.ringwald     va_start(argptr, cmd);
13257dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
13261cd208adSmatthias.ringwald     va_end(argptr);
13277dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
132893b8dc03Smatthias.ringwald }
1329c8e4258aSmatthias.ringwald 
1330ee091cf1Smatthias.ringwald // Create various non-HCI events.
1331ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1332ee091cf1Smatthias.ringwald 
1333c8e4258aSmatthias.ringwald void hci_emit_state(){
1334e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1335425d1371Smatthias.ringwald     uint8_t event[3];
133680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1337425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1338c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1339425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1340425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1341c8e4258aSmatthias.ringwald }
1342c8e4258aSmatthias.ringwald 
134317f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1344425d1371Smatthias.ringwald     uint8_t event[13];
1345c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1346425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
134717f1ba2aSmatthias.ringwald     event[2] = status;
1348c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1349c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1350c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1351c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1352425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1353425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1354c8e4258aSmatthias.ringwald }
1355c8e4258aSmatthias.ringwald 
13563c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1357425d1371Smatthias.ringwald     uint8_t event[6];
13583c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1359e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
13603c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
13613c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
13623c4d4b90Smatthias.ringwald     event[5] = reason;
1363425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1364425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13653c4d4b90Smatthias.ringwald }
13663c4d4b90Smatthias.ringwald 
1367ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1368e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1369425d1371Smatthias.ringwald     uint8_t event[4];
137080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1371425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1372ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1373425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1374425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1375ee091cf1Smatthias.ringwald }
137643bfb1bdSmatthias.ringwald 
137743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1378e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1379425d1371Smatthias.ringwald     uint8_t event[3];
138080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1381425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
138243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1383425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1384425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
138543bfb1bdSmatthias.ringwald }
1386038bc64cSmatthias.ringwald 
1387038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1388e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1389425d1371Smatthias.ringwald     uint8_t event[2];
139080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1391425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1392425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1393425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1394038bc64cSmatthias.ringwald }
13951b0e3922Smatthias.ringwald 
139609ba8edeSmatthias.ringwald #ifndef EMBEDDED
13971b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1398e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1399425d1371Smatthias.ringwald     uint8_t event[6];
14001b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1401425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1402425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1403425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1404425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1405425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1406425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
14071b0e3922Smatthias.ringwald }
140809ba8edeSmatthias.ringwald #endif
14091b0e3922Smatthias.ringwald 
14102ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1411e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1412425d1371Smatthias.ringwald     uint8_t event[3];
14132ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1414425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
14152ed6235cSmatthias.ringwald     event[2] = enabled;
1416425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1417e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
14182ed6235cSmatthias.ringwald }
1419627c2f45Smatthias.ringwald 
1420627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1421e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1422627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1423e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1424f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
14252f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1426f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1427e0abb8e7S[email protected] 
1428e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1429e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1430e0abb8e7S[email protected] 
1431e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1432e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1433627c2f45Smatthias.ringwald }
1434381fbed8Smatthias.ringwald 
1435381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1436e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1437425d1371Smatthias.ringwald     uint8_t event[3];
1438381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1439425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1440381fbed8Smatthias.ringwald     event[2] = enabled;
1441425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1442425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1443381fbed8Smatthias.ringwald }
1444