xref: /btstack/src/hci.c (revision 7d3b3569b0d2f527a74b4e938ee5bf9bef66b821)
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;
111*7d3b3569Smatthias.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++;
2438ea03fa5Smatthias.ringwald     // log_dbg("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){
335c7e0c5f6Smatthias.ringwald     log_dbg("Connection closed: handle %u, ", conn->con_handle);
336c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
337c7e0c5f6Smatthias.ringwald     log_dbg("\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;
356fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3571f7b95a1Smatthias.ringwald     hci_connection_t * conn;
35856cf178bSmatthias.ringwald     int i;
35922909952Smatthias.ringwald 
3606772a24cSmatthias.ringwald     switch (packet[0]) {
36122909952Smatthias.ringwald 
3626772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3637ec5eeaaSmatthias.ringwald             // get num cmd packets
3647ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3657ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3667ec5eeaaSmatthias.ringwald 
367e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
368e2edc0c3Smatthias.ringwald                 // from offset 5
369e2edc0c3Smatthias.ringwald                 // status
3701d279b20Smatthias.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"
371e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
37256cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
373e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
37456cf178bSmatthias.ringwald                 // ignore: total num SCO packets
37556cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
376e2edc0c3Smatthias.ringwald                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
377e2edc0c3Smatthias.ringwald                 }
37856cf178bSmatthias.ringwald             }
379381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
380381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
381381fbed8Smatthias.ringwald             }
38256cf178bSmatthias.ringwald             break;
38356cf178bSmatthias.ringwald 
3847ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
3857ec5eeaaSmatthias.ringwald             // get num cmd packets
3867ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
3877ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
3887ec5eeaaSmatthias.ringwald             break;
3897ec5eeaaSmatthias.ringwald 
39056cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
39156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
39256cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
39356cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
39456cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
39556cf178bSmatthias.ringwald                 if (!conn){
39656cf178bSmatthias.ringwald                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
39756cf178bSmatthias.ringwald                     continue;
39856cf178bSmatthias.ringwald                 }
39956cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4008ea03fa5Smatthias.ringwald                 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
40156cf178bSmatthias.ringwald             }
4026772a24cSmatthias.ringwald             break;
4036772a24cSmatthias.ringwald 
4041f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
40537eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
40637eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
40737eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
408cf0b66f0Smatthias.ringwald             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
40937eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4101f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4111f7b95a1Smatthias.ringwald                 if (!conn) {
4121f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4131f7b95a1Smatthias.ringwald                 }
4141f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
41532ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
41632ab9390Smatthias.ringwald                 hci_run();
41737eaa4cfSmatthias.ringwald             } else {
41837eaa4cfSmatthias.ringwald                 // TODO: decline request
41937eaa4cfSmatthias.ringwald             }
4201f7b95a1Smatthias.ringwald             break;
4211f7b95a1Smatthias.ringwald 
4226772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
423fe1ed1b8Smatthias.ringwald             // Connection management
424fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
425cf0b66f0Smatthias.ringwald             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
4261f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
427fe1ed1b8Smatthias.ringwald             if (conn) {
428b448a0e7Smatthias.ringwald                 if (!packet[2]){
429c8e4258aSmatthias.ringwald                     conn->state = OPEN;
430fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
431ee091cf1Smatthias.ringwald 
432c7492964Smatthias.ringwald #ifdef HAVE_TIME
433ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
434c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
435ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
436c7492964Smatthias.ringwald #endif
437cf0b66f0Smatthias.ringwald                     log_dbg("New connection: handle %u, ", conn->con_handle);
438fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
439cf0b66f0Smatthias.ringwald                     log_dbg("\n");
44043bfb1bdSmatthias.ringwald 
44143bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
442b448a0e7Smatthias.ringwald                 } else {
443b448a0e7Smatthias.ringwald                     // connection failed, remove entry
444b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
445b448a0e7Smatthias.ringwald                     free( conn );
446c12e46e7Smatthias.ringwald 
447c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
448c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
449c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
450c12e46e7Smatthias.ringwald                     }
451fe1ed1b8Smatthias.ringwald                 }
452fe1ed1b8Smatthias.ringwald             }
4536772a24cSmatthias.ringwald             break;
454fe1ed1b8Smatthias.ringwald 
4557fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
45664472d52Smatthias.ringwald             log_dbg("HCI_EVENT_LINK_KEY_REQUEST\n");
4577fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
45874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
45932ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
46064472d52Smatthias.ringwald             hci_run();
46129d53098Smatthias.ringwald             // request already answered
46229d53098Smatthias.ringwald             return;
4637fde4af9Smatthias.ringwald 
4647fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4657fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
46674ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
46729d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
468287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
46929d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4707fde4af9Smatthias.ringwald             break;
4717fde4af9Smatthias.ringwald 
4727fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4737fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4747fde4af9Smatthias.ringwald             break;
4757fde4af9Smatthias.ringwald 
47674ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
47774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
47874ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
47974ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
4805a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
4815a06394aSmatthias.ringwald             for (i=0; i<248;i++){
4825a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
4835a06394aSmatthias.ringwald                     packet[9+i] = 0;
4845a06394aSmatthias.ringwald                     break;
485cdc9101dSmatthias.ringwald                 }
4865a06394aSmatthias.ringwald             }
48774ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
48874ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
48974ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
49074ec757aSmatthias.ringwald             break;
49174ec757aSmatthias.ringwald 
49274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
49374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
49474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
49574ec757aSmatthias.ringwald             // first send inq result packet
49674ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
49774ec757aSmatthias.ringwald             // then send cached remote names
49874ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
49974ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
50074ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
50174ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
50274ec757aSmatthias.ringwald                 }
50374ec757aSmatthias.ringwald             }
50474ec757aSmatthias.ringwald             return;
50574ec757aSmatthias.ringwald 
5066772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
507fe1ed1b8Smatthias.ringwald             if (!packet[2]){
508fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
509fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
510fe1ed1b8Smatthias.ringwald                 if (conn) {
511c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
512fe1ed1b8Smatthias.ringwald                 }
513fe1ed1b8Smatthias.ringwald             }
5146772a24cSmatthias.ringwald             break;
5156772a24cSmatthias.ringwald 
516c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
517c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
518c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
519c68bdf90Smatthias.ringwald             }
520c68bdf90Smatthias.ringwald             break;
521c68bdf90Smatthias.ringwald 
5226772a24cSmatthias.ringwald         default:
5236772a24cSmatthias.ringwald             break;
524fe1ed1b8Smatthias.ringwald     }
525fe1ed1b8Smatthias.ringwald 
5263429f56bSmatthias.ringwald     // handle BT initialization
5273429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5287301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5297301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5307301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5317301ad89Smatthias.ringwald         // }
5327301ad89Smatthias.ringwald         // handle normal init sequence
5333429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5343429f56bSmatthias.ringwald             // odd: waiting for event
5353429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5363429f56bSmatthias.ringwald                 hci_stack.substate++;
5373429f56bSmatthias.ringwald             }
5383429f56bSmatthias.ringwald         }
53922909952Smatthias.ringwald     }
54022909952Smatthias.ringwald 
54189db417bSmatthias.ringwald     // help with BT sleep
54289db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
54389db417bSmatthias.ringwald         && hci_stack.substate == 1
54489db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
54589db417bSmatthias.ringwald         hci_stack.substate++;
54689db417bSmatthias.ringwald     }
54789db417bSmatthias.ringwald 
5482718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
54994ab26f8Smatthias.ringwald 
55094ab26f8Smatthias.ringwald 	// execute main loop
55194ab26f8Smatthias.ringwald 	hci_run();
55216833f0aSmatthias.ringwald }
55316833f0aSmatthias.ringwald 
55410e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
55510e830c9Smatthias.ringwald     switch (packet_type) {
55610e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
55710e830c9Smatthias.ringwald             event_handler(packet, size);
55810e830c9Smatthias.ringwald             break;
55910e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
56010e830c9Smatthias.ringwald             acl_handler(packet, size);
56110e830c9Smatthias.ringwald             break;
56210e830c9Smatthias.ringwald         default:
56310e830c9Smatthias.ringwald             break;
56410e830c9Smatthias.ringwald     }
56510e830c9Smatthias.ringwald }
56610e830c9Smatthias.ringwald 
567fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5682718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5692718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
57016833f0aSmatthias.ringwald }
57116833f0aSmatthias.ringwald 
572404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
573475c8125Smatthias.ringwald 
574475c8125Smatthias.ringwald     // reference to use transport layer implementation
57516833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
576475c8125Smatthias.ringwald 
57711e23e5fSmatthias.ringwald     // references to used control implementation
57811e23e5fSmatthias.ringwald     hci_stack.control = control;
57911e23e5fSmatthias.ringwald 
58011e23e5fSmatthias.ringwald     // reference to used config
58111e23e5fSmatthias.ringwald     hci_stack.config = config;
58211e23e5fSmatthias.ringwald 
583fe1ed1b8Smatthias.ringwald     // no connections yet
584fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
5850a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
586fe1ed1b8Smatthias.ringwald 
58702ea9861Smatthias.ringwald     // empty cmd buffer
58816833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
58916833f0aSmatthias.ringwald 
59016833f0aSmatthias.ringwald     // higher level handler
5912718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
59216833f0aSmatthias.ringwald 
593404843c1Smatthias.ringwald     // store and open remote device db
594404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
595404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
596404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
597404843c1Smatthias.ringwald     }
59829d53098Smatthias.ringwald 
59916833f0aSmatthias.ringwald     // register packet handlers with transport
60010e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
601475c8125Smatthias.ringwald }
602475c8125Smatthias.ringwald 
603404843c1Smatthias.ringwald void hci_close(){
604404843c1Smatthias.ringwald     // close remote device db
605404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
606404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
607404843c1Smatthias.ringwald     }
608404843c1Smatthias.ringwald }
609404843c1Smatthias.ringwald 
6108d213e1aSmatthias.ringwald // State-Module-Driver overview
6118d213e1aSmatthias.ringwald // state                    module  low-level
6128d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6138d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6148d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6158d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
616d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
617d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
618c7e0c5f6Smatthias.ringwald 
61940d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6207301ad89Smatthias.ringwald 
621038bc64cSmatthias.ringwald     // power on
622f9a30166Smatthias.ringwald     int err = 0;
623f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
624f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
625f9a30166Smatthias.ringwald     }
626038bc64cSmatthias.ringwald     if (err){
6277f2435e6Smatthias.ringwald         log_err( "POWER_ON failed\n");
628038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
629038bc64cSmatthias.ringwald         return err;
630038bc64cSmatthias.ringwald     }
631038bc64cSmatthias.ringwald 
632038bc64cSmatthias.ringwald     // open low-level device
633038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
634038bc64cSmatthias.ringwald     if (err){
6357f2435e6Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
636f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
637f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
638f9a30166Smatthias.ringwald         }
639038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
640038bc64cSmatthias.ringwald         return err;
641038bc64cSmatthias.ringwald     }
6428d213e1aSmatthias.ringwald     return 0;
6438d213e1aSmatthias.ringwald }
644038bc64cSmatthias.ringwald 
64540d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6468d213e1aSmatthias.ringwald 
6479418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off\n");
6489418f9c9Smatthias.ringwald 
6498d213e1aSmatthias.ringwald     // close low-level device
6508d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6518d213e1aSmatthias.ringwald 
6529418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off - hci_transport closed\n");
6539418f9c9Smatthias.ringwald 
6548d213e1aSmatthias.ringwald     // power off
6558d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6568d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6578d213e1aSmatthias.ringwald     }
6589418f9c9Smatthias.ringwald 
6599418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off - control closed\n");
6609418f9c9Smatthias.ringwald 
66172ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
66272ea5239Smatthias.ringwald }
66372ea5239Smatthias.ringwald 
66440d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
66572ea5239Smatthias.ringwald 
6669418f9c9Smatthias.ringwald     log_dbg("hci_power_control_sleep\n");
6673144bce4Smatthias.ringwald 
668b429b9b7Smatthias.ringwald #if 0
669b429b9b7Smatthias.ringwald     // don't close serial port during sleep
670b429b9b7Smatthias.ringwald 
67172ea5239Smatthias.ringwald     // close low-level device
67272ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
673b429b9b7Smatthias.ringwald #endif
67472ea5239Smatthias.ringwald 
67572ea5239Smatthias.ringwald     // sleep mode
6763144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
67772ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
67872ea5239Smatthias.ringwald     }
679b429b9b7Smatthias.ringwald 
68072ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6818d213e1aSmatthias.ringwald }
6828d213e1aSmatthias.ringwald 
68340d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
684b429b9b7Smatthias.ringwald 
6859418f9c9Smatthias.ringwald     log_dbg("hci_power_control_wake\n");
686b429b9b7Smatthias.ringwald 
687b429b9b7Smatthias.ringwald     // wake on
688b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
689b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
690b429b9b7Smatthias.ringwald     }
691b429b9b7Smatthias.ringwald 
692b429b9b7Smatthias.ringwald #if 0
693b429b9b7Smatthias.ringwald     // open low-level device
694b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
695b429b9b7Smatthias.ringwald     if (err){
696b429b9b7Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
697b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
698b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
699b429b9b7Smatthias.ringwald         }
700b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
701b429b9b7Smatthias.ringwald         return err;
702b429b9b7Smatthias.ringwald     }
703b429b9b7Smatthias.ringwald #endif
704b429b9b7Smatthias.ringwald 
705b429b9b7Smatthias.ringwald     return 0;
706b429b9b7Smatthias.ringwald }
707b429b9b7Smatthias.ringwald 
708b429b9b7Smatthias.ringwald 
7098d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
710d661ed19Smatthias.ringwald 
7117ec5eeaaSmatthias.ringwald     log_dbg("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
712d661ed19Smatthias.ringwald 
7138d213e1aSmatthias.ringwald     int err = 0;
7148d213e1aSmatthias.ringwald     switch (hci_stack.state){
7158d213e1aSmatthias.ringwald 
7168d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7178d213e1aSmatthias.ringwald             switch (power_mode){
7188d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7198d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7208d213e1aSmatthias.ringwald                     if (err) return err;
7217301ad89Smatthias.ringwald                     // set up state machine
7227301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7237301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7247301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7258d213e1aSmatthias.ringwald                     break;
7268d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7278d213e1aSmatthias.ringwald                     // do nothing
7288d213e1aSmatthias.ringwald                     break;
7298d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
730b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7318d213e1aSmatthias.ringwald                     break;
7328d213e1aSmatthias.ringwald             }
7338d213e1aSmatthias.ringwald             break;
7347301ad89Smatthias.ringwald 
7358d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7368d213e1aSmatthias.ringwald             switch (power_mode){
7378d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7388d213e1aSmatthias.ringwald                     // do nothing
7398d213e1aSmatthias.ringwald                     break;
7408d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7418d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7428d213e1aSmatthias.ringwald                     hci_power_control_off();
7438d213e1aSmatthias.ringwald                     break;
7448d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
745b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
74672ea5239Smatthias.ringwald                     hci_power_control_sleep();
7478d213e1aSmatthias.ringwald                     break;
7488d213e1aSmatthias.ringwald             }
7498d213e1aSmatthias.ringwald             break;
7507301ad89Smatthias.ringwald 
7518d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7528d213e1aSmatthias.ringwald             switch (power_mode){
7538d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7548d213e1aSmatthias.ringwald                     // do nothing
7558d213e1aSmatthias.ringwald                     break;
7568d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
757c7e0c5f6Smatthias.ringwald                     // see hci_run
758c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7598d213e1aSmatthias.ringwald                     break;
7608d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
761b546ac54Smatthias.ringwald                     // see hci_run
762b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
76389db417bSmatthias.ringwald                     hci_stack.substate = 0;
7648d213e1aSmatthias.ringwald                     break;
7658d213e1aSmatthias.ringwald             }
7668d213e1aSmatthias.ringwald             break;
7677301ad89Smatthias.ringwald 
7688d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7698d213e1aSmatthias.ringwald             switch (power_mode){
7708d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7718d213e1aSmatthias.ringwald                     // set up state machine
7728d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7738d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7748d213e1aSmatthias.ringwald                     break;
7758d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7768d213e1aSmatthias.ringwald                     // do nothing
7778d213e1aSmatthias.ringwald                     break;
7788d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
779b546ac54Smatthias.ringwald                     // see hci_run
780b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
78189db417bSmatthias.ringwald                     hci_stack.substate = 0;
7828d213e1aSmatthias.ringwald                     break;
7838d213e1aSmatthias.ringwald             }
7848d213e1aSmatthias.ringwald             break;
7858d213e1aSmatthias.ringwald 
7868d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7878d213e1aSmatthias.ringwald             switch (power_mode){
7888d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
789b546ac54Smatthias.ringwald                     // set up state machine
790b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
791b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
792b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7938d213e1aSmatthias.ringwald                     break;
7948d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
795b546ac54Smatthias.ringwald                     // see hci_run
796b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7978d213e1aSmatthias.ringwald                     break;
7988d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
799b546ac54Smatthias.ringwald                     // do nothing
8008d213e1aSmatthias.ringwald                     break;
8018d213e1aSmatthias.ringwald             }
8028d213e1aSmatthias.ringwald             break;
8038d213e1aSmatthias.ringwald 
8048d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
8058d213e1aSmatthias.ringwald             switch (power_mode){
8068d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8073144bce4Smatthias.ringwald                     err = hci_power_control_wake();
8083144bce4Smatthias.ringwald                     if (err) return err;
809b546ac54Smatthias.ringwald                     // set up state machine
810b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
811b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
812b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8138d213e1aSmatthias.ringwald                     break;
8148d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8154ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8168d213e1aSmatthias.ringwald                     break;
8178d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
818b546ac54Smatthias.ringwald                     // do nothing
8198d213e1aSmatthias.ringwald                     break;
8208d213e1aSmatthias.ringwald             }
8218d213e1aSmatthias.ringwald             break;
82211e23e5fSmatthias.ringwald     }
82368d92d03Smatthias.ringwald 
824038bc64cSmatthias.ringwald     // create internal event
825ee8bf225Smatthias.ringwald 	hci_emit_state();
826ee8bf225Smatthias.ringwald 
82768d92d03Smatthias.ringwald 	// trigger next/first action
82868d92d03Smatthias.ringwald 	hci_run();
82968d92d03Smatthias.ringwald 
830475c8125Smatthias.ringwald     return 0;
831475c8125Smatthias.ringwald }
832475c8125Smatthias.ringwald 
833381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
834381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
835381fbed8Smatthias.ringwald 
836381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
837381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
838381fbed8Smatthias.ringwald         return;
839381fbed8Smatthias.ringwald     }
840381fbed8Smatthias.ringwald 
841381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
842381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
843381fbed8Smatthias.ringwald }
844381fbed8Smatthias.ringwald 
84506b35ec0Smatthias.ringwald void hci_run(){
8468a485f27Smatthias.ringwald 
84732ab9390Smatthias.ringwald     hci_connection_t * connection;
84832ab9390Smatthias.ringwald     linked_item_t * it;
84932ab9390Smatthias.ringwald 
85032ab9390Smatthias.ringwald     // send pending HCI commands
85132ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
85232ab9390Smatthias.ringwald 
85332ab9390Smatthias.ringwald         connection = (hci_connection_t *) it;
85432ab9390Smatthias.ringwald 
85553b86778Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) {
856907dc1a9Smatthias.ringwald             // log_dbg("hci_run: cannot send command packet\n");
85753b86778Smatthias.ringwald             return;
85853b86778Smatthias.ringwald         }
85932ab9390Smatthias.ringwald 
86032ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
86164472d52Smatthias.ringwald             log_dbg("sending hci_accept_connection_request\n");
86232ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
86332ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
864c7e0c5f6Smatthias.ringwald         }
865c7e0c5f6Smatthias.ringwald 
866de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
86732ab9390Smatthias.ringwald 
86832ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
86932ab9390Smatthias.ringwald             link_key_t link_key;
87064472d52Smatthias.ringwald             log_dbg("responding to link key request\n");
87132ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
87232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
87332ab9390Smatthias.ringwald             } else {
87432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
87532ab9390Smatthias.ringwald             }
87632ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
87732ab9390Smatthias.ringwald         }
87832ab9390Smatthias.ringwald     }
87932ab9390Smatthias.ringwald 
880de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
881c7e0c5f6Smatthias.ringwald 
8823429f56bSmatthias.ringwald     switch (hci_stack.state){
8833429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
88453b86778Smatthias.ringwald             // log_dbg("hci_init: substate %u\n", hci_stack.substate);
8853429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
8863429f56bSmatthias.ringwald                 // odd: waiting for command completion
88706b35ec0Smatthias.ringwald                 return;
8883429f56bSmatthias.ringwald             }
88990919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
890c7492964Smatthias.ringwald                 case 0: // RESET
89122909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
892c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
893c7492964Smatthias.ringwald                         // skip baud change
894c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
895c7492964Smatthias.ringwald                     }
8963429f56bSmatthias.ringwald                     break;
897c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
898c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
899c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
900c7492964Smatthias.ringwald                     break;
901c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
902c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
903c7492964Smatthias.ringwald                     hci_stack.substate += 2;
904c7492964Smatthias.ringwald                     // break missing here for fall through
905c7492964Smatthias.ringwald 
906c7492964Smatthias.ringwald                 case 3:
90790919203Smatthias.ringwald                     // custom initialization
90890919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
90990919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
91090919203Smatthias.ringwald                         if (cmd) {
91190919203Smatthias.ringwald                             int size = 3 + cmd[2];
912622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
913c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
91490919203Smatthias.ringwald                             break;
91590919203Smatthias.ringwald                         }
916c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
91790919203Smatthias.ringwald                     }
9182f6c30e1Smatthias.ringwald                     // otherwise continue
919f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
920f432a6ddSmatthias.ringwald 					break;
921c7492964Smatthias.ringwald 				case 4:
922e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
923e2edc0c3Smatthias.ringwald 					break;
924c7492964Smatthias.ringwald                 case 5:
9253429f56bSmatthias.ringwald                     // ca. 15 sec
9263429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9273429f56bSmatthias.ringwald                     break;
928c7492964Smatthias.ringwald 				case 6:
9290a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
930f432a6ddSmatthias.ringwald 					break;
931c7492964Smatthias.ringwald                 case 7:
9328a485f27Smatthias.ringwald #ifndef EMBEDDED
9338a485f27Smatthias.ringwald                 {
9348a485f27Smatthias.ringwald                     char hostname[30];
9358a485f27Smatthias.ringwald                     gethostname(hostname, 30);
9368a485f27Smatthias.ringwald                     hostname[29] = '\0';
9378a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
9388a485f27Smatthias.ringwald                     break;
9398a485f27Smatthias.ringwald                 }
940c7492964Smatthias.ringwald                 case 8:
9413c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
9423c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
9433c4d4b90Smatthias.ringwald                     break;
9443c4d4b90Smatthias.ringwald 
945c7492964Smatthias.ringwald                 case 9:
9463c4d4b90Smatthias.ringwald #endif
9478a485f27Smatthias.ringwald #endif
9483429f56bSmatthias.ringwald                     // done.
9493429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
950b360b6adSmatthias.ringwald                     hci_emit_state();
9513429f56bSmatthias.ringwald                     break;
9523429f56bSmatthias.ringwald                 default:
9533429f56bSmatthias.ringwald                     break;
954475c8125Smatthias.ringwald             }
9553429f56bSmatthias.ringwald             hci_stack.substate++;
9563429f56bSmatthias.ringwald             break;
957c7e0c5f6Smatthias.ringwald 
958c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
959c7e0c5f6Smatthias.ringwald 
9603144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING\n");
961c7e0c5f6Smatthias.ringwald             // close all open connections
962c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
963c7e0c5f6Smatthias.ringwald             if (connection){
96432ab9390Smatthias.ringwald 
965c7e0c5f6Smatthias.ringwald                 // send disconnect
96632ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
96732ab9390Smatthias.ringwald 
96832ab9390Smatthias.ringwald                 log_dbg("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle);
9696ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
970c7e0c5f6Smatthias.ringwald 
971c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
972c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
973c7e0c5f6Smatthias.ringwald                 return;
974c7e0c5f6Smatthias.ringwald             }
9753144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, calling off\n");
976c7e0c5f6Smatthias.ringwald 
97772ea5239Smatthias.ringwald             // switch mode
978c7e0c5f6Smatthias.ringwald             hci_power_control_off();
9799418f9c9Smatthias.ringwald 
9809418f9c9Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, emitting state\n");
98172ea5239Smatthias.ringwald             hci_emit_state();
9823144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, done\n");
98372ea5239Smatthias.ringwald             break;
984c7e0c5f6Smatthias.ringwald 
98572ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
98689db417bSmatthias.ringwald             switch(hci_stack.substate) {
98789db417bSmatthias.ringwald                 case 0:
98889db417bSmatthias.ringwald                     log_dbg("HCI_STATE_FALLING_ASLEEP\n");
98972ea5239Smatthias.ringwald                     // close all open connections
99072ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
99172ea5239Smatthias.ringwald                     if (connection){
99232ab9390Smatthias.ringwald 
99372ea5239Smatthias.ringwald                         // send disconnect
99432ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
99532ab9390Smatthias.ringwald 
99632ab9390Smatthias.ringwald                         log_dbg("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle);
9976ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
99872ea5239Smatthias.ringwald 
99972ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
100072ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
100172ea5239Smatthias.ringwald                         return;
100272ea5239Smatthias.ringwald                     }
100372ea5239Smatthias.ringwald 
100489db417bSmatthias.ringwald                     // disable page and inquiry scan
100532ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
100632ab9390Smatthias.ringwald 
100732ab9390Smatthias.ringwald                     log_dbg("HCI_STATE_HALTING, disabling inq & page scans\n");
100889db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
100989db417bSmatthias.ringwald 
101089db417bSmatthias.ringwald                     // continue in next sub state
101189db417bSmatthias.ringwald                     hci_stack.substate++;
101289db417bSmatthias.ringwald                     break;
101389db417bSmatthias.ringwald                 case 1:
101489db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
101589db417bSmatthias.ringwald                     break;
101689db417bSmatthias.ringwald                 case 2:
101789db417bSmatthias.ringwald                     log_dbg("HCI_STATE_HALTING, calling sleep\n");
101872ea5239Smatthias.ringwald                     // switch mode
101989db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1020c7e0c5f6Smatthias.ringwald                     hci_emit_state();
102189db417bSmatthias.ringwald                 default:
102289db417bSmatthias.ringwald                     break;
102389db417bSmatthias.ringwald             }
1024c7e0c5f6Smatthias.ringwald             break;
1025c7e0c5f6Smatthias.ringwald 
10263429f56bSmatthias.ringwald         default:
10273429f56bSmatthias.ringwald             break;
10281f504dbdSmatthias.ringwald     }
10293429f56bSmatthias.ringwald }
103016833f0aSmatthias.ringwald 
103131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1032c8e4258aSmatthias.ringwald     bd_addr_t addr;
1033c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1034c8e4258aSmatthias.ringwald     // house-keeping
1035c8e4258aSmatthias.ringwald 
1036c8e4258aSmatthias.ringwald     // create_connection?
1037c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1038c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1039cf0b66f0Smatthias.ringwald         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
1040c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1041c8e4258aSmatthias.ringwald         if (conn) {
1042c8e4258aSmatthias.ringwald             // if connection exists
1043c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
1044c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
1045c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
1046c8e4258aSmatthias.ringwald             }
1047c8e4258aSmatthias.ringwald             //    otherwise, just ignore
1048c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1049c8e4258aSmatthias.ringwald 
1050c8e4258aSmatthias.ringwald         } else{
1051c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
1052c8e4258aSmatthias.ringwald             if (conn){
1053c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
1054c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
1055c8e4258aSmatthias.ringwald             }
1056c8e4258aSmatthias.ringwald         }
1057c8e4258aSmatthias.ringwald     }
1058c8e4258aSmatthias.ringwald 
10597fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
10607fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
10617fde4af9Smatthias.ringwald     }
10627fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
10637fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
10647fde4af9Smatthias.ringwald     }
10657fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
10667fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
10677fde4af9Smatthias.ringwald     }
10687fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
10697fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
10707fde4af9Smatthias.ringwald     }
10717fde4af9Smatthias.ringwald 
10728ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
10738ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
10748ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10758ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
10768ef73945Smatthias.ringwald         }
10778ef73945Smatthias.ringwald     }
1078c8e4258aSmatthias.ringwald 
107931452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1080622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
108131452debSmatthias.ringwald }
10828adf0ddaSmatthias.ringwald 
10831cd208adSmatthias.ringwald /**
10841cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
10851cd208adSmatthias.ringwald  */
1086fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
10871cd208adSmatthias.ringwald     va_list argptr;
10881cd208adSmatthias.ringwald     va_start(argptr, cmd);
10891cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
10901cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
10911cd208adSmatthias.ringwald     va_end(argptr);
10921cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
109393b8dc03Smatthias.ringwald }
1094c8e4258aSmatthias.ringwald 
1095ee091cf1Smatthias.ringwald // Create various non-HCI events.
1096ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1097ee091cf1Smatthias.ringwald 
1098c8e4258aSmatthias.ringwald void hci_emit_state(){
1099425d1371Smatthias.ringwald     uint8_t event[3];
110080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1101425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1102c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1103425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1104425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1105c8e4258aSmatthias.ringwald }
1106c8e4258aSmatthias.ringwald 
1107c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
1108425d1371Smatthias.ringwald     uint8_t event[13];
1109c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1110425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1111c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
1112c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1113c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1114c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1115c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1116425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1117425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1118c8e4258aSmatthias.ringwald }
1119c8e4258aSmatthias.ringwald 
11203c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1121425d1371Smatthias.ringwald     uint8_t event[6];
11223c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1123e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
11243c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
11253c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
11263c4d4b90Smatthias.ringwald     event[5] = reason;
1127425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1128425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11293c4d4b90Smatthias.ringwald }
11303c4d4b90Smatthias.ringwald 
1131ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1132425d1371Smatthias.ringwald     uint8_t event[4];
113380d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1134425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1135ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1136425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1137425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1138ee091cf1Smatthias.ringwald }
113943bfb1bdSmatthias.ringwald 
114043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1141425d1371Smatthias.ringwald     uint8_t event[3];
114280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1143425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
114443bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1145425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1146425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
114743bfb1bdSmatthias.ringwald }
1148038bc64cSmatthias.ringwald 
1149038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1150425d1371Smatthias.ringwald     uint8_t event[2];
115180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1152425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1153425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1154425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1155038bc64cSmatthias.ringwald }
11561b0e3922Smatthias.ringwald 
11571b0e3922Smatthias.ringwald 
11581b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1159425d1371Smatthias.ringwald     uint8_t event[6];
11601b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1161425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1162425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1163425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1164425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1165425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1166425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11671b0e3922Smatthias.ringwald }
11681b0e3922Smatthias.ringwald 
11692ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1170425d1371Smatthias.ringwald     uint8_t event[3];
11712ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1172425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
11732ed6235cSmatthias.ringwald     event[2] = enabled;
1174425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1175e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11762ed6235cSmatthias.ringwald }
1177627c2f45Smatthias.ringwald 
1178627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1179425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1180627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1181425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1182f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
11832f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1184f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1185425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1186425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1187627c2f45Smatthias.ringwald }
1188381fbed8Smatthias.ringwald 
1189381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1190425d1371Smatthias.ringwald     uint8_t event[3];
1191381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1192425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1193381fbed8Smatthias.ringwald     event[2] = enabled;
1194425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1195425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1196381fbed8Smatthias.ringwald }
1197