xref: /btstack/src/hci.c (revision f653b6bd934d7b010ab1ee878e5d8ac75db61b22)
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 
41475c8125Smatthias.ringwald #include <unistd.h>
4293b8dc03Smatthias.ringwald #include <stdarg.h>
4393b8dc03Smatthias.ringwald #include <string.h>
4456fe0872Smatthias.ringwald #include <stdio.h>
457f2435e6Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "debug.h"
47d8905019Smatthias.ringwald #include "hci_dump.h"
4893b8dc03Smatthias.ringwald 
492ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h"
502ed6235cSmatthias.ringwald #include "../include/btstack/version.h"
511b0e3922Smatthias.ringwald 
521e6aba47Smatthias.ringwald // temp
531e6aba47Smatthias.ringwald #include "l2cap.h"
541e6aba47Smatthias.ringwald 
55169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
56ee091cf1Smatthias.ringwald 
5706b35ec0Smatthias.ringwald // the STACK is here
5816833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
5916833f0aSmatthias.ringwald 
6097addcc5Smatthias.ringwald /**
61ee091cf1Smatthias.ringwald  * get connection for a given handle
62ee091cf1Smatthias.ringwald  *
63ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
64ee091cf1Smatthias.ringwald  */
65645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
66ee091cf1Smatthias.ringwald     linked_item_t *it;
67ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
68ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
69ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
70ee091cf1Smatthias.ringwald         }
71ee091cf1Smatthias.ringwald     }
72ee091cf1Smatthias.ringwald     return NULL;
73ee091cf1Smatthias.ringwald }
74ee091cf1Smatthias.ringwald 
75981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
76ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
77ee091cf1Smatthias.ringwald     struct timeval tv;
78ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
79c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
80ee091cf1Smatthias.ringwald         // connections might be timed out
81ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
82c21e6239Smatthias.ringwald         run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
83ee091cf1Smatthias.ringwald     } else {
84ee091cf1Smatthias.ringwald         // next timeout check at
85c21e6239Smatthias.ringwald         timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
86ee091cf1Smatthias.ringwald     }
87ee091cf1Smatthias.ringwald     run_loop_add_timer(timer);
88ee091cf1Smatthias.ringwald }
89ee091cf1Smatthias.ringwald 
90ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
91ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
92ee091cf1Smatthias.ringwald }
93ee091cf1Smatthias.ringwald 
94ee091cf1Smatthias.ringwald /**
95c8e4258aSmatthias.ringwald  * create connection for given address
96c8e4258aSmatthias.ringwald  *
97c8e4258aSmatthias.ringwald  * @return connection OR NULL, if not found
98c8e4258aSmatthias.ringwald  */
99c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
100c8e4258aSmatthias.ringwald     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
101c8e4258aSmatthias.ringwald     if (!conn) return NULL;
102c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
103c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1047fde4af9Smatthias.ringwald     conn->authentication_flags = 0;
105ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
106ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
107ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
108d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1097856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
11056cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
111c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
112c8e4258aSmatthias.ringwald     return conn;
113c8e4258aSmatthias.ringwald }
114c8e4258aSmatthias.ringwald 
115c8e4258aSmatthias.ringwald /**
11606b35ec0Smatthias.ringwald  * get connection for given address
11797addcc5Smatthias.ringwald  *
11897addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
11997addcc5Smatthias.ringwald  */
120fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
12106b35ec0Smatthias.ringwald     linked_item_t *it;
12206b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
12306b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
12406b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
12506b35ec0Smatthias.ringwald         }
12606b35ec0Smatthias.ringwald     }
12706b35ec0Smatthias.ringwald     return NULL;
12806b35ec0Smatthias.ringwald }
12906b35ec0Smatthias.ringwald 
13043bfb1bdSmatthias.ringwald /**
13180ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1327fde4af9Smatthias.ringwald  */
1337fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1347fde4af9Smatthias.ringwald     bd_addr_t addr;
1357fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1367fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1377fde4af9Smatthias.ringwald     if (conn) {
1387fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
13980ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1407fde4af9Smatthias.ringwald     }
1417fde4af9Smatthias.ringwald }
1427fde4af9Smatthias.ringwald 
14380ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
14480ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
14580ca58a0Smatthias.ringwald     if (!conn) return 0;
14680ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
14780ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
14880ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
14980ca58a0Smatthias.ringwald     return 1;
15080ca58a0Smatthias.ringwald }
15180ca58a0Smatthias.ringwald 
152c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
153c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
154c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
155c12e46e7Smatthias.ringwald     }
156c12e46e7Smatthias.ringwald }
157c12e46e7Smatthias.ringwald 
1587fde4af9Smatthias.ringwald 
1597fde4af9Smatthias.ringwald /**
16043bfb1bdSmatthias.ringwald  * count connections
16143bfb1bdSmatthias.ringwald  */
16243bfb1bdSmatthias.ringwald static int nr_hci_connections(){
16356c253c9Smatthias.ringwald     int count = 0;
16443bfb1bdSmatthias.ringwald     linked_item_t *it;
16543bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
16643bfb1bdSmatthias.ringwald     return count;
16743bfb1bdSmatthias.ringwald }
168c8e4258aSmatthias.ringwald 
16997addcc5Smatthias.ringwald /**
170ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
17116833f0aSmatthias.ringwald  */
1722718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
17316833f0aSmatthias.ringwald }
17416833f0aSmatthias.ringwald 
175998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
176998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
177998906cdSmatthias.ringwald     if (!connection) {
178998906cdSmatthias.ringwald         log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
179998906cdSmatthias.ringwald         return 0;
180998906cdSmatthias.ringwald     }
181998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
182998906cdSmatthias.ringwald }
183998906cdSmatthias.ringwald 
184998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
185998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
186998906cdSmatthias.ringwald     linked_item_t *it;
187998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
188998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
189998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
190998906cdSmatthias.ringwald             log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
191998906cdSmatthias.ringwald             return 0;
192998906cdSmatthias.ringwald         }
193998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
194998906cdSmatthias.ringwald     }
195998906cdSmatthias.ringwald     return free_slots;
196998906cdSmatthias.ringwald }
197998906cdSmatthias.ringwald 
198998906cdSmatthias.ringwald int hci_ready_to_send(hci_con_handle_t handle){
199998906cdSmatthias.ringwald     return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2;
200998906cdSmatthias.ringwald }
201c8e4258aSmatthias.ringwald 
202ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2037856c818Smatthias.ringwald 
2046218e6f1Smatthias.ringwald     // check for free places on BT module
2056218e6f1Smatthias.ringwald     if (!hci_number_free_acl_slots()) return -1;
2066218e6f1Smatthias.ringwald 
2077856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2087856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
20956cf178bSmatthias.ringwald     if (!connection) return 0;
21056cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
21156cf178bSmatthias.ringwald 
21256cf178bSmatthias.ringwald     // count packet
21356cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2148ea03fa5Smatthias.ringwald     // log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2157856c818Smatthias.ringwald 
2166218e6f1Smatthias.ringwald     // send packet - ignore errors
217622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2186218e6f1Smatthias.ringwald 
2196218e6f1Smatthias.ringwald     return 0;
220ee091cf1Smatthias.ringwald }
221ee091cf1Smatthias.ringwald 
22216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2237856c818Smatthias.ringwald 
2247856c818Smatthias.ringwald     // get info
2257856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2267856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2277856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2287856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2297856c818Smatthias.ringwald 
2307856c818Smatthias.ringwald     // ignore non-registered handle
2317856c818Smatthias.ringwald     if (!conn){
2327f2435e6Smatthias.ringwald         log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2337856c818Smatthias.ringwald         return;
2347856c818Smatthias.ringwald     }
2357856c818Smatthias.ringwald 
2367856c818Smatthias.ringwald     // update idle timestamp
2377856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2387856c818Smatthias.ringwald 
2397856c818Smatthias.ringwald     // handle different packet types
2407856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2417856c818Smatthias.ringwald 
2427856c818Smatthias.ringwald         case 0x01: // continuation fragment
2437856c818Smatthias.ringwald 
2447856c818Smatthias.ringwald             // sanity check
2457856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2467f2435e6Smatthias.ringwald                 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2477856c818Smatthias.ringwald                 return;
2487856c818Smatthias.ringwald             }
2497856c818Smatthias.ringwald 
2507856c818Smatthias.ringwald             // append fragment payload (header already stored)
2517856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2527856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2537856c818Smatthias.ringwald 
2547f2435e6Smatthias.ringwald             // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
2557856c818Smatthias.ringwald             //        acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
2567856c818Smatthias.ringwald 
2577856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2587856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2597856c818Smatthias.ringwald 
2602718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2617856c818Smatthias.ringwald                 // reset recombination buffer
2627856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2637856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2647856c818Smatthias.ringwald             }
2657856c818Smatthias.ringwald             break;
2667856c818Smatthias.ringwald 
2677856c818Smatthias.ringwald         case 0x02: { // first fragment
2687856c818Smatthias.ringwald 
2697856c818Smatthias.ringwald             // sanity check
2707856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
2717f2435e6Smatthias.ringwald                 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
2727856c818Smatthias.ringwald                 return;
2737856c818Smatthias.ringwald             }
2747856c818Smatthias.ringwald 
2757856c818Smatthias.ringwald             // peek into L2CAP packet!
2767856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
2777856c818Smatthias.ringwald 
2787856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
2797856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
2807856c818Smatthias.ringwald 
2817856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
2822718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
2837856c818Smatthias.ringwald 
2847856c818Smatthias.ringwald             } else {
2857856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
2867856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
2877856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
2887856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
2897856c818Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
2907f2435e6Smatthias.ringwald                 // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
2917856c818Smatthias.ringwald             }
2927856c818Smatthias.ringwald             break;
2937856c818Smatthias.ringwald 
2947856c818Smatthias.ringwald         }
2957856c818Smatthias.ringwald         default:
2967f2435e6Smatthias.ringwald             log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
2977856c818Smatthias.ringwald             return;
2987856c818Smatthias.ringwald     }
29994ab26f8Smatthias.ringwald 
30094ab26f8Smatthias.ringwald     // execute main loop
30194ab26f8Smatthias.ringwald     hci_run();
30216833f0aSmatthias.ringwald }
30322909952Smatthias.ringwald 
30474ec757aSmatthias.ringwald // avoid huge local variables
30574ec757aSmatthias.ringwald static device_name_t device_name;
30616833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3071281a47eSmatthias.ringwald     bd_addr_t addr;
308fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3091f7b95a1Smatthias.ringwald     hci_connection_t * conn;
31056cf178bSmatthias.ringwald     int i;
31129d53098Smatthias.ringwald     link_key_t link_key;
31222909952Smatthias.ringwald 
313e2edc0c3Smatthias.ringwald     // get num_cmd_packets
314e2edc0c3Smatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
315e2edc0c3Smatthias.ringwald         // Get Num_HCI_Command_Packets
316e2edc0c3Smatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
317e2edc0c3Smatthias.ringwald     }
318e2edc0c3Smatthias.ringwald 
3196772a24cSmatthias.ringwald     switch (packet[0]) {
32022909952Smatthias.ringwald 
3216772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
322e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
323e2edc0c3Smatthias.ringwald                 // from offset 5
324e2edc0c3Smatthias.ringwald                 // status
325e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
32656cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
327e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
32856cf178bSmatthias.ringwald                 // ignore: total num SCO packets
32956cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
330e2edc0c3Smatthias.ringwald                     log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
331e2edc0c3Smatthias.ringwald                 }
33256cf178bSmatthias.ringwald             }
33356cf178bSmatthias.ringwald             break;
33456cf178bSmatthias.ringwald 
33556cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
33656cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
33756cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
33856cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
33956cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
34056cf178bSmatthias.ringwald                 if (!conn){
34156cf178bSmatthias.ringwald                     log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
34256cf178bSmatthias.ringwald                     continue;
34356cf178bSmatthias.ringwald                 }
34456cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
3458ea03fa5Smatthias.ringwald                 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
34656cf178bSmatthias.ringwald             }
3476772a24cSmatthias.ringwald             break;
3486772a24cSmatthias.ringwald 
3491f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
35037eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
35137eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
35237eaa4cfSmatthias.ringwald             uint8_t link_type = packet[11];
353cf0b66f0Smatthias.ringwald             log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type);
35437eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
3551f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
3561f7b95a1Smatthias.ringwald                 if (!conn) {
3571f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
3581f7b95a1Smatthias.ringwald                 }
3591f7b95a1Smatthias.ringwald                 // TODO: check for malloc failure
3601f7b95a1Smatthias.ringwald                 conn->state = ACCEPTED_CONNECTION_REQUEST;
36137eaa4cfSmatthias.ringwald                 hci_send_cmd(&hci_accept_connection_request, addr, 1);
36237eaa4cfSmatthias.ringwald             } else {
36337eaa4cfSmatthias.ringwald                 // TODO: decline request
36437eaa4cfSmatthias.ringwald             }
3651f7b95a1Smatthias.ringwald             break;
3661f7b95a1Smatthias.ringwald 
3676772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
368fe1ed1b8Smatthias.ringwald             // Connection management
369fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
370cf0b66f0Smatthias.ringwald             log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
3711f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
372fe1ed1b8Smatthias.ringwald             if (conn) {
373b448a0e7Smatthias.ringwald                 if (!packet[2]){
374c8e4258aSmatthias.ringwald                     conn->state = OPEN;
375fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
376ee091cf1Smatthias.ringwald 
377ee091cf1Smatthias.ringwald                     gettimeofday(&conn->timestamp, NULL);
378c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
379ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
380ee091cf1Smatthias.ringwald 
381cf0b66f0Smatthias.ringwald                     log_dbg("New connection: handle %u, ", conn->con_handle);
382fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
383cf0b66f0Smatthias.ringwald                     log_dbg("\n");
38443bfb1bdSmatthias.ringwald 
38543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
386b448a0e7Smatthias.ringwald                 } else {
387b448a0e7Smatthias.ringwald                     // connection failed, remove entry
388b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
389b448a0e7Smatthias.ringwald                     free( conn );
390c12e46e7Smatthias.ringwald 
391c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
392c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
393c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
394c12e46e7Smatthias.ringwald                     }
395fe1ed1b8Smatthias.ringwald                 }
396fe1ed1b8Smatthias.ringwald             }
3976772a24cSmatthias.ringwald             break;
398fe1ed1b8Smatthias.ringwald 
3997fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
4007fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
40174ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
40229d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
40329d53098Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &addr, &link_key)){
40429d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_reply, &addr, &link_key);
40529d53098Smatthias.ringwald             } else {
40629d53098Smatthias.ringwald                 hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
40729d53098Smatthias.ringwald             }
40829d53098Smatthias.ringwald             // request already answered
40929d53098Smatthias.ringwald             return;
4107fde4af9Smatthias.ringwald 
4117fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
4127fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
41374ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
41429d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
41529d53098Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, &link_key);
41629d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
4177fde4af9Smatthias.ringwald             break;
4187fde4af9Smatthias.ringwald 
4197fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4207fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
4217fde4af9Smatthias.ringwald             break;
4227fde4af9Smatthias.ringwald 
42374ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
42474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
42574ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
42674ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
42774ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
42874ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
42974ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
43074ec757aSmatthias.ringwald             break;
43174ec757aSmatthias.ringwald 
43274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
43374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
43474ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
43574ec757aSmatthias.ringwald             // first send inq result packet
43674ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
43774ec757aSmatthias.ringwald             // then send cached remote names
43874ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
43974ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
44074ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
44174ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
44274ec757aSmatthias.ringwald                 }
44374ec757aSmatthias.ringwald             }
44474ec757aSmatthias.ringwald             return;
44574ec757aSmatthias.ringwald 
4466772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
447fe1ed1b8Smatthias.ringwald             if (!packet[2]){
448fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
449fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
450fe1ed1b8Smatthias.ringwald                 if (conn) {
451cf0b66f0Smatthias.ringwald                     log_dbg("Connection closed: handle %u, ", conn->con_handle);
452fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
453cf0b66f0Smatthias.ringwald                     log_dbg("\n");
454ee091cf1Smatthias.ringwald                     run_loop_remove_timer(&conn->timeout);
455fe1ed1b8Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
456fe1ed1b8Smatthias.ringwald                     free( conn );
45743bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
458fe1ed1b8Smatthias.ringwald                 }
459fe1ed1b8Smatthias.ringwald             }
4606772a24cSmatthias.ringwald             break;
4616772a24cSmatthias.ringwald 
4626772a24cSmatthias.ringwald         default:
4636772a24cSmatthias.ringwald             break;
464fe1ed1b8Smatthias.ringwald     }
465fe1ed1b8Smatthias.ringwald 
4663429f56bSmatthias.ringwald     // handle BT initialization
4673429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
4687301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
4697301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
4707301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
4717301ad89Smatthias.ringwald         // }
4727301ad89Smatthias.ringwald         // handle normal init sequence
4733429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
4743429f56bSmatthias.ringwald             // odd: waiting for event
4753429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
4763429f56bSmatthias.ringwald                 hci_stack.substate++;
4773429f56bSmatthias.ringwald             }
4783429f56bSmatthias.ringwald         }
47922909952Smatthias.ringwald     }
48022909952Smatthias.ringwald 
4812718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
48294ab26f8Smatthias.ringwald 
48394ab26f8Smatthias.ringwald 	// execute main loop
48494ab26f8Smatthias.ringwald 	hci_run();
48516833f0aSmatthias.ringwald }
48616833f0aSmatthias.ringwald 
48710e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
48810e830c9Smatthias.ringwald     switch (packet_type) {
48910e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
49010e830c9Smatthias.ringwald             event_handler(packet, size);
49110e830c9Smatthias.ringwald             break;
49210e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
49310e830c9Smatthias.ringwald             acl_handler(packet, size);
49410e830c9Smatthias.ringwald             break;
49510e830c9Smatthias.ringwald         default:
49610e830c9Smatthias.ringwald             break;
49710e830c9Smatthias.ringwald     }
49810e830c9Smatthias.ringwald }
49910e830c9Smatthias.ringwald 
500fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
5012718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
5022718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
50316833f0aSmatthias.ringwald }
50416833f0aSmatthias.ringwald 
505404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
506475c8125Smatthias.ringwald 
507475c8125Smatthias.ringwald     // reference to use transport layer implementation
50816833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
509475c8125Smatthias.ringwald 
51011e23e5fSmatthias.ringwald     // references to used control implementation
51111e23e5fSmatthias.ringwald     hci_stack.control = control;
51211e23e5fSmatthias.ringwald 
51311e23e5fSmatthias.ringwald     // reference to used config
51411e23e5fSmatthias.ringwald     hci_stack.config = config;
51511e23e5fSmatthias.ringwald 
516fe1ed1b8Smatthias.ringwald     // no connections yet
517fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
518fe1ed1b8Smatthias.ringwald 
51902ea9861Smatthias.ringwald     // empty cmd buffer
52016833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
52116833f0aSmatthias.ringwald 
52216833f0aSmatthias.ringwald     // higher level handler
5232718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
52416833f0aSmatthias.ringwald 
525404843c1Smatthias.ringwald     // store and open remote device db
526404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
527404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
528404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
529404843c1Smatthias.ringwald     }
53029d53098Smatthias.ringwald 
53116833f0aSmatthias.ringwald     // register packet handlers with transport
53210e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
533475c8125Smatthias.ringwald }
534475c8125Smatthias.ringwald 
535404843c1Smatthias.ringwald void hci_close(){
536404843c1Smatthias.ringwald     // close remote device db
537404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
538404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
539404843c1Smatthias.ringwald     }
540404843c1Smatthias.ringwald }
541404843c1Smatthias.ringwald 
542475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
543f12adbd6Smatthias.ringwald     if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
5447301ad89Smatthias.ringwald 
545038bc64cSmatthias.ringwald         // power on
546f9a30166Smatthias.ringwald         int err = 0;
547f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->on){
548f9a30166Smatthias.ringwald             err = (*hci_stack.control->on)(hci_stack.config);
549f9a30166Smatthias.ringwald         }
550038bc64cSmatthias.ringwald         if (err){
5517f2435e6Smatthias.ringwald             log_err( "POWER_ON failed\n");
552038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
553038bc64cSmatthias.ringwald             return err;
554038bc64cSmatthias.ringwald         }
555038bc64cSmatthias.ringwald 
556038bc64cSmatthias.ringwald         // open low-level device
557038bc64cSmatthias.ringwald         err = hci_stack.hci_transport->open(hci_stack.config);
558038bc64cSmatthias.ringwald         if (err){
5597f2435e6Smatthias.ringwald             log_err( "HCI_INIT failed, turning Bluetooth off again\n");
560f9a30166Smatthias.ringwald             if (hci_stack.control && hci_stack.control->off){
561f9a30166Smatthias.ringwald                 (*hci_stack.control->off)(hci_stack.config);
562f9a30166Smatthias.ringwald             }
563038bc64cSmatthias.ringwald             hci_emit_hci_open_failed();
564038bc64cSmatthias.ringwald             return err;
565038bc64cSmatthias.ringwald         }
566038bc64cSmatthias.ringwald 
5677301ad89Smatthias.ringwald         // set up state machine
5687301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
5697301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
5707301ad89Smatthias.ringwald         hci_stack.substate = 0;
5717301ad89Smatthias.ringwald 
572f12adbd6Smatthias.ringwald     } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
5737301ad89Smatthias.ringwald 
5747301ad89Smatthias.ringwald         // close low-level device
5757301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
5767301ad89Smatthias.ringwald 
5777301ad89Smatthias.ringwald         // power off
578f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
579f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
580f9a30166Smatthias.ringwald         }
58143bfb1bdSmatthias.ringwald 
58243bfb1bdSmatthias.ringwald         // we're off now
58343bfb1bdSmatthias.ringwald         hci_stack.state = HCI_STATE_OFF;
58411e23e5fSmatthias.ringwald     }
58568d92d03Smatthias.ringwald 
586038bc64cSmatthias.ringwald     // create internal event
587ee8bf225Smatthias.ringwald 	hci_emit_state();
588ee8bf225Smatthias.ringwald 
58968d92d03Smatthias.ringwald 	// trigger next/first action
59068d92d03Smatthias.ringwald 	hci_run();
59168d92d03Smatthias.ringwald 
592475c8125Smatthias.ringwald     return 0;
593475c8125Smatthias.ringwald }
594475c8125Smatthias.ringwald 
59506b35ec0Smatthias.ringwald void hci_run(){
5968a485f27Smatthias.ringwald 
5973429f56bSmatthias.ringwald     switch (hci_stack.state){
5983429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
5993429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
6003429f56bSmatthias.ringwald                 // odd: waiting for command completion
60106b35ec0Smatthias.ringwald                 return;
6023429f56bSmatthias.ringwald             }
6033429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
6043429f56bSmatthias.ringwald                 // cannot send command yet
60506b35ec0Smatthias.ringwald                 return;
6063429f56bSmatthias.ringwald             }
60790919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
6083429f56bSmatthias.ringwald                 case 0:
60922909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
6103429f56bSmatthias.ringwald                     break;
6113429f56bSmatthias.ringwald                 case 1:
61290919203Smatthias.ringwald                     // custom initialization
61390919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
61490919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
61590919203Smatthias.ringwald                         if (cmd) {
61690919203Smatthias.ringwald                             int size = 3 + cmd[2];
617622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
6182f6c30e1Smatthias.ringwald                             hci_stack.substate = 0; // more init commands
61990919203Smatthias.ringwald                             break;
62090919203Smatthias.ringwald                         }
62190919203Smatthias.ringwald                     }
6222f6c30e1Smatthias.ringwald                     // otherwise continue
623f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
624f432a6ddSmatthias.ringwald 					break;
6252f6c30e1Smatthias.ringwald 				case 2:
626e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
627e2edc0c3Smatthias.ringwald 					break;
6282f6c30e1Smatthias.ringwald                 case 3:
6293429f56bSmatthias.ringwald                     // ca. 15 sec
6303429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
6313429f56bSmatthias.ringwald                     break;
6322f6c30e1Smatthias.ringwald 				case 4:
633bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
634f432a6ddSmatthias.ringwald 					break;
6352f6c30e1Smatthias.ringwald                 case 5:
6368a485f27Smatthias.ringwald #ifndef EMBEDDED
6378a485f27Smatthias.ringwald                 {
6388a485f27Smatthias.ringwald                     char hostname[30];
6398a485f27Smatthias.ringwald                     gethostname(hostname, 30);
6408a485f27Smatthias.ringwald                     hostname[29] = '\0';
6418a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
6428a485f27Smatthias.ringwald                     break;
6438a485f27Smatthias.ringwald                 }
6448a485f27Smatthias.ringwald                 case 6:
6458a485f27Smatthias.ringwald #endif
6463429f56bSmatthias.ringwald                     // done.
6473429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
648b360b6adSmatthias.ringwald                     hci_emit_state();
6493429f56bSmatthias.ringwald                     break;
6503429f56bSmatthias.ringwald                 default:
6513429f56bSmatthias.ringwald                     break;
652475c8125Smatthias.ringwald             }
6533429f56bSmatthias.ringwald             hci_stack.substate++;
6543429f56bSmatthias.ringwald             break;
6553429f56bSmatthias.ringwald         default:
6563429f56bSmatthias.ringwald             break;
6571f504dbdSmatthias.ringwald     }
6583429f56bSmatthias.ringwald }
65916833f0aSmatthias.ringwald 
66031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
661c8e4258aSmatthias.ringwald     bd_addr_t addr;
662c8e4258aSmatthias.ringwald     hci_connection_t * conn;
663c8e4258aSmatthias.ringwald     // house-keeping
664c8e4258aSmatthias.ringwald 
665c8e4258aSmatthias.ringwald     // create_connection?
666c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
667c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
668cf0b66f0Smatthias.ringwald         log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
669c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
670c8e4258aSmatthias.ringwald         if (conn) {
671c8e4258aSmatthias.ringwald             // if connection exists
672c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
673c8e4258aSmatthias.ringwald                 // if OPEN, emit connection complete command
674c8e4258aSmatthias.ringwald                 hci_emit_connection_complete(conn);
675c8e4258aSmatthias.ringwald             }
676c8e4258aSmatthias.ringwald             //    otherwise, just ignore
677c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
678c8e4258aSmatthias.ringwald 
679c8e4258aSmatthias.ringwald         } else{
680c8e4258aSmatthias.ringwald             conn = create_connection_for_addr(addr);
681c8e4258aSmatthias.ringwald             if (conn){
682c8e4258aSmatthias.ringwald                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
683c8e4258aSmatthias.ringwald                 conn->state = SENT_CREATE_CONNECTION;
684c8e4258aSmatthias.ringwald             }
685c8e4258aSmatthias.ringwald         }
686c8e4258aSmatthias.ringwald     }
687c8e4258aSmatthias.ringwald 
6887fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
6897fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
6907fde4af9Smatthias.ringwald     }
6917fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
6927fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
6937fde4af9Smatthias.ringwald     }
6947fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
6957fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
6967fde4af9Smatthias.ringwald     }
6977fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
6987fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
6997fde4af9Smatthias.ringwald     }
7007fde4af9Smatthias.ringwald 
701c8e4258aSmatthias.ringwald     // accept connection
702c8e4258aSmatthias.ringwald 
703c8e4258aSmatthias.ringwald     // reject connection
704c8e4258aSmatthias.ringwald 
705c8e4258aSmatthias.ringwald     // close_connection?
706c8e4258aSmatthias.ringwald       // set state = SENT_DISCONNECT
707c8e4258aSmatthias.ringwald 
70831452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
709622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
71031452debSmatthias.ringwald }
7118adf0ddaSmatthias.ringwald 
7121cd208adSmatthias.ringwald /**
7131cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
7141cd208adSmatthias.ringwald  */
715fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
7161cd208adSmatthias.ringwald     va_list argptr;
7171cd208adSmatthias.ringwald     va_start(argptr, cmd);
7181cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
7191cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
7201cd208adSmatthias.ringwald     va_end(argptr);
7211cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
72293b8dc03Smatthias.ringwald }
723c8e4258aSmatthias.ringwald 
724ee091cf1Smatthias.ringwald // Create various non-HCI events.
725ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
726ee091cf1Smatthias.ringwald 
727c8e4258aSmatthias.ringwald void hci_emit_state(){
728c8e4258aSmatthias.ringwald     uint8_t len = 3;
729c8e4258aSmatthias.ringwald     uint8_t event[len];
73080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
7311b0e3922Smatthias.ringwald     event[1] = len - 3;
732c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
733c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
7342718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
735c8e4258aSmatthias.ringwald }
736c8e4258aSmatthias.ringwald 
737c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){
738c8e4258aSmatthias.ringwald     uint8_t len = 13;
739c8e4258aSmatthias.ringwald     uint8_t event[len];
740c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
7411b0e3922Smatthias.ringwald     event[1] = len - 3;
742c8e4258aSmatthias.ringwald     event[2] = 0; // status = OK
743c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
744c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
745c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
746c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
747c8e4258aSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
7482718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
749c8e4258aSmatthias.ringwald }
750c8e4258aSmatthias.ringwald 
751ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
752ee091cf1Smatthias.ringwald     uint8_t len = 4;
753ee091cf1Smatthias.ringwald     uint8_t event[len];
75480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
7551b0e3922Smatthias.ringwald     event[1] = len - 2;
756ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
757ee091cf1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
7582718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
759ee091cf1Smatthias.ringwald }
76043bfb1bdSmatthias.ringwald 
76143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
76243bfb1bdSmatthias.ringwald     uint8_t len = 3;
76343bfb1bdSmatthias.ringwald     uint8_t event[len];
76480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
7651b0e3922Smatthias.ringwald     event[1] = len - 2;
76643bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
76743bfb1bdSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
7682718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
76943bfb1bdSmatthias.ringwald }
770038bc64cSmatthias.ringwald 
771038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
7721b0e3922Smatthias.ringwald     uint8_t len = 2;
773038bc64cSmatthias.ringwald     uint8_t event[len];
77480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
7751b0e3922Smatthias.ringwald     event[1] = len - 2;
776038bc64cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
7772718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
778038bc64cSmatthias.ringwald }
7791b0e3922Smatthias.ringwald 
7801b0e3922Smatthias.ringwald 
7811b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
7821b0e3922Smatthias.ringwald     uint8_t len = 6;
7831b0e3922Smatthias.ringwald     uint8_t event[len];
7841b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
7851b0e3922Smatthias.ringwald     event[1] = len - 2;
7861b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MAJOR;
7871b0e3922Smatthias.ringwald     event[len++] = BTSTACK_MINOR;
7881b0e3922Smatthias.ringwald     bt_store_16(event, len, BTSTACK_REVISION);
7891b0e3922Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
7902718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
7911b0e3922Smatthias.ringwald }
7921b0e3922Smatthias.ringwald 
7932ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
7942ed6235cSmatthias.ringwald     uint8_t len = 3;
7952ed6235cSmatthias.ringwald     uint8_t event[len];
7962ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
797ddce1d8dSmatthias.ringwald     event[1] = len - 2;
7982ed6235cSmatthias.ringwald     event[2] = enabled;
7992ed6235cSmatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
8002718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
8012ed6235cSmatthias.ringwald }
802627c2f45Smatthias.ringwald 
803627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
804*f653b6bdSmatthias.ringwald     uint16_t len = 2+1+6+248;
805627c2f45Smatthias.ringwald     uint8_t event[len];
806627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
807627c2f45Smatthias.ringwald     event[1] = len - 2;
808*f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
809*f653b6bdSmatthias.ringwald     BD_ADDR_COPY(&event[3], addr);
810*f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
811627c2f45Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, len);
812627c2f45Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
813627c2f45Smatthias.ringwald }
814