xref: /btstack/src/hci.c (revision c0e866bf3773b328ac9664856ebbb1a4c6a4b649)
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             }
429381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
430381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
431381fbed8Smatthias.ringwald             }
43256cf178bSmatthias.ringwald             break;
43356cf178bSmatthias.ringwald 
4347ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4357ec5eeaaSmatthias.ringwald             // get num cmd packets
4367b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4377ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4387ec5eeaaSmatthias.ringwald             break;
4397ec5eeaaSmatthias.ringwald 
44056cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
44156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
44256cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
44356cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
44456cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
44556cf178bSmatthias.ringwald                 if (!conn){
4467d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
44756cf178bSmatthias.ringwald                     continue;
44856cf178bSmatthias.ringwald                 }
44956cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4507b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
45156cf178bSmatthias.ringwald             }
4526772a24cSmatthias.ringwald             break;
4536772a24cSmatthias.ringwald 
4541f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
45537eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
45637eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4572d00edd4Smatthias.ringwald             link_type = packet[11];
458339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
45937eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4601f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4611f7b95a1Smatthias.ringwald                 if (!conn) {
4621f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4631f7b95a1Smatthias.ringwald                 }
464ce4c8fabSmatthias.ringwald                 if (!conn) {
465ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
466ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
467ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
468ce4c8fabSmatthias.ringwald                     break;
469ce4c8fabSmatthias.ringwald                 }
47032ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
47132ab9390Smatthias.ringwald                 hci_run();
47237eaa4cfSmatthias.ringwald             } else {
473ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
474ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
475ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
47637eaa4cfSmatthias.ringwald             }
4771f7b95a1Smatthias.ringwald             break;
4781f7b95a1Smatthias.ringwald 
4796772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
480fe1ed1b8Smatthias.ringwald             // Connection management
481fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
482339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
4831f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
484fe1ed1b8Smatthias.ringwald             if (conn) {
485b448a0e7Smatthias.ringwald                 if (!packet[2]){
486c8e4258aSmatthias.ringwald                     conn->state = OPEN;
487fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
488ee091cf1Smatthias.ringwald 
489c785ef68Smatthias.ringwald                     // restart timer
490c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
491ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
492c785ef68Smatthias.ringwald 
493339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
49443bfb1bdSmatthias.ringwald 
49543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
496b448a0e7Smatthias.ringwald                 } else {
497b448a0e7Smatthias.ringwald                     // connection failed, remove entry
498b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
499a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
500c12e46e7Smatthias.ringwald 
501c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
502c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
503c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
504c12e46e7Smatthias.ringwald                     }
505fe1ed1b8Smatthias.ringwald                 }
506fe1ed1b8Smatthias.ringwald             }
5076772a24cSmatthias.ringwald             break;
508fe1ed1b8Smatthias.ringwald 
5097fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5107b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5117fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
51274ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
51332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
51464472d52Smatthias.ringwald             hci_run();
51529d53098Smatthias.ringwald             // request already answered
51629d53098Smatthias.ringwald             return;
5177fde4af9Smatthias.ringwald 
5187fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5197fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
52074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
52129d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
522287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
52329d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5247fde4af9Smatthias.ringwald             break;
5257fde4af9Smatthias.ringwald 
5267fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5277fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
5287fde4af9Smatthias.ringwald             break;
5297fde4af9Smatthias.ringwald 
530223aafc1Smatthias.ringwald #ifndef EMBEDDED
53174ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
53274ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
53374ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
53474ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5355a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5365a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5375a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5385a06394aSmatthias.ringwald                     packet[9+i] = 0;
5395a06394aSmatthias.ringwald                     break;
540cdc9101dSmatthias.ringwald                 }
5415a06394aSmatthias.ringwald             }
542d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
54374ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
54474ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
54574ec757aSmatthias.ringwald             break;
54674ec757aSmatthias.ringwald 
54774ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
54874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
54974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
55074ec757aSmatthias.ringwald             // first send inq result packet
55174ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
55274ec757aSmatthias.ringwald             // then send cached remote names
55374ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
55474ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
55574ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
55674ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
55774ec757aSmatthias.ringwald                 }
55874ec757aSmatthias.ringwald             }
55974ec757aSmatthias.ringwald             return;
560223aafc1Smatthias.ringwald #endif
56174ec757aSmatthias.ringwald 
5626772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
563fe1ed1b8Smatthias.ringwald             if (!packet[2]){
564fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
565fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
566fe1ed1b8Smatthias.ringwald                 if (conn) {
567c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
568fe1ed1b8Smatthias.ringwald                 }
569fe1ed1b8Smatthias.ringwald             }
5706772a24cSmatthias.ringwald             break;
5716772a24cSmatthias.ringwald 
572c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
573c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
574c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
575c68bdf90Smatthias.ringwald             }
576c68bdf90Smatthias.ringwald             break;
577c68bdf90Smatthias.ringwald 
5785909f7f2Smatthias.ringwald #ifdef HAVE_BLE
5795909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
5805909f7f2Smatthias.ringwald             switch (packet[2]) {
5815909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
5825909f7f2Smatthias.ringwald                     // Connection management
5835909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
5845909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
5855909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
5865909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
5875909f7f2Smatthias.ringwald                     if (packet[3]){
5885909f7f2Smatthias.ringwald                         if (conn){
5895909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
5905909f7f2Smatthias.ringwald                             linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
5915909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
5925909f7f2Smatthias.ringwald 
5935909f7f2Smatthias.ringwald                         }
5945909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
5955909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
5965909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
5975909f7f2Smatthias.ringwald                         }
5985909f7f2Smatthias.ringwald                         break;
5995909f7f2Smatthias.ringwald                     }
6005909f7f2Smatthias.ringwald                     if (!conn){
6015909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
6025909f7f2Smatthias.ringwald                     }
6035909f7f2Smatthias.ringwald                     if (!conn){
6045909f7f2Smatthias.ringwald                         // no memory
6055909f7f2Smatthias.ringwald                         break;
6065909f7f2Smatthias.ringwald                     }
6075909f7f2Smatthias.ringwald 
6085909f7f2Smatthias.ringwald                     conn->state = OPEN;
6095909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
6105909f7f2Smatthias.ringwald 
6115909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
6125909f7f2Smatthias.ringwald 
6135909f7f2Smatthias.ringwald                     // restart timer
6145909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
6155909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
6165909f7f2Smatthias.ringwald 
6175909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
6185909f7f2Smatthias.ringwald 
6195909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
6205909f7f2Smatthias.ringwald                     break;
6215909f7f2Smatthias.ringwald 
6225909f7f2Smatthias.ringwald                 default:
6235909f7f2Smatthias.ringwald                     break;
6245909f7f2Smatthias.ringwald             }
6255909f7f2Smatthias.ringwald             break;
6265909f7f2Smatthias.ringwald #endif
6275909f7f2Smatthias.ringwald 
6286772a24cSmatthias.ringwald         default:
6296772a24cSmatthias.ringwald             break;
630fe1ed1b8Smatthias.ringwald     }
631fe1ed1b8Smatthias.ringwald 
6323429f56bSmatthias.ringwald     // handle BT initialization
6333429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
6347301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
6357301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
6367301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
6377301ad89Smatthias.ringwald         // }
6387301ad89Smatthias.ringwald         // handle normal init sequence
6393429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
6403429f56bSmatthias.ringwald             // odd: waiting for event
6413429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
6423429f56bSmatthias.ringwald                 hci_stack.substate++;
6433429f56bSmatthias.ringwald             }
6443429f56bSmatthias.ringwald         }
64522909952Smatthias.ringwald     }
64622909952Smatthias.ringwald 
64789db417bSmatthias.ringwald     // help with BT sleep
64889db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
64989db417bSmatthias.ringwald         && hci_stack.substate == 1
65089db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
65189db417bSmatthias.ringwald         hci_stack.substate++;
65289db417bSmatthias.ringwald     }
65389db417bSmatthias.ringwald 
6542718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
65594ab26f8Smatthias.ringwald 
65694ab26f8Smatthias.ringwald 	// execute main loop
65794ab26f8Smatthias.ringwald 	hci_run();
65816833f0aSmatthias.ringwald }
65916833f0aSmatthias.ringwald 
66010e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
66110e830c9Smatthias.ringwald     switch (packet_type) {
66210e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
66310e830c9Smatthias.ringwald             event_handler(packet, size);
66410e830c9Smatthias.ringwald             break;
66510e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
66610e830c9Smatthias.ringwald             acl_handler(packet, size);
66710e830c9Smatthias.ringwald             break;
66810e830c9Smatthias.ringwald         default:
66910e830c9Smatthias.ringwald             break;
67010e830c9Smatthias.ringwald     }
67110e830c9Smatthias.ringwald }
67210e830c9Smatthias.ringwald 
673fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
6742718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
6752718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
67616833f0aSmatthias.ringwald }
67716833f0aSmatthias.ringwald 
678404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
679475c8125Smatthias.ringwald 
680475c8125Smatthias.ringwald     // reference to use transport layer implementation
68116833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
682475c8125Smatthias.ringwald 
68311e23e5fSmatthias.ringwald     // references to used control implementation
68411e23e5fSmatthias.ringwald     hci_stack.control = control;
68511e23e5fSmatthias.ringwald 
68611e23e5fSmatthias.ringwald     // reference to used config
68711e23e5fSmatthias.ringwald     hci_stack.config = config;
68811e23e5fSmatthias.ringwald 
689fe1ed1b8Smatthias.ringwald     // no connections yet
690fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
6910a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
692*c0e866bfSmatthias.ringwald     hci_stack.connectable = 0;
693758b46ceSmatthias.ringwald 
694b031bebbSmatthias.ringwald     // no pending cmds
695b031bebbSmatthias.ringwald     hci_stack.decline_reason = 0;
696b031bebbSmatthias.ringwald     hci_stack.new_scan_enable_value = 0xff;
697b031bebbSmatthias.ringwald 
69816833f0aSmatthias.ringwald     // higher level handler
6992718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
70016833f0aSmatthias.ringwald 
701404843c1Smatthias.ringwald     // store and open remote device db
702404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
703404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
704404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
705404843c1Smatthias.ringwald     }
70629d53098Smatthias.ringwald 
7078fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
7088fcba05dSmatthias.ringwald     hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
7098fcba05dSmatthias.ringwald 
71016833f0aSmatthias.ringwald     // register packet handlers with transport
71110e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
712f5454fc6Smatthias.ringwald 
713f5454fc6Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
714475c8125Smatthias.ringwald }
715475c8125Smatthias.ringwald 
716404843c1Smatthias.ringwald void hci_close(){
717404843c1Smatthias.ringwald     // close remote device db
718404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
719404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
720404843c1Smatthias.ringwald     }
721f5454fc6Smatthias.ringwald     while (hci_stack.connections) {
722f5454fc6Smatthias.ringwald         hci_shutdown_connection((hci_connection_t *) hci_stack.connections);
723f5454fc6Smatthias.ringwald }
724f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
725404843c1Smatthias.ringwald }
726404843c1Smatthias.ringwald 
7278d213e1aSmatthias.ringwald // State-Module-Driver overview
7288d213e1aSmatthias.ringwald // state                    module  low-level
7298d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
7308d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
7318d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
7328d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
733d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
734d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
735c7e0c5f6Smatthias.ringwald 
73640d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
7377301ad89Smatthias.ringwald 
738038bc64cSmatthias.ringwald     // power on
739f9a30166Smatthias.ringwald     int err = 0;
740f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
741f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
742f9a30166Smatthias.ringwald     }
743038bc64cSmatthias.ringwald     if (err){
7447d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
745038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
746038bc64cSmatthias.ringwald         return err;
747038bc64cSmatthias.ringwald     }
748038bc64cSmatthias.ringwald 
749038bc64cSmatthias.ringwald     // open low-level device
750038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
751038bc64cSmatthias.ringwald     if (err){
7527d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
753f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
754f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
755f9a30166Smatthias.ringwald         }
756038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
757038bc64cSmatthias.ringwald         return err;
758038bc64cSmatthias.ringwald     }
7598d213e1aSmatthias.ringwald     return 0;
7608d213e1aSmatthias.ringwald }
761038bc64cSmatthias.ringwald 
76240d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
7638d213e1aSmatthias.ringwald 
7647b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
7659418f9c9Smatthias.ringwald 
7668d213e1aSmatthias.ringwald     // close low-level device
7678d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
7688d213e1aSmatthias.ringwald 
7697b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
7709418f9c9Smatthias.ringwald 
7718d213e1aSmatthias.ringwald     // power off
7728d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
7738d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
7748d213e1aSmatthias.ringwald     }
7759418f9c9Smatthias.ringwald 
7767b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
7779418f9c9Smatthias.ringwald 
77872ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
77972ea5239Smatthias.ringwald }
78072ea5239Smatthias.ringwald 
78140d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
78272ea5239Smatthias.ringwald 
7837b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
7843144bce4Smatthias.ringwald 
785b429b9b7Smatthias.ringwald #if 0
786b429b9b7Smatthias.ringwald     // don't close serial port during sleep
787b429b9b7Smatthias.ringwald 
78872ea5239Smatthias.ringwald     // close low-level device
78972ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
790b429b9b7Smatthias.ringwald #endif
79172ea5239Smatthias.ringwald 
79272ea5239Smatthias.ringwald     // sleep mode
7933144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
79472ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
79572ea5239Smatthias.ringwald     }
796b429b9b7Smatthias.ringwald 
79772ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
7988d213e1aSmatthias.ringwald }
7998d213e1aSmatthias.ringwald 
80040d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
801b429b9b7Smatthias.ringwald 
8027b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
803b429b9b7Smatthias.ringwald 
804b429b9b7Smatthias.ringwald     // wake on
805b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
806b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
807b429b9b7Smatthias.ringwald     }
808b429b9b7Smatthias.ringwald 
809b429b9b7Smatthias.ringwald #if 0
810b429b9b7Smatthias.ringwald     // open low-level device
811b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
812b429b9b7Smatthias.ringwald     if (err){
8137d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
814b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
815b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
816b429b9b7Smatthias.ringwald         }
817b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
818b429b9b7Smatthias.ringwald         return err;
819b429b9b7Smatthias.ringwald     }
820b429b9b7Smatthias.ringwald #endif
821b429b9b7Smatthias.ringwald 
822b429b9b7Smatthias.ringwald     return 0;
823b429b9b7Smatthias.ringwald }
824b429b9b7Smatthias.ringwald 
825b429b9b7Smatthias.ringwald 
8268d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
827d661ed19Smatthias.ringwald 
8287b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
829d661ed19Smatthias.ringwald 
8308d213e1aSmatthias.ringwald     int err = 0;
8318d213e1aSmatthias.ringwald     switch (hci_stack.state){
8328d213e1aSmatthias.ringwald 
8338d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
8348d213e1aSmatthias.ringwald             switch (power_mode){
8358d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8368d213e1aSmatthias.ringwald                     err = hci_power_control_on();
8378d213e1aSmatthias.ringwald                     if (err) return err;
8387301ad89Smatthias.ringwald                     // set up state machine
8397301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
8407301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8417301ad89Smatthias.ringwald                     hci_stack.substate = 0;
8428d213e1aSmatthias.ringwald                     break;
8438d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8448d213e1aSmatthias.ringwald                     // do nothing
8458d213e1aSmatthias.ringwald                     break;
8468d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
847b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
8488d213e1aSmatthias.ringwald                     break;
8498d213e1aSmatthias.ringwald             }
8508d213e1aSmatthias.ringwald             break;
8517301ad89Smatthias.ringwald 
8528d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8538d213e1aSmatthias.ringwald             switch (power_mode){
8548d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8558d213e1aSmatthias.ringwald                     // do nothing
8568d213e1aSmatthias.ringwald                     break;
8578d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8588d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
8598d213e1aSmatthias.ringwald                     hci_power_control_off();
8608d213e1aSmatthias.ringwald                     break;
8618d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
862b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
86372ea5239Smatthias.ringwald                     hci_power_control_sleep();
8648d213e1aSmatthias.ringwald                     break;
8658d213e1aSmatthias.ringwald             }
8668d213e1aSmatthias.ringwald             break;
8677301ad89Smatthias.ringwald 
8688d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
8698d213e1aSmatthias.ringwald             switch (power_mode){
8708d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8718d213e1aSmatthias.ringwald                     // do nothing
8728d213e1aSmatthias.ringwald                     break;
8738d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
874c7e0c5f6Smatthias.ringwald                     // see hci_run
875c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8768d213e1aSmatthias.ringwald                     break;
8778d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
878b546ac54Smatthias.ringwald                     // see hci_run
879b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
88089db417bSmatthias.ringwald                     hci_stack.substate = 0;
8818d213e1aSmatthias.ringwald                     break;
8828d213e1aSmatthias.ringwald             }
8838d213e1aSmatthias.ringwald             break;
8847301ad89Smatthias.ringwald 
8858d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
8868d213e1aSmatthias.ringwald             switch (power_mode){
8878d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8888d213e1aSmatthias.ringwald                     // set up state machine
8898d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8908d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
8918d213e1aSmatthias.ringwald                     break;
8928d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8938d213e1aSmatthias.ringwald                     // do nothing
8948d213e1aSmatthias.ringwald                     break;
8958d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
896b546ac54Smatthias.ringwald                     // see hci_run
897b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
89889db417bSmatthias.ringwald                     hci_stack.substate = 0;
8998d213e1aSmatthias.ringwald                     break;
9008d213e1aSmatthias.ringwald             }
9018d213e1aSmatthias.ringwald             break;
9028d213e1aSmatthias.ringwald 
9038d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
9048d213e1aSmatthias.ringwald             switch (power_mode){
9058d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
90628171530Smatthias.ringwald 
90728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
90828171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
90928171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9108747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
9118747d67fSmatthias.ringwald                         hci_stack.substate = 6;
91228171530Smatthias.ringwald                         break;
91328171530Smatthias.ringwald                     }
91428171530Smatthias.ringwald #endif
915b546ac54Smatthias.ringwald                     // set up state machine
916b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
917b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
918b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9198d213e1aSmatthias.ringwald                     break;
9208d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
921b546ac54Smatthias.ringwald                     // see hci_run
922b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9238d213e1aSmatthias.ringwald                     break;
9248d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
925b546ac54Smatthias.ringwald                     // do nothing
9268d213e1aSmatthias.ringwald                     break;
9278d213e1aSmatthias.ringwald             }
9288d213e1aSmatthias.ringwald             break;
9298d213e1aSmatthias.ringwald 
9308d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
9318d213e1aSmatthias.ringwald             switch (power_mode){
9328d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
93328171530Smatthias.ringwald 
93428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
93528171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
93628171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
9378747d67fSmatthias.ringwald                         hci_stack.state = HCI_STATE_INITIALIZING;
9388747d67fSmatthias.ringwald                         hci_stack.substate = 6;
939758b46ceSmatthias.ringwald                         hci_update_scan_enable();
94028171530Smatthias.ringwald                         break;
94128171530Smatthias.ringwald                     }
94228171530Smatthias.ringwald #endif
9433144bce4Smatthias.ringwald                     err = hci_power_control_wake();
9443144bce4Smatthias.ringwald                     if (err) return err;
945b546ac54Smatthias.ringwald                     // set up state machine
946b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
947b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
948b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
9498d213e1aSmatthias.ringwald                     break;
9508d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
9514ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
9528d213e1aSmatthias.ringwald                     break;
9538d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
954b546ac54Smatthias.ringwald                     // do nothing
9558d213e1aSmatthias.ringwald                     break;
9568d213e1aSmatthias.ringwald             }
9578d213e1aSmatthias.ringwald             break;
95811e23e5fSmatthias.ringwald     }
95968d92d03Smatthias.ringwald 
960038bc64cSmatthias.ringwald     // create internal event
961ee8bf225Smatthias.ringwald 	hci_emit_state();
962ee8bf225Smatthias.ringwald 
96368d92d03Smatthias.ringwald 	// trigger next/first action
96468d92d03Smatthias.ringwald 	hci_run();
96568d92d03Smatthias.ringwald 
966475c8125Smatthias.ringwald     return 0;
967475c8125Smatthias.ringwald }
968475c8125Smatthias.ringwald 
969758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
970758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
971758b46ceSmatthias.ringwald     hci_stack.new_scan_enable_value  = hci_stack.connectable << 1 | hci_stack.discoverable;
972758b46ceSmatthias.ringwald     hci_run();
973758b46ceSmatthias.ringwald }
974758b46ceSmatthias.ringwald 
975381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
976381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
977381fbed8Smatthias.ringwald 
978381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
979381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
980381fbed8Smatthias.ringwald         return;
981381fbed8Smatthias.ringwald     }
982381fbed8Smatthias.ringwald 
983381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
984758b46ceSmatthias.ringwald     hci_update_scan_enable();
985758b46ceSmatthias.ringwald }
986b031bebbSmatthias.ringwald 
987758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
988758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
989758b46ceSmatthias.ringwald 
990758b46ceSmatthias.ringwald     // don't emit event
991758b46ceSmatthias.ringwald     if (hci_stack.connectable == enable) return;
992758b46ceSmatthias.ringwald 
993758b46ceSmatthias.ringwald     hci_stack.connectable = enable;
994758b46ceSmatthias.ringwald     hci_update_scan_enable();
995381fbed8Smatthias.ringwald }
996381fbed8Smatthias.ringwald 
99706b35ec0Smatthias.ringwald void hci_run(){
9988a485f27Smatthias.ringwald 
99932ab9390Smatthias.ringwald     hci_connection_t * connection;
100032ab9390Smatthias.ringwald     linked_item_t * it;
100132ab9390Smatthias.ringwald 
1002ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1003ce4c8fabSmatthias.ringwald 
1004b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1005b031bebbSmatthias.ringwald 
1006b031bebbSmatthias.ringwald     // decline incoming connections
1007ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
1008ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
1009ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
1010ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
1011ce4c8fabSmatthias.ringwald     }
1012ce4c8fabSmatthias.ringwald 
1013b031bebbSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1014b031bebbSmatthias.ringwald 
1015b031bebbSmatthias.ringwald     // send scan enable
1016eda18bf8Smatthias.ringwald     if (hci_stack.new_scan_enable_value != 0xff){
1017b031bebbSmatthias.ringwald         hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
1018b031bebbSmatthias.ringwald         hci_stack.new_scan_enable_value = 0xff;
1019b031bebbSmatthias.ringwald     }
1020b031bebbSmatthias.ringwald 
102132ab9390Smatthias.ringwald     // send pending HCI commands
102232ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
102332ab9390Smatthias.ringwald 
1024b031bebbSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
102532ab9390Smatthias.ringwald 
1026b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
102732ab9390Smatthias.ringwald 
102832ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
10297b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
103032ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
103132ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
1032c7e0c5f6Smatthias.ringwald         }
1033c7e0c5f6Smatthias.ringwald 
1034de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
103532ab9390Smatthias.ringwald 
103632ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
103732ab9390Smatthias.ringwald             link_key_t link_key;
10387b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
103932ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
104032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
104132ab9390Smatthias.ringwald             } else {
104232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
104332ab9390Smatthias.ringwald             }
104432ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
104532ab9390Smatthias.ringwald         }
104632ab9390Smatthias.ringwald     }
104732ab9390Smatthias.ringwald 
1048de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1049c7e0c5f6Smatthias.ringwald 
10503429f56bSmatthias.ringwald     switch (hci_stack.state){
10513429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
10527b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
10533429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
10543429f56bSmatthias.ringwald                 // odd: waiting for command completion
105506b35ec0Smatthias.ringwald                 return;
10563429f56bSmatthias.ringwald             }
105790919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
1058c7492964Smatthias.ringwald                 case 0: // RESET
105922909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
1060c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
1061c7492964Smatthias.ringwald                         // skip baud change
1062c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
1063c7492964Smatthias.ringwald                     }
10643429f56bSmatthias.ringwald                     break;
1065c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
10667dc17943Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
10677dc17943Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
1068c7492964Smatthias.ringwald                     break;
1069c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
1070c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
1071c7492964Smatthias.ringwald                     hci_stack.substate += 2;
1072c7492964Smatthias.ringwald                     // break missing here for fall through
1073c7492964Smatthias.ringwald 
1074c7492964Smatthias.ringwald                 case 3:
107590919203Smatthias.ringwald                     // custom initialization
1076db8946afSmatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_cmd){
10777dc17943Smatthias.ringwald                         int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
1078d62aca9fSmatthias.ringwald                         if (valid_cmd){
10797dc17943Smatthias.ringwald                             int size = 3 + hci_stack.hci_packet_buffer[2];
10807dc17943Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
1081c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
108290919203Smatthias.ringwald                             break;
108390919203Smatthias.ringwald                         }
1084d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
108590919203Smatthias.ringwald                     }
10862f6c30e1Smatthias.ringwald                     // otherwise continue
1087f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1088f432a6ddSmatthias.ringwald 					break;
1089c7492964Smatthias.ringwald 				case 4:
1090e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1091e2edc0c3Smatthias.ringwald 					break;
1092c7492964Smatthias.ringwald                 case 5:
10933429f56bSmatthias.ringwald                     // ca. 15 sec
10943429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
10953429f56bSmatthias.ringwald                     break;
1096c7492964Smatthias.ringwald 				case 6:
1097758b46ceSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan
1098f432a6ddSmatthias.ringwald 					break;
1099c7492964Smatthias.ringwald                 case 7:
11008a485f27Smatthias.ringwald #ifndef EMBEDDED
11018a485f27Smatthias.ringwald                 {
11028a485f27Smatthias.ringwald                     char hostname[30];
11038a485f27Smatthias.ringwald                     gethostname(hostname, 30);
11048a485f27Smatthias.ringwald                     hostname[29] = '\0';
11058a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
11068a485f27Smatthias.ringwald                     break;
11078a485f27Smatthias.ringwald                 }
1108c7492964Smatthias.ringwald                 case 8:
11093c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
11103c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
11113c4d4b90Smatthias.ringwald                     break;
11123c4d4b90Smatthias.ringwald 
1113c7492964Smatthias.ringwald                 case 9:
11143c4d4b90Smatthias.ringwald #endif
11158a485f27Smatthias.ringwald #endif
11163429f56bSmatthias.ringwald                     // done.
11173429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1118b360b6adSmatthias.ringwald                     hci_emit_state();
11193429f56bSmatthias.ringwald                     break;
11203429f56bSmatthias.ringwald                 default:
11213429f56bSmatthias.ringwald                     break;
1122475c8125Smatthias.ringwald             }
11233429f56bSmatthias.ringwald             hci_stack.substate++;
11243429f56bSmatthias.ringwald             break;
1125c7e0c5f6Smatthias.ringwald 
1126c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1127c7e0c5f6Smatthias.ringwald 
11287b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1129c7e0c5f6Smatthias.ringwald             // close all open connections
1130c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1131c7e0c5f6Smatthias.ringwald             if (connection){
113232ab9390Smatthias.ringwald 
1133c7e0c5f6Smatthias.ringwald                 // send disconnect
113432ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
113532ab9390Smatthias.ringwald 
113679bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11376ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1138c7e0c5f6Smatthias.ringwald 
1139c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1140c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1141c7e0c5f6Smatthias.ringwald                 return;
1142c7e0c5f6Smatthias.ringwald             }
11437b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1144c7e0c5f6Smatthias.ringwald 
114572ea5239Smatthias.ringwald             // switch mode
1146c7e0c5f6Smatthias.ringwald             hci_power_control_off();
11479418f9c9Smatthias.ringwald 
11487b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
114972ea5239Smatthias.ringwald             hci_emit_state();
11507b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
115172ea5239Smatthias.ringwald             break;
1152c7e0c5f6Smatthias.ringwald 
115372ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
115489db417bSmatthias.ringwald             switch(hci_stack.substate) {
115589db417bSmatthias.ringwald                 case 0:
11567b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
115772ea5239Smatthias.ringwald                     // close all open connections
115872ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
115966da7044Smatthias.ringwald 
116028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
116166da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
116266da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
116366da7044Smatthias.ringwald                         connection = NULL;
116466da7044Smatthias.ringwald                     }
116566da7044Smatthias.ringwald #endif
116672ea5239Smatthias.ringwald                     if (connection){
116732ab9390Smatthias.ringwald 
116872ea5239Smatthias.ringwald                         // send disconnect
116932ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
117032ab9390Smatthias.ringwald 
117179bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
11726ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
117372ea5239Smatthias.ringwald 
117472ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
117572ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
117672ea5239Smatthias.ringwald                         return;
117772ea5239Smatthias.ringwald                     }
117872ea5239Smatthias.ringwald 
117989db417bSmatthias.ringwald                     // disable page and inquiry scan
118032ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
118132ab9390Smatthias.ringwald 
1182758b46ceSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq cans\n");
1183758b46ceSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan
118489db417bSmatthias.ringwald 
118589db417bSmatthias.ringwald                     // continue in next sub state
118689db417bSmatthias.ringwald                     hci_stack.substate++;
118789db417bSmatthias.ringwald                     break;
118889db417bSmatthias.ringwald                 case 1:
118989db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
119089db417bSmatthias.ringwald                     break;
119189db417bSmatthias.ringwald                 case 2:
11927b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
119328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
119428171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
119528171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
119628171530Smatthias.ringwald                         // SLEEP MODE reached
119728171530Smatthias.ringwald                         hci_stack.state = HCI_STATE_SLEEPING;
119828171530Smatthias.ringwald                         hci_emit_state();
119928171530Smatthias.ringwald                         break;
120028171530Smatthias.ringwald                     }
120128171530Smatthias.ringwald #endif
120272ea5239Smatthias.ringwald                     // switch mode
120389db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1204c7e0c5f6Smatthias.ringwald                     hci_emit_state();
120528171530Smatthias.ringwald                     break;
120628171530Smatthias.ringwald 
120789db417bSmatthias.ringwald                 default:
120889db417bSmatthias.ringwald                     break;
120989db417bSmatthias.ringwald             }
1210c7e0c5f6Smatthias.ringwald             break;
1211c7e0c5f6Smatthias.ringwald 
12123429f56bSmatthias.ringwald         default:
12133429f56bSmatthias.ringwald             break;
12141f504dbdSmatthias.ringwald     }
12153429f56bSmatthias.ringwald }
121616833f0aSmatthias.ringwald 
121731452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1218c8e4258aSmatthias.ringwald     bd_addr_t addr;
1219c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1220c8e4258aSmatthias.ringwald     // house-keeping
1221c8e4258aSmatthias.ringwald 
1222c8e4258aSmatthias.ringwald     // create_connection?
1223c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1224c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1225339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1226c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1227c8e4258aSmatthias.ringwald         if (conn) {
1228c8e4258aSmatthias.ringwald             // if connection exists
1229c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
123017f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
123117f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1232c8e4258aSmatthias.ringwald             }
123317f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1234c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1235c8e4258aSmatthias.ringwald 
123617f1ba2aSmatthias.ringwald         }
1237c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
123817f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
123917f1ba2aSmatthias.ringwald         if (!conn){
124017f1ba2aSmatthias.ringwald             // notify client that alloc failed
124117f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
124217f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
124317f1ba2aSmatthias.ringwald         }
1244c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1245c8e4258aSmatthias.ringwald     }
1246c8e4258aSmatthias.ringwald 
12477fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
12487fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
12497fde4af9Smatthias.ringwald     }
12507fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
12517fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
12527fde4af9Smatthias.ringwald     }
12537fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
12547fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
12557fde4af9Smatthias.ringwald     }
12567fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
12577fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
12587fde4af9Smatthias.ringwald     }
12597fde4af9Smatthias.ringwald 
12608ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
12618ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
12628ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
12638ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
12648ef73945Smatthias.ringwald         }
12658ef73945Smatthias.ringwald     }
1266c8e4258aSmatthias.ringwald 
126731452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1268622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
126931452debSmatthias.ringwald }
12708adf0ddaSmatthias.ringwald 
12711cd208adSmatthias.ringwald /**
12721cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
12731cd208adSmatthias.ringwald  */
1274fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
12751cd208adSmatthias.ringwald     va_list argptr;
12761cd208adSmatthias.ringwald     va_start(argptr, cmd);
12777dc17943Smatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
12781cd208adSmatthias.ringwald     va_end(argptr);
12797dc17943Smatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
128093b8dc03Smatthias.ringwald }
1281c8e4258aSmatthias.ringwald 
1282ee091cf1Smatthias.ringwald // Create various non-HCI events.
1283ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1284ee091cf1Smatthias.ringwald 
1285c8e4258aSmatthias.ringwald void hci_emit_state(){
1286425d1371Smatthias.ringwald     uint8_t event[3];
128780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1288425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1289c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1290425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1291425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1292c8e4258aSmatthias.ringwald }
1293c8e4258aSmatthias.ringwald 
129417f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1295425d1371Smatthias.ringwald     uint8_t event[13];
1296c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1297425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
129817f1ba2aSmatthias.ringwald     event[2] = status;
1299c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1300c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1301c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1302c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1303425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1304425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1305c8e4258aSmatthias.ringwald }
1306c8e4258aSmatthias.ringwald 
13073c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1308425d1371Smatthias.ringwald     uint8_t event[6];
13093c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1310e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
13113c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
13123c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
13133c4d4b90Smatthias.ringwald     event[5] = reason;
1314425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1315425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13163c4d4b90Smatthias.ringwald }
13173c4d4b90Smatthias.ringwald 
1318ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1319425d1371Smatthias.ringwald     uint8_t event[4];
132080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1321425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1322ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1323425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1324425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1325ee091cf1Smatthias.ringwald }
132643bfb1bdSmatthias.ringwald 
132743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1328425d1371Smatthias.ringwald     uint8_t event[3];
132980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1330425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
133143bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1332425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1333425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
133443bfb1bdSmatthias.ringwald }
1335038bc64cSmatthias.ringwald 
1336038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1337425d1371Smatthias.ringwald     uint8_t event[2];
133880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1339425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1340425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1341425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1342038bc64cSmatthias.ringwald }
13431b0e3922Smatthias.ringwald 
134409ba8edeSmatthias.ringwald #ifndef EMBEDDED
13451b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1346425d1371Smatthias.ringwald     uint8_t event[6];
13471b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1348425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1349425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1350425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1351425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1352425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1353425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13541b0e3922Smatthias.ringwald }
135509ba8edeSmatthias.ringwald #endif
13561b0e3922Smatthias.ringwald 
13572ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1358425d1371Smatthias.ringwald     uint8_t event[3];
13592ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1360425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
13612ed6235cSmatthias.ringwald     event[2] = enabled;
1362425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1363e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
13642ed6235cSmatthias.ringwald }
1365627c2f45Smatthias.ringwald 
1366627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1367425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1368627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1369425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1370f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
13712f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1372f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1373425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1374425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1375627c2f45Smatthias.ringwald }
1376381fbed8Smatthias.ringwald 
1377381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1378425d1371Smatthias.ringwald     uint8_t event[3];
1379381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1380425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1381381fbed8Smatthias.ringwald     event[2] = enabled;
1382425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1383425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1384381fbed8Smatthias.ringwald }
1385