xref: /btstack/src/hci.c (revision 124062bc8d22b8c088b66a6a98854012b4525569)
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) {
1877d67539fSmatthias.ringwald         log_error("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) {
1997d67539fSmatthias.ringwald             log_error("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++;
2437b5fbe1fSmatthias.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){
2617d67539fSmatthias.ringwald         log_error( "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) {
2757d67539fSmatthias.ringwald                 log_error( "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 
2837d67539fSmatthias.ringwald             // log_error( "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) {
3007d67539fSmatthias.ringwald                 log_error( "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 
3077d67539fSmatthias.ringwald             // log_error( "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:
3267d67539fSmatthias.ringwald             log_error( "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){
3357b5fbe1fSmatthias.ringwald     log_info("Connection closed: handle %u, ", conn->con_handle);
336c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
3377b5fbe1fSmatthias.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
3657b5fbe1fSmatthias.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){
3777b5fbe1fSmatthias.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
3877b5fbe1fSmatthias.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){
3977d67539fSmatthias.ringwald                     log_error("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;
4017b5fbe1fSmatthias.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];
4097b5fbe1fSmatthias.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]);
4267b5fbe1fSmatthias.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
4387b5fbe1fSmatthias.ringwald                     log_info("New connection: handle %u, ", conn->con_handle);
439fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
4407b5fbe1fSmatthias.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:
4577b5fbe1fSmatthias.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 
58816833f0aSmatthias.ringwald     // higher level handler
5892718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
59016833f0aSmatthias.ringwald 
591404843c1Smatthias.ringwald     // store and open remote device db
592404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
593404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
594404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
595404843c1Smatthias.ringwald     }
59629d53098Smatthias.ringwald 
59716833f0aSmatthias.ringwald     // register packet handlers with transport
59810e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
599475c8125Smatthias.ringwald }
600475c8125Smatthias.ringwald 
601404843c1Smatthias.ringwald void hci_close(){
602404843c1Smatthias.ringwald     // close remote device db
603404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
604404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
605404843c1Smatthias.ringwald     }
606404843c1Smatthias.ringwald }
607404843c1Smatthias.ringwald 
6088d213e1aSmatthias.ringwald // State-Module-Driver overview
6098d213e1aSmatthias.ringwald // state                    module  low-level
6108d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6118d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6128d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6138d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
614d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
615d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
616c7e0c5f6Smatthias.ringwald 
61740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6187301ad89Smatthias.ringwald 
619038bc64cSmatthias.ringwald     // power on
620f9a30166Smatthias.ringwald     int err = 0;
621f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
622f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
623f9a30166Smatthias.ringwald     }
624038bc64cSmatthias.ringwald     if (err){
6257d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
626038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
627038bc64cSmatthias.ringwald         return err;
628038bc64cSmatthias.ringwald     }
629038bc64cSmatthias.ringwald 
630038bc64cSmatthias.ringwald     // open low-level device
631038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
632038bc64cSmatthias.ringwald     if (err){
6337d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
634f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
635f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
636f9a30166Smatthias.ringwald         }
637038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
638038bc64cSmatthias.ringwald         return err;
639038bc64cSmatthias.ringwald     }
6408d213e1aSmatthias.ringwald     return 0;
6418d213e1aSmatthias.ringwald }
642038bc64cSmatthias.ringwald 
64340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6448d213e1aSmatthias.ringwald 
6457b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
6469418f9c9Smatthias.ringwald 
6478d213e1aSmatthias.ringwald     // close low-level device
6488d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6498d213e1aSmatthias.ringwald 
6507b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
6519418f9c9Smatthias.ringwald 
6528d213e1aSmatthias.ringwald     // power off
6538d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6548d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6558d213e1aSmatthias.ringwald     }
6569418f9c9Smatthias.ringwald 
6577b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
6589418f9c9Smatthias.ringwald 
65972ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
66072ea5239Smatthias.ringwald }
66172ea5239Smatthias.ringwald 
66240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
66372ea5239Smatthias.ringwald 
6647b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
6653144bce4Smatthias.ringwald 
666b429b9b7Smatthias.ringwald #if 0
667b429b9b7Smatthias.ringwald     // don't close serial port during sleep
668b429b9b7Smatthias.ringwald 
66972ea5239Smatthias.ringwald     // close low-level device
67072ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
671b429b9b7Smatthias.ringwald #endif
67272ea5239Smatthias.ringwald 
67372ea5239Smatthias.ringwald     // sleep mode
6743144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
67572ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
67672ea5239Smatthias.ringwald     }
677b429b9b7Smatthias.ringwald 
67872ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6798d213e1aSmatthias.ringwald }
6808d213e1aSmatthias.ringwald 
68140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
682b429b9b7Smatthias.ringwald 
6837b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
684b429b9b7Smatthias.ringwald 
685b429b9b7Smatthias.ringwald     // wake on
686b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
687b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
688b429b9b7Smatthias.ringwald     }
689b429b9b7Smatthias.ringwald 
690b429b9b7Smatthias.ringwald #if 0
691b429b9b7Smatthias.ringwald     // open low-level device
692b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
693b429b9b7Smatthias.ringwald     if (err){
6947d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
695b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
696b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
697b429b9b7Smatthias.ringwald         }
698b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
699b429b9b7Smatthias.ringwald         return err;
700b429b9b7Smatthias.ringwald     }
701b429b9b7Smatthias.ringwald #endif
702b429b9b7Smatthias.ringwald 
703b429b9b7Smatthias.ringwald     return 0;
704b429b9b7Smatthias.ringwald }
705b429b9b7Smatthias.ringwald 
706b429b9b7Smatthias.ringwald 
7078d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
708d661ed19Smatthias.ringwald 
7097b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
710d661ed19Smatthias.ringwald 
7118d213e1aSmatthias.ringwald     int err = 0;
7128d213e1aSmatthias.ringwald     switch (hci_stack.state){
7138d213e1aSmatthias.ringwald 
7148d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7158d213e1aSmatthias.ringwald             switch (power_mode){
7168d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7178d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7188d213e1aSmatthias.ringwald                     if (err) return err;
7197301ad89Smatthias.ringwald                     // set up state machine
7207301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7217301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7227301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7238d213e1aSmatthias.ringwald                     break;
7248d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7258d213e1aSmatthias.ringwald                     // do nothing
7268d213e1aSmatthias.ringwald                     break;
7278d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
728b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7298d213e1aSmatthias.ringwald                     break;
7308d213e1aSmatthias.ringwald             }
7318d213e1aSmatthias.ringwald             break;
7327301ad89Smatthias.ringwald 
7338d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7348d213e1aSmatthias.ringwald             switch (power_mode){
7358d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7368d213e1aSmatthias.ringwald                     // do nothing
7378d213e1aSmatthias.ringwald                     break;
7388d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7398d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7408d213e1aSmatthias.ringwald                     hci_power_control_off();
7418d213e1aSmatthias.ringwald                     break;
7428d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
743b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
74472ea5239Smatthias.ringwald                     hci_power_control_sleep();
7458d213e1aSmatthias.ringwald                     break;
7468d213e1aSmatthias.ringwald             }
7478d213e1aSmatthias.ringwald             break;
7487301ad89Smatthias.ringwald 
7498d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7508d213e1aSmatthias.ringwald             switch (power_mode){
7518d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7528d213e1aSmatthias.ringwald                     // do nothing
7538d213e1aSmatthias.ringwald                     break;
7548d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
755c7e0c5f6Smatthias.ringwald                     // see hci_run
756c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7578d213e1aSmatthias.ringwald                     break;
7588d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
759b546ac54Smatthias.ringwald                     // see hci_run
760b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
76189db417bSmatthias.ringwald                     hci_stack.substate = 0;
7628d213e1aSmatthias.ringwald                     break;
7638d213e1aSmatthias.ringwald             }
7648d213e1aSmatthias.ringwald             break;
7657301ad89Smatthias.ringwald 
7668d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7678d213e1aSmatthias.ringwald             switch (power_mode){
7688d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7698d213e1aSmatthias.ringwald                     // set up state machine
7708d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7718d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7728d213e1aSmatthias.ringwald                     break;
7738d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7748d213e1aSmatthias.ringwald                     // do nothing
7758d213e1aSmatthias.ringwald                     break;
7768d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
777b546ac54Smatthias.ringwald                     // see hci_run
778b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
77989db417bSmatthias.ringwald                     hci_stack.substate = 0;
7808d213e1aSmatthias.ringwald                     break;
7818d213e1aSmatthias.ringwald             }
7828d213e1aSmatthias.ringwald             break;
7838d213e1aSmatthias.ringwald 
7848d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7858d213e1aSmatthias.ringwald             switch (power_mode){
7868d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
787b546ac54Smatthias.ringwald                     // set up state machine
788b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
789b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
790b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7918d213e1aSmatthias.ringwald                     break;
7928d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
793b546ac54Smatthias.ringwald                     // see hci_run
794b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7958d213e1aSmatthias.ringwald                     break;
7968d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
797b546ac54Smatthias.ringwald                     // do nothing
7988d213e1aSmatthias.ringwald                     break;
7998d213e1aSmatthias.ringwald             }
8008d213e1aSmatthias.ringwald             break;
8018d213e1aSmatthias.ringwald 
8028d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
8038d213e1aSmatthias.ringwald             switch (power_mode){
8048d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8053144bce4Smatthias.ringwald                     err = hci_power_control_wake();
8063144bce4Smatthias.ringwald                     if (err) return err;
807b546ac54Smatthias.ringwald                     // set up state machine
808b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
809b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
810b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8118d213e1aSmatthias.ringwald                     break;
8128d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8134ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8148d213e1aSmatthias.ringwald                     break;
8158d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
816b546ac54Smatthias.ringwald                     // do nothing
8178d213e1aSmatthias.ringwald                     break;
8188d213e1aSmatthias.ringwald             }
8198d213e1aSmatthias.ringwald             break;
82011e23e5fSmatthias.ringwald     }
82168d92d03Smatthias.ringwald 
822038bc64cSmatthias.ringwald     // create internal event
823ee8bf225Smatthias.ringwald 	hci_emit_state();
824ee8bf225Smatthias.ringwald 
82568d92d03Smatthias.ringwald 	// trigger next/first action
82668d92d03Smatthias.ringwald 	hci_run();
82768d92d03Smatthias.ringwald 
828475c8125Smatthias.ringwald     return 0;
829475c8125Smatthias.ringwald }
830475c8125Smatthias.ringwald 
831381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
832381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
833381fbed8Smatthias.ringwald 
834381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
835381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
836381fbed8Smatthias.ringwald         return;
837381fbed8Smatthias.ringwald     }
838381fbed8Smatthias.ringwald 
839381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
840381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
841381fbed8Smatthias.ringwald }
842381fbed8Smatthias.ringwald 
84306b35ec0Smatthias.ringwald void hci_run(){
8448a485f27Smatthias.ringwald 
84532ab9390Smatthias.ringwald     hci_connection_t * connection;
84632ab9390Smatthias.ringwald     linked_item_t * it;
84732ab9390Smatthias.ringwald 
84832ab9390Smatthias.ringwald     // send pending HCI commands
84932ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
85032ab9390Smatthias.ringwald 
85132ab9390Smatthias.ringwald         connection = (hci_connection_t *) it;
85232ab9390Smatthias.ringwald 
85353b86778Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) {
8547b5fbe1fSmatthias.ringwald             // log_info("hci_run: cannot send command packet\n");
85553b86778Smatthias.ringwald             return;
85653b86778Smatthias.ringwald         }
85732ab9390Smatthias.ringwald 
85832ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
8597b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
86032ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
86132ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
862c7e0c5f6Smatthias.ringwald         }
863c7e0c5f6Smatthias.ringwald 
864de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
86532ab9390Smatthias.ringwald 
86632ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
86732ab9390Smatthias.ringwald             link_key_t link_key;
8687b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
86932ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
87032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
87132ab9390Smatthias.ringwald             } else {
87232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
87332ab9390Smatthias.ringwald             }
87432ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
87532ab9390Smatthias.ringwald         }
87632ab9390Smatthias.ringwald     }
87732ab9390Smatthias.ringwald 
878de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
879c7e0c5f6Smatthias.ringwald 
8803429f56bSmatthias.ringwald     switch (hci_stack.state){
8813429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8827b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
8833429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
8843429f56bSmatthias.ringwald                 // odd: waiting for command completion
88506b35ec0Smatthias.ringwald                 return;
8863429f56bSmatthias.ringwald             }
88790919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
888c7492964Smatthias.ringwald                 case 0: // RESET
88922909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
890c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
891c7492964Smatthias.ringwald                         // skip baud change
892c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
893c7492964Smatthias.ringwald                     }
8943429f56bSmatthias.ringwald                     break;
895c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
896c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
897c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
898c7492964Smatthias.ringwald                     break;
899c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
900c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
901c7492964Smatthias.ringwald                     hci_stack.substate += 2;
902c7492964Smatthias.ringwald                     // break missing here for fall through
903c7492964Smatthias.ringwald 
904c7492964Smatthias.ringwald                 case 3:
90590919203Smatthias.ringwald                     // custom initialization
90690919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
90790919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
90890919203Smatthias.ringwald                         if (cmd) {
90990919203Smatthias.ringwald                             int size = 3 + cmd[2];
910622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
911c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
91290919203Smatthias.ringwald                             break;
91390919203Smatthias.ringwald                         }
914c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
91590919203Smatthias.ringwald                     }
9162f6c30e1Smatthias.ringwald                     // otherwise continue
917f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
918f432a6ddSmatthias.ringwald 					break;
919c7492964Smatthias.ringwald 				case 4:
920e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
921e2edc0c3Smatthias.ringwald 					break;
922c7492964Smatthias.ringwald                 case 5:
9233429f56bSmatthias.ringwald                     // ca. 15 sec
9243429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9253429f56bSmatthias.ringwald                     break;
926c7492964Smatthias.ringwald 				case 6:
9270a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
928f432a6ddSmatthias.ringwald 					break;
929c7492964Smatthias.ringwald                 case 7:
9308a485f27Smatthias.ringwald #ifndef EMBEDDED
9318a485f27Smatthias.ringwald                 {
9328a485f27Smatthias.ringwald                     char hostname[30];
9338a485f27Smatthias.ringwald                     gethostname(hostname, 30);
9348a485f27Smatthias.ringwald                     hostname[29] = '\0';
9358a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
9368a485f27Smatthias.ringwald                     break;
9378a485f27Smatthias.ringwald                 }
938c7492964Smatthias.ringwald                 case 8:
9393c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
9403c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
9413c4d4b90Smatthias.ringwald                     break;
9423c4d4b90Smatthias.ringwald 
943c7492964Smatthias.ringwald                 case 9:
9443c4d4b90Smatthias.ringwald #endif
9458a485f27Smatthias.ringwald #endif
9463429f56bSmatthias.ringwald                     // done.
9473429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
948b360b6adSmatthias.ringwald                     hci_emit_state();
9493429f56bSmatthias.ringwald                     break;
9503429f56bSmatthias.ringwald                 default:
9513429f56bSmatthias.ringwald                     break;
952475c8125Smatthias.ringwald             }
9533429f56bSmatthias.ringwald             hci_stack.substate++;
9543429f56bSmatthias.ringwald             break;
955c7e0c5f6Smatthias.ringwald 
956c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
957c7e0c5f6Smatthias.ringwald 
9587b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
959c7e0c5f6Smatthias.ringwald             // close all open connections
960c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
961c7e0c5f6Smatthias.ringwald             if (connection){
96232ab9390Smatthias.ringwald 
963c7e0c5f6Smatthias.ringwald                 // send disconnect
96432ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
96532ab9390Smatthias.ringwald 
9667b5fbe1fSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
9676ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
968c7e0c5f6Smatthias.ringwald 
969c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
970c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
971c7e0c5f6Smatthias.ringwald                 return;
972c7e0c5f6Smatthias.ringwald             }
9737b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
974c7e0c5f6Smatthias.ringwald 
97572ea5239Smatthias.ringwald             // switch mode
976c7e0c5f6Smatthias.ringwald             hci_power_control_off();
9779418f9c9Smatthias.ringwald 
9787b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
97972ea5239Smatthias.ringwald             hci_emit_state();
9807b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
98172ea5239Smatthias.ringwald             break;
982c7e0c5f6Smatthias.ringwald 
98372ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
98489db417bSmatthias.ringwald             switch(hci_stack.substate) {
98589db417bSmatthias.ringwald                 case 0:
9867b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
98772ea5239Smatthias.ringwald                     // close all open connections
98872ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
98972ea5239Smatthias.ringwald                     if (connection){
99032ab9390Smatthias.ringwald 
99172ea5239Smatthias.ringwald                         // send disconnect
99232ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
99332ab9390Smatthias.ringwald 
9947b5fbe1fSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
9956ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
99672ea5239Smatthias.ringwald 
99772ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
99872ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
99972ea5239Smatthias.ringwald                         return;
100072ea5239Smatthias.ringwald                     }
100172ea5239Smatthias.ringwald 
100289db417bSmatthias.ringwald                     // disable page and inquiry scan
100332ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
100432ab9390Smatthias.ringwald 
10057b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
100689db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
100789db417bSmatthias.ringwald 
100889db417bSmatthias.ringwald                     // continue in next sub state
100989db417bSmatthias.ringwald                     hci_stack.substate++;
101089db417bSmatthias.ringwald                     break;
101189db417bSmatthias.ringwald                 case 1:
101289db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
101389db417bSmatthias.ringwald                     break;
101489db417bSmatthias.ringwald                 case 2:
10157b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
101672ea5239Smatthias.ringwald                     // switch mode
101789db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1018c7e0c5f6Smatthias.ringwald                     hci_emit_state();
101989db417bSmatthias.ringwald                 default:
102089db417bSmatthias.ringwald                     break;
102189db417bSmatthias.ringwald             }
1022c7e0c5f6Smatthias.ringwald             break;
1023c7e0c5f6Smatthias.ringwald 
10243429f56bSmatthias.ringwald         default:
10253429f56bSmatthias.ringwald             break;
10261f504dbdSmatthias.ringwald     }
10273429f56bSmatthias.ringwald }
102816833f0aSmatthias.ringwald 
102931452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1030c8e4258aSmatthias.ringwald     bd_addr_t addr;
1031c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1032c8e4258aSmatthias.ringwald     // house-keeping
1033c8e4258aSmatthias.ringwald 
1034c8e4258aSmatthias.ringwald     // create_connection?
1035c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1036c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
10377b5fbe1fSmatthias.ringwald         log_info("Create_connection to "); print_bd_addr(addr); log_info("\n");
1038c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1039c8e4258aSmatthias.ringwald         if (conn) {
1040c8e4258aSmatthias.ringwald             // if connection exists
1041c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
1042c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
1043c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
1044c8e4258aSmatthias.ringwald             }
1045c8e4258aSmatthias.ringwald             //    otherwise, just ignore
1046c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1047c8e4258aSmatthias.ringwald 
1048c8e4258aSmatthias.ringwald         } else{
1049c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
1050c8e4258aSmatthias.ringwald             if (conn){
1051c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
1052c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
1053c8e4258aSmatthias.ringwald             }
1054c8e4258aSmatthias.ringwald         }
1055c8e4258aSmatthias.ringwald     }
1056c8e4258aSmatthias.ringwald 
10577fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
10587fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
10597fde4af9Smatthias.ringwald     }
10607fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
10617fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
10627fde4af9Smatthias.ringwald     }
10637fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
10647fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
10657fde4af9Smatthias.ringwald     }
10667fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
10677fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
10687fde4af9Smatthias.ringwald     }
10697fde4af9Smatthias.ringwald 
10708ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
10718ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
10728ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10738ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
10748ef73945Smatthias.ringwald         }
10758ef73945Smatthias.ringwald     }
1076c8e4258aSmatthias.ringwald 
107731452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1078622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
107931452debSmatthias.ringwald }
10808adf0ddaSmatthias.ringwald 
10811cd208adSmatthias.ringwald /**
10821cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
10831cd208adSmatthias.ringwald  */
1084fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
10851cd208adSmatthias.ringwald     va_list argptr;
10861cd208adSmatthias.ringwald     va_start(argptr, cmd);
10871cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
10881cd208adSmatthias.ringwald     va_end(argptr);
1089*124062bcSmatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_cmd_buffer, size);
109093b8dc03Smatthias.ringwald }
1091c8e4258aSmatthias.ringwald 
1092ee091cf1Smatthias.ringwald // Create various non-HCI events.
1093ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1094ee091cf1Smatthias.ringwald 
1095c8e4258aSmatthias.ringwald void hci_emit_state(){
1096425d1371Smatthias.ringwald     uint8_t event[3];
109780d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1098425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1099c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1100425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1101425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1102c8e4258aSmatthias.ringwald }
1103c8e4258aSmatthias.ringwald 
1104c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
1105425d1371Smatthias.ringwald     uint8_t event[13];
1106c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1107425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1108c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
1109c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1110c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1111c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1112c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1113425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1114425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1115c8e4258aSmatthias.ringwald }
1116c8e4258aSmatthias.ringwald 
11173c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1118425d1371Smatthias.ringwald     uint8_t event[6];
11193c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1120e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
11213c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
11223c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
11233c4d4b90Smatthias.ringwald     event[5] = reason;
1124425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1125425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11263c4d4b90Smatthias.ringwald }
11273c4d4b90Smatthias.ringwald 
1128ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1129425d1371Smatthias.ringwald     uint8_t event[4];
113080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1131425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1132ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1133425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1134425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1135ee091cf1Smatthias.ringwald }
113643bfb1bdSmatthias.ringwald 
113743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1138425d1371Smatthias.ringwald     uint8_t event[3];
113980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1140425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
114143bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1142425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1143425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
114443bfb1bdSmatthias.ringwald }
1145038bc64cSmatthias.ringwald 
1146038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1147425d1371Smatthias.ringwald     uint8_t event[2];
114880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1149425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1150425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1151425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1152038bc64cSmatthias.ringwald }
11531b0e3922Smatthias.ringwald 
11541b0e3922Smatthias.ringwald 
11551b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1156425d1371Smatthias.ringwald     uint8_t event[6];
11571b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1158425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1159425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1160425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1161425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1162425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1163425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11641b0e3922Smatthias.ringwald }
11651b0e3922Smatthias.ringwald 
11662ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1167425d1371Smatthias.ringwald     uint8_t event[3];
11682ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1169425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
11702ed6235cSmatthias.ringwald     event[2] = enabled;
1171425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1172e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11732ed6235cSmatthias.ringwald }
1174627c2f45Smatthias.ringwald 
1175627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1176425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1177627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1178425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1179f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
11802f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1181f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1182425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1183425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1184627c2f45Smatthias.ringwald }
1185381fbed8Smatthias.ringwald 
1186381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1187425d1371Smatthias.ringwald     uint8_t event[3];
1188381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1189425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1190381fbed8Smatthias.ringwald     event[2] = enabled;
1191425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1192425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1193381fbed8Smatthias.ringwald }
1194