xref: /btstack/src/hci.c (revision decc01a8dc6f97c744522ec8141a050e07aed30e)
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 
522ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h"
532ed6235cSmatthias.ringwald #include "../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 
78981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
79c7492964Smatthias.ringwald #ifdef HAVE_TIME
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);
92c7492964Smatthias.ringwald #endif
93ee091cf1Smatthias.ringwald }
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;
1117fde4af9Smatthias.ringwald     conn->authentication_flags = 0;
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){
212c24735b1Smatthias.ringwald     switch (packet_type) {
213c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
214c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
215c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
216de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
217c24735b1Smatthias.ringwald         default:
218c24735b1Smatthias.ringwald             return 0;
219c24735b1Smatthias.ringwald     }
220c24735b1Smatthias.ringwald }
221c24735b1Smatthias.ringwald 
222ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2237856c818Smatthias.ringwald 
2246218e6f1Smatthias.ringwald     // check for free places on BT module
2255932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2266218e6f1Smatthias.ringwald 
2277856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2287856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
22956cf178bSmatthias.ringwald     if (!connection) return 0;
23056cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
23156cf178bSmatthias.ringwald 
23256cf178bSmatthias.ringwald     // count packet
23356cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2348ea03fa5Smatthias.ringwald     // log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2357856c818Smatthias.ringwald 
2366218e6f1Smatthias.ringwald     // send packet - ignore errors
237622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2386218e6f1Smatthias.ringwald 
2396218e6f1Smatthias.ringwald     return 0;
240ee091cf1Smatthias.ringwald }
241ee091cf1Smatthias.ringwald 
24216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2437856c818Smatthias.ringwald 
2447856c818Smatthias.ringwald     // get info
2457856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2467856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2477856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2487856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2497856c818Smatthias.ringwald 
2507856c818Smatthias.ringwald     // ignore non-registered handle
2517856c818Smatthias.ringwald     if (!conn){
2527f2435e6Smatthias.ringwald         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2537856c818Smatthias.ringwald         return;
2547856c818Smatthias.ringwald     }
2557856c818Smatthias.ringwald 
2567856c818Smatthias.ringwald     // update idle timestamp
2577856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2587856c818Smatthias.ringwald 
2597856c818Smatthias.ringwald     // handle different packet types
2607856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2617856c818Smatthias.ringwald 
2627856c818Smatthias.ringwald         case 0x01: // continuation fragment
2637856c818Smatthias.ringwald 
2647856c818Smatthias.ringwald             // sanity check
2657856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2667f2435e6Smatthias.ringwald                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2677856c818Smatthias.ringwald                 return;
2687856c818Smatthias.ringwald             }
2697856c818Smatthias.ringwald 
2707856c818Smatthias.ringwald             // append fragment payload (header already stored)
2717856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2727856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2737856c818Smatthias.ringwald 
274*decc01a8Smatthias.ringwald             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
275*decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
2767856c818Smatthias.ringwald 
2777856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2787856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2797856c818Smatthias.ringwald 
2802718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2817856c818Smatthias.ringwald                 // reset recombination buffer
2827856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2837856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2847856c818Smatthias.ringwald             }
2857856c818Smatthias.ringwald             break;
2867856c818Smatthias.ringwald 
2877856c818Smatthias.ringwald         case 0x02: { // first fragment
2887856c818Smatthias.ringwald 
2897856c818Smatthias.ringwald             // sanity check
2907856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
2917f2435e6Smatthias.ringwald                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
2927856c818Smatthias.ringwald                 return;
2937856c818Smatthias.ringwald             }
2947856c818Smatthias.ringwald 
2957856c818Smatthias.ringwald             // peek into L2CAP packet!
2967856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
2977856c818Smatthias.ringwald 
298*decc01a8Smatthias.ringwald             // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
299*decc01a8Smatthias.ringwald 
3007856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3017856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3027856c818Smatthias.ringwald 
3037856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3042718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3057856c818Smatthias.ringwald 
3067856c818Smatthias.ringwald             } else {
3077856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3087856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3097856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3107856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
311*decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3127856c818Smatthias.ringwald             }
3137856c818Smatthias.ringwald             break;
3147856c818Smatthias.ringwald 
3157856c818Smatthias.ringwald         }
3167856c818Smatthias.ringwald         default:
3177f2435e6Smatthias.ringwald             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3187856c818Smatthias.ringwald             return;
3197856c818Smatthias.ringwald     }
32094ab26f8Smatthias.ringwald 
32194ab26f8Smatthias.ringwald     // execute main loop
32294ab26f8Smatthias.ringwald     hci_run();
32316833f0aSmatthias.ringwald }
32422909952Smatthias.ringwald 
32567a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
326c7e0c5f6Smatthias.ringwald     log_dbg("Connection closed: handle %u, ", conn->con_handle);
327c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
328c7e0c5f6Smatthias.ringwald     log_dbg("\n");
3293c4d4b90Smatthias.ringwald 
3303c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3313c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3323c4d4b90Smatthias.ringwald 
333c7492964Smatthias.ringwald #ifdef HAVE_TIME
334c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
335c7492964Smatthias.ringwald #endif
336c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
337c7e0c5f6Smatthias.ringwald     free( conn );
3383c4d4b90Smatthias.ringwald 
3393c4d4b90Smatthias.ringwald     // now it's gone
340c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
341c7e0c5f6Smatthias.ringwald }
342c7e0c5f6Smatthias.ringwald 
34374ec757aSmatthias.ringwald // avoid huge local variables
34474ec757aSmatthias.ringwald static device_name_t device_name;
34516833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3461281a47eSmatthias.ringwald     bd_addr_t addr;
347fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3481f7b95a1Smatthias.ringwald     hci_connection_t * conn;
34956cf178bSmatthias.ringwald     int i;
35022909952Smatthias.ringwald 
3516772a24cSmatthias.ringwald     switch (packet[0]) {
35222909952Smatthias.ringwald 
3536772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3547ec5eeaaSmatthias.ringwald             // get num cmd packets
3557ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3567ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3577ec5eeaaSmatthias.ringwald 
358e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
359e2edc0c3Smatthias.ringwald                 // from offset 5
360e2edc0c3Smatthias.ringwald                 // status
3611d279b20Smatthias.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"
362e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
36356cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
364e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
36556cf178bSmatthias.ringwald                 // ignore: total num SCO packets
36656cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
367e2edc0c3Smatthias.ringwald                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
368e2edc0c3Smatthias.ringwald                 }
36956cf178bSmatthias.ringwald             }
370381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
371381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
372381fbed8Smatthias.ringwald             }
37356cf178bSmatthias.ringwald             break;
37456cf178bSmatthias.ringwald 
3757ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
3767ec5eeaaSmatthias.ringwald             // get num cmd packets
3777ec5eeaaSmatthias.ringwald             // log_dbg("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
3787ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
3797ec5eeaaSmatthias.ringwald             break;
3807ec5eeaaSmatthias.ringwald 
38156cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
38256cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
38356cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
38456cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
38556cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
38656cf178bSmatthias.ringwald                 if (!conn){
38756cf178bSmatthias.ringwald                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
38856cf178bSmatthias.ringwald                     continue;
38956cf178bSmatthias.ringwald                 }
39056cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
3918ea03fa5Smatthias.ringwald                 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
39256cf178bSmatthias.ringwald             }
3936772a24cSmatthias.ringwald             break;
3946772a24cSmatthias.ringwald 
3951f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
39637eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
39737eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
39837eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
399cf0b66f0Smatthias.ringwald             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
40037eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4011f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4021f7b95a1Smatthias.ringwald                 if (!conn) {
4031f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4041f7b95a1Smatthias.ringwald                 }
4051f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
40632ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
40732ab9390Smatthias.ringwald                 hci_run();
40837eaa4cfSmatthias.ringwald             } else {
40937eaa4cfSmatthias.ringwald                 // TODO: decline request
41037eaa4cfSmatthias.ringwald             }
4111f7b95a1Smatthias.ringwald             break;
4121f7b95a1Smatthias.ringwald 
4136772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
414fe1ed1b8Smatthias.ringwald             // Connection management
415fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
416cf0b66f0Smatthias.ringwald             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
4171f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
418fe1ed1b8Smatthias.ringwald             if (conn) {
419b448a0e7Smatthias.ringwald                 if (!packet[2]){
420c8e4258aSmatthias.ringwald                     conn->state = OPEN;
421fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
422ee091cf1Smatthias.ringwald 
423c7492964Smatthias.ringwald #ifdef HAVE_TIME
424ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
425c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
426ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
427c7492964Smatthias.ringwald #endif
428cf0b66f0Smatthias.ringwald                     log_dbg("New connection: handle %u, ", conn->con_handle);
429fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
430cf0b66f0Smatthias.ringwald                     log_dbg("\n");
43143bfb1bdSmatthias.ringwald 
43243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
433b448a0e7Smatthias.ringwald                 } else {
434b448a0e7Smatthias.ringwald                     // connection failed, remove entry
435b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
436b448a0e7Smatthias.ringwald                     free( conn );
437c12e46e7Smatthias.ringwald 
438c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
439c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
440c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
441c12e46e7Smatthias.ringwald                     }
442fe1ed1b8Smatthias.ringwald                 }
443fe1ed1b8Smatthias.ringwald             }
4446772a24cSmatthias.ringwald             break;
445fe1ed1b8Smatthias.ringwald 
4467fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
44764472d52Smatthias.ringwald             log_dbg("HCI_EVENT_LINK_KEY_REQUEST\n");
4487fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
44974ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
45032ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
45164472d52Smatthias.ringwald             hci_run();
45229d53098Smatthias.ringwald             // request already answered
45329d53098Smatthias.ringwald             return;
4547fde4af9Smatthias.ringwald 
4557fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4567fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
45774ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
45829d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
459287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
46029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4617fde4af9Smatthias.ringwald             break;
4627fde4af9Smatthias.ringwald 
4637fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4647fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4657fde4af9Smatthias.ringwald             break;
4667fde4af9Smatthias.ringwald 
46774ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
46874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
46974ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
47074ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
4715a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
4725a06394aSmatthias.ringwald             for (i=0; i<248;i++){
4735a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
4745a06394aSmatthias.ringwald                     packet[9+i] = 0;
4755a06394aSmatthias.ringwald                     break;
476cdc9101dSmatthias.ringwald                 }
4775a06394aSmatthias.ringwald             }
47874ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
47974ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
48074ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
48174ec757aSmatthias.ringwald             break;
48274ec757aSmatthias.ringwald 
48374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
48474ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
48574ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
48674ec757aSmatthias.ringwald             // first send inq result packet
48774ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
48874ec757aSmatthias.ringwald             // then send cached remote names
48974ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
49074ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
49174ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
49274ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
49374ec757aSmatthias.ringwald                 }
49474ec757aSmatthias.ringwald             }
49574ec757aSmatthias.ringwald             return;
49674ec757aSmatthias.ringwald 
4976772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
498fe1ed1b8Smatthias.ringwald             if (!packet[2]){
499fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
500fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
501fe1ed1b8Smatthias.ringwald                 if (conn) {
502c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
503fe1ed1b8Smatthias.ringwald                 }
504fe1ed1b8Smatthias.ringwald             }
5056772a24cSmatthias.ringwald             break;
5066772a24cSmatthias.ringwald 
507c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
508c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
509c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
510c68bdf90Smatthias.ringwald             }
511c68bdf90Smatthias.ringwald             break;
512c68bdf90Smatthias.ringwald 
5136772a24cSmatthias.ringwald         default:
5146772a24cSmatthias.ringwald             break;
515fe1ed1b8Smatthias.ringwald     }
516fe1ed1b8Smatthias.ringwald 
5173429f56bSmatthias.ringwald     // handle BT initialization
5183429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5197301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5207301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5217301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5227301ad89Smatthias.ringwald         // }
5237301ad89Smatthias.ringwald         // handle normal init sequence
5243429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5253429f56bSmatthias.ringwald             // odd: waiting for event
5263429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5273429f56bSmatthias.ringwald                 hci_stack.substate++;
5283429f56bSmatthias.ringwald             }
5293429f56bSmatthias.ringwald         }
53022909952Smatthias.ringwald     }
53122909952Smatthias.ringwald 
53289db417bSmatthias.ringwald     // help with BT sleep
53389db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
53489db417bSmatthias.ringwald         && hci_stack.substate == 1
53589db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
53689db417bSmatthias.ringwald         hci_stack.substate++;
53789db417bSmatthias.ringwald     }
53889db417bSmatthias.ringwald 
5392718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
54094ab26f8Smatthias.ringwald 
54194ab26f8Smatthias.ringwald 	// execute main loop
54294ab26f8Smatthias.ringwald 	hci_run();
54316833f0aSmatthias.ringwald }
54416833f0aSmatthias.ringwald 
54510e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
54610e830c9Smatthias.ringwald     switch (packet_type) {
54710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
54810e830c9Smatthias.ringwald             event_handler(packet, size);
54910e830c9Smatthias.ringwald             break;
55010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
55110e830c9Smatthias.ringwald             acl_handler(packet, size);
55210e830c9Smatthias.ringwald             break;
55310e830c9Smatthias.ringwald         default:
55410e830c9Smatthias.ringwald             break;
55510e830c9Smatthias.ringwald     }
55610e830c9Smatthias.ringwald }
55710e830c9Smatthias.ringwald 
558fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5592718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5602718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
56116833f0aSmatthias.ringwald }
56216833f0aSmatthias.ringwald 
563404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
564475c8125Smatthias.ringwald 
565475c8125Smatthias.ringwald     // reference to use transport layer implementation
56616833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
567475c8125Smatthias.ringwald 
56811e23e5fSmatthias.ringwald     // references to used control implementation
56911e23e5fSmatthias.ringwald     hci_stack.control = control;
57011e23e5fSmatthias.ringwald 
57111e23e5fSmatthias.ringwald     // reference to used config
57211e23e5fSmatthias.ringwald     hci_stack.config = config;
57311e23e5fSmatthias.ringwald 
574fe1ed1b8Smatthias.ringwald     // no connections yet
575fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
5760a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
577fe1ed1b8Smatthias.ringwald 
57802ea9861Smatthias.ringwald     // empty cmd buffer
57916833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
58016833f0aSmatthias.ringwald 
58116833f0aSmatthias.ringwald     // higher level handler
5822718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
58316833f0aSmatthias.ringwald 
584404843c1Smatthias.ringwald     // store and open remote device db
585404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
586404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
587404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
588404843c1Smatthias.ringwald     }
58929d53098Smatthias.ringwald 
59016833f0aSmatthias.ringwald     // register packet handlers with transport
59110e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
592475c8125Smatthias.ringwald }
593475c8125Smatthias.ringwald 
594404843c1Smatthias.ringwald void hci_close(){
595404843c1Smatthias.ringwald     // close remote device db
596404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
597404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
598404843c1Smatthias.ringwald     }
599404843c1Smatthias.ringwald }
600404843c1Smatthias.ringwald 
6018d213e1aSmatthias.ringwald // State-Module-Driver overview
6028d213e1aSmatthias.ringwald // state                    module  low-level
6038d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6048d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6058d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6068d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
607d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
608d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
609c7e0c5f6Smatthias.ringwald 
61040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6117301ad89Smatthias.ringwald 
612038bc64cSmatthias.ringwald     // power on
613f9a30166Smatthias.ringwald     int err = 0;
614f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
615f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
616f9a30166Smatthias.ringwald     }
617038bc64cSmatthias.ringwald     if (err){
6187f2435e6Smatthias.ringwald         log_err( "POWER_ON failed\n");
619038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
620038bc64cSmatthias.ringwald         return err;
621038bc64cSmatthias.ringwald     }
622038bc64cSmatthias.ringwald 
623038bc64cSmatthias.ringwald     // open low-level device
624038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
625038bc64cSmatthias.ringwald     if (err){
6267f2435e6Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
627f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
628f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
629f9a30166Smatthias.ringwald         }
630038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
631038bc64cSmatthias.ringwald         return err;
632038bc64cSmatthias.ringwald     }
6338d213e1aSmatthias.ringwald     return 0;
6348d213e1aSmatthias.ringwald }
635038bc64cSmatthias.ringwald 
63640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6378d213e1aSmatthias.ringwald 
6389418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off\n");
6399418f9c9Smatthias.ringwald 
6408d213e1aSmatthias.ringwald     // close low-level device
6418d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6428d213e1aSmatthias.ringwald 
6439418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off - hci_transport closed\n");
6449418f9c9Smatthias.ringwald 
6458d213e1aSmatthias.ringwald     // power off
6468d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6478d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6488d213e1aSmatthias.ringwald     }
6499418f9c9Smatthias.ringwald 
6509418f9c9Smatthias.ringwald     log_dbg("hci_power_control_off - control closed\n");
6519418f9c9Smatthias.ringwald 
65272ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
65372ea5239Smatthias.ringwald }
65472ea5239Smatthias.ringwald 
65540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
65672ea5239Smatthias.ringwald 
6579418f9c9Smatthias.ringwald     log_dbg("hci_power_control_sleep\n");
6583144bce4Smatthias.ringwald 
659b429b9b7Smatthias.ringwald #if 0
660b429b9b7Smatthias.ringwald     // don't close serial port during sleep
661b429b9b7Smatthias.ringwald 
66272ea5239Smatthias.ringwald     // close low-level device
66372ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
664b429b9b7Smatthias.ringwald #endif
66572ea5239Smatthias.ringwald 
66672ea5239Smatthias.ringwald     // sleep mode
6673144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
66872ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
66972ea5239Smatthias.ringwald     }
670b429b9b7Smatthias.ringwald 
67172ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
6728d213e1aSmatthias.ringwald }
6738d213e1aSmatthias.ringwald 
67440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
675b429b9b7Smatthias.ringwald 
6769418f9c9Smatthias.ringwald     log_dbg("hci_power_control_wake\n");
677b429b9b7Smatthias.ringwald 
678b429b9b7Smatthias.ringwald     // wake on
679b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
680b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
681b429b9b7Smatthias.ringwald     }
682b429b9b7Smatthias.ringwald 
683b429b9b7Smatthias.ringwald #if 0
684b429b9b7Smatthias.ringwald     // open low-level device
685b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
686b429b9b7Smatthias.ringwald     if (err){
687b429b9b7Smatthias.ringwald         log_err( "HCI_INIT failed, turning Bluetooth off again\n");
688b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
689b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
690b429b9b7Smatthias.ringwald         }
691b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
692b429b9b7Smatthias.ringwald         return err;
693b429b9b7Smatthias.ringwald     }
694b429b9b7Smatthias.ringwald #endif
695b429b9b7Smatthias.ringwald 
696b429b9b7Smatthias.ringwald     return 0;
697b429b9b7Smatthias.ringwald }
698b429b9b7Smatthias.ringwald 
699b429b9b7Smatthias.ringwald 
7008d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
701d661ed19Smatthias.ringwald 
7027ec5eeaaSmatthias.ringwald     log_dbg("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
703d661ed19Smatthias.ringwald 
7048d213e1aSmatthias.ringwald     int err = 0;
7058d213e1aSmatthias.ringwald     switch (hci_stack.state){
7068d213e1aSmatthias.ringwald 
7078d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7088d213e1aSmatthias.ringwald             switch (power_mode){
7098d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7108d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7118d213e1aSmatthias.ringwald                     if (err) return err;
7127301ad89Smatthias.ringwald                     // set up state machine
7137301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7147301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7157301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7168d213e1aSmatthias.ringwald                     break;
7178d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7188d213e1aSmatthias.ringwald                     // do nothing
7198d213e1aSmatthias.ringwald                     break;
7208d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
721b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7228d213e1aSmatthias.ringwald                     break;
7238d213e1aSmatthias.ringwald             }
7248d213e1aSmatthias.ringwald             break;
7257301ad89Smatthias.ringwald 
7268d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7278d213e1aSmatthias.ringwald             switch (power_mode){
7288d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7298d213e1aSmatthias.ringwald                     // do nothing
7308d213e1aSmatthias.ringwald                     break;
7318d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7328d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7338d213e1aSmatthias.ringwald                     hci_power_control_off();
7348d213e1aSmatthias.ringwald                     break;
7358d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
736b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
73772ea5239Smatthias.ringwald                     hci_power_control_sleep();
7388d213e1aSmatthias.ringwald                     break;
7398d213e1aSmatthias.ringwald             }
7408d213e1aSmatthias.ringwald             break;
7417301ad89Smatthias.ringwald 
7428d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7438d213e1aSmatthias.ringwald             switch (power_mode){
7448d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7458d213e1aSmatthias.ringwald                     // do nothing
7468d213e1aSmatthias.ringwald                     break;
7478d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
748c7e0c5f6Smatthias.ringwald                     // see hci_run
749c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7508d213e1aSmatthias.ringwald                     break;
7518d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
752b546ac54Smatthias.ringwald                     // see hci_run
753b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
75489db417bSmatthias.ringwald                     hci_stack.substate = 0;
7558d213e1aSmatthias.ringwald                     break;
7568d213e1aSmatthias.ringwald             }
7578d213e1aSmatthias.ringwald             break;
7587301ad89Smatthias.ringwald 
7598d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
7608d213e1aSmatthias.ringwald             switch (power_mode){
7618d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7628d213e1aSmatthias.ringwald                     // set up state machine
7638d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7648d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
7658d213e1aSmatthias.ringwald                     break;
7668d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7678d213e1aSmatthias.ringwald                     // do nothing
7688d213e1aSmatthias.ringwald                     break;
7698d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
770b546ac54Smatthias.ringwald                     // see hci_run
771b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
77289db417bSmatthias.ringwald                     hci_stack.substate = 0;
7738d213e1aSmatthias.ringwald                     break;
7748d213e1aSmatthias.ringwald             }
7758d213e1aSmatthias.ringwald             break;
7768d213e1aSmatthias.ringwald 
7778d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
7788d213e1aSmatthias.ringwald             switch (power_mode){
7798d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
780b546ac54Smatthias.ringwald                     // set up state machine
781b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
782b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
783b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
7848d213e1aSmatthias.ringwald                     break;
7858d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
786b546ac54Smatthias.ringwald                     // see hci_run
787b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
7888d213e1aSmatthias.ringwald                     break;
7898d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
790b546ac54Smatthias.ringwald                     // do nothing
7918d213e1aSmatthias.ringwald                     break;
7928d213e1aSmatthias.ringwald             }
7938d213e1aSmatthias.ringwald             break;
7948d213e1aSmatthias.ringwald 
7958d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
7968d213e1aSmatthias.ringwald             switch (power_mode){
7978d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7983144bce4Smatthias.ringwald                     err = hci_power_control_wake();
7993144bce4Smatthias.ringwald                     if (err) return err;
800b546ac54Smatthias.ringwald                     // set up state machine
801b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
802b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
803b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8048d213e1aSmatthias.ringwald                     break;
8058d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8064ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8078d213e1aSmatthias.ringwald                     break;
8088d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
809b546ac54Smatthias.ringwald                     // do nothing
8108d213e1aSmatthias.ringwald                     break;
8118d213e1aSmatthias.ringwald             }
8128d213e1aSmatthias.ringwald             break;
81311e23e5fSmatthias.ringwald     }
81468d92d03Smatthias.ringwald 
815038bc64cSmatthias.ringwald     // create internal event
816ee8bf225Smatthias.ringwald 	hci_emit_state();
817ee8bf225Smatthias.ringwald 
81868d92d03Smatthias.ringwald 	// trigger next/first action
81968d92d03Smatthias.ringwald 	hci_run();
82068d92d03Smatthias.ringwald 
821475c8125Smatthias.ringwald     return 0;
822475c8125Smatthias.ringwald }
823475c8125Smatthias.ringwald 
824381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
825381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
826381fbed8Smatthias.ringwald 
827381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
828381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
829381fbed8Smatthias.ringwald         return;
830381fbed8Smatthias.ringwald     }
831381fbed8Smatthias.ringwald 
832381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
833381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
834381fbed8Smatthias.ringwald }
835381fbed8Smatthias.ringwald 
83606b35ec0Smatthias.ringwald void hci_run(){
8378a485f27Smatthias.ringwald 
83832ab9390Smatthias.ringwald     hci_connection_t * connection;
83932ab9390Smatthias.ringwald     linked_item_t * it;
84032ab9390Smatthias.ringwald 
84132ab9390Smatthias.ringwald     // send pending HCI commands
84232ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
84332ab9390Smatthias.ringwald 
84432ab9390Smatthias.ringwald         connection = (hci_connection_t *) it;
84532ab9390Smatthias.ringwald 
846de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
84732ab9390Smatthias.ringwald 
84832ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
84964472d52Smatthias.ringwald             log_dbg("sending hci_accept_connection_request\n");
85032ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
85132ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
852c7e0c5f6Smatthias.ringwald         }
853c7e0c5f6Smatthias.ringwald 
854de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
85532ab9390Smatthias.ringwald 
85632ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
85732ab9390Smatthias.ringwald             link_key_t link_key;
85864472d52Smatthias.ringwald             log_dbg("responding to link key request\n");
85932ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
86032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
86132ab9390Smatthias.ringwald             } else {
86232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
86332ab9390Smatthias.ringwald             }
86432ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
86532ab9390Smatthias.ringwald         }
86632ab9390Smatthias.ringwald     }
86732ab9390Smatthias.ringwald 
868de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
869c7e0c5f6Smatthias.ringwald 
8703429f56bSmatthias.ringwald     switch (hci_stack.state){
8713429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
8723429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
8733429f56bSmatthias.ringwald                 // odd: waiting for command completion
87406b35ec0Smatthias.ringwald                 return;
8753429f56bSmatthias.ringwald             }
87690919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
877c7492964Smatthias.ringwald                 case 0: // RESET
87822909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
879c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
880c7492964Smatthias.ringwald                         // skip baud change
881c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
882c7492964Smatthias.ringwald                     }
8833429f56bSmatthias.ringwald                     break;
884c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
885c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
886c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
887c7492964Smatthias.ringwald                     break;
888c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
889c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
890c7492964Smatthias.ringwald                     hci_stack.substate += 2;
891c7492964Smatthias.ringwald                     // break missing here for fall through
892c7492964Smatthias.ringwald 
893c7492964Smatthias.ringwald                 case 3:
89490919203Smatthias.ringwald                     // custom initialization
89590919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
89690919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
89790919203Smatthias.ringwald                         if (cmd) {
89890919203Smatthias.ringwald                             int size = 3 + cmd[2];
899622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
900c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
90190919203Smatthias.ringwald                             break;
90290919203Smatthias.ringwald                         }
903c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
90490919203Smatthias.ringwald                     }
9052f6c30e1Smatthias.ringwald                     // otherwise continue
906f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
907f432a6ddSmatthias.ringwald 					break;
908c7492964Smatthias.ringwald 				case 4:
909e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
910e2edc0c3Smatthias.ringwald 					break;
911c7492964Smatthias.ringwald                 case 5:
9123429f56bSmatthias.ringwald                     // ca. 15 sec
9133429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9143429f56bSmatthias.ringwald                     break;
915c7492964Smatthias.ringwald 				case 6:
9160a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
917f432a6ddSmatthias.ringwald 					break;
918c7492964Smatthias.ringwald                 case 7:
9198a485f27Smatthias.ringwald #ifndef EMBEDDED
9208a485f27Smatthias.ringwald                 {
9218a485f27Smatthias.ringwald                     char hostname[30];
9228a485f27Smatthias.ringwald                     gethostname(hostname, 30);
9238a485f27Smatthias.ringwald                     hostname[29] = '\0';
9248a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
9258a485f27Smatthias.ringwald                     break;
9268a485f27Smatthias.ringwald                 }
927c7492964Smatthias.ringwald                 case 8:
9283c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
9293c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
9303c4d4b90Smatthias.ringwald                     break;
9313c4d4b90Smatthias.ringwald 
932c7492964Smatthias.ringwald                 case 9:
9333c4d4b90Smatthias.ringwald #endif
9348a485f27Smatthias.ringwald #endif
9353429f56bSmatthias.ringwald                     // done.
9363429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
937b360b6adSmatthias.ringwald                     hci_emit_state();
9383429f56bSmatthias.ringwald                     break;
9393429f56bSmatthias.ringwald                 default:
9403429f56bSmatthias.ringwald                     break;
941475c8125Smatthias.ringwald             }
9423429f56bSmatthias.ringwald             hci_stack.substate++;
9433429f56bSmatthias.ringwald             break;
944c7e0c5f6Smatthias.ringwald 
945c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
946c7e0c5f6Smatthias.ringwald 
9473144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING\n");
948c7e0c5f6Smatthias.ringwald             // close all open connections
949c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
950c7e0c5f6Smatthias.ringwald             if (connection){
95132ab9390Smatthias.ringwald 
952c7e0c5f6Smatthias.ringwald                 // send disconnect
95332ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
95432ab9390Smatthias.ringwald 
95532ab9390Smatthias.ringwald                 log_dbg("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle);
9566ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
957c7e0c5f6Smatthias.ringwald 
958c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
959c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
960c7e0c5f6Smatthias.ringwald                 return;
961c7e0c5f6Smatthias.ringwald             }
9623144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, calling off\n");
963c7e0c5f6Smatthias.ringwald 
96472ea5239Smatthias.ringwald             // switch mode
965c7e0c5f6Smatthias.ringwald             hci_power_control_off();
9669418f9c9Smatthias.ringwald 
9679418f9c9Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, emitting state\n");
96872ea5239Smatthias.ringwald             hci_emit_state();
9693144bce4Smatthias.ringwald             log_dbg("HCI_STATE_HALTING, done\n");
97072ea5239Smatthias.ringwald             break;
971c7e0c5f6Smatthias.ringwald 
97272ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
97389db417bSmatthias.ringwald             switch(hci_stack.substate) {
97489db417bSmatthias.ringwald                 case 0:
97589db417bSmatthias.ringwald                     log_dbg("HCI_STATE_FALLING_ASLEEP\n");
97672ea5239Smatthias.ringwald                     // close all open connections
97772ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
97872ea5239Smatthias.ringwald                     if (connection){
97932ab9390Smatthias.ringwald 
98072ea5239Smatthias.ringwald                         // send disconnect
98132ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
98232ab9390Smatthias.ringwald 
98332ab9390Smatthias.ringwald                         log_dbg("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle);
9846ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
98572ea5239Smatthias.ringwald 
98672ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
98772ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
98872ea5239Smatthias.ringwald                         return;
98972ea5239Smatthias.ringwald                     }
99072ea5239Smatthias.ringwald 
99189db417bSmatthias.ringwald                     // disable page and inquiry scan
99232ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
99332ab9390Smatthias.ringwald 
99432ab9390Smatthias.ringwald                     log_dbg("HCI_STATE_HALTING, disabling inq & page scans\n");
99589db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
99689db417bSmatthias.ringwald 
99789db417bSmatthias.ringwald                     // continue in next sub state
99889db417bSmatthias.ringwald                     hci_stack.substate++;
99989db417bSmatthias.ringwald                     break;
100089db417bSmatthias.ringwald                 case 1:
100189db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
100289db417bSmatthias.ringwald                     break;
100389db417bSmatthias.ringwald                 case 2:
100489db417bSmatthias.ringwald                     log_dbg("HCI_STATE_HALTING, calling sleep\n");
100572ea5239Smatthias.ringwald                     // switch mode
100689db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1007c7e0c5f6Smatthias.ringwald                     hci_emit_state();
100889db417bSmatthias.ringwald                 default:
100989db417bSmatthias.ringwald                     break;
101089db417bSmatthias.ringwald             }
1011c7e0c5f6Smatthias.ringwald             break;
1012c7e0c5f6Smatthias.ringwald 
10133429f56bSmatthias.ringwald         default:
10143429f56bSmatthias.ringwald             break;
10151f504dbdSmatthias.ringwald     }
10163429f56bSmatthias.ringwald }
101716833f0aSmatthias.ringwald 
101831452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1019c8e4258aSmatthias.ringwald     bd_addr_t addr;
1020c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1021c8e4258aSmatthias.ringwald     // house-keeping
1022c8e4258aSmatthias.ringwald 
1023c8e4258aSmatthias.ringwald     // create_connection?
1024c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1025c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1026cf0b66f0Smatthias.ringwald         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
1027c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1028c8e4258aSmatthias.ringwald         if (conn) {
1029c8e4258aSmatthias.ringwald             // if connection exists
1030c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
1031c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
1032c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
1033c8e4258aSmatthias.ringwald             }
1034c8e4258aSmatthias.ringwald             //    otherwise, just ignore
1035c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1036c8e4258aSmatthias.ringwald 
1037c8e4258aSmatthias.ringwald         } else{
1038c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
1039c8e4258aSmatthias.ringwald             if (conn){
1040c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
1041c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
1042c8e4258aSmatthias.ringwald             }
1043c8e4258aSmatthias.ringwald         }
1044c8e4258aSmatthias.ringwald     }
1045c8e4258aSmatthias.ringwald 
10467fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
10477fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
10487fde4af9Smatthias.ringwald     }
10497fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
10507fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
10517fde4af9Smatthias.ringwald     }
10527fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
10537fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
10547fde4af9Smatthias.ringwald     }
10557fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
10567fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
10577fde4af9Smatthias.ringwald     }
10587fde4af9Smatthias.ringwald 
10598ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
10608ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
10618ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
10628ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
10638ef73945Smatthias.ringwald         }
10648ef73945Smatthias.ringwald     }
1065c8e4258aSmatthias.ringwald 
106631452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1067622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
106831452debSmatthias.ringwald }
10698adf0ddaSmatthias.ringwald 
10701cd208adSmatthias.ringwald /**
10711cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
10721cd208adSmatthias.ringwald  */
1073fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
10741cd208adSmatthias.ringwald     va_list argptr;
10751cd208adSmatthias.ringwald     va_start(argptr, cmd);
10761cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
10771cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
10781cd208adSmatthias.ringwald     va_end(argptr);
10791cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
108093b8dc03Smatthias.ringwald }
1081c8e4258aSmatthias.ringwald 
1082ee091cf1Smatthias.ringwald // Create various non-HCI events.
1083ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1084ee091cf1Smatthias.ringwald 
1085c8e4258aSmatthias.ringwald void hci_emit_state(){
1086c8e4258aSmatthias.ringwald     uint8_t len = 3;
1087c8e4258aSmatthias.ringwald     uint8_t event[len];
108880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
10891b0e3922Smatthias.ringwald     event[1] = len - 3;
1090c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1091c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
10922718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1093c8e4258aSmatthias.ringwald }
1094c8e4258aSmatthias.ringwald 
1095c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
1096c8e4258aSmatthias.ringwald     uint8_t len = 13;
1097c8e4258aSmatthias.ringwald     uint8_t event[len];
1098c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
10991b0e3922Smatthias.ringwald     event[1] = len - 3;
1100c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
1101c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1102c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1103c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1104c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1105c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11062718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1107c8e4258aSmatthias.ringwald }
1108c8e4258aSmatthias.ringwald 
11093c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
11103c4d4b90Smatthias.ringwald     uint8_t len = 6;
11113c4d4b90Smatthias.ringwald     uint8_t event[len];
11123c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
11133c4d4b90Smatthias.ringwald     event[1] = len - 3;
11143c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
11153c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
11163c4d4b90Smatthias.ringwald     event[5] = reason;
11173c4d4b90Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11183c4d4b90Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
11193c4d4b90Smatthias.ringwald }
11203c4d4b90Smatthias.ringwald 
1121ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1122ee091cf1Smatthias.ringwald     uint8_t len = 4;
1123ee091cf1Smatthias.ringwald     uint8_t event[len];
112480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
11251b0e3922Smatthias.ringwald     event[1] = len - 2;
1126ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1127ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11282718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1129ee091cf1Smatthias.ringwald }
113043bfb1bdSmatthias.ringwald 
113143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
113243bfb1bdSmatthias.ringwald     uint8_t len = 3;
113343bfb1bdSmatthias.ringwald     uint8_t event[len];
113480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
11351b0e3922Smatthias.ringwald     event[1] = len - 2;
113643bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
113743bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11382718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
113943bfb1bdSmatthias.ringwald }
1140038bc64cSmatthias.ringwald 
1141038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
11421b0e3922Smatthias.ringwald     uint8_t len = 2;
1143038bc64cSmatthias.ringwald     uint8_t event[len];
114480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
11451b0e3922Smatthias.ringwald     event[1] = len - 2;
1146038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11472718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1148038bc64cSmatthias.ringwald }
11491b0e3922Smatthias.ringwald 
11501b0e3922Smatthias.ringwald 
11511b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
11521b0e3922Smatthias.ringwald     uint8_t len = 6;
11531b0e3922Smatthias.ringwald     uint8_t event[len];
11541b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
11551b0e3922Smatthias.ringwald     event[1] = len - 2;
11561b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
11571b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
11581b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
11591b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11602718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
11611b0e3922Smatthias.ringwald }
11621b0e3922Smatthias.ringwald 
11632ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
11642ed6235cSmatthias.ringwald     uint8_t len = 3;
11652ed6235cSmatthias.ringwald     uint8_t event[len];
11662ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1167ddce1d8dSmatthias.ringwald     event[1] = len - 2;
11682ed6235cSmatthias.ringwald     event[2] = enabled;
11692ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
11702718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
11712ed6235cSmatthias.ringwald }
1172627c2f45Smatthias.ringwald 
1173627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1174f653b6bdSmatthias.ringwald     uint16_t len = 2+1+6+248;
1175627c2f45Smatthias.ringwald     uint8_t event[len];
1176627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1177627c2f45Smatthias.ringwald     event[1] = len - 2;
1178f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
11792f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1180f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1181627c2f45Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, len);
1182627c2f45Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1183627c2f45Smatthias.ringwald }
1184381fbed8Smatthias.ringwald 
1185381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1186381fbed8Smatthias.ringwald     uint8_t len = 3;
1187381fbed8Smatthias.ringwald     uint8_t event[len];
1188381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1189381fbed8Smatthias.ringwald     event[1] = len - 2;
1190381fbed8Smatthias.ringwald     event[2] = enabled;
1191381fbed8Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
1192381fbed8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
1193381fbed8Smatthias.ringwald }
1194