xref: /btstack/src/hci.c (revision a7a04bd9eb920f465af67ce84f4d0c002b87b199)
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 
49a3b02b71Smatthias.ringwald #include "btstack_memory.h"
507f2435e6Smatthias.ringwald #include "debug.h"
51d8905019Smatthias.ringwald #include "hci_dump.h"
5293b8dc03Smatthias.ringwald 
53ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
54ae1fd9f3Smatthias.ringwald #include <btstack/version.h>
551b0e3922Smatthias.ringwald 
56a3b02b71Smatthias.ringwald // tmpe
571e6aba47Smatthias.ringwald #include "l2cap.h"
581e6aba47Smatthias.ringwald 
59169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
60ee091cf1Smatthias.ringwald 
6106b35ec0Smatthias.ringwald // the STACK is here
6216833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
6316833f0aSmatthias.ringwald 
6497addcc5Smatthias.ringwald /**
65ee091cf1Smatthias.ringwald  * get connection for a given handle
66ee091cf1Smatthias.ringwald  *
67ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
68ee091cf1Smatthias.ringwald  */
69645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
70ee091cf1Smatthias.ringwald     linked_item_t *it;
71ee091cf1Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
72ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
73ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
74ee091cf1Smatthias.ringwald         }
75ee091cf1Smatthias.ringwald     }
76ee091cf1Smatthias.ringwald     return NULL;
77ee091cf1Smatthias.ringwald }
78ee091cf1Smatthias.ringwald 
792b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
80ee091cf1Smatthias.ringwald     hci_connection_t * connection = linked_item_get_user(&timer->item);
81c785ef68Smatthias.ringwald #ifdef HAVE_TIME
82ee091cf1Smatthias.ringwald     struct timeval tv;
83ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
84c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
85ee091cf1Smatthias.ringwald         // connections might be timed out
86ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
87ee091cf1Smatthias.ringwald     }
882b12a0b9Smatthias.ringwald #endif
89c785ef68Smatthias.ringwald #ifdef EMBEDDED
90c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
91c785ef68Smatthias.ringwald         // connections might be timed out
92c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
93c785ef68Smatthias.ringwald     }
94c785ef68Smatthias.ringwald #endif
95c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
96c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
97c785ef68Smatthias.ringwald }
98ee091cf1Smatthias.ringwald 
99ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
100c7492964Smatthias.ringwald #ifdef HAVE_TIME
101ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
102c7492964Smatthias.ringwald #endif
103c785ef68Smatthias.ringwald #ifdef EMBEDDED
104c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
105c785ef68Smatthias.ringwald #endif
106ee091cf1Smatthias.ringwald }
107ee091cf1Smatthias.ringwald 
108ee091cf1Smatthias.ringwald /**
109c8e4258aSmatthias.ringwald  * create connection for given address
110c8e4258aSmatthias.ringwald  *
11117f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
112c8e4258aSmatthias.ringwald  */
113c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
114a3b02b71Smatthias.ringwald     hci_connection_t * conn = btstack_memory_hci_connection_get();
115c8e4258aSmatthias.ringwald     if (!conn) return NULL;
116c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
117c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1187d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
119ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
120ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
121ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
122d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1237856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
12456cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
125c8e4258aSmatthias.ringwald     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
126c8e4258aSmatthias.ringwald     return conn;
127c8e4258aSmatthias.ringwald }
128c8e4258aSmatthias.ringwald 
129c8e4258aSmatthias.ringwald /**
13006b35ec0Smatthias.ringwald  * get connection for given address
13197addcc5Smatthias.ringwald  *
13297addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
13397addcc5Smatthias.ringwald  */
134fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
13506b35ec0Smatthias.ringwald     linked_item_t *it;
13606b35ec0Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
13706b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
13806b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
13906b35ec0Smatthias.ringwald         }
14006b35ec0Smatthias.ringwald     }
14106b35ec0Smatthias.ringwald     return NULL;
14206b35ec0Smatthias.ringwald }
14306b35ec0Smatthias.ringwald 
14443bfb1bdSmatthias.ringwald /**
14580ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1467fde4af9Smatthias.ringwald  */
1477fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1487fde4af9Smatthias.ringwald     bd_addr_t addr;
1497fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1507fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1517fde4af9Smatthias.ringwald     if (conn) {
1527fde4af9Smatthias.ringwald         conn->authentication_flags |= flags;
15380ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1547fde4af9Smatthias.ringwald     }
1557fde4af9Smatthias.ringwald }
1567fde4af9Smatthias.ringwald 
15780ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
15880ca58a0Smatthias.ringwald     hci_connection_t * conn = connection_for_handle(handle);
15980ca58a0Smatthias.ringwald     if (!conn) return 0;
16080ca58a0Smatthias.ringwald     if (!conn->authentication_flags) return 0;
16180ca58a0Smatthias.ringwald     if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
16280ca58a0Smatthias.ringwald     if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
16380ca58a0Smatthias.ringwald     return 1;
16480ca58a0Smatthias.ringwald }
16580ca58a0Smatthias.ringwald 
166c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
167c12e46e7Smatthias.ringwald     if (hci_stack.remote_device_db) {
168c12e46e7Smatthias.ringwald         hci_stack.remote_device_db->delete_link_key(addr);
169c12e46e7Smatthias.ringwald     }
170c12e46e7Smatthias.ringwald }
171c12e46e7Smatthias.ringwald 
1727fde4af9Smatthias.ringwald 
1737fde4af9Smatthias.ringwald /**
17443bfb1bdSmatthias.ringwald  * count connections
17543bfb1bdSmatthias.ringwald  */
17640d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
17756c253c9Smatthias.ringwald     int count = 0;
17843bfb1bdSmatthias.ringwald     linked_item_t *it;
17943bfb1bdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
18043bfb1bdSmatthias.ringwald     return count;
18143bfb1bdSmatthias.ringwald }
182c8e4258aSmatthias.ringwald 
18397addcc5Smatthias.ringwald /**
184ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
18516833f0aSmatthias.ringwald  */
1862718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
18716833f0aSmatthias.ringwald }
18816833f0aSmatthias.ringwald 
189998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
190998906cdSmatthias.ringwald     hci_connection_t * connection = connection_for_handle(handle);
191998906cdSmatthias.ringwald     if (!connection) {
1927d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
193998906cdSmatthias.ringwald         return 0;
194998906cdSmatthias.ringwald     }
195998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
196998906cdSmatthias.ringwald }
197998906cdSmatthias.ringwald 
198998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
199998906cdSmatthias.ringwald     uint8_t free_slots = hci_stack.total_num_acl_packets;
200998906cdSmatthias.ringwald     linked_item_t *it;
201998906cdSmatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
202998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
203998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2047d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
205998906cdSmatthias.ringwald             return 0;
206998906cdSmatthias.ringwald         }
207998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
208998906cdSmatthias.ringwald     }
209998906cdSmatthias.ringwald     return free_slots;
210998906cdSmatthias.ringwald }
211998906cdSmatthias.ringwald 
2125250fb9eSmatthias.ringwald uint16_t hci_max_acl_data_packet_length(){
2135250fb9eSmatthias.ringwald     return hci_stack.acl_data_packet_length;
2145250fb9eSmatthias.ringwald }
2155250fb9eSmatthias.ringwald 
216c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2172b12a0b9Smatthias.ringwald 
2182b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2192b12a0b9Smatthias.ringwald     if (hci_stack.hci_transport->can_send_packet_now){
2202b12a0b9Smatthias.ringwald         if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
2212b12a0b9Smatthias.ringwald             return 0;
2222b12a0b9Smatthias.ringwald         }
2232b12a0b9Smatthias.ringwald     }
2242b12a0b9Smatthias.ringwald 
2252b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
226c24735b1Smatthias.ringwald     switch (packet_type) {
227c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
228c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
229c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
230de009a8cSmatthias.ringwald             return hci_stack.num_cmd_packets;
231c24735b1Smatthias.ringwald         default:
232c24735b1Smatthias.ringwald             return 0;
233c24735b1Smatthias.ringwald     }
234c24735b1Smatthias.ringwald }
235c24735b1Smatthias.ringwald 
236ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2377856c818Smatthias.ringwald 
2386218e6f1Smatthias.ringwald     // check for free places on BT module
2395932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2406218e6f1Smatthias.ringwald 
2417856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2427856c818Smatthias.ringwald     hci_connection_t *connection = connection_for_handle( con_handle);
24356cf178bSmatthias.ringwald     if (!connection) return 0;
24456cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
24556cf178bSmatthias.ringwald 
24656cf178bSmatthias.ringwald     // count packet
24756cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2487b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2497856c818Smatthias.ringwald 
2506218e6f1Smatthias.ringwald     // send packet - ignore errors
251622d0de9Smatthias.ringwald     hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2526218e6f1Smatthias.ringwald 
2536218e6f1Smatthias.ringwald     return 0;
254ee091cf1Smatthias.ringwald }
255ee091cf1Smatthias.ringwald 
25616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2577856c818Smatthias.ringwald 
2587856c818Smatthias.ringwald     // get info
2597856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2607856c818Smatthias.ringwald     hci_connection_t *conn      = connection_for_handle(con_handle);
2617856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2627856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2637856c818Smatthias.ringwald 
2647856c818Smatthias.ringwald     // ignore non-registered handle
2657856c818Smatthias.ringwald     if (!conn){
2667d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
2677856c818Smatthias.ringwald         return;
2687856c818Smatthias.ringwald     }
2697856c818Smatthias.ringwald 
2707856c818Smatthias.ringwald     // update idle timestamp
2717856c818Smatthias.ringwald     hci_connection_timestamp(conn);
2727856c818Smatthias.ringwald 
2737856c818Smatthias.ringwald     // handle different packet types
2747856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
2757856c818Smatthias.ringwald 
2767856c818Smatthias.ringwald         case 0x01: // continuation fragment
2777856c818Smatthias.ringwald 
2787856c818Smatthias.ringwald             // sanity check
2797856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
2807d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
2817856c818Smatthias.ringwald                 return;
2827856c818Smatthias.ringwald             }
2837856c818Smatthias.ringwald 
2847856c818Smatthias.ringwald             // append fragment payload (header already stored)
2857856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
2867856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
2877856c818Smatthias.ringwald 
2887d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
289decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
2907856c818Smatthias.ringwald 
2917856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
2927856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
2937856c818Smatthias.ringwald 
2942718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
2957856c818Smatthias.ringwald                 // reset recombination buffer
2967856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
2977856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
2987856c818Smatthias.ringwald             }
2997856c818Smatthias.ringwald             break;
3007856c818Smatthias.ringwald 
3017856c818Smatthias.ringwald         case 0x02: { // first fragment
3027856c818Smatthias.ringwald 
3037856c818Smatthias.ringwald             // sanity check
3047856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3057d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3067856c818Smatthias.ringwald                 return;
3077856c818Smatthias.ringwald             }
3087856c818Smatthias.ringwald 
3097856c818Smatthias.ringwald             // peek into L2CAP packet!
3107856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3117856c818Smatthias.ringwald 
3127d67539fSmatthias.ringwald             // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
313decc01a8Smatthias.ringwald 
3147856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3157856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3167856c818Smatthias.ringwald 
3177856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3182718e2e7Smatthias.ringwald                 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3197856c818Smatthias.ringwald 
3207856c818Smatthias.ringwald             } else {
3217856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3227856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3237856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3247856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
325decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3267856c818Smatthias.ringwald             }
3277856c818Smatthias.ringwald             break;
3287856c818Smatthias.ringwald 
3297856c818Smatthias.ringwald         }
3307856c818Smatthias.ringwald         default:
3317d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3327856c818Smatthias.ringwald             return;
3337856c818Smatthias.ringwald     }
33494ab26f8Smatthias.ringwald 
33594ab26f8Smatthias.ringwald     // execute main loop
33694ab26f8Smatthias.ringwald     hci_run();
33716833f0aSmatthias.ringwald }
33822909952Smatthias.ringwald 
33967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
3407b5fbe1fSmatthias.ringwald     log_info("Connection closed: handle %u, ", conn->con_handle);
341c7e0c5f6Smatthias.ringwald     print_bd_addr( conn->address );
3427b5fbe1fSmatthias.ringwald     log_info("\n");
3433c4d4b90Smatthias.ringwald 
3443c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3453c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3463c4d4b90Smatthias.ringwald 
347c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
348c785ef68Smatthias.ringwald 
349c7e0c5f6Smatthias.ringwald     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
350a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3513c4d4b90Smatthias.ringwald 
3523c4d4b90Smatthias.ringwald     // now it's gone
353c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
354c7e0c5f6Smatthias.ringwald }
355c7e0c5f6Smatthias.ringwald 
3568f8108aaSmatthias.ringwald static uint16_t packet_type_sizes[] = {
3578f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3588f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3598f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
3608f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
3618f8108aaSmatthias.ringwald };
3628f8108aaSmatthias.ringwald 
3638f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){
3648f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
3658f8108aaSmatthias.ringwald     int i;
3668f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
3678f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
3688f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
3698f8108aaSmatthias.ringwald             packet_types |= 1 << i;
3708f8108aaSmatthias.ringwald         }
3718f8108aaSmatthias.ringwald     }
3728f8108aaSmatthias.ringwald     // flip bits for "may not be used"
3738f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
3748f8108aaSmatthias.ringwald     return packet_types;
3758f8108aaSmatthias.ringwald }
3768f8108aaSmatthias.ringwald 
3778f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
3788f8108aaSmatthias.ringwald     return hci_stack.packet_types;
3798f8108aaSmatthias.ringwald }
3808f8108aaSmatthias.ringwald 
38174ec757aSmatthias.ringwald // avoid huge local variables
38274ec757aSmatthias.ringwald static device_name_t device_name;
38316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
3841281a47eSmatthias.ringwald     bd_addr_t addr;
3852d00edd4Smatthias.ringwald     uint8_t link_type;
386fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
3871f7b95a1Smatthias.ringwald     hci_connection_t * conn;
38856cf178bSmatthias.ringwald     int i;
38922909952Smatthias.ringwald 
3906772a24cSmatthias.ringwald     switch (packet[0]) {
39122909952Smatthias.ringwald 
3926772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
3937ec5eeaaSmatthias.ringwald             // get num cmd packets
3947b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
3957ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[2];
3967ec5eeaaSmatthias.ringwald 
397e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
398e2edc0c3Smatthias.ringwald                 // from offset 5
399e2edc0c3Smatthias.ringwald                 // status
4001d279b20Smatthias.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"
401e2edc0c3Smatthias.ringwald                 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
40256cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
403e2edc0c3Smatthias.ringwald                 hci_stack.total_num_acl_packets  = packet[9];
40456cf178bSmatthias.ringwald                 // ignore: total num SCO packets
40556cf178bSmatthias.ringwald                 if (hci_stack.state == HCI_STATE_INITIALIZING){
406*a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
407*a7a04bd9Smatthias.ringwald                     if (HCI_ACL_BUFFER_SIZE < hci_stack.acl_data_packet_length){
408*a7a04bd9Smatthias.ringwald                         hci_stack.acl_data_packet_length = HCI_ACL_BUFFER_SIZE;
4098f8108aaSmatthias.ringwald                     }
410*a7a04bd9Smatthias.ringwald                     // determine usable ACL packet types
4118f8108aaSmatthias.ringwald                     hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(max_acl_payload);
4128f8108aaSmatthias.ringwald 
4138f8108aaSmatthias.ringwald                     log_error("hci_read_buffer_size: size %u, count %u, packet types %04x\n",
4148f8108aaSmatthias.ringwald                              hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types);
415e2edc0c3Smatthias.ringwald                 }
41656cf178bSmatthias.ringwald             }
417381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
418381fbed8Smatthias.ringwald                 hci_emit_discoverable_enabled(hci_stack.discoverable);
419381fbed8Smatthias.ringwald             }
42056cf178bSmatthias.ringwald             break;
42156cf178bSmatthias.ringwald 
4227ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
4237ec5eeaaSmatthias.ringwald             // get num cmd packets
4247b5fbe1fSmatthias.ringwald             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
4257ec5eeaaSmatthias.ringwald             hci_stack.num_cmd_packets = packet[3];
4267ec5eeaaSmatthias.ringwald             break;
4277ec5eeaaSmatthias.ringwald 
42856cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
42956cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
43056cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
43156cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
43256cf178bSmatthias.ringwald                 conn = connection_for_handle(handle);
43356cf178bSmatthias.ringwald                 if (!conn){
4347d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
43556cf178bSmatthias.ringwald                     continue;
43656cf178bSmatthias.ringwald                 }
43756cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
4387b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
43956cf178bSmatthias.ringwald             }
4406772a24cSmatthias.ringwald             break;
4416772a24cSmatthias.ringwald 
4421f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
44337eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
44437eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
4452d00edd4Smatthias.ringwald             link_type = packet[11];
4467b5fbe1fSmatthias.ringwald             log_info("Connection_incoming: "); print_bd_addr(addr); log_info(", type %u\n", link_type);
44737eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
4481f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
4491f7b95a1Smatthias.ringwald                 if (!conn) {
4501f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
4511f7b95a1Smatthias.ringwald                 }
452ce4c8fabSmatthias.ringwald                 if (!conn) {
453ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
454ce4c8fabSmatthias.ringwald                     hci_stack.decline_reason = 0x0d;
455ce4c8fabSmatthias.ringwald                     BD_ADDR_COPY(hci_stack.decline_addr, addr);
456ce4c8fabSmatthias.ringwald                     break;
457ce4c8fabSmatthias.ringwald                 }
45832ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
45932ab9390Smatthias.ringwald                 hci_run();
46037eaa4cfSmatthias.ringwald             } else {
461ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
462ce4c8fabSmatthias.ringwald                 hci_stack.decline_reason = 0x0a;
463ce4c8fabSmatthias.ringwald                 BD_ADDR_COPY(hci_stack.decline_addr, addr);
46437eaa4cfSmatthias.ringwald             }
4651f7b95a1Smatthias.ringwald             break;
4661f7b95a1Smatthias.ringwald 
4676772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
468fe1ed1b8Smatthias.ringwald             // Connection management
469fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
4707b5fbe1fSmatthias.ringwald             log_info("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_info("\n");
4711f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
472fe1ed1b8Smatthias.ringwald             if (conn) {
473b448a0e7Smatthias.ringwald                 if (!packet[2]){
474c8e4258aSmatthias.ringwald                     conn->state = OPEN;
475fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
476ee091cf1Smatthias.ringwald 
477c785ef68Smatthias.ringwald                     // restart timer
478c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
479ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
480c785ef68Smatthias.ringwald 
4817b5fbe1fSmatthias.ringwald                     log_info("New connection: handle %u, ", conn->con_handle);
482fe1ed1b8Smatthias.ringwald                     print_bd_addr( conn->address );
4837b5fbe1fSmatthias.ringwald                     log_info("\n");
48443bfb1bdSmatthias.ringwald 
48543bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
486b448a0e7Smatthias.ringwald                 } else {
487b448a0e7Smatthias.ringwald                     // connection failed, remove entry
488b448a0e7Smatthias.ringwald                     linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
489a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
490c12e46e7Smatthias.ringwald 
491c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
492c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
493c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
494c12e46e7Smatthias.ringwald                     }
495fe1ed1b8Smatthias.ringwald                 }
496fe1ed1b8Smatthias.ringwald             }
4976772a24cSmatthias.ringwald             break;
498fe1ed1b8Smatthias.ringwald 
4997fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
5007b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
5017fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
50274ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
50332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
50464472d52Smatthias.ringwald             hci_run();
50529d53098Smatthias.ringwald             // request already answered
50629d53098Smatthias.ringwald             return;
5077fde4af9Smatthias.ringwald 
5087fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_NOTIFICATION:
5097fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
51074ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
51129d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
512287d19b8Smatthias.ringwald             hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
51329d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
5147fde4af9Smatthias.ringwald             break;
5157fde4af9Smatthias.ringwald 
5167fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
5177fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
5187fde4af9Smatthias.ringwald             break;
5197fde4af9Smatthias.ringwald 
52074ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
52174ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
52274ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
52374ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
5245a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
5255a06394aSmatthias.ringwald             for (i=0; i<248;i++){
5265a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
5275a06394aSmatthias.ringwald                     packet[9+i] = 0;
5285a06394aSmatthias.ringwald                     break;
529cdc9101dSmatthias.ringwald                 }
5305a06394aSmatthias.ringwald             }
53174ec757aSmatthias.ringwald             bzero(&device_name, sizeof(device_name_t));
53274ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
53374ec757aSmatthias.ringwald             hci_stack.remote_device_db->put_name(&addr, &device_name);
53474ec757aSmatthias.ringwald             break;
53574ec757aSmatthias.ringwald 
53674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
53774ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
53874ec757aSmatthias.ringwald             if (!hci_stack.remote_device_db) break;
53974ec757aSmatthias.ringwald             // first send inq result packet
54074ec757aSmatthias.ringwald             hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
54174ec757aSmatthias.ringwald             // then send cached remote names
54274ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
54374ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
54474ec757aSmatthias.ringwald                 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
54574ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
54674ec757aSmatthias.ringwald                 }
54774ec757aSmatthias.ringwald             }
54874ec757aSmatthias.ringwald             return;
54974ec757aSmatthias.ringwald 
5506772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
551fe1ed1b8Smatthias.ringwald             if (!packet[2]){
552fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
553fe1ed1b8Smatthias.ringwald                 hci_connection_t * conn = connection_for_handle(handle);
554fe1ed1b8Smatthias.ringwald                 if (conn) {
555c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
556fe1ed1b8Smatthias.ringwald                 }
557fe1ed1b8Smatthias.ringwald             }
5586772a24cSmatthias.ringwald             break;
5596772a24cSmatthias.ringwald 
560c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
561c68bdf90Smatthias.ringwald             if(hci_stack.control->hw_error){
562c68bdf90Smatthias.ringwald                 (*hci_stack.control->hw_error)();
563c68bdf90Smatthias.ringwald             }
564c68bdf90Smatthias.ringwald             break;
565c68bdf90Smatthias.ringwald 
5666772a24cSmatthias.ringwald         default:
5676772a24cSmatthias.ringwald             break;
568fe1ed1b8Smatthias.ringwald     }
569fe1ed1b8Smatthias.ringwald 
5703429f56bSmatthias.ringwald     // handle BT initialization
5713429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
5727301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
5737301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
5747301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
5757301ad89Smatthias.ringwald         // }
5767301ad89Smatthias.ringwald         // handle normal init sequence
5773429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
5783429f56bSmatthias.ringwald             // odd: waiting for event
5793429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
5803429f56bSmatthias.ringwald                 hci_stack.substate++;
5813429f56bSmatthias.ringwald             }
5823429f56bSmatthias.ringwald         }
58322909952Smatthias.ringwald     }
58422909952Smatthias.ringwald 
58589db417bSmatthias.ringwald     // help with BT sleep
58689db417bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
58789db417bSmatthias.ringwald         && hci_stack.substate == 1
58889db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
58989db417bSmatthias.ringwald         hci_stack.substate++;
59089db417bSmatthias.ringwald     }
59189db417bSmatthias.ringwald 
5922718e2e7Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
59394ab26f8Smatthias.ringwald 
59494ab26f8Smatthias.ringwald 	// execute main loop
59594ab26f8Smatthias.ringwald 	hci_run();
59616833f0aSmatthias.ringwald }
59716833f0aSmatthias.ringwald 
59810e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
59910e830c9Smatthias.ringwald     switch (packet_type) {
60010e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
60110e830c9Smatthias.ringwald             event_handler(packet, size);
60210e830c9Smatthias.ringwald             break;
60310e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
60410e830c9Smatthias.ringwald             acl_handler(packet, size);
60510e830c9Smatthias.ringwald             break;
60610e830c9Smatthias.ringwald         default:
60710e830c9Smatthias.ringwald             break;
60810e830c9Smatthias.ringwald     }
60910e830c9Smatthias.ringwald }
61010e830c9Smatthias.ringwald 
611fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
6122718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
6132718e2e7Smatthias.ringwald     hci_stack.packet_handler = handler;
61416833f0aSmatthias.ringwald }
61516833f0aSmatthias.ringwald 
616404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
617475c8125Smatthias.ringwald 
618475c8125Smatthias.ringwald     // reference to use transport layer implementation
61916833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
620475c8125Smatthias.ringwald 
62111e23e5fSmatthias.ringwald     // references to used control implementation
62211e23e5fSmatthias.ringwald     hci_stack.control = control;
62311e23e5fSmatthias.ringwald 
62411e23e5fSmatthias.ringwald     // reference to used config
62511e23e5fSmatthias.ringwald     hci_stack.config = config;
62611e23e5fSmatthias.ringwald 
627fe1ed1b8Smatthias.ringwald     // no connections yet
628fe1ed1b8Smatthias.ringwald     hci_stack.connections = NULL;
6290a90cc40Smatthias.ringwald     hci_stack.discoverable = 0;
630fe1ed1b8Smatthias.ringwald 
63116833f0aSmatthias.ringwald     // higher level handler
6322718e2e7Smatthias.ringwald     hci_stack.packet_handler = dummy_handler;
63316833f0aSmatthias.ringwald 
634404843c1Smatthias.ringwald     // store and open remote device db
635404843c1Smatthias.ringwald     hci_stack.remote_device_db = remote_device_db;
636404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
637404843c1Smatthias.ringwald         hci_stack.remote_device_db->open();
638404843c1Smatthias.ringwald     }
63929d53098Smatthias.ringwald 
64016833f0aSmatthias.ringwald     // register packet handlers with transport
64110e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
642475c8125Smatthias.ringwald }
643475c8125Smatthias.ringwald 
644404843c1Smatthias.ringwald void hci_close(){
645404843c1Smatthias.ringwald     // close remote device db
646404843c1Smatthias.ringwald     if (hci_stack.remote_device_db) {
647404843c1Smatthias.ringwald         hci_stack.remote_device_db->close();
648404843c1Smatthias.ringwald     }
649404843c1Smatthias.ringwald }
650404843c1Smatthias.ringwald 
6518d213e1aSmatthias.ringwald // State-Module-Driver overview
6528d213e1aSmatthias.ringwald // state                    module  low-level
6538d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
6548d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
6558d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
6568d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
657d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
658d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
659c7e0c5f6Smatthias.ringwald 
66040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
6617301ad89Smatthias.ringwald 
662038bc64cSmatthias.ringwald     // power on
663f9a30166Smatthias.ringwald     int err = 0;
664f9a30166Smatthias.ringwald     if (hci_stack.control && hci_stack.control->on){
665f9a30166Smatthias.ringwald         err = (*hci_stack.control->on)(hci_stack.config);
666f9a30166Smatthias.ringwald     }
667038bc64cSmatthias.ringwald     if (err){
6687d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
669038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
670038bc64cSmatthias.ringwald         return err;
671038bc64cSmatthias.ringwald     }
672038bc64cSmatthias.ringwald 
673038bc64cSmatthias.ringwald     // open low-level device
674038bc64cSmatthias.ringwald     err = hci_stack.hci_transport->open(hci_stack.config);
675038bc64cSmatthias.ringwald     if (err){
6767d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
677f9a30166Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
678f9a30166Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
679f9a30166Smatthias.ringwald         }
680038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
681038bc64cSmatthias.ringwald         return err;
682038bc64cSmatthias.ringwald     }
6838d213e1aSmatthias.ringwald     return 0;
6848d213e1aSmatthias.ringwald }
685038bc64cSmatthias.ringwald 
68640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
6878d213e1aSmatthias.ringwald 
6887b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
6899418f9c9Smatthias.ringwald 
6908d213e1aSmatthias.ringwald     // close low-level device
6918d213e1aSmatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
6928d213e1aSmatthias.ringwald 
6937b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
6949418f9c9Smatthias.ringwald 
6958d213e1aSmatthias.ringwald     // power off
6968d213e1aSmatthias.ringwald     if (hci_stack.control && hci_stack.control->off){
6978d213e1aSmatthias.ringwald         (*hci_stack.control->off)(hci_stack.config);
6988d213e1aSmatthias.ringwald     }
6999418f9c9Smatthias.ringwald 
7007b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
7019418f9c9Smatthias.ringwald 
70272ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_OFF;
70372ea5239Smatthias.ringwald }
70472ea5239Smatthias.ringwald 
70540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
70672ea5239Smatthias.ringwald 
7077b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
7083144bce4Smatthias.ringwald 
709b429b9b7Smatthias.ringwald #if 0
710b429b9b7Smatthias.ringwald     // don't close serial port during sleep
711b429b9b7Smatthias.ringwald 
71272ea5239Smatthias.ringwald     // close low-level device
71372ea5239Smatthias.ringwald     hci_stack.hci_transport->close(hci_stack.config);
714b429b9b7Smatthias.ringwald #endif
71572ea5239Smatthias.ringwald 
71672ea5239Smatthias.ringwald     // sleep mode
7173144bce4Smatthias.ringwald     if (hci_stack.control && hci_stack.control->sleep){
71872ea5239Smatthias.ringwald         (*hci_stack.control->sleep)(hci_stack.config);
71972ea5239Smatthias.ringwald     }
720b429b9b7Smatthias.ringwald 
72172ea5239Smatthias.ringwald     hci_stack.state = HCI_STATE_SLEEPING;
7228d213e1aSmatthias.ringwald }
7238d213e1aSmatthias.ringwald 
72440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
725b429b9b7Smatthias.ringwald 
7267b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
727b429b9b7Smatthias.ringwald 
728b429b9b7Smatthias.ringwald     // wake on
729b429b9b7Smatthias.ringwald     if (hci_stack.control && hci_stack.control->wake){
730b429b9b7Smatthias.ringwald         (*hci_stack.control->wake)(hci_stack.config);
731b429b9b7Smatthias.ringwald     }
732b429b9b7Smatthias.ringwald 
733b429b9b7Smatthias.ringwald #if 0
734b429b9b7Smatthias.ringwald     // open low-level device
735b429b9b7Smatthias.ringwald     int err = hci_stack.hci_transport->open(hci_stack.config);
736b429b9b7Smatthias.ringwald     if (err){
7377d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
738b429b9b7Smatthias.ringwald         if (hci_stack.control && hci_stack.control->off){
739b429b9b7Smatthias.ringwald             (*hci_stack.control->off)(hci_stack.config);
740b429b9b7Smatthias.ringwald         }
741b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
742b429b9b7Smatthias.ringwald         return err;
743b429b9b7Smatthias.ringwald     }
744b429b9b7Smatthias.ringwald #endif
745b429b9b7Smatthias.ringwald 
746b429b9b7Smatthias.ringwald     return 0;
747b429b9b7Smatthias.ringwald }
748b429b9b7Smatthias.ringwald 
749b429b9b7Smatthias.ringwald 
7508d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
751d661ed19Smatthias.ringwald 
7527b5fbe1fSmatthias.ringwald     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
753d661ed19Smatthias.ringwald 
7548d213e1aSmatthias.ringwald     int err = 0;
7558d213e1aSmatthias.ringwald     switch (hci_stack.state){
7568d213e1aSmatthias.ringwald 
7578d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
7588d213e1aSmatthias.ringwald             switch (power_mode){
7598d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7608d213e1aSmatthias.ringwald                     err = hci_power_control_on();
7618d213e1aSmatthias.ringwald                     if (err) return err;
7627301ad89Smatthias.ringwald                     // set up state machine
7637301ad89Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
7647301ad89Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
7657301ad89Smatthias.ringwald                     hci_stack.substate = 0;
7668d213e1aSmatthias.ringwald                     break;
7678d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7688d213e1aSmatthias.ringwald                     // do nothing
7698d213e1aSmatthias.ringwald                     break;
7708d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
771b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
7728d213e1aSmatthias.ringwald                     break;
7738d213e1aSmatthias.ringwald             }
7748d213e1aSmatthias.ringwald             break;
7757301ad89Smatthias.ringwald 
7768d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
7778d213e1aSmatthias.ringwald             switch (power_mode){
7788d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7798d213e1aSmatthias.ringwald                     // do nothing
7808d213e1aSmatthias.ringwald                     break;
7818d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
7828d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
7838d213e1aSmatthias.ringwald                     hci_power_control_off();
7848d213e1aSmatthias.ringwald                     break;
7858d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
786b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
78772ea5239Smatthias.ringwald                     hci_power_control_sleep();
7888d213e1aSmatthias.ringwald                     break;
7898d213e1aSmatthias.ringwald             }
7908d213e1aSmatthias.ringwald             break;
7917301ad89Smatthias.ringwald 
7928d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
7938d213e1aSmatthias.ringwald             switch (power_mode){
7948d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
7958d213e1aSmatthias.ringwald                     // do nothing
7968d213e1aSmatthias.ringwald                     break;
7978d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
798c7e0c5f6Smatthias.ringwald                     // see hci_run
799c7e0c5f6Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8008d213e1aSmatthias.ringwald                     break;
8018d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
802b546ac54Smatthias.ringwald                     // see hci_run
803b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
80489db417bSmatthias.ringwald                     hci_stack.substate = 0;
8058d213e1aSmatthias.ringwald                     break;
8068d213e1aSmatthias.ringwald             }
8078d213e1aSmatthias.ringwald             break;
8087301ad89Smatthias.ringwald 
8098d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
8108d213e1aSmatthias.ringwald             switch (power_mode){
8118d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8128d213e1aSmatthias.ringwald                     // set up state machine
8138d213e1aSmatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
8148d213e1aSmatthias.ringwald                     hci_stack.substate = 0;
8158d213e1aSmatthias.ringwald                     break;
8168d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8178d213e1aSmatthias.ringwald                     // do nothing
8188d213e1aSmatthias.ringwald                     break;
8198d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
820b546ac54Smatthias.ringwald                     // see hci_run
821b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_FALLING_ASLEEP;
82289db417bSmatthias.ringwald                     hci_stack.substate = 0;
8238d213e1aSmatthias.ringwald                     break;
8248d213e1aSmatthias.ringwald             }
8258d213e1aSmatthias.ringwald             break;
8268d213e1aSmatthias.ringwald 
8278d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
8288d213e1aSmatthias.ringwald             switch (power_mode){
8298d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
830b546ac54Smatthias.ringwald                     // set up state machine
831b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
832b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
833b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8348d213e1aSmatthias.ringwald                     break;
8358d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
836b546ac54Smatthias.ringwald                     // see hci_run
837b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8388d213e1aSmatthias.ringwald                     break;
8398d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
840b546ac54Smatthias.ringwald                     // do nothing
8418d213e1aSmatthias.ringwald                     break;
8428d213e1aSmatthias.ringwald             }
8438d213e1aSmatthias.ringwald             break;
8448d213e1aSmatthias.ringwald 
8458d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
8468d213e1aSmatthias.ringwald             switch (power_mode){
8478d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
8483144bce4Smatthias.ringwald                     err = hci_power_control_wake();
8493144bce4Smatthias.ringwald                     if (err) return err;
850b546ac54Smatthias.ringwald                     // set up state machine
851b546ac54Smatthias.ringwald                     hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
852b546ac54Smatthias.ringwald                     hci_stack.state = HCI_STATE_INITIALIZING;
853b546ac54Smatthias.ringwald                     hci_stack.substate = 0;
8548d213e1aSmatthias.ringwald                     break;
8558d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
8564ddb5bedSmatthias.ringwald                     hci_stack.state = HCI_STATE_HALTING;
8578d213e1aSmatthias.ringwald                     break;
8588d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
859b546ac54Smatthias.ringwald                     // do nothing
8608d213e1aSmatthias.ringwald                     break;
8618d213e1aSmatthias.ringwald             }
8628d213e1aSmatthias.ringwald             break;
86311e23e5fSmatthias.ringwald     }
86468d92d03Smatthias.ringwald 
865038bc64cSmatthias.ringwald     // create internal event
866ee8bf225Smatthias.ringwald 	hci_emit_state();
867ee8bf225Smatthias.ringwald 
86868d92d03Smatthias.ringwald 	// trigger next/first action
86968d92d03Smatthias.ringwald 	hci_run();
87068d92d03Smatthias.ringwald 
871475c8125Smatthias.ringwald     return 0;
872475c8125Smatthias.ringwald }
873475c8125Smatthias.ringwald 
874381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
875381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
876381fbed8Smatthias.ringwald 
877381fbed8Smatthias.ringwald     if (hci_stack.discoverable == enable){
878381fbed8Smatthias.ringwald         hci_emit_discoverable_enabled(hci_stack.discoverable);
879381fbed8Smatthias.ringwald         return;
880381fbed8Smatthias.ringwald     }
881381fbed8Smatthias.ringwald 
882381fbed8Smatthias.ringwald     hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan
883381fbed8Smatthias.ringwald     hci_stack.discoverable = enable;
884381fbed8Smatthias.ringwald }
885381fbed8Smatthias.ringwald 
88606b35ec0Smatthias.ringwald void hci_run(){
8878a485f27Smatthias.ringwald 
88832ab9390Smatthias.ringwald     hci_connection_t * connection;
88932ab9390Smatthias.ringwald     linked_item_t * it;
89032ab9390Smatthias.ringwald 
891ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
892ce4c8fabSmatthias.ringwald 
893ce4c8fabSmatthias.ringwald     // global/non-connection oriented commands - decline incoming connections
894ce4c8fabSmatthias.ringwald     if (hci_stack.decline_reason){
895ce4c8fabSmatthias.ringwald         uint8_t reason = hci_stack.decline_reason;
896ce4c8fabSmatthias.ringwald         hci_stack.decline_reason = 0;
897ce4c8fabSmatthias.ringwald         hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
898ce4c8fabSmatthias.ringwald     }
899ce4c8fabSmatthias.ringwald 
90032ab9390Smatthias.ringwald     // send pending HCI commands
90132ab9390Smatthias.ringwald     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
90232ab9390Smatthias.ringwald 
90332ab9390Smatthias.ringwald         connection = (hci_connection_t *) it;
90432ab9390Smatthias.ringwald 
90553b86778Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) {
9067b5fbe1fSmatthias.ringwald             // log_info("hci_run: cannot send command packet\n");
90753b86778Smatthias.ringwald             return;
90853b86778Smatthias.ringwald         }
90932ab9390Smatthias.ringwald 
91032ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
9117b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
91232ab9390Smatthias.ringwald             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
91332ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
914c7e0c5f6Smatthias.ringwald         }
915c7e0c5f6Smatthias.ringwald 
916de009a8cSmatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
91732ab9390Smatthias.ringwald 
91832ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
91932ab9390Smatthias.ringwald             link_key_t link_key;
9207b5fbe1fSmatthias.ringwald             log_info("responding to link key request\n");
92132ab9390Smatthias.ringwald             if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
92232ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
92332ab9390Smatthias.ringwald             } else {
92432ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
92532ab9390Smatthias.ringwald             }
92632ab9390Smatthias.ringwald             connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
92732ab9390Smatthias.ringwald         }
92832ab9390Smatthias.ringwald     }
92932ab9390Smatthias.ringwald 
930de009a8cSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
931c7e0c5f6Smatthias.ringwald 
9323429f56bSmatthias.ringwald     switch (hci_stack.state){
9333429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
9347b5fbe1fSmatthias.ringwald             // log_info("hci_init: substate %u\n", hci_stack.substate);
9353429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
9363429f56bSmatthias.ringwald                 // odd: waiting for command completion
93706b35ec0Smatthias.ringwald                 return;
9383429f56bSmatthias.ringwald             }
93990919203Smatthias.ringwald             switch (hci_stack.substate >> 1){
940c7492964Smatthias.ringwald                 case 0: // RESET
94122909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
942c7492964Smatthias.ringwald                     if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
943c7492964Smatthias.ringwald                         // skip baud change
944c7492964Smatthias.ringwald                         hci_stack.substate = 4; // >> 1 = 2
945c7492964Smatthias.ringwald                     }
9463429f56bSmatthias.ringwald                     break;
947c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
948c7492964Smatthias.ringwald                     hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer);
949c7492964Smatthias.ringwald                     hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]);
950c7492964Smatthias.ringwald                     break;
951c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
952c7492964Smatthias.ringwald                     hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
953c7492964Smatthias.ringwald                     hci_stack.substate += 2;
954c7492964Smatthias.ringwald                     // break missing here for fall through
955c7492964Smatthias.ringwald 
956c7492964Smatthias.ringwald                 case 3:
95790919203Smatthias.ringwald                     // custom initialization
95890919203Smatthias.ringwald                     if (hci_stack.control && hci_stack.control->next_command){
95990919203Smatthias.ringwald                         uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config);
96090919203Smatthias.ringwald                         if (cmd) {
96190919203Smatthias.ringwald                             int size = 3 + cmd[2];
962622d0de9Smatthias.ringwald                             hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size);
963c7492964Smatthias.ringwald                             hci_stack.substate = 4; // more init commands
96490919203Smatthias.ringwald                             break;
96590919203Smatthias.ringwald                         }
966c7492964Smatthias.ringwald                         printf("hci_run: init script done\n\r");
96790919203Smatthias.ringwald                     }
9682f6c30e1Smatthias.ringwald                     // otherwise continue
969f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
970f432a6ddSmatthias.ringwald 					break;
971c7492964Smatthias.ringwald 				case 4:
972e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
973e2edc0c3Smatthias.ringwald 					break;
974c7492964Smatthias.ringwald                 case 5:
9753429f56bSmatthias.ringwald                     // ca. 15 sec
9763429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
9773429f56bSmatthias.ringwald                     break;
978c7492964Smatthias.ringwald 				case 6:
9790a90cc40Smatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
980f432a6ddSmatthias.ringwald 					break;
981c7492964Smatthias.ringwald                 case 7:
9828a485f27Smatthias.ringwald #ifndef EMBEDDED
9838a485f27Smatthias.ringwald                 {
9848a485f27Smatthias.ringwald                     char hostname[30];
9858a485f27Smatthias.ringwald                     gethostname(hostname, 30);
9868a485f27Smatthias.ringwald                     hostname[29] = '\0';
9878a485f27Smatthias.ringwald                     hci_send_cmd(&hci_write_local_name, hostname);
9888a485f27Smatthias.ringwald                     break;
9898a485f27Smatthias.ringwald                 }
990c7492964Smatthias.ringwald                 case 8:
9913c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL
9923c4d4b90Smatthias.ringwald                     hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
9933c4d4b90Smatthias.ringwald                     break;
9943c4d4b90Smatthias.ringwald 
995c7492964Smatthias.ringwald                 case 9:
9963c4d4b90Smatthias.ringwald #endif
9978a485f27Smatthias.ringwald #endif
9983429f56bSmatthias.ringwald                     // done.
9993429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
1000b360b6adSmatthias.ringwald                     hci_emit_state();
10013429f56bSmatthias.ringwald                     break;
10023429f56bSmatthias.ringwald                 default:
10033429f56bSmatthias.ringwald                     break;
1004475c8125Smatthias.ringwald             }
10053429f56bSmatthias.ringwald             hci_stack.substate++;
10063429f56bSmatthias.ringwald             break;
1007c7e0c5f6Smatthias.ringwald 
1008c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1009c7e0c5f6Smatthias.ringwald 
10107b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1011c7e0c5f6Smatthias.ringwald             // close all open connections
1012c7e0c5f6Smatthias.ringwald             connection =  (hci_connection_t *) hci_stack.connections;
1013c7e0c5f6Smatthias.ringwald             if (connection){
101432ab9390Smatthias.ringwald 
1015c7e0c5f6Smatthias.ringwald                 // send disconnect
101632ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
101732ab9390Smatthias.ringwald 
10187b5fbe1fSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
10196ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1020c7e0c5f6Smatthias.ringwald 
1021c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1022c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1023c7e0c5f6Smatthias.ringwald                 return;
1024c7e0c5f6Smatthias.ringwald             }
10257b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1026c7e0c5f6Smatthias.ringwald 
102772ea5239Smatthias.ringwald             // switch mode
1028c7e0c5f6Smatthias.ringwald             hci_power_control_off();
10299418f9c9Smatthias.ringwald 
10307b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
103172ea5239Smatthias.ringwald             hci_emit_state();
10327b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
103372ea5239Smatthias.ringwald             break;
1034c7e0c5f6Smatthias.ringwald 
103572ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
103689db417bSmatthias.ringwald             switch(hci_stack.substate) {
103789db417bSmatthias.ringwald                 case 0:
10387b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
103972ea5239Smatthias.ringwald                     // close all open connections
104072ea5239Smatthias.ringwald                     connection =  (hci_connection_t *) hci_stack.connections;
104172ea5239Smatthias.ringwald                     if (connection){
104232ab9390Smatthias.ringwald 
104372ea5239Smatthias.ringwald                         // send disconnect
104432ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
104532ab9390Smatthias.ringwald 
10467b5fbe1fSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
10476ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
104872ea5239Smatthias.ringwald 
104972ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
105072ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
105172ea5239Smatthias.ringwald                         return;
105272ea5239Smatthias.ringwald                     }
105372ea5239Smatthias.ringwald 
105489db417bSmatthias.ringwald                     // disable page and inquiry scan
105532ab9390Smatthias.ringwald                     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
105632ab9390Smatthias.ringwald 
10577b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
105889db417bSmatthias.ringwald                     hci_send_cmd(&hci_write_scan_enable, 0); // none
105989db417bSmatthias.ringwald 
106089db417bSmatthias.ringwald                     // continue in next sub state
106189db417bSmatthias.ringwald                     hci_stack.substate++;
106289db417bSmatthias.ringwald                     break;
106389db417bSmatthias.ringwald                 case 1:
106489db417bSmatthias.ringwald                     // wait for command complete "hci_write_scan_enable" in event_handler();
106589db417bSmatthias.ringwald                     break;
106689db417bSmatthias.ringwald                 case 2:
10677b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
106872ea5239Smatthias.ringwald                     // switch mode
106989db417bSmatthias.ringwald                     hci_power_control_sleep();  // changes hci_stack.state to SLEEP
1070c7e0c5f6Smatthias.ringwald                     hci_emit_state();
107189db417bSmatthias.ringwald                 default:
107289db417bSmatthias.ringwald                     break;
107389db417bSmatthias.ringwald             }
1074c7e0c5f6Smatthias.ringwald             break;
1075c7e0c5f6Smatthias.ringwald 
10763429f56bSmatthias.ringwald         default:
10773429f56bSmatthias.ringwald             break;
10781f504dbdSmatthias.ringwald     }
10793429f56bSmatthias.ringwald }
108016833f0aSmatthias.ringwald 
108131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1082c8e4258aSmatthias.ringwald     bd_addr_t addr;
1083c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1084c8e4258aSmatthias.ringwald     // house-keeping
1085c8e4258aSmatthias.ringwald 
1086c8e4258aSmatthias.ringwald     // create_connection?
1087c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1088c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
10897b5fbe1fSmatthias.ringwald         log_info("Create_connection to "); print_bd_addr(addr); log_info("\n");
1090c8e4258aSmatthias.ringwald         conn = connection_for_address(addr);
1091c8e4258aSmatthias.ringwald         if (conn) {
1092c8e4258aSmatthias.ringwald             // if connection exists
1093c8e4258aSmatthias.ringwald             if (conn->state == OPEN) {
109417f1ba2aSmatthias.ringwald                 // and OPEN, emit connection complete command
109517f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, 0);
1096c8e4258aSmatthias.ringwald             }
109717f1ba2aSmatthias.ringwald             //    otherwise, just ignore as it is already in the open process
1098c8e4258aSmatthias.ringwald             return 0; // don't sent packet to controller
1099c8e4258aSmatthias.ringwald 
110017f1ba2aSmatthias.ringwald         }
1101c8e4258aSmatthias.ringwald         // create connection struct and register, state = SENT_CREATE_CONNECTION
110217f1ba2aSmatthias.ringwald         conn = create_connection_for_addr(addr);
110317f1ba2aSmatthias.ringwald         if (!conn){
110417f1ba2aSmatthias.ringwald             // notify client that alloc failed
110517f1ba2aSmatthias.ringwald             hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
110617f1ba2aSmatthias.ringwald             return 0; // don't sent packet to controller
110717f1ba2aSmatthias.ringwald         }
1108c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1109c8e4258aSmatthias.ringwald     }
1110c8e4258aSmatthias.ringwald 
11117fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
11127fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
11137fde4af9Smatthias.ringwald     }
11147fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
11157fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
11167fde4af9Smatthias.ringwald     }
11177fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_reply)){
11187fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
11197fde4af9Smatthias.ringwald     }
11207fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
11217fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
11227fde4af9Smatthias.ringwald     }
11237fde4af9Smatthias.ringwald 
11248ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
11258ef73945Smatthias.ringwald         if (hci_stack.remote_device_db){
11268ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
11278ef73945Smatthias.ringwald             hci_stack.remote_device_db->delete_link_key(&addr);
11288ef73945Smatthias.ringwald         }
11298ef73945Smatthias.ringwald     }
1130c8e4258aSmatthias.ringwald 
113131452debSmatthias.ringwald     hci_stack.num_cmd_packets--;
1132622d0de9Smatthias.ringwald     return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
113331452debSmatthias.ringwald }
11348adf0ddaSmatthias.ringwald 
11351cd208adSmatthias.ringwald /**
11361cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
11371cd208adSmatthias.ringwald  */
1138fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
11391cd208adSmatthias.ringwald     va_list argptr;
11401cd208adSmatthias.ringwald     va_start(argptr, cmd);
11411cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
11421cd208adSmatthias.ringwald     va_end(argptr);
1143124062bcSmatthias.ringwald     return hci_send_cmd_packet(hci_stack.hci_cmd_buffer, size);
114493b8dc03Smatthias.ringwald }
1145c8e4258aSmatthias.ringwald 
1146ee091cf1Smatthias.ringwald // Create various non-HCI events.
1147ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1148ee091cf1Smatthias.ringwald 
1149c8e4258aSmatthias.ringwald void hci_emit_state(){
1150425d1371Smatthias.ringwald     uint8_t event[3];
115180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1152425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1153c8e4258aSmatthias.ringwald     event[2] = hci_stack.state;
1154425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1155425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1156c8e4258aSmatthias.ringwald }
1157c8e4258aSmatthias.ringwald 
115817f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1159425d1371Smatthias.ringwald     uint8_t event[13];
1160c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1161425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
116217f1ba2aSmatthias.ringwald     event[2] = status;
1163c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1164c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1165c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1166c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1167425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1168425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1169c8e4258aSmatthias.ringwald }
1170c8e4258aSmatthias.ringwald 
11713c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1172425d1371Smatthias.ringwald     uint8_t event[6];
11733c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1174e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
11753c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
11763c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
11773c4d4b90Smatthias.ringwald     event[5] = reason;
1178425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1179425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
11803c4d4b90Smatthias.ringwald }
11813c4d4b90Smatthias.ringwald 
1182ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1183425d1371Smatthias.ringwald     uint8_t event[4];
118480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1185425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1186ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1187425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1188425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1189ee091cf1Smatthias.ringwald }
119043bfb1bdSmatthias.ringwald 
119143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1192425d1371Smatthias.ringwald     uint8_t event[3];
119380d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1194425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
119543bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1196425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1197425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
119843bfb1bdSmatthias.ringwald }
1199038bc64cSmatthias.ringwald 
1200038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1201425d1371Smatthias.ringwald     uint8_t event[2];
120280d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1203425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1204425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1205425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1206038bc64cSmatthias.ringwald }
12071b0e3922Smatthias.ringwald 
12081b0e3922Smatthias.ringwald 
12091b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1210425d1371Smatthias.ringwald     uint8_t event[6];
12111b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1212425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1213425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1214425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1215425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1216425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1217425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
12181b0e3922Smatthias.ringwald }
12191b0e3922Smatthias.ringwald 
12202ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1221425d1371Smatthias.ringwald     uint8_t event[3];
12222ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1223425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
12242ed6235cSmatthias.ringwald     event[2] = enabled;
1225425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1226e518c4b8Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
12272ed6235cSmatthias.ringwald }
1228627c2f45Smatthias.ringwald 
1229627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1230425d1371Smatthias.ringwald     uint8_t event[2+1+6+248];
1231627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1232425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1233f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
12342f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1235f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1236425d1371Smatthias.ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
1237425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1238627c2f45Smatthias.ringwald }
1239381fbed8Smatthias.ringwald 
1240381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1241425d1371Smatthias.ringwald     uint8_t event[3];
1242381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1243425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1244381fbed8Smatthias.ringwald     event[2] = enabled;
1245425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1246425d1371Smatthias.ringwald     hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1247381fbed8Smatthias.ringwald }
1248