xref: /btstack/src/hci.c (revision da5275c56c305c7e28093de69dd68fc7881545d3)
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*da5275c5S[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 6
66*da5275c5S[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)) {
447188981d2Smatthias.ringwald                 bd_addr_t addr;
448188981d2Smatthias.ringwald                 bt_flip_addr(addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
449188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
450188981d2Smatthias.ringwald                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(addr));
451188981d2Smatthias.ringwald             }
452381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
453381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
454381fbed8Smatthias.ringwald             }
455559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
456559e517eS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], 8);
457559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
458559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
459559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
460559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
461559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
462559e517eS[email protected]             }
46356cf178bSmatthias.ringwald             break;
46456cf178bSmatthias.ringwald 
4657ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4667ec5eeaaSmatthias.ringwald             // get num cmd packets
4677b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4687ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4697ec5eeaaSmatthias.ringwald             break;
4707ec5eeaaSmatthias.ringwald 
47156cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
47256cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
47356cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
47456cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
47556cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
47656cf178bSmatthias.ringwald                 if (!conn){
4777d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
47856cf178bSmatthias.ringwald                     continue;
47956cf178bSmatthias.ringwald                 }
48056cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4817b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
48256cf178bSmatthias.ringwald             }
4836772a24cSmatthias.ringwald             break;
4846772a24cSmatthias.ringwald 
4851f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
48637eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
48737eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4882d00edd4Smatthias.ringwald             link_type = packet[11];
489339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
49037eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4911f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4921f7b95a1Smatthias.ringwald                 if (!conn) {
4931f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4941f7b95a1Smatthias.ringwald                 }
495ce4c8fabSmatthias.ringwald                 if (!conn) {
496ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
497ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
498ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
499ce4c8fabSmatthias.ringwald                     break;
500ce4c8fabSmatthias.ringwald                 }
50132ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
50232ab9390Smatthias.ringwald                 hci_run();
50337eaa4cfSmatthias.ringwald             } else {
504ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
505ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
506ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
50737eaa4cfSmatthias.ringwald             }
5081f7b95a1Smatthias.ringwald             break;
5091f7b95a1Smatthias.ringwald 
5106772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
511fe1ed1b8Smatthias.ringwald             // Connection management
512fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
513339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
5141f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
515fe1ed1b8Smatthias.ringwald             if (conn) {
516b448a0e7Smatthias.ringwald                 if (!packet[2]){
517c8e4258aSmatthias.ringwald                     conn->state = OPEN;
518fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
519ee091cf1Smatthias.ringwald 
520c785ef68Smatthias.ringwald                     // restart timer
521c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
522ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
523c785ef68Smatthias.ringwald 
524339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
52543bfb1bdSmatthias.ringwald 
52643bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
527b448a0e7Smatthias.ringwald                 } else {
528b448a0e7Smatthias.ringwald                     // connection failed, remove entry
529b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
530a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
531c12e46e7Smatthias.ringwald 
532c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
533c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
534c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
535c12e46e7Smatthias.ringwald                     }
536fe1ed1b8Smatthias.ringwald                 }
537fe1ed1b8Smatthias.ringwald             }
5386772a24cSmatthias.ringwald             break;
539fe1ed1b8Smatthias.ringwald 
5407fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5417b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5427fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
54374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
54432ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
54564472d52Smatthias.ringwald             hci_run();
546d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
54729d53098Smatthias.ringwald             return;
5487fde4af9Smatthias.ringwald 
5497fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5507fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
55174ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
55229d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
553287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
55429d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5557fde4af9Smatthias.ringwald             break;
5567fde4af9Smatthias.ringwald 
5577fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5587fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
559d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
560d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
561d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
562d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
5637fde4af9Smatthias.ringwald             break;
5647fde4af9Smatthias.ringwald 
565223aafc1Smatthias.ringwald #ifndef EMBEDDED
56674ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
56774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
56874ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
56974ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5705a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5715a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5725a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5735a06394aSmatthias.ringwald                     packet[9+i] = 0;
5745a06394aSmatthias.ringwald                     break;
575cdc9101dSmatthias.ringwald                 }
5765a06394aSmatthias.ringwald             }
577d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
57874ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
57974ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
58074ec757aSmatthias.ringwald             break;
58174ec757aSmatthias.ringwald 
58274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
58374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
58474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
58574ec757aSmatthias.ringwald             // first send inq result packet
58674ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
58774ec757aSmatthias.ringwald             // then send cached remote names
58874ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
58974ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
59074ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
59174ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
59274ec757aSmatthias.ringwald                 }
59374ec757aSmatthias.ringwald             }
59474ec757aSmatthias.ringwald             return;
595223aafc1Smatthias.ringwald #endif
59674ec757aSmatthias.ringwald 
5976772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
598fe1ed1b8Smatthias.ringwald             if (!packet[2]){
599fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
600fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
601fe1ed1b8Smatthias.ringwald                 if (conn) {
602c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
603fe1ed1b8Smatthias.ringwald                 }
604fe1ed1b8Smatthias.ringwald             }
6056772a24cSmatthias.ringwald             break;
6066772a24cSmatthias.ringwald 
607c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
608c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
609c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
610c68bdf90Smatthias.ringwald             }
611c68bdf90Smatthias.ringwald             break;
612c68bdf90Smatthias.ringwald 
6135909f7f2Smatthias.ringwald #ifdef HAVE_BLE
6145909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
6155909f7f2Smatthias.ringwald             switch (packet[2]) {
6165909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
6175909f7f2Smatthias.ringwald                     // Connection management
6185909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
6195909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
6205909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
6215909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
6225909f7f2Smatthias.ringwald                     if (packet[3]){
6235909f7f2Smatthias.ringwald                         if (conn){
6245909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
6255909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
6265909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
6275909f7f2Smatthias.ringwald 
6285909f7f2Smatthias.ringwald                         }
6295909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
6305909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
6315909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
6325909f7f2Smatthias.ringwald                         }
6335909f7f2Smatthias.ringwald                         break;
6345909f7f2Smatthias.ringwald                     }
6355909f7f2Smatthias.ringwald                     if (!conn){
6365909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
6375909f7f2Smatthias.ringwald                     }
6385909f7f2Smatthias.ringwald                     if (!conn){
6395909f7f2Smatthias.ringwald                         // no memory
6405909f7f2Smatthias.ringwald                         break;
6415909f7f2Smatthias.ringwald                     }
6425909f7f2Smatthias.ringwald 
6435909f7f2Smatthias.ringwald                     conn->state = OPEN;
6445909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
6455909f7f2Smatthias.ringwald 
6465909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
6475909f7f2Smatthias.ringwald 
6485909f7f2Smatthias.ringwald                     // restart timer
6495909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
6505909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
6515909f7f2Smatthias.ringwald 
6525909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
6535909f7f2Smatthias.ringwald 
6545909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
6555909f7f2Smatthias.ringwald                     break;
6565909f7f2Smatthias.ringwald 
6575909f7f2Smatthias.ringwald                 default:
6585909f7f2Smatthias.ringwald                     break;
6595909f7f2Smatthias.ringwald             }
6605909f7f2Smatthias.ringwald             break;
6615909f7f2Smatthias.ringwald #endif
6625909f7f2Smatthias.ringwald 
6636772a24cSmatthias.ringwald         default:
6646772a24cSmatthias.ringwald             break;
665fe1ed1b8Smatthias.ringwald     }
666fe1ed1b8Smatthias.ringwald 
6673429f56bSmatthias.ringwald     // handle BT initialization
6683429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
6697301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
6707301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
6717301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
6727301ad89Smatthias.ringwald         // }
6737301ad89Smatthias.ringwald         // handle normal init sequence
6743429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
6753429f56bSmatthias.ringwald             // odd: waiting for event
676f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
6773429f56bSmatthias.ringwald                 hci_stack.substate++;
6783429f56bSmatthias.ringwald             }
6793429f56bSmatthias.ringwald         }
68022909952Smatthias.ringwald     }
68122909952Smatthias.ringwald 
68289db417bSmatthias.ringwald     // help with BT sleep
68389db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
68489db417bSmatthias.ringwald         && hci_stack.substate == 1
68589db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
68689db417bSmatthias.ringwald         hci_stack.substate++;
68789db417bSmatthias.ringwald     }
68889db417bSmatthias.ringwald 
6892718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
69094ab26f8Smatthias.ringwald 
69194ab26f8Smatthias.ringwald 	// execute main loop
69294ab26f8Smatthias.ringwald 	hci_run();
69316833f0aSmatthias.ringwald }
69416833f0aSmatthias.ringwald 
6950a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
69610e830c9Smatthias.ringwald     switch (packet_type) {
69710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
69810e830c9Smatthias.ringwald             event_handler(packet, size);
69910e830c9Smatthias.ringwald             break;
70010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
70110e830c9Smatthias.ringwald             acl_handler(packet, size);
70210e830c9Smatthias.ringwald             break;
70310e830c9Smatthias.ringwald         default:
70410e830c9Smatthias.ringwald             break;
70510e830c9Smatthias.ringwald     }
70610e830c9Smatthias.ringwald }
70710e830c9Smatthias.ringwald 
708fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
7092718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
7102718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
71116833f0aSmatthias.ringwald }
71216833f0aSmatthias.ringwald 
7134f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
714475c8125Smatthias.ringwald 
715475c8125Smatthias.ringwald     // reference to use transport layer implementation
71616833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
717475c8125Smatthias.ringwald 
71811e23e5fSmatthias.ringwald     // references to used control implementation
71911e23e5fSmatthias.ringwald     hci_stack.control = control;
72011e23e5fSmatthias.ringwald 
72111e23e5fSmatthias.ringwald     // reference to used config
72211e23e5fSmatthias.ringwald     hci_stack.config = config;
72311e23e5fSmatthias.ringwald 
724fe1ed1b8Smatthias.ringwald     // no connections yet
725fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
7260a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
727c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
728758b46ceSmatthias.ringwald 
729b031bebbSmatthias.ringwald     // no pending cmds
730b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
731b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
732b031bebbSmatthias.ringwald 
73316833f0aSmatthias.ringwald     // higher level handler
7342718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
73516833f0aSmatthias.ringwald 
736404843c1Smatthias.ringwald     // store and open remote device db
737404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
738404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
739404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
740404843c1Smatthias.ringwald     }
74129d53098Smatthias.ringwald 
7428fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
7438fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7448fcba05dSmatthias.ringwald 
74516833f0aSmatthias.ringwald     // register packet handlers with transport
74610e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
747f5454fc6Smatthias.ringwald 
748f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
749475c8125Smatthias.ringwald }
750475c8125Smatthias.ringwald 
751404843c1Smatthias.ringwald void hci_close(){
752404843c1Smatthias.ringwald     // close remote device db
753404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
754404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
755404843c1Smatthias.ringwald     }
756f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
757f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
758f5454fc6Smatthias.ringwald }
759f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
760404843c1Smatthias.ringwald }
761404843c1Smatthias.ringwald 
7628d213e1aSmatthias.ringwald // State-Module-Driver overview
7638d213e1aSmatthias.ringwald // state                    module  low-level
7648d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
7658d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
7668d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
7678d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
768d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
769d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
770c7e0c5f6Smatthias.ringwald 
77140d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
7727301ad89Smatthias.ringwald 
773038bc64cSmatthias.ringwald     // power on
774f9a30166Smatthias.ringwald     int err = 0;
775f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
776f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
777f9a30166Smatthias.ringwald     }
778038bc64cSmatthias.ringwald     if (err){
7797d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
780038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
781038bc64cSmatthias.ringwald         return err;
782038bc64cSmatthias.ringwald     }
783038bc64cSmatthias.ringwald 
784038bc64cSmatthias.ringwald     // open low-level device
785038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
786038bc64cSmatthias.ringwald     if (err){
7877d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
788f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
789f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
790f9a30166Smatthias.ringwald         }
791038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
792038bc64cSmatthias.ringwald         return err;
793038bc64cSmatthias.ringwald     }
7948d213e1aSmatthias.ringwald     return 0;
7958d213e1aSmatthias.ringwald }
796038bc64cSmatthias.ringwald 
79740d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
7988d213e1aSmatthias.ringwald 
7997b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
8009418f9c9Smatthias.ringwald 
8018d213e1aSmatthias.ringwald     // close low-level device
8028d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
8038d213e1aSmatthias.ringwald 
8047b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
8059418f9c9Smatthias.ringwald 
8068d213e1aSmatthias.ringwald     // power off
8078d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
8088d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
8098d213e1aSmatthias.ringwald     }
8109418f9c9Smatthias.ringwald 
8117b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
8129418f9c9Smatthias.ringwald 
81372ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
81472ea5239Smatthias.ringwald }
81572ea5239Smatthias.ringwald 
81640d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
81772ea5239Smatthias.ringwald 
8187b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
8193144bce4Smatthias.ringwald 
820b429b9b7Smatthias.ringwald #if 0
821b429b9b7Smatthias.ringwald     // don't close serial port during sleep
822b429b9b7Smatthias.ringwald 
82372ea5239Smatthias.ringwald     // close low-level device
82472ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
825b429b9b7Smatthias.ringwald #endif
82672ea5239Smatthias.ringwald 
82772ea5239Smatthias.ringwald     // sleep mode
8283144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
82972ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
83072ea5239Smatthias.ringwald     }
831b429b9b7Smatthias.ringwald 
83272ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
8338d213e1aSmatthias.ringwald }
8348d213e1aSmatthias.ringwald 
83540d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
836b429b9b7Smatthias.ringwald 
8377b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
838b429b9b7Smatthias.ringwald 
839b429b9b7Smatthias.ringwald     // wake on
840b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
841b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
842b429b9b7Smatthias.ringwald     }
843b429b9b7Smatthias.ringwald 
844b429b9b7Smatthias.ringwald #if 0
845b429b9b7Smatthias.ringwald     // open low-level device
846b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
847b429b9b7Smatthias.ringwald     if (err){
8487d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
849b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
850b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
851b429b9b7Smatthias.ringwald         }
852b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
853b429b9b7Smatthias.ringwald         return err;
854b429b9b7Smatthias.ringwald     }
855b429b9b7Smatthias.ringwald #endif
856b429b9b7Smatthias.ringwald 
857b429b9b7Smatthias.ringwald     return 0;
858b429b9b7Smatthias.ringwald }
859b429b9b7Smatthias.ringwald 
860b429b9b7Smatthias.ringwald 
8618d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
862d661ed19Smatthias.ringwald 
8637b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
864d661ed19Smatthias.ringwald 
8658d213e1aSmatthias.ringwald     int err = 0;
8668d213e1aSmatthias.ringwald     switch (hci_stack.state){
8678d213e1aSmatthias.ringwald 
8688d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
8698d213e1aSmatthias.ringwald             switch (power_mode){
8708d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8718d213e1aSmatthias.ringwald                     err = hci_power_control_on();
8728d213e1aSmatthias.ringwald                     if (err) return err;
8737301ad89Smatthias.ringwald                     // set up state machine
8747301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
8757301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8767301ad89Smatthias.ringwald                     hci_stack.substate = 0;
8778d213e1aSmatthias.ringwald                     break;
8788d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8798d213e1aSmatthias.ringwald                     // do nothing
8808d213e1aSmatthias.ringwald                     break;
8818d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
882b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
8838d213e1aSmatthias.ringwald                     break;
8848d213e1aSmatthias.ringwald             }
8858d213e1aSmatthias.ringwald             break;
8867301ad89Smatthias.ringwald 
8878d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8888d213e1aSmatthias.ringwald             switch (power_mode){
8898d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8908d213e1aSmatthias.ringwald                     // do nothing
8918d213e1aSmatthias.ringwald                     break;
8928d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8938d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
8948d213e1aSmatthias.ringwald                     hci_power_control_off();
8958d213e1aSmatthias.ringwald                     break;
8968d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
897b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
89872ea5239Smatthias.ringwald                     hci_power_control_sleep();
8998d213e1aSmatthias.ringwald                     break;
9008d213e1aSmatthias.ringwald             }
9018d213e1aSmatthias.ringwald             break;
9027301ad89Smatthias.ringwald 
9038d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
9048d213e1aSmatthias.ringwald             switch (power_mode){
9058d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9068d213e1aSmatthias.ringwald                     // do nothing
9078d213e1aSmatthias.ringwald                     break;
9088d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
909c7e0c5f6Smatthias.ringwald                     // see hci_run
910c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9118d213e1aSmatthias.ringwald                     break;
9128d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
913b546ac54Smatthias.ringwald                     // see hci_run
914b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
91589db417bSmatthias.ringwald                     hci_stack.substate = 0;
9168d213e1aSmatthias.ringwald                     break;
9178d213e1aSmatthias.ringwald             }
9188d213e1aSmatthias.ringwald             break;
9197301ad89Smatthias.ringwald 
9208d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
9218d213e1aSmatthias.ringwald             switch (power_mode){
9228d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9238d213e1aSmatthias.ringwald                     // set up state machine
9248d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9258d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
9268d213e1aSmatthias.ringwald                     break;
9278d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9288d213e1aSmatthias.ringwald                     // do nothing
9298d213e1aSmatthias.ringwald                     break;
9308d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
931b546ac54Smatthias.ringwald                     // see hci_run
932b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
93389db417bSmatthias.ringwald                     hci_stack.substate = 0;
9348d213e1aSmatthias.ringwald                     break;
9358d213e1aSmatthias.ringwald             }
9368d213e1aSmatthias.ringwald             break;
9378d213e1aSmatthias.ringwald 
9388d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
9398d213e1aSmatthias.ringwald             switch (power_mode){
9408d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
94128171530Smatthias.ringwald 
94228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
94328171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
94428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9458747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
946*da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
94728171530Smatthias.ringwald                         break;
94828171530Smatthias.ringwald                     }
94928171530Smatthias.ringwald #endif
950b546ac54Smatthias.ringwald                     // set up state machine
951b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
952b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
953b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9548d213e1aSmatthias.ringwald                     break;
9558d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
956b546ac54Smatthias.ringwald                     // see hci_run
957b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9588d213e1aSmatthias.ringwald                     break;
9598d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
960b546ac54Smatthias.ringwald                     // do nothing
9618d213e1aSmatthias.ringwald                     break;
9628d213e1aSmatthias.ringwald             }
9638d213e1aSmatthias.ringwald             break;
9648d213e1aSmatthias.ringwald 
9658d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
9668d213e1aSmatthias.ringwald             switch (power_mode){
9678d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
96828171530Smatthias.ringwald 
96928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
97028171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
97128171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9728747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
973*da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
974758b46ceSmatthias.ringwald                         hci_update_scan_enable();
97528171530Smatthias.ringwald                         break;
97628171530Smatthias.ringwald                     }
97728171530Smatthias.ringwald #endif
9783144bce4Smatthias.ringwald                     err = hci_power_control_wake();
9793144bce4Smatthias.ringwald                     if (err) return err;
980b546ac54Smatthias.ringwald                     // set up state machine
981b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
982b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
983b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9848d213e1aSmatthias.ringwald                     break;
9858d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9864ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9878d213e1aSmatthias.ringwald                     break;
9888d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
989b546ac54Smatthias.ringwald                     // do nothing
9908d213e1aSmatthias.ringwald                     break;
9918d213e1aSmatthias.ringwald             }
9928d213e1aSmatthias.ringwald             break;
99311e23e5fSmatthias.ringwald     }
99468d92d03Smatthias.ringwald 
995038bc64cSmatthias.ringwald     // create internal event
996ee8bf225Smatthias.ringwald 	hci_emit_state();
997ee8bf225Smatthias.ringwald 
99868d92d03Smatthias.ringwald 	// trigger next/first action
99968d92d03Smatthias.ringwald 	hci_run();
100068d92d03Smatthias.ringwald 
1001475c8125Smatthias.ringwald     return 0;
1002475c8125Smatthias.ringwald }
1003475c8125Smatthias.ringwald 
1004758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1005758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1006758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1007758b46ceSmatthias.ringwald     hci_run();
1008758b46ceSmatthias.ringwald }
1009758b46ceSmatthias.ringwald 
1010381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1011381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1012381fbed8Smatthias.ringwald 
1013381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1014381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1015381fbed8Smatthias.ringwald         return;
1016381fbed8Smatthias.ringwald     }
1017381fbed8Smatthias.ringwald 
1018381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1019758b46ceSmatthias.ringwald     hci_update_scan_enable();
1020758b46ceSmatthias.ringwald }
1021b031bebbSmatthias.ringwald 
1022758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1023758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1024758b46ceSmatthias.ringwald 
1025758b46ceSmatthias.ringwald     // don't emit event
1026758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1027758b46ceSmatthias.ringwald 
1028758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1029758b46ceSmatthias.ringwald     hci_update_scan_enable();
1030381fbed8Smatthias.ringwald }
1031381fbed8Smatthias.ringwald 
103206b35ec0Smatthias.ringwald void hci_run(){
10338a485f27Smatthias.ringwald 
103432ab9390Smatthias.ringwald     hci_connection_t * connection;
103532ab9390Smatthias.ringwald     linked_item_t * it;
103632ab9390Smatthias.ringwald 
1037ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1038ce4c8fabSmatthias.ringwald 
1039b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1040b031bebbSmatthias.ringwald 
1041b031bebbSmatthias.ringwald     // decline incoming connections
1042ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1043ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1044ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1045ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1046ce4c8fabSmatthias.ringwald     }
1047ce4c8fabSmatthias.ringwald 
1048b031bebbSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1049b031bebbSmatthias.ringwald 
1050b031bebbSmatthias.ringwald     // send scan enable
105196774428Smatthias.ringwald     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff){
1052b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1053b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1054b031bebbSmatthias.ringwald     }
1055b031bebbSmatthias.ringwald 
105632ab9390Smatthias.ringwald     // send pending HCI commands
105732ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
105832ab9390Smatthias.ringwald 
1059b031bebbSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
106032ab9390Smatthias.ringwald 
1061b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
106232ab9390Smatthias.ringwald 
106332ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
10647b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
106532ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
106632ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1067c7e0c5f6Smatthias.ringwald         }
1068c7e0c5f6Smatthias.ringwald 
1069de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
107032ab9390Smatthias.ringwald 
107132ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
107232ab9390Smatthias.ringwald             link_key_t link_key;
10737b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
107432ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
107532ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
107632ab9390Smatthias.ringwald             } else {
107732ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
107832ab9390Smatthias.ringwald             }
107928ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
108032ab9390Smatthias.ringwald         }
108132ab9390Smatthias.ringwald     }
108232ab9390Smatthias.ringwald 
1083de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1084c7e0c5f6Smatthias.ringwald 
10853429f56bSmatthias.ringwald     switch (hci_stack.state){
10863429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10877b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
10883429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
10893429f56bSmatthias.ringwald                 // odd: waiting for command completion
109006b35ec0Smatthias.ringwald                 return;
10913429f56bSmatthias.ringwald             }
109290919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1093c7492964Smatthias.ringwald                 case 0: // RESET
109422909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
1095c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1096c7492964Smatthias.ringwald                         // skip baud change
1097c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1098c7492964Smatthias.ringwald                     }
10993429f56bSmatthias.ringwald                     break;
1100c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
11017dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
11027dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1103c7492964Smatthias.ringwald                     break;
1104c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1105c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1106c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1107c7492964Smatthias.ringwald                     // break missing here for fall through
1108c7492964Smatthias.ringwald 
1109c7492964Smatthias.ringwald                 case 3:
111090919203Smatthias.ringwald                     // custom initialization
1111db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
11127dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1113d62aca9fSmatthias.ringwald                         if (valid_cmd){
11147dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
11157dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1116c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
111790919203Smatthias.ringwald                             break;
111890919203Smatthias.ringwald                         }
1119d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
112090919203Smatthias.ringwald                     }
11212f6c30e1Smatthias.ringwald                     // otherwise continue
1122f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1123f432a6ddSmatthias.ringwald 					break;
1124c7492964Smatthias.ringwald 				case 4:
1125e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1126e2edc0c3Smatthias.ringwald 					break;
1127c7492964Smatthias.ringwald                 case 5:
11283429f56bSmatthias.ringwald                     // ca. 15 sec
11293429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
11303429f56bSmatthias.ringwald                     break;
1131c7492964Smatthias.ringwald 				case 6:
1132758b46ceSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
1133f432a6ddSmatthias.ringwald 					break;
1134c7492964Smatthias.ringwald                 case 7:
1135559e517eS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
1136559e517eS[email protected]                     break;
1137559e517eS[email protected]                 case 8:
11388a485f27Smatthias.ringwald #ifndef EMBEDDED
11398a485f27Smatthias.ringwald                 {
11408a485f27Smatthias.ringwald                     char hostname[30];
11418a485f27Smatthias.ringwald                     gethostname(hostname, 30);
11428a485f27Smatthias.ringwald                     hostname[29] = '\0';
11438a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
11448a485f27Smatthias.ringwald                     break;
11458a485f27Smatthias.ringwald                 }
1146559e517eS[email protected]                 case 9:
11473c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
11483c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
11493c4d4b90Smatthias.ringwald                     break;
11503c4d4b90Smatthias.ringwald 
1151559e517eS[email protected]                 case 10:
11523c4d4b90Smatthias.ringwald #endif
11538a485f27Smatthias.ringwald #endif
11543429f56bSmatthias.ringwald                     // done.
11553429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1156b360b6adSmatthias.ringwald                     hci_emit_state();
11573429f56bSmatthias.ringwald                     break;
11583429f56bSmatthias.ringwald                 default:
11593429f56bSmatthias.ringwald                     break;
1160475c8125Smatthias.ringwald             }
11613429f56bSmatthias.ringwald             hci_stack.substate++;
11623429f56bSmatthias.ringwald             break;
1163c7e0c5f6Smatthias.ringwald 
1164c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1165c7e0c5f6Smatthias.ringwald 
11667b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1167c7e0c5f6Smatthias.ringwald             // close all open connections
1168c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1169c7e0c5f6Smatthias.ringwald             if (connection){
117032ab9390Smatthias.ringwald 
1171c7e0c5f6Smatthias.ringwald                 // send disconnect
117232ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
117332ab9390Smatthias.ringwald 
117479bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11756ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1176c7e0c5f6Smatthias.ringwald 
1177c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1178c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1179c7e0c5f6Smatthias.ringwald                 return;
1180c7e0c5f6Smatthias.ringwald             }
11817b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1182c7e0c5f6Smatthias.ringwald 
118372ea5239Smatthias.ringwald             // switch mode
1184c7e0c5f6Smatthias.ringwald             hci_power_control_off();
11859418f9c9Smatthias.ringwald 
11867b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
118772ea5239Smatthias.ringwald             hci_emit_state();
11887b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
118972ea5239Smatthias.ringwald             break;
1190c7e0c5f6Smatthias.ringwald 
119172ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
119289db417bSmatthias.ringwald             switch(hci_stack.substate) {
119389db417bSmatthias.ringwald                 case 0:
11947b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
119572ea5239Smatthias.ringwald                     // close all open connections
119672ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
119766da7044Smatthias.ringwald 
119828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
119966da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
120066da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
120166da7044Smatthias.ringwald                         connection = NULL;
120266da7044Smatthias.ringwald                     }
120366da7044Smatthias.ringwald #endif
120472ea5239Smatthias.ringwald                     if (connection){
120532ab9390Smatthias.ringwald 
120672ea5239Smatthias.ringwald                         // send disconnect
120732ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
120832ab9390Smatthias.ringwald 
120979bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
12106ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
121172ea5239Smatthias.ringwald 
121272ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
121372ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
121472ea5239Smatthias.ringwald                         return;
121572ea5239Smatthias.ringwald                     }
121672ea5239Smatthias.ringwald 
121789db417bSmatthias.ringwald                     // disable page and inquiry scan
121832ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
121932ab9390Smatthias.ringwald 
1220758b46ceSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq cans\n");
1221758b46ceSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
122289db417bSmatthias.ringwald 
122389db417bSmatthias.ringwald                     // continue in next sub state
122489db417bSmatthias.ringwald                     hci_stack.substate++;
122589db417bSmatthias.ringwald                     break;
122689db417bSmatthias.ringwald                 case 1:
122789db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
122889db417bSmatthias.ringwald                     break;
122989db417bSmatthias.ringwald                 case 2:
12307b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
123128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
123228171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
123328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
123428171530Smatthias.ringwald                         // SLEEP MODE reached
123528171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
123628171530Smatthias.ringwald                         hci_emit_state();
123728171530Smatthias.ringwald                         break;
123828171530Smatthias.ringwald                     }
123928171530Smatthias.ringwald #endif
124072ea5239Smatthias.ringwald                     // switch mode
124189db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1242c7e0c5f6Smatthias.ringwald                     hci_emit_state();
124328171530Smatthias.ringwald                     break;
124428171530Smatthias.ringwald 
124589db417bSmatthias.ringwald                 default:
124689db417bSmatthias.ringwald                     break;
124789db417bSmatthias.ringwald             }
1248c7e0c5f6Smatthias.ringwald             break;
1249c7e0c5f6Smatthias.ringwald 
12503429f56bSmatthias.ringwald         default:
12513429f56bSmatthias.ringwald             break;
12521f504dbdSmatthias.ringwald     }
12533429f56bSmatthias.ringwald }
125416833f0aSmatthias.ringwald 
125531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1256c8e4258aSmatthias.ringwald     bd_addr_t addr;
1257c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1258c8e4258aSmatthias.ringwald     // house-keeping
1259c8e4258aSmatthias.ringwald 
1260c8e4258aSmatthias.ringwald     // create_connection?
1261c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1262c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1263339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1264c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1265c8e4258aSmatthias.ringwald         if (conn) {
1266c8e4258aSmatthias.ringwald             // if connection exists
1267c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
126817f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
126917f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1270c8e4258aSmatthias.ringwald             }
127117f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1272c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1273c8e4258aSmatthias.ringwald 
127417f1ba2aSmatthias.ringwald         }
1275c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
127617f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
127717f1ba2aSmatthias.ringwald         if (!conn){
127817f1ba2aSmatthias.ringwald             // notify client that alloc failed
127917f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
128017f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
128117f1ba2aSmatthias.ringwald         }
1282c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1283c8e4258aSmatthias.ringwald     }
1284c8e4258aSmatthias.ringwald 
12857fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
12867fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
12877fde4af9Smatthias.ringwald     }
12887fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
12897fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
12907fde4af9Smatthias.ringwald     }
12917fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
12927fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
12937fde4af9Smatthias.ringwald     }
12947fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
12957fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
12967fde4af9Smatthias.ringwald     }
12977fde4af9Smatthias.ringwald 
12988ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
12998ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
13008ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
13018ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
13028ef73945Smatthias.ringwald         }
13038ef73945Smatthias.ringwald     }
1304c8e4258aSmatthias.ringwald 
130531452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1306622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
130731452debSmatthias.ringwald }
13088adf0ddaSmatthias.ringwald 
13091cd208adSmatthias.ringwald /**
13101cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
13111cd208adSmatthias.ringwald  */
1312fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
13131cd208adSmatthias.ringwald     va_list argptr;
13141cd208adSmatthias.ringwald     va_start(argptr, cmd);
13157dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
13161cd208adSmatthias.ringwald     va_end(argptr);
13177dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
131893b8dc03Smatthias.ringwald }
1319c8e4258aSmatthias.ringwald 
1320ee091cf1Smatthias.ringwald // Create various non-HCI events.
1321ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1322ee091cf1Smatthias.ringwald 
1323c8e4258aSmatthias.ringwald void hci_emit_state(){
1324e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1325425d1371Smatthias.ringwald     uint8_t event[3];
132680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1327425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1328c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1329425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1330425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1331c8e4258aSmatthias.ringwald }
1332c8e4258aSmatthias.ringwald 
133317f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1334425d1371Smatthias.ringwald     uint8_t event[13];
1335c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1336425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
133717f1ba2aSmatthias.ringwald     event[2] = status;
1338c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1339c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1340c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1341c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1342425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1343425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1344c8e4258aSmatthias.ringwald }
1345c8e4258aSmatthias.ringwald 
13463c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1347425d1371Smatthias.ringwald     uint8_t event[6];
13483c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1349e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
13503c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
13513c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
13523c4d4b90Smatthias.ringwald     event[5] = reason;
1353425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1354425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13553c4d4b90Smatthias.ringwald }
13563c4d4b90Smatthias.ringwald 
1357ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1358e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1359425d1371Smatthias.ringwald     uint8_t event[4];
136080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1361425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1362ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1363425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1364425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1365ee091cf1Smatthias.ringwald }
136643bfb1bdSmatthias.ringwald 
136743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1368e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1369425d1371Smatthias.ringwald     uint8_t event[3];
137080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1371425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
137243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1373425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1374425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
137543bfb1bdSmatthias.ringwald }
1376038bc64cSmatthias.ringwald 
1377038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1378e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1379425d1371Smatthias.ringwald     uint8_t event[2];
138080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1381425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1382425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1383425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1384038bc64cSmatthias.ringwald }
13851b0e3922Smatthias.ringwald 
138609ba8edeSmatthias.ringwald #ifndef EMBEDDED
13871b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1388e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1389425d1371Smatthias.ringwald     uint8_t event[6];
13901b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1391425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1392425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1393425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1394425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1395425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1396425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13971b0e3922Smatthias.ringwald }
139809ba8edeSmatthias.ringwald #endif
13991b0e3922Smatthias.ringwald 
14002ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1401e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1402425d1371Smatthias.ringwald     uint8_t event[3];
14032ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1404425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
14052ed6235cSmatthias.ringwald     event[2] = enabled;
1406425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1407e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
14082ed6235cSmatthias.ringwald }
1409627c2f45Smatthias.ringwald 
1410627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1411e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1412627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1413e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1414f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
14152f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1416f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1417e0abb8e7S[email protected] 
1418e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1419e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1420e0abb8e7S[email protected] 
1421e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1422e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1423627c2f45Smatthias.ringwald }
1424381fbed8Smatthias.ringwald 
1425381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1426e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1427425d1371Smatthias.ringwald     uint8_t event[3];
1428381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1429425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1430381fbed8Smatthias.ringwald     event[2] = enabled;
1431425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1432425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1433381fbed8Smatthias.ringwald }
1434