xref: /btstack/src/hci.c (revision 188981d28a157e81d77b76c0ef07b35348444a65)
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 
64758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
65758b46ceSmatthias.ringwald 
6606b35ec0Smatthias.ringwald // the STACK is here
6716833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
6816833f0aSmatthias.ringwald 
6997addcc5Smatthias.ringwald /**
70ee091cf1Smatthias.ringwald  * get connection for a given handle
71ee091cf1Smatthias.ringwald  *
72ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
73ee091cf1Smatthias.ringwald  */
74645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
75ee091cf1Smatthias.ringwald     linked_item_t *it;
76ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
77ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
78ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
79ee091cf1Smatthias.ringwald         }
80ee091cf1Smatthias.ringwald     }
81ee091cf1Smatthias.ringwald     return NULL;
82ee091cf1Smatthias.ringwald }
83ee091cf1Smatthias.ringwald 
842b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
85ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
86c785ef68Smatthias.ringwald #ifdef HAVE_TIME
87ee091cf1Smatthias.ringwald     struct timeval tv;
88ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
89c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
90ee091cf1Smatthias.ringwald         // connections might be timed out
91ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
92ee091cf1Smatthias.ringwald     }
932b12a0b9Smatthias.ringwald #endif
94e5780900Smatthias.ringwald #ifdef HAVE_TICK
95c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
96c785ef68Smatthias.ringwald         // connections might be timed out
97c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
98c785ef68Smatthias.ringwald     }
99c785ef68Smatthias.ringwald #endif
100c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
101c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
102c785ef68Smatthias.ringwald }
103ee091cf1Smatthias.ringwald 
104ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
105c7492964Smatthias.ringwald #ifdef HAVE_TIME
106ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
107c7492964Smatthias.ringwald #endif
108e5780900Smatthias.ringwald #ifdef HAVE_TICK
109c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
110c785ef68Smatthias.ringwald #endif
111ee091cf1Smatthias.ringwald }
112ee091cf1Smatthias.ringwald 
113ee091cf1Smatthias.ringwald /**
114c8e4258aSmatthias.ringwald  * create connection for given address
115c8e4258aSmatthias.ringwald  *
11617f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
117c8e4258aSmatthias.ringwald  */
118c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
119a3b02b71Smatthias.ringwald     hci_connection_t * conn = btstack_memory_hci_connection_get();
120c8e4258aSmatthias.ringwald     if (!conn) return NULL;
121c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
122c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1237d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
124ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
125ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
126ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
127d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1287856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
12956cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
130c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
131c8e4258aSmatthias.ringwald     return conn;
132c8e4258aSmatthias.ringwald }
133c8e4258aSmatthias.ringwald 
134c8e4258aSmatthias.ringwald /**
13506b35ec0Smatthias.ringwald  * get connection for given address
13697addcc5Smatthias.ringwald  *
13797addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
13897addcc5Smatthias.ringwald  */
139fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
14006b35ec0Smatthias.ringwald     linked_item_t *it;
14106b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
14206b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
14306b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
14406b35ec0Smatthias.ringwald         }
14506b35ec0Smatthias.ringwald     }
14606b35ec0Smatthias.ringwald     return NULL;
14706b35ec0Smatthias.ringwald }
14806b35ec0Smatthias.ringwald 
14943bfb1bdSmatthias.ringwald /**
15080ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1517fde4af9Smatthias.ringwald  */
1527fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1537fde4af9Smatthias.ringwald     bd_addr_t addr;
1547fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1557fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1567fde4af9Smatthias.ringwald     if (conn) {
1577fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
15880ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1597fde4af9Smatthias.ringwald     }
1607fde4af9Smatthias.ringwald }
1617fde4af9Smatthias.ringwald 
16280ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
16380ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
16480ca58a0Smatthias.ringwald     if (!conn) return 0;
16580ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
16680ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
16780ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
16880ca58a0Smatthias.ringwald     return 1;
16980ca58a0Smatthias.ringwald }
17080ca58a0Smatthias.ringwald 
171c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
172c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
173c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
174c12e46e7Smatthias.ringwald     }
175c12e46e7Smatthias.ringwald }
176c12e46e7Smatthias.ringwald 
1777fde4af9Smatthias.ringwald 
1787fde4af9Smatthias.ringwald /**
17943bfb1bdSmatthias.ringwald  * count connections
18043bfb1bdSmatthias.ringwald  */
18140d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
18256c253c9Smatthias.ringwald     int count = 0;
18343bfb1bdSmatthias.ringwald     linked_item_t *it;
18443bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
18543bfb1bdSmatthias.ringwald     return count;
18643bfb1bdSmatthias.ringwald }
187c8e4258aSmatthias.ringwald 
18897addcc5Smatthias.ringwald /**
189ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
19016833f0aSmatthias.ringwald  */
1912718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
19216833f0aSmatthias.ringwald }
19316833f0aSmatthias.ringwald 
194998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
195998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
196998906cdSmatthias.ringwald     if (!connection) {
1977d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
198998906cdSmatthias.ringwald         return 0;
199998906cdSmatthias.ringwald     }
200998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
201998906cdSmatthias.ringwald }
202998906cdSmatthias.ringwald 
203998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
204998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
205998906cdSmatthias.ringwald     linked_item_t *it;
206998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
207998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
208998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2097d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
210998906cdSmatthias.ringwald             return 0;
211998906cdSmatthias.ringwald         }
212998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
213998906cdSmatthias.ringwald     }
214998906cdSmatthias.ringwald     return free_slots;
215998906cdSmatthias.ringwald }
216998906cdSmatthias.ringwald 
217c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2182b12a0b9Smatthias.ringwald 
2192b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2202b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2212b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2222b12a0b9Smatthias.ringwald             return 0;
2232b12a0b9Smatthias.ringwald         }
2242b12a0b9Smatthias.ringwald     }
2252b12a0b9Smatthias.ringwald 
2262b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
227c24735b1Smatthias.ringwald     switch (packet_type) {
228c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
229c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
230c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
231de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
232c24735b1Smatthias.ringwald         default:
233c24735b1Smatthias.ringwald             return 0;
234c24735b1Smatthias.ringwald     }
235c24735b1Smatthias.ringwald }
236c24735b1Smatthias.ringwald 
237ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2387856c818Smatthias.ringwald 
2396218e6f1Smatthias.ringwald     // check for free places on BT module
2405932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2416218e6f1Smatthias.ringwald 
2427856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2437856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
24456cf178bSmatthias.ringwald     if (!connection) return 0;
24556cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
24656cf178bSmatthias.ringwald 
24756cf178bSmatthias.ringwald     // count packet
24856cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2497b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2507856c818Smatthias.ringwald 
25100d8e42eSmatthias.ringwald     // send packet
25200d8e42eSmatthias.ringwald     int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2536218e6f1Smatthias.ringwald 
25400d8e42eSmatthias.ringwald     return err;
255ee091cf1Smatthias.ringwald }
256ee091cf1Smatthias.ringwald 
25716833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2587856c818Smatthias.ringwald 
2597856c818Smatthias.ringwald     // get info
2607856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2617856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2627856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2637856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2647856c818Smatthias.ringwald 
2657856c818Smatthias.ringwald     // ignore non-registered handle
2667856c818Smatthias.ringwald     if (!conn){
2677d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2687856c818Smatthias.ringwald         return;
2697856c818Smatthias.ringwald     }
2707856c818Smatthias.ringwald 
2717856c818Smatthias.ringwald     // update idle timestamp
2727856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2737856c818Smatthias.ringwald 
2747856c818Smatthias.ringwald     // handle different packet types
2757856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2767856c818Smatthias.ringwald 
2777856c818Smatthias.ringwald         case 0x01: // continuation fragment
2787856c818Smatthias.ringwald 
2797856c818Smatthias.ringwald             // sanity check
2807856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2817d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2827856c818Smatthias.ringwald                 return;
2837856c818Smatthias.ringwald             }
2847856c818Smatthias.ringwald 
2857856c818Smatthias.ringwald             // append fragment payload (header already stored)
2867856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2877856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2887856c818Smatthias.ringwald 
2897d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
290decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
2917856c818Smatthias.ringwald 
2927856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2937856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2947856c818Smatthias.ringwald 
2952718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2967856c818Smatthias.ringwald                 // reset recombination buffer
2977856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2987856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2997856c818Smatthias.ringwald             }
3007856c818Smatthias.ringwald             break;
3017856c818Smatthias.ringwald 
3027856c818Smatthias.ringwald         case 0x02: { // first fragment
3037856c818Smatthias.ringwald 
3047856c818Smatthias.ringwald             // sanity check
3057856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3067d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3077856c818Smatthias.ringwald                 return;
3087856c818Smatthias.ringwald             }
3097856c818Smatthias.ringwald 
3107856c818Smatthias.ringwald             // peek into L2CAP packet!
3117856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3127856c818Smatthias.ringwald 
3137d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
314decc01a8Smatthias.ringwald 
3157856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3167856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3177856c818Smatthias.ringwald 
3187856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3192718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3207856c818Smatthias.ringwald 
3217856c818Smatthias.ringwald             } else {
3227856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3237856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3247856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3257856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
326decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3277856c818Smatthias.ringwald             }
3287856c818Smatthias.ringwald             break;
3297856c818Smatthias.ringwald 
3307856c818Smatthias.ringwald         }
3317856c818Smatthias.ringwald         default:
3327d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3337856c818Smatthias.ringwald             return;
3347856c818Smatthias.ringwald     }
33594ab26f8Smatthias.ringwald 
33694ab26f8Smatthias.ringwald     // execute main loop
33794ab26f8Smatthias.ringwald     hci_run();
33816833f0aSmatthias.ringwald }
33922909952Smatthias.ringwald 
34067a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
341339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3423c4d4b90Smatthias.ringwald 
3433c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3443c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3453c4d4b90Smatthias.ringwald 
346c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
347c785ef68Smatthias.ringwald 
348c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
349a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3503c4d4b90Smatthias.ringwald 
3513c4d4b90Smatthias.ringwald     // now it's gone
352c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
353c7e0c5f6Smatthias.ringwald }
354c7e0c5f6Smatthias.ringwald 
3550c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3568f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3578f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3588f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3598f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3608f8108aaSmatthias.ringwald };
3618f8108aaSmatthias.ringwald 
3628f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){
3638f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
3648f8108aaSmatthias.ringwald     int i;
3658f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
3668f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
3678f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
3688f8108aaSmatthias.ringwald             packet_types |= 1 << i;
3698f8108aaSmatthias.ringwald         }
3708f8108aaSmatthias.ringwald     }
3718f8108aaSmatthias.ringwald     // flip bits for "may not be used"
3728f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
3738f8108aaSmatthias.ringwald     return packet_types;
3748f8108aaSmatthias.ringwald }
3758f8108aaSmatthias.ringwald 
3768f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
3778f8108aaSmatthias.ringwald     return hci_stack.packet_types;
3788f8108aaSmatthias.ringwald }
3798f8108aaSmatthias.ringwald 
3807dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
3817dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
3827dc17943Smatthias.ringwald     return hci_stack.hci_packet_buffer;
3837dc17943Smatthias.ringwald }
3847dc17943Smatthias.ringwald 
3857dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
3867dc17943Smatthias.ringwald     return hci_stack.acl_data_packet_length;
3877dc17943Smatthias.ringwald }
3887dc17943Smatthias.ringwald 
38974ec757aSmatthias.ringwald // avoid huge local variables
390223aafc1Smatthias.ringwald #ifndef EMBEDDED
39174ec757aSmatthias.ringwald static device_name_t device_name;
392223aafc1Smatthias.ringwald #endif
39316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3941281a47eSmatthias.ringwald     bd_addr_t addr;
3952d00edd4Smatthias.ringwald     uint8_t link_type;
396fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3971f7b95a1Smatthias.ringwald     hci_connection_t * conn;
39856cf178bSmatthias.ringwald     int i;
39922909952Smatthias.ringwald 
4005909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
4015909f7f2Smatthias.ringwald 
4026772a24cSmatthias.ringwald     switch (packet[0]) {
40322909952Smatthias.ringwald 
4046772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
4057ec5eeaaSmatthias.ringwald             // get num cmd packets
4067b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
4077ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
4087ec5eeaaSmatthias.ringwald 
409e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
410e2edc0c3Smatthias.ringwald                 // from offset 5
411e2edc0c3Smatthias.ringwald                 // status
4121d279b20Smatthias.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"
413e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
41456cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
415e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
41656cf178bSmatthias.ringwald                 // ignore: total num SCO packets
41756cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
418a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
4198fcba05dSmatthias.ringwald                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
4208fcba05dSmatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
4218f8108aaSmatthias.ringwald                     }
422a7a04bd9Smatthias.ringwald                     // determine usable ACL packet types
42328c93ceeSmatthias.ringwald                     hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length);
4248f8108aaSmatthias.ringwald 
4258fcba05dSmatthias.ringwald                     log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n",
4268f8108aaSmatthias.ringwald                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types);
427e2edc0c3Smatthias.ringwald                 }
42856cf178bSmatthias.ringwald             }
429*188981d2Smatthias.ringwald             // Dump local address
430*188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
431*188981d2Smatthias.ringwald                 bd_addr_t addr;
432*188981d2Smatthias.ringwald                 bt_flip_addr(addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
433*188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
434*188981d2Smatthias.ringwald                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(addr));
435*188981d2Smatthias.ringwald             }
436381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
437381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
438381fbed8Smatthias.ringwald             }
43956cf178bSmatthias.ringwald             break;
44056cf178bSmatthias.ringwald 
4417ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4427ec5eeaaSmatthias.ringwald             // get num cmd packets
4437b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4447ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4457ec5eeaaSmatthias.ringwald             break;
4467ec5eeaaSmatthias.ringwald 
44756cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
44856cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
44956cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
45056cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
45156cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
45256cf178bSmatthias.ringwald                 if (!conn){
4537d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
45456cf178bSmatthias.ringwald                     continue;
45556cf178bSmatthias.ringwald                 }
45656cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4577b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
45856cf178bSmatthias.ringwald             }
4596772a24cSmatthias.ringwald             break;
4606772a24cSmatthias.ringwald 
4611f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
46237eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
46337eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4642d00edd4Smatthias.ringwald             link_type = packet[11];
465339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
46637eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4671f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4681f7b95a1Smatthias.ringwald                 if (!conn) {
4691f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4701f7b95a1Smatthias.ringwald                 }
471ce4c8fabSmatthias.ringwald                 if (!conn) {
472ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
473ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
474ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
475ce4c8fabSmatthias.ringwald                     break;
476ce4c8fabSmatthias.ringwald                 }
47732ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
47832ab9390Smatthias.ringwald                 hci_run();
47937eaa4cfSmatthias.ringwald             } else {
480ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
481ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
482ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
48337eaa4cfSmatthias.ringwald             }
4841f7b95a1Smatthias.ringwald             break;
4851f7b95a1Smatthias.ringwald 
4866772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
487fe1ed1b8Smatthias.ringwald             // Connection management
488fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
489339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
4901f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
491fe1ed1b8Smatthias.ringwald             if (conn) {
492b448a0e7Smatthias.ringwald                 if (!packet[2]){
493c8e4258aSmatthias.ringwald                     conn->state = OPEN;
494fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
495ee091cf1Smatthias.ringwald 
496c785ef68Smatthias.ringwald                     // restart timer
497c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
498ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
499c785ef68Smatthias.ringwald 
500339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
50143bfb1bdSmatthias.ringwald 
50243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
503b448a0e7Smatthias.ringwald                 } else {
504b448a0e7Smatthias.ringwald                     // connection failed, remove entry
505b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
506a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
507c12e46e7Smatthias.ringwald 
508c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
509c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
510c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
511c12e46e7Smatthias.ringwald                     }
512fe1ed1b8Smatthias.ringwald                 }
513fe1ed1b8Smatthias.ringwald             }
5146772a24cSmatthias.ringwald             break;
515fe1ed1b8Smatthias.ringwald 
5167fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5177b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5187fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
51974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
52032ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
52164472d52Smatthias.ringwald             hci_run();
522d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
52329d53098Smatthias.ringwald             return;
5247fde4af9Smatthias.ringwald 
5257fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5267fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
52774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
52829d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
529287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
53029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5317fde4af9Smatthias.ringwald             break;
5327fde4af9Smatthias.ringwald 
5337fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5347fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
535d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
536d9a327f9Smatthias.ringwald             if (!hci_stack.remote_device_db) break;
537d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
538d9a327f9Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
5397fde4af9Smatthias.ringwald             break;
5407fde4af9Smatthias.ringwald 
541223aafc1Smatthias.ringwald #ifndef EMBEDDED
54274ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
54374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
54474ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
54574ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5465a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5475a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5485a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5495a06394aSmatthias.ringwald                     packet[9+i] = 0;
5505a06394aSmatthias.ringwald                     break;
551cdc9101dSmatthias.ringwald                 }
5525a06394aSmatthias.ringwald             }
553d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
55474ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
55574ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
55674ec757aSmatthias.ringwald             break;
55774ec757aSmatthias.ringwald 
55874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
55974ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
56074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
56174ec757aSmatthias.ringwald             // first send inq result packet
56274ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
56374ec757aSmatthias.ringwald             // then send cached remote names
56474ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
56574ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
56674ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
56774ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
56874ec757aSmatthias.ringwald                 }
56974ec757aSmatthias.ringwald             }
57074ec757aSmatthias.ringwald             return;
571223aafc1Smatthias.ringwald #endif
57274ec757aSmatthias.ringwald 
5736772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
574fe1ed1b8Smatthias.ringwald             if (!packet[2]){
575fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
576fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
577fe1ed1b8Smatthias.ringwald                 if (conn) {
578c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
579fe1ed1b8Smatthias.ringwald                 }
580fe1ed1b8Smatthias.ringwald             }
5816772a24cSmatthias.ringwald             break;
5826772a24cSmatthias.ringwald 
583c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
584c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
585c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
586c68bdf90Smatthias.ringwald             }
587c68bdf90Smatthias.ringwald             break;
588c68bdf90Smatthias.ringwald 
5895909f7f2Smatthias.ringwald #ifdef HAVE_BLE
5905909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
5915909f7f2Smatthias.ringwald             switch (packet[2]) {
5925909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
5935909f7f2Smatthias.ringwald                     // Connection management
5945909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
5955909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
5965909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
5975909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
5985909f7f2Smatthias.ringwald                     if (packet[3]){
5995909f7f2Smatthias.ringwald                         if (conn){
6005909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
6015909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
6025909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
6035909f7f2Smatthias.ringwald 
6045909f7f2Smatthias.ringwald                         }
6055909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
6065909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
6075909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
6085909f7f2Smatthias.ringwald                         }
6095909f7f2Smatthias.ringwald                         break;
6105909f7f2Smatthias.ringwald                     }
6115909f7f2Smatthias.ringwald                     if (!conn){
6125909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
6135909f7f2Smatthias.ringwald                     }
6145909f7f2Smatthias.ringwald                     if (!conn){
6155909f7f2Smatthias.ringwald                         // no memory
6165909f7f2Smatthias.ringwald                         break;
6175909f7f2Smatthias.ringwald                     }
6185909f7f2Smatthias.ringwald 
6195909f7f2Smatthias.ringwald                     conn->state = OPEN;
6205909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
6215909f7f2Smatthias.ringwald 
6225909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
6235909f7f2Smatthias.ringwald 
6245909f7f2Smatthias.ringwald                     // restart timer
6255909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
6265909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
6275909f7f2Smatthias.ringwald 
6285909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
6295909f7f2Smatthias.ringwald 
6305909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
6315909f7f2Smatthias.ringwald                     break;
6325909f7f2Smatthias.ringwald 
6335909f7f2Smatthias.ringwald                 default:
6345909f7f2Smatthias.ringwald                     break;
6355909f7f2Smatthias.ringwald             }
6365909f7f2Smatthias.ringwald             break;
6375909f7f2Smatthias.ringwald #endif
6385909f7f2Smatthias.ringwald 
6396772a24cSmatthias.ringwald         default:
6406772a24cSmatthias.ringwald             break;
641fe1ed1b8Smatthias.ringwald     }
642fe1ed1b8Smatthias.ringwald 
6433429f56bSmatthias.ringwald     // handle BT initialization
6443429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
6457301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
6467301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
6477301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
6487301ad89Smatthias.ringwald         // }
6497301ad89Smatthias.ringwald         // handle normal init sequence
6503429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
6513429f56bSmatthias.ringwald             // odd: waiting for event
6523429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
6533429f56bSmatthias.ringwald                 hci_stack.substate++;
6543429f56bSmatthias.ringwald             }
6553429f56bSmatthias.ringwald         }
65622909952Smatthias.ringwald     }
65722909952Smatthias.ringwald 
65889db417bSmatthias.ringwald     // help with BT sleep
65989db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
66089db417bSmatthias.ringwald         && hci_stack.substate == 1
66189db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
66289db417bSmatthias.ringwald         hci_stack.substate++;
66389db417bSmatthias.ringwald     }
66489db417bSmatthias.ringwald 
6652718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
66694ab26f8Smatthias.ringwald 
66794ab26f8Smatthias.ringwald 	// execute main loop
66894ab26f8Smatthias.ringwald 	hci_run();
66916833f0aSmatthias.ringwald }
67016833f0aSmatthias.ringwald 
67110e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
67210e830c9Smatthias.ringwald     switch (packet_type) {
67310e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
67410e830c9Smatthias.ringwald             event_handler(packet, size);
67510e830c9Smatthias.ringwald             break;
67610e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
67710e830c9Smatthias.ringwald             acl_handler(packet, size);
67810e830c9Smatthias.ringwald             break;
67910e830c9Smatthias.ringwald         default:
68010e830c9Smatthias.ringwald             break;
68110e830c9Smatthias.ringwald     }
68210e830c9Smatthias.ringwald }
68310e830c9Smatthias.ringwald 
684fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
6852718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
6862718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
68716833f0aSmatthias.ringwald }
68816833f0aSmatthias.ringwald 
6894f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
690475c8125Smatthias.ringwald 
691475c8125Smatthias.ringwald     // reference to use transport layer implementation
69216833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
693475c8125Smatthias.ringwald 
69411e23e5fSmatthias.ringwald     // references to used control implementation
69511e23e5fSmatthias.ringwald     hci_stack.control = control;
69611e23e5fSmatthias.ringwald 
69711e23e5fSmatthias.ringwald     // reference to used config
69811e23e5fSmatthias.ringwald     hci_stack.config = config;
69911e23e5fSmatthias.ringwald 
700fe1ed1b8Smatthias.ringwald     // no connections yet
701fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
7020a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
703c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
704758b46ceSmatthias.ringwald 
705b031bebbSmatthias.ringwald     // no pending cmds
706b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
707b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
708b031bebbSmatthias.ringwald 
70916833f0aSmatthias.ringwald     // higher level handler
7102718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
71116833f0aSmatthias.ringwald 
712404843c1Smatthias.ringwald     // store and open remote device db
713404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
714404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
715404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
716404843c1Smatthias.ringwald     }
71729d53098Smatthias.ringwald 
7188fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
7198fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7208fcba05dSmatthias.ringwald 
72116833f0aSmatthias.ringwald     // register packet handlers with transport
72210e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
723f5454fc6Smatthias.ringwald 
724f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
725475c8125Smatthias.ringwald }
726475c8125Smatthias.ringwald 
727404843c1Smatthias.ringwald void hci_close(){
728404843c1Smatthias.ringwald     // close remote device db
729404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
730404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
731404843c1Smatthias.ringwald     }
732f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
733f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
734f5454fc6Smatthias.ringwald }
735f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
736404843c1Smatthias.ringwald }
737404843c1Smatthias.ringwald 
7388d213e1aSmatthias.ringwald // State-Module-Driver overview
7398d213e1aSmatthias.ringwald // state                    module  low-level
7408d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
7418d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
7428d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
7438d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
744d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
745d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
746c7e0c5f6Smatthias.ringwald 
74740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
7487301ad89Smatthias.ringwald 
749038bc64cSmatthias.ringwald     // power on
750f9a30166Smatthias.ringwald     int err = 0;
751f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
752f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
753f9a30166Smatthias.ringwald     }
754038bc64cSmatthias.ringwald     if (err){
7557d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
756038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
757038bc64cSmatthias.ringwald         return err;
758038bc64cSmatthias.ringwald     }
759038bc64cSmatthias.ringwald 
760038bc64cSmatthias.ringwald     // open low-level device
761038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
762038bc64cSmatthias.ringwald     if (err){
7637d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
764f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
765f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
766f9a30166Smatthias.ringwald         }
767038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
768038bc64cSmatthias.ringwald         return err;
769038bc64cSmatthias.ringwald     }
7708d213e1aSmatthias.ringwald     return 0;
7718d213e1aSmatthias.ringwald }
772038bc64cSmatthias.ringwald 
77340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
7748d213e1aSmatthias.ringwald 
7757b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
7769418f9c9Smatthias.ringwald 
7778d213e1aSmatthias.ringwald     // close low-level device
7788d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
7798d213e1aSmatthias.ringwald 
7807b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
7819418f9c9Smatthias.ringwald 
7828d213e1aSmatthias.ringwald     // power off
7838d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
7848d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
7858d213e1aSmatthias.ringwald     }
7869418f9c9Smatthias.ringwald 
7877b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
7889418f9c9Smatthias.ringwald 
78972ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
79072ea5239Smatthias.ringwald }
79172ea5239Smatthias.ringwald 
79240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
79372ea5239Smatthias.ringwald 
7947b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
7953144bce4Smatthias.ringwald 
796b429b9b7Smatthias.ringwald #if 0
797b429b9b7Smatthias.ringwald     // don't close serial port during sleep
798b429b9b7Smatthias.ringwald 
79972ea5239Smatthias.ringwald     // close low-level device
80072ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
801b429b9b7Smatthias.ringwald #endif
80272ea5239Smatthias.ringwald 
80372ea5239Smatthias.ringwald     // sleep mode
8043144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
80572ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
80672ea5239Smatthias.ringwald     }
807b429b9b7Smatthias.ringwald 
80872ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
8098d213e1aSmatthias.ringwald }
8108d213e1aSmatthias.ringwald 
81140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
812b429b9b7Smatthias.ringwald 
8137b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
814b429b9b7Smatthias.ringwald 
815b429b9b7Smatthias.ringwald     // wake on
816b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
817b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
818b429b9b7Smatthias.ringwald     }
819b429b9b7Smatthias.ringwald 
820b429b9b7Smatthias.ringwald #if 0
821b429b9b7Smatthias.ringwald     // open low-level device
822b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
823b429b9b7Smatthias.ringwald     if (err){
8247d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
825b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
826b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
827b429b9b7Smatthias.ringwald         }
828b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
829b429b9b7Smatthias.ringwald         return err;
830b429b9b7Smatthias.ringwald     }
831b429b9b7Smatthias.ringwald #endif
832b429b9b7Smatthias.ringwald 
833b429b9b7Smatthias.ringwald     return 0;
834b429b9b7Smatthias.ringwald }
835b429b9b7Smatthias.ringwald 
836b429b9b7Smatthias.ringwald 
8378d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
838d661ed19Smatthias.ringwald 
8397b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
840d661ed19Smatthias.ringwald 
8418d213e1aSmatthias.ringwald     int err = 0;
8428d213e1aSmatthias.ringwald     switch (hci_stack.state){
8438d213e1aSmatthias.ringwald 
8448d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
8458d213e1aSmatthias.ringwald             switch (power_mode){
8468d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8478d213e1aSmatthias.ringwald                     err = hci_power_control_on();
8488d213e1aSmatthias.ringwald                     if (err) return err;
8497301ad89Smatthias.ringwald                     // set up state machine
8507301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
8517301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8527301ad89Smatthias.ringwald                     hci_stack.substate = 0;
8538d213e1aSmatthias.ringwald                     break;
8548d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8558d213e1aSmatthias.ringwald                     // do nothing
8568d213e1aSmatthias.ringwald                     break;
8578d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
858b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
8598d213e1aSmatthias.ringwald                     break;
8608d213e1aSmatthias.ringwald             }
8618d213e1aSmatthias.ringwald             break;
8627301ad89Smatthias.ringwald 
8638d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8648d213e1aSmatthias.ringwald             switch (power_mode){
8658d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8668d213e1aSmatthias.ringwald                     // do nothing
8678d213e1aSmatthias.ringwald                     break;
8688d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8698d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
8708d213e1aSmatthias.ringwald                     hci_power_control_off();
8718d213e1aSmatthias.ringwald                     break;
8728d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
873b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
87472ea5239Smatthias.ringwald                     hci_power_control_sleep();
8758d213e1aSmatthias.ringwald                     break;
8768d213e1aSmatthias.ringwald             }
8778d213e1aSmatthias.ringwald             break;
8787301ad89Smatthias.ringwald 
8798d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
8808d213e1aSmatthias.ringwald             switch (power_mode){
8818d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8828d213e1aSmatthias.ringwald                     // do nothing
8838d213e1aSmatthias.ringwald                     break;
8848d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
885c7e0c5f6Smatthias.ringwald                     // see hci_run
886c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8878d213e1aSmatthias.ringwald                     break;
8888d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
889b546ac54Smatthias.ringwald                     // see hci_run
890b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
89189db417bSmatthias.ringwald                     hci_stack.substate = 0;
8928d213e1aSmatthias.ringwald                     break;
8938d213e1aSmatthias.ringwald             }
8948d213e1aSmatthias.ringwald             break;
8957301ad89Smatthias.ringwald 
8968d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
8978d213e1aSmatthias.ringwald             switch (power_mode){
8988d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8998d213e1aSmatthias.ringwald                     // set up state machine
9008d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
9018d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
9028d213e1aSmatthias.ringwald                     break;
9038d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9048d213e1aSmatthias.ringwald                     // do nothing
9058d213e1aSmatthias.ringwald                     break;
9068d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
907b546ac54Smatthias.ringwald                     // see hci_run
908b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
90989db417bSmatthias.ringwald                     hci_stack.substate = 0;
9108d213e1aSmatthias.ringwald                     break;
9118d213e1aSmatthias.ringwald             }
9128d213e1aSmatthias.ringwald             break;
9138d213e1aSmatthias.ringwald 
9148d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
9158d213e1aSmatthias.ringwald             switch (power_mode){
9168d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
91728171530Smatthias.ringwald 
91828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
91928171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
92028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9218747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
9228747d67fSmatthias.ringwald                         hci_stack.substate = 6;
92328171530Smatthias.ringwald                         break;
92428171530Smatthias.ringwald                     }
92528171530Smatthias.ringwald #endif
926b546ac54Smatthias.ringwald                     // set up state machine
927b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
928b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
929b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9308d213e1aSmatthias.ringwald                     break;
9318d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
932b546ac54Smatthias.ringwald                     // see hci_run
933b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9348d213e1aSmatthias.ringwald                     break;
9358d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
936b546ac54Smatthias.ringwald                     // do nothing
9378d213e1aSmatthias.ringwald                     break;
9388d213e1aSmatthias.ringwald             }
9398d213e1aSmatthias.ringwald             break;
9408d213e1aSmatthias.ringwald 
9418d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
9428d213e1aSmatthias.ringwald             switch (power_mode){
9438d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
94428171530Smatthias.ringwald 
94528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
94628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
94728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9488747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
9498747d67fSmatthias.ringwald                         hci_stack.substate = 6;
950758b46ceSmatthias.ringwald                         hci_update_scan_enable();
95128171530Smatthias.ringwald                         break;
95228171530Smatthias.ringwald                     }
95328171530Smatthias.ringwald #endif
9543144bce4Smatthias.ringwald                     err = hci_power_control_wake();
9553144bce4Smatthias.ringwald                     if (err) return err;
956b546ac54Smatthias.ringwald                     // set up state machine
957b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
958b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
959b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9608d213e1aSmatthias.ringwald                     break;
9618d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9624ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9638d213e1aSmatthias.ringwald                     break;
9648d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
965b546ac54Smatthias.ringwald                     // do nothing
9668d213e1aSmatthias.ringwald                     break;
9678d213e1aSmatthias.ringwald             }
9688d213e1aSmatthias.ringwald             break;
96911e23e5fSmatthias.ringwald     }
97068d92d03Smatthias.ringwald 
971038bc64cSmatthias.ringwald     // create internal event
972ee8bf225Smatthias.ringwald 	hci_emit_state();
973ee8bf225Smatthias.ringwald 
97468d92d03Smatthias.ringwald 	// trigger next/first action
97568d92d03Smatthias.ringwald 	hci_run();
97668d92d03Smatthias.ringwald 
977475c8125Smatthias.ringwald     return 0;
978475c8125Smatthias.ringwald }
979475c8125Smatthias.ringwald 
980758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
981758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
982758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
983758b46ceSmatthias.ringwald     hci_run();
984758b46ceSmatthias.ringwald }
985758b46ceSmatthias.ringwald 
986381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
987381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
988381fbed8Smatthias.ringwald 
989381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
990381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
991381fbed8Smatthias.ringwald         return;
992381fbed8Smatthias.ringwald     }
993381fbed8Smatthias.ringwald 
994381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
995758b46ceSmatthias.ringwald     hci_update_scan_enable();
996758b46ceSmatthias.ringwald }
997b031bebbSmatthias.ringwald 
998758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
999758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1000758b46ceSmatthias.ringwald 
1001758b46ceSmatthias.ringwald     // don't emit event
1002758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
1003758b46ceSmatthias.ringwald 
1004758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
1005758b46ceSmatthias.ringwald     hci_update_scan_enable();
1006381fbed8Smatthias.ringwald }
1007381fbed8Smatthias.ringwald 
100806b35ec0Smatthias.ringwald void hci_run(){
10098a485f27Smatthias.ringwald 
101032ab9390Smatthias.ringwald     hci_connection_t * connection;
101132ab9390Smatthias.ringwald     linked_item_t * it;
101232ab9390Smatthias.ringwald 
1013ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1014ce4c8fabSmatthias.ringwald 
1015b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1016b031bebbSmatthias.ringwald 
1017b031bebbSmatthias.ringwald     // decline incoming connections
1018ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1019ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1020ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1021ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1022ce4c8fabSmatthias.ringwald     }
1023ce4c8fabSmatthias.ringwald 
1024b031bebbSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1025b031bebbSmatthias.ringwald 
1026b031bebbSmatthias.ringwald     // send scan enable
1027eda18bf8Smatthias.ringwald     if (hci_stack.new_scan_enable_value != 0xff){
1028b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1029b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1030b031bebbSmatthias.ringwald     }
1031b031bebbSmatthias.ringwald 
103232ab9390Smatthias.ringwald     // send pending HCI commands
103332ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
103432ab9390Smatthias.ringwald 
1035b031bebbSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
103632ab9390Smatthias.ringwald 
1037b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
103832ab9390Smatthias.ringwald 
103932ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
10407b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
104132ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
104232ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1043c7e0c5f6Smatthias.ringwald         }
1044c7e0c5f6Smatthias.ringwald 
1045de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
104632ab9390Smatthias.ringwald 
104732ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
104832ab9390Smatthias.ringwald             link_key_t link_key;
10497b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
105032ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
105132ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
105232ab9390Smatthias.ringwald             } else {
105332ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
105432ab9390Smatthias.ringwald             }
105532ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
105632ab9390Smatthias.ringwald         }
105732ab9390Smatthias.ringwald     }
105832ab9390Smatthias.ringwald 
1059de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1060c7e0c5f6Smatthias.ringwald 
10613429f56bSmatthias.ringwald     switch (hci_stack.state){
10623429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10637b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
10643429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
10653429f56bSmatthias.ringwald                 // odd: waiting for command completion
106606b35ec0Smatthias.ringwald                 return;
10673429f56bSmatthias.ringwald             }
106890919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1069c7492964Smatthias.ringwald                 case 0: // RESET
107022909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
1071c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1072c7492964Smatthias.ringwald                         // skip baud change
1073c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1074c7492964Smatthias.ringwald                     }
10753429f56bSmatthias.ringwald                     break;
1076c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
10777dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
10787dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1079c7492964Smatthias.ringwald                     break;
1080c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1081c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1082c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1083c7492964Smatthias.ringwald                     // break missing here for fall through
1084c7492964Smatthias.ringwald 
1085c7492964Smatthias.ringwald                 case 3:
108690919203Smatthias.ringwald                     // custom initialization
1087db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
10887dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1089d62aca9fSmatthias.ringwald                         if (valid_cmd){
10907dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
10917dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1092c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
109390919203Smatthias.ringwald                             break;
109490919203Smatthias.ringwald                         }
1095d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
109690919203Smatthias.ringwald                     }
10972f6c30e1Smatthias.ringwald                     // otherwise continue
1098f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1099f432a6ddSmatthias.ringwald 					break;
1100c7492964Smatthias.ringwald 				case 4:
1101e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1102e2edc0c3Smatthias.ringwald 					break;
1103c7492964Smatthias.ringwald                 case 5:
11043429f56bSmatthias.ringwald                     // ca. 15 sec
11053429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
11063429f56bSmatthias.ringwald                     break;
1107c7492964Smatthias.ringwald 				case 6:
1108758b46ceSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
1109f432a6ddSmatthias.ringwald 					break;
1110c7492964Smatthias.ringwald                 case 7:
11118a485f27Smatthias.ringwald #ifndef EMBEDDED
11128a485f27Smatthias.ringwald                 {
11138a485f27Smatthias.ringwald                     char hostname[30];
11148a485f27Smatthias.ringwald                     gethostname(hostname, 30);
11158a485f27Smatthias.ringwald                     hostname[29] = '\0';
11168a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
11178a485f27Smatthias.ringwald                     break;
11188a485f27Smatthias.ringwald                 }
1119c7492964Smatthias.ringwald                 case 8:
11203c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
11213c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
11223c4d4b90Smatthias.ringwald                     break;
11233c4d4b90Smatthias.ringwald 
1124c7492964Smatthias.ringwald                 case 9:
11253c4d4b90Smatthias.ringwald #endif
11268a485f27Smatthias.ringwald #endif
11273429f56bSmatthias.ringwald                     // done.
11283429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1129b360b6adSmatthias.ringwald                     hci_emit_state();
11303429f56bSmatthias.ringwald                     break;
11313429f56bSmatthias.ringwald                 default:
11323429f56bSmatthias.ringwald                     break;
1133475c8125Smatthias.ringwald             }
11343429f56bSmatthias.ringwald             hci_stack.substate++;
11353429f56bSmatthias.ringwald             break;
1136c7e0c5f6Smatthias.ringwald 
1137c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1138c7e0c5f6Smatthias.ringwald 
11397b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1140c7e0c5f6Smatthias.ringwald             // close all open connections
1141c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1142c7e0c5f6Smatthias.ringwald             if (connection){
114332ab9390Smatthias.ringwald 
1144c7e0c5f6Smatthias.ringwald                 // send disconnect
114532ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
114632ab9390Smatthias.ringwald 
114779bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11486ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1149c7e0c5f6Smatthias.ringwald 
1150c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1151c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1152c7e0c5f6Smatthias.ringwald                 return;
1153c7e0c5f6Smatthias.ringwald             }
11547b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1155c7e0c5f6Smatthias.ringwald 
115672ea5239Smatthias.ringwald             // switch mode
1157c7e0c5f6Smatthias.ringwald             hci_power_control_off();
11589418f9c9Smatthias.ringwald 
11597b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
116072ea5239Smatthias.ringwald             hci_emit_state();
11617b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
116272ea5239Smatthias.ringwald             break;
1163c7e0c5f6Smatthias.ringwald 
116472ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
116589db417bSmatthias.ringwald             switch(hci_stack.substate) {
116689db417bSmatthias.ringwald                 case 0:
11677b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
116872ea5239Smatthias.ringwald                     // close all open connections
116972ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
117066da7044Smatthias.ringwald 
117128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
117266da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
117366da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
117466da7044Smatthias.ringwald                         connection = NULL;
117566da7044Smatthias.ringwald                     }
117666da7044Smatthias.ringwald #endif
117772ea5239Smatthias.ringwald                     if (connection){
117832ab9390Smatthias.ringwald 
117972ea5239Smatthias.ringwald                         // send disconnect
118032ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
118132ab9390Smatthias.ringwald 
118279bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11836ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
118472ea5239Smatthias.ringwald 
118572ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
118672ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
118772ea5239Smatthias.ringwald                         return;
118872ea5239Smatthias.ringwald                     }
118972ea5239Smatthias.ringwald 
119089db417bSmatthias.ringwald                     // disable page and inquiry scan
119132ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
119232ab9390Smatthias.ringwald 
1193758b46ceSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq cans\n");
1194758b46ceSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
119589db417bSmatthias.ringwald 
119689db417bSmatthias.ringwald                     // continue in next sub state
119789db417bSmatthias.ringwald                     hci_stack.substate++;
119889db417bSmatthias.ringwald                     break;
119989db417bSmatthias.ringwald                 case 1:
120089db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
120189db417bSmatthias.ringwald                     break;
120289db417bSmatthias.ringwald                 case 2:
12037b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
120428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
120528171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
120628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
120728171530Smatthias.ringwald                         // SLEEP MODE reached
120828171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
120928171530Smatthias.ringwald                         hci_emit_state();
121028171530Smatthias.ringwald                         break;
121128171530Smatthias.ringwald                     }
121228171530Smatthias.ringwald #endif
121372ea5239Smatthias.ringwald                     // switch mode
121489db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1215c7e0c5f6Smatthias.ringwald                     hci_emit_state();
121628171530Smatthias.ringwald                     break;
121728171530Smatthias.ringwald 
121889db417bSmatthias.ringwald                 default:
121989db417bSmatthias.ringwald                     break;
122089db417bSmatthias.ringwald             }
1221c7e0c5f6Smatthias.ringwald             break;
1222c7e0c5f6Smatthias.ringwald 
12233429f56bSmatthias.ringwald         default:
12243429f56bSmatthias.ringwald             break;
12251f504dbdSmatthias.ringwald     }
12263429f56bSmatthias.ringwald }
122716833f0aSmatthias.ringwald 
122831452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1229c8e4258aSmatthias.ringwald     bd_addr_t addr;
1230c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1231c8e4258aSmatthias.ringwald     // house-keeping
1232c8e4258aSmatthias.ringwald 
1233c8e4258aSmatthias.ringwald     // create_connection?
1234c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1235c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1236339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1237c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1238c8e4258aSmatthias.ringwald         if (conn) {
1239c8e4258aSmatthias.ringwald             // if connection exists
1240c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
124117f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
124217f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1243c8e4258aSmatthias.ringwald             }
124417f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1245c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1246c8e4258aSmatthias.ringwald 
124717f1ba2aSmatthias.ringwald         }
1248c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
124917f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
125017f1ba2aSmatthias.ringwald         if (!conn){
125117f1ba2aSmatthias.ringwald             // notify client that alloc failed
125217f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
125317f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
125417f1ba2aSmatthias.ringwald         }
1255c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1256c8e4258aSmatthias.ringwald     }
1257c8e4258aSmatthias.ringwald 
12587fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
12597fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
12607fde4af9Smatthias.ringwald     }
12617fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
12627fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
12637fde4af9Smatthias.ringwald     }
12647fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
12657fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
12667fde4af9Smatthias.ringwald     }
12677fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
12687fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
12697fde4af9Smatthias.ringwald     }
12707fde4af9Smatthias.ringwald 
12718ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
12728ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
12738ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
12748ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
12758ef73945Smatthias.ringwald         }
12768ef73945Smatthias.ringwald     }
1277c8e4258aSmatthias.ringwald 
127831452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1279622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
128031452debSmatthias.ringwald }
12818adf0ddaSmatthias.ringwald 
12821cd208adSmatthias.ringwald /**
12831cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
12841cd208adSmatthias.ringwald  */
1285fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
12861cd208adSmatthias.ringwald     va_list argptr;
12871cd208adSmatthias.ringwald     va_start(argptr, cmd);
12887dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
12891cd208adSmatthias.ringwald     va_end(argptr);
12907dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
129193b8dc03Smatthias.ringwald }
1292c8e4258aSmatthias.ringwald 
1293ee091cf1Smatthias.ringwald // Create various non-HCI events.
1294ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1295ee091cf1Smatthias.ringwald 
1296c8e4258aSmatthias.ringwald void hci_emit_state(){
1297425d1371Smatthias.ringwald     uint8_t event[3];
129880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1299425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1300c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1301425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1302425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1303c8e4258aSmatthias.ringwald }
1304c8e4258aSmatthias.ringwald 
130517f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1306425d1371Smatthias.ringwald     uint8_t event[13];
1307c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1308425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
130917f1ba2aSmatthias.ringwald     event[2] = status;
1310c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1311c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1312c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1313c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1314425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1315425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1316c8e4258aSmatthias.ringwald }
1317c8e4258aSmatthias.ringwald 
13183c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1319425d1371Smatthias.ringwald     uint8_t event[6];
13203c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1321e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
13223c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
13233c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
13243c4d4b90Smatthias.ringwald     event[5] = reason;
1325425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1326425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13273c4d4b90Smatthias.ringwald }
13283c4d4b90Smatthias.ringwald 
1329ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1330425d1371Smatthias.ringwald     uint8_t event[4];
133180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1332425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1333ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1334425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1335425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1336ee091cf1Smatthias.ringwald }
133743bfb1bdSmatthias.ringwald 
133843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1339425d1371Smatthias.ringwald     uint8_t event[3];
134080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1341425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
134243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1343425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1344425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
134543bfb1bdSmatthias.ringwald }
1346038bc64cSmatthias.ringwald 
1347038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1348425d1371Smatthias.ringwald     uint8_t event[2];
134980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1350425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1351425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1352425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1353038bc64cSmatthias.ringwald }
13541b0e3922Smatthias.ringwald 
135509ba8edeSmatthias.ringwald #ifndef EMBEDDED
13561b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1357425d1371Smatthias.ringwald     uint8_t event[6];
13581b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1359425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1360425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1361425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1362425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1363425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1364425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13651b0e3922Smatthias.ringwald }
136609ba8edeSmatthias.ringwald #endif
13671b0e3922Smatthias.ringwald 
13682ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1369425d1371Smatthias.ringwald     uint8_t event[3];
13702ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1371425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
13722ed6235cSmatthias.ringwald     event[2] = enabled;
1373425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1374e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13752ed6235cSmatthias.ringwald }
1376627c2f45Smatthias.ringwald 
1377627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1378425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1379627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1380425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1381f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
13822f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1383f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1384425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1385425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1386627c2f45Smatthias.ringwald }
1387381fbed8Smatthias.ringwald 
1388381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1389425d1371Smatthias.ringwald     uint8_t event[3];
1390381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1391425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1392381fbed8Smatthias.ringwald     event[2] = enabled;
1393425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1394425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1395381fbed8Smatthias.ringwald }
1396