xref: /btstack/src/hci.c (revision 7b5fbe1fe360de5c158bf2fe68046e05388a60ec)
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 
497f2435e6Smatthias.ringwald #include "debug.h"
50d8905019Smatthias.ringwald #include "hci_dump.h"
5193b8dc03Smatthias.ringwald 
52ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
53ae1fd9f3Smatthias.ringwald #include <btstack/version.h>
541b0e3922Smatthias.ringwald 
551e6aba47Smatthias.ringwald // temp
561e6aba47Smatthias.ringwald #include "l2cap.h"
571e6aba47Smatthias.ringwald 
58169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
59ee091cf1Smatthias.ringwald 
6006b35ec0Smatthias.ringwald // the STACK is here
6116833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
6216833f0aSmatthias.ringwald 
6397addcc5Smatthias.ringwald /**
64ee091cf1Smatthias.ringwald  * get connection for a given handle
65ee091cf1Smatthias.ringwald  *
66ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
67ee091cf1Smatthias.ringwald  */
68645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
69ee091cf1Smatthias.ringwald     linked_item_t *it;
70ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
71ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
72ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
73ee091cf1Smatthias.ringwald         }
74ee091cf1Smatthias.ringwald     }
75ee091cf1Smatthias.ringwald     return NULL;
76ee091cf1Smatthias.ringwald }
77ee091cf1Smatthias.ringwald 
78c7492964Smatthias.ringwald #ifdef HAVE_TIME
792b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
80ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
81ee091cf1Smatthias.ringwald     struct timeval tv;
82ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
83c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
84ee091cf1Smatthias.ringwald         // connections might be timed out
85ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
86c21e6239Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
87ee091cf1Smatthias.ringwald     } else {
88ee091cf1Smatthias.ringwald         // next timeout check at
89c21e6239Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
90ee091cf1Smatthias.ringwald     }
91ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
92ee091cf1Smatthias.ringwald }
932b12a0b9Smatthias.ringwald #endif
94ee091cf1Smatthias.ringwald 
95ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
96c7492964Smatthias.ringwald #ifdef HAVE_TIME
97ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
98c7492964Smatthias.ringwald #endif
99ee091cf1Smatthias.ringwald }
100ee091cf1Smatthias.ringwald 
101ee091cf1Smatthias.ringwald /**
102c8e4258aSmatthias.ringwald  * create connection for given address
103c8e4258aSmatthias.ringwald  *
104c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
105c8e4258aSmatthias.ringwald  */
106c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
107c8e4258aSmatthias.ringwald     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
108c8e4258aSmatthias.ringwald     if (!conn) return NULL;
109c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
110c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1117d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
112c7492964Smatthias.ringwald #ifdef HAVE_TIME
113ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
114ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
115ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
116c7492964Smatthias.ringwald #endif
117d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1187856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
11956cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
120c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
121c8e4258aSmatthias.ringwald     return conn;
122c8e4258aSmatthias.ringwald }
123c8e4258aSmatthias.ringwald 
124c8e4258aSmatthias.ringwald /**
12506b35ec0Smatthias.ringwald  * get connection for given address
12697addcc5Smatthias.ringwald  *
12797addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
12897addcc5Smatthias.ringwald  */
129fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
13006b35ec0Smatthias.ringwald     linked_item_t *it;
13106b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
13206b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
13306b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
13406b35ec0Smatthias.ringwald         }
13506b35ec0Smatthias.ringwald     }
13606b35ec0Smatthias.ringwald     return NULL;
13706b35ec0Smatthias.ringwald }
13806b35ec0Smatthias.ringwald 
13943bfb1bdSmatthias.ringwald /**
14080ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1417fde4af9Smatthias.ringwald  */
1427fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1437fde4af9Smatthias.ringwald     bd_addr_t addr;
1447fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1457fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1467fde4af9Smatthias.ringwald     if (conn) {
1477fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
14880ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1497fde4af9Smatthias.ringwald     }
1507fde4af9Smatthias.ringwald }
1517fde4af9Smatthias.ringwald 
15280ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
15380ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
15480ca58a0Smatthias.ringwald     if (!conn) return 0;
15580ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
15680ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
15780ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
15880ca58a0Smatthias.ringwald     return 1;
15980ca58a0Smatthias.ringwald }
16080ca58a0Smatthias.ringwald 
161c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
162c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
163c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
164c12e46e7Smatthias.ringwald     }
165c12e46e7Smatthias.ringwald }
166c12e46e7Smatthias.ringwald 
1677fde4af9Smatthias.ringwald 
1687fde4af9Smatthias.ringwald /**
16943bfb1bdSmatthias.ringwald  * count connections
17043bfb1bdSmatthias.ringwald  */
17140d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
17256c253c9Smatthias.ringwald     int count = 0;
17343bfb1bdSmatthias.ringwald     linked_item_t *it;
17443bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
17543bfb1bdSmatthias.ringwald     return count;
17643bfb1bdSmatthias.ringwald }
177c8e4258aSmatthias.ringwald 
17897addcc5Smatthias.ringwald /**
179ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
18016833f0aSmatthias.ringwald  */
1812718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
18216833f0aSmatthias.ringwald }
18316833f0aSmatthias.ringwald 
184998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
185998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
186998906cdSmatthias.ringwald     if (!connection) {
187998906cdSmatthias.ringwald         log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
188998906cdSmatthias.ringwald         return 0;
189998906cdSmatthias.ringwald     }
190998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
191998906cdSmatthias.ringwald }
192998906cdSmatthias.ringwald 
193998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
194998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
195998906cdSmatthias.ringwald     linked_item_t *it;
196998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
197998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
198998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
199998906cdSmatthias.ringwald             log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
200998906cdSmatthias.ringwald             return 0;
201998906cdSmatthias.ringwald         }
202998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
203998906cdSmatthias.ringwald     }
204998906cdSmatthias.ringwald     return free_slots;
205998906cdSmatthias.ringwald }
206998906cdSmatthias.ringwald 
2075250fb9eSmatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
2085250fb9eSmatthias.ringwald     return hci_stack.acl_data_packet_length;
2095250fb9eSmatthias.ringwald }
2105250fb9eSmatthias.ringwald 
211c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2122b12a0b9Smatthias.ringwald 
2132b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2142b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2152b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2162b12a0b9Smatthias.ringwald             return 0;
2172b12a0b9Smatthias.ringwald         }
2182b12a0b9Smatthias.ringwald     }
2192b12a0b9Smatthias.ringwald 
2202b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
221c24735b1Smatthias.ringwald     switch (packet_type) {
222c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
223c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
224c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
225de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
226c24735b1Smatthias.ringwald         default:
227c24735b1Smatthias.ringwald             return 0;
228c24735b1Smatthias.ringwald     }
229c24735b1Smatthias.ringwald }
230c24735b1Smatthias.ringwald 
231ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2327856c818Smatthias.ringwald 
2336218e6f1Smatthias.ringwald     // check for free places on BT module
2345932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2356218e6f1Smatthias.ringwald 
2367856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2377856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
23856cf178bSmatthias.ringwald     if (!connection) return 0;
23956cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
24056cf178bSmatthias.ringwald 
24156cf178bSmatthias.ringwald     // count packet
24256cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
243*7b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2447856c818Smatthias.ringwald 
2456218e6f1Smatthias.ringwald     // send packet - ignore errors
246622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2476218e6f1Smatthias.ringwald 
2486218e6f1Smatthias.ringwald     return 0;
249ee091cf1Smatthias.ringwald }
250ee091cf1Smatthias.ringwald 
25116833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2527856c818Smatthias.ringwald 
2537856c818Smatthias.ringwald     // get info
2547856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2557856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2567856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2577856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2587856c818Smatthias.ringwald 
2597856c818Smatthias.ringwald     // ignore non-registered handle
2607856c818Smatthias.ringwald     if (!conn){
2617f2435e6Smatthias.ringwald         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2627856c818Smatthias.ringwald         return;
2637856c818Smatthias.ringwald     }
2647856c818Smatthias.ringwald 
2657856c818Smatthias.ringwald     // update idle timestamp
2667856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2677856c818Smatthias.ringwald 
2687856c818Smatthias.ringwald     // handle different packet types
2697856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2707856c818Smatthias.ringwald 
2717856c818Smatthias.ringwald         case 0x01: // continuation fragment
2727856c818Smatthias.ringwald 
2737856c818Smatthias.ringwald             // sanity check
2747856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2757f2435e6Smatthias.ringwald                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2767856c818Smatthias.ringwald                 return;
2777856c818Smatthias.ringwald             }
2787856c818Smatthias.ringwald 
2797856c818Smatthias.ringwald             // append fragment payload (header already stored)
2807856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2817856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2827856c818Smatthias.ringwald 
283decc01a8Smatthias.ringwald             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
284decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
2857856c818Smatthias.ringwald 
2867856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2877856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2887856c818Smatthias.ringwald 
2892718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2907856c818Smatthias.ringwald                 // reset recombination buffer
2917856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2927856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2937856c818Smatthias.ringwald             }
2947856c818Smatthias.ringwald             break;
2957856c818Smatthias.ringwald 
2967856c818Smatthias.ringwald         case 0x02: { // first fragment
2977856c818Smatthias.ringwald 
2987856c818Smatthias.ringwald             // sanity check
2997856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3007f2435e6Smatthias.ringwald                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3017856c818Smatthias.ringwald                 return;
3027856c818Smatthias.ringwald             }
3037856c818Smatthias.ringwald 
3047856c818Smatthias.ringwald             // peek into L2CAP packet!
3057856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3067856c818Smatthias.ringwald 
307decc01a8Smatthias.ringwald             // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
308decc01a8Smatthias.ringwald 
3097856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3107856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3117856c818Smatthias.ringwald 
3127856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3132718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3147856c818Smatthias.ringwald 
3157856c818Smatthias.ringwald             } else {
3167856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3177856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3187856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3197856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
320decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3217856c818Smatthias.ringwald             }
3227856c818Smatthias.ringwald             break;
3237856c818Smatthias.ringwald 
3247856c818Smatthias.ringwald         }
3257856c818Smatthias.ringwald         default:
3267f2435e6Smatthias.ringwald             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3277856c818Smatthias.ringwald             return;
3287856c818Smatthias.ringwald     }
32994ab26f8Smatthias.ringwald 
33094ab26f8Smatthias.ringwald     // execute main loop
33194ab26f8Smatthias.ringwald     hci_run();
33216833f0aSmatthias.ringwald }
33322909952Smatthias.ringwald 
33467a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
335*7b5fbe1fSmatthias.ringwald     log_info("Connection closed: handle %u, ", conn->con_handle);
336c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
337*7b5fbe1fSmatthias.ringwald     log_info("\n");
3383c4d4b90Smatthias.ringwald 
3393c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3403c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3413c4d4b90Smatthias.ringwald 
342c7492964Smatthias.ringwald #ifdef HAVE_TIME
343c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
344c7492964Smatthias.ringwald #endif
345c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
346c7e0c5f6Smatthias.ringwald     free( conn );
3473c4d4b90Smatthias.ringwald 
3483c4d4b90Smatthias.ringwald     // now it's gone
349c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
350c7e0c5f6Smatthias.ringwald }
351c7e0c5f6Smatthias.ringwald 
35274ec757aSmatthias.ringwald // avoid huge local variables
35374ec757aSmatthias.ringwald static device_name_t device_name;
35416833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3551281a47eSmatthias.ringwald     bd_addr_t addr;
3562d00edd4Smatthias.ringwald     uint8_t link_type;
357fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3581f7b95a1Smatthias.ringwald     hci_connection_t * conn;
35956cf178bSmatthias.ringwald     int i;
36022909952Smatthias.ringwald 
3616772a24cSmatthias.ringwald     switch (packet[0]) {
36222909952Smatthias.ringwald 
3636772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3647ec5eeaaSmatthias.ringwald             // get num cmd packets
365*7b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3667ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3677ec5eeaaSmatthias.ringwald 
368e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
369e2edc0c3Smatthias.ringwald                 // from offset 5
370e2edc0c3Smatthias.ringwald                 // status
3711d279b20Smatthias.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"
372e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
37356cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
374e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
37556cf178bSmatthias.ringwald                 // ignore: total num SCO packets
37656cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
377*7b5fbe1fSmatthias.ringwald                     log_info("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
378e2edc0c3Smatthias.ringwald                 }
37956cf178bSmatthias.ringwald             }
380381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
381381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
382381fbed8Smatthias.ringwald             }
38356cf178bSmatthias.ringwald             break;
38456cf178bSmatthias.ringwald 
3857ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
3867ec5eeaaSmatthias.ringwald             // get num cmd packets
387*7b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
3887ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
3897ec5eeaaSmatthias.ringwald             break;
3907ec5eeaaSmatthias.ringwald 
39156cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
39256cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
39356cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
39456cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
39556cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
39656cf178bSmatthias.ringwald                 if (!conn){
39756cf178bSmatthias.ringwald                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
39856cf178bSmatthias.ringwald                     continue;
39956cf178bSmatthias.ringwald                 }
40056cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
401*7b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
40256cf178bSmatthias.ringwald             }
4036772a24cSmatthias.ringwald             break;
4046772a24cSmatthias.ringwald 
4051f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
40637eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
40737eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4082d00edd4Smatthias.ringwald             link_type = packet[11];
409*7b5fbe1fSmatthias.ringwald             log_info("Connection_incoming: "); print_bd_addr(addr); log_info(", type %u\n", link_type);
41037eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4111f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4121f7b95a1Smatthias.ringwald                 if (!conn) {
4131f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4141f7b95a1Smatthias.ringwald                 }
4151f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
41632ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
41732ab9390Smatthias.ringwald                 hci_run();
41837eaa4cfSmatthias.ringwald             } else {
41937eaa4cfSmatthias.ringwald                 // TODO: decline request
42037eaa4cfSmatthias.ringwald             }
4211f7b95a1Smatthias.ringwald             break;
4221f7b95a1Smatthias.ringwald 
4236772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
424fe1ed1b8Smatthias.ringwald             // Connection management
425fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
426*7b5fbe1fSmatthias.ringwald             log_info("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_info("\n");
4271f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
428fe1ed1b8Smatthias.ringwald             if (conn) {
429b448a0e7Smatthias.ringwald                 if (!packet[2]){
430c8e4258aSmatthias.ringwald                     conn->state = OPEN;
431fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
432ee091cf1Smatthias.ringwald 
433c7492964Smatthias.ringwald #ifdef HAVE_TIME
434ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
435c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
436ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
437c7492964Smatthias.ringwald #endif
438*7b5fbe1fSmatthias.ringwald                     log_info("New connection: handle %u, ", conn->con_handle);
439fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
440*7b5fbe1fSmatthias.ringwald                     log_info("\n");
44143bfb1bdSmatthias.ringwald 
44243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
443b448a0e7Smatthias.ringwald                 } else {
444b448a0e7Smatthias.ringwald                     // connection failed, remove entry
445b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
446b448a0e7Smatthias.ringwald                     free( conn );
447c12e46e7Smatthias.ringwald 
448c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
449c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
450c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
451c12e46e7Smatthias.ringwald                     }
452fe1ed1b8Smatthias.ringwald                 }
453fe1ed1b8Smatthias.ringwald             }
4546772a24cSmatthias.ringwald             break;
455fe1ed1b8Smatthias.ringwald 
4567fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
457*7b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
4587fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
45974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
46032ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
46164472d52Smatthias.ringwald             hci_run();
46229d53098Smatthias.ringwald             // request already answered
46329d53098Smatthias.ringwald             return;
4647fde4af9Smatthias.ringwald 
4657fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4667fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
46774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
46829d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
469287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
47029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4717fde4af9Smatthias.ringwald             break;
4727fde4af9Smatthias.ringwald 
4737fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4747fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4757fde4af9Smatthias.ringwald             break;
4767fde4af9Smatthias.ringwald 
47774ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
47874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
47974ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
48074ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
4815a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
4825a06394aSmatthias.ringwald             for (i=0; i<248;i++){
4835a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
4845a06394aSmatthias.ringwald                     packet[9+i] = 0;
4855a06394aSmatthias.ringwald                     break;
486cdc9101dSmatthias.ringwald                 }
4875a06394aSmatthias.ringwald             }
48874ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
48974ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
49074ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
49174ec757aSmatthias.ringwald             break;
49274ec757aSmatthias.ringwald 
49374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
49474ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
49574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
49674ec757aSmatthias.ringwald             // first send inq result packet
49774ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
49874ec757aSmatthias.ringwald             // then send cached remote names
49974ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
50074ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
50174ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
50274ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
50374ec757aSmatthias.ringwald                 }
50474ec757aSmatthias.ringwald             }
50574ec757aSmatthias.ringwald             return;
50674ec757aSmatthias.ringwald 
5076772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
508fe1ed1b8Smatthias.ringwald             if (!packet[2]){
509fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
510fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
511fe1ed1b8Smatthias.ringwald                 if (conn) {
512c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
513fe1ed1b8Smatthias.ringwald                 }
514fe1ed1b8Smatthias.ringwald             }
5156772a24cSmatthias.ringwald             break;
5166772a24cSmatthias.ringwald 
517c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
518c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
519c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
520c68bdf90Smatthias.ringwald             }
521c68bdf90Smatthias.ringwald             break;
522c68bdf90Smatthias.ringwald 
5236772a24cSmatthias.ringwald         default:
5246772a24cSmatthias.ringwald             break;
525fe1ed1b8Smatthias.ringwald     }
526fe1ed1b8Smatthias.ringwald 
5273429f56bSmatthias.ringwald     // handle BT initialization
5283429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5297301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5307301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5317301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5327301ad89Smatthias.ringwald         // }
5337301ad89Smatthias.ringwald         // handle normal init sequence
5343429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5353429f56bSmatthias.ringwald             // odd: waiting for event
5363429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5373429f56bSmatthias.ringwald                 hci_stack.substate++;
5383429f56bSmatthias.ringwald             }
5393429f56bSmatthias.ringwald         }
54022909952Smatthias.ringwald     }
54122909952Smatthias.ringwald 
54289db417bSmatthias.ringwald     // help with BT sleep
54389db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
54489db417bSmatthias.ringwald         && hci_stack.substate == 1
54589db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
54689db417bSmatthias.ringwald         hci_stack.substate++;
54789db417bSmatthias.ringwald     }
54889db417bSmatthias.ringwald 
5492718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
55094ab26f8Smatthias.ringwald 
55194ab26f8Smatthias.ringwald 	// execute main loop
55294ab26f8Smatthias.ringwald 	hci_run();
55316833f0aSmatthias.ringwald }
55416833f0aSmatthias.ringwald 
55510e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
55610e830c9Smatthias.ringwald     switch (packet_type) {
55710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
55810e830c9Smatthias.ringwald             event_handler(packet, size);
55910e830c9Smatthias.ringwald             break;
56010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
56110e830c9Smatthias.ringwald             acl_handler(packet, size);
56210e830c9Smatthias.ringwald             break;
56310e830c9Smatthias.ringwald         default:
56410e830c9Smatthias.ringwald             break;
56510e830c9Smatthias.ringwald     }
56610e830c9Smatthias.ringwald }
56710e830c9Smatthias.ringwald 
568fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5692718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5702718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
57116833f0aSmatthias.ringwald }
57216833f0aSmatthias.ringwald 
573404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
574475c8125Smatthias.ringwald 
575475c8125Smatthias.ringwald     // reference to use transport layer implementation
57616833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
577475c8125Smatthias.ringwald 
57811e23e5fSmatthias.ringwald     // references to used control implementation
57911e23e5fSmatthias.ringwald     hci_stack.control = control;
58011e23e5fSmatthias.ringwald 
58111e23e5fSmatthias.ringwald     // reference to used config
58211e23e5fSmatthias.ringwald     hci_stack.config = config;
58311e23e5fSmatthias.ringwald 
584fe1ed1b8Smatthias.ringwald     // no connections yet
585fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
5860a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
587fe1ed1b8Smatthias.ringwald 
58802ea9861Smatthias.ringwald     // empty cmd buffer
58916833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
59016833f0aSmatthias.ringwald 
59116833f0aSmatthias.ringwald     // higher level handler
5922718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
59316833f0aSmatthias.ringwald 
594404843c1Smatthias.ringwald     // store and open remote device db
595404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
596404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
597404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
598404843c1Smatthias.ringwald     }
59929d53098Smatthias.ringwald 
60016833f0aSmatthias.ringwald     // register packet handlers with transport
60110e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
602475c8125Smatthias.ringwald }
603475c8125Smatthias.ringwald 
604404843c1Smatthias.ringwald void hci_close(){
605404843c1Smatthias.ringwald     // close remote device db
606404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
607404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
608404843c1Smatthias.ringwald     }
609404843c1Smatthias.ringwald }
610404843c1Smatthias.ringwald 
6118d213e1aSmatthias.ringwald // State-Module-Driver overview
6128d213e1aSmatthias.ringwald // state                    module  low-level
6138d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6148d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6158d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6168d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
617d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
618d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
619c7e0c5f6Smatthias.ringwald 
62040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6217301ad89Smatthias.ringwald 
622038bc64cSmatthias.ringwald     // power on
623f9a30166Smatthias.ringwald     int err = 0;
624f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
625f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
626f9a30166Smatthias.ringwald     }
627038bc64cSmatthias.ringwald     if (err){
6287f2435e6Smatthias.ringwald         log_err( "POWER_ON failed\n");
629038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
630038bc64cSmatthias.ringwald         return err;
631038bc64cSmatthias.ringwald     }
632038bc64cSmatthias.ringwald 
633038bc64cSmatthias.ringwald     // open low-level device
634038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
635038bc64cSmatthias.ringwald     if (err){
6367f2435e6Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
637f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
638f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
639f9a30166Smatthias.ringwald         }
640038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
641038bc64cSmatthias.ringwald         return err;
642038bc64cSmatthias.ringwald     }
6438d213e1aSmatthias.ringwald     return 0;
6448d213e1aSmatthias.ringwald }
645038bc64cSmatthias.ringwald 
64640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6478d213e1aSmatthias.ringwald 
648*7b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
6499418f9c9Smatthias.ringwald 
6508d213e1aSmatthias.ringwald     // close low-level device
6518d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6528d213e1aSmatthias.ringwald 
653*7b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
6549418f9c9Smatthias.ringwald 
6558d213e1aSmatthias.ringwald     // power off
6568d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6578d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6588d213e1aSmatthias.ringwald     }
6599418f9c9Smatthias.ringwald 
660*7b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
6619418f9c9Smatthias.ringwald 
66272ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
66372ea5239Smatthias.ringwald }
66472ea5239Smatthias.ringwald 
66540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
66672ea5239Smatthias.ringwald 
667*7b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
6683144bce4Smatthias.ringwald 
669b429b9b7Smatthias.ringwald #if 0
670b429b9b7Smatthias.ringwald     // don't close serial port during sleep
671b429b9b7Smatthias.ringwald 
67272ea5239Smatthias.ringwald     // close low-level device
67372ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
674b429b9b7Smatthias.ringwald #endif
67572ea5239Smatthias.ringwald 
67672ea5239Smatthias.ringwald     // sleep mode
6773144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
67872ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
67972ea5239Smatthias.ringwald     }
680b429b9b7Smatthias.ringwald 
68172ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6828d213e1aSmatthias.ringwald }
6838d213e1aSmatthias.ringwald 
68440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
685b429b9b7Smatthias.ringwald 
686*7b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
687b429b9b7Smatthias.ringwald 
688b429b9b7Smatthias.ringwald     // wake on
689b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
690b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
691b429b9b7Smatthias.ringwald     }
692b429b9b7Smatthias.ringwald 
693b429b9b7Smatthias.ringwald #if 0
694b429b9b7Smatthias.ringwald     // open low-level device
695b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
696b429b9b7Smatthias.ringwald     if (err){
697b429b9b7Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
698b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
699b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
700b429b9b7Smatthias.ringwald         }
701b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
702b429b9b7Smatthias.ringwald         return err;
703b429b9b7Smatthias.ringwald     }
704b429b9b7Smatthias.ringwald #endif
705b429b9b7Smatthias.ringwald 
706b429b9b7Smatthias.ringwald     return 0;
707b429b9b7Smatthias.ringwald }
708b429b9b7Smatthias.ringwald 
709b429b9b7Smatthias.ringwald 
7108d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
711d661ed19Smatthias.ringwald 
712*7b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
713d661ed19Smatthias.ringwald 
7148d213e1aSmatthias.ringwald     int err = 0;
7158d213e1aSmatthias.ringwald     switch (hci_stack.state){
7168d213e1aSmatthias.ringwald 
7178d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7188d213e1aSmatthias.ringwald             switch (power_mode){
7198d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7208d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7218d213e1aSmatthias.ringwald                     if (err) return err;
7227301ad89Smatthias.ringwald                     // set up state machine
7237301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7247301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7257301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7268d213e1aSmatthias.ringwald                     break;
7278d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7288d213e1aSmatthias.ringwald                     // do nothing
7298d213e1aSmatthias.ringwald                     break;
7308d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
731b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7328d213e1aSmatthias.ringwald                     break;
7338d213e1aSmatthias.ringwald             }
7348d213e1aSmatthias.ringwald             break;
7357301ad89Smatthias.ringwald 
7368d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7378d213e1aSmatthias.ringwald             switch (power_mode){
7388d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7398d213e1aSmatthias.ringwald                     // do nothing
7408d213e1aSmatthias.ringwald                     break;
7418d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7428d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7438d213e1aSmatthias.ringwald                     hci_power_control_off();
7448d213e1aSmatthias.ringwald                     break;
7458d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
746b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
74772ea5239Smatthias.ringwald                     hci_power_control_sleep();
7488d213e1aSmatthias.ringwald                     break;
7498d213e1aSmatthias.ringwald             }
7508d213e1aSmatthias.ringwald             break;
7517301ad89Smatthias.ringwald 
7528d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7538d213e1aSmatthias.ringwald             switch (power_mode){
7548d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7558d213e1aSmatthias.ringwald                     // do nothing
7568d213e1aSmatthias.ringwald                     break;
7578d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
758c7e0c5f6Smatthias.ringwald                     // see hci_run
759c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7608d213e1aSmatthias.ringwald                     break;
7618d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
762b546ac54Smatthias.ringwald                     // see hci_run
763b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
76489db417bSmatthias.ringwald                     hci_stack.substate = 0;
7658d213e1aSmatthias.ringwald                     break;
7668d213e1aSmatthias.ringwald             }
7678d213e1aSmatthias.ringwald             break;
7687301ad89Smatthias.ringwald 
7698d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7708d213e1aSmatthias.ringwald             switch (power_mode){
7718d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7728d213e1aSmatthias.ringwald                     // set up state machine
7738d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7748d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7758d213e1aSmatthias.ringwald                     break;
7768d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7778d213e1aSmatthias.ringwald                     // do nothing
7788d213e1aSmatthias.ringwald                     break;
7798d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
780b546ac54Smatthias.ringwald                     // see hci_run
781b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
78289db417bSmatthias.ringwald                     hci_stack.substate = 0;
7838d213e1aSmatthias.ringwald                     break;
7848d213e1aSmatthias.ringwald             }
7858d213e1aSmatthias.ringwald             break;
7868d213e1aSmatthias.ringwald 
7878d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7888d213e1aSmatthias.ringwald             switch (power_mode){
7898d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
790b546ac54Smatthias.ringwald                     // set up state machine
791b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
792b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
793b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7948d213e1aSmatthias.ringwald                     break;
7958d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
796b546ac54Smatthias.ringwald                     // see hci_run
797b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7988d213e1aSmatthias.ringwald                     break;
7998d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
800b546ac54Smatthias.ringwald                     // do nothing
8018d213e1aSmatthias.ringwald                     break;
8028d213e1aSmatthias.ringwald             }
8038d213e1aSmatthias.ringwald             break;
8048d213e1aSmatthias.ringwald 
8058d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
8068d213e1aSmatthias.ringwald             switch (power_mode){
8078d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8083144bce4Smatthias.ringwald                     err = hci_power_control_wake();
8093144bce4Smatthias.ringwald                     if (err) return err;
810b546ac54Smatthias.ringwald                     // set up state machine
811b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
812b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
813b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8148d213e1aSmatthias.ringwald                     break;
8158d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8164ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8178d213e1aSmatthias.ringwald                     break;
8188d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
819b546ac54Smatthias.ringwald                     // do nothing
8208d213e1aSmatthias.ringwald                     break;
8218d213e1aSmatthias.ringwald             }
8228d213e1aSmatthias.ringwald             break;
82311e23e5fSmatthias.ringwald     }
82468d92d03Smatthias.ringwald 
825038bc64cSmatthias.ringwald     // create internal event
826ee8bf225Smatthias.ringwald 	hci_emit_state();
827ee8bf225Smatthias.ringwald 
82868d92d03Smatthias.ringwald 	// trigger next/first action
82968d92d03Smatthias.ringwald 	hci_run();
83068d92d03Smatthias.ringwald 
831475c8125Smatthias.ringwald     return 0;
832475c8125Smatthias.ringwald }
833475c8125Smatthias.ringwald 
834381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
835381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
836381fbed8Smatthias.ringwald 
837381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
838381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
839381fbed8Smatthias.ringwald         return;
840381fbed8Smatthias.ringwald     }
841381fbed8Smatthias.ringwald 
842381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
843381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
844381fbed8Smatthias.ringwald }
845381fbed8Smatthias.ringwald 
84606b35ec0Smatthias.ringwald void hci_run(){
8478a485f27Smatthias.ringwald 
84832ab9390Smatthias.ringwald     hci_connection_t * connection;
84932ab9390Smatthias.ringwald     linked_item_t * it;
85032ab9390Smatthias.ringwald 
85132ab9390Smatthias.ringwald     // send pending HCI commands
85232ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
85332ab9390Smatthias.ringwald 
85432ab9390Smatthias.ringwald         connection = (hci_connection_t *) it;
85532ab9390Smatthias.ringwald 
85653b86778Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) {
857*7b5fbe1fSmatthias.ringwald             // log_info("hci_run: cannot send command packet\n");
85853b86778Smatthias.ringwald             return;
85953b86778Smatthias.ringwald         }
86032ab9390Smatthias.ringwald 
86132ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
862*7b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
86332ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
86432ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
865c7e0c5f6Smatthias.ringwald         }
866c7e0c5f6Smatthias.ringwald 
867de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
86832ab9390Smatthias.ringwald 
86932ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
87032ab9390Smatthias.ringwald             link_key_t link_key;
871*7b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
87232ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
87332ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
87432ab9390Smatthias.ringwald             } else {
87532ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
87632ab9390Smatthias.ringwald             }
87732ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
87832ab9390Smatthias.ringwald         }
87932ab9390Smatthias.ringwald     }
88032ab9390Smatthias.ringwald 
881de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
882c7e0c5f6Smatthias.ringwald 
8833429f56bSmatthias.ringwald     switch (hci_stack.state){
8843429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
885*7b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
8863429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
8873429f56bSmatthias.ringwald                 // odd: waiting for command completion
88806b35ec0Smatthias.ringwald                 return;
8893429f56bSmatthias.ringwald             }
89090919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
891c7492964Smatthias.ringwald                 case 0: // RESET
89222909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
893c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
894c7492964Smatthias.ringwald                         // skip baud change
895c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
896c7492964Smatthias.ringwald                     }
8973429f56bSmatthias.ringwald                     break;
898c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
899c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
900c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
901c7492964Smatthias.ringwald                     break;
902c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
903c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
904c7492964Smatthias.ringwald                     hci_stack.substate += 2;
905c7492964Smatthias.ringwald                     // break missing here for fall through
906c7492964Smatthias.ringwald 
907c7492964Smatthias.ringwald                 case 3:
90890919203Smatthias.ringwald                     // custom initialization
90990919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
91090919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
91190919203Smatthias.ringwald                         if (cmd) {
91290919203Smatthias.ringwald                             int size = 3 + cmd[2];
913622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
914c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
91590919203Smatthias.ringwald                             break;
91690919203Smatthias.ringwald                         }
917c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
91890919203Smatthias.ringwald                     }
9192f6c30e1Smatthias.ringwald                     // otherwise continue
920f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
921f432a6ddSmatthias.ringwald 					break;
922c7492964Smatthias.ringwald 				case 4:
923e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
924e2edc0c3Smatthias.ringwald 					break;
925c7492964Smatthias.ringwald                 case 5:
9263429f56bSmatthias.ringwald                     // ca. 15 sec
9273429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9283429f56bSmatthias.ringwald                     break;
929c7492964Smatthias.ringwald 				case 6:
9300a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
931f432a6ddSmatthias.ringwald 					break;
932c7492964Smatthias.ringwald                 case 7:
9338a485f27Smatthias.ringwald #ifndef EMBEDDED
9348a485f27Smatthias.ringwald                 {
9358a485f27Smatthias.ringwald                     char hostname[30];
9368a485f27Smatthias.ringwald                     gethostname(hostname, 30);
9378a485f27Smatthias.ringwald                     hostname[29] = '\0';
9388a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
9398a485f27Smatthias.ringwald                     break;
9408a485f27Smatthias.ringwald                 }
941c7492964Smatthias.ringwald                 case 8:
9423c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
9433c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
9443c4d4b90Smatthias.ringwald                     break;
9453c4d4b90Smatthias.ringwald 
946c7492964Smatthias.ringwald                 case 9:
9473c4d4b90Smatthias.ringwald #endif
9488a485f27Smatthias.ringwald #endif
9493429f56bSmatthias.ringwald                     // done.
9503429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
951b360b6adSmatthias.ringwald                     hci_emit_state();
9523429f56bSmatthias.ringwald                     break;
9533429f56bSmatthias.ringwald                 default:
9543429f56bSmatthias.ringwald                     break;
955475c8125Smatthias.ringwald             }
9563429f56bSmatthias.ringwald             hci_stack.substate++;
9573429f56bSmatthias.ringwald             break;
958c7e0c5f6Smatthias.ringwald 
959c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
960c7e0c5f6Smatthias.ringwald 
961*7b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
962c7e0c5f6Smatthias.ringwald             // close all open connections
963c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
964c7e0c5f6Smatthias.ringwald             if (connection){
96532ab9390Smatthias.ringwald 
966c7e0c5f6Smatthias.ringwald                 // send disconnect
96732ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
96832ab9390Smatthias.ringwald 
969*7b5fbe1fSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
9706ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
971c7e0c5f6Smatthias.ringwald 
972c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
973c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
974c7e0c5f6Smatthias.ringwald                 return;
975c7e0c5f6Smatthias.ringwald             }
976*7b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
977c7e0c5f6Smatthias.ringwald 
97872ea5239Smatthias.ringwald             // switch mode
979c7e0c5f6Smatthias.ringwald             hci_power_control_off();
9809418f9c9Smatthias.ringwald 
981*7b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
98272ea5239Smatthias.ringwald             hci_emit_state();
983*7b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
98472ea5239Smatthias.ringwald             break;
985c7e0c5f6Smatthias.ringwald 
98672ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
98789db417bSmatthias.ringwald             switch(hci_stack.substate) {
98889db417bSmatthias.ringwald                 case 0:
989*7b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
99072ea5239Smatthias.ringwald                     // close all open connections
99172ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
99272ea5239Smatthias.ringwald                     if (connection){
99332ab9390Smatthias.ringwald 
99472ea5239Smatthias.ringwald                         // send disconnect
99532ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
99632ab9390Smatthias.ringwald 
997*7b5fbe1fSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
9986ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
99972ea5239Smatthias.ringwald 
100072ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
100172ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
100272ea5239Smatthias.ringwald                         return;
100372ea5239Smatthias.ringwald                     }
100472ea5239Smatthias.ringwald 
100589db417bSmatthias.ringwald                     // disable page and inquiry scan
100632ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
100732ab9390Smatthias.ringwald 
1008*7b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
100989db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
101089db417bSmatthias.ringwald 
101189db417bSmatthias.ringwald                     // continue in next sub state
101289db417bSmatthias.ringwald                     hci_stack.substate++;
101389db417bSmatthias.ringwald                     break;
101489db417bSmatthias.ringwald                 case 1:
101589db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
101689db417bSmatthias.ringwald                     break;
101789db417bSmatthias.ringwald                 case 2:
1018*7b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
101972ea5239Smatthias.ringwald                     // switch mode
102089db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1021c7e0c5f6Smatthias.ringwald                     hci_emit_state();
102289db417bSmatthias.ringwald                 default:
102389db417bSmatthias.ringwald                     break;
102489db417bSmatthias.ringwald             }
1025c7e0c5f6Smatthias.ringwald             break;
1026c7e0c5f6Smatthias.ringwald 
10273429f56bSmatthias.ringwald         default:
10283429f56bSmatthias.ringwald             break;
10291f504dbdSmatthias.ringwald     }
10303429f56bSmatthias.ringwald }
103116833f0aSmatthias.ringwald 
103231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1033c8e4258aSmatthias.ringwald     bd_addr_t addr;
1034c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1035c8e4258aSmatthias.ringwald     // house-keeping
1036c8e4258aSmatthias.ringwald 
1037c8e4258aSmatthias.ringwald     // create_connection?
1038c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1039c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1040*7b5fbe1fSmatthias.ringwald         log_info("Create_connection to "); print_bd_addr(addr); log_info("\n");
1041c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1042c8e4258aSmatthias.ringwald         if (conn) {
1043c8e4258aSmatthias.ringwald             // if connection exists
1044c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
1045c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
1046c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
1047c8e4258aSmatthias.ringwald             }
1048c8e4258aSmatthias.ringwald             //    otherwise, just ignore
1049c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1050c8e4258aSmatthias.ringwald 
1051c8e4258aSmatthias.ringwald         } else{
1052c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
1053c8e4258aSmatthias.ringwald             if (conn){
1054c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
1055c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
1056c8e4258aSmatthias.ringwald             }
1057c8e4258aSmatthias.ringwald         }
1058c8e4258aSmatthias.ringwald     }
1059c8e4258aSmatthias.ringwald 
10607fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
10617fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
10627fde4af9Smatthias.ringwald     }
10637fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
10647fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
10657fde4af9Smatthias.ringwald     }
10667fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
10677fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
10687fde4af9Smatthias.ringwald     }
10697fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
10707fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
10717fde4af9Smatthias.ringwald     }
10727fde4af9Smatthias.ringwald 
10738ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
10748ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
10758ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10768ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
10778ef73945Smatthias.ringwald         }
10788ef73945Smatthias.ringwald     }
1079c8e4258aSmatthias.ringwald 
108031452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1081622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
108231452debSmatthias.ringwald }
10838adf0ddaSmatthias.ringwald 
10841cd208adSmatthias.ringwald /**
10851cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
10861cd208adSmatthias.ringwald  */
1087fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
10881cd208adSmatthias.ringwald     va_list argptr;
10891cd208adSmatthias.ringwald     va_start(argptr, cmd);
10901cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
10911cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
10921cd208adSmatthias.ringwald     va_end(argptr);
10931cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
109493b8dc03Smatthias.ringwald }
1095c8e4258aSmatthias.ringwald 
1096ee091cf1Smatthias.ringwald // Create various non-HCI events.
1097ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1098ee091cf1Smatthias.ringwald 
1099c8e4258aSmatthias.ringwald void hci_emit_state(){
1100425d1371Smatthias.ringwald     uint8_t event[3];
110180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1102425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1103c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1104425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1105425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1106c8e4258aSmatthias.ringwald }
1107c8e4258aSmatthias.ringwald 
1108c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
1109425d1371Smatthias.ringwald     uint8_t event[13];
1110c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1111425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1112c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
1113c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1114c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1115c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1116c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1117425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1118425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1119c8e4258aSmatthias.ringwald }
1120c8e4258aSmatthias.ringwald 
11213c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1122425d1371Smatthias.ringwald     uint8_t event[6];
11233c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1124e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
11253c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
11263c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
11273c4d4b90Smatthias.ringwald     event[5] = reason;
1128425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1129425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11303c4d4b90Smatthias.ringwald }
11313c4d4b90Smatthias.ringwald 
1132ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1133425d1371Smatthias.ringwald     uint8_t event[4];
113480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1135425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1136ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1137425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1138425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1139ee091cf1Smatthias.ringwald }
114043bfb1bdSmatthias.ringwald 
114143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1142425d1371Smatthias.ringwald     uint8_t event[3];
114380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1144425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
114543bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1146425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1147425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
114843bfb1bdSmatthias.ringwald }
1149038bc64cSmatthias.ringwald 
1150038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1151425d1371Smatthias.ringwald     uint8_t event[2];
115280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1153425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1154425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1155425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1156038bc64cSmatthias.ringwald }
11571b0e3922Smatthias.ringwald 
11581b0e3922Smatthias.ringwald 
11591b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1160425d1371Smatthias.ringwald     uint8_t event[6];
11611b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1162425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1163425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1164425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1165425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1166425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1167425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11681b0e3922Smatthias.ringwald }
11691b0e3922Smatthias.ringwald 
11702ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1171425d1371Smatthias.ringwald     uint8_t event[3];
11722ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1173425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
11742ed6235cSmatthias.ringwald     event[2] = enabled;
1175425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1176e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11772ed6235cSmatthias.ringwald }
1178627c2f45Smatthias.ringwald 
1179627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1180425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1181627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1182425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1183f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
11842f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1185f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1186425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1187425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1188627c2f45Smatthias.ringwald }
1189381fbed8Smatthias.ringwald 
1190381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1191425d1371Smatthias.ringwald     uint8_t event[3];
1192381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1193425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1194381fbed8Smatthias.ringwald     event[2] = enabled;
1195425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1196425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1197381fbed8Smatthias.ringwald }
1198