xref: /btstack/src/hci.c (revision 65389bfc791fb92815dec44656edcb92007912b8)
11f504dbdSmatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
381f504dbdSmatthias.ringwald  *  hci.c
391f504dbdSmatthias.ringwald  *
401f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
411f504dbdSmatthias.ringwald  *
421f504dbdSmatthias.ringwald  */
431f504dbdSmatthias.ringwald 
44c8901d41Smatthias.ringwald #include "config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
477f2435e6Smatthias.ringwald 
4893b8dc03Smatthias.ringwald #include <stdarg.h>
4993b8dc03Smatthias.ringwald #include <string.h>
5056fe0872Smatthias.ringwald #include <stdio.h>
517f2435e6Smatthias.ringwald 
52549e6ebeSmatthias.ringwald #ifndef EMBEDDED
53549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5409ba8edeSmatthias.ringwald #include <btstack/version.h>
55549e6ebeSmatthias.ringwald #endif
56549e6ebeSmatthias.ringwald 
57a3b02b71Smatthias.ringwald #include "btstack_memory.h"
587f2435e6Smatthias.ringwald #include "debug.h"
59d8905019Smatthias.ringwald #include "hci_dump.h"
6093b8dc03Smatthias.ringwald 
61ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
621b0e3922Smatthias.ringwald 
63169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
64ee091cf1Smatthias.ringwald 
65a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
66da5275c5S[email protected] 
6728171530Smatthias.ringwald #ifdef USE_BLUETOOL
6828171530Smatthias.ringwald #include "bt_control_iphone.h"
6928171530Smatthias.ringwald #endif
7028171530Smatthias.ringwald 
71758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
72758b46ceSmatthias.ringwald 
7306b35ec0Smatthias.ringwald // the STACK is here
7416833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
7516833f0aSmatthias.ringwald 
7697addcc5Smatthias.ringwald /**
77ee091cf1Smatthias.ringwald  * get connection for a given handle
78ee091cf1Smatthias.ringwald  *
79ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
80ee091cf1Smatthias.ringwald  */
81645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
82ee091cf1Smatthias.ringwald     linked_item_t *it;
83ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
84ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
85ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
86ee091cf1Smatthias.ringwald         }
87ee091cf1Smatthias.ringwald     }
88ee091cf1Smatthias.ringwald     return NULL;
89ee091cf1Smatthias.ringwald }
90ee091cf1Smatthias.ringwald 
912b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
9228ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
93c785ef68Smatthias.ringwald #ifdef HAVE_TIME
94ee091cf1Smatthias.ringwald     struct timeval tv;
95ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
96c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
97ee091cf1Smatthias.ringwald         // connections might be timed out
98ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
99ee091cf1Smatthias.ringwald     }
1002b12a0b9Smatthias.ringwald #endif
101e5780900Smatthias.ringwald #ifdef HAVE_TICK
102c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
103c785ef68Smatthias.ringwald         // connections might be timed out
104c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
105c785ef68Smatthias.ringwald     }
106c785ef68Smatthias.ringwald #endif
107c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
108c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
109c785ef68Smatthias.ringwald }
110ee091cf1Smatthias.ringwald 
111ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
112c7492964Smatthias.ringwald #ifdef HAVE_TIME
113ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
114c7492964Smatthias.ringwald #endif
115e5780900Smatthias.ringwald #ifdef HAVE_TICK
116c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
117c785ef68Smatthias.ringwald #endif
118ee091cf1Smatthias.ringwald }
119ee091cf1Smatthias.ringwald 
120ee091cf1Smatthias.ringwald /**
121c8e4258aSmatthias.ringwald  * create connection for given address
122c8e4258aSmatthias.ringwald  *
12317f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
124c8e4258aSmatthias.ringwald  */
125c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
12628ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
127c8e4258aSmatthias.ringwald     if (!conn) return NULL;
128c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
129c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1307d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
131ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
132ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
133ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
134d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1357856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
13656cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
137c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
138c8e4258aSmatthias.ringwald     return conn;
139c8e4258aSmatthias.ringwald }
140c8e4258aSmatthias.ringwald 
141c8e4258aSmatthias.ringwald /**
14206b35ec0Smatthias.ringwald  * get connection for given address
14397addcc5Smatthias.ringwald  *
14497addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
14597addcc5Smatthias.ringwald  */
146fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
14706b35ec0Smatthias.ringwald     linked_item_t *it;
14806b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
14906b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
15006b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
15106b35ec0Smatthias.ringwald         }
15206b35ec0Smatthias.ringwald     }
15306b35ec0Smatthias.ringwald     return NULL;
15406b35ec0Smatthias.ringwald }
15506b35ec0Smatthias.ringwald 
15628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
15728ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
15828ca2b46S[email protected] }
15928ca2b46S[email protected] 
16028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
16128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
16228ca2b46S[email protected] }
16328ca2b46S[email protected] 
16428ca2b46S[email protected] 
16543bfb1bdSmatthias.ringwald /**
16680ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1677fde4af9Smatthias.ringwald  */
1687fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1697fde4af9Smatthias.ringwald     bd_addr_t addr;
1707fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1717fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1727fde4af9Smatthias.ringwald     if (conn) {
17328ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
17480ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1757fde4af9Smatthias.ringwald     }
1767fde4af9Smatthias.ringwald }
1777fde4af9Smatthias.ringwald 
17880ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
17980ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
18080ca58a0Smatthias.ringwald     if (!conn) return 0;
18180ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
18280ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
18380ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
18480ca58a0Smatthias.ringwald     return 1;
18580ca58a0Smatthias.ringwald }
18680ca58a0Smatthias.ringwald 
187c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
188c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
189c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
190c12e46e7Smatthias.ringwald     }
191c12e46e7Smatthias.ringwald }
192c12e46e7Smatthias.ringwald 
1937fde4af9Smatthias.ringwald 
1947fde4af9Smatthias.ringwald /**
19543bfb1bdSmatthias.ringwald  * count connections
19643bfb1bdSmatthias.ringwald  */
19740d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
19856c253c9Smatthias.ringwald     int count = 0;
19943bfb1bdSmatthias.ringwald     linked_item_t *it;
20043bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
20143bfb1bdSmatthias.ringwald     return count;
20243bfb1bdSmatthias.ringwald }
203c8e4258aSmatthias.ringwald 
20497addcc5Smatthias.ringwald /**
205ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
20616833f0aSmatthias.ringwald  */
2072718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
20816833f0aSmatthias.ringwald }
20916833f0aSmatthias.ringwald 
210998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
211998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
212998906cdSmatthias.ringwald     if (!connection) {
2137d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
214998906cdSmatthias.ringwald         return 0;
215998906cdSmatthias.ringwald     }
216998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
217998906cdSmatthias.ringwald }
218998906cdSmatthias.ringwald 
219998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
220998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
221998906cdSmatthias.ringwald     linked_item_t *it;
222998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
223998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
224998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2257d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
226998906cdSmatthias.ringwald             return 0;
227998906cdSmatthias.ringwald         }
228998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
229998906cdSmatthias.ringwald     }
230998906cdSmatthias.ringwald     return free_slots;
231998906cdSmatthias.ringwald }
232998906cdSmatthias.ringwald 
233c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2342b12a0b9Smatthias.ringwald 
2352b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2362b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2372b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2382b12a0b9Smatthias.ringwald             return 0;
2392b12a0b9Smatthias.ringwald         }
2402b12a0b9Smatthias.ringwald     }
2412b12a0b9Smatthias.ringwald 
2422b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
243c24735b1Smatthias.ringwald     switch (packet_type) {
244c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
245c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
246c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
247de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
248c24735b1Smatthias.ringwald         default:
249c24735b1Smatthias.ringwald             return 0;
250c24735b1Smatthias.ringwald     }
251c24735b1Smatthias.ringwald }
252c24735b1Smatthias.ringwald 
253ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2547856c818Smatthias.ringwald 
2556218e6f1Smatthias.ringwald     // check for free places on BT module
2565932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2576218e6f1Smatthias.ringwald 
2587856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2597856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
26056cf178bSmatthias.ringwald     if (!connection) return 0;
26156cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
26256cf178bSmatthias.ringwald 
26356cf178bSmatthias.ringwald     // count packet
26456cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2657b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2667856c818Smatthias.ringwald 
26700d8e42eSmatthias.ringwald     // send packet
26800d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2696218e6f1Smatthias.ringwald 
27000d8e42eSmatthias.ringwald     return err;
271ee091cf1Smatthias.ringwald }
272ee091cf1Smatthias.ringwald 
27316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2747856c818Smatthias.ringwald 
2757856c818Smatthias.ringwald     // get info
2767856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2777856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2787856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2797856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2807856c818Smatthias.ringwald 
2817856c818Smatthias.ringwald     // ignore non-registered handle
2827856c818Smatthias.ringwald     if (!conn){
2837d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2847856c818Smatthias.ringwald         return;
2857856c818Smatthias.ringwald     }
2867856c818Smatthias.ringwald 
2877856c818Smatthias.ringwald     // update idle timestamp
2887856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2897856c818Smatthias.ringwald 
2907856c818Smatthias.ringwald     // handle different packet types
2917856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2927856c818Smatthias.ringwald 
2937856c818Smatthias.ringwald         case 0x01: // continuation fragment
2947856c818Smatthias.ringwald 
2957856c818Smatthias.ringwald             // sanity check
2967856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2977d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2987856c818Smatthias.ringwald                 return;
2997856c818Smatthias.ringwald             }
3007856c818Smatthias.ringwald 
3017856c818Smatthias.ringwald             // append fragment payload (header already stored)
3027856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3037856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3047856c818Smatthias.ringwald 
3057d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
306decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3077856c818Smatthias.ringwald 
3087856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3097856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3107856c818Smatthias.ringwald 
3112718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3127856c818Smatthias.ringwald                 // reset recombination buffer
3137856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3147856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3157856c818Smatthias.ringwald             }
3167856c818Smatthias.ringwald             break;
3177856c818Smatthias.ringwald 
3187856c818Smatthias.ringwald         case 0x02: { // first fragment
3197856c818Smatthias.ringwald 
3207856c818Smatthias.ringwald             // sanity check
3217856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3227d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3237856c818Smatthias.ringwald                 return;
3247856c818Smatthias.ringwald             }
3257856c818Smatthias.ringwald 
3267856c818Smatthias.ringwald             // peek into L2CAP packet!
3277856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3287856c818Smatthias.ringwald 
3297d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
330decc01a8Smatthias.ringwald 
3317856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3327856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3337856c818Smatthias.ringwald 
3347856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3352718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3367856c818Smatthias.ringwald 
3377856c818Smatthias.ringwald             } else {
3387856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3397856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3407856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3417856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
342decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3437856c818Smatthias.ringwald             }
3447856c818Smatthias.ringwald             break;
3457856c818Smatthias.ringwald 
3467856c818Smatthias.ringwald         }
3477856c818Smatthias.ringwald         default:
3487d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3497856c818Smatthias.ringwald             return;
3507856c818Smatthias.ringwald     }
35194ab26f8Smatthias.ringwald 
35294ab26f8Smatthias.ringwald     // execute main loop
35394ab26f8Smatthias.ringwald     hci_run();
35416833f0aSmatthias.ringwald }
35522909952Smatthias.ringwald 
35667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
357339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3583c4d4b90Smatthias.ringwald 
3593c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3603c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3613c4d4b90Smatthias.ringwald 
362c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
363c785ef68Smatthias.ringwald 
364c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
365a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3663c4d4b90Smatthias.ringwald 
3673c4d4b90Smatthias.ringwald     // now it's gone
368c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
369c7e0c5f6Smatthias.ringwald }
370c7e0c5f6Smatthias.ringwald 
3710c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3728f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3738f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3748f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3758f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3768f8108aaSmatthias.ringwald };
377*65389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
378*65389bfcS[email protected]      0, // 3 slot packets
379*65389bfcS[email protected]      1, // 5 slot packets
380*65389bfcS[email protected]     25, // EDR 2 mpbs
381*65389bfcS[email protected]     26, // EDR 3 mbps
382*65389bfcS[email protected]     39, // 3 slot EDR packts
383*65389bfcS[email protected]     40, // 5 slot EDR packet
384*65389bfcS[email protected] };
385*65389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
386*65389bfcS[email protected]     0x0f00, // 3 slot packets
387*65389bfcS[email protected]     0xf000, // 5 slot packets
388*65389bfcS[email protected]     0x1102, // EDR 2 mpbs
389*65389bfcS[email protected]     0x2204, // EDR 3 mbps
390*65389bfcS[email protected]     0x0300, // 3 slot EDR packts
391*65389bfcS[email protected]     0x3000, // 5 slot EDR packet
392*65389bfcS[email protected] };
3938f8108aaSmatthias.ringwald 
394*65389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
395*65389bfcS[email protected]     // enable packet types based on size
3968f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
3978f8108aaSmatthias.ringwald     int i;
3988f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
3998f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4008f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4018f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4028f8108aaSmatthias.ringwald         }
4038f8108aaSmatthias.ringwald     }
404*65389bfcS[email protected]     // disable packet types due to missing local supported features
405*65389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
406*65389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
407*65389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
408*65389bfcS[email protected]         if (feature_set) continue;
409*65389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
410*65389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
411*65389bfcS[email protected]     }
4128f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4138f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4148f8108aaSmatthias.ringwald     return packet_types;
4158f8108aaSmatthias.ringwald }
4168f8108aaSmatthias.ringwald 
4178f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4188f8108aaSmatthias.ringwald     return hci_stack.packet_types;
4198f8108aaSmatthias.ringwald }
4208f8108aaSmatthias.ringwald 
4217dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4227dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4237dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
4247dc17943Smatthias.ringwald }
4257dc17943Smatthias.ringwald 
4267dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
4277dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
4287dc17943Smatthias.ringwald }
4297dc17943Smatthias.ringwald 
43074ec757aSmatthias.ringwald // avoid huge local variables
431223aafc1Smatthias.ringwald #ifndef EMBEDDED
43274ec757aSmatthias.ringwald static device_name_t device_name;
433223aafc1Smatthias.ringwald #endif
43416833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
4351281a47eSmatthias.ringwald     bd_addr_t addr;
4362d00edd4Smatthias.ringwald     uint8_t link_type;
437fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
4381f7b95a1Smatthias.ringwald     hci_connection_t * conn;
43956cf178bSmatthias.ringwald     int i;
44022909952Smatthias.ringwald 
4415909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4425909f7f2Smatthias.ringwald 
4436772a24cSmatthias.ringwald     switch (packet[0]) {
44422909952Smatthias.ringwald 
4456772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4467ec5eeaaSmatthias.ringwald             // get num cmd packets
4477b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4487ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4497ec5eeaaSmatthias.ringwald 
450e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
451e2edc0c3Smatthias.ringwald                 // from offset 5
452e2edc0c3Smatthias.ringwald                 // status
4531d279b20Smatthias.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"
454e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
45556cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
456e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
45756cf178bSmatthias.ringwald                 // ignore: total num SCO packets
45856cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
459a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
4608fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
4618fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
4628f8108aaSmatthias.ringwald                     }
463*65389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
464*65389bfcS[email protected]                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
465e2edc0c3Smatthias.ringwald                 }
46656cf178bSmatthias.ringwald             }
467188981d2Smatthias.ringwald             // Dump local address
468188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
469e2386ba1S[email protected]                 bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
470188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
471e2386ba1S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr));
472188981d2Smatthias.ringwald             }
473381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
474381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
475381fbed8Smatthias.ringwald             }
476*65389bfcS[email protected]             // Note: HCI init checks
477559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
478*65389bfcS[email protected]                 memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
479559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
480559e517eS[email protected]                     hci_stack.local_supported_features[0], hci_stack.local_supported_features[1],
481559e517eS[email protected]                     hci_stack.local_supported_features[2], hci_stack.local_supported_features[3],
482559e517eS[email protected]                     hci_stack.local_supported_features[4], hci_stack.local_supported_features[5],
483559e517eS[email protected]                     hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]);
484*65389bfcS[email protected] 
485*65389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
486*65389bfcS[email protected]                 hci_stack.packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(hci_stack.acl_data_packet_length, &hci_stack.local_supported_features[0]);
487*65389bfcS[email protected]                 log_info("packet types %04x\n", hci_stack.packet_types);
488559e517eS[email protected]             }
48956cf178bSmatthias.ringwald             break;
49056cf178bSmatthias.ringwald 
4917ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4927ec5eeaaSmatthias.ringwald             // get num cmd packets
4937b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4947ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4957ec5eeaaSmatthias.ringwald             break;
4967ec5eeaaSmatthias.ringwald 
49756cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
49856cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
49956cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
50056cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
50156cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
50256cf178bSmatthias.ringwald                 if (!conn){
5037d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
50456cf178bSmatthias.ringwald                     continue;
50556cf178bSmatthias.ringwald                 }
50656cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5077b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
50856cf178bSmatthias.ringwald             }
5096772a24cSmatthias.ringwald             break;
5106772a24cSmatthias.ringwald 
5111f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
51237eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
51337eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5142d00edd4Smatthias.ringwald             link_type = packet[11];
515339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
51637eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5171f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5181f7b95a1Smatthias.ringwald                 if (!conn) {
5191f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5201f7b95a1Smatthias.ringwald                 }
521ce4c8fabSmatthias.ringwald                 if (!conn) {
522ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
523ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
524ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
525ce4c8fabSmatthias.ringwald                     break;
526ce4c8fabSmatthias.ringwald                 }
52732ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
52832ab9390Smatthias.ringwald                 hci_run();
52937eaa4cfSmatthias.ringwald             } else {
530ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
531ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
532ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
53337eaa4cfSmatthias.ringwald             }
5341f7b95a1Smatthias.ringwald             break;
5351f7b95a1Smatthias.ringwald 
5366772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
537fe1ed1b8Smatthias.ringwald             // Connection management
538fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
539339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
5401f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
541fe1ed1b8Smatthias.ringwald             if (conn) {
542b448a0e7Smatthias.ringwald                 if (!packet[2]){
543c8e4258aSmatthias.ringwald                     conn->state = OPEN;
544fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
545ee091cf1Smatthias.ringwald 
546c785ef68Smatthias.ringwald                     // restart timer
547c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
548ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
549c785ef68Smatthias.ringwald 
550339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
55143bfb1bdSmatthias.ringwald 
55243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
553b448a0e7Smatthias.ringwald                 } else {
554b448a0e7Smatthias.ringwald                     // connection failed, remove entry
555b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
556a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
557c12e46e7Smatthias.ringwald 
558c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
559c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
560c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
561c12e46e7Smatthias.ringwald                     }
562fe1ed1b8Smatthias.ringwald                 }
563fe1ed1b8Smatthias.ringwald             }
5646772a24cSmatthias.ringwald             break;
565fe1ed1b8Smatthias.ringwald 
5667fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5677b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5687fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
56974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
57032ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
57164472d52Smatthias.ringwald             hci_run();
572d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
57329d53098Smatthias.ringwald             return;
5747fde4af9Smatthias.ringwald 
5757fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5767fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
57774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
57829d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
579287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
58029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5817fde4af9Smatthias.ringwald             break;
5827fde4af9Smatthias.ringwald 
5837fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5847fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
585d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
586d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
587d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
588d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
5897fde4af9Smatthias.ringwald             break;
5907fde4af9Smatthias.ringwald 
5911d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
5921d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
5931d6b20aeS[email protected]             if (hci_stack.ssp_io_capability == SSP_IO_CAPABILITY_UNKNOWN) break;
594dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
595dbe1a790S[email protected]             break;
596dbe1a790S[email protected] 
597dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
598dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_CONFIRM_REQUEST);
599dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
600dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
601dbe1a790S[email protected]             break;
602dbe1a790S[email protected] 
603dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
604dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_PASSKEY_REQUEST);
605dbe1a790S[email protected]             if (!hci_stack.ssp_auto_accept) break;
606dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
6071d6b20aeS[email protected]             break;
6081d6b20aeS[email protected] 
609223aafc1Smatthias.ringwald #ifndef EMBEDDED
61074ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
61174ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
61274ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
61374ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
6145a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
6155a06394aSmatthias.ringwald             for (i=0; i<248;i++){
6165a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
6175a06394aSmatthias.ringwald                     packet[9+i] = 0;
6185a06394aSmatthias.ringwald                     break;
619cdc9101dSmatthias.ringwald                 }
6205a06394aSmatthias.ringwald             }
621d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
62274ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
62374ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
62474ec757aSmatthias.ringwald             break;
62574ec757aSmatthias.ringwald 
62674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
62774ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
62874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
62974ec757aSmatthias.ringwald             // first send inq result packet
63074ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
63174ec757aSmatthias.ringwald             // then send cached remote names
63274ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
63374ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
63474ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
63574ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
63674ec757aSmatthias.ringwald                 }
63774ec757aSmatthias.ringwald             }
63874ec757aSmatthias.ringwald             return;
639223aafc1Smatthias.ringwald #endif
64074ec757aSmatthias.ringwald 
6416772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
642fe1ed1b8Smatthias.ringwald             if (!packet[2]){
643fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
644fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
645fe1ed1b8Smatthias.ringwald                 if (conn) {
646c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
647fe1ed1b8Smatthias.ringwald                 }
648fe1ed1b8Smatthias.ringwald             }
6496772a24cSmatthias.ringwald             break;
6506772a24cSmatthias.ringwald 
651c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
652c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
653c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
654c68bdf90Smatthias.ringwald             }
655c68bdf90Smatthias.ringwald             break;
656c68bdf90Smatthias.ringwald 
6575909f7f2Smatthias.ringwald #ifdef HAVE_BLE
6585909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
6595909f7f2Smatthias.ringwald             switch (packet[2]) {
6605909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
6615909f7f2Smatthias.ringwald                     // Connection management
6625909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
6635909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
6645909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
6655909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
6665909f7f2Smatthias.ringwald                     if (packet[3]){
6675909f7f2Smatthias.ringwald                         if (conn){
6685909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
6695909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
6705909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
6715909f7f2Smatthias.ringwald 
6725909f7f2Smatthias.ringwald                         }
6735909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
6745909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
6755909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
6765909f7f2Smatthias.ringwald                         }
6775909f7f2Smatthias.ringwald                         break;
6785909f7f2Smatthias.ringwald                     }
6795909f7f2Smatthias.ringwald                     if (!conn){
6805909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
6815909f7f2Smatthias.ringwald                     }
6825909f7f2Smatthias.ringwald                     if (!conn){
6835909f7f2Smatthias.ringwald                         // no memory
6845909f7f2Smatthias.ringwald                         break;
6855909f7f2Smatthias.ringwald                     }
6865909f7f2Smatthias.ringwald 
6875909f7f2Smatthias.ringwald                     conn->state = OPEN;
6885909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
6895909f7f2Smatthias.ringwald 
6905909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
6915909f7f2Smatthias.ringwald 
6925909f7f2Smatthias.ringwald                     // restart timer
6935909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
6945909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
6955909f7f2Smatthias.ringwald 
6965909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
6975909f7f2Smatthias.ringwald 
6985909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
6995909f7f2Smatthias.ringwald                     break;
7005909f7f2Smatthias.ringwald 
7015909f7f2Smatthias.ringwald                 default:
7025909f7f2Smatthias.ringwald                     break;
7035909f7f2Smatthias.ringwald             }
7045909f7f2Smatthias.ringwald             break;
7055909f7f2Smatthias.ringwald #endif
7065909f7f2Smatthias.ringwald 
7076772a24cSmatthias.ringwald         default:
7086772a24cSmatthias.ringwald             break;
709fe1ed1b8Smatthias.ringwald     }
710fe1ed1b8Smatthias.ringwald 
7113429f56bSmatthias.ringwald     // handle BT initialization
7123429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
7137301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
7147301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
7157301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
7167301ad89Smatthias.ringwald         // }
7177301ad89Smatthias.ringwald         // handle normal init sequence
7183429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
7193429f56bSmatthias.ringwald             // odd: waiting for event
720f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
7213429f56bSmatthias.ringwald                 hci_stack.substate++;
7223429f56bSmatthias.ringwald             }
7233429f56bSmatthias.ringwald         }
72422909952Smatthias.ringwald     }
72522909952Smatthias.ringwald 
72689db417bSmatthias.ringwald     // help with BT sleep
72789db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
72889db417bSmatthias.ringwald         && hci_stack.substate == 1
72989db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
73089db417bSmatthias.ringwald         hci_stack.substate++;
73189db417bSmatthias.ringwald     }
73289db417bSmatthias.ringwald 
7332718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
73494ab26f8Smatthias.ringwald 
73594ab26f8Smatthias.ringwald 	// execute main loop
73694ab26f8Smatthias.ringwald 	hci_run();
73716833f0aSmatthias.ringwald }
73816833f0aSmatthias.ringwald 
7390a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
74010e830c9Smatthias.ringwald     switch (packet_type) {
74110e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
74210e830c9Smatthias.ringwald             event_handler(packet, size);
74310e830c9Smatthias.ringwald             break;
74410e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
74510e830c9Smatthias.ringwald             acl_handler(packet, size);
74610e830c9Smatthias.ringwald             break;
74710e830c9Smatthias.ringwald         default:
74810e830c9Smatthias.ringwald             break;
74910e830c9Smatthias.ringwald     }
75010e830c9Smatthias.ringwald }
75110e830c9Smatthias.ringwald 
752fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
7532718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
7542718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
75516833f0aSmatthias.ringwald }
75616833f0aSmatthias.ringwald 
7574f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
758475c8125Smatthias.ringwald 
759475c8125Smatthias.ringwald     // reference to use transport layer implementation
76016833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
761475c8125Smatthias.ringwald 
76211e23e5fSmatthias.ringwald     // references to used control implementation
76311e23e5fSmatthias.ringwald     hci_stack.control = control;
76411e23e5fSmatthias.ringwald 
76511e23e5fSmatthias.ringwald     // reference to used config
76611e23e5fSmatthias.ringwald     hci_stack.config = config;
76711e23e5fSmatthias.ringwald 
768fe1ed1b8Smatthias.ringwald     // no connections yet
769fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
7700a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
771c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
772758b46ceSmatthias.ringwald 
773b031bebbSmatthias.ringwald     // no pending cmds
774b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
775b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
776b031bebbSmatthias.ringwald 
77716833f0aSmatthias.ringwald     // higher level handler
7782718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
77916833f0aSmatthias.ringwald 
780404843c1Smatthias.ringwald     // store and open remote device db
781404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
782404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
783404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
784404843c1Smatthias.ringwald     }
78529d53098Smatthias.ringwald 
7868fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
7878fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7888fcba05dSmatthias.ringwald 
78916833f0aSmatthias.ringwald     // register packet handlers with transport
79010e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
791f5454fc6Smatthias.ringwald 
792f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
793e2386ba1S[email protected] 
794e2386ba1S[email protected]     // class of device
795e2386ba1S[email protected]     hci_stack.class_of_device = 0x007a020c; // Smartphone
796a45d6b9fS[email protected] 
797a45d6b9fS[email protected]     // Secure Simple Pairing
798a45d6b9fS[email protected]     hci_stack.ssp_enable = 0;
799a45d6b9fS[email protected]     hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_UNKNOWN;
800a45d6b9fS[email protected]     hci_stack.ssp_authentication_requirement = 0;
801475c8125Smatthias.ringwald }
802475c8125Smatthias.ringwald 
803404843c1Smatthias.ringwald void hci_close(){
804404843c1Smatthias.ringwald     // close remote device db
805404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
806404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
807404843c1Smatthias.ringwald     }
808f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
809f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
810f5454fc6Smatthias.ringwald }
811f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
812404843c1Smatthias.ringwald }
813404843c1Smatthias.ringwald 
8148d213e1aSmatthias.ringwald // State-Module-Driver overview
8158d213e1aSmatthias.ringwald // state                    module  low-level
8168d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
8178d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
8188d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
8198d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
820d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
821d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
822c7e0c5f6Smatthias.ringwald 
82340d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
8247301ad89Smatthias.ringwald 
825038bc64cSmatthias.ringwald     // power on
826f9a30166Smatthias.ringwald     int err = 0;
827f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
828f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
829f9a30166Smatthias.ringwald     }
830038bc64cSmatthias.ringwald     if (err){
8317d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
832038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
833038bc64cSmatthias.ringwald         return err;
834038bc64cSmatthias.ringwald     }
835038bc64cSmatthias.ringwald 
836038bc64cSmatthias.ringwald     // open low-level device
837038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
838038bc64cSmatthias.ringwald     if (err){
8397d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
840f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
841f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
842f9a30166Smatthias.ringwald         }
843038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
844038bc64cSmatthias.ringwald         return err;
845038bc64cSmatthias.ringwald     }
8468d213e1aSmatthias.ringwald     return 0;
8478d213e1aSmatthias.ringwald }
848038bc64cSmatthias.ringwald 
84940d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
8508d213e1aSmatthias.ringwald 
8517b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
8529418f9c9Smatthias.ringwald 
8538d213e1aSmatthias.ringwald     // close low-level device
8548d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
8558d213e1aSmatthias.ringwald 
8567b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
8579418f9c9Smatthias.ringwald 
8588d213e1aSmatthias.ringwald     // power off
8598d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
8608d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
8618d213e1aSmatthias.ringwald     }
8629418f9c9Smatthias.ringwald 
8637b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
8649418f9c9Smatthias.ringwald 
86572ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
86672ea5239Smatthias.ringwald }
86772ea5239Smatthias.ringwald 
86840d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
86972ea5239Smatthias.ringwald 
8707b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
8713144bce4Smatthias.ringwald 
872b429b9b7Smatthias.ringwald #if 0
873b429b9b7Smatthias.ringwald     // don't close serial port during sleep
874b429b9b7Smatthias.ringwald 
87572ea5239Smatthias.ringwald     // close low-level device
87672ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
877b429b9b7Smatthias.ringwald #endif
87872ea5239Smatthias.ringwald 
87972ea5239Smatthias.ringwald     // sleep mode
8803144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
88172ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
88272ea5239Smatthias.ringwald     }
883b429b9b7Smatthias.ringwald 
88472ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
8858d213e1aSmatthias.ringwald }
8868d213e1aSmatthias.ringwald 
88740d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
888b429b9b7Smatthias.ringwald 
8897b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
890b429b9b7Smatthias.ringwald 
891b429b9b7Smatthias.ringwald     // wake on
892b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
893b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
894b429b9b7Smatthias.ringwald     }
895b429b9b7Smatthias.ringwald 
896b429b9b7Smatthias.ringwald #if 0
897b429b9b7Smatthias.ringwald     // open low-level device
898b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
899b429b9b7Smatthias.ringwald     if (err){
9007d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
901b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
902b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
903b429b9b7Smatthias.ringwald         }
904b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
905b429b9b7Smatthias.ringwald         return err;
906b429b9b7Smatthias.ringwald     }
907b429b9b7Smatthias.ringwald #endif
908b429b9b7Smatthias.ringwald 
909b429b9b7Smatthias.ringwald     return 0;
910b429b9b7Smatthias.ringwald }
911b429b9b7Smatthias.ringwald 
912b429b9b7Smatthias.ringwald 
9138d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
914d661ed19Smatthias.ringwald 
9157b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
916d661ed19Smatthias.ringwald 
9178d213e1aSmatthias.ringwald     int err = 0;
9188d213e1aSmatthias.ringwald     switch (hci_stack.state){
9198d213e1aSmatthias.ringwald 
9208d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
9218d213e1aSmatthias.ringwald             switch (power_mode){
9228d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9238d213e1aSmatthias.ringwald                     err = hci_power_control_on();
9248d213e1aSmatthias.ringwald                     if (err) return err;
9257301ad89Smatthias.ringwald                     // set up state machine
9267301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
9277301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9287301ad89Smatthias.ringwald                     hci_stack.substate = 0;
9298d213e1aSmatthias.ringwald                     break;
9308d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9318d213e1aSmatthias.ringwald                     // do nothing
9328d213e1aSmatthias.ringwald                     break;
9338d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
934b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
9358d213e1aSmatthias.ringwald                     break;
9368d213e1aSmatthias.ringwald             }
9378d213e1aSmatthias.ringwald             break;
9387301ad89Smatthias.ringwald 
9398d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9408d213e1aSmatthias.ringwald             switch (power_mode){
9418d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9428d213e1aSmatthias.ringwald                     // do nothing
9438d213e1aSmatthias.ringwald                     break;
9448d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9458d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
9468d213e1aSmatthias.ringwald                     hci_power_control_off();
9478d213e1aSmatthias.ringwald                     break;
9488d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
949b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
95072ea5239Smatthias.ringwald                     hci_power_control_sleep();
9518d213e1aSmatthias.ringwald                     break;
9528d213e1aSmatthias.ringwald             }
9538d213e1aSmatthias.ringwald             break;
9547301ad89Smatthias.ringwald 
9558d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
9568d213e1aSmatthias.ringwald             switch (power_mode){
9578d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9588d213e1aSmatthias.ringwald                     // do nothing
9598d213e1aSmatthias.ringwald                     break;
9608d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
961c7e0c5f6Smatthias.ringwald                     // see hci_run
962c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9638d213e1aSmatthias.ringwald                     break;
9648d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
965b546ac54Smatthias.ringwald                     // see hci_run
966b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
96789db417bSmatthias.ringwald                     hci_stack.substate = 0;
9688d213e1aSmatthias.ringwald                     break;
9698d213e1aSmatthias.ringwald             }
9708d213e1aSmatthias.ringwald             break;
9717301ad89Smatthias.ringwald 
9728d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
9738d213e1aSmatthias.ringwald             switch (power_mode){
9748d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
9758d213e1aSmatthias.ringwald                     // set up state machine
9768d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9778d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
9788d213e1aSmatthias.ringwald                     break;
9798d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9808d213e1aSmatthias.ringwald                     // do nothing
9818d213e1aSmatthias.ringwald                     break;
9828d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
983b546ac54Smatthias.ringwald                     // see hci_run
984b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
98589db417bSmatthias.ringwald                     hci_stack.substate = 0;
9868d213e1aSmatthias.ringwald                     break;
9878d213e1aSmatthias.ringwald             }
9888d213e1aSmatthias.ringwald             break;
9898d213e1aSmatthias.ringwald 
9908d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
9918d213e1aSmatthias.ringwald             switch (power_mode){
9928d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
99328171530Smatthias.ringwald 
99428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
99528171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
99628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9978747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
998da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
99928171530Smatthias.ringwald                         break;
100028171530Smatthias.ringwald                     }
100128171530Smatthias.ringwald #endif
1002b546ac54Smatthias.ringwald                     // set up state machine
1003b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1004b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1005b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10068d213e1aSmatthias.ringwald                     break;
10078d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1008b546ac54Smatthias.ringwald                     // see hci_run
1009b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10108d213e1aSmatthias.ringwald                     break;
10118d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1012b546ac54Smatthias.ringwald                     // do nothing
10138d213e1aSmatthias.ringwald                     break;
10148d213e1aSmatthias.ringwald             }
10158d213e1aSmatthias.ringwald             break;
10168d213e1aSmatthias.ringwald 
10178d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
10188d213e1aSmatthias.ringwald             switch (power_mode){
10198d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
102028171530Smatthias.ringwald 
102128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
102228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
102328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
10248747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
1025da5275c5S[email protected]                         hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1026758b46ceSmatthias.ringwald                         hci_update_scan_enable();
102728171530Smatthias.ringwald                         break;
102828171530Smatthias.ringwald                     }
102928171530Smatthias.ringwald #endif
10303144bce4Smatthias.ringwald                     err = hci_power_control_wake();
10313144bce4Smatthias.ringwald                     if (err) return err;
1032b546ac54Smatthias.ringwald                     // set up state machine
1033b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
1034b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
1035b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
10368d213e1aSmatthias.ringwald                     break;
10378d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
10384ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
10398d213e1aSmatthias.ringwald                     break;
10408d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1041b546ac54Smatthias.ringwald                     // do nothing
10428d213e1aSmatthias.ringwald                     break;
10438d213e1aSmatthias.ringwald             }
10448d213e1aSmatthias.ringwald             break;
104511e23e5fSmatthias.ringwald     }
104668d92d03Smatthias.ringwald 
1047038bc64cSmatthias.ringwald     // create internal event
1048ee8bf225Smatthias.ringwald 	hci_emit_state();
1049ee8bf225Smatthias.ringwald 
105068d92d03Smatthias.ringwald 	// trigger next/first action
105168d92d03Smatthias.ringwald 	hci_run();
105268d92d03Smatthias.ringwald 
1053475c8125Smatthias.ringwald     return 0;
1054475c8125Smatthias.ringwald }
1055475c8125Smatthias.ringwald 
1056758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1057758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
1058758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
1059758b46ceSmatthias.ringwald     hci_run();
1060758b46ceSmatthias.ringwald }
1061758b46ceSmatthias.ringwald 
1062381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1063381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1064381fbed8Smatthias.ringwald 
1065381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
1066381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
1067381fbed8Smatthias.ringwald         return;
1068381fbed8Smatthias.ringwald     }
1069381fbed8Smatthias.ringwald 
1070381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
1071758b46ceSmatthias.ringwald     hci_update_scan_enable();
1072758b46ceSmatthias.ringwald }
1073b031bebbSmatthias.ringwald 
1074758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1075758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1076758b46ceSmatthias.ringwald 
1077758b46ceSmatthias.ringwald     // don't emit event
1078758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1079758b46ceSmatthias.ringwald 
1080758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1081758b46ceSmatthias.ringwald     hci_update_scan_enable();
1082381fbed8Smatthias.ringwald }
1083381fbed8Smatthias.ringwald 
108406b35ec0Smatthias.ringwald void hci_run(){
10858a485f27Smatthias.ringwald 
108632ab9390Smatthias.ringwald     hci_connection_t * connection;
108732ab9390Smatthias.ringwald     linked_item_t * it;
108832ab9390Smatthias.ringwald 
1089ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1090ce4c8fabSmatthias.ringwald 
1091b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1092b031bebbSmatthias.ringwald 
1093b031bebbSmatthias.ringwald     // decline incoming connections
1094ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1095ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1096ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1097ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1098dbe1a790S[email protected]         return;
1099ce4c8fabSmatthias.ringwald     }
1100ce4c8fabSmatthias.ringwald 
1101b031bebbSmatthias.ringwald     // send scan enable
110296774428Smatthias.ringwald     if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff){
1103b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1104b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1105dbe1a790S[email protected]         return;
1106b031bebbSmatthias.ringwald     }
1107b031bebbSmatthias.ringwald 
110832ab9390Smatthias.ringwald     // send pending HCI commands
110932ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
111032ab9390Smatthias.ringwald 
1111b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
111232ab9390Smatthias.ringwald 
111332ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
11147b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
111532ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
111632ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1117dbe1a790S[email protected]             return;
1118c7e0c5f6Smatthias.ringwald         }
1119c7e0c5f6Smatthias.ringwald 
112032ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
112132ab9390Smatthias.ringwald             link_key_t link_key;
11227b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
112332ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
112432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
112532ab9390Smatthias.ringwald             } else {
112632ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
112732ab9390Smatthias.ringwald             }
112828ca2b46S[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
1129dbe1a790S[email protected]             return;
113032ab9390Smatthias.ringwald         }
11311d6b20aeS[email protected] 
1132dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
1133dbe1a790S[email protected]             hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement);
1134dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
1135dbe1a790S[email protected]             return;
113632ab9390Smatthias.ringwald         }
113732ab9390Smatthias.ringwald 
1138dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1139dbe1a790S[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1140dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
1141dbe1a790S[email protected]             return;
1142dbe1a790S[email protected]         }
1143dbe1a790S[email protected] 
1144dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1145dbe1a790S[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1146dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
1147dbe1a790S[email protected]             return;
1148dbe1a790S[email protected]         }
1149dbe1a790S[email protected]     }
1150c7e0c5f6Smatthias.ringwald 
11513429f56bSmatthias.ringwald     switch (hci_stack.state){
11523429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11537b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
11543429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
11553429f56bSmatthias.ringwald                 // odd: waiting for command completion
115606b35ec0Smatthias.ringwald                 return;
11573429f56bSmatthias.ringwald             }
115890919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1159c7492964Smatthias.ringwald                 case 0: // RESET
116022909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
1161c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1162c7492964Smatthias.ringwald                         // skip baud change
1163c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1164c7492964Smatthias.ringwald                     }
11653429f56bSmatthias.ringwald                     break;
1166c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
11677dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
11687dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1169c7492964Smatthias.ringwald                     break;
1170c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1171c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1172c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1173c7492964Smatthias.ringwald                     // break missing here for fall through
1174c7492964Smatthias.ringwald 
1175c7492964Smatthias.ringwald                 case 3:
117690919203Smatthias.ringwald                     // custom initialization
1177db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
11787dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1179d62aca9fSmatthias.ringwald                         if (valid_cmd){
11807dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
11817dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1182c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
118390919203Smatthias.ringwald                             break;
118490919203Smatthias.ringwald                         }
1185d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
118690919203Smatthias.ringwald                     }
11872f6c30e1Smatthias.ringwald                     // otherwise continue
1188f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1189f432a6ddSmatthias.ringwald 					break;
1190c7492964Smatthias.ringwald 				case 4:
1191e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1192e2edc0c3Smatthias.ringwald 					break;
1193c7492964Smatthias.ringwald                 case 5:
1194*65389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
11953429f56bSmatthias.ringwald                     break;
1196c7492964Smatthias.ringwald                 case 6:
1197*65389bfcS[email protected]                     // ca. 15 sec
1198*65389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1199559e517eS[email protected]                     break;
1200e2386ba1S[email protected]                 case 7:
1201e2386ba1S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device);
1202e2386ba1S[email protected]                     break;
1203559e517eS[email protected]                 case 8:
1204e2386ba1S[email protected]                     if (hci_stack.local_name){
1205e2386ba1S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack.local_name);
1206e2386ba1S[email protected]                     } else {
12078a485f27Smatthias.ringwald                         char hostname[30];
1208e2386ba1S[email protected] #ifdef EMBEDDED
1209e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1210e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
1211e2386ba1S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr));
1212e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1213e2386ba1S[email protected] #else
1214e2386ba1S[email protected]                         // hostname for POSIX systems
12158a485f27Smatthias.ringwald                         gethostname(hostname, 30);
12168a485f27Smatthias.ringwald                         hostname[29] = '\0';
1217e2386ba1S[email protected] #endif
12188a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
12198a485f27Smatthias.ringwald                     }
12203c4d4b90Smatthias.ringwald                     break;
1221e2386ba1S[email protected]                 case 9:
1222e0dbca83S[email protected]                     hci_send_cmd(&hci_set_event_mask,0xffffffff, 0xFFFFFFFF); ///0x1DFFFFFF
1223e2386ba1S[email protected]                     break;
1224559e517eS[email protected]                 case 10:
1225a45d6b9fS[email protected]                     hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable);
1226e0dbca83S[email protected]                     break;
1227e0dbca83S[email protected]                 case 11:
1228a45d6b9fS[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
1229a45d6b9fS[email protected] 					break;
1230a45d6b9fS[email protected]                 case 12:
12313429f56bSmatthias.ringwald                     // done.
12323429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1233b360b6adSmatthias.ringwald                     hci_emit_state();
12343429f56bSmatthias.ringwald                     break;
12353429f56bSmatthias.ringwald                 default:
12363429f56bSmatthias.ringwald                     break;
1237475c8125Smatthias.ringwald             }
12383429f56bSmatthias.ringwald             hci_stack.substate++;
12393429f56bSmatthias.ringwald             break;
1240c7e0c5f6Smatthias.ringwald 
1241c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1242c7e0c5f6Smatthias.ringwald 
12437b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1244c7e0c5f6Smatthias.ringwald             // close all open connections
1245c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1246c7e0c5f6Smatthias.ringwald             if (connection){
124732ab9390Smatthias.ringwald 
1248c7e0c5f6Smatthias.ringwald                 // send disconnect
124932ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
125032ab9390Smatthias.ringwald 
125179bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
12526ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1253c7e0c5f6Smatthias.ringwald 
1254c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1255c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1256c7e0c5f6Smatthias.ringwald                 return;
1257c7e0c5f6Smatthias.ringwald             }
12587b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1259c7e0c5f6Smatthias.ringwald 
126072ea5239Smatthias.ringwald             // switch mode
1261c7e0c5f6Smatthias.ringwald             hci_power_control_off();
12629418f9c9Smatthias.ringwald 
12637b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
126472ea5239Smatthias.ringwald             hci_emit_state();
12657b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
126672ea5239Smatthias.ringwald             break;
1267c7e0c5f6Smatthias.ringwald 
126872ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
126989db417bSmatthias.ringwald             switch(hci_stack.substate) {
127089db417bSmatthias.ringwald                 case 0:
12717b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
127272ea5239Smatthias.ringwald                     // close all open connections
127372ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
127466da7044Smatthias.ringwald 
127528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
127666da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
127766da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
127866da7044Smatthias.ringwald                         connection = NULL;
127966da7044Smatthias.ringwald                     }
128066da7044Smatthias.ringwald #endif
128172ea5239Smatthias.ringwald                     if (connection){
128232ab9390Smatthias.ringwald 
128372ea5239Smatthias.ringwald                         // send disconnect
128432ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
128532ab9390Smatthias.ringwald 
128679bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
12876ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
128872ea5239Smatthias.ringwald 
128972ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
129072ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
129172ea5239Smatthias.ringwald                         return;
129272ea5239Smatthias.ringwald                     }
129372ea5239Smatthias.ringwald 
129489db417bSmatthias.ringwald                     // disable page and inquiry scan
129532ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
129632ab9390Smatthias.ringwald 
1297758b46ceSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq cans\n");
1298758b46ceSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
129989db417bSmatthias.ringwald 
130089db417bSmatthias.ringwald                     // continue in next sub state
130189db417bSmatthias.ringwald                     hci_stack.substate++;
130289db417bSmatthias.ringwald                     break;
130389db417bSmatthias.ringwald                 case 1:
130489db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
130589db417bSmatthias.ringwald                     break;
130689db417bSmatthias.ringwald                 case 2:
13077b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
130828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
130928171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
131028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
131128171530Smatthias.ringwald                         // SLEEP MODE reached
131228171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
131328171530Smatthias.ringwald                         hci_emit_state();
131428171530Smatthias.ringwald                         break;
131528171530Smatthias.ringwald                     }
131628171530Smatthias.ringwald #endif
131772ea5239Smatthias.ringwald                     // switch mode
131889db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1319c7e0c5f6Smatthias.ringwald                     hci_emit_state();
132028171530Smatthias.ringwald                     break;
132128171530Smatthias.ringwald 
132289db417bSmatthias.ringwald                 default:
132389db417bSmatthias.ringwald                     break;
132489db417bSmatthias.ringwald             }
1325c7e0c5f6Smatthias.ringwald             break;
1326c7e0c5f6Smatthias.ringwald 
13273429f56bSmatthias.ringwald         default:
13283429f56bSmatthias.ringwald             break;
13291f504dbdSmatthias.ringwald     }
13303429f56bSmatthias.ringwald }
133116833f0aSmatthias.ringwald 
133231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1333c8e4258aSmatthias.ringwald     bd_addr_t addr;
1334c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1335c8e4258aSmatthias.ringwald     // house-keeping
1336c8e4258aSmatthias.ringwald 
1337c8e4258aSmatthias.ringwald     // create_connection?
1338c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1339c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1340339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1341c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1342c8e4258aSmatthias.ringwald         if (conn) {
1343c8e4258aSmatthias.ringwald             // if connection exists
1344c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
134517f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
134617f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1347c8e4258aSmatthias.ringwald             }
134817f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1349c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1350c8e4258aSmatthias.ringwald 
135117f1ba2aSmatthias.ringwald         }
1352c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
135317f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
135417f1ba2aSmatthias.ringwald         if (!conn){
135517f1ba2aSmatthias.ringwald             // notify client that alloc failed
135617f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
135717f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
135817f1ba2aSmatthias.ringwald         }
1359c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1360c8e4258aSmatthias.ringwald     }
1361c8e4258aSmatthias.ringwald 
13627fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
13637fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
13647fde4af9Smatthias.ringwald     }
13657fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
13667fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
13677fde4af9Smatthias.ringwald     }
13687fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
13697fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
13707fde4af9Smatthias.ringwald     }
13717fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
13727fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
13737fde4af9Smatthias.ringwald     }
13747fde4af9Smatthias.ringwald 
13758ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
13768ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
13778ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
13788ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
13798ef73945Smatthias.ringwald         }
13808ef73945Smatthias.ringwald     }
1381c8e4258aSmatthias.ringwald 
138231452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1383622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
138431452debSmatthias.ringwald }
13858adf0ddaSmatthias.ringwald 
1386dbe1a790S[email protected] // Configure Secure Simple Pairing
1387dbe1a790S[email protected] 
1388dbe1a790S[email protected] // enable will enable SSP during init
1389dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
1390dbe1a790S[email protected]     hci_stack.ssp_enable = enable;
1391dbe1a790S[email protected] }
1392dbe1a790S[email protected] 
1393dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1394dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
1395dbe1a790S[email protected]     hci_stack.ssp_io_capability = io_capability;
1396dbe1a790S[email protected] }
1397dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
1398dbe1a790S[email protected]     hci_stack.ssp_authentication_requirement = authentication_requirement;
1399dbe1a790S[email protected] }
1400dbe1a790S[email protected] 
1401dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1402dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
1403dbe1a790S[email protected]     hci_stack.ssp_auto_accept = auto_accept;
1404dbe1a790S[email protected] }
1405dbe1a790S[email protected] 
14061cd208adSmatthias.ringwald /**
14071cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
14081cd208adSmatthias.ringwald  */
1409fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
14101cd208adSmatthias.ringwald     va_list argptr;
14111cd208adSmatthias.ringwald     va_start(argptr, cmd);
14127dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
14131cd208adSmatthias.ringwald     va_end(argptr);
14147dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
141593b8dc03Smatthias.ringwald }
1416c8e4258aSmatthias.ringwald 
1417ee091cf1Smatthias.ringwald // Create various non-HCI events.
1418ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1419ee091cf1Smatthias.ringwald 
1420c8e4258aSmatthias.ringwald void hci_emit_state(){
1421e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack.state);
1422425d1371Smatthias.ringwald     uint8_t event[3];
142380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1424425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1425c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1426425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1427425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1428c8e4258aSmatthias.ringwald }
1429c8e4258aSmatthias.ringwald 
143017f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1431425d1371Smatthias.ringwald     uint8_t event[13];
1432c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1433425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
143417f1ba2aSmatthias.ringwald     event[2] = status;
1435c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1436c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1437c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1438c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1439425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1440425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1441c8e4258aSmatthias.ringwald }
1442c8e4258aSmatthias.ringwald 
14433c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1444425d1371Smatthias.ringwald     uint8_t event[6];
14453c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1446e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
14473c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
14483c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
14493c4d4b90Smatthias.ringwald     event[5] = reason;
1450425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1451425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
14523c4d4b90Smatthias.ringwald }
14533c4d4b90Smatthias.ringwald 
1454ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1455e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1456425d1371Smatthias.ringwald     uint8_t event[4];
145780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1458425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1459ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1460425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1461425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1462ee091cf1Smatthias.ringwald }
146343bfb1bdSmatthias.ringwald 
146443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1465e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1466425d1371Smatthias.ringwald     uint8_t event[3];
146780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1468425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
146943bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1470425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1471425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
147243bfb1bdSmatthias.ringwald }
1473038bc64cSmatthias.ringwald 
1474038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1475e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1476425d1371Smatthias.ringwald     uint8_t event[2];
147780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1478425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1479425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1480425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1481038bc64cSmatthias.ringwald }
14821b0e3922Smatthias.ringwald 
148309ba8edeSmatthias.ringwald #ifndef EMBEDDED
14841b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1485e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1486425d1371Smatthias.ringwald     uint8_t event[6];
14871b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1488425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1489425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1490425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1491425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1492425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1493425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
14941b0e3922Smatthias.ringwald }
149509ba8edeSmatthias.ringwald #endif
14961b0e3922Smatthias.ringwald 
14972ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1498e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1499425d1371Smatthias.ringwald     uint8_t event[3];
15002ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1501425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
15022ed6235cSmatthias.ringwald     event[2] = enabled;
1503425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1504e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
15052ed6235cSmatthias.ringwald }
1506627c2f45Smatthias.ringwald 
1507627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1508e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1509627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1510e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1511f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
15122f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1513f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1514e0abb8e7S[email protected] 
1515e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1516e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1517e0abb8e7S[email protected] 
1518e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
1519e0abb8e7S[email protected]     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1520627c2f45Smatthias.ringwald }
1521381fbed8Smatthias.ringwald 
1522381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1523e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1524425d1371Smatthias.ringwald     uint8_t event[3];
1525381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1526425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1527381fbed8Smatthias.ringwald     event[2] = enabled;
1528425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1529425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1530381fbed8Smatthias.ringwald }
1531