xref: /btstack/src/hci.c (revision f5454fc67e593c7eb8a59de8fc7404f6705d5be2)
11f504dbdSmatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 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.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
331f504dbdSmatthias.ringwald  *  hci.c
341f504dbdSmatthias.ringwald  *
351f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
361f504dbdSmatthias.ringwald  *
371f504dbdSmatthias.ringwald  */
381f504dbdSmatthias.ringwald 
39c8901d41Smatthias.ringwald #include "config.h"
4028171530Smatthias.ringwald 
417f2435e6Smatthias.ringwald #include "hci.h"
427f2435e6Smatthias.ringwald 
4393b8dc03Smatthias.ringwald #include <stdarg.h>
4493b8dc03Smatthias.ringwald #include <string.h>
4556fe0872Smatthias.ringwald #include <stdio.h>
467f2435e6Smatthias.ringwald 
47549e6ebeSmatthias.ringwald #ifndef EMBEDDED
48549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
4909ba8edeSmatthias.ringwald #include <btstack/version.h>
50549e6ebeSmatthias.ringwald #endif
51549e6ebeSmatthias.ringwald 
52a3b02b71Smatthias.ringwald #include "btstack_memory.h"
537f2435e6Smatthias.ringwald #include "debug.h"
54d8905019Smatthias.ringwald #include "hci_dump.h"
5593b8dc03Smatthias.ringwald 
56ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
571b0e3922Smatthias.ringwald 
58169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
59ee091cf1Smatthias.ringwald 
6028171530Smatthias.ringwald #ifdef USE_BLUETOOL
6128171530Smatthias.ringwald #include "bt_control_iphone.h"
6228171530Smatthias.ringwald #endif
6328171530Smatthias.ringwald 
6406b35ec0Smatthias.ringwald // the STACK is here
6516833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
6616833f0aSmatthias.ringwald 
6797addcc5Smatthias.ringwald /**
68ee091cf1Smatthias.ringwald  * get connection for a given handle
69ee091cf1Smatthias.ringwald  *
70ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
71ee091cf1Smatthias.ringwald  */
72645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
73ee091cf1Smatthias.ringwald     linked_item_t *it;
74ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
75ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
76ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
77ee091cf1Smatthias.ringwald         }
78ee091cf1Smatthias.ringwald     }
79ee091cf1Smatthias.ringwald     return NULL;
80ee091cf1Smatthias.ringwald }
81ee091cf1Smatthias.ringwald 
822b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
83ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
84c785ef68Smatthias.ringwald #ifdef HAVE_TIME
85ee091cf1Smatthias.ringwald     struct timeval tv;
86ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
87c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
88ee091cf1Smatthias.ringwald         // connections might be timed out
89ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
90ee091cf1Smatthias.ringwald     }
912b12a0b9Smatthias.ringwald #endif
92e5780900Smatthias.ringwald #ifdef HAVE_TICK
93c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
94c785ef68Smatthias.ringwald         // connections might be timed out
95c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
96c785ef68Smatthias.ringwald     }
97c785ef68Smatthias.ringwald #endif
98c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
99c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
100c785ef68Smatthias.ringwald }
101ee091cf1Smatthias.ringwald 
102ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
103c7492964Smatthias.ringwald #ifdef HAVE_TIME
104ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
105c7492964Smatthias.ringwald #endif
106e5780900Smatthias.ringwald #ifdef HAVE_TICK
107c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
108c785ef68Smatthias.ringwald #endif
109ee091cf1Smatthias.ringwald }
110ee091cf1Smatthias.ringwald 
111ee091cf1Smatthias.ringwald /**
112c8e4258aSmatthias.ringwald  * create connection for given address
113c8e4258aSmatthias.ringwald  *
11417f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
115c8e4258aSmatthias.ringwald  */
116c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
117a3b02b71Smatthias.ringwald     hci_connection_t * conn = btstack_memory_hci_connection_get();
118c8e4258aSmatthias.ringwald     if (!conn) return NULL;
119c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
120c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1217d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
122ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
123ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
124ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
125d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1267856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
12756cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
128c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
129c8e4258aSmatthias.ringwald     return conn;
130c8e4258aSmatthias.ringwald }
131c8e4258aSmatthias.ringwald 
132c8e4258aSmatthias.ringwald /**
13306b35ec0Smatthias.ringwald  * get connection for given address
13497addcc5Smatthias.ringwald  *
13597addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
13697addcc5Smatthias.ringwald  */
137fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
13806b35ec0Smatthias.ringwald     linked_item_t *it;
13906b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
14006b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
14106b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
14206b35ec0Smatthias.ringwald         }
14306b35ec0Smatthias.ringwald     }
14406b35ec0Smatthias.ringwald     return NULL;
14506b35ec0Smatthias.ringwald }
14606b35ec0Smatthias.ringwald 
14743bfb1bdSmatthias.ringwald /**
14880ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1497fde4af9Smatthias.ringwald  */
1507fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1517fde4af9Smatthias.ringwald     bd_addr_t addr;
1527fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1537fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1547fde4af9Smatthias.ringwald     if (conn) {
1557fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
15680ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1577fde4af9Smatthias.ringwald     }
1587fde4af9Smatthias.ringwald }
1597fde4af9Smatthias.ringwald 
16080ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
16180ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
16280ca58a0Smatthias.ringwald     if (!conn) return 0;
16380ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
16480ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
16580ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
16680ca58a0Smatthias.ringwald     return 1;
16780ca58a0Smatthias.ringwald }
16880ca58a0Smatthias.ringwald 
169c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
170c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
171c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
172c12e46e7Smatthias.ringwald     }
173c12e46e7Smatthias.ringwald }
174c12e46e7Smatthias.ringwald 
1757fde4af9Smatthias.ringwald 
1767fde4af9Smatthias.ringwald /**
17743bfb1bdSmatthias.ringwald  * count connections
17843bfb1bdSmatthias.ringwald  */
17940d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
18056c253c9Smatthias.ringwald     int count = 0;
18143bfb1bdSmatthias.ringwald     linked_item_t *it;
18243bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
18343bfb1bdSmatthias.ringwald     return count;
18443bfb1bdSmatthias.ringwald }
185c8e4258aSmatthias.ringwald 
18697addcc5Smatthias.ringwald /**
187ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
18816833f0aSmatthias.ringwald  */
1892718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
19016833f0aSmatthias.ringwald }
19116833f0aSmatthias.ringwald 
192998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
193998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
194998906cdSmatthias.ringwald     if (!connection) {
1957d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
196998906cdSmatthias.ringwald         return 0;
197998906cdSmatthias.ringwald     }
198998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
199998906cdSmatthias.ringwald }
200998906cdSmatthias.ringwald 
201998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
202998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
203998906cdSmatthias.ringwald     linked_item_t *it;
204998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
205998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
206998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2077d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
208998906cdSmatthias.ringwald             return 0;
209998906cdSmatthias.ringwald         }
210998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
211998906cdSmatthias.ringwald     }
212998906cdSmatthias.ringwald     return free_slots;
213998906cdSmatthias.ringwald }
214998906cdSmatthias.ringwald 
215c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2162b12a0b9Smatthias.ringwald 
2172b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2182b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2192b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2202b12a0b9Smatthias.ringwald             return 0;
2212b12a0b9Smatthias.ringwald         }
2222b12a0b9Smatthias.ringwald     }
2232b12a0b9Smatthias.ringwald 
2242b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
225c24735b1Smatthias.ringwald     switch (packet_type) {
226c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
227c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
228c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
229de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
230c24735b1Smatthias.ringwald         default:
231c24735b1Smatthias.ringwald             return 0;
232c24735b1Smatthias.ringwald     }
233c24735b1Smatthias.ringwald }
234c24735b1Smatthias.ringwald 
235ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2367856c818Smatthias.ringwald 
2376218e6f1Smatthias.ringwald     // check for free places on BT module
2385932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2396218e6f1Smatthias.ringwald 
2407856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2417856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
24256cf178bSmatthias.ringwald     if (!connection) return 0;
24356cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
24456cf178bSmatthias.ringwald 
24556cf178bSmatthias.ringwald     // count packet
24656cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2477b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2487856c818Smatthias.ringwald 
24900d8e42eSmatthias.ringwald     // send packet
25000d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2516218e6f1Smatthias.ringwald 
25200d8e42eSmatthias.ringwald     return err;
253ee091cf1Smatthias.ringwald }
254ee091cf1Smatthias.ringwald 
25516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2567856c818Smatthias.ringwald 
2577856c818Smatthias.ringwald     // get info
2587856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2597856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2607856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2617856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2627856c818Smatthias.ringwald 
2637856c818Smatthias.ringwald     // ignore non-registered handle
2647856c818Smatthias.ringwald     if (!conn){
2657d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2667856c818Smatthias.ringwald         return;
2677856c818Smatthias.ringwald     }
2687856c818Smatthias.ringwald 
2697856c818Smatthias.ringwald     // update idle timestamp
2707856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2717856c818Smatthias.ringwald 
2727856c818Smatthias.ringwald     // handle different packet types
2737856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2747856c818Smatthias.ringwald 
2757856c818Smatthias.ringwald         case 0x01: // continuation fragment
2767856c818Smatthias.ringwald 
2777856c818Smatthias.ringwald             // sanity check
2787856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2797d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2807856c818Smatthias.ringwald                 return;
2817856c818Smatthias.ringwald             }
2827856c818Smatthias.ringwald 
2837856c818Smatthias.ringwald             // append fragment payload (header already stored)
2847856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2857856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2867856c818Smatthias.ringwald 
2877d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
288decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
2897856c818Smatthias.ringwald 
2907856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2917856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2927856c818Smatthias.ringwald 
2932718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2947856c818Smatthias.ringwald                 // reset recombination buffer
2957856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2967856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2977856c818Smatthias.ringwald             }
2987856c818Smatthias.ringwald             break;
2997856c818Smatthias.ringwald 
3007856c818Smatthias.ringwald         case 0x02: { // first fragment
3017856c818Smatthias.ringwald 
3027856c818Smatthias.ringwald             // sanity check
3037856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3047d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3057856c818Smatthias.ringwald                 return;
3067856c818Smatthias.ringwald             }
3077856c818Smatthias.ringwald 
3087856c818Smatthias.ringwald             // peek into L2CAP packet!
3097856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3107856c818Smatthias.ringwald 
3117d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
312decc01a8Smatthias.ringwald 
3137856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3147856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3157856c818Smatthias.ringwald 
3167856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3172718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3187856c818Smatthias.ringwald 
3197856c818Smatthias.ringwald             } else {
3207856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3217856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3227856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3237856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
324decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3257856c818Smatthias.ringwald             }
3267856c818Smatthias.ringwald             break;
3277856c818Smatthias.ringwald 
3287856c818Smatthias.ringwald         }
3297856c818Smatthias.ringwald         default:
3307d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3317856c818Smatthias.ringwald             return;
3327856c818Smatthias.ringwald     }
33394ab26f8Smatthias.ringwald 
33494ab26f8Smatthias.ringwald     // execute main loop
33594ab26f8Smatthias.ringwald     hci_run();
33616833f0aSmatthias.ringwald }
33722909952Smatthias.ringwald 
33867a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
339339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3403c4d4b90Smatthias.ringwald 
3413c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3423c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3433c4d4b90Smatthias.ringwald 
344c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
345c785ef68Smatthias.ringwald 
346c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
347a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3483c4d4b90Smatthias.ringwald 
3493c4d4b90Smatthias.ringwald     // now it's gone
350c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
351c7e0c5f6Smatthias.ringwald }
352c7e0c5f6Smatthias.ringwald 
3530c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3548f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3558f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3568f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3578f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3588f8108aaSmatthias.ringwald };
3598f8108aaSmatthias.ringwald 
3608f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){
3618f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
3628f8108aaSmatthias.ringwald     int i;
3638f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
3648f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
3658f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
3668f8108aaSmatthias.ringwald             packet_types |= 1 << i;
3678f8108aaSmatthias.ringwald         }
3688f8108aaSmatthias.ringwald     }
3698f8108aaSmatthias.ringwald     // flip bits for "may not be used"
3708f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
3718f8108aaSmatthias.ringwald     return packet_types;
3728f8108aaSmatthias.ringwald }
3738f8108aaSmatthias.ringwald 
3748f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
3758f8108aaSmatthias.ringwald     return hci_stack.packet_types;
3768f8108aaSmatthias.ringwald }
3778f8108aaSmatthias.ringwald 
3787dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
3797dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
3807dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
3817dc17943Smatthias.ringwald }
3827dc17943Smatthias.ringwald 
3837dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
3847dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
3857dc17943Smatthias.ringwald }
3867dc17943Smatthias.ringwald 
38774ec757aSmatthias.ringwald // avoid huge local variables
388223aafc1Smatthias.ringwald #ifndef EMBEDDED
38974ec757aSmatthias.ringwald static device_name_t device_name;
390223aafc1Smatthias.ringwald #endif
39116833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3921281a47eSmatthias.ringwald     bd_addr_t addr;
3932d00edd4Smatthias.ringwald     uint8_t link_type;
394fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3951f7b95a1Smatthias.ringwald     hci_connection_t * conn;
39656cf178bSmatthias.ringwald     int i;
39722909952Smatthias.ringwald 
3985909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
3995909f7f2Smatthias.ringwald 
4006772a24cSmatthias.ringwald     switch (packet[0]) {
40122909952Smatthias.ringwald 
4026772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4037ec5eeaaSmatthias.ringwald             // get num cmd packets
4047b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4057ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4067ec5eeaaSmatthias.ringwald 
407e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
408e2edc0c3Smatthias.ringwald                 // from offset 5
409e2edc0c3Smatthias.ringwald                 // status
4101d279b20Smatthias.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"
411e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
41256cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
413e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
41456cf178bSmatthias.ringwald                 // ignore: total num SCO packets
41556cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
416a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
4178fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
4188fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
4198f8108aaSmatthias.ringwald                     }
420a7a04bd9Smatthias.ringwald                     // determine usable ACL packet types
42128c93ceeSmatthias.ringwald                     hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length);
4228f8108aaSmatthias.ringwald 
4238fcba05dSmatthias.ringwald                     log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n",
4248f8108aaSmatthias.ringwald                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types);
425e2edc0c3Smatthias.ringwald                 }
42656cf178bSmatthias.ringwald             }
427381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
428381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
429381fbed8Smatthias.ringwald             }
43056cf178bSmatthias.ringwald             break;
43156cf178bSmatthias.ringwald 
4327ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4337ec5eeaaSmatthias.ringwald             // get num cmd packets
4347b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4357ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4367ec5eeaaSmatthias.ringwald             break;
4377ec5eeaaSmatthias.ringwald 
43856cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
43956cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
44056cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
44156cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
44256cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
44356cf178bSmatthias.ringwald                 if (!conn){
4447d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
44556cf178bSmatthias.ringwald                     continue;
44656cf178bSmatthias.ringwald                 }
44756cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4487b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
44956cf178bSmatthias.ringwald             }
4506772a24cSmatthias.ringwald             break;
4516772a24cSmatthias.ringwald 
4521f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
45337eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
45437eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4552d00edd4Smatthias.ringwald             link_type = packet[11];
456339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
45737eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4581f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4591f7b95a1Smatthias.ringwald                 if (!conn) {
4601f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4611f7b95a1Smatthias.ringwald                 }
462ce4c8fabSmatthias.ringwald                 if (!conn) {
463ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
464ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
465ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
466ce4c8fabSmatthias.ringwald                     break;
467ce4c8fabSmatthias.ringwald                 }
46832ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
46932ab9390Smatthias.ringwald                 hci_run();
47037eaa4cfSmatthias.ringwald             } else {
471ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
472ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
473ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
47437eaa4cfSmatthias.ringwald             }
4751f7b95a1Smatthias.ringwald             break;
4761f7b95a1Smatthias.ringwald 
4776772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
478fe1ed1b8Smatthias.ringwald             // Connection management
479fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
480339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
4811f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
482fe1ed1b8Smatthias.ringwald             if (conn) {
483b448a0e7Smatthias.ringwald                 if (!packet[2]){
484c8e4258aSmatthias.ringwald                     conn->state = OPEN;
485fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
486ee091cf1Smatthias.ringwald 
487c785ef68Smatthias.ringwald                     // restart timer
488c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
489ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
490c785ef68Smatthias.ringwald 
491339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
49243bfb1bdSmatthias.ringwald 
49343bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
494b448a0e7Smatthias.ringwald                 } else {
495b448a0e7Smatthias.ringwald                     // connection failed, remove entry
496b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
497a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
498c12e46e7Smatthias.ringwald 
499c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
500c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
501c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
502c12e46e7Smatthias.ringwald                     }
503fe1ed1b8Smatthias.ringwald                 }
504fe1ed1b8Smatthias.ringwald             }
5056772a24cSmatthias.ringwald             break;
506fe1ed1b8Smatthias.ringwald 
5077fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5087b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5097fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
51074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
51132ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
51264472d52Smatthias.ringwald             hci_run();
51329d53098Smatthias.ringwald             // request already answered
51429d53098Smatthias.ringwald             return;
5157fde4af9Smatthias.ringwald 
5167fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5177fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
51874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
51929d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
520287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
52129d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5227fde4af9Smatthias.ringwald             break;
5237fde4af9Smatthias.ringwald 
5247fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5257fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
5267fde4af9Smatthias.ringwald             break;
5277fde4af9Smatthias.ringwald 
528223aafc1Smatthias.ringwald #ifndef EMBEDDED
52974ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
53074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
53174ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
53274ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5335a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5345a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5355a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5365a06394aSmatthias.ringwald                     packet[9+i] = 0;
5375a06394aSmatthias.ringwald                     break;
538cdc9101dSmatthias.ringwald                 }
5395a06394aSmatthias.ringwald             }
540d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
54174ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
54274ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
54374ec757aSmatthias.ringwald             break;
54474ec757aSmatthias.ringwald 
54574ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
54674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
54774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
54874ec757aSmatthias.ringwald             // first send inq result packet
54974ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
55074ec757aSmatthias.ringwald             // then send cached remote names
55174ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
55274ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
55374ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
55474ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
55574ec757aSmatthias.ringwald                 }
55674ec757aSmatthias.ringwald             }
55774ec757aSmatthias.ringwald             return;
558223aafc1Smatthias.ringwald #endif
55974ec757aSmatthias.ringwald 
5606772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
561fe1ed1b8Smatthias.ringwald             if (!packet[2]){
562fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
563fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
564fe1ed1b8Smatthias.ringwald                 if (conn) {
565c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
566fe1ed1b8Smatthias.ringwald                 }
567fe1ed1b8Smatthias.ringwald             }
5686772a24cSmatthias.ringwald             break;
5696772a24cSmatthias.ringwald 
570c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
571c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
572c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
573c68bdf90Smatthias.ringwald             }
574c68bdf90Smatthias.ringwald             break;
575c68bdf90Smatthias.ringwald 
5765909f7f2Smatthias.ringwald #ifdef HAVE_BLE
5775909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
5785909f7f2Smatthias.ringwald             switch (packet[2]) {
5795909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
5805909f7f2Smatthias.ringwald                     // Connection management
5815909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
5825909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
5835909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
5845909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
5855909f7f2Smatthias.ringwald                     if (packet[3]){
5865909f7f2Smatthias.ringwald                         if (conn){
5875909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
5885909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
5895909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
5905909f7f2Smatthias.ringwald 
5915909f7f2Smatthias.ringwald                         }
5925909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
5935909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
5945909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
5955909f7f2Smatthias.ringwald                         }
5965909f7f2Smatthias.ringwald                         break;
5975909f7f2Smatthias.ringwald                     }
5985909f7f2Smatthias.ringwald                     if (!conn){
5995909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
6005909f7f2Smatthias.ringwald                     }
6015909f7f2Smatthias.ringwald                     if (!conn){
6025909f7f2Smatthias.ringwald                         // no memory
6035909f7f2Smatthias.ringwald                         break;
6045909f7f2Smatthias.ringwald                     }
6055909f7f2Smatthias.ringwald 
6065909f7f2Smatthias.ringwald                     conn->state = OPEN;
6075909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
6085909f7f2Smatthias.ringwald 
6095909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
6105909f7f2Smatthias.ringwald 
6115909f7f2Smatthias.ringwald                     // restart timer
6125909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
6135909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
6145909f7f2Smatthias.ringwald 
6155909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
6165909f7f2Smatthias.ringwald 
6175909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
6185909f7f2Smatthias.ringwald                     break;
6195909f7f2Smatthias.ringwald 
6205909f7f2Smatthias.ringwald                 default:
6215909f7f2Smatthias.ringwald                     break;
6225909f7f2Smatthias.ringwald             }
6235909f7f2Smatthias.ringwald             break;
6245909f7f2Smatthias.ringwald #endif
6255909f7f2Smatthias.ringwald 
6266772a24cSmatthias.ringwald         default:
6276772a24cSmatthias.ringwald             break;
628fe1ed1b8Smatthias.ringwald     }
629fe1ed1b8Smatthias.ringwald 
6303429f56bSmatthias.ringwald     // handle BT initialization
6313429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
6327301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
6337301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
6347301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
6357301ad89Smatthias.ringwald         // }
6367301ad89Smatthias.ringwald         // handle normal init sequence
6373429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
6383429f56bSmatthias.ringwald             // odd: waiting for event
6393429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
6403429f56bSmatthias.ringwald                 hci_stack.substate++;
6413429f56bSmatthias.ringwald             }
6423429f56bSmatthias.ringwald         }
64322909952Smatthias.ringwald     }
64422909952Smatthias.ringwald 
64589db417bSmatthias.ringwald     // help with BT sleep
64689db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
64789db417bSmatthias.ringwald         && hci_stack.substate == 1
64889db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
64989db417bSmatthias.ringwald         hci_stack.substate++;
65089db417bSmatthias.ringwald     }
65189db417bSmatthias.ringwald 
6522718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
65394ab26f8Smatthias.ringwald 
65494ab26f8Smatthias.ringwald 	// execute main loop
65594ab26f8Smatthias.ringwald 	hci_run();
65616833f0aSmatthias.ringwald }
65716833f0aSmatthias.ringwald 
65810e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
65910e830c9Smatthias.ringwald     switch (packet_type) {
66010e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
66110e830c9Smatthias.ringwald             event_handler(packet, size);
66210e830c9Smatthias.ringwald             break;
66310e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
66410e830c9Smatthias.ringwald             acl_handler(packet, size);
66510e830c9Smatthias.ringwald             break;
66610e830c9Smatthias.ringwald         default:
66710e830c9Smatthias.ringwald             break;
66810e830c9Smatthias.ringwald     }
66910e830c9Smatthias.ringwald }
67010e830c9Smatthias.ringwald 
671fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
6722718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
6732718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
67416833f0aSmatthias.ringwald }
67516833f0aSmatthias.ringwald 
676404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
677475c8125Smatthias.ringwald 
678475c8125Smatthias.ringwald     // reference to use transport layer implementation
67916833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
680475c8125Smatthias.ringwald 
68111e23e5fSmatthias.ringwald     // references to used control implementation
68211e23e5fSmatthias.ringwald     hci_stack.control = control;
68311e23e5fSmatthias.ringwald 
68411e23e5fSmatthias.ringwald     // reference to used config
68511e23e5fSmatthias.ringwald     hci_stack.config = config;
68611e23e5fSmatthias.ringwald 
687fe1ed1b8Smatthias.ringwald     // no connections yet
688fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
6890a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
690fe1ed1b8Smatthias.ringwald 
691b031bebbSmatthias.ringwald     // no pending cmds
692b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
693b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
694b031bebbSmatthias.ringwald 
69516833f0aSmatthias.ringwald     // higher level handler
6962718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
69716833f0aSmatthias.ringwald 
698404843c1Smatthias.ringwald     // store and open remote device db
699404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
700404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
701404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
702404843c1Smatthias.ringwald     }
70329d53098Smatthias.ringwald 
7048fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
7058fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7068fcba05dSmatthias.ringwald 
70716833f0aSmatthias.ringwald     // register packet handlers with transport
70810e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
709*f5454fc6Smatthias.ringwald 
710*f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
711475c8125Smatthias.ringwald }
712475c8125Smatthias.ringwald 
713404843c1Smatthias.ringwald void hci_close(){
714404843c1Smatthias.ringwald     // close remote device db
715404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
716404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
717404843c1Smatthias.ringwald     }
718*f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
719*f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
720*f5454fc6Smatthias.ringwald }
721*f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
722404843c1Smatthias.ringwald }
723404843c1Smatthias.ringwald 
7248d213e1aSmatthias.ringwald // State-Module-Driver overview
7258d213e1aSmatthias.ringwald // state                    module  low-level
7268d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
7278d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
7288d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
7298d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
730d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
731d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
732c7e0c5f6Smatthias.ringwald 
73340d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
7347301ad89Smatthias.ringwald 
735038bc64cSmatthias.ringwald     // power on
736f9a30166Smatthias.ringwald     int err = 0;
737f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
738f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
739f9a30166Smatthias.ringwald     }
740038bc64cSmatthias.ringwald     if (err){
7417d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
742038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
743038bc64cSmatthias.ringwald         return err;
744038bc64cSmatthias.ringwald     }
745038bc64cSmatthias.ringwald 
746038bc64cSmatthias.ringwald     // open low-level device
747038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
748038bc64cSmatthias.ringwald     if (err){
7497d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
750f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
751f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
752f9a30166Smatthias.ringwald         }
753038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
754038bc64cSmatthias.ringwald         return err;
755038bc64cSmatthias.ringwald     }
7568d213e1aSmatthias.ringwald     return 0;
7578d213e1aSmatthias.ringwald }
758038bc64cSmatthias.ringwald 
75940d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
7608d213e1aSmatthias.ringwald 
7617b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
7629418f9c9Smatthias.ringwald 
7638d213e1aSmatthias.ringwald     // close low-level device
7648d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
7658d213e1aSmatthias.ringwald 
7667b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
7679418f9c9Smatthias.ringwald 
7688d213e1aSmatthias.ringwald     // power off
7698d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
7708d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
7718d213e1aSmatthias.ringwald     }
7729418f9c9Smatthias.ringwald 
7737b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
7749418f9c9Smatthias.ringwald 
77572ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
77672ea5239Smatthias.ringwald }
77772ea5239Smatthias.ringwald 
77840d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
77972ea5239Smatthias.ringwald 
7807b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
7813144bce4Smatthias.ringwald 
782b429b9b7Smatthias.ringwald #if 0
783b429b9b7Smatthias.ringwald     // don't close serial port during sleep
784b429b9b7Smatthias.ringwald 
78572ea5239Smatthias.ringwald     // close low-level device
78672ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
787b429b9b7Smatthias.ringwald #endif
78872ea5239Smatthias.ringwald 
78972ea5239Smatthias.ringwald     // sleep mode
7903144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
79172ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
79272ea5239Smatthias.ringwald     }
793b429b9b7Smatthias.ringwald 
79472ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
7958d213e1aSmatthias.ringwald }
7968d213e1aSmatthias.ringwald 
79740d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
798b429b9b7Smatthias.ringwald 
7997b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
800b429b9b7Smatthias.ringwald 
801b429b9b7Smatthias.ringwald     // wake on
802b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
803b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
804b429b9b7Smatthias.ringwald     }
805b429b9b7Smatthias.ringwald 
806b429b9b7Smatthias.ringwald #if 0
807b429b9b7Smatthias.ringwald     // open low-level device
808b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
809b429b9b7Smatthias.ringwald     if (err){
8107d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
811b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
812b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
813b429b9b7Smatthias.ringwald         }
814b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
815b429b9b7Smatthias.ringwald         return err;
816b429b9b7Smatthias.ringwald     }
817b429b9b7Smatthias.ringwald #endif
818b429b9b7Smatthias.ringwald 
819b429b9b7Smatthias.ringwald     return 0;
820b429b9b7Smatthias.ringwald }
821b429b9b7Smatthias.ringwald 
822b429b9b7Smatthias.ringwald 
8238d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
824d661ed19Smatthias.ringwald 
8257b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
826d661ed19Smatthias.ringwald 
8278d213e1aSmatthias.ringwald     int err = 0;
8288d213e1aSmatthias.ringwald     switch (hci_stack.state){
8298d213e1aSmatthias.ringwald 
8308d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
8318d213e1aSmatthias.ringwald             switch (power_mode){
8328d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8338d213e1aSmatthias.ringwald                     err = hci_power_control_on();
8348d213e1aSmatthias.ringwald                     if (err) return err;
8357301ad89Smatthias.ringwald                     // set up state machine
8367301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
8377301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8387301ad89Smatthias.ringwald                     hci_stack.substate = 0;
8398d213e1aSmatthias.ringwald                     break;
8408d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8418d213e1aSmatthias.ringwald                     // do nothing
8428d213e1aSmatthias.ringwald                     break;
8438d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
844b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
8458d213e1aSmatthias.ringwald                     break;
8468d213e1aSmatthias.ringwald             }
8478d213e1aSmatthias.ringwald             break;
8487301ad89Smatthias.ringwald 
8498d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8508d213e1aSmatthias.ringwald             switch (power_mode){
8518d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8528d213e1aSmatthias.ringwald                     // do nothing
8538d213e1aSmatthias.ringwald                     break;
8548d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8558d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
8568d213e1aSmatthias.ringwald                     hci_power_control_off();
8578d213e1aSmatthias.ringwald                     break;
8588d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
859b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
86072ea5239Smatthias.ringwald                     hci_power_control_sleep();
8618d213e1aSmatthias.ringwald                     break;
8628d213e1aSmatthias.ringwald             }
8638d213e1aSmatthias.ringwald             break;
8647301ad89Smatthias.ringwald 
8658d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
8668d213e1aSmatthias.ringwald             switch (power_mode){
8678d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8688d213e1aSmatthias.ringwald                     // do nothing
8698d213e1aSmatthias.ringwald                     break;
8708d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
871c7e0c5f6Smatthias.ringwald                     // see hci_run
872c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8738d213e1aSmatthias.ringwald                     break;
8748d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
875b546ac54Smatthias.ringwald                     // see hci_run
876b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
87789db417bSmatthias.ringwald                     hci_stack.substate = 0;
8788d213e1aSmatthias.ringwald                     break;
8798d213e1aSmatthias.ringwald             }
8808d213e1aSmatthias.ringwald             break;
8817301ad89Smatthias.ringwald 
8828d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
8838d213e1aSmatthias.ringwald             switch (power_mode){
8848d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8858d213e1aSmatthias.ringwald                     // set up state machine
8868d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8878d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
8888d213e1aSmatthias.ringwald                     break;
8898d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8908d213e1aSmatthias.ringwald                     // do nothing
8918d213e1aSmatthias.ringwald                     break;
8928d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
893b546ac54Smatthias.ringwald                     // see hci_run
894b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
89589db417bSmatthias.ringwald                     hci_stack.substate = 0;
8968d213e1aSmatthias.ringwald                     break;
8978d213e1aSmatthias.ringwald             }
8988d213e1aSmatthias.ringwald             break;
8998d213e1aSmatthias.ringwald 
9008d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
9018d213e1aSmatthias.ringwald             switch (power_mode){
9028d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
90328171530Smatthias.ringwald 
90428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
90528171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
90628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9078747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
9088747d67fSmatthias.ringwald                         hci_stack.substate = 6;
90928171530Smatthias.ringwald                         break;
91028171530Smatthias.ringwald                     }
91128171530Smatthias.ringwald #endif
912b546ac54Smatthias.ringwald                     // set up state machine
913b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
914b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
915b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9168d213e1aSmatthias.ringwald                     break;
9178d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
918b546ac54Smatthias.ringwald                     // see hci_run
919b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9208d213e1aSmatthias.ringwald                     break;
9218d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
922b546ac54Smatthias.ringwald                     // do nothing
9238d213e1aSmatthias.ringwald                     break;
9248d213e1aSmatthias.ringwald             }
9258d213e1aSmatthias.ringwald             break;
9268d213e1aSmatthias.ringwald 
9278d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
9288d213e1aSmatthias.ringwald             switch (power_mode){
9298d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
93028171530Smatthias.ringwald 
93128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
93228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
93328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9348747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
9358747d67fSmatthias.ringwald                         hci_stack.substate = 6;
93628171530Smatthias.ringwald                         break;
93728171530Smatthias.ringwald                     }
93828171530Smatthias.ringwald #endif
9393144bce4Smatthias.ringwald                     err = hci_power_control_wake();
9403144bce4Smatthias.ringwald                     if (err) return err;
941b546ac54Smatthias.ringwald                     // set up state machine
942b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
943b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
944b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9458d213e1aSmatthias.ringwald                     break;
9468d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9474ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9488d213e1aSmatthias.ringwald                     break;
9498d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
950b546ac54Smatthias.ringwald                     // do nothing
9518d213e1aSmatthias.ringwald                     break;
9528d213e1aSmatthias.ringwald             }
9538d213e1aSmatthias.ringwald             break;
95411e23e5fSmatthias.ringwald     }
95568d92d03Smatthias.ringwald 
956038bc64cSmatthias.ringwald     // create internal event
957ee8bf225Smatthias.ringwald 	hci_emit_state();
958ee8bf225Smatthias.ringwald 
95968d92d03Smatthias.ringwald 	// trigger next/first action
96068d92d03Smatthias.ringwald 	hci_run();
96168d92d03Smatthias.ringwald 
962475c8125Smatthias.ringwald     return 0;
963475c8125Smatthias.ringwald }
964475c8125Smatthias.ringwald 
965381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
966381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
967381fbed8Smatthias.ringwald 
968381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
969381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
970381fbed8Smatthias.ringwald         return;
971381fbed8Smatthias.ringwald     }
972381fbed8Smatthias.ringwald 
973b031bebbSmatthias.ringwald     // store request to send command but accept in higher layer view
974b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 2 | enable; // 1 = inq scan, 2 = page scan
975381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
976b031bebbSmatthias.ringwald 
977b031bebbSmatthias.ringwald     hci_run();
978381fbed8Smatthias.ringwald }
979381fbed8Smatthias.ringwald 
98006b35ec0Smatthias.ringwald void hci_run(){
9818a485f27Smatthias.ringwald 
98232ab9390Smatthias.ringwald     hci_connection_t * connection;
98332ab9390Smatthias.ringwald     linked_item_t * it;
98432ab9390Smatthias.ringwald 
985ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
986ce4c8fabSmatthias.ringwald 
987b031bebbSmatthias.ringwald     // global/non-connection oriented commands
988b031bebbSmatthias.ringwald 
989b031bebbSmatthias.ringwald     // decline incoming connections
990ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
991ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
992ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
993ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
994ce4c8fabSmatthias.ringwald     }
995ce4c8fabSmatthias.ringwald 
996b031bebbSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
997b031bebbSmatthias.ringwald 
998b031bebbSmatthias.ringwald     // send scan enable
999eda18bf8Smatthias.ringwald     if (hci_stack.new_scan_enable_value != 0xff){
1000b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1001b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1002b031bebbSmatthias.ringwald     }
1003b031bebbSmatthias.ringwald 
100432ab9390Smatthias.ringwald     // send pending HCI commands
100532ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
100632ab9390Smatthias.ringwald 
1007b031bebbSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
100832ab9390Smatthias.ringwald 
1009b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
101032ab9390Smatthias.ringwald 
101132ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
10127b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
101332ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
101432ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1015c7e0c5f6Smatthias.ringwald         }
1016c7e0c5f6Smatthias.ringwald 
1017de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
101832ab9390Smatthias.ringwald 
101932ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
102032ab9390Smatthias.ringwald             link_key_t link_key;
10217b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
102232ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
102332ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
102432ab9390Smatthias.ringwald             } else {
102532ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
102632ab9390Smatthias.ringwald             }
102732ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
102832ab9390Smatthias.ringwald         }
102932ab9390Smatthias.ringwald     }
103032ab9390Smatthias.ringwald 
1031de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1032c7e0c5f6Smatthias.ringwald 
10333429f56bSmatthias.ringwald     switch (hci_stack.state){
10343429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10357b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
10363429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
10373429f56bSmatthias.ringwald                 // odd: waiting for command completion
103806b35ec0Smatthias.ringwald                 return;
10393429f56bSmatthias.ringwald             }
104090919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1041c7492964Smatthias.ringwald                 case 0: // RESET
104222909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
1043c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1044c7492964Smatthias.ringwald                         // skip baud change
1045c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1046c7492964Smatthias.ringwald                     }
10473429f56bSmatthias.ringwald                     break;
1048c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
10497dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
10507dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1051c7492964Smatthias.ringwald                     break;
1052c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1053c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1054c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1055c7492964Smatthias.ringwald                     // break missing here for fall through
1056c7492964Smatthias.ringwald 
1057c7492964Smatthias.ringwald                 case 3:
105890919203Smatthias.ringwald                     // custom initialization
1059db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
10607dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1061d62aca9fSmatthias.ringwald                         if (valid_cmd){
10627dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
10637dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1064c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
106590919203Smatthias.ringwald                             break;
106690919203Smatthias.ringwald                         }
1067d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
106890919203Smatthias.ringwald                     }
10692f6c30e1Smatthias.ringwald                     // otherwise continue
1070f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1071f432a6ddSmatthias.ringwald 					break;
1072c7492964Smatthias.ringwald 				case 4:
1073e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1074e2edc0c3Smatthias.ringwald 					break;
1075c7492964Smatthias.ringwald                 case 5:
10763429f56bSmatthias.ringwald                     // ca. 15 sec
10773429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
10783429f56bSmatthias.ringwald                     break;
1079c7492964Smatthias.ringwald 				case 6:
10800a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
1081f432a6ddSmatthias.ringwald 					break;
1082c7492964Smatthias.ringwald                 case 7:
10838a485f27Smatthias.ringwald #ifndef EMBEDDED
10848a485f27Smatthias.ringwald                 {
10858a485f27Smatthias.ringwald                     char hostname[30];
10868a485f27Smatthias.ringwald                     gethostname(hostname, 30);
10878a485f27Smatthias.ringwald                     hostname[29] = '\0';
10888a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
10898a485f27Smatthias.ringwald                     break;
10908a485f27Smatthias.ringwald                 }
1091c7492964Smatthias.ringwald                 case 8:
10923c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
10933c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
10943c4d4b90Smatthias.ringwald                     break;
10953c4d4b90Smatthias.ringwald 
1096c7492964Smatthias.ringwald                 case 9:
10973c4d4b90Smatthias.ringwald #endif
10988a485f27Smatthias.ringwald #endif
10993429f56bSmatthias.ringwald                     // done.
11003429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1101b360b6adSmatthias.ringwald                     hci_emit_state();
11023429f56bSmatthias.ringwald                     break;
11033429f56bSmatthias.ringwald                 default:
11043429f56bSmatthias.ringwald                     break;
1105475c8125Smatthias.ringwald             }
11063429f56bSmatthias.ringwald             hci_stack.substate++;
11073429f56bSmatthias.ringwald             break;
1108c7e0c5f6Smatthias.ringwald 
1109c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1110c7e0c5f6Smatthias.ringwald 
11117b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1112c7e0c5f6Smatthias.ringwald             // close all open connections
1113c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1114c7e0c5f6Smatthias.ringwald             if (connection){
111532ab9390Smatthias.ringwald 
1116c7e0c5f6Smatthias.ringwald                 // send disconnect
111732ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
111832ab9390Smatthias.ringwald 
111979bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11206ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1121c7e0c5f6Smatthias.ringwald 
1122c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1123c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1124c7e0c5f6Smatthias.ringwald                 return;
1125c7e0c5f6Smatthias.ringwald             }
11267b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1127c7e0c5f6Smatthias.ringwald 
112872ea5239Smatthias.ringwald             // switch mode
1129c7e0c5f6Smatthias.ringwald             hci_power_control_off();
11309418f9c9Smatthias.ringwald 
11317b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
113272ea5239Smatthias.ringwald             hci_emit_state();
11337b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
113472ea5239Smatthias.ringwald             break;
1135c7e0c5f6Smatthias.ringwald 
113672ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
113789db417bSmatthias.ringwald             switch(hci_stack.substate) {
113889db417bSmatthias.ringwald                 case 0:
11397b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
114072ea5239Smatthias.ringwald                     // close all open connections
114172ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
114266da7044Smatthias.ringwald 
114328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
114466da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
114566da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
114666da7044Smatthias.ringwald                         connection = NULL;
114766da7044Smatthias.ringwald                     }
114866da7044Smatthias.ringwald #endif
114972ea5239Smatthias.ringwald                     if (connection){
115032ab9390Smatthias.ringwald 
115172ea5239Smatthias.ringwald                         // send disconnect
115232ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
115332ab9390Smatthias.ringwald 
115479bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11556ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
115672ea5239Smatthias.ringwald 
115772ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
115872ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
115972ea5239Smatthias.ringwald                         return;
116072ea5239Smatthias.ringwald                     }
116172ea5239Smatthias.ringwald 
116289db417bSmatthias.ringwald                     // disable page and inquiry scan
116332ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
116432ab9390Smatthias.ringwald 
11657b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
116689db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
116789db417bSmatthias.ringwald 
116889db417bSmatthias.ringwald                     // continue in next sub state
116989db417bSmatthias.ringwald                     hci_stack.substate++;
117089db417bSmatthias.ringwald                     break;
117189db417bSmatthias.ringwald                 case 1:
117289db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
117389db417bSmatthias.ringwald                     break;
117489db417bSmatthias.ringwald                 case 2:
11757b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
117628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
117728171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
117828171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
117928171530Smatthias.ringwald                         // SLEEP MODE reached
118028171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
118128171530Smatthias.ringwald                         hci_emit_state();
118228171530Smatthias.ringwald                         break;
118328171530Smatthias.ringwald                     }
118428171530Smatthias.ringwald #endif
118572ea5239Smatthias.ringwald                     // switch mode
118689db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1187c7e0c5f6Smatthias.ringwald                     hci_emit_state();
118828171530Smatthias.ringwald                     break;
118928171530Smatthias.ringwald 
119089db417bSmatthias.ringwald                 default:
119189db417bSmatthias.ringwald                     break;
119289db417bSmatthias.ringwald             }
1193c7e0c5f6Smatthias.ringwald             break;
1194c7e0c5f6Smatthias.ringwald 
11953429f56bSmatthias.ringwald         default:
11963429f56bSmatthias.ringwald             break;
11971f504dbdSmatthias.ringwald     }
11983429f56bSmatthias.ringwald }
119916833f0aSmatthias.ringwald 
120031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1201c8e4258aSmatthias.ringwald     bd_addr_t addr;
1202c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1203c8e4258aSmatthias.ringwald     // house-keeping
1204c8e4258aSmatthias.ringwald 
1205c8e4258aSmatthias.ringwald     // create_connection?
1206c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1207c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1208339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1209c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1210c8e4258aSmatthias.ringwald         if (conn) {
1211c8e4258aSmatthias.ringwald             // if connection exists
1212c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
121317f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
121417f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1215c8e4258aSmatthias.ringwald             }
121617f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1217c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1218c8e4258aSmatthias.ringwald 
121917f1ba2aSmatthias.ringwald         }
1220c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
122117f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
122217f1ba2aSmatthias.ringwald         if (!conn){
122317f1ba2aSmatthias.ringwald             // notify client that alloc failed
122417f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
122517f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
122617f1ba2aSmatthias.ringwald         }
1227c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1228c8e4258aSmatthias.ringwald     }
1229c8e4258aSmatthias.ringwald 
12307fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
12317fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
12327fde4af9Smatthias.ringwald     }
12337fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
12347fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
12357fde4af9Smatthias.ringwald     }
12367fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
12377fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
12387fde4af9Smatthias.ringwald     }
12397fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
12407fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
12417fde4af9Smatthias.ringwald     }
12427fde4af9Smatthias.ringwald 
12438ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
12448ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
12458ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
12468ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
12478ef73945Smatthias.ringwald         }
12488ef73945Smatthias.ringwald     }
1249c8e4258aSmatthias.ringwald 
125031452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1251622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
125231452debSmatthias.ringwald }
12538adf0ddaSmatthias.ringwald 
12541cd208adSmatthias.ringwald /**
12551cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
12561cd208adSmatthias.ringwald  */
1257fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
12581cd208adSmatthias.ringwald     va_list argptr;
12591cd208adSmatthias.ringwald     va_start(argptr, cmd);
12607dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
12611cd208adSmatthias.ringwald     va_end(argptr);
12627dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
126393b8dc03Smatthias.ringwald }
1264c8e4258aSmatthias.ringwald 
1265ee091cf1Smatthias.ringwald // Create various non-HCI events.
1266ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1267ee091cf1Smatthias.ringwald 
1268c8e4258aSmatthias.ringwald void hci_emit_state(){
1269425d1371Smatthias.ringwald     uint8_t event[3];
127080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1271425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1272c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1273425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1274425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1275c8e4258aSmatthias.ringwald }
1276c8e4258aSmatthias.ringwald 
127717f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1278425d1371Smatthias.ringwald     uint8_t event[13];
1279c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1280425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
128117f1ba2aSmatthias.ringwald     event[2] = status;
1282c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1283c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1284c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1285c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1286425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1287425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1288c8e4258aSmatthias.ringwald }
1289c8e4258aSmatthias.ringwald 
12903c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1291425d1371Smatthias.ringwald     uint8_t event[6];
12923c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1293e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
12943c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
12953c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
12963c4d4b90Smatthias.ringwald     event[5] = reason;
1297425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1298425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
12993c4d4b90Smatthias.ringwald }
13003c4d4b90Smatthias.ringwald 
1301ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1302425d1371Smatthias.ringwald     uint8_t event[4];
130380d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1304425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1305ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1306425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1307425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1308ee091cf1Smatthias.ringwald }
130943bfb1bdSmatthias.ringwald 
131043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1311425d1371Smatthias.ringwald     uint8_t event[3];
131280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1313425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
131443bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1315425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1316425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
131743bfb1bdSmatthias.ringwald }
1318038bc64cSmatthias.ringwald 
1319038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1320425d1371Smatthias.ringwald     uint8_t event[2];
132180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1322425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1323425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1324425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1325038bc64cSmatthias.ringwald }
13261b0e3922Smatthias.ringwald 
132709ba8edeSmatthias.ringwald #ifndef EMBEDDED
13281b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1329425d1371Smatthias.ringwald     uint8_t event[6];
13301b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1331425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1332425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1333425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1334425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1335425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1336425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13371b0e3922Smatthias.ringwald }
133809ba8edeSmatthias.ringwald #endif
13391b0e3922Smatthias.ringwald 
13402ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1341425d1371Smatthias.ringwald     uint8_t event[3];
13422ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1343425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
13442ed6235cSmatthias.ringwald     event[2] = enabled;
1345425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1346e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13472ed6235cSmatthias.ringwald }
1348627c2f45Smatthias.ringwald 
1349627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1350425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1351627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1352425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1353f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
13542f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1355f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1356425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1357425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1358627c2f45Smatthias.ringwald }
1359381fbed8Smatthias.ringwald 
1360381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1361425d1371Smatthias.ringwald     uint8_t event[3];
1362381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1363425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1364381fbed8Smatthias.ringwald     event[2] = enabled;
1365425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1366425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1367381fbed8Smatthias.ringwald }
1368