xref: /btstack/src/hci.c (revision c785ef68cf2f75708d1b63e77b925cf02aef517e)
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 
397f2435e6Smatthias.ringwald #include "hci.h"
407f2435e6Smatthias.ringwald 
4193b8dc03Smatthias.ringwald #include <stdarg.h>
4293b8dc03Smatthias.ringwald #include <string.h>
4356fe0872Smatthias.ringwald #include <stdio.h>
447f2435e6Smatthias.ringwald 
45549e6ebeSmatthias.ringwald #ifndef EMBEDDED
46549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
47549e6ebeSmatthias.ringwald #endif
48549e6ebeSmatthias.ringwald 
49a3b02b71Smatthias.ringwald #include "btstack_memory.h"
507f2435e6Smatthias.ringwald #include "debug.h"
51d8905019Smatthias.ringwald #include "hci_dump.h"
5293b8dc03Smatthias.ringwald 
53ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
54ae1fd9f3Smatthias.ringwald #include <btstack/version.h>
551b0e3922Smatthias.ringwald 
56a3b02b71Smatthias.ringwald // tmpe
571e6aba47Smatthias.ringwald #include "l2cap.h"
581e6aba47Smatthias.ringwald 
59169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
60ee091cf1Smatthias.ringwald 
6106b35ec0Smatthias.ringwald // the STACK is here
6216833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
6316833f0aSmatthias.ringwald 
6497addcc5Smatthias.ringwald /**
65ee091cf1Smatthias.ringwald  * get connection for a given handle
66ee091cf1Smatthias.ringwald  *
67ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
68ee091cf1Smatthias.ringwald  */
69645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
70ee091cf1Smatthias.ringwald     linked_item_t *it;
71ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
72ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
73ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
74ee091cf1Smatthias.ringwald         }
75ee091cf1Smatthias.ringwald     }
76ee091cf1Smatthias.ringwald     return NULL;
77ee091cf1Smatthias.ringwald }
78ee091cf1Smatthias.ringwald 
792b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
80ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
81*c785ef68Smatthias.ringwald #ifdef HAVE_TIME
82ee091cf1Smatthias.ringwald     struct timeval tv;
83ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
84c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
85ee091cf1Smatthias.ringwald         // connections might be timed out
86ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
87ee091cf1Smatthias.ringwald     }
882b12a0b9Smatthias.ringwald #endif
89*c785ef68Smatthias.ringwald #ifdef EMBEDDED
90*c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
91*c785ef68Smatthias.ringwald         // connections might be timed out
92*c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
93*c785ef68Smatthias.ringwald     }
94*c785ef68Smatthias.ringwald #endif
95*c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
96*c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
97*c785ef68Smatthias.ringwald }
98ee091cf1Smatthias.ringwald 
99ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
100c7492964Smatthias.ringwald #ifdef HAVE_TIME
101ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
102c7492964Smatthias.ringwald #endif
103*c785ef68Smatthias.ringwald #ifdef EMBEDDED
104*c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
105*c785ef68Smatthias.ringwald #endif
106ee091cf1Smatthias.ringwald }
107ee091cf1Smatthias.ringwald 
108ee091cf1Smatthias.ringwald /**
109c8e4258aSmatthias.ringwald  * create connection for given address
110c8e4258aSmatthias.ringwald  *
11117f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
112c8e4258aSmatthias.ringwald  */
113c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
114a3b02b71Smatthias.ringwald     hci_connection_t * conn = btstack_memory_hci_connection_get();
115c8e4258aSmatthias.ringwald     if (!conn) return NULL;
116c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
117c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1187d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
119ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
120ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
121ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
122d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1237856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
12456cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
125c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
126c8e4258aSmatthias.ringwald     return conn;
127c8e4258aSmatthias.ringwald }
128c8e4258aSmatthias.ringwald 
129c8e4258aSmatthias.ringwald /**
13006b35ec0Smatthias.ringwald  * get connection for given address
13197addcc5Smatthias.ringwald  *
13297addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
13397addcc5Smatthias.ringwald  */
134fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
13506b35ec0Smatthias.ringwald     linked_item_t *it;
13606b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
13706b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
13806b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
13906b35ec0Smatthias.ringwald         }
14006b35ec0Smatthias.ringwald     }
14106b35ec0Smatthias.ringwald     return NULL;
14206b35ec0Smatthias.ringwald }
14306b35ec0Smatthias.ringwald 
14443bfb1bdSmatthias.ringwald /**
14580ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1467fde4af9Smatthias.ringwald  */
1477fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1487fde4af9Smatthias.ringwald     bd_addr_t addr;
1497fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1507fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1517fde4af9Smatthias.ringwald     if (conn) {
1527fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
15380ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1547fde4af9Smatthias.ringwald     }
1557fde4af9Smatthias.ringwald }
1567fde4af9Smatthias.ringwald 
15780ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
15880ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
15980ca58a0Smatthias.ringwald     if (!conn) return 0;
16080ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
16180ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
16280ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
16380ca58a0Smatthias.ringwald     return 1;
16480ca58a0Smatthias.ringwald }
16580ca58a0Smatthias.ringwald 
166c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
167c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
168c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
169c12e46e7Smatthias.ringwald     }
170c12e46e7Smatthias.ringwald }
171c12e46e7Smatthias.ringwald 
1727fde4af9Smatthias.ringwald 
1737fde4af9Smatthias.ringwald /**
17443bfb1bdSmatthias.ringwald  * count connections
17543bfb1bdSmatthias.ringwald  */
17640d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
17756c253c9Smatthias.ringwald     int count = 0;
17843bfb1bdSmatthias.ringwald     linked_item_t *it;
17943bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
18043bfb1bdSmatthias.ringwald     return count;
18143bfb1bdSmatthias.ringwald }
182c8e4258aSmatthias.ringwald 
18397addcc5Smatthias.ringwald /**
184ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
18516833f0aSmatthias.ringwald  */
1862718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
18716833f0aSmatthias.ringwald }
18816833f0aSmatthias.ringwald 
189998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
190998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
191998906cdSmatthias.ringwald     if (!connection) {
1927d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
193998906cdSmatthias.ringwald         return 0;
194998906cdSmatthias.ringwald     }
195998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
196998906cdSmatthias.ringwald }
197998906cdSmatthias.ringwald 
198998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
199998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
200998906cdSmatthias.ringwald     linked_item_t *it;
201998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
202998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
203998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2047d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
205998906cdSmatthias.ringwald             return 0;
206998906cdSmatthias.ringwald         }
207998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
208998906cdSmatthias.ringwald     }
209998906cdSmatthias.ringwald     return free_slots;
210998906cdSmatthias.ringwald }
211998906cdSmatthias.ringwald 
2125250fb9eSmatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
2135250fb9eSmatthias.ringwald     return hci_stack.acl_data_packet_length;
2145250fb9eSmatthias.ringwald }
2155250fb9eSmatthias.ringwald 
216c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2172b12a0b9Smatthias.ringwald 
2182b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2192b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2202b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2212b12a0b9Smatthias.ringwald             return 0;
2222b12a0b9Smatthias.ringwald         }
2232b12a0b9Smatthias.ringwald     }
2242b12a0b9Smatthias.ringwald 
2252b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
226c24735b1Smatthias.ringwald     switch (packet_type) {
227c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
228c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
229c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
230de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
231c24735b1Smatthias.ringwald         default:
232c24735b1Smatthias.ringwald             return 0;
233c24735b1Smatthias.ringwald     }
234c24735b1Smatthias.ringwald }
235c24735b1Smatthias.ringwald 
236ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2377856c818Smatthias.ringwald 
2386218e6f1Smatthias.ringwald     // check for free places on BT module
2395932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2406218e6f1Smatthias.ringwald 
2417856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2427856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
24356cf178bSmatthias.ringwald     if (!connection) return 0;
24456cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
24556cf178bSmatthias.ringwald 
24656cf178bSmatthias.ringwald     // count packet
24756cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2487b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2497856c818Smatthias.ringwald 
2506218e6f1Smatthias.ringwald     // send packet - ignore errors
251622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2526218e6f1Smatthias.ringwald 
2536218e6f1Smatthias.ringwald     return 0;
254ee091cf1Smatthias.ringwald }
255ee091cf1Smatthias.ringwald 
25616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2577856c818Smatthias.ringwald 
2587856c818Smatthias.ringwald     // get info
2597856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2607856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2617856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2627856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2637856c818Smatthias.ringwald 
2647856c818Smatthias.ringwald     // ignore non-registered handle
2657856c818Smatthias.ringwald     if (!conn){
2667d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2677856c818Smatthias.ringwald         return;
2687856c818Smatthias.ringwald     }
2697856c818Smatthias.ringwald 
2707856c818Smatthias.ringwald     // update idle timestamp
2717856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2727856c818Smatthias.ringwald 
2737856c818Smatthias.ringwald     // handle different packet types
2747856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2757856c818Smatthias.ringwald 
2767856c818Smatthias.ringwald         case 0x01: // continuation fragment
2777856c818Smatthias.ringwald 
2787856c818Smatthias.ringwald             // sanity check
2797856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2807d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2817856c818Smatthias.ringwald                 return;
2827856c818Smatthias.ringwald             }
2837856c818Smatthias.ringwald 
2847856c818Smatthias.ringwald             // append fragment payload (header already stored)
2857856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2867856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2877856c818Smatthias.ringwald 
2887d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
289decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
2907856c818Smatthias.ringwald 
2917856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2927856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2937856c818Smatthias.ringwald 
2942718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2957856c818Smatthias.ringwald                 // reset recombination buffer
2967856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2977856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2987856c818Smatthias.ringwald             }
2997856c818Smatthias.ringwald             break;
3007856c818Smatthias.ringwald 
3017856c818Smatthias.ringwald         case 0x02: { // first fragment
3027856c818Smatthias.ringwald 
3037856c818Smatthias.ringwald             // sanity check
3047856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3057d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3067856c818Smatthias.ringwald                 return;
3077856c818Smatthias.ringwald             }
3087856c818Smatthias.ringwald 
3097856c818Smatthias.ringwald             // peek into L2CAP packet!
3107856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3117856c818Smatthias.ringwald 
3127d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
313decc01a8Smatthias.ringwald 
3147856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3157856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3167856c818Smatthias.ringwald 
3177856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3182718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3197856c818Smatthias.ringwald 
3207856c818Smatthias.ringwald             } else {
3217856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3227856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3237856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3247856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
325decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3267856c818Smatthias.ringwald             }
3277856c818Smatthias.ringwald             break;
3287856c818Smatthias.ringwald 
3297856c818Smatthias.ringwald         }
3307856c818Smatthias.ringwald         default:
3317d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3327856c818Smatthias.ringwald             return;
3337856c818Smatthias.ringwald     }
33494ab26f8Smatthias.ringwald 
33594ab26f8Smatthias.ringwald     // execute main loop
33694ab26f8Smatthias.ringwald     hci_run();
33716833f0aSmatthias.ringwald }
33822909952Smatthias.ringwald 
33967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
3407b5fbe1fSmatthias.ringwald     log_info("Connection closed: handle %u, ", conn->con_handle);
341c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
3427b5fbe1fSmatthias.ringwald     log_info("\n");
3433c4d4b90Smatthias.ringwald 
3443c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3453c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3463c4d4b90Smatthias.ringwald 
347c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
348*c785ef68Smatthias.ringwald 
349c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
350a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3513c4d4b90Smatthias.ringwald 
3523c4d4b90Smatthias.ringwald     // now it's gone
353c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
354c7e0c5f6Smatthias.ringwald }
355c7e0c5f6Smatthias.ringwald 
35674ec757aSmatthias.ringwald // avoid huge local variables
35774ec757aSmatthias.ringwald static device_name_t device_name;
35816833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3591281a47eSmatthias.ringwald     bd_addr_t addr;
3602d00edd4Smatthias.ringwald     uint8_t link_type;
361fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3621f7b95a1Smatthias.ringwald     hci_connection_t * conn;
36356cf178bSmatthias.ringwald     int i;
36422909952Smatthias.ringwald 
3656772a24cSmatthias.ringwald     switch (packet[0]) {
36622909952Smatthias.ringwald 
3676772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3687ec5eeaaSmatthias.ringwald             // get num cmd packets
3697b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3707ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3717ec5eeaaSmatthias.ringwald 
372e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
373e2edc0c3Smatthias.ringwald                 // from offset 5
374e2edc0c3Smatthias.ringwald                 // status
3751d279b20Smatthias.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"
376e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
37756cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
378e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
37956cf178bSmatthias.ringwald                 // ignore: total num SCO packets
38056cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
3817b5fbe1fSmatthias.ringwald                     log_info("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
382e2edc0c3Smatthias.ringwald                 }
38356cf178bSmatthias.ringwald             }
384381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
385381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
386381fbed8Smatthias.ringwald             }
38756cf178bSmatthias.ringwald             break;
38856cf178bSmatthias.ringwald 
3897ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
3907ec5eeaaSmatthias.ringwald             // get num cmd packets
3917b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
3927ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
3937ec5eeaaSmatthias.ringwald             break;
3947ec5eeaaSmatthias.ringwald 
39556cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
39656cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
39756cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
39856cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
39956cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
40056cf178bSmatthias.ringwald                 if (!conn){
4017d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
40256cf178bSmatthias.ringwald                     continue;
40356cf178bSmatthias.ringwald                 }
40456cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4057b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
40656cf178bSmatthias.ringwald             }
4076772a24cSmatthias.ringwald             break;
4086772a24cSmatthias.ringwald 
4091f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
41037eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
41137eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4122d00edd4Smatthias.ringwald             link_type = packet[11];
4137b5fbe1fSmatthias.ringwald             log_info("Connection_incoming: "); print_bd_addr(addr); log_info(", type %u\n", link_type);
41437eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4151f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4161f7b95a1Smatthias.ringwald                 if (!conn) {
4171f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4181f7b95a1Smatthias.ringwald                 }
419ce4c8fabSmatthias.ringwald                 if (!conn) {
420ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
421ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
422ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
423ce4c8fabSmatthias.ringwald                     break;
424ce4c8fabSmatthias.ringwald                 }
42532ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
42632ab9390Smatthias.ringwald                 hci_run();
42737eaa4cfSmatthias.ringwald             } else {
428ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
429ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
430ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
43137eaa4cfSmatthias.ringwald             }
4321f7b95a1Smatthias.ringwald             break;
4331f7b95a1Smatthias.ringwald 
4346772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
435fe1ed1b8Smatthias.ringwald             // Connection management
436fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
4377b5fbe1fSmatthias.ringwald             log_info("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_info("\n");
4381f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
439fe1ed1b8Smatthias.ringwald             if (conn) {
440b448a0e7Smatthias.ringwald                 if (!packet[2]){
441c8e4258aSmatthias.ringwald                     conn->state = OPEN;
442fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
443ee091cf1Smatthias.ringwald 
444*c785ef68Smatthias.ringwald                     // restart timer
445c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
446ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
447*c785ef68Smatthias.ringwald 
4487b5fbe1fSmatthias.ringwald                     log_info("New connection: handle %u, ", conn->con_handle);
449fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
4507b5fbe1fSmatthias.ringwald                     log_info("\n");
45143bfb1bdSmatthias.ringwald 
45243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
453b448a0e7Smatthias.ringwald                 } else {
454b448a0e7Smatthias.ringwald                     // connection failed, remove entry
455b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
456a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
457c12e46e7Smatthias.ringwald 
458c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
459c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
460c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
461c12e46e7Smatthias.ringwald                     }
462fe1ed1b8Smatthias.ringwald                 }
463fe1ed1b8Smatthias.ringwald             }
4646772a24cSmatthias.ringwald             break;
465fe1ed1b8Smatthias.ringwald 
4667fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
4677b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
4687fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
46974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
47032ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
47164472d52Smatthias.ringwald             hci_run();
47229d53098Smatthias.ringwald             // request already answered
47329d53098Smatthias.ringwald             return;
4747fde4af9Smatthias.ringwald 
4757fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4767fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
47774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
47829d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
479287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
48029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4817fde4af9Smatthias.ringwald             break;
4827fde4af9Smatthias.ringwald 
4837fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4847fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4857fde4af9Smatthias.ringwald             break;
4867fde4af9Smatthias.ringwald 
48774ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
48874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
48974ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
49074ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
4915a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
4925a06394aSmatthias.ringwald             for (i=0; i<248;i++){
4935a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
4945a06394aSmatthias.ringwald                     packet[9+i] = 0;
4955a06394aSmatthias.ringwald                     break;
496cdc9101dSmatthias.ringwald                 }
4975a06394aSmatthias.ringwald             }
49874ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
49974ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
50074ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
50174ec757aSmatthias.ringwald             break;
50274ec757aSmatthias.ringwald 
50374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
50474ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
50574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
50674ec757aSmatthias.ringwald             // first send inq result packet
50774ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
50874ec757aSmatthias.ringwald             // then send cached remote names
50974ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
51074ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
51174ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
51274ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
51374ec757aSmatthias.ringwald                 }
51474ec757aSmatthias.ringwald             }
51574ec757aSmatthias.ringwald             return;
51674ec757aSmatthias.ringwald 
5176772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
518fe1ed1b8Smatthias.ringwald             if (!packet[2]){
519fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
520fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
521fe1ed1b8Smatthias.ringwald                 if (conn) {
522c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
523fe1ed1b8Smatthias.ringwald                 }
524fe1ed1b8Smatthias.ringwald             }
5256772a24cSmatthias.ringwald             break;
5266772a24cSmatthias.ringwald 
527c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
528c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
529c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
530c68bdf90Smatthias.ringwald             }
531c68bdf90Smatthias.ringwald             break;
532c68bdf90Smatthias.ringwald 
5336772a24cSmatthias.ringwald         default:
5346772a24cSmatthias.ringwald             break;
535fe1ed1b8Smatthias.ringwald     }
536fe1ed1b8Smatthias.ringwald 
5373429f56bSmatthias.ringwald     // handle BT initialization
5383429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5397301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5407301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5417301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5427301ad89Smatthias.ringwald         // }
5437301ad89Smatthias.ringwald         // handle normal init sequence
5443429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5453429f56bSmatthias.ringwald             // odd: waiting for event
5463429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5473429f56bSmatthias.ringwald                 hci_stack.substate++;
5483429f56bSmatthias.ringwald             }
5493429f56bSmatthias.ringwald         }
55022909952Smatthias.ringwald     }
55122909952Smatthias.ringwald 
55289db417bSmatthias.ringwald     // help with BT sleep
55389db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
55489db417bSmatthias.ringwald         && hci_stack.substate == 1
55589db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
55689db417bSmatthias.ringwald         hci_stack.substate++;
55789db417bSmatthias.ringwald     }
55889db417bSmatthias.ringwald 
5592718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
56094ab26f8Smatthias.ringwald 
56194ab26f8Smatthias.ringwald 	// execute main loop
56294ab26f8Smatthias.ringwald 	hci_run();
56316833f0aSmatthias.ringwald }
56416833f0aSmatthias.ringwald 
56510e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
56610e830c9Smatthias.ringwald     switch (packet_type) {
56710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
56810e830c9Smatthias.ringwald             event_handler(packet, size);
56910e830c9Smatthias.ringwald             break;
57010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
57110e830c9Smatthias.ringwald             acl_handler(packet, size);
57210e830c9Smatthias.ringwald             break;
57310e830c9Smatthias.ringwald         default:
57410e830c9Smatthias.ringwald             break;
57510e830c9Smatthias.ringwald     }
57610e830c9Smatthias.ringwald }
57710e830c9Smatthias.ringwald 
578fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5792718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5802718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
58116833f0aSmatthias.ringwald }
58216833f0aSmatthias.ringwald 
583404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
584475c8125Smatthias.ringwald 
585475c8125Smatthias.ringwald     // reference to use transport layer implementation
58616833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
587475c8125Smatthias.ringwald 
58811e23e5fSmatthias.ringwald     // references to used control implementation
58911e23e5fSmatthias.ringwald     hci_stack.control = control;
59011e23e5fSmatthias.ringwald 
59111e23e5fSmatthias.ringwald     // reference to used config
59211e23e5fSmatthias.ringwald     hci_stack.config = config;
59311e23e5fSmatthias.ringwald 
594fe1ed1b8Smatthias.ringwald     // no connections yet
595fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
5960a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
597fe1ed1b8Smatthias.ringwald 
59816833f0aSmatthias.ringwald     // higher level handler
5992718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
60016833f0aSmatthias.ringwald 
601404843c1Smatthias.ringwald     // store and open remote device db
602404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
603404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
604404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
605404843c1Smatthias.ringwald     }
60629d53098Smatthias.ringwald 
60716833f0aSmatthias.ringwald     // register packet handlers with transport
60810e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
609475c8125Smatthias.ringwald }
610475c8125Smatthias.ringwald 
611404843c1Smatthias.ringwald void hci_close(){
612404843c1Smatthias.ringwald     // close remote device db
613404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
614404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
615404843c1Smatthias.ringwald     }
616404843c1Smatthias.ringwald }
617404843c1Smatthias.ringwald 
6188d213e1aSmatthias.ringwald // State-Module-Driver overview
6198d213e1aSmatthias.ringwald // state                    module  low-level
6208d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6218d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6228d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6238d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
624d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
625d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
626c7e0c5f6Smatthias.ringwald 
62740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6287301ad89Smatthias.ringwald 
629038bc64cSmatthias.ringwald     // power on
630f9a30166Smatthias.ringwald     int err = 0;
631f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
632f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
633f9a30166Smatthias.ringwald     }
634038bc64cSmatthias.ringwald     if (err){
6357d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
636038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
637038bc64cSmatthias.ringwald         return err;
638038bc64cSmatthias.ringwald     }
639038bc64cSmatthias.ringwald 
640038bc64cSmatthias.ringwald     // open low-level device
641038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
642038bc64cSmatthias.ringwald     if (err){
6437d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
644f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
645f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
646f9a30166Smatthias.ringwald         }
647038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
648038bc64cSmatthias.ringwald         return err;
649038bc64cSmatthias.ringwald     }
6508d213e1aSmatthias.ringwald     return 0;
6518d213e1aSmatthias.ringwald }
652038bc64cSmatthias.ringwald 
65340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6548d213e1aSmatthias.ringwald 
6557b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
6569418f9c9Smatthias.ringwald 
6578d213e1aSmatthias.ringwald     // close low-level device
6588d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6598d213e1aSmatthias.ringwald 
6607b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
6619418f9c9Smatthias.ringwald 
6628d213e1aSmatthias.ringwald     // power off
6638d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6648d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6658d213e1aSmatthias.ringwald     }
6669418f9c9Smatthias.ringwald 
6677b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
6689418f9c9Smatthias.ringwald 
66972ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
67072ea5239Smatthias.ringwald }
67172ea5239Smatthias.ringwald 
67240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
67372ea5239Smatthias.ringwald 
6747b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
6753144bce4Smatthias.ringwald 
676b429b9b7Smatthias.ringwald #if 0
677b429b9b7Smatthias.ringwald     // don't close serial port during sleep
678b429b9b7Smatthias.ringwald 
67972ea5239Smatthias.ringwald     // close low-level device
68072ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
681b429b9b7Smatthias.ringwald #endif
68272ea5239Smatthias.ringwald 
68372ea5239Smatthias.ringwald     // sleep mode
6843144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
68572ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
68672ea5239Smatthias.ringwald     }
687b429b9b7Smatthias.ringwald 
68872ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6898d213e1aSmatthias.ringwald }
6908d213e1aSmatthias.ringwald 
69140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
692b429b9b7Smatthias.ringwald 
6937b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
694b429b9b7Smatthias.ringwald 
695b429b9b7Smatthias.ringwald     // wake on
696b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
697b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
698b429b9b7Smatthias.ringwald     }
699b429b9b7Smatthias.ringwald 
700b429b9b7Smatthias.ringwald #if 0
701b429b9b7Smatthias.ringwald     // open low-level device
702b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
703b429b9b7Smatthias.ringwald     if (err){
7047d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
705b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
706b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
707b429b9b7Smatthias.ringwald         }
708b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
709b429b9b7Smatthias.ringwald         return err;
710b429b9b7Smatthias.ringwald     }
711b429b9b7Smatthias.ringwald #endif
712b429b9b7Smatthias.ringwald 
713b429b9b7Smatthias.ringwald     return 0;
714b429b9b7Smatthias.ringwald }
715b429b9b7Smatthias.ringwald 
716b429b9b7Smatthias.ringwald 
7178d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
718d661ed19Smatthias.ringwald 
7197b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
720d661ed19Smatthias.ringwald 
7218d213e1aSmatthias.ringwald     int err = 0;
7228d213e1aSmatthias.ringwald     switch (hci_stack.state){
7238d213e1aSmatthias.ringwald 
7248d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7258d213e1aSmatthias.ringwald             switch (power_mode){
7268d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7278d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7288d213e1aSmatthias.ringwald                     if (err) return err;
7297301ad89Smatthias.ringwald                     // set up state machine
7307301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7317301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7327301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7338d213e1aSmatthias.ringwald                     break;
7348d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7358d213e1aSmatthias.ringwald                     // do nothing
7368d213e1aSmatthias.ringwald                     break;
7378d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
738b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7398d213e1aSmatthias.ringwald                     break;
7408d213e1aSmatthias.ringwald             }
7418d213e1aSmatthias.ringwald             break;
7427301ad89Smatthias.ringwald 
7438d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7448d213e1aSmatthias.ringwald             switch (power_mode){
7458d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7468d213e1aSmatthias.ringwald                     // do nothing
7478d213e1aSmatthias.ringwald                     break;
7488d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7498d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7508d213e1aSmatthias.ringwald                     hci_power_control_off();
7518d213e1aSmatthias.ringwald                     break;
7528d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
753b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
75472ea5239Smatthias.ringwald                     hci_power_control_sleep();
7558d213e1aSmatthias.ringwald                     break;
7568d213e1aSmatthias.ringwald             }
7578d213e1aSmatthias.ringwald             break;
7587301ad89Smatthias.ringwald 
7598d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7608d213e1aSmatthias.ringwald             switch (power_mode){
7618d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7628d213e1aSmatthias.ringwald                     // do nothing
7638d213e1aSmatthias.ringwald                     break;
7648d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
765c7e0c5f6Smatthias.ringwald                     // see hci_run
766c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7678d213e1aSmatthias.ringwald                     break;
7688d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
769b546ac54Smatthias.ringwald                     // see hci_run
770b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
77189db417bSmatthias.ringwald                     hci_stack.substate = 0;
7728d213e1aSmatthias.ringwald                     break;
7738d213e1aSmatthias.ringwald             }
7748d213e1aSmatthias.ringwald             break;
7757301ad89Smatthias.ringwald 
7768d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7778d213e1aSmatthias.ringwald             switch (power_mode){
7788d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7798d213e1aSmatthias.ringwald                     // set up state machine
7808d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7818d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7828d213e1aSmatthias.ringwald                     break;
7838d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7848d213e1aSmatthias.ringwald                     // do nothing
7858d213e1aSmatthias.ringwald                     break;
7868d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
787b546ac54Smatthias.ringwald                     // see hci_run
788b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
78989db417bSmatthias.ringwald                     hci_stack.substate = 0;
7908d213e1aSmatthias.ringwald                     break;
7918d213e1aSmatthias.ringwald             }
7928d213e1aSmatthias.ringwald             break;
7938d213e1aSmatthias.ringwald 
7948d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7958d213e1aSmatthias.ringwald             switch (power_mode){
7968d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
797b546ac54Smatthias.ringwald                     // set up state machine
798b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
799b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
800b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8018d213e1aSmatthias.ringwald                     break;
8028d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
803b546ac54Smatthias.ringwald                     // see hci_run
804b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8058d213e1aSmatthias.ringwald                     break;
8068d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
807b546ac54Smatthias.ringwald                     // do nothing
8088d213e1aSmatthias.ringwald                     break;
8098d213e1aSmatthias.ringwald             }
8108d213e1aSmatthias.ringwald             break;
8118d213e1aSmatthias.ringwald 
8128d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
8138d213e1aSmatthias.ringwald             switch (power_mode){
8148d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8153144bce4Smatthias.ringwald                     err = hci_power_control_wake();
8163144bce4Smatthias.ringwald                     if (err) return err;
817b546ac54Smatthias.ringwald                     // set up state machine
818b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
819b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
820b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8218d213e1aSmatthias.ringwald                     break;
8228d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8234ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8248d213e1aSmatthias.ringwald                     break;
8258d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
826b546ac54Smatthias.ringwald                     // do nothing
8278d213e1aSmatthias.ringwald                     break;
8288d213e1aSmatthias.ringwald             }
8298d213e1aSmatthias.ringwald             break;
83011e23e5fSmatthias.ringwald     }
83168d92d03Smatthias.ringwald 
832038bc64cSmatthias.ringwald     // create internal event
833ee8bf225Smatthias.ringwald 	hci_emit_state();
834ee8bf225Smatthias.ringwald 
83568d92d03Smatthias.ringwald 	// trigger next/first action
83668d92d03Smatthias.ringwald 	hci_run();
83768d92d03Smatthias.ringwald 
838475c8125Smatthias.ringwald     return 0;
839475c8125Smatthias.ringwald }
840475c8125Smatthias.ringwald 
841381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
842381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
843381fbed8Smatthias.ringwald 
844381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
845381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
846381fbed8Smatthias.ringwald         return;
847381fbed8Smatthias.ringwald     }
848381fbed8Smatthias.ringwald 
849381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
850381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
851381fbed8Smatthias.ringwald }
852381fbed8Smatthias.ringwald 
85306b35ec0Smatthias.ringwald void hci_run(){
8548a485f27Smatthias.ringwald 
85532ab9390Smatthias.ringwald     hci_connection_t * connection;
85632ab9390Smatthias.ringwald     linked_item_t * it;
85732ab9390Smatthias.ringwald 
858ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
859ce4c8fabSmatthias.ringwald 
860ce4c8fabSmatthias.ringwald     // global/non-connection oriented commands - decline incoming connections
861ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
862ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
863ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
864ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
865ce4c8fabSmatthias.ringwald     }
866ce4c8fabSmatthias.ringwald 
86732ab9390Smatthias.ringwald     // send pending HCI commands
86832ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
86932ab9390Smatthias.ringwald 
87032ab9390Smatthias.ringwald         connection = (hci_connection_t *) it;
87132ab9390Smatthias.ringwald 
87253b86778Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) {
8737b5fbe1fSmatthias.ringwald             // log_info("hci_run: cannot send command packet\n");
87453b86778Smatthias.ringwald             return;
87553b86778Smatthias.ringwald         }
87632ab9390Smatthias.ringwald 
87732ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
8787b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
87932ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
88032ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
881c7e0c5f6Smatthias.ringwald         }
882c7e0c5f6Smatthias.ringwald 
883de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
88432ab9390Smatthias.ringwald 
88532ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
88632ab9390Smatthias.ringwald             link_key_t link_key;
8877b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
88832ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
88932ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
89032ab9390Smatthias.ringwald             } else {
89132ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
89232ab9390Smatthias.ringwald             }
89332ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
89432ab9390Smatthias.ringwald         }
89532ab9390Smatthias.ringwald     }
89632ab9390Smatthias.ringwald 
897de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
898c7e0c5f6Smatthias.ringwald 
8993429f56bSmatthias.ringwald     switch (hci_stack.state){
9003429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9017b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
9023429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
9033429f56bSmatthias.ringwald                 // odd: waiting for command completion
90406b35ec0Smatthias.ringwald                 return;
9053429f56bSmatthias.ringwald             }
90690919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
907c7492964Smatthias.ringwald                 case 0: // RESET
90822909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
909c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
910c7492964Smatthias.ringwald                         // skip baud change
911c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
912c7492964Smatthias.ringwald                     }
9133429f56bSmatthias.ringwald                     break;
914c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
915c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
916c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
917c7492964Smatthias.ringwald                     break;
918c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
919c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
920c7492964Smatthias.ringwald                     hci_stack.substate += 2;
921c7492964Smatthias.ringwald                     // break missing here for fall through
922c7492964Smatthias.ringwald 
923c7492964Smatthias.ringwald                 case 3:
92490919203Smatthias.ringwald                     // custom initialization
92590919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
92690919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
92790919203Smatthias.ringwald                         if (cmd) {
92890919203Smatthias.ringwald                             int size = 3 + cmd[2];
929622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
930c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
93190919203Smatthias.ringwald                             break;
93290919203Smatthias.ringwald                         }
933c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
93490919203Smatthias.ringwald                     }
9352f6c30e1Smatthias.ringwald                     // otherwise continue
936f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
937f432a6ddSmatthias.ringwald 					break;
938c7492964Smatthias.ringwald 				case 4:
939e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
940e2edc0c3Smatthias.ringwald 					break;
941c7492964Smatthias.ringwald                 case 5:
9423429f56bSmatthias.ringwald                     // ca. 15 sec
9433429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9443429f56bSmatthias.ringwald                     break;
945c7492964Smatthias.ringwald 				case 6:
9460a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
947f432a6ddSmatthias.ringwald 					break;
948c7492964Smatthias.ringwald                 case 7:
9498a485f27Smatthias.ringwald #ifndef EMBEDDED
9508a485f27Smatthias.ringwald                 {
9518a485f27Smatthias.ringwald                     char hostname[30];
9528a485f27Smatthias.ringwald                     gethostname(hostname, 30);
9538a485f27Smatthias.ringwald                     hostname[29] = '\0';
9548a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
9558a485f27Smatthias.ringwald                     break;
9568a485f27Smatthias.ringwald                 }
957c7492964Smatthias.ringwald                 case 8:
9583c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
9593c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
9603c4d4b90Smatthias.ringwald                     break;
9613c4d4b90Smatthias.ringwald 
962c7492964Smatthias.ringwald                 case 9:
9633c4d4b90Smatthias.ringwald #endif
9648a485f27Smatthias.ringwald #endif
9653429f56bSmatthias.ringwald                     // done.
9663429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
967b360b6adSmatthias.ringwald                     hci_emit_state();
9683429f56bSmatthias.ringwald                     break;
9693429f56bSmatthias.ringwald                 default:
9703429f56bSmatthias.ringwald                     break;
971475c8125Smatthias.ringwald             }
9723429f56bSmatthias.ringwald             hci_stack.substate++;
9733429f56bSmatthias.ringwald             break;
974c7e0c5f6Smatthias.ringwald 
975c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
976c7e0c5f6Smatthias.ringwald 
9777b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
978c7e0c5f6Smatthias.ringwald             // close all open connections
979c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
980c7e0c5f6Smatthias.ringwald             if (connection){
98132ab9390Smatthias.ringwald 
982c7e0c5f6Smatthias.ringwald                 // send disconnect
98332ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
98432ab9390Smatthias.ringwald 
9857b5fbe1fSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
9866ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
987c7e0c5f6Smatthias.ringwald 
988c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
989c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
990c7e0c5f6Smatthias.ringwald                 return;
991c7e0c5f6Smatthias.ringwald             }
9927b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
993c7e0c5f6Smatthias.ringwald 
99472ea5239Smatthias.ringwald             // switch mode
995c7e0c5f6Smatthias.ringwald             hci_power_control_off();
9969418f9c9Smatthias.ringwald 
9977b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
99872ea5239Smatthias.ringwald             hci_emit_state();
9997b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
100072ea5239Smatthias.ringwald             break;
1001c7e0c5f6Smatthias.ringwald 
100272ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
100389db417bSmatthias.ringwald             switch(hci_stack.substate) {
100489db417bSmatthias.ringwald                 case 0:
10057b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
100672ea5239Smatthias.ringwald                     // close all open connections
100772ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
100872ea5239Smatthias.ringwald                     if (connection){
100932ab9390Smatthias.ringwald 
101072ea5239Smatthias.ringwald                         // send disconnect
101132ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
101232ab9390Smatthias.ringwald 
10137b5fbe1fSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
10146ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
101572ea5239Smatthias.ringwald 
101672ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
101772ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
101872ea5239Smatthias.ringwald                         return;
101972ea5239Smatthias.ringwald                     }
102072ea5239Smatthias.ringwald 
102189db417bSmatthias.ringwald                     // disable page and inquiry scan
102232ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
102332ab9390Smatthias.ringwald 
10247b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
102589db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
102689db417bSmatthias.ringwald 
102789db417bSmatthias.ringwald                     // continue in next sub state
102889db417bSmatthias.ringwald                     hci_stack.substate++;
102989db417bSmatthias.ringwald                     break;
103089db417bSmatthias.ringwald                 case 1:
103189db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
103289db417bSmatthias.ringwald                     break;
103389db417bSmatthias.ringwald                 case 2:
10347b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
103572ea5239Smatthias.ringwald                     // switch mode
103689db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1037c7e0c5f6Smatthias.ringwald                     hci_emit_state();
103889db417bSmatthias.ringwald                 default:
103989db417bSmatthias.ringwald                     break;
104089db417bSmatthias.ringwald             }
1041c7e0c5f6Smatthias.ringwald             break;
1042c7e0c5f6Smatthias.ringwald 
10433429f56bSmatthias.ringwald         default:
10443429f56bSmatthias.ringwald             break;
10451f504dbdSmatthias.ringwald     }
10463429f56bSmatthias.ringwald }
104716833f0aSmatthias.ringwald 
104831452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1049c8e4258aSmatthias.ringwald     bd_addr_t addr;
1050c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1051c8e4258aSmatthias.ringwald     // house-keeping
1052c8e4258aSmatthias.ringwald 
1053c8e4258aSmatthias.ringwald     // create_connection?
1054c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1055c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
10567b5fbe1fSmatthias.ringwald         log_info("Create_connection to "); print_bd_addr(addr); log_info("\n");
1057c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1058c8e4258aSmatthias.ringwald         if (conn) {
1059c8e4258aSmatthias.ringwald             // if connection exists
1060c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
106117f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
106217f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1063c8e4258aSmatthias.ringwald             }
106417f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1065c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1066c8e4258aSmatthias.ringwald 
106717f1ba2aSmatthias.ringwald         }
1068c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
106917f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
107017f1ba2aSmatthias.ringwald         if (!conn){
107117f1ba2aSmatthias.ringwald             // notify client that alloc failed
107217f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
107317f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
107417f1ba2aSmatthias.ringwald         }
1075c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1076c8e4258aSmatthias.ringwald     }
1077c8e4258aSmatthias.ringwald 
10787fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
10797fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
10807fde4af9Smatthias.ringwald     }
10817fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
10827fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
10837fde4af9Smatthias.ringwald     }
10847fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
10857fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
10867fde4af9Smatthias.ringwald     }
10877fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
10887fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
10897fde4af9Smatthias.ringwald     }
10907fde4af9Smatthias.ringwald 
10918ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
10928ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
10938ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10948ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
10958ef73945Smatthias.ringwald         }
10968ef73945Smatthias.ringwald     }
1097c8e4258aSmatthias.ringwald 
109831452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1099622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
110031452debSmatthias.ringwald }
11018adf0ddaSmatthias.ringwald 
11021cd208adSmatthias.ringwald /**
11031cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
11041cd208adSmatthias.ringwald  */
1105fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
11061cd208adSmatthias.ringwald     va_list argptr;
11071cd208adSmatthias.ringwald     va_start(argptr, cmd);
11081cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
11091cd208adSmatthias.ringwald     va_end(argptr);
1110124062bcSmatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_cmd_buffer, size);
111193b8dc03Smatthias.ringwald }
1112c8e4258aSmatthias.ringwald 
1113ee091cf1Smatthias.ringwald // Create various non-HCI events.
1114ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1115ee091cf1Smatthias.ringwald 
1116c8e4258aSmatthias.ringwald void hci_emit_state(){
1117425d1371Smatthias.ringwald     uint8_t event[3];
111880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1119425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1120c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1121425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1122425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1123c8e4258aSmatthias.ringwald }
1124c8e4258aSmatthias.ringwald 
112517f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1126425d1371Smatthias.ringwald     uint8_t event[13];
1127c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1128425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
112917f1ba2aSmatthias.ringwald     event[2] = status;
1130c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1131c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1132c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1133c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1134425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1135425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1136c8e4258aSmatthias.ringwald }
1137c8e4258aSmatthias.ringwald 
11383c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1139425d1371Smatthias.ringwald     uint8_t event[6];
11403c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1141e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
11423c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
11433c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
11443c4d4b90Smatthias.ringwald     event[5] = reason;
1145425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1146425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11473c4d4b90Smatthias.ringwald }
11483c4d4b90Smatthias.ringwald 
1149ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1150425d1371Smatthias.ringwald     uint8_t event[4];
115180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1152425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1153ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1154425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1155425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1156ee091cf1Smatthias.ringwald }
115743bfb1bdSmatthias.ringwald 
115843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1159425d1371Smatthias.ringwald     uint8_t event[3];
116080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1161425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
116243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1163425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1164425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
116543bfb1bdSmatthias.ringwald }
1166038bc64cSmatthias.ringwald 
1167038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1168425d1371Smatthias.ringwald     uint8_t event[2];
116980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1170425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1171425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1172425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1173038bc64cSmatthias.ringwald }
11741b0e3922Smatthias.ringwald 
11751b0e3922Smatthias.ringwald 
11761b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1177425d1371Smatthias.ringwald     uint8_t event[6];
11781b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1179425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1180425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1181425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1182425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1183425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1184425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11851b0e3922Smatthias.ringwald }
11861b0e3922Smatthias.ringwald 
11872ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1188425d1371Smatthias.ringwald     uint8_t event[3];
11892ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1190425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
11912ed6235cSmatthias.ringwald     event[2] = enabled;
1192425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1193e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11942ed6235cSmatthias.ringwald }
1195627c2f45Smatthias.ringwald 
1196627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1197425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1198627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1199425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1200f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
12012f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1202f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1203425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1204425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1205627c2f45Smatthias.ringwald }
1206381fbed8Smatthias.ringwald 
1207381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1208425d1371Smatthias.ringwald     uint8_t event[3];
1209381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1210425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1211381fbed8Smatthias.ringwald     event[2] = enabled;
1212425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1213425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1214381fbed8Smatthias.ringwald }
1215